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) {
231 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
233 /* Mountd is not running. */
237 if (errno != EEXIST) {
238 /* Cannot open pidfile for some reason. */
243 * Refuse to send broadcast or group signals, this has
244 * happened due to the bugs in pidfile(3).
246 if (mountdpid <= 0) {
247 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
251 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
252 if (kill(mountdpid, SIGHUP) == -1)
253 err(1, "signal mountd");
257 main(int argc, char *argv[])
259 const char *mntfromname, **vfslist, *vfstype;
261 struct statfs *mntbuf;
262 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
264 char *cp, *ep, *options;
266 all = init_flags = late = onlylate = 0;
271 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
283 init_flags |= MNT_FORCE;
293 /* For compatibility with the Linux version of mount. */
297 options = catopt(options, optarg);
298 if (specified_ro(optarg))
307 options = catopt(options, "ro");
312 errx(1, "only one -t option may be specified");
313 vfslist = makevfslist(optarg);
317 init_flags |= MNT_UPDATE;
323 options = catopt(options, "noro");
333 #define BADTYPE(type) \
334 (strcmp(type, FSTAB_RO) && \
335 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
337 if ((init_flags & MNT_UPDATE) && (ro == 0))
338 options = catopt(options, "noro");
343 if ((mntsize = getmntinfo(&mntbuf,
344 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
345 err(1, "getmntinfo");
347 while ((fs = getfsent()) != NULL) {
348 if (BADTYPE(fs->fs_type))
350 if (checkvfsname(fs->fs_vfstype, vfslist))
352 if (hasopt(fs->fs_mntops, "noauto"))
354 if (!hasopt(fs->fs_mntops, "late") && onlylate)
356 if (hasopt(fs->fs_mntops, "late") && !late)
358 if (hasopt(fs->fs_mntops, "failok"))
362 if (!(init_flags & MNT_UPDATE) &&
363 ismounted(fs, mntbuf, mntsize))
365 options = update_options(options, fs->fs_mntops,
367 if (mountfs(fs->fs_vfstype, fs->fs_spec,
368 fs->fs_file, init_flags, options,
369 fs->fs_mntops) && !failok)
372 } else if (fstab_style) {
373 for (i = 0; i < mntsize; i++) {
374 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
376 putfsent(&mntbuf[i]);
379 for (i = 0; i < mntsize; i++) {
380 if (checkvfsname(mntbuf[i].f_fstypename,
384 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
394 rmslashes(*argv, *argv);
395 if (init_flags & MNT_UPDATE) {
398 if ((mntbuf = getmntpt(*argv)) == NULL)
399 errx(1, "not currently mounted %s", *argv);
401 * Only get the mntflags from fstab if both mntpoint
402 * and mntspec are identical. Also handle the special
403 * case where just '/' is mounted and 'spec' is not
404 * identical with the one from fstab ('/dev' is missing
405 * in the spec-string at boot-time).
407 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
408 if (strcmp(fs->fs_spec,
409 mntbuf->f_mntfromname) == 0 &&
411 mntbuf->f_mntonname) == 0) {
413 mntfromname = mntbuf->f_mntfromname;
414 } else if (argv[0][0] == '/' &&
415 argv[0][1] == '\0' &&
416 strcmp(fs->fs_vfstype,
417 mntbuf->f_fstypename) == 0) {
420 mntfromname = fs->fs_spec;
424 options = update_options(options, fs->fs_mntops,
427 mntfromname = mntbuf->f_mntfromname;
428 options = update_options(options, NULL,
431 rval = mountfs(mntbuf->f_fstypename, mntfromname,
432 mntbuf->f_mntonname, init_flags, options, 0);
435 if ((fs = getfsfile(*argv)) == NULL &&
436 (fs = getfsspec(*argv)) == NULL)
437 errx(1, "%s: unknown special file or file system",
439 if (BADTYPE(fs->fs_type))
440 errx(1, "%s has unknown file system type",
442 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
443 init_flags, options, fs->fs_mntops);
447 * If -t flag has not been specified, the path cannot be
448 * found, spec contains either a ':' or a '@', then assume
449 * that an NFS file system is being specified ala Sun.
450 * Check if the hostname contains only allowed characters
451 * to reduce false positives. IPv6 addresses containing
452 * ':' will be correctly parsed only if the separator is '@'.
453 * The definition of a valid hostname is taken from RFC 1034.
455 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
456 (ep = strchr(argv[0], ':')) != NULL)) {
459 ep = cp + strlen(cp);
463 if (!isdigit(*cp) && !isalpha(*cp) &&
464 *cp != '.' && *cp != '-' && *cp != ':')
471 rval = mountfs(vfstype,
472 argv[0], argv[1], init_flags, options, NULL);
480 * If the mount was successfully, and done by root, tell mountd the
483 if (rval == 0 && getuid() == 0)
490 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
492 char realfsfile[PATH_MAX];
495 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
496 /* the root file system can always be remounted */
499 /* The user may have specified a symlink in fstab, resolve the path */
500 if (realpath(fs->fs_file, realfsfile) == NULL) {
501 /* Cannot resolve the path, use original one */
502 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
506 * Consider the filesystem to be mounted if:
507 * It has the same mountpoint as a mounted filesytem, and
508 * It has the same type as that same mounted filesystem, and
509 * It has the same device name as that same mounted filesystem, OR
510 * It is a nonremountable filesystem
512 for (i = mntsize - 1; i >= 0; --i)
513 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
514 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
515 (!isremountable(fs->fs_vfstype) ||
516 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
522 isremountable(const char *vfsname)
526 for (cp = remountable_fs_names; *cp; cp++)
527 if (strcmp(*cp, vfsname) == 0)
533 hasopt(const char *mntopts, const char *option)
538 if (option[0] == 'n' && option[1] == 'o') {
543 optbuf = strdup(mntopts);
545 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
546 if (opt[0] == 'n' && opt[1] == 'o') {
547 if (!strcasecmp(opt + 2, option))
549 } else if (!strcasecmp(opt, option))
557 append_arg(struct cpa *sa, char *arg)
559 if (sa->c + 1 == sa->sz) {
560 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
561 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
563 errx(1, "realloc failed");
565 sa->a[++sa->c] = arg;
569 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
570 const char *options, const char *mntopts)
574 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
575 static struct cpa mnt_argv;
577 /* resolve the mountpoint with realpath(3) */
578 if (checkpath(name, mntpath) != 0) {
586 optbuf = catopt(strdup(mntopts), options);
588 if (strcmp(name, "/") == 0)
590 if (flags & MNT_FORCE)
591 optbuf = catopt(optbuf, "force");
592 if (flags & MNT_RDONLY)
593 optbuf = catopt(optbuf, "ro");
596 * The mount_mfs (newfs) command uses -o to select the
597 * optimization mode. We don't pass the default "-o rw"
600 if (flags & MNT_UPDATE)
601 optbuf = catopt(optbuf, "update");
603 /* Compatibility glue. */
604 if (strcmp(vfstype, "msdos") == 0)
607 /* Construct the name of the appropriate mount command */
608 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
611 append_arg(&mnt_argv, execname);
612 mangle(optbuf, &mnt_argv);
613 if (mountprog != NULL)
614 strlcpy(execname, mountprog, sizeof(execname));
616 append_arg(&mnt_argv, strdup(spec));
617 append_arg(&mnt_argv, strdup(name));
618 append_arg(&mnt_argv, NULL);
621 if (use_mountprog(vfstype))
622 printf("exec: %s", execname);
624 printf("mount -t %s", vfstype);
625 for (i = 1; i < mnt_argv.c; i++)
626 (void)printf(" %s", mnt_argv.a[i]);
634 if (use_mountprog(vfstype)) {
635 ret = exec_mountprog(name, execname, mnt_argv.a);
637 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
645 if (statfs(name, &sf) < 0) {
646 warn("statfs %s", name);
659 prmount(struct statfs *sfp)
666 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
669 flags = sfp->f_flags & MNT_VISFLAGMASK;
670 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
671 if (flags & o->o_opt) {
672 (void)printf(", %s", o->o_name);
676 * Inform when file system is mounted by an unprivileged user
677 * or privileged non-root user.
679 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
680 (void)printf(", mounted by ");
681 if ((pw = getpwuid(sfp->f_owner)) != NULL)
682 (void)printf("%s", pw->pw_name);
684 (void)printf("%d", sfp->f_owner);
687 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
688 (void)printf(", writes: sync %ju async %ju",
689 (uintmax_t)sfp->f_syncwrites,
690 (uintmax_t)sfp->f_asyncwrites);
691 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
692 (void)printf(", reads: sync %ju async %ju",
693 (uintmax_t)sfp->f_syncreads,
694 (uintmax_t)sfp->f_asyncreads);
695 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
697 for (i = 0; i < sizeof(sfp->f_fsid); i++)
698 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
705 getmntpt(const char *name)
707 struct statfs *mntbuf;
710 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
711 for (i = mntsize - 1; i >= 0; i--) {
712 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
713 strcmp(mntbuf[i].f_mntonname, name) == 0)
720 catopt(char *s0, const char *s1)
724 if (s1 == NULL || *s1 == '\0')
728 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
729 errx(1, "asprintf failed");
739 mangle(char *options, struct cpa *a)
743 for (s = options; (p = strsep(&s, ",")) != NULL;)
745 if (strcmp(p, "noauto") == 0) {
747 * Do not pass noauto option to nmount().
748 * or external mount program. noauto is
749 * only used to prevent mounting a filesystem
750 * when 'mount -a' is specified, and is
751 * not a real mount option.
754 } else if (strcmp(p, "late") == 0) {
756 * "late" is used to prevent certain file
757 * systems from being mounted before late
758 * in the boot cycle; for instance,
759 * loopback NFS mounts can't be mounted
760 * before mountd starts.
763 } else if (strcmp(p, "failok") == 0) {
765 * "failok" is used to prevent certain file
766 * systems from being causing the system to
767 * drop into single user mode in the boot
768 * cycle, and is not a real mount option.
771 } else if (strncmp(p, "mountprog", 9) == 0) {
773 * "mountprog" is used to force the use of
774 * userland mount programs.
776 val = strchr(p, '=');
780 mountprog = strdup(val);
783 if (mountprog == NULL) {
784 errx(1, "Need value for -o mountprog");
787 } else if (strcmp(p, "userquota") == 0) {
789 } else if (strncmp(p, userquotaeq,
790 sizeof(userquotaeq) - 1) == 0) {
792 } else if (strcmp(p, "groupquota") == 0) {
794 } else if (strncmp(p, groupquotaeq,
795 sizeof(groupquotaeq) - 1) == 0) {
797 } else if (*p == '-') {
802 append_arg(a, p + 1);
805 append_arg(a, strdup("-o"));
813 update_options(char *opts, char *fstab, int curflags)
817 char *expopt, *newopt, *tmpopt;
822 /* remove meta options from list */
823 remopt(fstab, MOUNT_META_OPTION_FSTAB);
824 remopt(fstab, MOUNT_META_OPTION_CURRENT);
825 cur = flags2opts(curflags);
828 * Expand all meta-options passed to us first.
831 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
832 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
833 expopt = catopt(expopt, fstab);
834 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
835 expopt = catopt(expopt, cur);
837 expopt = catopt(expopt, o);
843 * Remove previous contradictory arguments. Given option "foo" we
844 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
845 * and "foo" - so we can deal with possible options like "notice".
848 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
849 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
850 errx(1, "malloc failed");
852 strcpy(tmpopt, "no");
854 remopt(newopt, tmpopt);
857 if (strncmp("no", o, 2) == 0)
860 newopt = catopt(newopt, o);
868 remopt(char *string, const char *opt)
872 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
877 for (p = string; (o = strsep(&p, ",")) != NULL;) {
878 if (strcmp(opt, o) != 0) {
879 if (*r == ',' && *o != '\0')
881 while ((*r++ = *o++) != '\0')
893 (void)fprintf(stderr, "%s\n%s\n%s\n",
894 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
895 " mount [-dfpruvw] special | node",
896 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
901 putfsent(struct statfs *ent)
908 /* flags2opts() doesn't return the "rw" option. */
909 if ((ent->f_flags & MNT_RDONLY) != 0)
912 rw = catopt(NULL, "rw");
914 opts = flags2opts(ent->f_flags);
915 opts = catopt(rw, opts);
917 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
918 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
919 strlcpy(ent->f_mntfromname,
920 (strnstr(ent->f_mntfromname, ":", 8) +1),
921 sizeof(ent->f_mntfromname));
924 l = strlen(ent->f_mntfromname);
925 printf("%s%s%s%s", ent->f_mntfromname,
928 l < 24 ? "\t" : " ");
929 l = strlen(ent->f_mntonname);
930 printf("%s%s%s%s", ent->f_mntonname,
933 l < 24 ? "\t" : " ");
934 printf("%s\t", ent->f_fstypename);
940 if ((fst = getfsspec(ent->f_mntfromname)))
941 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
942 else if ((fst = getfsfile(ent->f_mntonname)))
943 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
944 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
945 if (strcmp(ent->f_mntonname, "/") == 0)
955 flags2opts(int flags)
961 if (flags & MNT_RDONLY) res = catopt(res, "ro");
962 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
963 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
964 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
965 if (flags & MNT_UNION) res = catopt(res, "union");
966 if (flags & MNT_ASYNC) res = catopt(res, "async");
967 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
968 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
969 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
970 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
971 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
972 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
973 if (flags & MNT_ACLS) res = catopt(res, "acls");
974 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");