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
29 * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)mount.c 8.25 (Berkeley) 5/8/95
31 * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/mountctl.h>
38 #include <sys/dtype.h>
39 #include <sys/diskslice.h>
56 #include "pathnames.h"
59 #define MOUNT_META_OPTION_FSTAB "fstab"
60 #define MOUNT_META_OPTION_CURRENT "current"
62 int debug, fstab_style, verbose;
64 static char *catopt(char *, const char *);
66 *getmntpt(const char *);
67 static int hasopt(const char *, const char *);
68 static int ismounted(struct fstab *, struct statfs *, int);
69 static int isremountable(const char *);
70 static void mangle(char *, int *, const char **);
71 static char *update_options(char *, char *, int);
72 static int mountfs(const char *, const char *, const char *,
73 int, const char *, const char *);
74 static void remopt(char *, const char *);
75 static void printdefvals(const struct statfs *);
76 static void prmount(struct statfs *);
77 static void putfsent(const struct statfs *);
78 static void usage(void);
79 static char *flags2opts(int);
80 static char *xstrdup(const char *str);
81 static void checkdisklabel(const char *devpath, const char **vfstypep);
84 * List of VFS types that can be remounted without becoming mounted on top
86 * XXX Is this list correct?
88 static const char *remountable_fs_names[] = {
89 "ufs", "ffs", "ext2fs", "hammer", "hammer2",
94 main(int argc, char **argv)
96 const char *mntfromname, **vfslist, *vfstype;
98 struct statfs *mntbuf;
101 int all, ch, i, init_flags, mntsize, rval, have_fstab;
104 all = init_flags = 0;
108 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) {
120 init_flags |= MNT_FORCE;
124 options = catopt(options, optarg);
131 options = catopt(options, "ro");
135 errx(1, "only one -t option may be specified");
136 vfslist = makevfslist(optarg);
140 init_flags |= MNT_UPDATE;
146 options = catopt(options, "noro");
157 #define BADTYPE(type) \
158 (strcmp(type, FSTAB_RO) && \
159 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
164 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
165 err(1, "getmntinfo");
167 while ((fs = getfsent()) != NULL) {
168 if (BADTYPE(fs->fs_type))
170 if (checkvfsname(fs->fs_vfstype, vfslist))
172 if (hasopt(fs->fs_mntops, "noauto"))
174 if (!(init_flags & MNT_UPDATE) &&
175 ismounted(fs, mntbuf, mntsize))
177 options = update_options(options,
178 fs->fs_mntops, mntbuf->f_flags);
179 if (mountfs(fs->fs_vfstype, fs->fs_spec,
180 fs->fs_file, init_flags, options,
184 } else if (fstab_style) {
185 for (i = 0; i < mntsize; i++) {
186 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
188 putfsent(&mntbuf[i]);
191 for (i = 0; i < mntsize; i++) {
192 if (checkvfsname(mntbuf[i].f_fstypename,
203 rmslashes(*argv, *argv);
205 if (init_flags & MNT_UPDATE) {
208 if ((mntbuf = getmntpt(*argv)) == NULL)
209 errx(1, "not currently mounted %s", *argv);
211 * Only get the mntflags from fstab if both mntpoint
212 * and mntspec are identical. Also handle the special
213 * case where just '/' is mounted and 'spec' is not
214 * identical with the one from fstab ('/dev' is missing
215 * in the spec-string at boot-time).
217 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
218 if (strcmp(fs->fs_spec,
219 mntbuf->f_mntfromname) == 0 &&
221 mntbuf->f_mntonname) == 0) {
223 mntfromname = mntbuf->f_mntfromname;
224 } else if (argv[0][0] == '/' &&
225 argv[0][1] == '\0') {
228 mntfromname = fs->fs_spec;
232 options = update_options(options, fs->fs_mntops,
235 mntfromname = mntbuf->f_mntfromname;
236 options = update_options(options, NULL,
239 rval = mountfs(mntbuf->f_fstypename, mntfromname,
240 mntbuf->f_mntonname, init_flags, options, 0);
243 if ((fs = getfsfile(*argv)) == NULL &&
244 (fs = getfsspec(*argv)) == NULL)
245 errx(1, "%s: unknown special file or file system",
247 if (BADTYPE(fs->fs_type))
248 errx(1, "%s has unknown file system type",
250 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
251 init_flags, options, fs->fs_mntops);
255 * If -t flag has not been specified, the path cannot be
258 * If the spec is not a file and contains a ':' then assume
261 * If the spec is a cdev attempt to extract the fstype from
264 * When all else fails ufs is assumed.
266 if (vfslist == NULL) {
267 if (strpbrk(argv[0], ":") != NULL &&
268 access(argv[0], 0) == -1) {
271 checkdisklabel(argv[0], &vfstype);
275 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
276 init_flags, options, NULL);
284 * If the mount was successfully, and done by root, tell mountd the
285 * good news. Pid checks are probably unnecessary, but don't hurt.
287 if (rval == 0 && getuid() == 0 &&
288 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
289 if (fscanf(mountdfp, "%d", &pid) == 1 &&
290 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
291 err(1, "signal mountd");
299 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
303 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
304 /* the root file system can always be remounted */
307 for (i = mntsize - 1; i >= 0; --i)
308 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
309 (!isremountable(fs->fs_vfstype) ||
310 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
316 isremountable(const char *vfsname)
320 for (cp = remountable_fs_names; *cp; cp++)
321 if (strcmp(*cp, vfsname) == 0)
327 hasopt(const char *mntopts, const char *option)
332 if (option[0] == 'n' && option[1] == 'o') {
337 optbuf = xstrdup(mntopts);
339 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
340 if (opt[0] == 'n' && opt[1] == 'o') {
341 if (!strcasecmp(opt + 2, option))
343 } else if (!strcasecmp(opt, option))
351 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
352 const char *options, const char *mntopts)
354 /* List of directories containing mount_xxx subcommands. */
355 static const char *edirs[] = {
360 const char *argv[100], **edir;
364 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
371 /* resolve the mountpoint with realpath(3) */
372 checkpath(name, mntpath);
377 if (options == NULL) {
378 if (*mntopts == '\0') {
385 optbuf = catopt(xstrdup(mntopts), options);
387 if (strcmp(name, "/") == 0)
389 if (flags & MNT_FORCE)
390 optbuf = catopt(optbuf, "force");
391 if (flags & MNT_RDONLY)
392 optbuf = catopt(optbuf, "ro");
395 * The mount_mfs (newfs) command uses -o to select the
396 * optimization mode. We don't pass the default "-o rw"
399 if (flags & MNT_UPDATE)
400 optbuf = catopt(optbuf, "update");
403 argv[argc++] = vfstype;
404 mangle(optbuf, &argc, argv);
410 printf("exec: mount_%s", vfstype);
411 for (i = 1; i < argc; i++)
412 printf(" %s", argv[i]);
417 switch (pid = fork()) {
418 case -1: /* Error. */
423 /* Go find an executable. */
424 for (edir = edirs; *edir; edir++) {
426 sizeof(execname), "%s/mount_%s", *edir, vfstype);
427 execv(execname, __DECONST(char * const *, argv));
429 if (errno == ENOENT) {
432 for (edir = edirs; *edir; edir++)
433 len += strlen(*edir) + 2; /* ", " */
434 if ((cp = malloc(len)) == NULL)
435 errx(1, "malloc failed");
437 for (edir = edirs; *edir; edir++) {
442 warn("exec mount_%s not found in %s", vfstype, cp);
446 default: /* Parent. */
449 if (waitpid(pid, &status, 0) < 0) {
454 if (WIFEXITED(status)) {
455 if (WEXITSTATUS(status) != 0)
456 return (WEXITSTATUS(status));
457 } else if (WIFSIGNALED(status)) {
458 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
463 if (statfs(name, &sf) < 0) {
464 warn("statfs %s", name);
479 prmount(struct statfs *sfp)
489 if ((buf = malloc(len)) == NULL)
490 errx(1, "malloc failed");
492 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
495 /* Get a string buffer with all the used flags names */
496 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
497 -1, NULL, 0, buf, len);
500 printf(", mounted by ");
501 if ((pw = getpwuid(sfp->f_owner)) != NULL)
502 printf("%s", pw->pw_name);
504 printf("%d", sfp->f_owner);
507 if (error != -1 && strlen(buf))
511 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
512 printf(", writes: sync %ld async %ld",
513 sfp->f_syncwrites, sfp->f_asyncwrites);
514 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
515 printf(", reads: sync %ld async %ld",
516 sfp->f_syncreads, sfp->f_asyncreads);
521 static struct statfs *
522 getmntpt(const char *name)
524 struct statfs *mntbuf;
527 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
528 for (i = mntsize - 1; i >= 0; i--) {
529 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
530 strcmp(mntbuf[i].f_mntonname, name) == 0)
537 catopt(char *s0, const char *s1)
542 if (s1 == NULL || *s1 == '\0')
546 i = strlen(s0) + strlen(s1) + 1 + 1;
547 if ((cp = malloc(i)) == NULL)
548 errx(1, "malloc failed");
549 snprintf(cp, i, "%s,%s", s0, s1);
559 mangle(char *options, int *argcp, const char **argv)
565 for (s = options; (p = strsep(&s, ",")) != NULL;)
574 } else if (strcmp(p, "rw") != 0) {
585 update_options(char *opts, char *fstab, int curflags)
589 char *expopt, *newopt, *tmpopt;
594 /* remove meta options from list */
595 remopt(fstab, MOUNT_META_OPTION_FSTAB);
596 remopt(fstab, MOUNT_META_OPTION_CURRENT);
597 cur = flags2opts(curflags);
600 * Expand all meta-options passed to us first.
603 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
604 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
605 expopt = catopt(expopt, fstab);
606 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
607 expopt = catopt(expopt, cur);
609 expopt = catopt(expopt, o);
615 * Remove previous contradictory arguments. Given option "foo" we
616 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
617 * and "foo" - so we can deal with possible options like "notice".
620 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
621 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
622 errx(1, "malloc failed");
624 strcpy(tmpopt, "no");
626 remopt(newopt, tmpopt);
629 if (strncmp("no", o, 2) == 0)
632 newopt = catopt(newopt, o);
640 remopt(char *string, const char *opt)
644 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
649 for (p = string; (o = strsep(&p, ",")) != NULL;) {
650 if (strcmp(opt, o) != 0) {
651 if (*r == ',' && *o != '\0')
653 while ((*r++ = *o++) != '\0')
665 fprintf(stderr, "%s\n%s\n%s\n",
666 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
667 " mount [-dfpruvw] {special | node}",
668 " mount [-dfpruvw] [-o options] [-t type] special node");
673 * Prints the default values for dump frequency and pass number of fsck.
674 * This happens when mount(8) is called with -p and there is no fstab file
675 * or there is but the values aren't specified.
678 printdefvals(const struct statfs *ent)
680 if (strcmp(ent->f_fstypename, "ufs") == 0) {
681 if (strcmp(ent->f_mntonname, "/") == 0)
691 putfsent(const struct statfs *ent)
697 opts = flags2opts(ent->f_flags);
698 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
699 ent->f_fstypename, opts);
703 * If fstab file is missing don't call getfsspec() or getfsfile()
704 * at all, because the same warning will be printed twice for every
705 * mounted filesystem.
707 if (stat(_PATH_FSTAB, &sb) != -1) {
708 if ((fst = getfsspec(ent->f_mntfromname)))
709 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
710 else if ((fst = getfsfile(ent->f_mntonname)))
711 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
721 flags2opts(int flags)
727 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
729 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
730 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
731 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
732 if (flags & MNT_NODEV) res = catopt(res, "nodev");
733 if (flags & MNT_UNION) res = catopt(res, "union");
734 if (flags & MNT_ASYNC) res = catopt(res, "async");
735 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
736 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
737 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
738 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
739 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
740 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
746 xstrdup(const char *str)
748 char* ret = strdup(str);
750 errx(1, "strdup failed (could not allocate memory)");
756 * Hack 'cd9660' for the default fstype on cd media if no disklabel
760 iscdmedia(const char *path, int fd __unused)
764 if (strrchr(path, '/'))
765 path = strrchr(path, '/') + 1;
766 if (sscanf(path, "acd%d", &n) == 1)
768 if (sscanf(path, "cd%d", &n) == 1)
774 * If the device path is a cdev attempt to access the disklabel to determine
775 * the filesystem type. Adjust *vfstypep if we can figure it out
778 * Ignore any portion of the device path after an '@' or ':'.
781 checkdisklabel(const char *devpath, const char **vfstypep)
784 struct partinfo info;
785 char *path = strdup(devpath);
788 if (strchr(path, '@'))
789 *strchr(path, '@') = 0;
790 if (strchr(path, ':'))
791 *strchr(path, ':') = 0;
793 if (stat(path, &st) < 0)
795 if (!S_ISCHR(st.st_mode))
797 fd = open(path, O_RDONLY);
800 if (ioctl(fd, DIOCGPART, &info) == 0) {
801 if (info.fstype >= 0 && info.fstype < (int)FSMAXTYPES) {
802 if (fstype_to_vfsname[info.fstype]) {
803 *vfstypep = fstype_to_vfsname[info.fstype];
804 } else if (iscdmedia(path, fd)) {
805 *vfstypep = "cd9660";
808 "mount: warning: fstype in disklabel "
809 "not set to anything I understand\n");
811 "attempting to mount with -t ufs\n");