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. 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>
57 #include "pathnames.h"
60 #define MOUNT_META_OPTION_FSTAB "fstab"
61 #define MOUNT_META_OPTION_CURRENT "current"
63 int debug, fstab_style, verbose;
65 static char *catopt(char *, const char *);
67 *getmntpt(const char *);
68 static int hasopt(const char *, const char *);
69 static int ismounted(struct fstab *, struct statfs *, int);
70 static int isremountable(const char *);
71 static void mangle(char *, int *, const char **);
72 static char *update_options(char *, char *, int);
73 static int mountfs(const char *, const char *, const char *,
74 int, const char *, const char *);
75 static void remopt(char *, const char *);
76 static void printdefvals(const struct statfs *);
77 static void prmount(struct statfs *);
78 static void putfsent(const struct statfs *);
79 static void usage(void);
80 static char *flags2opts(int);
81 static char *xstrdup(const char *str);
82 static void checkdisklabel(const char *devpath, const char **vfstypep);
85 * List of VFS types that can be remounted without becoming mounted on top
87 * XXX Is this list correct?
89 static const char *remountable_fs_names[] = {
90 "ufs", "ffs", "ext2fs", "hammer", "hammer2",
100 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
102 /* Mountd is not running. */
106 if (errno != EEXIST) {
107 /* Cannot open pidfile for some reason. */
110 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
111 if (kill(mountdpid, SIGUSR1) == -1)
112 err(1, "signal mountd");
116 main(int argc, char **argv)
118 const char *mntfromname, **vfslist, *vfstype;
120 struct statfs *mntbuf;
121 int all, ch, i, init_flags, mntsize, rval, have_fstab;
124 all = init_flags = 0;
128 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1) {
140 init_flags |= MNT_FORCE;
144 options = catopt(options, optarg);
151 options = catopt(options, "ro");
155 errx(1, "only one -t option may be specified");
156 vfslist = makevfslist(optarg);
160 init_flags |= MNT_UPDATE;
166 options = catopt(options, "noro");
177 #define BADTYPE(type) \
178 (strcmp(type, FSTAB_RO) && \
179 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
184 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
185 err(1, "getmntinfo");
187 while ((fs = getfsent()) != NULL) {
188 if (BADTYPE(fs->fs_type))
190 if (checkvfsname(fs->fs_vfstype, vfslist))
192 if (hasopt(fs->fs_mntops, "noauto"))
194 if (!(init_flags & MNT_UPDATE) &&
195 ismounted(fs, mntbuf, mntsize))
197 options = update_options(options,
198 fs->fs_mntops, mntbuf->f_flags);
199 if (mountfs(fs->fs_vfstype, fs->fs_spec,
200 fs->fs_file, init_flags, options,
204 } else if (fstab_style) {
205 for (i = 0; i < mntsize; i++) {
206 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
208 putfsent(&mntbuf[i]);
211 for (i = 0; i < mntsize; i++) {
212 if (checkvfsname(mntbuf[i].f_fstypename,
223 rmslashes(*argv, *argv);
225 if (init_flags & MNT_UPDATE) {
228 if ((mntbuf = getmntpt(*argv)) == NULL)
229 errx(1, "not currently mounted %s", *argv);
231 * Only get the mntflags from fstab if both mntpoint
232 * and mntspec are identical. Also handle the special
233 * case where just '/' is mounted and 'spec' is not
234 * identical with the one from fstab ('/dev' is missing
235 * in the spec-string at boot-time).
237 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
238 if (strcmp(fs->fs_spec,
239 mntbuf->f_mntfromname) == 0 &&
241 mntbuf->f_mntonname) == 0) {
243 mntfromname = mntbuf->f_mntfromname;
244 } else if (argv[0][0] == '/' &&
245 argv[0][1] == '\0') {
248 mntfromname = fs->fs_spec;
252 options = update_options(options, fs->fs_mntops,
255 mntfromname = mntbuf->f_mntfromname;
256 options = update_options(options, NULL,
259 rval = mountfs(mntbuf->f_fstypename, mntfromname,
260 mntbuf->f_mntonname, init_flags, options, 0);
263 if ((fs = getfsfile(*argv)) == NULL &&
264 (fs = getfsspec(*argv)) == NULL)
265 errx(1, "%s: unknown special file or file system",
267 if (BADTYPE(fs->fs_type))
268 errx(1, "%s has unknown file system type",
270 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
271 init_flags, options, fs->fs_mntops);
275 * If -t flag has not been specified, the path cannot be
278 * If the spec is not a file and contains a ':' then assume
281 * If the spec is not a file and contains a '@' then assume
282 * HAMMER2. Note that there may not be a raw device
283 * specified in this situation.
285 * If the spec is a cdev attempt to extract the fstype from
288 * When all else fails ufs is assumed.
290 if (vfslist == NULL) {
291 if (strpbrk(argv[0], ":") != NULL &&
292 access(argv[0], 0) == -1) {
294 } else if (strpbrk(argv[0], "@") != NULL &&
295 access(argv[0], 0) == -1) {
298 checkdisklabel(argv[0], &vfstype);
302 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
303 init_flags, options, NULL);
311 * If the mount was successfully, and done by root, tell mountd the
314 if (rval == 0 && getuid() == 0)
321 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
325 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
326 /* the root file system can always be remounted */
329 for (i = mntsize - 1; i >= 0; --i)
330 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
331 (!isremountable(fs->fs_vfstype) ||
332 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
338 isremountable(const char *vfsname)
342 for (cp = remountable_fs_names; *cp; cp++)
343 if (strcmp(*cp, vfsname) == 0)
349 hasopt(const char *mntopts, const char *option)
354 if (option[0] == 'n' && option[1] == 'o') {
359 optbuf = xstrdup(mntopts);
361 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
362 if (opt[0] == 'n' && opt[1] == 'o') {
363 if (!strcasecmp(opt + 2, option))
365 } else if (!strcasecmp(opt, option))
373 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
374 const char *options, const char *mntopts)
376 /* List of directories containing mount_xxx subcommands. */
377 static const char *edirs[] = {
382 const char *argv[100], **edir;
388 char execname[MAXPATHLEN + 1];
389 char mntpath[MAXPATHLEN];
391 /* resolve the mountpoint with realpath(3) */
392 checkpath(name, mntpath);
397 if (options == NULL) {
398 if (*mntopts == '\0') {
405 optbuf = catopt(xstrdup(mntopts), options);
407 if (strcmp(name, "/") == 0)
409 if (flags & MNT_FORCE)
410 optbuf = catopt(optbuf, "force");
411 if (flags & MNT_RDONLY)
412 optbuf = catopt(optbuf, "ro");
415 * The mount_mfs (newfs) command uses -o to select the
416 * optimization mode. We don't pass the default "-o rw"
419 if (flags & MNT_UPDATE)
420 optbuf = catopt(optbuf, "update");
422 asprintf(&argv0, "mount_%s", vfstype);
425 argv[argc++] = argv0;
426 mangle(optbuf, &argc, argv);
432 printf("exec: mount_%s", vfstype);
433 for (i = 1; i < argc; i++)
434 printf(" %s", argv[i]);
439 switch (pid = fork()) {
440 case -1: /* Error. */
445 /* Go find an executable. */
446 for (edir = edirs; *edir; edir++) {
447 snprintf(execname, sizeof(execname),
448 "%s/mount_%s", *edir, vfstype);
449 execv(execname, __DECONST(char * const *, argv));
451 if (errno == ENOENT) {
454 for (edir = edirs; *edir; edir++)
455 len += strlen(*edir) + 2; /* ", " */
456 if ((cp = malloc(len)) == NULL)
457 errx(1, "malloc failed");
459 for (edir = edirs; *edir; edir++) {
464 warn("exec mount_%s not found in %s", vfstype, cp);
468 default: /* Parent. */
471 if (waitpid(pid, &status, 0) < 0) {
476 if (WIFEXITED(status)) {
477 if (WEXITSTATUS(status) != 0)
478 return (WEXITSTATUS(status));
479 } else if (WIFSIGNALED(status)) {
480 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
485 if (statfs(name, &sf) < 0) {
486 warn("statfs %s", name);
501 prmount(struct statfs *sfp)
511 if ((buf = malloc(len)) == NULL)
512 errx(1, "malloc failed");
514 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
517 /* Get a string buffer with all the used flags names */
518 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
519 -1, NULL, 0, buf, len);
522 printf(", mounted by ");
523 if ((pw = getpwuid(sfp->f_owner)) != NULL)
524 printf("%s", pw->pw_name);
526 printf("%d", sfp->f_owner);
529 if (error != -1 && strlen(buf))
533 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
534 printf(", writes: sync %ld async %ld",
535 sfp->f_syncwrites, sfp->f_asyncwrites);
536 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
537 printf(", reads: sync %ld async %ld",
538 sfp->f_syncreads, sfp->f_asyncreads);
543 static struct statfs *
544 getmntpt(const char *name)
546 struct statfs *mntbuf;
549 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
550 for (i = mntsize - 1; i >= 0; i--) {
551 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
552 strcmp(mntbuf[i].f_mntonname, name) == 0)
559 catopt(char *s0, const char *s1)
564 if (s1 == NULL || *s1 == '\0')
568 i = strlen(s0) + strlen(s1) + 1 + 1;
569 if ((cp = malloc(i)) == NULL)
570 errx(1, "malloc failed");
571 snprintf(cp, i, "%s,%s", s0, s1);
581 mangle(char *options, int *argcp, const char **argv)
587 for (s = options; (p = strsep(&s, ",")) != NULL;)
596 } else if (strcmp(p, "rw") != 0) {
607 update_options(char *opts, char *fstab, int curflags)
611 char *expopt, *newopt, *tmpopt;
616 /* remove meta options from list */
617 remopt(fstab, MOUNT_META_OPTION_FSTAB);
618 remopt(fstab, MOUNT_META_OPTION_CURRENT);
619 cur = flags2opts(curflags);
622 * Expand all meta-options passed to us first.
625 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
626 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
627 expopt = catopt(expopt, fstab);
628 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
629 expopt = catopt(expopt, cur);
631 expopt = catopt(expopt, o);
637 * Remove previous contradictory arguments. Given option "foo" we
638 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
639 * and "foo" - so we can deal with possible options like "notice".
642 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
643 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
644 errx(1, "malloc failed");
646 strcpy(tmpopt, "no");
648 remopt(newopt, tmpopt);
651 if (strncmp("no", o, 2) == 0)
654 newopt = catopt(newopt, o);
662 remopt(char *string, const char *opt)
666 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
671 for (p = string; (o = strsep(&p, ",")) != NULL;) {
672 if (strcmp(opt, o) != 0) {
673 if (*r == ',' && *o != '\0')
675 while ((*r++ = *o++) != '\0')
687 fprintf(stderr, "%s\n%s\n%s\n",
688 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
689 " mount [-dfpruvw] {special | node}",
690 " mount [-dfpruvw] [-o options] [-t type] special node");
695 * Prints the default values for dump frequency and pass number of fsck.
696 * This happens when mount(8) is called with -p and there is no fstab file
697 * or there is but the values aren't specified.
700 printdefvals(const struct statfs *ent)
702 if (strcmp(ent->f_fstypename, "ufs") == 0) {
703 if (strcmp(ent->f_mntonname, "/") == 0)
713 putfsent(const struct statfs *ent)
719 opts = flags2opts(ent->f_flags);
720 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
721 ent->f_fstypename, opts);
725 * If fstab file is missing don't call getfsspec() or getfsfile()
726 * at all, because the same warning will be printed twice for every
727 * mounted filesystem.
729 if (stat(_PATH_FSTAB, &sb) != -1) {
730 if ((fst = getfsspec(ent->f_mntfromname)))
731 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
732 else if ((fst = getfsfile(ent->f_mntonname)))
733 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
743 flags2opts(int flags)
749 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
751 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
752 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
753 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
754 if (flags & MNT_NODEV) res = catopt(res, "nodev");
755 if (flags & MNT_ASYNC) res = catopt(res, "async");
756 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
757 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
758 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
759 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
760 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
761 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
767 xstrdup(const char *str)
769 char* ret = strdup(str);
771 errx(1, "strdup failed (could not allocate memory)");
777 * Hack 'cd9660' for the default fstype on cd media if no disklabel
781 iscdmedia(const char *path, int fd __unused)
785 if (strrchr(path, '/'))
786 path = strrchr(path, '/') + 1;
787 if (sscanf(path, "acd%d", &n) == 1)
789 if (sscanf(path, "cd%d", &n) == 1)
795 * If the device path is a cdev attempt to access the disklabel to determine
796 * the filesystem type. Adjust *vfstypep if we can figure it out
799 * Ignore any portion of the device path after an '@' or ':'.
802 checkdisklabel(const char *devpath, const char **vfstypep)
805 struct partinfo info;
806 char *path = strdup(devpath);
809 if (strchr(path, '@'))
810 *strchr(path, '@') = 0;
811 if (strchr(path, ':'))
812 *strchr(path, ':') = 0;
814 if (stat(path, &st) < 0)
816 if (!S_ISCHR(st.st_mode))
818 fd = open(path, O_RDONLY);
821 if (ioctl(fd, DIOCGPART, &info) == 0) {
822 if (info.fstype >= 0 && info.fstype < (int)FSMAXTYPES) {
823 if (fstype_to_vfsname[info.fstype]) {
824 *vfstypep = fstype_to_vfsname[info.fstype];
825 } else if (iscdmedia(path, fd)) {
826 *vfstypep = "cd9660";
829 "mount: warning: fstype in disklabel "
830 "not set to anything I understand\n");
832 "attempting to mount with -t ufs\n");