2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
37 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/mount.h>
65 #include "pathnames.h"
68 #define MOUNT_META_OPTION_FSTAB "fstab"
69 #define MOUNT_META_OPTION_CURRENT "current"
71 static int debug, fstab_style, verbose;
79 char *catopt(char *, const char *);
80 struct statfs *getmntpt(const char *);
81 int hasopt(const char *, const char *);
82 int ismounted(struct fstab *, struct statfs *, int);
83 int isremountable(const char *);
84 void mangle(char *, struct cpa *);
85 char *update_options(char *, char *, int);
86 int mountfs(const char *, const char *, const char *,
87 int, const char *, const char *);
88 void remopt(char *, const char *);
89 void prmount(struct statfs *);
90 void putfsent(struct statfs *);
92 char *flags2opts(int);
94 /* Map from mount options to printable formats. */
99 { MNT_ASYNC, "asynchronous" },
100 { MNT_EXPORTED, "NFS exported" },
101 { MNT_LOCAL, "local" },
102 { MNT_NOATIME, "noatime" },
103 { MNT_NOEXEC, "noexec" },
104 { MNT_NOSUID, "nosuid" },
105 { MNT_NOSYMFOLLOW, "nosymfollow" },
106 { MNT_QUOTA, "with quotas" },
107 { MNT_RDONLY, "read-only" },
108 { MNT_SYNCHRONOUS, "synchronous" },
109 { MNT_UNION, "union" },
110 { MNT_NOCLUSTERR, "noclusterr" },
111 { MNT_NOCLUSTERW, "noclusterw" },
112 { MNT_SUIDDIR, "suiddir" },
113 { MNT_SOFTDEP, "soft-updates" },
114 { MNT_SUJ, "journaled soft-updates" },
115 { MNT_MULTILABEL, "multilabel" },
116 { MNT_ACLS, "acls" },
117 { MNT_NFS4ACLS, "nfsv4acls" },
118 { MNT_GJOURNAL, "gjournal" },
119 { MNT_AUTOMOUNTED, "automounted" },
120 { MNT_VERIFIED, "verified" },
121 { MNT_UNTRUSTED, "untrusted" },
122 { MNT_NOCOVER, "nocover" },
123 { MNT_EMPTYDIR, "emptydir" },
128 * List of VFS types that can be remounted without becoming mounted on top
130 * XXX Is this list correct?
133 remountable_fs_names[] = {
134 "ufs", "ffs", "ext2fs",
138 static const char userquotaeq[] = "userquota=";
139 static const char groupquotaeq[] = "groupquota=";
141 static char *mountprog = NULL;
144 use_mountprog(const char *vfstype)
146 /* XXX: We need to get away from implementing external mount
147 * programs for every filesystem, and move towards having
148 * each filesystem properly implement the nmount() system call.
152 "cd9660", "mfs", "msdosfs", "nfs",
153 "nullfs", "smbfs", "udf", "unionfs",
157 if (mountprog != NULL)
160 for (i = 0; fs[i] != NULL; ++i) {
161 if (strcmp(vfstype, fs[i]) == 0)
169 exec_mountprog(const char *name, const char *execname, char *const argv[])
174 switch (pid = fork()) {
175 case -1: /* Error. */
179 /* Go find an executable. */
180 execvP(execname, _PATH_SYSPATH, argv);
181 if (errno == ENOENT) {
182 warn("exec %s not found", execname);
183 if (execname[0] != '/') {
184 warnx("in path: %s", _PATH_SYSPATH);
188 default: /* Parent. */
189 if (waitpid(pid, &status, 0) < 0) {
194 if (WIFEXITED(status)) {
195 if (WEXITSTATUS(status) != 0)
196 return (WEXITSTATUS(status));
197 } else if (WIFSIGNALED(status)) {
198 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
208 specified_ro(const char *arg)
213 optbuf = strdup(arg);
217 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
218 if (strcmp(opt, "ro") == 0) {
234 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
236 /* Mountd is not running. */
240 if (errno != EEXIST) {
241 /* Cannot open pidfile for some reason. */
246 * Refuse to send broadcast or group signals, this has
247 * happened due to the bugs in pidfile(3).
249 if (mountdpid <= 0) {
250 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
254 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
255 if (kill(mountdpid, SIGHUP) == -1)
256 err(1, "signal mountd");
260 main(int argc, char *argv[])
262 const char *mntfromname, **vfslist, *vfstype;
264 struct statfs *mntbuf;
265 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
267 char *cp, *ep, *options;
269 all = init_flags = late = onlylate = 0;
274 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
286 init_flags |= MNT_FORCE;
296 /* For compatibility with the Linux version of mount. */
300 options = catopt(options, optarg);
301 if (specified_ro(optarg))
310 options = catopt(options, "ro");
315 errx(1, "only one -t option may be specified");
316 vfslist = makevfslist(optarg);
320 init_flags |= MNT_UPDATE;
326 options = catopt(options, "noro");
336 #define BADTYPE(type) \
337 (strcmp(type, FSTAB_RO) && \
338 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
340 if ((init_flags & MNT_UPDATE) && (ro == 0))
341 options = catopt(options, "noro");
346 if ((mntsize = getmntinfo(&mntbuf,
347 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
348 err(1, "getmntinfo");
350 while ((fs = getfsent()) != NULL) {
351 if (BADTYPE(fs->fs_type))
353 if (checkvfsname(fs->fs_vfstype, vfslist))
355 if (hasopt(fs->fs_mntops, "noauto"))
357 if (!hasopt(fs->fs_mntops, "late") && onlylate)
359 if (hasopt(fs->fs_mntops, "late") && !late)
361 if (hasopt(fs->fs_mntops, "failok"))
365 if (!(init_flags & MNT_UPDATE) &&
366 !hasopt(fs->fs_mntops, "update") &&
367 ismounted(fs, mntbuf, mntsize))
369 options = update_options(options, fs->fs_mntops,
371 if (mountfs(fs->fs_vfstype, fs->fs_spec,
372 fs->fs_file, init_flags, options,
373 fs->fs_mntops) && !failok)
376 } else if (fstab_style) {
377 for (i = 0; i < mntsize; i++) {
378 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
380 putfsent(&mntbuf[i]);
383 for (i = 0; i < mntsize; i++) {
384 if (checkvfsname(mntbuf[i].f_fstypename,
388 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
398 rmslashes(*argv, *argv);
399 if (init_flags & MNT_UPDATE) {
402 if ((mntbuf = getmntpt(*argv)) == NULL)
403 errx(1, "not currently mounted %s", *argv);
405 * Only get the mntflags from fstab if both mntpoint
406 * and mntspec are identical. Also handle the special
407 * case where just '/' is mounted and 'spec' is not
408 * identical with the one from fstab ('/dev' is missing
409 * in the spec-string at boot-time).
411 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
412 if (strcmp(fs->fs_spec,
413 mntbuf->f_mntfromname) == 0 &&
415 mntbuf->f_mntonname) == 0) {
417 mntfromname = mntbuf->f_mntfromname;
418 } else if (argv[0][0] == '/' &&
419 argv[0][1] == '\0' &&
420 strcmp(fs->fs_vfstype,
421 mntbuf->f_fstypename) == 0) {
424 mntfromname = fs->fs_spec;
428 options = update_options(options, fs->fs_mntops,
431 mntfromname = mntbuf->f_mntfromname;
432 options = update_options(options, NULL,
435 rval = mountfs(mntbuf->f_fstypename, mntfromname,
436 mntbuf->f_mntonname, init_flags, options, 0);
439 if ((fs = getfsfile(*argv)) == NULL &&
440 (fs = getfsspec(*argv)) == NULL)
441 errx(1, "%s: unknown special file or file system",
443 if (BADTYPE(fs->fs_type))
444 errx(1, "%s has unknown file system type",
446 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
447 init_flags, options, fs->fs_mntops);
451 * If -t flag has not been specified, the path cannot be
452 * found, spec contains either a ':' or a '@', then assume
453 * that an NFS file system is being specified ala Sun.
454 * Check if the hostname contains only allowed characters
455 * to reduce false positives. IPv6 addresses containing
456 * ':' will be correctly parsed only if the separator is '@'.
457 * The definition of a valid hostname is taken from RFC 1034.
459 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
460 (ep = strchr(argv[0], ':')) != NULL)) {
463 ep = cp + strlen(cp);
467 if (!isdigit(*cp) && !isalpha(*cp) &&
468 *cp != '.' && *cp != '-' && *cp != ':')
475 rval = mountfs(vfstype,
476 argv[0], argv[1], init_flags, options, NULL);
484 * If the mount was successfully, and done by root, tell mountd the
487 if (rval == 0 && getuid() == 0)
494 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
496 char realfsfile[PATH_MAX];
499 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
500 /* the root file system can always be remounted */
503 /* The user may have specified a symlink in fstab, resolve the path */
504 if (realpath(fs->fs_file, realfsfile) == NULL) {
505 /* Cannot resolve the path, use original one */
506 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
510 * Consider the filesystem to be mounted if:
511 * It has the same mountpoint as a mounted filesytem, and
512 * It has the same type as that same mounted filesystem, and
513 * It has the same device name as that same mounted filesystem, OR
514 * It is a nonremountable filesystem
516 for (i = mntsize - 1; i >= 0; --i)
517 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
518 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
519 (!isremountable(fs->fs_vfstype) ||
520 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
526 isremountable(const char *vfsname)
530 for (cp = remountable_fs_names; *cp; cp++)
531 if (strcmp(*cp, vfsname) == 0)
537 hasopt(const char *mntopts, const char *option)
542 if (option[0] == 'n' && option[1] == 'o') {
547 optbuf = strdup(mntopts);
549 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
550 if (opt[0] == 'n' && opt[1] == 'o') {
551 if (!strcasecmp(opt + 2, option))
553 } else if (!strcasecmp(opt, option))
561 append_arg(struct cpa *sa, char *arg)
563 if (sa->c + 1 == sa->sz) {
564 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
565 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
567 errx(1, "realloc failed");
569 sa->a[++sa->c] = arg;
573 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
574 const char *options, const char *mntopts)
578 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
579 static struct cpa mnt_argv;
581 /* resolve the mountpoint with realpath(3) */
582 if (checkpath(name, mntpath) != 0) {
590 optbuf = catopt(strdup(mntopts), options);
592 if (strcmp(name, "/") == 0)
594 if (flags & MNT_FORCE)
595 optbuf = catopt(optbuf, "force");
596 if (flags & MNT_RDONLY)
597 optbuf = catopt(optbuf, "ro");
600 * The mount_mfs (newfs) command uses -o to select the
601 * optimization mode. We don't pass the default "-o rw"
604 if (flags & MNT_UPDATE)
605 optbuf = catopt(optbuf, "update");
607 /* Compatibility glue. */
608 if (strcmp(vfstype, "msdos") == 0)
611 /* Construct the name of the appropriate mount command */
612 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
615 append_arg(&mnt_argv, execname);
616 mangle(optbuf, &mnt_argv);
617 if (mountprog != NULL)
618 strlcpy(execname, mountprog, sizeof(execname));
620 append_arg(&mnt_argv, strdup(spec));
621 append_arg(&mnt_argv, strdup(name));
622 append_arg(&mnt_argv, NULL);
625 if (use_mountprog(vfstype))
626 printf("exec: %s", execname);
628 printf("mount -t %s", vfstype);
629 for (i = 1; i < mnt_argv.c; i++)
630 (void)printf(" %s", mnt_argv.a[i]);
638 if (use_mountprog(vfstype)) {
639 ret = exec_mountprog(name, execname, mnt_argv.a);
641 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
649 if (statfs(name, &sf) < 0) {
650 warn("statfs %s", name);
663 prmount(struct statfs *sfp)
670 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
673 flags = sfp->f_flags & MNT_VISFLAGMASK;
674 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
675 if (flags & o->o_opt) {
676 (void)printf(", %s", o->o_name);
680 * Inform when file system is mounted by an unprivileged user
681 * or privileged non-root user.
683 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
684 (void)printf(", mounted by ");
685 if ((pw = getpwuid(sfp->f_owner)) != NULL)
686 (void)printf("%s", pw->pw_name);
688 (void)printf("%d", sfp->f_owner);
691 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
692 (void)printf(", writes: sync %ju async %ju",
693 (uintmax_t)sfp->f_syncwrites,
694 (uintmax_t)sfp->f_asyncwrites);
695 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
696 (void)printf(", reads: sync %ju async %ju",
697 (uintmax_t)sfp->f_syncreads,
698 (uintmax_t)sfp->f_asyncreads);
699 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
701 for (i = 0; i < sizeof(sfp->f_fsid); i++)
702 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
709 getmntpt(const char *name)
711 struct statfs *mntbuf;
714 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
715 for (i = mntsize - 1; i >= 0; i--) {
716 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
717 strcmp(mntbuf[i].f_mntonname, name) == 0)
724 catopt(char *s0, const char *s1)
728 if (s1 == NULL || *s1 == '\0')
732 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
733 errx(1, "asprintf failed");
743 mangle(char *options, struct cpa *a)
747 for (s = options; (p = strsep(&s, ",")) != NULL;)
749 if (strcmp(p, "noauto") == 0) {
751 * Do not pass noauto option to nmount().
752 * or external mount program. noauto is
753 * only used to prevent mounting a filesystem
754 * when 'mount -a' is specified, and is
755 * not a real mount option.
758 } else if (strcmp(p, "late") == 0) {
760 * "late" is used to prevent certain file
761 * systems from being mounted before late
762 * in the boot cycle; for instance,
763 * loopback NFS mounts can't be mounted
764 * before mountd starts.
767 } else if (strcmp(p, "failok") == 0) {
769 * "failok" is used to prevent certain file
770 * systems from being causing the system to
771 * drop into single user mode in the boot
772 * cycle, and is not a real mount option.
775 } else if (strncmp(p, "mountprog", 9) == 0) {
777 * "mountprog" is used to force the use of
778 * userland mount programs.
780 val = strchr(p, '=');
784 mountprog = strdup(val);
787 if (mountprog == NULL) {
788 errx(1, "Need value for -o mountprog");
791 } else if (strcmp(p, "userquota") == 0) {
793 } else if (strncmp(p, userquotaeq,
794 sizeof(userquotaeq) - 1) == 0) {
796 } else if (strcmp(p, "groupquota") == 0) {
798 } else if (strncmp(p, groupquotaeq,
799 sizeof(groupquotaeq) - 1) == 0) {
801 } else if (*p == '-') {
806 append_arg(a, p + 1);
809 append_arg(a, strdup("-o"));
817 update_options(char *opts, char *fstab, int curflags)
821 char *expopt, *newopt, *tmpopt;
826 /* remove meta options from list */
827 remopt(fstab, MOUNT_META_OPTION_FSTAB);
828 remopt(fstab, MOUNT_META_OPTION_CURRENT);
829 cur = flags2opts(curflags);
832 * Expand all meta-options passed to us first.
835 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
836 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
837 expopt = catopt(expopt, fstab);
838 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
839 expopt = catopt(expopt, cur);
841 expopt = catopt(expopt, o);
847 * Remove previous contradictory arguments. Given option "foo" we
848 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
849 * and "foo" - so we can deal with possible options like "notice".
852 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
853 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
854 errx(1, "malloc failed");
856 strcpy(tmpopt, "no");
858 remopt(newopt, tmpopt);
861 if (strncmp("no", o, 2) == 0)
864 newopt = catopt(newopt, o);
872 remopt(char *string, const char *opt)
876 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
881 for (p = string; (o = strsep(&p, ",")) != NULL;) {
882 if (strcmp(opt, o) != 0) {
883 if (*r == ',' && *o != '\0')
885 while ((*r++ = *o++) != '\0')
897 (void)fprintf(stderr, "%s\n%s\n%s\n",
898 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
899 " mount [-dfpruvw] special | node",
900 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
905 putfsent(struct statfs *ent)
912 /* flags2opts() doesn't return the "rw" option. */
913 if ((ent->f_flags & MNT_RDONLY) != 0)
916 rw = catopt(NULL, "rw");
918 opts = flags2opts(ent->f_flags);
919 opts = catopt(rw, opts);
921 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
922 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
923 strlcpy(ent->f_mntfromname,
924 (strnstr(ent->f_mntfromname, ":", 8) +1),
925 sizeof(ent->f_mntfromname));
928 l = strlen(ent->f_mntfromname);
929 printf("%s%s%s%s", ent->f_mntfromname,
932 l < 24 ? "\t" : " ");
933 l = strlen(ent->f_mntonname);
934 printf("%s%s%s%s", ent->f_mntonname,
937 l < 24 ? "\t" : " ");
938 printf("%s\t", ent->f_fstypename);
944 if ((fst = getfsspec(ent->f_mntfromname)))
945 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
946 else if ((fst = getfsfile(ent->f_mntonname)))
947 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
948 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
949 if (strcmp(ent->f_mntonname, "/") == 0)
959 flags2opts(int flags)
965 if (flags & MNT_RDONLY) res = catopt(res, "ro");
966 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
967 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
968 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
969 if (flags & MNT_UNION) res = catopt(res, "union");
970 if (flags & MNT_ASYNC) res = catopt(res, "async");
971 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
972 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
973 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
974 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
975 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
976 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
977 if (flags & MNT_ACLS) res = catopt(res, "acls");
978 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");
979 if (flags & MNT_UNTRUSTED) res = catopt(res, "untrusted");
980 if (flags & MNT_NOCOVER) res = catopt(res, "nocover");
981 if (flags & MNT_EMPTYDIR) res = catopt(res, "emptydir");