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" },
126 * List of VFS types that can be remounted without becoming mounted on top
128 * XXX Is this list correct?
131 remountable_fs_names[] = {
132 "ufs", "ffs", "ext2fs",
136 static const char userquotaeq[] = "userquota=";
137 static const char groupquotaeq[] = "groupquota=";
139 static char *mountprog = NULL;
142 use_mountprog(const char *vfstype)
144 /* XXX: We need to get away from implementing external mount
145 * programs for every filesystem, and move towards having
146 * each filesystem properly implement the nmount() system call.
150 "cd9660", "mfs", "msdosfs", "nfs",
151 "nullfs", "smbfs", "udf", "unionfs",
155 if (mountprog != NULL)
158 for (i = 0; fs[i] != NULL; ++i) {
159 if (strcmp(vfstype, fs[i]) == 0)
167 exec_mountprog(const char *name, const char *execname, char *const argv[])
172 switch (pid = fork()) {
173 case -1: /* Error. */
177 /* Go find an executable. */
178 execvP(execname, _PATH_SYSPATH, argv);
179 if (errno == ENOENT) {
180 warn("exec %s not found", execname);
181 if (execname[0] != '/') {
182 warnx("in path: %s", _PATH_SYSPATH);
186 default: /* Parent. */
187 if (waitpid(pid, &status, 0) < 0) {
192 if (WIFEXITED(status)) {
193 if (WEXITSTATUS(status) != 0)
194 return (WEXITSTATUS(status));
195 } else if (WIFSIGNALED(status)) {
196 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
206 specified_ro(const char *arg)
211 optbuf = strdup(arg);
215 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
216 if (strcmp(opt, "ro") == 0) {
232 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
234 /* Mountd is not running. */
238 if (errno != EEXIST) {
239 /* Cannot open pidfile for some reason. */
244 * Refuse to send broadcast or group signals, this has
245 * happened due to the bugs in pidfile(3).
247 if (mountdpid <= 0) {
248 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
252 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
253 if (kill(mountdpid, SIGHUP) == -1)
254 err(1, "signal mountd");
258 main(int argc, char *argv[])
260 const char *mntfromname, **vfslist, *vfstype;
262 struct statfs *mntbuf;
263 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
265 char *cp, *ep, *options;
267 all = init_flags = late = onlylate = 0;
272 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
284 init_flags |= MNT_FORCE;
294 /* For compatibility with the Linux version of mount. */
298 options = catopt(options, optarg);
299 if (specified_ro(optarg))
308 options = catopt(options, "ro");
313 errx(1, "only one -t option may be specified");
314 vfslist = makevfslist(optarg);
318 init_flags |= MNT_UPDATE;
324 options = catopt(options, "noro");
334 #define BADTYPE(type) \
335 (strcmp(type, FSTAB_RO) && \
336 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
338 if ((init_flags & MNT_UPDATE) && (ro == 0))
339 options = catopt(options, "noro");
344 if ((mntsize = getmntinfo(&mntbuf,
345 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
346 err(1, "getmntinfo");
348 while ((fs = getfsent()) != NULL) {
349 if (BADTYPE(fs->fs_type))
351 if (checkvfsname(fs->fs_vfstype, vfslist))
353 if (hasopt(fs->fs_mntops, "noauto"))
355 if (!hasopt(fs->fs_mntops, "late") && onlylate)
357 if (hasopt(fs->fs_mntops, "late") && !late)
359 if (hasopt(fs->fs_mntops, "failok"))
363 if (!(init_flags & MNT_UPDATE) &&
364 ismounted(fs, mntbuf, mntsize))
366 options = update_options(options, fs->fs_mntops,
368 if (mountfs(fs->fs_vfstype, fs->fs_spec,
369 fs->fs_file, init_flags, options,
370 fs->fs_mntops) && !failok)
373 } else if (fstab_style) {
374 for (i = 0; i < mntsize; i++) {
375 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
377 putfsent(&mntbuf[i]);
380 for (i = 0; i < mntsize; i++) {
381 if (checkvfsname(mntbuf[i].f_fstypename,
385 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
395 rmslashes(*argv, *argv);
396 if (init_flags & MNT_UPDATE) {
399 if ((mntbuf = getmntpt(*argv)) == NULL)
400 errx(1, "not currently mounted %s", *argv);
402 * Only get the mntflags from fstab if both mntpoint
403 * and mntspec are identical. Also handle the special
404 * case where just '/' is mounted and 'spec' is not
405 * identical with the one from fstab ('/dev' is missing
406 * in the spec-string at boot-time).
408 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
409 if (strcmp(fs->fs_spec,
410 mntbuf->f_mntfromname) == 0 &&
412 mntbuf->f_mntonname) == 0) {
414 mntfromname = mntbuf->f_mntfromname;
415 } else if (argv[0][0] == '/' &&
416 argv[0][1] == '\0' &&
417 strcmp(fs->fs_vfstype,
418 mntbuf->f_fstypename) == 0) {
421 mntfromname = fs->fs_spec;
425 options = update_options(options, fs->fs_mntops,
428 mntfromname = mntbuf->f_mntfromname;
429 options = update_options(options, NULL,
432 rval = mountfs(mntbuf->f_fstypename, mntfromname,
433 mntbuf->f_mntonname, init_flags, options, 0);
436 if ((fs = getfsfile(*argv)) == NULL &&
437 (fs = getfsspec(*argv)) == NULL)
438 errx(1, "%s: unknown special file or file system",
440 if (BADTYPE(fs->fs_type))
441 errx(1, "%s has unknown file system type",
443 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
444 init_flags, options, fs->fs_mntops);
448 * If -t flag has not been specified, the path cannot be
449 * found, spec contains either a ':' or a '@', then assume
450 * that an NFS file system is being specified ala Sun.
451 * Check if the hostname contains only allowed characters
452 * to reduce false positives. IPv6 addresses containing
453 * ':' will be correctly parsed only if the separator is '@'.
454 * The definition of a valid hostname is taken from RFC 1034.
456 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
457 (ep = strchr(argv[0], ':')) != NULL)) {
460 ep = cp + strlen(cp);
464 if (!isdigit(*cp) && !isalpha(*cp) &&
465 *cp != '.' && *cp != '-' && *cp != ':')
472 rval = mountfs(vfstype,
473 argv[0], argv[1], init_flags, options, NULL);
481 * If the mount was successfully, and done by root, tell mountd the
484 if (rval == 0 && getuid() == 0)
491 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
493 char realfsfile[PATH_MAX];
496 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
497 /* the root file system can always be remounted */
500 /* The user may have specified a symlink in fstab, resolve the path */
501 if (realpath(fs->fs_file, realfsfile) == NULL) {
502 /* Cannot resolve the path, use original one */
503 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
507 * Consider the filesystem to be mounted if:
508 * It has the same mountpoint as a mounted filesytem, and
509 * It has the same type as that same mounted filesystem, and
510 * It has the same device name as that same mounted filesystem, OR
511 * It is a nonremountable filesystem
513 for (i = mntsize - 1; i >= 0; --i)
514 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
515 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
516 (!isremountable(fs->fs_vfstype) ||
517 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
523 isremountable(const char *vfsname)
527 for (cp = remountable_fs_names; *cp; cp++)
528 if (strcmp(*cp, vfsname) == 0)
534 hasopt(const char *mntopts, const char *option)
539 if (option[0] == 'n' && option[1] == 'o') {
544 optbuf = strdup(mntopts);
546 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
547 if (opt[0] == 'n' && opt[1] == 'o') {
548 if (!strcasecmp(opt + 2, option))
550 } else if (!strcasecmp(opt, option))
558 append_arg(struct cpa *sa, char *arg)
560 if (sa->c + 1 == sa->sz) {
561 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
562 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
564 errx(1, "realloc failed");
566 sa->a[++sa->c] = arg;
570 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
571 const char *options, const char *mntopts)
575 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
576 static struct cpa mnt_argv;
578 /* resolve the mountpoint with realpath(3) */
579 if (checkpath(name, mntpath) != 0) {
587 optbuf = catopt(strdup(mntopts), options);
589 if (strcmp(name, "/") == 0)
591 if (flags & MNT_FORCE)
592 optbuf = catopt(optbuf, "force");
593 if (flags & MNT_RDONLY)
594 optbuf = catopt(optbuf, "ro");
597 * The mount_mfs (newfs) command uses -o to select the
598 * optimization mode. We don't pass the default "-o rw"
601 if (flags & MNT_UPDATE)
602 optbuf = catopt(optbuf, "update");
604 /* Compatibility glue. */
605 if (strcmp(vfstype, "msdos") == 0)
608 /* Construct the name of the appropriate mount command */
609 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
612 append_arg(&mnt_argv, execname);
613 mangle(optbuf, &mnt_argv);
614 if (mountprog != NULL)
615 strlcpy(execname, mountprog, sizeof(execname));
617 append_arg(&mnt_argv, strdup(spec));
618 append_arg(&mnt_argv, strdup(name));
619 append_arg(&mnt_argv, NULL);
622 if (use_mountprog(vfstype))
623 printf("exec: %s", execname);
625 printf("mount -t %s", vfstype);
626 for (i = 1; i < mnt_argv.c; i++)
627 (void)printf(" %s", mnt_argv.a[i]);
635 if (use_mountprog(vfstype)) {
636 ret = exec_mountprog(name, execname, mnt_argv.a);
638 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
646 if (statfs(name, &sf) < 0) {
647 warn("statfs %s", name);
660 prmount(struct statfs *sfp)
667 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
670 flags = sfp->f_flags & MNT_VISFLAGMASK;
671 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
672 if (flags & o->o_opt) {
673 (void)printf(", %s", o->o_name);
677 * Inform when file system is mounted by an unprivileged user
678 * or privileged non-root user.
680 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
681 (void)printf(", mounted by ");
682 if ((pw = getpwuid(sfp->f_owner)) != NULL)
683 (void)printf("%s", pw->pw_name);
685 (void)printf("%d", sfp->f_owner);
688 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
689 (void)printf(", writes: sync %ju async %ju",
690 (uintmax_t)sfp->f_syncwrites,
691 (uintmax_t)sfp->f_asyncwrites);
692 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
693 (void)printf(", reads: sync %ju async %ju",
694 (uintmax_t)sfp->f_syncreads,
695 (uintmax_t)sfp->f_asyncreads);
696 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
698 for (i = 0; i < sizeof(sfp->f_fsid); i++)
699 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
706 getmntpt(const char *name)
708 struct statfs *mntbuf;
711 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
712 for (i = mntsize - 1; i >= 0; i--) {
713 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
714 strcmp(mntbuf[i].f_mntonname, name) == 0)
721 catopt(char *s0, const char *s1)
725 if (s1 == NULL || *s1 == '\0')
729 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
730 errx(1, "asprintf failed");
740 mangle(char *options, struct cpa *a)
744 for (s = options; (p = strsep(&s, ",")) != NULL;)
746 if (strcmp(p, "noauto") == 0) {
748 * Do not pass noauto option to nmount().
749 * or external mount program. noauto is
750 * only used to prevent mounting a filesystem
751 * when 'mount -a' is specified, and is
752 * not a real mount option.
755 } else if (strcmp(p, "late") == 0) {
757 * "late" is used to prevent certain file
758 * systems from being mounted before late
759 * in the boot cycle; for instance,
760 * loopback NFS mounts can't be mounted
761 * before mountd starts.
764 } else if (strcmp(p, "failok") == 0) {
766 * "failok" is used to prevent certain file
767 * systems from being causing the system to
768 * drop into single user mode in the boot
769 * cycle, and is not a real mount option.
772 } else if (strncmp(p, "mountprog", 9) == 0) {
774 * "mountprog" is used to force the use of
775 * userland mount programs.
777 val = strchr(p, '=');
781 mountprog = strdup(val);
784 if (mountprog == NULL) {
785 errx(1, "Need value for -o mountprog");
788 } else if (strcmp(p, "userquota") == 0) {
790 } else if (strncmp(p, userquotaeq,
791 sizeof(userquotaeq) - 1) == 0) {
793 } else if (strcmp(p, "groupquota") == 0) {
795 } else if (strncmp(p, groupquotaeq,
796 sizeof(groupquotaeq) - 1) == 0) {
798 } else if (*p == '-') {
803 append_arg(a, p + 1);
806 append_arg(a, strdup("-o"));
814 update_options(char *opts, char *fstab, int curflags)
818 char *expopt, *newopt, *tmpopt;
823 /* remove meta options from list */
824 remopt(fstab, MOUNT_META_OPTION_FSTAB);
825 remopt(fstab, MOUNT_META_OPTION_CURRENT);
826 cur = flags2opts(curflags);
829 * Expand all meta-options passed to us first.
832 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
833 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
834 expopt = catopt(expopt, fstab);
835 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
836 expopt = catopt(expopt, cur);
838 expopt = catopt(expopt, o);
844 * Remove previous contradictory arguments. Given option "foo" we
845 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
846 * and "foo" - so we can deal with possible options like "notice".
849 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
850 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
851 errx(1, "malloc failed");
853 strcpy(tmpopt, "no");
855 remopt(newopt, tmpopt);
858 if (strncmp("no", o, 2) == 0)
861 newopt = catopt(newopt, o);
869 remopt(char *string, const char *opt)
873 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
878 for (p = string; (o = strsep(&p, ",")) != NULL;) {
879 if (strcmp(opt, o) != 0) {
880 if (*r == ',' && *o != '\0')
882 while ((*r++ = *o++) != '\0')
894 (void)fprintf(stderr, "%s\n%s\n%s\n",
895 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
896 " mount [-dfpruvw] special | node",
897 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
902 putfsent(struct statfs *ent)
909 /* flags2opts() doesn't return the "rw" option. */
910 if ((ent->f_flags & MNT_RDONLY) != 0)
913 rw = catopt(NULL, "rw");
915 opts = flags2opts(ent->f_flags);
916 opts = catopt(rw, opts);
918 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
919 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
920 strlcpy(ent->f_mntfromname,
921 (strnstr(ent->f_mntfromname, ":", 8) +1),
922 sizeof(ent->f_mntfromname));
925 l = strlen(ent->f_mntfromname);
926 printf("%s%s%s%s", ent->f_mntfromname,
929 l < 24 ? "\t" : " ");
930 l = strlen(ent->f_mntonname);
931 printf("%s%s%s%s", ent->f_mntonname,
934 l < 24 ? "\t" : " ");
935 printf("%s\t", ent->f_fstypename);
941 if ((fst = getfsspec(ent->f_mntfromname)))
942 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
943 else if ((fst = getfsfile(ent->f_mntonname)))
944 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
945 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
946 if (strcmp(ent->f_mntonname, "/") == 0)
956 flags2opts(int flags)
962 if (flags & MNT_RDONLY) res = catopt(res, "ro");
963 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
964 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
965 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
966 if (flags & MNT_UNION) res = catopt(res, "union");
967 if (flags & MNT_ASYNC) res = catopt(res, "async");
968 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
969 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
970 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
971 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
972 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
973 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
974 if (flags & MNT_ACLS) res = catopt(res, "acls");
975 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");
976 if (flags & MNT_UNTRUSTED) res = catopt(res, "untrusted");