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 $
36 * $DragonFly: src/sbin/mount/mount.c,v 1.10 2005/04/03 17:13:08 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/mountctl.h>
43 #include <sys/dtype.h>
44 #include <sys/diskslice.h>
61 #include "pathnames.h"
64 #define MOUNT_META_OPTION_FSTAB "fstab"
65 #define MOUNT_META_OPTION_CURRENT "current"
67 int debug, fstab_style, verbose;
69 static char *catopt(char *, const char *);
71 *getmntpt(const char *);
72 static int hasopt(const char *, const char *);
73 static int ismounted(struct fstab *, struct statfs *, int);
74 static int isremountable(const char *);
75 static void mangle(char *, int *, const char **);
76 static char *update_options(char *, char *, int);
77 static int mountfs(const char *, const char *, const char *,
78 int, const char *, const char *);
79 static void remopt(char *, const char *);
80 static void printdefvals(const struct statfs *);
81 static void prmount(struct statfs *);
82 static void putfsent(const struct statfs *);
83 static void usage(void);
84 static char *flags2opts(int);
85 static char *xstrdup(const char *str);
86 static void checkdisklabel(const char *devpath, const char **vfstypep);
89 * List of VFS types that can be remounted without becoming mounted on top
91 * XXX Is this list correct?
93 static const char *remountable_fs_names[] = {
94 "ufs", "ffs", "ext2fs", "hammer", "hammer2",
99 main(int argc, char **argv)
101 const char *mntfromname, **vfslist, *vfstype;
103 struct statfs *mntbuf;
106 int all, ch, i, init_flags, mntsize, rval, have_fstab;
109 all = init_flags = 0;
113 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) {
125 init_flags |= MNT_FORCE;
129 options = catopt(options, optarg);
136 options = catopt(options, "ro");
140 errx(1, "only one -t option may be specified");
141 vfslist = makevfslist(optarg);
145 init_flags |= MNT_UPDATE;
151 options = catopt(options, "noro");
162 #define BADTYPE(type) \
163 (strcmp(type, FSTAB_RO) && \
164 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
169 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
170 err(1, "getmntinfo");
172 while ((fs = getfsent()) != NULL) {
173 if (BADTYPE(fs->fs_type))
175 if (checkvfsname(fs->fs_vfstype, vfslist))
177 if (hasopt(fs->fs_mntops, "noauto"))
179 if (!(init_flags & MNT_UPDATE) &&
180 ismounted(fs, mntbuf, mntsize))
182 options = update_options(options,
183 fs->fs_mntops, mntbuf->f_flags);
184 if (mountfs(fs->fs_vfstype, fs->fs_spec,
185 fs->fs_file, init_flags, options,
189 } else if (fstab_style) {
190 for (i = 0; i < mntsize; i++) {
191 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
193 putfsent(&mntbuf[i]);
196 for (i = 0; i < mntsize; i++) {
197 if (checkvfsname(mntbuf[i].f_fstypename,
208 rmslashes(*argv, *argv);
210 if (init_flags & MNT_UPDATE) {
213 if ((mntbuf = getmntpt(*argv)) == NULL)
214 errx(1, "not currently mounted %s", *argv);
216 * Only get the mntflags from fstab if both mntpoint
217 * and mntspec are identical. Also handle the special
218 * case where just '/' is mounted and 'spec' is not
219 * identical with the one from fstab ('/dev' is missing
220 * in the spec-string at boot-time).
222 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
223 if (strcmp(fs->fs_spec,
224 mntbuf->f_mntfromname) == 0 &&
226 mntbuf->f_mntonname) == 0) {
228 mntfromname = mntbuf->f_mntfromname;
229 } else if (argv[0][0] == '/' &&
230 argv[0][1] == '\0') {
233 mntfromname = fs->fs_spec;
237 options = update_options(options, fs->fs_mntops,
240 mntfromname = mntbuf->f_mntfromname;
241 options = update_options(options, NULL,
244 rval = mountfs(mntbuf->f_fstypename, mntfromname,
245 mntbuf->f_mntonname, init_flags, options, 0);
248 if ((fs = getfsfile(*argv)) == NULL &&
249 (fs = getfsspec(*argv)) == NULL)
250 errx(1, "%s: unknown special file or file system",
252 if (BADTYPE(fs->fs_type))
253 errx(1, "%s has unknown file system type",
255 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
256 init_flags, options, fs->fs_mntops);
260 * If -t flag has not been specified, the path cannot be
263 * If the spec is not a file and contains a ':' then assume
266 * If the spec is a cdev attempt to extract the fstype from
269 * When all else fails ufs is assumed.
271 if (vfslist == NULL) {
272 if (strpbrk(argv[0], ":") != NULL &&
273 access(argv[0], 0) == -1) {
276 checkdisklabel(argv[0], &vfstype);
280 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
281 init_flags, options, NULL);
289 * If the mount was successfully, and done by root, tell mountd the
290 * good news. Pid checks are probably unnecessary, but don't hurt.
292 if (rval == 0 && getuid() == 0 &&
293 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
294 if (fscanf(mountdfp, "%d", &pid) == 1 &&
295 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
296 err(1, "signal mountd");
304 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
308 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
309 /* the root file system can always be remounted */
312 for (i = mntsize - 1; i >= 0; --i)
313 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
314 (!isremountable(fs->fs_vfstype) ||
315 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
321 isremountable(const char *vfsname)
325 for (cp = remountable_fs_names; *cp; cp++)
326 if (strcmp(*cp, vfsname) == 0)
332 hasopt(const char *mntopts, const char *option)
337 if (option[0] == 'n' && option[1] == 'o') {
342 optbuf = xstrdup(mntopts);
344 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
345 if (opt[0] == 'n' && opt[1] == 'o') {
346 if (!strcasecmp(opt + 2, option))
348 } else if (!strcasecmp(opt, option))
356 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
357 const char *options, const char *mntopts)
359 /* List of directories containing mount_xxx subcommands. */
360 static const char *edirs[] = {
365 const char *argv[100], **edir;
369 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
376 /* resolve the mountpoint with realpath(3) */
377 checkpath(name, mntpath);
382 if (options == NULL) {
383 if (*mntopts == '\0') {
390 optbuf = catopt(xstrdup(mntopts), options);
392 if (strcmp(name, "/") == 0)
394 if (flags & MNT_FORCE)
395 optbuf = catopt(optbuf, "force");
396 if (flags & MNT_RDONLY)
397 optbuf = catopt(optbuf, "ro");
400 * The mount_mfs (newfs) command uses -o to select the
401 * optimization mode. We don't pass the default "-o rw"
404 if (flags & MNT_UPDATE)
405 optbuf = catopt(optbuf, "update");
408 argv[argc++] = vfstype;
409 mangle(optbuf, &argc, argv);
415 printf("exec: mount_%s", vfstype);
416 for (i = 1; i < argc; i++)
417 printf(" %s", argv[i]);
422 switch (pid = fork()) {
423 case -1: /* Error. */
428 /* Go find an executable. */
429 for (edir = edirs; *edir; edir++) {
431 sizeof(execname), "%s/mount_%s", *edir, vfstype);
432 execv(execname, __DECONST(char * const *, argv));
434 if (errno == ENOENT) {
437 for (edir = edirs; *edir; edir++)
438 len += strlen(*edir) + 2; /* ", " */
439 if ((cp = malloc(len)) == NULL)
440 errx(1, "malloc failed");
442 for (edir = edirs; *edir; edir++) {
447 warn("exec mount_%s not found in %s", vfstype, cp);
451 default: /* Parent. */
454 if (waitpid(pid, &status, 0) < 0) {
459 if (WIFEXITED(status)) {
460 if (WEXITSTATUS(status) != 0)
461 return (WEXITSTATUS(status));
462 } else if (WIFSIGNALED(status)) {
463 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
468 if (statfs(name, &sf) < 0) {
469 warn("statfs %s", name);
484 prmount(struct statfs *sfp)
494 if ((buf = malloc(len)) == NULL)
495 errx(1, "malloc failed");
497 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
500 /* Get a string buffer with all the used flags names */
501 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
502 -1, NULL, 0, buf, len);
505 printf(", mounted by ");
506 if ((pw = getpwuid(sfp->f_owner)) != NULL)
507 printf("%s", pw->pw_name);
509 printf("%d", sfp->f_owner);
516 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
517 printf(", writes: sync %ld async %ld",
518 sfp->f_syncwrites, sfp->f_asyncwrites);
519 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
520 printf(", reads: sync %ld async %ld",
521 sfp->f_syncreads, sfp->f_asyncreads);
526 static struct statfs *
527 getmntpt(const char *name)
529 struct statfs *mntbuf;
532 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
533 for (i = mntsize - 1; i >= 0; i--) {
534 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
535 strcmp(mntbuf[i].f_mntonname, name) == 0)
542 catopt(char *s0, const char *s1)
547 if (s1 == NULL || *s1 == '\0')
551 i = strlen(s0) + strlen(s1) + 1 + 1;
552 if ((cp = malloc(i)) == NULL)
553 errx(1, "malloc failed");
554 snprintf(cp, i, "%s,%s", s0, s1);
564 mangle(char *options, int *argcp, const char **argv)
570 for (s = options; (p = strsep(&s, ",")) != NULL;)
579 } else if (strcmp(p, "rw") != 0) {
590 update_options(char *opts, char *fstab, int curflags)
594 char *expopt, *newopt, *tmpopt;
599 /* remove meta options from list */
600 remopt(fstab, MOUNT_META_OPTION_FSTAB);
601 remopt(fstab, MOUNT_META_OPTION_CURRENT);
602 cur = flags2opts(curflags);
605 * Expand all meta-options passed to us first.
608 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
609 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
610 expopt = catopt(expopt, fstab);
611 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
612 expopt = catopt(expopt, cur);
614 expopt = catopt(expopt, o);
620 * Remove previous contradictory arguments. Given option "foo" we
621 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
622 * and "foo" - so we can deal with possible options like "notice".
625 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
626 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
627 errx(1, "malloc failed");
629 strcpy(tmpopt, "no");
631 remopt(newopt, tmpopt);
634 if (strncmp("no", o, 2) == 0)
637 newopt = catopt(newopt, o);
645 remopt(char *string, const char *opt)
649 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
654 for (p = string; (o = strsep(&p, ",")) != NULL;) {
655 if (strcmp(opt, o) != 0) {
656 if (*r == ',' && *o != '\0')
658 while ((*r++ = *o++) != '\0')
670 fprintf(stderr, "%s\n%s\n%s\n",
671 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
672 " mount [-dfpruvw] {special | node}",
673 " mount [-dfpruvw] [-o options] [-t type] special node");
678 * Prints the default values for dump frequency and pass number of fsck.
679 * This happens when mount(8) is called with -p and there is no fstab file
680 * or there is but the values aren't specified.
683 printdefvals(const struct statfs *ent)
685 if (strcmp(ent->f_fstypename, "ufs") == 0) {
686 if (strcmp(ent->f_mntonname, "/") == 0)
696 putfsent(const struct statfs *ent)
702 opts = flags2opts(ent->f_flags);
703 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
704 ent->f_fstypename, opts);
708 * If fstab file is missing don't call getfsspec() or getfsfile()
709 * at all, because the same warning will be printed twice for every
710 * mounted filesystem.
712 if (stat(_PATH_FSTAB, &sb) != -1) {
713 if ((fst = getfsspec(ent->f_mntfromname)))
714 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
715 else if ((fst = getfsfile(ent->f_mntonname)))
716 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
726 flags2opts(int flags)
732 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
734 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
735 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
736 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
737 if (flags & MNT_NODEV) res = catopt(res, "nodev");
738 if (flags & MNT_UNION) res = catopt(res, "union");
739 if (flags & MNT_ASYNC) res = catopt(res, "async");
740 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
741 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
742 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
743 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
744 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
745 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
751 xstrdup(const char *str)
753 char* ret = strdup(str);
755 errx(1, "strdup failed (could not allocate memory)");
761 * Hack 'cd9660' for the default fstype on cd media if no disklabel
765 iscdmedia(const char *path, int fd __unused)
769 if (strrchr(path, '/'))
770 path = strrchr(path, '/') + 1;
771 if (sscanf(path, "acd%d", &n) == 1)
773 if (sscanf(path, "cd%d", &n) == 1)
779 * If the device path is a cdev attempt to access the disklabel to determine
780 * the filesystem type. Adjust *vfstypep if we can figure it out
783 * Ignore any portion of the device path after an '@' or ':'.
786 checkdisklabel(const char *devpath, const char **vfstypep)
789 struct partinfo info;
790 char *path = strdup(devpath);
793 if (strchr(path, '@'))
794 *strchr(path, '@') = 0;
795 if (strchr(path, ':'))
796 *strchr(path, ':') = 0;
798 if (stat(path, &st) < 0)
800 if (!S_ISCHR(st.st_mode))
802 fd = open(path, O_RDONLY);
805 if (ioctl(fd, DIOCGPART, &info) == 0) {
806 if (info.fstype >= 0 && info.fstype < (int)FSMAXTYPES) {
807 if (fstype_to_vfsname[info.fstype]) {
808 *vfstypep = fstype_to_vfsname[info.fstype];
809 } else if (iscdmedia(path, fd)) {
810 *vfstypep = "cd9660";
813 "mount: warning: fstype in disklabel "
814 "not set to anything I understand\n");
816 "attempting to mount with -t ufs\n");