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 static 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" },
117 { MNT_AUTOMOUNTED, "automounted" },
122 * List of VFS types that can be remounted without becoming mounted on top
124 * XXX Is this list correct?
127 remountable_fs_names[] = {
128 "ufs", "ffs", "ext2fs",
132 static const char userquotaeq[] = "userquota=";
133 static const char groupquotaeq[] = "groupquota=";
135 static char *mountprog = NULL;
138 use_mountprog(const char *vfstype)
140 /* XXX: We need to get away from implementing external mount
141 * programs for every filesystem, and move towards having
142 * each filesystem properly implement the nmount() system call.
146 "cd9660", "mfs", "msdosfs", "nfs",
147 "nullfs", "smbfs", "udf", "unionfs",
151 if (mountprog != NULL)
154 for (i = 0; fs[i] != NULL; ++i) {
155 if (strcmp(vfstype, fs[i]) == 0)
163 exec_mountprog(const char *name, const char *execname, char *const argv[])
168 switch (pid = fork()) {
169 case -1: /* Error. */
173 /* Go find an executable. */
174 execvP(execname, _PATH_SYSPATH, argv);
175 if (errno == ENOENT) {
176 warn("exec %s not found", execname);
177 if (execname[0] != '/') {
178 warnx("in path: %s", _PATH_SYSPATH);
182 default: /* Parent. */
183 if (waitpid(pid, &status, 0) < 0) {
188 if (WIFEXITED(status)) {
189 if (WEXITSTATUS(status) != 0)
190 return (WEXITSTATUS(status));
191 } else if (WIFSIGNALED(status)) {
192 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
202 specified_ro(const char *arg)
207 optbuf = strdup(arg);
211 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
212 if (strcmp(opt, "ro") == 0) {
227 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
229 /* Mountd is not running. */
233 if (errno != EEXIST) {
234 /* Cannot open pidfile for some reason. */
237 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
238 if (kill(mountdpid, SIGHUP) == -1)
239 err(1, "signal mountd");
243 main(int argc, char *argv[])
245 const char *mntfromname, **vfslist, *vfstype;
247 struct statfs *mntbuf;
248 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
250 char *cp, *ep, *options;
252 all = init_flags = late = onlylate = 0;
257 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
269 init_flags |= MNT_FORCE;
279 /* For compatibility with the Linux version of mount. */
283 options = catopt(options, optarg);
284 if (specified_ro(optarg))
293 options = catopt(options, "ro");
298 errx(1, "only one -t option may be specified");
299 vfslist = makevfslist(optarg);
303 init_flags |= MNT_UPDATE;
309 options = catopt(options, "noro");
319 #define BADTYPE(type) \
320 (strcmp(type, FSTAB_RO) && \
321 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
323 if ((init_flags & MNT_UPDATE) && (ro == 0))
324 options = catopt(options, "noro");
329 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
330 err(1, "getmntinfo");
332 while ((fs = getfsent()) != NULL) {
333 if (BADTYPE(fs->fs_type))
335 if (checkvfsname(fs->fs_vfstype, vfslist))
337 if (hasopt(fs->fs_mntops, "noauto"))
339 if (!hasopt(fs->fs_mntops, "late") && onlylate)
341 if (hasopt(fs->fs_mntops, "late") && !late)
343 if (hasopt(fs->fs_mntops, "failok"))
347 if (!(init_flags & MNT_UPDATE) &&
348 ismounted(fs, mntbuf, mntsize))
350 options = update_options(options, fs->fs_mntops,
352 if (mountfs(fs->fs_vfstype, fs->fs_spec,
353 fs->fs_file, init_flags, options,
354 fs->fs_mntops) && !failok)
357 } else if (fstab_style) {
358 for (i = 0; i < mntsize; i++) {
359 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
361 putfsent(&mntbuf[i]);
364 for (i = 0; i < mntsize; i++) {
365 if (checkvfsname(mntbuf[i].f_fstypename,
369 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
379 rmslashes(*argv, *argv);
380 if (init_flags & MNT_UPDATE) {
383 if ((mntbuf = getmntpt(*argv)) == NULL)
384 errx(1, "not currently mounted %s", *argv);
386 * Only get the mntflags from fstab if both mntpoint
387 * and mntspec are identical. Also handle the special
388 * case where just '/' is mounted and 'spec' is not
389 * identical with the one from fstab ('/dev' is missing
390 * in the spec-string at boot-time).
392 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
393 if (strcmp(fs->fs_spec,
394 mntbuf->f_mntfromname) == 0 &&
396 mntbuf->f_mntonname) == 0) {
398 mntfromname = mntbuf->f_mntfromname;
399 } else if (argv[0][0] == '/' &&
400 argv[0][1] == '\0') {
403 mntfromname = fs->fs_spec;
407 options = update_options(options, fs->fs_mntops,
410 mntfromname = mntbuf->f_mntfromname;
411 options = update_options(options, NULL,
414 rval = mountfs(mntbuf->f_fstypename, mntfromname,
415 mntbuf->f_mntonname, init_flags, options, 0);
418 if ((fs = getfsfile(*argv)) == NULL &&
419 (fs = getfsspec(*argv)) == NULL)
420 errx(1, "%s: unknown special file or file system",
422 if (BADTYPE(fs->fs_type))
423 errx(1, "%s has unknown file system type",
425 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
426 init_flags, options, fs->fs_mntops);
430 * If -t flag has not been specified, the path cannot be
431 * found, spec contains either a ':' or a '@', then assume
432 * that an NFS file system is being specified ala Sun.
433 * Check if the hostname contains only allowed characters
434 * to reduce false positives. IPv6 addresses containing
435 * ':' will be correctly parsed only if the separator is '@'.
436 * The definition of a valid hostname is taken from RFC 1034.
438 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
439 (ep = strchr(argv[0], ':')) != NULL)) {
442 ep = cp + strlen(cp);
446 if (!isdigit(*cp) && !isalpha(*cp) &&
447 *cp != '.' && *cp != '-' && *cp != ':')
454 rval = mountfs(vfstype,
455 argv[0], argv[1], init_flags, options, NULL);
463 * If the mount was successfully, and done by root, tell mountd the
466 if (rval == 0 && getuid() == 0)
473 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
475 char realfsfile[PATH_MAX];
478 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
479 /* the root file system can always be remounted */
482 /* The user may have specified a symlink in fstab, resolve the path */
483 if (realpath(fs->fs_file, realfsfile) == NULL) {
484 /* Cannot resolve the path, use original one */
485 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
489 * Consider the filesystem to be mounted if:
490 * It has the same mountpoint as a mounted filesytem, and
491 * It has the same type as that same mounted filesystem, and
492 * It has the same device name as that same mounted filesystem, OR
493 * It is a nonremountable filesystem
495 for (i = mntsize - 1; i >= 0; --i)
496 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
497 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
498 (!isremountable(fs->fs_vfstype) ||
499 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
505 isremountable(const char *vfsname)
509 for (cp = remountable_fs_names; *cp; cp++)
510 if (strcmp(*cp, vfsname) == 0)
516 hasopt(const char *mntopts, const char *option)
521 if (option[0] == 'n' && option[1] == 'o') {
526 optbuf = strdup(mntopts);
528 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
529 if (opt[0] == 'n' && opt[1] == 'o') {
530 if (!strcasecmp(opt + 2, option))
532 } else if (!strcasecmp(opt, option))
540 append_arg(struct cpa *sa, char *arg)
542 if (sa->c + 1 == sa->sz) {
543 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
544 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
546 errx(1, "realloc failed");
548 sa->a[++sa->c] = arg;
552 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
553 const char *options, const char *mntopts)
557 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
558 static struct cpa mnt_argv;
560 /* resolve the mountpoint with realpath(3) */
561 if (checkpath(name, mntpath) != 0) {
569 optbuf = catopt(strdup(mntopts), options);
571 if (strcmp(name, "/") == 0)
573 if (flags & MNT_FORCE)
574 optbuf = catopt(optbuf, "force");
575 if (flags & MNT_RDONLY)
576 optbuf = catopt(optbuf, "ro");
579 * The mount_mfs (newfs) command uses -o to select the
580 * optimization mode. We don't pass the default "-o rw"
583 if (flags & MNT_UPDATE)
584 optbuf = catopt(optbuf, "update");
586 /* Compatibility glue. */
587 if (strcmp(vfstype, "msdos") == 0) {
589 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
593 /* Construct the name of the appropriate mount command */
594 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
597 append_arg(&mnt_argv, execname);
598 mangle(optbuf, &mnt_argv);
599 if (mountprog != NULL)
600 strcpy(execname, mountprog);
602 append_arg(&mnt_argv, strdup(spec));
603 append_arg(&mnt_argv, strdup(name));
604 append_arg(&mnt_argv, NULL);
607 if (use_mountprog(vfstype))
608 printf("exec: %s", execname);
610 printf("mount -t %s", vfstype);
611 for (i = 1; i < mnt_argv.c; i++)
612 (void)printf(" %s", mnt_argv.a[i]);
620 if (use_mountprog(vfstype)) {
621 ret = exec_mountprog(name, execname, mnt_argv.a);
623 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
631 if (statfs(name, &sf) < 0) {
632 warn("statfs %s", name);
645 prmount(struct statfs *sfp)
652 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
655 flags = sfp->f_flags & MNT_VISFLAGMASK;
656 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
657 if (flags & o->o_opt) {
658 (void)printf(", %s", o->o_name);
662 * Inform when file system is mounted by an unprivileged user
663 * or privileged non-root user.
665 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
666 (void)printf(", mounted by ");
667 if ((pw = getpwuid(sfp->f_owner)) != NULL)
668 (void)printf("%s", pw->pw_name);
670 (void)printf("%d", sfp->f_owner);
673 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
674 (void)printf(", writes: sync %ju async %ju",
675 (uintmax_t)sfp->f_syncwrites,
676 (uintmax_t)sfp->f_asyncwrites);
677 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
678 (void)printf(", reads: sync %ju async %ju",
679 (uintmax_t)sfp->f_syncreads,
680 (uintmax_t)sfp->f_asyncreads);
681 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
683 for (i = 0; i < sizeof(sfp->f_fsid); i++)
684 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
691 getmntpt(const char *name)
693 struct statfs *mntbuf;
696 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
697 for (i = mntsize - 1; i >= 0; i--) {
698 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
699 strcmp(mntbuf[i].f_mntonname, name) == 0)
706 catopt(char *s0, const char *s1)
711 if (s1 == NULL || *s1 == '\0')
715 i = strlen(s0) + strlen(s1) + 1 + 1;
716 if ((cp = malloc(i)) == NULL)
717 errx(1, "malloc failed");
718 (void)snprintf(cp, i, "%s,%s", s0, s1);
728 mangle(char *options, struct cpa *a)
732 for (s = options; (p = strsep(&s, ",")) != NULL;)
734 if (strcmp(p, "noauto") == 0) {
736 * Do not pass noauto option to nmount().
737 * or external mount program. noauto is
738 * only used to prevent mounting a filesystem
739 * when 'mount -a' is specified, and is
740 * not a real mount option.
743 } else if (strcmp(p, "late") == 0) {
745 * "late" is used to prevent certain file
746 * systems from being mounted before late
747 * in the boot cycle; for instance,
748 * loopback NFS mounts can't be mounted
749 * before mountd starts.
752 } else if (strcmp(p, "failok") == 0) {
754 * "failok" is used to prevent certain file
755 * systems from being causing the system to
756 * drop into single user mode in the boot
757 * cycle, and is not a real mount option.
760 } else if (strncmp(p, "mountprog", 9) == 0) {
762 * "mountprog" is used to force the use of
763 * userland mount programs.
765 val = strchr(p, '=');
769 mountprog = strdup(val);
772 if (mountprog == NULL) {
773 errx(1, "Need value for -o mountprog");
776 } else if (strcmp(p, "userquota") == 0) {
778 } else if (strncmp(p, userquotaeq,
779 sizeof(userquotaeq) - 1) == 0) {
781 } else if (strcmp(p, "groupquota") == 0) {
783 } else if (strncmp(p, groupquotaeq,
784 sizeof(groupquotaeq) - 1) == 0) {
786 } else if (*p == '-') {
791 append_arg(a, p + 1);
794 append_arg(a, strdup("-o"));
802 update_options(char *opts, char *fstab, int curflags)
806 char *expopt, *newopt, *tmpopt;
811 /* remove meta options from list */
812 remopt(fstab, MOUNT_META_OPTION_FSTAB);
813 remopt(fstab, MOUNT_META_OPTION_CURRENT);
814 cur = flags2opts(curflags);
817 * Expand all meta-options passed to us first.
820 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
821 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
822 expopt = catopt(expopt, fstab);
823 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
824 expopt = catopt(expopt, cur);
826 expopt = catopt(expopt, o);
832 * Remove previous contradictory arguments. Given option "foo" we
833 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
834 * and "foo" - so we can deal with possible options like "notice".
837 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
838 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
839 errx(1, "malloc failed");
841 strcpy(tmpopt, "no");
843 remopt(newopt, tmpopt);
846 if (strncmp("no", o, 2) == 0)
849 newopt = catopt(newopt, o);
857 remopt(char *string, const char *opt)
861 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
866 for (p = string; (o = strsep(&p, ",")) != NULL;) {
867 if (strcmp(opt, o) != 0) {
868 if (*r == ',' && *o != '\0')
870 while ((*r++ = *o++) != '\0')
882 (void)fprintf(stderr, "%s\n%s\n%s\n",
883 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
884 " mount [-dfpruvw] special | node",
885 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
890 putfsent(struct statfs *ent)
897 /* flags2opts() doesn't return the "rw" option. */
898 if ((ent->f_flags & MNT_RDONLY) != 0)
901 rw = catopt(NULL, "rw");
903 opts = flags2opts(ent->f_flags);
904 opts = catopt(rw, opts);
906 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
907 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
908 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
912 l = strlen(ent->f_mntfromname);
913 printf("%s%s%s%s", ent->f_mntfromname,
916 l < 24 ? "\t" : " ");
917 l = strlen(ent->f_mntonname);
918 printf("%s%s%s%s", ent->f_mntonname,
921 l < 24 ? "\t" : " ");
922 printf("%s\t", ent->f_fstypename);
928 if ((fst = getfsspec(ent->f_mntfromname)))
929 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
930 else if ((fst = getfsfile(ent->f_mntonname)))
931 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
932 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
933 if (strcmp(ent->f_mntonname, "/") == 0)
943 flags2opts(int flags)
949 if (flags & MNT_RDONLY) res = catopt(res, "ro");
950 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
951 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
952 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
953 if (flags & MNT_UNION) res = catopt(res, "union");
954 if (flags & MNT_ASYNC) res = catopt(res, "async");
955 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
956 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
957 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
958 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
959 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
960 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
961 if (flags & MNT_ACLS) res = catopt(res, "acls");
962 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");