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";
38 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/mount.h>
64 #include "pathnames.h"
67 #define MOUNT_META_OPTION_FSTAB "fstab"
68 #define MOUNT_META_OPTION_CURRENT "current"
70 int debug, fstab_style, verbose;
72 char *catopt(char *, const char *);
73 struct statfs *getmntpt(const char *);
74 int hasopt(const char *, const char *);
75 int ismounted(struct fstab *, struct statfs *, int);
76 int isremountable(const char *);
77 void mangle(char *, int *, char *[]);
78 char *update_options(char *, char *, int);
79 int mountfs(const char *, const char *, const char *,
80 int, const char *, const char *);
81 void remopt(char *, const char *);
82 void prmount(struct statfs *);
83 void putfsent(const struct statfs *);
85 char *flags2opts(int);
87 /* Map from mount options to printable formats. */
92 { MNT_ASYNC, "asynchronous" },
93 { MNT_EXPORTED, "NFS exported" },
94 { MNT_LOCAL, "local" },
95 { MNT_NOATIME, "noatime" },
96 { MNT_NOEXEC, "noexec" },
97 { MNT_NOSUID, "nosuid" },
98 { MNT_NOSYMFOLLOW, "nosymfollow" },
99 { MNT_QUOTA, "with quotas" },
100 { MNT_RDONLY, "read-only" },
101 { MNT_SYNCHRONOUS, "synchronous" },
102 { MNT_UNION, "union" },
103 { MNT_NOCLUSTERR, "noclusterr" },
104 { MNT_NOCLUSTERW, "noclusterw" },
105 { MNT_SUIDDIR, "suiddir" },
106 { MNT_SOFTDEP, "soft-updates" },
107 { MNT_MULTILABEL, "multilabel" },
108 { MNT_ACLS, "acls" },
109 { MNT_GJOURNAL, "gjournal" },
114 * List of VFS types that can be remounted without becoming mounted on top
116 * XXX Is this list correct?
119 remountable_fs_names[] = {
120 "ufs", "ffs", "ext2fs",
124 static const char userquotaeq[] = "userquota=";
125 static const char groupquotaeq[] = "groupquota=";
128 use_mountprog(const char *vfstype)
130 /* XXX: We need to get away from implementing external mount
131 * programs for every filesystem, and move towards having
132 * each filesystem properly implement the nmount() system call.
136 "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
137 "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
142 for (i = 0; fs[i] != NULL; ++i) {
143 if (strcmp(vfstype, fs[i]) == 0)
151 exec_mountprog(const char *name, const char *execname, char *const argv[])
156 switch (pid = fork()) {
157 case -1: /* Error. */
161 /* Go find an executable. */
162 execvP(execname, _PATH_SYSPATH, argv);
163 if (errno == ENOENT) {
164 warn("exec %s not found in %s", execname,
168 default: /* Parent. */
169 if (waitpid(pid, &status, 0) < 0) {
174 if (WIFEXITED(status)) {
175 if (WEXITSTATUS(status) != 0)
176 return (WEXITSTATUS(status));
177 } else if (WIFSIGNALED(status)) {
178 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
188 specified_ro(const char *arg)
193 optbuf = strdup(arg);
197 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
198 if (strcmp(opt, "ro") == 0) {
208 main(int argc, char *argv[])
210 const char *mntfromname, **vfslist, *vfstype;
212 struct statfs *mntbuf;
215 int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro;
216 char *cp, *ep, *options;
218 all = init_flags = late = 0;
223 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
235 init_flags |= MNT_FORCE;
242 options = catopt(options, optarg);
243 if (specified_ro(optarg))
252 options = catopt(options, "ro");
257 errx(1, "only one -t option may be specified");
258 vfslist = makevfslist(optarg);
262 init_flags |= MNT_UPDATE;
268 options = catopt(options, "noro");
278 #define BADTYPE(type) \
279 (strcmp(type, FSTAB_RO) && \
280 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
282 if ((init_flags & MNT_UPDATE) && (ro == 0))
283 options = catopt(options, "noro");
288 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
289 err(1, "getmntinfo");
291 while ((fs = getfsent()) != NULL) {
292 if (BADTYPE(fs->fs_type))
294 if (checkvfsname(fs->fs_vfstype, vfslist))
296 if (hasopt(fs->fs_mntops, "noauto"))
298 if (hasopt(fs->fs_mntops, "late") && !late)
300 if (!(init_flags & MNT_UPDATE) &&
301 ismounted(fs, mntbuf, mntsize))
303 options = update_options(options, fs->fs_mntops,
305 if (mountfs(fs->fs_vfstype, fs->fs_spec,
306 fs->fs_file, init_flags, options,
310 } else if (fstab_style) {
311 for (i = 0; i < mntsize; i++) {
312 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
314 putfsent(&mntbuf[i]);
317 for (i = 0; i < mntsize; i++) {
318 if (checkvfsname(mntbuf[i].f_fstypename,
329 rmslashes(*argv, *argv);
330 if (init_flags & MNT_UPDATE) {
333 if ((mntbuf = getmntpt(*argv)) == NULL)
334 errx(1, "not currently mounted %s", *argv);
336 * Only get the mntflags from fstab if both mntpoint
337 * and mntspec are identical. Also handle the special
338 * case where just '/' is mounted and 'spec' is not
339 * identical with the one from fstab ('/dev' is missing
340 * in the spec-string at boot-time).
342 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
343 if (strcmp(fs->fs_spec,
344 mntbuf->f_mntfromname) == 0 &&
346 mntbuf->f_mntonname) == 0) {
348 mntfromname = mntbuf->f_mntfromname;
349 } else if (argv[0][0] == '/' &&
350 argv[0][1] == '\0') {
353 mntfromname = fs->fs_spec;
357 options = update_options(options, fs->fs_mntops,
360 mntfromname = mntbuf->f_mntfromname;
361 options = update_options(options, NULL,
364 rval = mountfs(mntbuf->f_fstypename, mntfromname,
365 mntbuf->f_mntonname, init_flags, options, 0);
368 if ((fs = getfsfile(*argv)) == NULL &&
369 (fs = getfsspec(*argv)) == NULL)
370 errx(1, "%s: unknown special file or file system",
372 if (BADTYPE(fs->fs_type))
373 errx(1, "%s has unknown file system type",
375 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
376 init_flags, options, fs->fs_mntops);
380 * If -t flag has not been specified, the path cannot be
381 * found, spec contains either a ':' or a '@', then assume
382 * that an NFS file system is being specified ala Sun.
383 * Check if the hostname contains only allowed characters
384 * to reduce false positives. IPv6 addresses containing
385 * ':' will be correctly parsed only if the separator is '@'.
386 * The definition of a valid hostname is taken from RFC 1034.
388 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
389 (ep = strchr(argv[0], ':')) != NULL)) {
392 ep = cp + strlen(cp);
396 if (!isdigit(*cp) && !isalpha(*cp) &&
397 *cp != '.' && *cp != '-' && *cp != ':')
404 rval = mountfs(vfstype,
405 argv[0], argv[1], init_flags, options, NULL);
413 * If the mount was successfully, and done by root, tell mountd the
414 * good news. Pid checks are probably unnecessary, but don't hurt.
416 if (rval == 0 && getuid() == 0 &&
417 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
418 if (fscanf(mountdfp, "%d", &pid) == 1 &&
419 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
420 err(1, "signal mountd");
421 (void)fclose(mountdfp);
428 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
430 char realfsfile[PATH_MAX];
433 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
434 /* the root file system can always be remounted */
437 /* The user may have specified a symlink in fstab, resolve the path */
438 if (realpath(fs->fs_file, realfsfile) == NULL) {
439 /* Cannot resolve the path, use original one */
440 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
443 for (i = mntsize - 1; i >= 0; --i)
444 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
445 (!isremountable(fs->fs_vfstype) ||
446 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
452 isremountable(const char *vfsname)
456 for (cp = remountable_fs_names; *cp; cp++)
457 if (strcmp(*cp, vfsname) == 0)
463 hasopt(const char *mntopts, const char *option)
468 if (option[0] == 'n' && option[1] == 'o') {
473 optbuf = strdup(mntopts);
475 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
476 if (opt[0] == 'n' && opt[1] == 'o') {
477 if (!strcasecmp(opt + 2, option))
479 } else if (!strcasecmp(opt, option))
487 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
488 const char *options, const char *mntopts)
493 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
495 /* resolve the mountpoint with realpath(3) */
496 (void)checkpath(name, mntpath);
501 optbuf = catopt(strdup(mntopts), options);
503 if (strcmp(name, "/") == 0)
505 if (flags & MNT_FORCE)
506 optbuf = catopt(optbuf, "force");
507 if (flags & MNT_RDONLY)
508 optbuf = catopt(optbuf, "ro");
511 * The mount_mfs (newfs) command uses -o to select the
512 * optimization mode. We don't pass the default "-o rw"
515 if (flags & MNT_UPDATE)
516 optbuf = catopt(optbuf, "update");
518 /* Compatibility glue. */
519 if (strcmp(vfstype, "msdos") == 0)
522 /* Construct the name of the appropriate mount command */
523 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
526 argv[argc++] = execname;
527 mangle(optbuf, &argc, argv);
528 argv[argc++] = strdup(spec);
529 argv[argc++] = strdup(name);
533 (void)printf("exec: mount_%s", vfstype);
534 for (i = 1; i < argc; i++)
535 (void)printf(" %s", argv[i]);
540 if (use_mountprog(vfstype)) {
541 ret = exec_mountprog(name, execname, argv);
543 ret = mount_fs(vfstype, argc, argv);
549 if (statfs(name, &sf) < 0) {
550 warn("statfs %s", name);
563 prmount(struct statfs *sfp)
570 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
573 flags = sfp->f_flags & MNT_VISFLAGMASK;
574 for (o = optnames; flags && o->o_opt; o++)
575 if (flags & o->o_opt) {
576 (void)printf(", %s", o->o_name);
580 * Inform when file system is mounted by an unprivileged user
581 * or privileged non-root user.
583 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
584 (void)printf(", mounted by ");
585 if ((pw = getpwuid(sfp->f_owner)) != NULL)
586 (void)printf("%s", pw->pw_name);
588 (void)printf("%d", sfp->f_owner);
591 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
592 (void)printf(", writes: sync %ju async %ju",
593 (uintmax_t)sfp->f_syncwrites,
594 (uintmax_t)sfp->f_asyncwrites);
595 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
596 (void)printf(", reads: sync %ju async %ju",
597 (uintmax_t)sfp->f_syncreads,
598 (uintmax_t)sfp->f_asyncreads);
599 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
601 for (i = 0; i < sizeof(sfp->f_fsid); i++)
602 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
609 getmntpt(const char *name)
611 struct statfs *mntbuf;
614 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
615 for (i = mntsize - 1; i >= 0; i--) {
616 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
617 strcmp(mntbuf[i].f_mntonname, name) == 0)
624 catopt(char *s0, const char *s1)
629 if (s1 == NULL || *s1 == '\0')
633 i = strlen(s0) + strlen(s1) + 1 + 1;
634 if ((cp = malloc(i)) == NULL)
635 errx(1, "malloc failed");
636 (void)snprintf(cp, i, "%s,%s", s0, s1);
646 mangle(char *options, int *argcp, char *argv[])
652 for (s = options; (p = strsep(&s, ",")) != NULL;)
654 if (strcmp(p, "noauto") == 0) {
656 * Do not pass noauto option to nmount().
657 * or external mount program. noauto is
658 * only used to prevent mounting a filesystem
659 * when 'mount -a' is specified, and is
660 * not a real mount option.
663 } else if (strcmp(p, "late") == 0) {
665 * "late" is used to prevent certain file
666 * systems from being mounted before late
667 * in the boot cycle; for instance,
668 * loopback NFS mounts can't be mounted
669 * before mountd starts.
672 } else if (strcmp(p, "userquota") == 0) {
674 } else if (strncmp(p, userquotaeq,
675 sizeof(userquotaeq) - 1) == 0) {
677 } else if (strcmp(p, "groupquota") == 0) {
679 } else if (strncmp(p, groupquotaeq,
680 sizeof(groupquotaeq) - 1) == 0) {
682 } else if (*p == '-') {
690 argv[argc++] = strdup("-o");
700 update_options(char *opts, char *fstab, int curflags)
704 char *expopt, *newopt, *tmpopt;
709 /* remove meta options from list */
710 remopt(fstab, MOUNT_META_OPTION_FSTAB);
711 remopt(fstab, MOUNT_META_OPTION_CURRENT);
712 cur = flags2opts(curflags);
715 * Expand all meta-options passed to us first.
718 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
719 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
720 expopt = catopt(expopt, fstab);
721 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
722 expopt = catopt(expopt, cur);
724 expopt = catopt(expopt, o);
730 * Remove previous contradictory arguments. Given option "foo" we
731 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
732 * and "foo" - so we can deal with possible options like "notice".
735 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
736 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
737 errx(1, "malloc failed");
739 strcpy(tmpopt, "no");
741 remopt(newopt, tmpopt);
744 if (strncmp("no", o, 2) == 0)
747 newopt = catopt(newopt, o);
755 remopt(char *string, const char *opt)
759 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
764 for (p = string; (o = strsep(&p, ",")) != NULL;) {
765 if (strcmp(opt, o) != 0) {
766 if (*r == ',' && *o != '\0')
768 while ((*r++ = *o++) != '\0')
780 (void)fprintf(stderr, "%s\n%s\n%s\n",
781 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
782 " mount [-dfpruvw] special | node",
783 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
788 putfsent(const struct statfs *ent)
793 opts = flags2opts(ent->f_flags);
796 * "rw" is not a real mount option; this is why we print NULL as "rw"
797 * if opts is still NULL here.
799 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
800 ent->f_fstypename, opts ? opts : "rw");
803 if ((fst = getfsspec(ent->f_mntfromname)))
804 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
805 else if ((fst = getfsfile(ent->f_mntonname)))
806 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
807 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
808 if (strcmp(ent->f_mntonname, "/") == 0)
818 flags2opts(int flags)
824 if (flags & MNT_RDONLY) res = catopt(res, "ro");
825 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
826 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
827 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
828 if (flags & MNT_UNION) res = catopt(res, "union");
829 if (flags & MNT_ASYNC) res = catopt(res, "async");
830 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
831 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
832 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
833 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
834 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
835 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
836 if (flags & MNT_ACLS) res = catopt(res, "acls");
837 if (flags & MNT_GJOURNAL) res = catopt(res, "gjournal");