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" },
125 * List of VFS types that can be remounted without becoming mounted on top
127 * XXX Is this list correct?
130 remountable_fs_names[] = {
131 "ufs", "ffs", "ext2fs",
135 static const char userquotaeq[] = "userquota=";
136 static const char groupquotaeq[] = "groupquota=";
138 static char *mountprog = NULL;
141 use_mountprog(const char *vfstype)
143 /* XXX: We need to get away from implementing external mount
144 * programs for every filesystem, and move towards having
145 * each filesystem properly implement the nmount() system call.
149 "cd9660", "mfs", "msdosfs", "nfs",
150 "nullfs", "smbfs", "udf", "unionfs",
154 if (mountprog != NULL)
157 for (i = 0; fs[i] != NULL; ++i) {
158 if (strcmp(vfstype, fs[i]) == 0)
166 exec_mountprog(const char *name, const char *execname, char *const argv[])
171 switch (pid = fork()) {
172 case -1: /* Error. */
176 /* Go find an executable. */
177 execvP(execname, _PATH_SYSPATH, argv);
178 if (errno == ENOENT) {
179 warn("exec %s not found", execname);
180 if (execname[0] != '/') {
181 warnx("in path: %s", _PATH_SYSPATH);
185 default: /* Parent. */
186 if (waitpid(pid, &status, 0) < 0) {
191 if (WIFEXITED(status)) {
192 if (WEXITSTATUS(status) != 0)
193 return (WEXITSTATUS(status));
194 } else if (WIFSIGNALED(status)) {
195 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
205 specified_ro(const char *arg)
210 optbuf = strdup(arg);
214 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
215 if (strcmp(opt, "ro") == 0) {
230 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
232 /* Mountd is not running. */
236 if (errno != EEXIST) {
237 /* Cannot open pidfile for some reason. */
240 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
241 if (kill(mountdpid, SIGHUP) == -1)
242 err(1, "signal mountd");
246 main(int argc, char *argv[])
248 const char *mntfromname, **vfslist, *vfstype;
250 struct statfs *mntbuf;
251 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
253 char *cp, *ep, *options;
255 all = init_flags = late = onlylate = 0;
260 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
272 init_flags |= MNT_FORCE;
282 /* For compatibility with the Linux version of mount. */
286 options = catopt(options, optarg);
287 if (specified_ro(optarg))
296 options = catopt(options, "ro");
301 errx(1, "only one -t option may be specified");
302 vfslist = makevfslist(optarg);
306 init_flags |= MNT_UPDATE;
312 options = catopt(options, "noro");
322 #define BADTYPE(type) \
323 (strcmp(type, FSTAB_RO) && \
324 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
326 if ((init_flags & MNT_UPDATE) && (ro == 0))
327 options = catopt(options, "noro");
332 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
333 err(1, "getmntinfo");
335 while ((fs = getfsent()) != NULL) {
336 if (BADTYPE(fs->fs_type))
338 if (checkvfsname(fs->fs_vfstype, vfslist))
340 if (hasopt(fs->fs_mntops, "noauto"))
342 if (!hasopt(fs->fs_mntops, "late") && onlylate)
344 if (hasopt(fs->fs_mntops, "late") && !late)
346 if (hasopt(fs->fs_mntops, "failok"))
350 if (!(init_flags & MNT_UPDATE) &&
351 ismounted(fs, mntbuf, mntsize))
353 options = update_options(options, fs->fs_mntops,
355 if (mountfs(fs->fs_vfstype, fs->fs_spec,
356 fs->fs_file, init_flags, options,
357 fs->fs_mntops) && !failok)
360 } else if (fstab_style) {
361 for (i = 0; i < mntsize; i++) {
362 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
364 putfsent(&mntbuf[i]);
367 for (i = 0; i < mntsize; i++) {
368 if (checkvfsname(mntbuf[i].f_fstypename,
372 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
382 rmslashes(*argv, *argv);
383 if (init_flags & MNT_UPDATE) {
386 if ((mntbuf = getmntpt(*argv)) == NULL)
387 errx(1, "not currently mounted %s", *argv);
389 * Only get the mntflags from fstab if both mntpoint
390 * and mntspec are identical. Also handle the special
391 * case where just '/' is mounted and 'spec' is not
392 * identical with the one from fstab ('/dev' is missing
393 * in the spec-string at boot-time).
395 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
396 if (strcmp(fs->fs_spec,
397 mntbuf->f_mntfromname) == 0 &&
399 mntbuf->f_mntonname) == 0) {
401 mntfromname = mntbuf->f_mntfromname;
402 } else if (argv[0][0] == '/' &&
403 argv[0][1] == '\0' &&
404 strcmp(fs->fs_vfstype,
405 mntbuf->f_fstypename) == 0) {
408 mntfromname = fs->fs_spec;
412 options = update_options(options, fs->fs_mntops,
415 mntfromname = mntbuf->f_mntfromname;
416 options = update_options(options, NULL,
419 rval = mountfs(mntbuf->f_fstypename, mntfromname,
420 mntbuf->f_mntonname, init_flags, options, 0);
423 if ((fs = getfsfile(*argv)) == NULL &&
424 (fs = getfsspec(*argv)) == NULL)
425 errx(1, "%s: unknown special file or file system",
427 if (BADTYPE(fs->fs_type))
428 errx(1, "%s has unknown file system type",
430 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
431 init_flags, options, fs->fs_mntops);
435 * If -t flag has not been specified, the path cannot be
436 * found, spec contains either a ':' or a '@', then assume
437 * that an NFS file system is being specified ala Sun.
438 * Check if the hostname contains only allowed characters
439 * to reduce false positives. IPv6 addresses containing
440 * ':' will be correctly parsed only if the separator is '@'.
441 * The definition of a valid hostname is taken from RFC 1034.
443 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
444 (ep = strchr(argv[0], ':')) != NULL)) {
447 ep = cp + strlen(cp);
451 if (!isdigit(*cp) && !isalpha(*cp) &&
452 *cp != '.' && *cp != '-' && *cp != ':')
459 rval = mountfs(vfstype,
460 argv[0], argv[1], init_flags, options, NULL);
468 * If the mount was successfully, and done by root, tell mountd the
471 if (rval == 0 && getuid() == 0)
478 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
480 char realfsfile[PATH_MAX];
483 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
484 /* the root file system can always be remounted */
487 /* The user may have specified a symlink in fstab, resolve the path */
488 if (realpath(fs->fs_file, realfsfile) == NULL) {
489 /* Cannot resolve the path, use original one */
490 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
494 * Consider the filesystem to be mounted if:
495 * It has the same mountpoint as a mounted filesytem, and
496 * It has the same type as that same mounted filesystem, and
497 * It has the same device name as that same mounted filesystem, OR
498 * It is a nonremountable filesystem
500 for (i = mntsize - 1; i >= 0; --i)
501 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
502 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
503 (!isremountable(fs->fs_vfstype) ||
504 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
510 isremountable(const char *vfsname)
514 for (cp = remountable_fs_names; *cp; cp++)
515 if (strcmp(*cp, vfsname) == 0)
521 hasopt(const char *mntopts, const char *option)
526 if (option[0] == 'n' && option[1] == 'o') {
531 optbuf = strdup(mntopts);
533 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
534 if (opt[0] == 'n' && opt[1] == 'o') {
535 if (!strcasecmp(opt + 2, option))
537 } else if (!strcasecmp(opt, option))
545 append_arg(struct cpa *sa, char *arg)
547 if (sa->c + 1 == sa->sz) {
548 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
549 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
551 errx(1, "realloc failed");
553 sa->a[++sa->c] = arg;
557 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
558 const char *options, const char *mntopts)
562 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
563 static struct cpa mnt_argv;
565 /* resolve the mountpoint with realpath(3) */
566 if (checkpath(name, mntpath) != 0) {
574 optbuf = catopt(strdup(mntopts), options);
576 if (strcmp(name, "/") == 0)
578 if (flags & MNT_FORCE)
579 optbuf = catopt(optbuf, "force");
580 if (flags & MNT_RDONLY)
581 optbuf = catopt(optbuf, "ro");
584 * The mount_mfs (newfs) command uses -o to select the
585 * optimization mode. We don't pass the default "-o rw"
588 if (flags & MNT_UPDATE)
589 optbuf = catopt(optbuf, "update");
591 /* Compatibility glue. */
592 if (strcmp(vfstype, "msdos") == 0)
595 /* Construct the name of the appropriate mount command */
596 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
599 append_arg(&mnt_argv, execname);
600 mangle(optbuf, &mnt_argv);
601 if (mountprog != NULL)
602 strlcpy(execname, mountprog, sizeof(execname));
604 append_arg(&mnt_argv, strdup(spec));
605 append_arg(&mnt_argv, strdup(name));
606 append_arg(&mnt_argv, NULL);
609 if (use_mountprog(vfstype))
610 printf("exec: %s", execname);
612 printf("mount -t %s", vfstype);
613 for (i = 1; i < mnt_argv.c; i++)
614 (void)printf(" %s", mnt_argv.a[i]);
622 if (use_mountprog(vfstype)) {
623 ret = exec_mountprog(name, execname, mnt_argv.a);
625 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
633 if (statfs(name, &sf) < 0) {
634 warn("statfs %s", name);
647 prmount(struct statfs *sfp)
654 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
657 flags = sfp->f_flags & MNT_VISFLAGMASK;
658 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
659 if (flags & o->o_opt) {
660 (void)printf(", %s", o->o_name);
664 * Inform when file system is mounted by an unprivileged user
665 * or privileged non-root user.
667 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
668 (void)printf(", mounted by ");
669 if ((pw = getpwuid(sfp->f_owner)) != NULL)
670 (void)printf("%s", pw->pw_name);
672 (void)printf("%d", sfp->f_owner);
675 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
676 (void)printf(", writes: sync %ju async %ju",
677 (uintmax_t)sfp->f_syncwrites,
678 (uintmax_t)sfp->f_asyncwrites);
679 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
680 (void)printf(", reads: sync %ju async %ju",
681 (uintmax_t)sfp->f_syncreads,
682 (uintmax_t)sfp->f_asyncreads);
683 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
685 for (i = 0; i < sizeof(sfp->f_fsid); i++)
686 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
693 getmntpt(const char *name)
695 struct statfs *mntbuf;
698 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
699 for (i = mntsize - 1; i >= 0; i--) {
700 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
701 strcmp(mntbuf[i].f_mntonname, name) == 0)
708 catopt(char *s0, const char *s1)
712 if (s1 == NULL || *s1 == '\0')
716 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
717 errx(1, "asprintf failed");
727 mangle(char *options, struct cpa *a)
731 for (s = options; (p = strsep(&s, ",")) != NULL;)
733 if (strcmp(p, "noauto") == 0) {
735 * Do not pass noauto option to nmount().
736 * or external mount program. noauto is
737 * only used to prevent mounting a filesystem
738 * when 'mount -a' is specified, and is
739 * not a real mount option.
742 } else if (strcmp(p, "late") == 0) {
744 * "late" is used to prevent certain file
745 * systems from being mounted before late
746 * in the boot cycle; for instance,
747 * loopback NFS mounts can't be mounted
748 * before mountd starts.
751 } else if (strcmp(p, "failok") == 0) {
753 * "failok" is used to prevent certain file
754 * systems from being causing the system to
755 * drop into single user mode in the boot
756 * cycle, and is not a real mount option.
759 } else if (strncmp(p, "mountprog", 9) == 0) {
761 * "mountprog" is used to force the use of
762 * userland mount programs.
764 val = strchr(p, '=');
768 mountprog = strdup(val);
771 if (mountprog == NULL) {
772 errx(1, "Need value for -o mountprog");
775 } else if (strcmp(p, "userquota") == 0) {
777 } else if (strncmp(p, userquotaeq,
778 sizeof(userquotaeq) - 1) == 0) {
780 } else if (strcmp(p, "groupquota") == 0) {
782 } else if (strncmp(p, groupquotaeq,
783 sizeof(groupquotaeq) - 1) == 0) {
785 } else if (*p == '-') {
790 append_arg(a, p + 1);
793 append_arg(a, strdup("-o"));
801 update_options(char *opts, char *fstab, int curflags)
805 char *expopt, *newopt, *tmpopt;
810 /* remove meta options from list */
811 remopt(fstab, MOUNT_META_OPTION_FSTAB);
812 remopt(fstab, MOUNT_META_OPTION_CURRENT);
813 cur = flags2opts(curflags);
816 * Expand all meta-options passed to us first.
819 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
820 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
821 expopt = catopt(expopt, fstab);
822 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
823 expopt = catopt(expopt, cur);
825 expopt = catopt(expopt, o);
831 * Remove previous contradictory arguments. Given option "foo" we
832 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
833 * and "foo" - so we can deal with possible options like "notice".
836 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
837 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
838 errx(1, "malloc failed");
840 strcpy(tmpopt, "no");
842 remopt(newopt, tmpopt);
845 if (strncmp("no", o, 2) == 0)
848 newopt = catopt(newopt, o);
856 remopt(char *string, const char *opt)
860 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
865 for (p = string; (o = strsep(&p, ",")) != NULL;) {
866 if (strcmp(opt, o) != 0) {
867 if (*r == ',' && *o != '\0')
869 while ((*r++ = *o++) != '\0')
881 (void)fprintf(stderr, "%s\n%s\n%s\n",
882 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
883 " mount [-dfpruvw] special | node",
884 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
889 putfsent(struct statfs *ent)
896 /* flags2opts() doesn't return the "rw" option. */
897 if ((ent->f_flags & MNT_RDONLY) != 0)
900 rw = catopt(NULL, "rw");
902 opts = flags2opts(ent->f_flags);
903 opts = catopt(rw, opts);
905 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
906 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
907 strlcpy(ent->f_mntfromname,
908 (strnstr(ent->f_mntfromname, ":", 8) +1),
909 sizeof(ent->f_mntfromname));
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");