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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $";
48 #include <sys/param.h>
49 #include <sys/mount.h>
65 #include "pathnames.h"
68 #define MOUNT_META_OPTION_FSTAB "fstab"
69 #define MOUNT_META_OPTION_CURRENT "current"
71 int debug, fstab_style, verbose;
73 char *catopt __P((char *, const char *));
75 *getmntpt __P((const char *));
76 int hasopt __P((const char *, const char *));
77 int ismounted __P((struct fstab *, struct statfs *, int));
78 int isremountable __P((const char *));
79 void mangle __P((char *, int *, const char **));
80 char *update_options __P((char *, char *, int));
81 int mountfs __P((const char *, const char *, const char *,
82 int, const char *, const char *));
83 void remopt __P((char *, const char *));
84 void prmount __P((struct statfs *));
85 void putfsent __P((const struct statfs *));
86 void usage __P((void));
87 char *flags2opts __P((int));
89 /* Map from mount options to printable formats. */
94 { MNT_ASYNC, "asynchronous" },
95 { MNT_EXPORTED, "NFS exported" },
96 { MNT_LOCAL, "local" },
97 { MNT_NOATIME, "noatime" },
98 { MNT_NODEV, "nodev" },
99 { MNT_NOEXEC, "noexec" },
100 { MNT_NOSUID, "nosuid" },
101 { MNT_NOSYMFOLLOW, "nosymfollow" },
102 { MNT_QUOTA, "with quotas" },
103 { MNT_RDONLY, "read-only" },
104 { MNT_SYNCHRONOUS, "synchronous" },
105 { MNT_UNION, "union" },
106 { MNT_NOCLUSTERR, "noclusterr" },
107 { MNT_NOCLUSTERW, "noclusterw" },
108 { MNT_SUIDDIR, "suiddir" },
109 { MNT_SOFTDEP, "soft-updates" },
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",
129 const char *mntfromname, **vfslist, *vfstype;
131 struct statfs *mntbuf;
134 int all, ch, i, init_flags, mntsize, rval, have_fstab;
137 all = init_flags = 0;
141 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
150 init_flags |= MNT_FORCE;
154 options = catopt(options, optarg);
161 options = catopt(options, "ro");
165 errx(1, "only one -t option may be specified");
166 vfslist = makevfslist(optarg);
170 init_flags |= MNT_UPDATE;
176 options = catopt(options, "noro");
186 #define BADTYPE(type) \
187 (strcmp(type, FSTAB_RO) && \
188 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
193 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
194 err(1, "getmntinfo");
196 while ((fs = getfsent()) != NULL) {
197 if (BADTYPE(fs->fs_type))
199 if (checkvfsname(fs->fs_vfstype, vfslist))
201 if (hasopt(fs->fs_mntops, "noauto"))
203 if (!(init_flags & MNT_UPDATE) &&
204 ismounted(fs, mntbuf, mntsize))
206 if (mountfs(fs->fs_vfstype, fs->fs_spec,
207 fs->fs_file, init_flags, options,
211 } else if (fstab_style) {
212 for (i = 0; i < mntsize; i++) {
213 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
215 putfsent(&mntbuf[i]);
218 for (i = 0; i < mntsize; i++) {
219 if (checkvfsname(mntbuf[i].f_fstypename,
230 if (init_flags & MNT_UPDATE) {
233 if ((mntbuf = getmntpt(*argv)) == NULL)
234 errx(1, "not currently mounted %s", *argv);
236 * Only get the mntflags from fstab if both mntpoint
237 * and mntspec are identical. Also handle the special
238 * case where just '/' is mounted and 'spec' is not
239 * identical with the one from fstab ('/dev' is missing
240 * in the spec-string at boot-time).
242 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
243 if (strcmp(fs->fs_spec,
244 mntbuf->f_mntfromname) == 0 &&
246 mntbuf->f_mntonname) == 0) {
248 mntfromname = mntbuf->f_mntfromname;
249 } else if (argv[0][0] == '/' &&
250 argv[0][1] == '\0') {
253 mntfromname = fs->fs_spec;
257 options = update_options(options, fs->fs_mntops,
260 mntfromname = mntbuf->f_mntfromname;
261 options = update_options(options, NULL,
264 rval = mountfs(mntbuf->f_fstypename, mntfromname,
265 mntbuf->f_mntonname, init_flags, options, 0);
268 rmslashes(*argv, *argv);
269 if ((fs = getfsfile(*argv)) == NULL &&
270 (fs = getfsspec(*argv)) == NULL)
271 errx(1, "%s: unknown special file or file system",
273 if (BADTYPE(fs->fs_type))
274 errx(1, "%s has unknown file system type",
276 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
277 init_flags, options, fs->fs_mntops);
281 * If -t flag has not been specified, the path cannot be
282 * found, spec contains either a ':' or a '@', and the
283 * spec is not a file with those characters, then assume
284 * that an NFS filesystem is being specified ala Sun.
286 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
287 access(argv[0], 0) == -1)
289 rval = mountfs(vfstype,
290 argv[0], argv[1], init_flags, options, NULL);
298 * If the mount was successfully, and done by root, tell mountd the
299 * good news. Pid checks are probably unnecessary, but don't hurt.
301 if (rval == 0 && getuid() == 0 &&
302 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
303 if (fscanf(mountdfp, "%d", &pid) == 1 &&
304 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
305 err(1, "signal mountd");
306 (void)fclose(mountdfp);
313 ismounted(fs, mntbuf, mntsize)
315 struct statfs *mntbuf;
320 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
321 /* the root file system can always be remounted */
324 for (i = mntsize - 1; i >= 0; --i)
325 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
326 (!isremountable(fs->fs_vfstype) ||
327 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
333 isremountable(vfsname)
338 for (cp = remountable_fs_names; *cp; cp++)
339 if (strcmp(*cp, vfsname) == 0)
345 hasopt(mntopts, option)
346 const char *mntopts, *option;
351 if (option[0] == 'n' && option[1] == 'o') {
356 optbuf = strdup(mntopts);
358 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
359 if (opt[0] == 'n' && opt[1] == 'o') {
360 if (!strcasecmp(opt + 2, option))
362 } else if (!strcasecmp(opt, option))
370 mountfs(vfstype, spec, name, flags, options, mntopts)
371 const char *vfstype, *spec, *name, *options, *mntopts;
374 /* List of directories containing mount_xxx subcommands. */
375 static const char *edirs[] = {
380 const char *argv[100], **edir;
384 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
391 /* resolve the mountpoint with realpath(3) */
392 (void)checkpath(name, mntpath);
397 if (options == NULL) {
398 if (*mntopts == '\0') {
405 optbuf = catopt(strdup(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");
423 argv[argc++] = vfstype;
424 mangle(optbuf, &argc, argv);
430 (void)printf("exec: mount_%s", vfstype);
431 for (i = 1; i < argc; i++)
432 (void)printf(" %s", argv[i]);
437 switch (pid = fork()) {
438 case -1: /* Error. */
443 if (strcmp(vfstype, "ufs") == 0)
444 exit(mount_ufs(argc, (char * const *) argv));
446 /* Go find an executable. */
447 for (edir = edirs; *edir; edir++) {
448 (void)snprintf(execname,
449 sizeof(execname), "%s/mount_%s", *edir, vfstype);
450 execv(execname, (char * const *)argv);
452 if (errno == ENOENT) {
455 for (edir = edirs; *edir; edir++)
456 len += strlen(*edir) + 2; /* ", " */
457 if ((cp = malloc(len)) == NULL)
458 errx(1, "malloc failed");
460 for (edir = edirs; *edir; edir++) {
465 warn("exec mount_%s not found in %s", vfstype, cp);
469 default: /* Parent. */
472 if (waitpid(pid, &status, 0) < 0) {
477 if (WIFEXITED(status)) {
478 if (WEXITSTATUS(status) != 0)
479 return (WEXITSTATUS(status));
480 } else if (WIFSIGNALED(status)) {
481 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
486 if (statfs(name, &sf) < 0) {
487 warn("statfs %s", name);
509 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
512 flags = sfp->f_flags & MNT_VISFLAGMASK;
513 for (o = optnames; flags && o->o_opt; o++)
514 if (flags & o->o_opt) {
515 (void)printf(", %s", o->o_name);
519 (void)printf(", mounted by ");
520 if ((pw = getpwuid(sfp->f_owner)) != NULL)
521 (void)printf("%s", pw->pw_name);
523 (void)printf("%d", sfp->f_owner);
526 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
527 (void)printf(", writes: sync %ld async %ld",
528 sfp->f_syncwrites, sfp->f_asyncwrites);
529 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
530 (void)printf(", reads: sync %ld async %ld",
531 sfp->f_syncreads, sfp->f_asyncreads);
540 struct statfs *mntbuf;
543 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
544 for (i = mntsize - 1; i >= 0; i--) {
545 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
546 strcmp(mntbuf[i].f_mntonname, name) == 0)
560 if (s1 == NULL || *s1 == '\0')
564 i = strlen(s0) + strlen(s1) + 1 + 1;
565 if ((cp = malloc(i)) == NULL)
566 errx(1, "malloc failed");
567 (void)snprintf(cp, i, "%s,%s", s0, s1);
577 mangle(options, argcp, argv)
586 for (s = options; (p = strsep(&s, ",")) != NULL;)
595 } else if (strcmp(p, "rw") != 0) {
606 update_options(opts, fstab, curflags)
613 char *expopt, *newopt, *tmpopt;
618 /* remove meta options from list */
619 remopt(fstab, MOUNT_META_OPTION_FSTAB);
620 remopt(fstab, MOUNT_META_OPTION_CURRENT);
621 cur = flags2opts(curflags);
624 * Expand all meta-options passed to us first.
627 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
628 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
629 expopt = catopt(expopt, fstab);
630 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
631 expopt = catopt(expopt, cur);
633 expopt = catopt(expopt, o);
639 * Remove previous contradictory arguments. Given option "foo" we
640 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
641 * and "foo" - so we can deal with possible options like "notice".
644 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
645 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
646 errx(1, "malloc failed");
648 strcpy(tmpopt, "no");
650 remopt(newopt, tmpopt);
653 if (strncmp("no", o, 2) == 0)
656 newopt = catopt(newopt, o);
670 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
675 for (p = string; (o = strsep(&p, ",")) != NULL;) {
676 if (strcmp(opt, o) != 0) {
677 if (*r == ',' && *o != '\0')
679 while ((*r++ = *o++) != '\0')
691 (void)fprintf(stderr, "%s\n%s\n%s\n",
692 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
693 " mount [-adfpruvw] [-t ufs | external_type]",
694 " mount [-dfpruvw] special | node");
700 const struct statfs *ent;
705 opts = flags2opts(ent->f_flags);
706 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
707 ent->f_fstypename, opts);
710 if ((fst = getfsspec(ent->f_mntfromname)))
711 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
712 else if ((fst = getfsfile(ent->f_mntonname)))
713 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
714 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
715 if (strcmp(ent->f_mntonname, "/") == 0)
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");