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_SUJ, "journaled soft-updates" },
113 { MNT_MULTILABEL, "multilabel" },
114 { MNT_ACLS, "acls" },
115 { MNT_NFS4ACLS, "nfsv4acls" },
116 { MNT_GJOURNAL, "gjournal" },
121 * List of VFS types that can be remounted without becoming mounted on top
123 * XXX Is this list correct?
126 remountable_fs_names[] = {
127 "ufs", "ffs", "ext2fs",
131 static const char userquotaeq[] = "userquota=";
132 static const char groupquotaeq[] = "groupquota=";
134 static char *mountprog = NULL;
137 use_mountprog(const char *vfstype)
139 /* XXX: We need to get away from implementing external mount
140 * programs for every filesystem, and move towards having
141 * each filesystem properly implement the nmount() system call.
145 "cd9660", "mfs", "msdosfs", "nfs", "ntfs",
146 "nwfs", "nullfs", "oldnfs", "portalfs", "smbfs", "udf", "unionfs",
150 if (mountprog != NULL)
153 for (i = 0; fs[i] != NULL; ++i) {
154 if (strcmp(vfstype, fs[i]) == 0)
162 exec_mountprog(const char *name, const char *execname, char *const argv[])
167 switch (pid = fork()) {
168 case -1: /* Error. */
172 /* Go find an executable. */
173 execvP(execname, _PATH_SYSPATH, argv);
174 if (errno == ENOENT) {
175 warn("exec %s not found", execname);
176 if (execname[0] != '/') {
177 warnx("in path: %s", _PATH_SYSPATH);
181 default: /* Parent. */
182 if (waitpid(pid, &status, 0) < 0) {
187 if (WIFEXITED(status)) {
188 if (WEXITSTATUS(status) != 0)
189 return (WEXITSTATUS(status));
190 } else if (WIFSIGNALED(status)) {
191 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
201 specified_ro(const char *arg)
206 optbuf = strdup(arg);
210 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
211 if (strcmp(opt, "ro") == 0) {
226 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
228 /* Mountd is not running. */
232 if (errno != EEXIST) {
233 /* Cannot open pidfile for some reason. */
236 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
237 if (kill(mountdpid, SIGHUP) == -1)
238 err(1, "signal mountd");
242 main(int argc, char *argv[])
244 const char *mntfromname, **vfslist, *vfstype;
246 struct statfs *mntbuf;
247 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
248 char *cp, *ep, *options;
250 all = init_flags = late = 0;
255 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
267 init_flags |= MNT_FORCE;
274 options = catopt(options, optarg);
275 if (specified_ro(optarg))
284 options = catopt(options, "ro");
289 errx(1, "only one -t option may be specified");
290 vfslist = makevfslist(optarg);
294 init_flags |= MNT_UPDATE;
300 options = catopt(options, "noro");
310 #define BADTYPE(type) \
311 (strcmp(type, FSTAB_RO) && \
312 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
314 if ((init_flags & MNT_UPDATE) && (ro == 0))
315 options = catopt(options, "noro");
320 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
321 err(1, "getmntinfo");
323 while ((fs = getfsent()) != NULL) {
324 if (BADTYPE(fs->fs_type))
326 if (checkvfsname(fs->fs_vfstype, vfslist))
328 if (hasopt(fs->fs_mntops, "noauto"))
330 if (hasopt(fs->fs_mntops, "late") && !late)
332 if (hasopt(fs->fs_mntops, "failok"))
336 if (!(init_flags & MNT_UPDATE) &&
337 ismounted(fs, mntbuf, mntsize))
339 options = update_options(options, fs->fs_mntops,
341 if (mountfs(fs->fs_vfstype, fs->fs_spec,
342 fs->fs_file, init_flags, options,
343 fs->fs_mntops) && !failok)
346 } else if (fstab_style) {
347 for (i = 0; i < mntsize; i++) {
348 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
350 putfsent(&mntbuf[i]);
353 for (i = 0; i < mntsize; i++) {
354 if (checkvfsname(mntbuf[i].f_fstypename,
358 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
368 rmslashes(*argv, *argv);
369 if (init_flags & MNT_UPDATE) {
372 if ((mntbuf = getmntpt(*argv)) == NULL)
373 errx(1, "not currently mounted %s", *argv);
375 * Only get the mntflags from fstab if both mntpoint
376 * and mntspec are identical. Also handle the special
377 * case where just '/' is mounted and 'spec' is not
378 * identical with the one from fstab ('/dev' is missing
379 * in the spec-string at boot-time).
381 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
382 if (strcmp(fs->fs_spec,
383 mntbuf->f_mntfromname) == 0 &&
385 mntbuf->f_mntonname) == 0) {
387 mntfromname = mntbuf->f_mntfromname;
388 } else if (argv[0][0] == '/' &&
389 argv[0][1] == '\0') {
392 mntfromname = fs->fs_spec;
396 options = update_options(options, fs->fs_mntops,
399 mntfromname = mntbuf->f_mntfromname;
400 options = update_options(options, NULL,
403 rval = mountfs(mntbuf->f_fstypename, mntfromname,
404 mntbuf->f_mntonname, init_flags, options, 0);
407 if ((fs = getfsfile(*argv)) == NULL &&
408 (fs = getfsspec(*argv)) == NULL)
409 errx(1, "%s: unknown special file or file system",
411 if (BADTYPE(fs->fs_type))
412 errx(1, "%s has unknown file system type",
414 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
415 init_flags, options, fs->fs_mntops);
419 * If -t flag has not been specified, the path cannot be
420 * found, spec contains either a ':' or a '@', then assume
421 * that an NFS file system is being specified ala Sun.
422 * Check if the hostname contains only allowed characters
423 * to reduce false positives. IPv6 addresses containing
424 * ':' will be correctly parsed only if the separator is '@'.
425 * The definition of a valid hostname is taken from RFC 1034.
427 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
428 (ep = strchr(argv[0], ':')) != NULL)) {
431 ep = cp + strlen(cp);
435 if (!isdigit(*cp) && !isalpha(*cp) &&
436 *cp != '.' && *cp != '-' && *cp != ':')
443 rval = mountfs(vfstype,
444 argv[0], argv[1], init_flags, options, NULL);
452 * If the mount was successfully, and done by root, tell mountd the
455 if (rval == 0 && getuid() == 0)
462 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
464 char realfsfile[PATH_MAX];
467 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
468 /* the root file system can always be remounted */
471 /* The user may have specified a symlink in fstab, resolve the path */
472 if (realpath(fs->fs_file, realfsfile) == NULL) {
473 /* Cannot resolve the path, use original one */
474 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
477 for (i = mntsize - 1; i >= 0; --i)
478 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
479 (!isremountable(fs->fs_vfstype) ||
480 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
486 isremountable(const char *vfsname)
490 for (cp = remountable_fs_names; *cp; cp++)
491 if (strcmp(*cp, vfsname) == 0)
497 hasopt(const char *mntopts, const char *option)
502 if (option[0] == 'n' && option[1] == 'o') {
507 optbuf = strdup(mntopts);
509 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
510 if (opt[0] == 'n' && opt[1] == 'o') {
511 if (!strcasecmp(opt + 2, option))
513 } else if (!strcasecmp(opt, option))
521 append_arg(struct cpa *sa, char *arg)
523 if (sa->c + 1 == sa->sz) {
524 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
525 sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
527 errx(1, "realloc failed");
529 sa->a[++sa->c] = arg;
533 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
534 const char *options, const char *mntopts)
538 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
539 static struct cpa mnt_argv;
541 /* resolve the mountpoint with realpath(3) */
542 (void)checkpath(name, mntpath);
547 optbuf = catopt(strdup(mntopts), options);
549 if (strcmp(name, "/") == 0)
551 if (flags & MNT_FORCE)
552 optbuf = catopt(optbuf, "force");
553 if (flags & MNT_RDONLY)
554 optbuf = catopt(optbuf, "ro");
557 * The mount_mfs (newfs) command uses -o to select the
558 * optimization mode. We don't pass the default "-o rw"
561 if (flags & MNT_UPDATE)
562 optbuf = catopt(optbuf, "update");
564 /* Compatibility glue. */
565 if (strcmp(vfstype, "msdos") == 0) {
567 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
571 /* Construct the name of the appropriate mount command */
572 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
575 append_arg(&mnt_argv, execname);
576 mangle(optbuf, &mnt_argv);
577 if (mountprog != NULL)
578 strcpy(execname, mountprog);
580 append_arg(&mnt_argv, strdup(spec));
581 append_arg(&mnt_argv, strdup(name));
582 append_arg(&mnt_argv, NULL);
585 if (use_mountprog(vfstype))
586 printf("exec: %s", execname);
588 printf("mount -t %s", vfstype);
589 for (i = 1; i < mnt_argv.c; i++)
590 (void)printf(" %s", mnt_argv.a[i]);
598 if (use_mountprog(vfstype)) {
599 ret = exec_mountprog(name, execname, mnt_argv.a);
601 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
609 if (statfs(name, &sf) < 0) {
610 warn("statfs %s", name);
623 prmount(struct statfs *sfp)
630 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
633 flags = sfp->f_flags & MNT_VISFLAGMASK;
634 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
635 if (flags & o->o_opt) {
636 (void)printf(", %s", o->o_name);
640 * Inform when file system is mounted by an unprivileged user
641 * or privileged non-root user.
643 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
644 (void)printf(", mounted by ");
645 if ((pw = getpwuid(sfp->f_owner)) != NULL)
646 (void)printf("%s", pw->pw_name);
648 (void)printf("%d", sfp->f_owner);
651 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
652 (void)printf(", writes: sync %ju async %ju",
653 (uintmax_t)sfp->f_syncwrites,
654 (uintmax_t)sfp->f_asyncwrites);
655 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
656 (void)printf(", reads: sync %ju async %ju",
657 (uintmax_t)sfp->f_syncreads,
658 (uintmax_t)sfp->f_asyncreads);
659 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
661 for (i = 0; i < sizeof(sfp->f_fsid); i++)
662 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
669 getmntpt(const char *name)
671 struct statfs *mntbuf;
674 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
675 for (i = mntsize - 1; i >= 0; i--) {
676 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
677 strcmp(mntbuf[i].f_mntonname, name) == 0)
684 catopt(char *s0, const char *s1)
689 if (s1 == NULL || *s1 == '\0')
693 i = strlen(s0) + strlen(s1) + 1 + 1;
694 if ((cp = malloc(i)) == NULL)
695 errx(1, "malloc failed");
696 (void)snprintf(cp, i, "%s,%s", s0, s1);
706 mangle(char *options, struct cpa *a)
710 for (s = options; (p = strsep(&s, ",")) != NULL;)
712 if (strcmp(p, "noauto") == 0) {
714 * Do not pass noauto option to nmount().
715 * or external mount program. noauto is
716 * only used to prevent mounting a filesystem
717 * when 'mount -a' is specified, and is
718 * not a real mount option.
721 } else if (strcmp(p, "late") == 0) {
723 * "late" is used to prevent certain file
724 * systems from being mounted before late
725 * in the boot cycle; for instance,
726 * loopback NFS mounts can't be mounted
727 * before mountd starts.
730 } else if (strcmp(p, "failok") == 0) {
732 * "failok" is used to prevent certain file
733 * systems from being causing the system to
734 * drop into single user mode in the boot
735 * cycle, and is not a real mount option.
738 } else if (strncmp(p, "mountprog", 9) == 0) {
740 * "mountprog" is used to force the use of
741 * userland mount programs.
743 val = strchr(p, '=');
747 mountprog = strdup(val);
750 if (mountprog == NULL) {
751 errx(1, "Need value for -o mountprog");
754 } else if (strcmp(p, "userquota") == 0) {
756 } else if (strncmp(p, userquotaeq,
757 sizeof(userquotaeq) - 1) == 0) {
759 } else if (strcmp(p, "groupquota") == 0) {
761 } else if (strncmp(p, groupquotaeq,
762 sizeof(groupquotaeq) - 1) == 0) {
764 } else if (*p == '-') {
769 append_arg(a, p + 1);
772 append_arg(a, strdup("-o"));
780 update_options(char *opts, char *fstab, int curflags)
784 char *expopt, *newopt, *tmpopt;
789 /* remove meta options from list */
790 remopt(fstab, MOUNT_META_OPTION_FSTAB);
791 remopt(fstab, MOUNT_META_OPTION_CURRENT);
792 cur = flags2opts(curflags);
795 * Expand all meta-options passed to us first.
798 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
799 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
800 expopt = catopt(expopt, fstab);
801 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
802 expopt = catopt(expopt, cur);
804 expopt = catopt(expopt, o);
810 * Remove previous contradictory arguments. Given option "foo" we
811 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
812 * and "foo" - so we can deal with possible options like "notice".
815 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
816 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
817 errx(1, "malloc failed");
819 strcpy(tmpopt, "no");
821 remopt(newopt, tmpopt);
824 if (strncmp("no", o, 2) == 0)
827 newopt = catopt(newopt, o);
835 remopt(char *string, const char *opt)
839 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
844 for (p = string; (o = strsep(&p, ",")) != NULL;) {
845 if (strcmp(opt, o) != 0) {
846 if (*r == ',' && *o != '\0')
848 while ((*r++ = *o++) != '\0')
860 (void)fprintf(stderr, "%s\n%s\n%s\n",
861 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
862 " mount [-dfpruvw] special | node",
863 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
868 putfsent(struct statfs *ent)
875 /* flags2opts() doesn't return the "rw" option. */
876 if ((ent->f_flags & MNT_RDONLY) != 0)
879 rw = catopt(NULL, "rw");
881 opts = flags2opts(ent->f_flags);
882 opts = catopt(rw, opts);
884 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
885 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
886 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
890 l = strlen(ent->f_mntfromname);
891 printf("%s%s%s%s", ent->f_mntfromname,
894 l < 24 ? "\t" : " ");
895 l = strlen(ent->f_mntonname);
896 printf("%s%s%s%s", ent->f_mntonname,
899 l < 24 ? "\t" : " ");
900 printf("%s\t", ent->f_fstypename);
906 if ((fst = getfsspec(ent->f_mntfromname)))
907 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
908 else if ((fst = getfsfile(ent->f_mntonname)))
909 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
910 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
911 if (strcmp(ent->f_mntonname, "/") == 0)
921 flags2opts(int flags)
927 if (flags & MNT_RDONLY) res = catopt(res, "ro");
928 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
929 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
930 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
931 if (flags & MNT_UNION) res = catopt(res, "union");
932 if (flags & MNT_ASYNC) res = catopt(res, "async");
933 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
934 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
935 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
936 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
937 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
938 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
939 if (flags & MNT_ACLS) res = catopt(res, "acls");
940 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");