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.6 2004/02/06 22:11:48 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/mount.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 char *catopt(char *, const char *);
66 *getmntpt(const char *);
67 int hasopt(const char *, const char *);
68 int ismounted(struct fstab *, struct statfs *, int);
69 int isremountable(const char *);
70 void mangle(char *, int *, const char **);
71 char *update_options(char *, char *, int);
72 int mountfs(const char *, const char *, const char *,
73 int, const char *, const char *);
74 void remopt(char *, const char *);
75 void prmount(struct statfs *);
76 void putfsent(const struct statfs *);
78 char *flags2opts(int);
80 /* Map from mount options to printable formats. */
85 { MNT_ASYNC, "asynchronous" },
86 { MNT_EXPORTED, "NFS exported" },
87 { MNT_LOCAL, "local" },
88 { MNT_NOATIME, "noatime" },
89 { MNT_NODEV, "nodev" },
90 { MNT_NOEXEC, "noexec" },
91 { MNT_NOSUID, "nosuid" },
92 { MNT_NOSYMFOLLOW, "nosymfollow" },
93 { MNT_QUOTA, "with quotas" },
94 { MNT_RDONLY, "read-only" },
95 { MNT_SYNCHRONOUS, "synchronous" },
96 { MNT_UNION, "union" },
97 { MNT_NOCLUSTERR, "noclusterr" },
98 { MNT_NOCLUSTERW, "noclusterw" },
99 { MNT_SUIDDIR, "suiddir" },
100 { MNT_SOFTDEP, "soft-updates" },
105 * List of VFS types that can be remounted without becoming mounted on top
107 * XXX Is this list correct?
110 remountable_fs_names[] = {
111 "ufs", "ffs", "ext2fs",
116 main(int argc, char **argv)
118 const char *mntfromname, **vfslist, *vfstype;
120 struct statfs *mntbuf;
123 int all, ch, i, init_flags, mntsize, rval, have_fstab;
126 all = init_flags = 0;
130 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
139 init_flags |= MNT_FORCE;
143 options = catopt(options, optarg);
150 options = catopt(options, "ro");
154 errx(1, "only one -t option may be specified");
155 vfslist = makevfslist(optarg);
159 init_flags |= MNT_UPDATE;
165 options = catopt(options, "noro");
175 #define BADTYPE(type) \
176 (strcmp(type, FSTAB_RO) && \
177 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
182 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
183 err(1, "getmntinfo");
185 while ((fs = getfsent()) != NULL) {
186 if (BADTYPE(fs->fs_type))
188 if (checkvfsname(fs->fs_vfstype, vfslist))
190 if (hasopt(fs->fs_mntops, "noauto"))
192 if (!(init_flags & MNT_UPDATE) &&
193 ismounted(fs, mntbuf, mntsize))
195 options = update_options(options,
196 fs->fs_mntops, mntbuf->f_flags);
197 if (mountfs(fs->fs_vfstype, fs->fs_spec,
198 fs->fs_file, init_flags, options,
202 } else if (fstab_style) {
203 for (i = 0; i < mntsize; i++) {
204 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
206 putfsent(&mntbuf[i]);
209 for (i = 0; i < mntsize; i++) {
210 if (checkvfsname(mntbuf[i].f_fstypename,
221 if (init_flags & MNT_UPDATE) {
224 if ((mntbuf = getmntpt(*argv)) == NULL)
225 errx(1, "not currently mounted %s", *argv);
227 * Only get the mntflags from fstab if both mntpoint
228 * and mntspec are identical. Also handle the special
229 * case where just '/' is mounted and 'spec' is not
230 * identical with the one from fstab ('/dev' is missing
231 * in the spec-string at boot-time).
233 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
234 if (strcmp(fs->fs_spec,
235 mntbuf->f_mntfromname) == 0 &&
237 mntbuf->f_mntonname) == 0) {
239 mntfromname = mntbuf->f_mntfromname;
240 } else if (argv[0][0] == '/' &&
241 argv[0][1] == '\0') {
244 mntfromname = fs->fs_spec;
248 options = update_options(options, fs->fs_mntops,
251 mntfromname = mntbuf->f_mntfromname;
252 options = update_options(options, NULL,
255 rval = mountfs(mntbuf->f_fstypename, mntfromname,
256 mntbuf->f_mntonname, init_flags, options, 0);
259 rmslashes(*argv, *argv);
260 if ((fs = getfsfile(*argv)) == NULL &&
261 (fs = getfsspec(*argv)) == NULL)
262 errx(1, "%s: unknown special file or file system",
264 if (BADTYPE(fs->fs_type))
265 errx(1, "%s has unknown file system type",
267 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
268 init_flags, options, fs->fs_mntops);
272 * If -t flag has not been specified, the path cannot be
273 * found, spec contains either a ':' or a '@', and the
274 * spec is not a file with those characters, then assume
275 * that an NFS filesystem is being specified ala Sun.
277 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
278 access(argv[0], 0) == -1)
280 rval = mountfs(vfstype,
281 argv[0], argv[1], 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");
297 (void)fclose(mountdfp);
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 = strdup(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 (void)checkpath(name, mntpath);
382 if (options == NULL) {
383 if (*mntopts == '\0') {
390 optbuf = catopt(strdup(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 (void)printf("exec: mount_%s", vfstype);
416 for (i = 1; i < argc; i++)
417 (void)printf(" %s", argv[i]);
422 switch (pid = fork()) {
423 case -1: /* Error. */
428 if (strcmp(vfstype, "ufs") == 0)
429 exit(mount_ufs(argc, (char * const *) argv));
431 /* Go find an executable. */
432 for (edir = edirs; *edir; edir++) {
433 (void)snprintf(execname,
434 sizeof(execname), "%s/mount_%s", *edir, vfstype);
435 execv(execname, (char * const *)argv);
437 if (errno == ENOENT) {
440 for (edir = edirs; *edir; edir++)
441 len += strlen(*edir) + 2; /* ", " */
442 if ((cp = malloc(len)) == NULL)
443 errx(1, "malloc failed");
445 for (edir = edirs; *edir; edir++) {
450 warn("exec mount_%s not found in %s", vfstype, cp);
454 default: /* Parent. */
457 if (waitpid(pid, &status, 0) < 0) {
462 if (WIFEXITED(status)) {
463 if (WEXITSTATUS(status) != 0)
464 return (WEXITSTATUS(status));
465 } else if (WIFSIGNALED(status)) {
466 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
471 if (statfs(name, &sf) < 0) {
472 warn("statfs %s", name);
487 prmount(struct statfs *sfp)
493 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
496 flags = sfp->f_flags & MNT_VISFLAGMASK;
497 for (o = optnames; flags && o->o_opt; o++)
498 if (flags & o->o_opt) {
499 (void)printf(", %s", o->o_name);
503 (void)printf(", mounted by ");
504 if ((pw = getpwuid(sfp->f_owner)) != NULL)
505 (void)printf("%s", pw->pw_name);
507 (void)printf("%d", sfp->f_owner);
510 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
511 (void)printf(", writes: sync %ld async %ld",
512 sfp->f_syncwrites, sfp->f_asyncwrites);
513 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
514 (void)printf(", reads: sync %ld async %ld",
515 sfp->f_syncreads, sfp->f_asyncreads);
521 getmntpt(const char *name)
523 struct statfs *mntbuf;
526 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
527 for (i = mntsize - 1; i >= 0; i--) {
528 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
529 strcmp(mntbuf[i].f_mntonname, name) == 0)
536 catopt(char *s0, const char *s1)
541 if (s1 == NULL || *s1 == '\0')
545 i = strlen(s0) + strlen(s1) + 1 + 1;
546 if ((cp = malloc(i)) == NULL)
547 errx(1, "malloc failed");
548 (void)snprintf(cp, i, "%s,%s", s0, s1);
558 mangle(char *options, int *argcp, const char **argv)
564 for (s = options; (p = strsep(&s, ",")) != NULL;)
573 } else if (strcmp(p, "rw") != 0) {
584 update_options(char *opts, char *fstab, int curflags)
588 char *expopt, *newopt, *tmpopt;
593 /* remove meta options from list */
594 remopt(fstab, MOUNT_META_OPTION_FSTAB);
595 remopt(fstab, MOUNT_META_OPTION_CURRENT);
596 cur = flags2opts(curflags);
599 * Expand all meta-options passed to us first.
602 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
603 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
604 expopt = catopt(expopt, fstab);
605 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
606 expopt = catopt(expopt, cur);
608 expopt = catopt(expopt, o);
614 * Remove previous contradictory arguments. Given option "foo" we
615 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
616 * and "foo" - so we can deal with possible options like "notice".
619 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
620 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
621 errx(1, "malloc failed");
623 strcpy(tmpopt, "no");
625 remopt(newopt, tmpopt);
628 if (strncmp("no", o, 2) == 0)
631 newopt = catopt(newopt, o);
639 remopt(char *string, const char *opt)
643 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
648 for (p = string; (o = strsep(&p, ",")) != NULL;) {
649 if (strcmp(opt, o) != 0) {
650 if (*r == ',' && *o != '\0')
652 while ((*r++ = *o++) != '\0')
664 (void)fprintf(stderr, "%s\n%s\n%s\n",
665 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
666 " mount [-adfpruvw] [-o options] [-t ufs | external_type]",
667 " mount [-dfpruvw] special | node");
672 putfsent(const struct statfs *ent)
677 opts = flags2opts(ent->f_flags);
678 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
679 ent->f_fstypename, opts);
682 if ((fst = getfsspec(ent->f_mntfromname)))
683 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
684 else if ((fst = getfsfile(ent->f_mntonname)))
685 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
686 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
687 if (strcmp(ent->f_mntonname, "/") == 0)
697 flags2opts(int flags)
703 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
705 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
706 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
707 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
708 if (flags & MNT_NODEV) res = catopt(res, "nodev");
709 if (flags & MNT_UNION) res = catopt(res, "union");
710 if (flags & MNT_ASYNC) res = catopt(res, "async");
711 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
712 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
713 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
714 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
715 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");