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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)mount.c 8.25 (Berkeley) 5/8/95
35 * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/mountctl.h>
42 #include <sys/dtype.h>
43 #include <sys/diskslice.h>
60 #include "pathnames.h"
63 #define MOUNT_META_OPTION_FSTAB "fstab"
64 #define MOUNT_META_OPTION_CURRENT "current"
66 int debug, fstab_style, verbose;
68 static char *catopt(char *, const char *);
70 *getmntpt(const char *);
71 static int hasopt(const char *, const char *);
72 static int ismounted(struct fstab *, struct statfs *, int);
73 static int isremountable(const char *);
74 static void mangle(char *, int *, const char **);
75 static char *update_options(char *, char *, int);
76 static int mountfs(const char *, const char *, const char *,
77 int, const char *, const char *);
78 static void remopt(char *, const char *);
79 static void printdefvals(const struct statfs *);
80 static void prmount(struct statfs *);
81 static void putfsent(const struct statfs *);
82 static void usage(void);
83 static char *flags2opts(int);
84 static char *xstrdup(const char *str);
85 static void checkdisklabel(const char *devpath, const char **vfstypep);
88 * List of VFS types that can be remounted without becoming mounted on top
90 * XXX Is this list correct?
92 static const char *remountable_fs_names[] = {
93 "ufs", "ffs", "ext2fs", "hammer", "hammer2",
98 main(int argc, char **argv)
100 const char *mntfromname, **vfslist, *vfstype;
102 struct statfs *mntbuf;
105 int all, ch, i, init_flags, mntsize, rval, have_fstab;
108 all = init_flags = 0;
112 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) {
124 init_flags |= MNT_FORCE;
128 options = catopt(options, optarg);
135 options = catopt(options, "ro");
139 errx(1, "only one -t option may be specified");
140 vfslist = makevfslist(optarg);
144 init_flags |= MNT_UPDATE;
150 options = catopt(options, "noro");
161 #define BADTYPE(type) \
162 (strcmp(type, FSTAB_RO) && \
163 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
168 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
169 err(1, "getmntinfo");
171 while ((fs = getfsent()) != NULL) {
172 if (BADTYPE(fs->fs_type))
174 if (checkvfsname(fs->fs_vfstype, vfslist))
176 if (hasopt(fs->fs_mntops, "noauto"))
178 if (!(init_flags & MNT_UPDATE) &&
179 ismounted(fs, mntbuf, mntsize))
181 options = update_options(options,
182 fs->fs_mntops, mntbuf->f_flags);
183 if (mountfs(fs->fs_vfstype, fs->fs_spec,
184 fs->fs_file, init_flags, options,
188 } else if (fstab_style) {
189 for (i = 0; i < mntsize; i++) {
190 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
192 putfsent(&mntbuf[i]);
195 for (i = 0; i < mntsize; i++) {
196 if (checkvfsname(mntbuf[i].f_fstypename,
207 rmslashes(*argv, *argv);
209 if (init_flags & MNT_UPDATE) {
212 if ((mntbuf = getmntpt(*argv)) == NULL)
213 errx(1, "not currently mounted %s", *argv);
215 * Only get the mntflags from fstab if both mntpoint
216 * and mntspec are identical. Also handle the special
217 * case where just '/' is mounted and 'spec' is not
218 * identical with the one from fstab ('/dev' is missing
219 * in the spec-string at boot-time).
221 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
222 if (strcmp(fs->fs_spec,
223 mntbuf->f_mntfromname) == 0 &&
225 mntbuf->f_mntonname) == 0) {
227 mntfromname = mntbuf->f_mntfromname;
228 } else if (argv[0][0] == '/' &&
229 argv[0][1] == '\0') {
232 mntfromname = fs->fs_spec;
236 options = update_options(options, fs->fs_mntops,
239 mntfromname = mntbuf->f_mntfromname;
240 options = update_options(options, NULL,
243 rval = mountfs(mntbuf->f_fstypename, mntfromname,
244 mntbuf->f_mntonname, init_flags, options, 0);
247 if ((fs = getfsfile(*argv)) == NULL &&
248 (fs = getfsspec(*argv)) == NULL)
249 errx(1, "%s: unknown special file or file system",
251 if (BADTYPE(fs->fs_type))
252 errx(1, "%s has unknown file system type",
254 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
255 init_flags, options, fs->fs_mntops);
259 * If -t flag has not been specified, the path cannot be
262 * If the spec is not a file and contains a ':' then assume
265 * If the spec is a cdev attempt to extract the fstype from
268 * When all else fails ufs is assumed.
270 if (vfslist == NULL) {
271 if (strpbrk(argv[0], ":") != NULL &&
272 access(argv[0], 0) == -1) {
275 checkdisklabel(argv[0], &vfstype);
279 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
280 init_flags, options, NULL);
288 * If the mount was successfully, and done by root, tell mountd the
289 * good news. Pid checks are probably unnecessary, but don't hurt.
291 if (rval == 0 && getuid() == 0 &&
292 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
293 if (fscanf(mountdfp, "%d", &pid) == 1 &&
294 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
295 err(1, "signal mountd");
303 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
307 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
308 /* the root file system can always be remounted */
311 for (i = mntsize - 1; i >= 0; --i)
312 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
313 (!isremountable(fs->fs_vfstype) ||
314 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
320 isremountable(const char *vfsname)
324 for (cp = remountable_fs_names; *cp; cp++)
325 if (strcmp(*cp, vfsname) == 0)
331 hasopt(const char *mntopts, const char *option)
336 if (option[0] == 'n' && option[1] == 'o') {
341 optbuf = xstrdup(mntopts);
343 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
344 if (opt[0] == 'n' && opt[1] == 'o') {
345 if (!strcasecmp(opt + 2, option))
347 } else if (!strcasecmp(opt, option))
355 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
356 const char *options, const char *mntopts)
358 /* List of directories containing mount_xxx subcommands. */
359 static const char *edirs[] = {
364 const char *argv[100], **edir;
368 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
375 /* resolve the mountpoint with realpath(3) */
376 checkpath(name, mntpath);
381 if (options == NULL) {
382 if (*mntopts == '\0') {
389 optbuf = catopt(xstrdup(mntopts), options);
391 if (strcmp(name, "/") == 0)
393 if (flags & MNT_FORCE)
394 optbuf = catopt(optbuf, "force");
395 if (flags & MNT_RDONLY)
396 optbuf = catopt(optbuf, "ro");
399 * The mount_mfs (newfs) command uses -o to select the
400 * optimization mode. We don't pass the default "-o rw"
403 if (flags & MNT_UPDATE)
404 optbuf = catopt(optbuf, "update");
407 argv[argc++] = vfstype;
408 mangle(optbuf, &argc, argv);
414 printf("exec: mount_%s", vfstype);
415 for (i = 1; i < argc; i++)
416 printf(" %s", argv[i]);
421 switch (pid = fork()) {
422 case -1: /* Error. */
427 /* Go find an executable. */
428 for (edir = edirs; *edir; edir++) {
430 sizeof(execname), "%s/mount_%s", *edir, vfstype);
431 execv(execname, __DECONST(char * const *, argv));
433 if (errno == ENOENT) {
436 for (edir = edirs; *edir; edir++)
437 len += strlen(*edir) + 2; /* ", " */
438 if ((cp = malloc(len)) == NULL)
439 errx(1, "malloc failed");
441 for (edir = edirs; *edir; edir++) {
446 warn("exec mount_%s not found in %s", vfstype, cp);
450 default: /* Parent. */
453 if (waitpid(pid, &status, 0) < 0) {
458 if (WIFEXITED(status)) {
459 if (WEXITSTATUS(status) != 0)
460 return (WEXITSTATUS(status));
461 } else if (WIFSIGNALED(status)) {
462 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
467 if (statfs(name, &sf) < 0) {
468 warn("statfs %s", name);
483 prmount(struct statfs *sfp)
493 if ((buf = malloc(len)) == NULL)
494 errx(1, "malloc failed");
496 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
499 /* Get a string buffer with all the used flags names */
500 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
501 -1, NULL, 0, buf, len);
504 printf(", mounted by ");
505 if ((pw = getpwuid(sfp->f_owner)) != NULL)
506 printf("%s", pw->pw_name);
508 printf("%d", sfp->f_owner);
515 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
516 printf(", writes: sync %ld async %ld",
517 sfp->f_syncwrites, sfp->f_asyncwrites);
518 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
519 printf(", reads: sync %ld async %ld",
520 sfp->f_syncreads, sfp->f_asyncreads);
525 static struct statfs *
526 getmntpt(const char *name)
528 struct statfs *mntbuf;
531 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
532 for (i = mntsize - 1; i >= 0; i--) {
533 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
534 strcmp(mntbuf[i].f_mntonname, name) == 0)
541 catopt(char *s0, const char *s1)
546 if (s1 == NULL || *s1 == '\0')
550 i = strlen(s0) + strlen(s1) + 1 + 1;
551 if ((cp = malloc(i)) == NULL)
552 errx(1, "malloc failed");
553 snprintf(cp, i, "%s,%s", s0, s1);
563 mangle(char *options, int *argcp, const char **argv)
569 for (s = options; (p = strsep(&s, ",")) != NULL;)
578 } else if (strcmp(p, "rw") != 0) {
589 update_options(char *opts, char *fstab, int curflags)
593 char *expopt, *newopt, *tmpopt;
598 /* remove meta options from list */
599 remopt(fstab, MOUNT_META_OPTION_FSTAB);
600 remopt(fstab, MOUNT_META_OPTION_CURRENT);
601 cur = flags2opts(curflags);
604 * Expand all meta-options passed to us first.
607 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
608 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
609 expopt = catopt(expopt, fstab);
610 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
611 expopt = catopt(expopt, cur);
613 expopt = catopt(expopt, o);
619 * Remove previous contradictory arguments. Given option "foo" we
620 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
621 * and "foo" - so we can deal with possible options like "notice".
624 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
625 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
626 errx(1, "malloc failed");
628 strcpy(tmpopt, "no");
630 remopt(newopt, tmpopt);
633 if (strncmp("no", o, 2) == 0)
636 newopt = catopt(newopt, o);
644 remopt(char *string, const char *opt)
648 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
653 for (p = string; (o = strsep(&p, ",")) != NULL;) {
654 if (strcmp(opt, o) != 0) {
655 if (*r == ',' && *o != '\0')
657 while ((*r++ = *o++) != '\0')
669 fprintf(stderr, "%s\n%s\n%s\n",
670 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
671 " mount [-dfpruvw] {special | node}",
672 " mount [-dfpruvw] [-o options] [-t type] special node");
677 * Prints the default values for dump frequency and pass number of fsck.
678 * This happens when mount(8) is called with -p and there is no fstab file
679 * or there is but the values aren't specified.
682 printdefvals(const struct statfs *ent)
684 if (strcmp(ent->f_fstypename, "ufs") == 0) {
685 if (strcmp(ent->f_mntonname, "/") == 0)
695 putfsent(const struct statfs *ent)
701 opts = flags2opts(ent->f_flags);
702 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
703 ent->f_fstypename, opts);
707 * If fstab file is missing don't call getfsspec() or getfsfile()
708 * at all, because the same warning will be printed twice for every
709 * mounted filesystem.
711 if (stat(_PATH_FSTAB, &sb) != -1) {
712 if ((fst = getfsspec(ent->f_mntfromname)))
713 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
714 else if ((fst = getfsfile(ent->f_mntonname)))
715 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
725 flags2opts(int flags)
731 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
733 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
734 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
735 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
736 if (flags & MNT_NODEV) res = catopt(res, "nodev");
737 if (flags & MNT_UNION) res = catopt(res, "union");
738 if (flags & MNT_ASYNC) res = catopt(res, "async");
739 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
740 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
741 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
742 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
743 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
744 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
750 xstrdup(const char *str)
752 char* ret = strdup(str);
754 errx(1, "strdup failed (could not allocate memory)");
760 * Hack 'cd9660' for the default fstype on cd media if no disklabel
764 iscdmedia(const char *path, int fd __unused)
768 if (strrchr(path, '/'))
769 path = strrchr(path, '/') + 1;
770 if (sscanf(path, "acd%d", &n) == 1)
772 if (sscanf(path, "cd%d", &n) == 1)
778 * If the device path is a cdev attempt to access the disklabel to determine
779 * the filesystem type. Adjust *vfstypep if we can figure it out
782 * Ignore any portion of the device path after an '@' or ':'.
785 checkdisklabel(const char *devpath, const char **vfstypep)
788 struct partinfo info;
789 char *path = strdup(devpath);
792 if (strchr(path, '@'))
793 *strchr(path, '@') = 0;
794 if (strchr(path, ':'))
795 *strchr(path, ':') = 0;
797 if (stat(path, &st) < 0)
799 if (!S_ISCHR(st.st_mode))
801 fd = open(path, O_RDONLY);
804 if (ioctl(fd, DIOCGPART, &info) == 0) {
805 if (info.fstype >= 0 && info.fstype < (int)FSMAXTYPES) {
806 if (fstype_to_vfsname[info.fstype]) {
807 *vfstypep = fstype_to_vfsname[info.fstype];
808 } else if (iscdmedia(path, fd)) {
809 *vfstypep = "cd9660";
812 "mount: warning: fstype in disklabel "
813 "not set to anything I understand\n");
815 "attempting to mount with -t ufs\n");