2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
35 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/mount.h>
63 #include "pathnames.h"
66 #define MOUNT_META_OPTION_FSTAB "fstab"
67 #define MOUNT_META_OPTION_CURRENT "current"
69 int debug, fstab_style, verbose;
77 char *catopt(char *, const char *);
78 struct statfs *getmntpt(const char *);
79 int hasopt(const char *, const char *);
80 int ismounted(struct fstab *, struct statfs *, int);
81 int isremountable(const char *);
82 void mangle(char *, struct cpa *);
83 char *update_options(char *, char *, int);
84 int mountfs(const char *, const char *, const char *,
85 int, const char *, const char *);
86 void remopt(char *, const char *);
87 void prmount(struct statfs *);
88 void putfsent(struct statfs *);
90 char *flags2opts(int);
92 /* Map from mount options to printable formats. */
97 { MNT_ASYNC, "asynchronous" },
98 { MNT_EXPORTED, "NFS exported" },
99 { MNT_LOCAL, "local" },
100 { MNT_NOATIME, "noatime" },
101 { MNT_NOEXEC, "noexec" },
102 { MNT_NOSUID, "nosuid" },
103 { MNT_NOSYMFOLLOW, "nosymfollow" },
104 { MNT_QUOTA, "with quotas" },
105 { MNT_RDONLY, "read-only" },
106 { MNT_SYNCHRONOUS, "synchronous" },
107 { MNT_UNION, "union" },
108 { MNT_NOCLUSTERR, "noclusterr" },
109 { MNT_NOCLUSTERW, "noclusterw" },
110 { MNT_SUIDDIR, "suiddir" },
111 { MNT_SOFTDEP, "soft-updates" },
112 { MNT_MULTILABEL, "multilabel" },
113 { MNT_ACLS, "acls" },
114 { MNT_NFS4ACLS, "nfsv4acls" },
115 { MNT_GJOURNAL, "gjournal" },
120 * List of VFS types that can be remounted without becoming mounted on top
122 * XXX Is this list correct?
125 remountable_fs_names[] = {
126 "ufs", "ffs", "ext2fs",
130 static const char userquotaeq[] = "userquota=";
131 static const char groupquotaeq[] = "groupquota=";
133 static char *mountprog = NULL;
136 use_mountprog(const char *vfstype)
138 /* XXX: We need to get away from implementing external mount
139 * programs for every filesystem, and move towards having
140 * each filesystem properly implement the nmount() system call.
144 "cd9660", "mfs", "msdosfs", "newnfs", "nfs", "ntfs",
145 "nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs",
149 if (mountprog != NULL)
152 for (i = 0; fs[i] != NULL; ++i) {
153 if (strcmp(vfstype, fs[i]) == 0)
161 exec_mountprog(const char *name, const char *execname, char *const argv[])
166 switch (pid = fork()) {
167 case -1: /* Error. */
171 /* Go find an executable. */
172 execvP(execname, _PATH_SYSPATH, argv);
173 if (errno == ENOENT) {
174 warn("exec %s not found", execname);
175 if (execname[0] != '/') {
176 warnx("in path: %s", _PATH_SYSPATH);
180 default: /* Parent. */
181 if (waitpid(pid, &status, 0) < 0) {
186 if (WIFEXITED(status)) {
187 if (WEXITSTATUS(status) != 0)
188 return (WEXITSTATUS(status));
189 } else if (WIFSIGNALED(status)) {
190 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
200 specified_ro(const char *arg)
205 optbuf = strdup(arg);
209 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
210 if (strcmp(opt, "ro") == 0) {
225 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
227 /* Mountd is not running. */
231 if (errno != EEXIST) {
232 /* Cannot open pidfile for some reason. */
235 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
236 if (kill(mountdpid, SIGHUP) == -1)
237 err(1, "signal mountd");
241 main(int argc, char *argv[])
243 const char *mntfromname, **vfslist, *vfstype;
245 struct statfs *mntbuf;
246 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
247 char *cp, *ep, *options;
249 all = init_flags = late = 0;
254 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
266 init_flags |= MNT_FORCE;
273 options = catopt(options, optarg);
274 if (specified_ro(optarg))
283 options = catopt(options, "ro");
288 errx(1, "only one -t option may be specified");
289 vfslist = makevfslist(optarg);
293 init_flags |= MNT_UPDATE;
299 options = catopt(options, "noro");
309 #define BADTYPE(type) \
310 (strcmp(type, FSTAB_RO) && \
311 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
313 if ((init_flags & MNT_UPDATE) && (ro == 0))
314 options = catopt(options, "noro");
319 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
320 err(1, "getmntinfo");
322 while ((fs = getfsent()) != NULL) {
323 if (BADTYPE(fs->fs_type))
325 if (checkvfsname(fs->fs_vfstype, vfslist))
327 if (hasopt(fs->fs_mntops, "noauto"))
329 if (hasopt(fs->fs_mntops, "late") && !late)
331 if (hasopt(fs->fs_mntops, "failok"))
335 if (!(init_flags & MNT_UPDATE) &&
336 ismounted(fs, mntbuf, mntsize))
338 options = update_options(options, fs->fs_mntops,
340 if (mountfs(fs->fs_vfstype, fs->fs_spec,
341 fs->fs_file, init_flags, options,
342 fs->fs_mntops) && !failok)
345 } else if (fstab_style) {
346 for (i = 0; i < mntsize; i++) {
347 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
349 putfsent(&mntbuf[i]);
352 for (i = 0; i < mntsize; i++) {
353 if (checkvfsname(mntbuf[i].f_fstypename,
357 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
367 rmslashes(*argv, *argv);
368 if (init_flags & MNT_UPDATE) {
371 if ((mntbuf = getmntpt(*argv)) == NULL)
372 errx(1, "not currently mounted %s", *argv);
374 * Only get the mntflags from fstab if both mntpoint
375 * and mntspec are identical. Also handle the special
376 * case where just '/' is mounted and 'spec' is not
377 * identical with the one from fstab ('/dev' is missing
378 * in the spec-string at boot-time).
380 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
381 if (strcmp(fs->fs_spec,
382 mntbuf->f_mntfromname) == 0 &&
384 mntbuf->f_mntonname) == 0) {
386 mntfromname = mntbuf->f_mntfromname;
387 } else if (argv[0][0] == '/' &&
388 argv[0][1] == '\0') {
391 mntfromname = fs->fs_spec;
395 options = update_options(options, fs->fs_mntops,
398 mntfromname = mntbuf->f_mntfromname;
399 options = update_options(options, NULL,
402 rval = mountfs(mntbuf->f_fstypename, mntfromname,
403 mntbuf->f_mntonname, init_flags, options, 0);
406 if ((fs = getfsfile(*argv)) == NULL &&
407 (fs = getfsspec(*argv)) == NULL)
408 errx(1, "%s: unknown special file or file system",
410 if (BADTYPE(fs->fs_type))
411 errx(1, "%s has unknown file system type",
413 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
414 init_flags, options, fs->fs_mntops);
418 * If -t flag has not been specified, the path cannot be
419 * found, spec contains either a ':' or a '@', then assume
420 * that an NFS file system is being specified ala Sun.
421 * Check if the hostname contains only allowed characters
422 * to reduce false positives. IPv6 addresses containing
423 * ':' will be correctly parsed only if the separator is '@'.
424 * The definition of a valid hostname is taken from RFC 1034.
426 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
427 (ep = strchr(argv[0], ':')) != NULL)) {
430 ep = cp + strlen(cp);
434 if (!isdigit(*cp) && !isalpha(*cp) &&
435 *cp != '.' && *cp != '-' && *cp != ':')
442 rval = mountfs(vfstype,
443 argv[0], argv[1], init_flags, options, NULL);
451 * If the mount was successfully, and done by root, tell mountd the
454 if (rval == 0 && getuid() == 0)
461 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
463 char realfsfile[PATH_MAX];
466 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
467 /* the root file system can always be remounted */
470 /* The user may have specified a symlink in fstab, resolve the path */
471 if (realpath(fs->fs_file, realfsfile) == NULL) {
472 /* Cannot resolve the path, use original one */
473 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
476 for (i = mntsize - 1; i >= 0; --i)
477 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
478 (!isremountable(fs->fs_vfstype) ||
479 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
485 isremountable(const char *vfsname)
489 for (cp = remountable_fs_names; *cp; cp++)
490 if (strcmp(*cp, vfsname) == 0)
496 hasopt(const char *mntopts, const char *option)
501 if (option[0] == 'n' && option[1] == 'o') {
506 optbuf = strdup(mntopts);
508 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
509 if (opt[0] == 'n' && opt[1] == 'o') {
510 if (!strcasecmp(opt + 2, option))
512 } else if (!strcasecmp(opt, option))
520 append_arg(struct cpa *sa, char *arg)
522 if (sa->c + 1 == sa->sz) {
523 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
524 sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
526 errx(1, "realloc failed");
528 sa->a[++sa->c] = arg;
532 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
533 const char *options, const char *mntopts)
537 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
538 static struct cpa mnt_argv;
540 /* resolve the mountpoint with realpath(3) */
541 (void)checkpath(name, mntpath);
546 optbuf = catopt(strdup(mntopts), options);
548 if (strcmp(name, "/") == 0)
550 if (flags & MNT_FORCE)
551 optbuf = catopt(optbuf, "force");
552 if (flags & MNT_RDONLY)
553 optbuf = catopt(optbuf, "ro");
556 * The mount_mfs (newfs) command uses -o to select the
557 * optimization mode. We don't pass the default "-o rw"
560 if (flags & MNT_UPDATE)
561 optbuf = catopt(optbuf, "update");
563 /* Compatibility glue. */
564 if (strcmp(vfstype, "msdos") == 0) {
566 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
570 /* Construct the name of the appropriate mount command */
571 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
574 append_arg(&mnt_argv, execname);
575 mangle(optbuf, &mnt_argv);
576 if (mountprog != NULL)
577 strcpy(execname, mountprog);
579 append_arg(&mnt_argv, strdup(spec));
580 append_arg(&mnt_argv, strdup(name));
581 append_arg(&mnt_argv, NULL);
584 if (use_mountprog(vfstype))
585 printf("exec: %s", execname);
587 printf("mount -t %s", vfstype);
588 for (i = 1; i < mnt_argv.c; i++)
589 (void)printf(" %s", mnt_argv.a[i]);
597 if (use_mountprog(vfstype)) {
598 ret = exec_mountprog(name, execname, mnt_argv.a);
600 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
608 if (statfs(name, &sf) < 0) {
609 warn("statfs %s", name);
622 prmount(struct statfs *sfp)
629 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
632 flags = sfp->f_flags & MNT_VISFLAGMASK;
633 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
634 if (flags & o->o_opt) {
635 (void)printf(", %s", o->o_name);
639 * Inform when file system is mounted by an unprivileged user
640 * or privileged non-root user.
642 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
643 (void)printf(", mounted by ");
644 if ((pw = getpwuid(sfp->f_owner)) != NULL)
645 (void)printf("%s", pw->pw_name);
647 (void)printf("%d", sfp->f_owner);
650 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
651 (void)printf(", writes: sync %ju async %ju",
652 (uintmax_t)sfp->f_syncwrites,
653 (uintmax_t)sfp->f_asyncwrites);
654 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
655 (void)printf(", reads: sync %ju async %ju",
656 (uintmax_t)sfp->f_syncreads,
657 (uintmax_t)sfp->f_asyncreads);
658 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
660 for (i = 0; i < sizeof(sfp->f_fsid); i++)
661 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
668 getmntpt(const char *name)
670 struct statfs *mntbuf;
673 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
674 for (i = mntsize - 1; i >= 0; i--) {
675 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
676 strcmp(mntbuf[i].f_mntonname, name) == 0)
683 catopt(char *s0, const char *s1)
688 if (s1 == NULL || *s1 == '\0')
692 i = strlen(s0) + strlen(s1) + 1 + 1;
693 if ((cp = malloc(i)) == NULL)
694 errx(1, "malloc failed");
695 (void)snprintf(cp, i, "%s,%s", s0, s1);
705 mangle(char *options, struct cpa *a)
709 for (s = options; (p = strsep(&s, ",")) != NULL;)
711 if (strcmp(p, "noauto") == 0) {
713 * Do not pass noauto option to nmount().
714 * or external mount program. noauto is
715 * only used to prevent mounting a filesystem
716 * when 'mount -a' is specified, and is
717 * not a real mount option.
720 } else if (strcmp(p, "late") == 0) {
722 * "late" is used to prevent certain file
723 * systems from being mounted before late
724 * in the boot cycle; for instance,
725 * loopback NFS mounts can't be mounted
726 * before mountd starts.
729 } else if (strcmp(p, "failok") == 0) {
731 * "failok" is used to prevent certain file
732 * systems from being causing the system to
733 * drop into single user mode in the boot
734 * cycle, and is not a real mount option.
737 } else if (strncmp(p, "mountprog", 9) == 0) {
739 * "mountprog" is used to force the use of
740 * userland mount programs.
742 val = strchr(p, '=');
746 mountprog = strdup(val);
749 if (mountprog == NULL) {
750 errx(1, "Need value for -o mountprog");
753 } else if (strcmp(p, "userquota") == 0) {
755 } else if (strncmp(p, userquotaeq,
756 sizeof(userquotaeq) - 1) == 0) {
758 } else if (strcmp(p, "groupquota") == 0) {
760 } else if (strncmp(p, groupquotaeq,
761 sizeof(groupquotaeq) - 1) == 0) {
763 } else if (*p == '-') {
768 append_arg(a, p + 1);
771 append_arg(a, strdup("-o"));
779 update_options(char *opts, char *fstab, int curflags)
783 char *expopt, *newopt, *tmpopt;
788 /* remove meta options from list */
789 remopt(fstab, MOUNT_META_OPTION_FSTAB);
790 remopt(fstab, MOUNT_META_OPTION_CURRENT);
791 cur = flags2opts(curflags);
794 * Expand all meta-options passed to us first.
797 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
798 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
799 expopt = catopt(expopt, fstab);
800 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
801 expopt = catopt(expopt, cur);
803 expopt = catopt(expopt, o);
809 * Remove previous contradictory arguments. Given option "foo" we
810 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
811 * and "foo" - so we can deal with possible options like "notice".
814 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
815 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
816 errx(1, "malloc failed");
818 strcpy(tmpopt, "no");
820 remopt(newopt, tmpopt);
823 if (strncmp("no", o, 2) == 0)
826 newopt = catopt(newopt, o);
834 remopt(char *string, const char *opt)
838 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
843 for (p = string; (o = strsep(&p, ",")) != NULL;) {
844 if (strcmp(opt, o) != 0) {
845 if (*r == ',' && *o != '\0')
847 while ((*r++ = *o++) != '\0')
859 (void)fprintf(stderr, "%s\n%s\n%s\n",
860 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
861 " mount [-dfpruvw] special | node",
862 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
867 putfsent(struct statfs *ent)
874 /* flags2opts() doesn't return the "rw" option. */
875 if ((ent->f_flags & MNT_RDONLY) != 0)
878 rw = catopt(NULL, "rw");
880 opts = flags2opts(ent->f_flags);
881 opts = catopt(rw, opts);
883 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
884 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
885 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
889 l = strlen(ent->f_mntfromname);
890 printf("%s%s%s%s", ent->f_mntfromname,
893 l < 24 ? "\t" : " ");
894 l = strlen(ent->f_mntonname);
895 printf("%s%s%s%s", ent->f_mntonname,
898 l < 24 ? "\t" : " ");
899 printf("%s\t", ent->f_fstypename);
905 if ((fst = getfsspec(ent->f_mntfromname)))
906 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
907 else if ((fst = getfsfile(ent->f_mntonname)))
908 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
909 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
910 if (strcmp(ent->f_mntonname, "/") == 0)
920 flags2opts(int flags)
926 if (flags & MNT_RDONLY) res = catopt(res, "ro");
927 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
928 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
929 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
930 if (flags & MNT_UNION) res = catopt(res, "union");
931 if (flags & MNT_ASYNC) res = catopt(res, "async");
932 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
933 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
934 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
935 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
936 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
937 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
938 if (flags & MNT_ACLS) res = catopt(res, "acls");
939 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");