2 * Copyright (c) 1999-2004 Poul-Henning Kamp
3 * Copyright (c) 1999 Michael Smith
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/filedesc.h>
53 #include <sys/reboot.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysproto.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
63 #include <geom/geom.h>
65 #include <machine/stdarg.h>
67 #include <security/audit/audit.h>
68 #include <security/mac/mac_framework.h>
70 #include "opt_rootdevname.h"
72 #define ROOTNAME "root_device"
73 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
75 static void set_rootvnode(void);
76 static int vfs_domount(struct thread *td, const char *fstype,
77 char *fspath, int fsflags, void *fsdata);
78 static int vfs_mountroot_ask(void);
79 static int vfs_mountroot_try(const char *mountfrom, const char *options);
80 static void free_mntarg(struct mntarg *ma);
82 static int usermount = 0;
83 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
84 "Unprivileged users may mount and unmount file systems");
86 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
87 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
88 static uma_zone_t mount_zone;
90 /* List of mounted filesystems. */
91 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
93 /* For any iteration/modification of mountlist */
94 struct mtx mountlist_mtx;
95 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
98 * The vnode of the system's root (/ in the filesystem, without chroot
101 struct vnode *rootvnode;
104 * The root filesystem is detailed in the kernel environment variable
105 * vfs.root.mountfrom, which is expected to be in the general format
107 * <vfsname>:[<path>][ <vfsname>:[<path>] ...]
108 * vfsname := the name of a VFS known to the kernel and capable
109 * of being mounted as root
110 * path := disk device name or other data used by the filesystem
111 * to locate its physical store
113 * If the environment variable vfs.root.mountfrom is a space separated list,
114 * each list element is tried in turn and the root filesystem will be mounted
115 * from the first one that suceeds.
117 * The environment variable vfs.root.mountfrom.options is a comma delimited
118 * set of string mount options. These mount options must be parseable
119 * by nmount() in the kernel.
123 * Global opts, taken by all filesystems
125 static const char *global_opts[] = {
137 * The root specifiers we will try if RB_CDROM is specified.
139 static char *cdrom_rootdevnames[] = {
145 /* legacy find-root code */
146 char *rootdevnames[2] = {NULL, NULL};
148 # define ROOTDEVNAME NULL
150 static const char *ctrootdevname = ROOTDEVNAME;
153 * ---------------------------------------------------------------------
154 * Functions for building and sanitizing the mount options
157 /* Remove one mount option. */
159 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
162 TAILQ_REMOVE(opts, opt, link);
163 free(opt->name, M_MOUNT);
164 if (opt->value != NULL)
165 free(opt->value, M_MOUNT);
169 /* Release all resources related to the mount options. */
171 vfs_freeopts(struct vfsoptlist *opts)
175 while (!TAILQ_EMPTY(opts)) {
176 opt = TAILQ_FIRST(opts);
177 vfs_freeopt(opts, opt);
183 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
185 struct vfsopt *opt, *temp;
189 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
190 if (strcmp(opt->name, name) == 0)
191 vfs_freeopt(opts, opt);
196 * Check if options are equal (with or without the "no" prefix).
199 vfs_equalopts(const char *opt1, const char *opt2)
203 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
204 if (strcmp(opt1, opt2) == 0)
206 /* "noopt" vs. "opt" */
207 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
209 /* "opt" vs. "noopt" */
210 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
212 while ((p = strchr(opt1, '.')) != NULL &&
213 !strncmp(opt1, opt2, ++p - opt1)) {
216 /* "foo.noopt" vs. "foo.opt" */
217 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
219 /* "foo.opt" vs. "foo.noopt" */
220 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
227 * If a mount option is specified several times,
228 * (with or without the "no" prefix) only keep
229 * the last occurence of it.
232 vfs_sanitizeopts(struct vfsoptlist *opts)
234 struct vfsopt *opt, *opt2, *tmp;
236 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
237 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
238 while (opt2 != NULL) {
239 if (vfs_equalopts(opt->name, opt2->name)) {
240 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
241 vfs_freeopt(opts, opt2);
244 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
251 * Build a linked list of mount options from a struct uio.
254 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
256 struct vfsoptlist *opts;
258 size_t memused, namelen, optlen;
259 unsigned int i, iovcnt;
262 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
265 iovcnt = auio->uio_iovcnt;
266 for (i = 0; i < iovcnt; i += 2) {
267 namelen = auio->uio_iov[i].iov_len;
268 optlen = auio->uio_iov[i + 1].iov_len;
269 memused += sizeof(struct vfsopt) + optlen + namelen;
271 * Avoid consuming too much memory, and attempts to overflow
274 if (memused > VFS_MOUNTARG_SIZE_MAX ||
275 optlen > VFS_MOUNTARG_SIZE_MAX ||
276 namelen > VFS_MOUNTARG_SIZE_MAX) {
281 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
282 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
289 * Do this early, so jumps to "bad" will free the current
292 TAILQ_INSERT_TAIL(opts, opt, link);
294 if (auio->uio_segflg == UIO_SYSSPACE) {
295 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
297 error = copyin(auio->uio_iov[i].iov_base, opt->name,
302 /* Ensure names are null-terminated strings. */
303 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
309 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
310 if (auio->uio_segflg == UIO_SYSSPACE) {
311 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
314 error = copyin(auio->uio_iov[i + 1].iov_base,
321 vfs_sanitizeopts(opts);
330 * Merge the old mount options with the new ones passed
331 * in the MNT_UPDATE case.
333 * XXX This function will keep a "nofoo" option in the
334 * new options if there is no matching "foo" option
335 * to be cancelled in the old options. This is a bug
336 * if the option's canonical name is "foo". E.g., "noro"
337 * shouldn't end up in the mount point's active options,
341 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
343 struct vfsopt *opt, *opt2, *new;
345 TAILQ_FOREACH(opt, opts, link) {
347 * Check that this option hasn't been redefined
348 * nor cancelled with a "no" mount option.
350 opt2 = TAILQ_FIRST(toopts);
351 while (opt2 != NULL) {
352 if (strcmp(opt2->name, opt->name) == 0)
354 if (strncmp(opt2->name, "no", 2) == 0 &&
355 strcmp(opt2->name + 2, opt->name) == 0) {
356 vfs_freeopt(toopts, opt2);
359 opt2 = TAILQ_NEXT(opt2, link);
361 /* We want this option, duplicate it. */
362 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
363 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
364 strcpy(new->name, opt->name);
366 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
367 bcopy(opt->value, new->value, opt->len);
372 new->seen = opt->seen;
373 TAILQ_INSERT_TAIL(toopts, new, link);
380 * Mount a filesystem.
385 struct nmount_args /* {
395 AUDIT_ARG_FFLAGS(uap->flags);
396 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
397 uap->iovp, uap->iovcnt, uap->flags);
400 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
401 * userspace to set this flag, but we must filter it out if we want
402 * MNT_UPDATE on the root file system to work.
403 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
405 uap->flags &= ~MNT_ROOTFS;
407 iovcnt = uap->iovcnt;
409 * Check that we have an even number of iovec's
410 * and that we have at least two options.
412 if ((iovcnt & 1) || (iovcnt < 4)) {
413 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
418 error = copyinuio(uap->iovp, iovcnt, &auio);
420 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
424 error = vfs_donmount(td, uap->flags, auio);
431 * ---------------------------------------------------------------------
432 * Various utility functions
436 vfs_ref(struct mount *mp)
439 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
446 vfs_rel(struct mount *mp)
449 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
456 mount_init(void *mem, int size, int flags)
460 mp = (struct mount *)mem;
461 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
462 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
467 mount_fini(void *mem, int size)
471 mp = (struct mount *)mem;
472 lockdestroy(&mp->mnt_explock);
473 mtx_destroy(&mp->mnt_mtx);
477 * Allocate and initialize the mount point struct.
480 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
485 mp = uma_zalloc(mount_zone, M_WAITOK);
486 bzero(&mp->mnt_startzero,
487 __rangeof(struct mount, mnt_startzero, mnt_endzero));
488 TAILQ_INIT(&mp->mnt_nvnodelist);
489 mp->mnt_nvnodelistsize = 0;
491 (void) vfs_busy(mp, MBF_NOWAIT);
492 mp->mnt_op = vfsp->vfc_vfsops;
494 vfsp->vfc_refcount++; /* XXX Unlocked */
495 mp->mnt_stat.f_type = vfsp->vfc_typenum;
497 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
498 mp->mnt_vnodecovered = vp;
499 mp->mnt_cred = crdup(cred);
500 mp->mnt_stat.f_owner = cred->cr_uid;
501 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
502 mp->mnt_iosize_max = DFLTPHYS;
505 mac_mount_create(cred, mp);
507 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
512 * Destroy the mount struct previously allocated by vfs_mount_alloc().
515 vfs_mount_destroy(struct mount *mp)
519 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
520 if (mp->mnt_kern_flag & MNTK_MWAIT) {
521 mp->mnt_kern_flag &= ~MNTK_MWAIT;
525 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
526 KASSERT(mp->mnt_ref == 0,
527 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
528 __FILE__, __LINE__));
529 if (mp->mnt_writeopcount != 0)
530 panic("vfs_mount_destroy: nonzero writeopcount");
531 if (mp->mnt_secondary_writes != 0)
532 panic("vfs_mount_destroy: nonzero secondary_writes");
533 mp->mnt_vfc->vfc_refcount--;
534 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
537 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
539 panic("unmount: dangling vnode");
541 if (mp->mnt_nvnodelistsize != 0)
542 panic("vfs_mount_destroy: nonzero nvnodelistsize");
543 if (mp->mnt_lockref != 0)
544 panic("vfs_mount_destroy: nonzero lock refcount");
547 mac_mount_destroy(mp);
549 if (mp->mnt_opt != NULL)
550 vfs_freeopts(mp->mnt_opt);
551 crfree(mp->mnt_cred);
552 uma_zfree(mount_zone, mp);
556 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
558 struct vfsoptlist *optlist;
559 struct vfsopt *opt, *noro_opt, *tmp_opt;
560 char *fstype, *fspath, *errmsg;
561 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
562 int has_rw, has_noro;
564 errmsg = fspath = NULL;
565 errmsg_len = has_noro = has_rw = fspathlen = 0;
568 error = vfs_buildopts(fsoptions, &optlist);
572 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
573 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
576 * We need these two options before the others,
577 * and they are mandatory for any filesystem.
578 * Ensure they are NUL terminated as well.
581 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
582 if (error || fstype[fstypelen - 1] != '\0') {
585 strncpy(errmsg, "Invalid fstype", errmsg_len);
589 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
590 if (error || fspath[fspathlen - 1] != '\0') {
593 strncpy(errmsg, "Invalid fspath", errmsg_len);
598 * We need to see if we have the "update" option
599 * before we call vfs_domount(), since vfs_domount() has special
600 * logic based on MNT_UPDATE. This is very important
601 * when we want to update the root filesystem.
603 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
604 if (strcmp(opt->name, "update") == 0) {
605 fsflags |= MNT_UPDATE;
606 vfs_freeopt(optlist, opt);
608 else if (strcmp(opt->name, "async") == 0)
609 fsflags |= MNT_ASYNC;
610 else if (strcmp(opt->name, "force") == 0) {
611 fsflags |= MNT_FORCE;
612 vfs_freeopt(optlist, opt);
614 else if (strcmp(opt->name, "reload") == 0) {
615 fsflags |= MNT_RELOAD;
616 vfs_freeopt(optlist, opt);
618 else if (strcmp(opt->name, "multilabel") == 0)
619 fsflags |= MNT_MULTILABEL;
620 else if (strcmp(opt->name, "noasync") == 0)
621 fsflags &= ~MNT_ASYNC;
622 else if (strcmp(opt->name, "noatime") == 0)
623 fsflags |= MNT_NOATIME;
624 else if (strcmp(opt->name, "atime") == 0) {
625 free(opt->name, M_MOUNT);
626 opt->name = strdup("nonoatime", M_MOUNT);
628 else if (strcmp(opt->name, "noclusterr") == 0)
629 fsflags |= MNT_NOCLUSTERR;
630 else if (strcmp(opt->name, "clusterr") == 0) {
631 free(opt->name, M_MOUNT);
632 opt->name = strdup("nonoclusterr", M_MOUNT);
634 else if (strcmp(opt->name, "noclusterw") == 0)
635 fsflags |= MNT_NOCLUSTERW;
636 else if (strcmp(opt->name, "clusterw") == 0) {
637 free(opt->name, M_MOUNT);
638 opt->name = strdup("nonoclusterw", M_MOUNT);
640 else if (strcmp(opt->name, "noexec") == 0)
641 fsflags |= MNT_NOEXEC;
642 else if (strcmp(opt->name, "exec") == 0) {
643 free(opt->name, M_MOUNT);
644 opt->name = strdup("nonoexec", M_MOUNT);
646 else if (strcmp(opt->name, "nosuid") == 0)
647 fsflags |= MNT_NOSUID;
648 else if (strcmp(opt->name, "suid") == 0) {
649 free(opt->name, M_MOUNT);
650 opt->name = strdup("nonosuid", M_MOUNT);
652 else if (strcmp(opt->name, "nosymfollow") == 0)
653 fsflags |= MNT_NOSYMFOLLOW;
654 else if (strcmp(opt->name, "symfollow") == 0) {
655 free(opt->name, M_MOUNT);
656 opt->name = strdup("nonosymfollow", M_MOUNT);
658 else if (strcmp(opt->name, "noro") == 0) {
659 fsflags &= ~MNT_RDONLY;
662 else if (strcmp(opt->name, "rw") == 0) {
663 fsflags &= ~MNT_RDONLY;
666 else if (strcmp(opt->name, "ro") == 0)
667 fsflags |= MNT_RDONLY;
668 else if (strcmp(opt->name, "rdonly") == 0) {
669 free(opt->name, M_MOUNT);
670 opt->name = strdup("ro", M_MOUNT);
671 fsflags |= MNT_RDONLY;
673 else if (strcmp(opt->name, "suiddir") == 0)
674 fsflags |= MNT_SUIDDIR;
675 else if (strcmp(opt->name, "sync") == 0)
676 fsflags |= MNT_SYNCHRONOUS;
677 else if (strcmp(opt->name, "union") == 0)
678 fsflags |= MNT_UNION;
682 * If "rw" was specified as a mount option, and we
683 * are trying to update a mount-point from "ro" to "rw",
684 * we need a mount option "noro", since in vfs_mergeopts(),
685 * "noro" will cancel "ro", but "rw" will not do anything.
687 if (has_rw && !has_noro) {
688 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
689 noro_opt->name = strdup("noro", M_MOUNT);
690 noro_opt->value = NULL;
694 TAILQ_INSERT_TAIL(optlist, noro_opt, link);
698 * Be ultra-paranoid about making sure the type and fspath
699 * variables will fit in our mp buffers, including the
702 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
703 error = ENAMETOOLONG;
708 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
711 /* copyout the errmsg */
712 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
713 && errmsg_len > 0 && errmsg != NULL) {
714 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
716 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
717 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
720 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
721 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
726 vfs_freeopts(optlist);
733 #ifndef _SYS_SYSPROTO_H_
745 struct mount_args /* {
753 struct vfsconf *vfsp = NULL;
754 struct mntarg *ma = NULL;
757 AUDIT_ARG_FFLAGS(uap->flags);
760 * Filter out MNT_ROOTFS. We do not want clients of mount() in
761 * userspace to set this flag, but we must filter it out if we want
762 * MNT_UPDATE on the root file system to work.
763 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
765 uap->flags &= ~MNT_ROOTFS;
767 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
768 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
770 free(fstype, M_TEMP);
774 AUDIT_ARG_TEXT(fstype);
776 vfsp = vfs_byname_kld(fstype, td, &error);
777 free(fstype, M_TEMP);
782 if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
787 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
788 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
789 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
790 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
791 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
793 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags);
800 * vfs_domount(): actually attempt a filesystem mount.
804 struct thread *td, /* Calling thread. */
805 const char *fstype, /* Filesystem type. */
806 char *fspath, /* Mount path. */
807 int fsflags, /* Flags common to all filesystems. */
808 void *fsdata /* Options local to the filesystem. */
813 struct vfsconf *vfsp;
814 struct oexport_args oexport;
815 struct export_args export;
820 mtx_assert(&Giant, MA_OWNED);
822 * Be ultra-paranoid about making sure the type and fspath
823 * variables will fit in our mp buffers, including the
826 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
827 return (ENAMETOOLONG);
829 if (jailed(td->td_ucred) || usermount == 0) {
830 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
835 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
837 if (fsflags & MNT_EXPORTED) {
838 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
842 if (fsflags & MNT_SUIDDIR) {
843 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
848 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
850 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
851 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
852 fsflags |= MNT_NOSUID | MNT_USER;
855 /* Load KLDs before we lock the covered vnode to avoid reversals. */
857 if ((fsflags & MNT_UPDATE) == 0) {
858 /* Don't try to load KLDs if we're mounting the root. */
859 if (fsflags & MNT_ROOTFS)
860 vfsp = vfs_byname(fstype);
862 vfsp = vfs_byname_kld(fstype, td, &error);
865 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
869 * Get vnode to be covered
871 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
873 if ((error = namei(&nd)) != 0)
875 NDFREE(&nd, NDF_ONLY_PNBUF);
877 if (fsflags & MNT_UPDATE) {
878 if ((vp->v_vflag & VV_ROOT) == 0) {
886 * We only allow the filesystem to be reloaded if it
887 * is currently mounted read-only.
889 if ((fsflags & MNT_RELOAD) &&
890 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
893 return (EOPNOTSUPP); /* Needs translation */
897 * Only privileged root, or (if MNT_USER is set) the user that
898 * did the original mount is permitted to update it.
900 error = vfs_suser(mp, td);
905 if (vfs_busy(mp, MBF_NOWAIT)) {
910 if ((vp->v_iflag & VI_MOUNT) != 0 ||
911 vp->v_mountedhere != NULL) {
917 vp->v_iflag |= VI_MOUNT;
920 mp->mnt_flag |= fsflags &
921 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
924 mp->mnt_optnew = fsdata;
925 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
928 * If the user is not root, ensure that they own the directory
929 * onto which we are attempting to mount.
931 error = VOP_GETATTR(vp, &va, td->td_ucred);
936 if (va.va_uid != td->td_ucred->cr_uid) {
937 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
944 error = vinvalbuf(vp, V_SAVE, 0, 0);
949 if (vp->v_type != VDIR) {
954 if ((vp->v_iflag & VI_MOUNT) != 0 ||
955 vp->v_mountedhere != NULL) {
960 vp->v_iflag |= VI_MOUNT;
964 * Allocate and initialize the filesystem.
966 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
969 /* XXXMAC: pass to vfs_mount_alloc? */
970 mp->mnt_optnew = fsdata;
974 * Set the mount level flags.
977 mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
978 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
980 if ((mp->mnt_flag & MNT_ASYNC) == 0)
981 mp->mnt_kern_flag &= ~MNTK_ASYNC;
984 * Mount the filesystem.
985 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
986 * get. No freeing of cn_pnbuf.
988 error = VFS_MOUNT(mp);
991 * Process the export option only if we are
992 * updating mount options.
994 if (!error && (fsflags & MNT_UPDATE)) {
995 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
996 sizeof(export)) == 0)
997 error = vfs_export(mp, &export);
998 else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
999 sizeof(oexport)) == 0) {
1000 export.ex_flags = oexport.ex_flags;
1001 export.ex_root = oexport.ex_root;
1002 export.ex_anon = oexport.ex_anon;
1003 export.ex_addr = oexport.ex_addr;
1004 export.ex_addrlen = oexport.ex_addrlen;
1005 export.ex_mask = oexport.ex_mask;
1006 export.ex_masklen = oexport.ex_masklen;
1007 export.ex_indexfile = oexport.ex_indexfile;
1008 export.ex_numsecflavors = 0;
1009 error = vfs_export(mp, &export);
1014 if (mp->mnt_opt != NULL)
1015 vfs_freeopts(mp->mnt_opt);
1016 mp->mnt_opt = mp->mnt_optnew;
1017 (void)VFS_STATFS(mp, &mp->mnt_stat);
1020 * Prevent external consumers of mount options from reading
1023 mp->mnt_optnew = NULL;
1024 if (mp->mnt_flag & MNT_UPDATE) {
1027 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1028 (flag & ~MNT_QUOTA);
1030 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD |
1031 MNT_FORCE | MNT_SNAPSHOT);
1032 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1033 mp->mnt_kern_flag |= MNTK_ASYNC;
1035 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1037 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1038 if (mp->mnt_syncer == NULL)
1039 error = vfs_allocate_syncvnode(mp);
1041 if (mp->mnt_syncer != NULL)
1042 vrele(mp->mnt_syncer);
1043 mp->mnt_syncer = NULL;
1047 vp->v_iflag &= ~VI_MOUNT;
1053 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1054 mp->mnt_kern_flag |= MNTK_ASYNC;
1056 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1058 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1060 * Put the new filesystem on the mount list after root.
1064 struct vnode *newdp;
1067 vp->v_iflag &= ~VI_MOUNT;
1069 vp->v_mountedhere = mp;
1070 mtx_lock(&mountlist_mtx);
1071 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1072 mtx_unlock(&mountlist_mtx);
1073 vfs_event_signal(NULL, VQ_MOUNT, 0);
1074 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
1075 panic("mount: lost mount");
1076 VOP_UNLOCK(newdp, 0);
1078 mountcheckdirs(vp, newdp);
1080 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1081 error = vfs_allocate_syncvnode(mp);
1087 vp->v_iflag &= ~VI_MOUNT;
1090 vfs_mount_destroy(mp);
1097 * Unmount a filesystem.
1099 * Note: unmount takes a path to the vnode mounted on as argument, not
1100 * special file (as before).
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct unmount_args {
1112 register struct unmount_args /* {
1119 int error, id0, id1;
1121 AUDIT_ARG_VALUE(uap->flags);
1122 if (jailed(td->td_ucred) || usermount == 0) {
1123 error = priv_check(td, PRIV_VFS_UNMOUNT);
1128 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1129 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1131 free(pathbuf, M_TEMP);
1135 if (uap->flags & MNT_BYFSID) {
1136 AUDIT_ARG_TEXT(pathbuf);
1137 /* Decode the filesystem ID. */
1138 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1140 free(pathbuf, M_TEMP);
1144 mtx_lock(&mountlist_mtx);
1145 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1146 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1147 mp->mnt_stat.f_fsid.val[1] == id1)
1150 mtx_unlock(&mountlist_mtx);
1152 AUDIT_ARG_UPATH1(td, pathbuf);
1153 mtx_lock(&mountlist_mtx);
1154 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1155 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1158 mtx_unlock(&mountlist_mtx);
1160 free(pathbuf, M_TEMP);
1163 * Previously we returned ENOENT for a nonexistent path and
1164 * EINVAL for a non-mountpoint. We cannot tell these apart
1165 * now, so in the !MNT_BYFSID case return the more likely
1166 * EINVAL for compatibility.
1169 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1173 * Don't allow unmounting the root filesystem.
1175 if (mp->mnt_flag & MNT_ROOTFS) {
1179 error = dounmount(mp, uap->flags, td);
1185 * Do the actual filesystem unmount.
1188 dounmount(mp, flags, td)
1193 struct vnode *coveredvp, *fsrootvp;
1198 mtx_assert(&Giant, MA_OWNED);
1200 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1201 mnt_gen_r = mp->mnt_gen;
1204 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1207 * Check for mp being unmounted while waiting for the
1208 * covered vnode lock.
1210 if (coveredvp->v_mountedhere != mp ||
1211 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1212 VOP_UNLOCK(coveredvp, 0);
1217 * Only privileged root, or (if MNT_USER is set) the user that did the
1218 * original mount is permitted to unmount this filesystem.
1220 error = vfs_suser(mp, td);
1223 VOP_UNLOCK(coveredvp, 0);
1228 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1231 VOP_UNLOCK(coveredvp, 0);
1234 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1235 /* Allow filesystems to detect that a forced unmount is in progress. */
1236 if (flags & MNT_FORCE)
1237 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1239 if (mp->mnt_lockref) {
1240 if ((flags & MNT_FORCE) == 0) {
1241 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_NOINSMNTQ |
1243 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1244 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1249 VOP_UNLOCK(coveredvp, 0);
1252 mp->mnt_kern_flag |= MNTK_DRAINING;
1253 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1257 KASSERT(mp->mnt_lockref == 0,
1258 ("%s: invalid lock refcount in the drain path @ %s:%d",
1259 __func__, __FILE__, __LINE__));
1261 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1262 __func__, __FILE__, __LINE__));
1263 vn_start_write(NULL, &mp, V_WAIT);
1265 if (mp->mnt_flag & MNT_EXPUBLIC)
1266 vfs_setpublicfs(NULL, NULL, NULL);
1268 vfs_msync(mp, MNT_WAIT);
1270 async_flag = mp->mnt_flag & MNT_ASYNC;
1271 mp->mnt_flag &= ~MNT_ASYNC;
1272 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1274 cache_purgevfs(mp); /* remove cache entries for this file sys */
1275 if (mp->mnt_syncer != NULL)
1276 vrele(mp->mnt_syncer);
1278 * For forced unmounts, move process cdir/rdir refs on the fs root
1279 * vnode to the covered vnode. For non-forced unmounts we want
1280 * such references to cause an EBUSY error.
1282 if ((flags & MNT_FORCE) &&
1283 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1284 if (mp->mnt_vnodecovered != NULL)
1285 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1286 if (fsrootvp == rootvnode) {
1292 if (((mp->mnt_flag & MNT_RDONLY) ||
1293 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
1294 error = VFS_UNMOUNT(mp, flags);
1295 vn_finished_write(mp);
1297 * If we failed to flush the dirty blocks for this mount point,
1298 * undo all the cdir/rdir and rootvnode changes we made above.
1299 * Unless we failed to do so because the device is reporting that
1300 * it doesn't exist anymore.
1302 if (error && error != ENXIO) {
1303 if ((flags & MNT_FORCE) &&
1304 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1305 if (mp->mnt_vnodecovered != NULL)
1306 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1307 if (rootvnode == NULL) {
1308 rootvnode = fsrootvp;
1314 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1315 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1317 (void) vfs_allocate_syncvnode(mp);
1320 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1321 mp->mnt_flag |= async_flag;
1322 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1323 mp->mnt_kern_flag |= MNTK_ASYNC;
1324 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1325 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1330 VOP_UNLOCK(coveredvp, 0);
1333 mtx_lock(&mountlist_mtx);
1334 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1335 mtx_unlock(&mountlist_mtx);
1336 if (coveredvp != NULL) {
1337 coveredvp->v_mountedhere = NULL;
1340 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1341 vfs_mount_destroy(mp);
1346 * ---------------------------------------------------------------------
1347 * Mounting of root filesystem
1351 struct root_hold_token {
1353 LIST_ENTRY(root_hold_token) list;
1356 static LIST_HEAD(, root_hold_token) root_holds =
1357 LIST_HEAD_INITIALIZER(root_holds);
1359 static int root_mount_complete;
1364 struct root_hold_token *
1365 root_mount_hold(const char *identifier)
1367 struct root_hold_token *h;
1372 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1373 h->who = identifier;
1374 mtx_lock(&mountlist_mtx);
1375 LIST_INSERT_HEAD(&root_holds, h, list);
1376 mtx_unlock(&mountlist_mtx);
1381 * Release root mount.
1384 root_mount_rel(struct root_hold_token *h)
1389 mtx_lock(&mountlist_mtx);
1390 LIST_REMOVE(h, list);
1391 wakeup(&root_holds);
1392 mtx_unlock(&mountlist_mtx);
1397 * Wait for all subsystems to release root mount.
1400 root_mount_prepare(void)
1402 struct root_hold_token *h;
1403 struct timeval lastfail;
1410 mtx_lock(&mountlist_mtx);
1411 if (LIST_EMPTY(&root_holds)) {
1412 mtx_unlock(&mountlist_mtx);
1415 if (ppsratecheck(&lastfail, &curfail, 1)) {
1416 printf("Root mount waiting for:");
1417 LIST_FOREACH(h, &root_holds, list)
1418 printf(" %s", h->who);
1421 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1427 * Root was mounted, share the good news.
1430 root_mount_done(void)
1433 /* Keep prison0's root in sync with the global rootvnode. */
1434 mtx_lock(&prison0.pr_mtx);
1435 prison0.pr_root = rootvnode;
1436 vref(prison0.pr_root);
1437 mtx_unlock(&prison0.pr_mtx);
1439 * Use a mutex to prevent the wakeup being missed and waiting for
1440 * an extra 1 second sleep.
1442 mtx_lock(&mountlist_mtx);
1443 root_mount_complete = 1;
1444 wakeup(&root_mount_complete);
1445 mtx_unlock(&mountlist_mtx);
1449 * Return true if root is already mounted.
1455 /* No mutex is acquired here because int stores are atomic. */
1456 return (root_mount_complete);
1460 * Wait until root is mounted.
1463 root_mount_wait(void)
1467 * Panic on an obvious deadlock - the function can't be called from
1468 * a thread which is doing the whole SYSINIT stuff.
1470 KASSERT(curthread->td_proc->p_pid != 0,
1471 ("root_mount_wait: cannot be called from the swapper thread"));
1472 mtx_lock(&mountlist_mtx);
1473 while (!root_mount_complete) {
1474 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1477 mtx_unlock(&mountlist_mtx);
1485 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
1486 panic("Cannot find root vnode");
1488 VOP_UNLOCK(rootvnode, 0);
1490 p = curthread->td_proc;
1491 FILEDESC_XLOCK(p->p_fd);
1493 if (p->p_fd->fd_cdir != NULL)
1494 vrele(p->p_fd->fd_cdir);
1495 p->p_fd->fd_cdir = rootvnode;
1498 if (p->p_fd->fd_rdir != NULL)
1499 vrele(p->p_fd->fd_rdir);
1500 p->p_fd->fd_rdir = rootvnode;
1503 FILEDESC_XUNLOCK(p->p_fd);
1505 EVENTHANDLER_INVOKE(mountroot);
1509 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1510 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1516 struct thread *td = curthread;
1517 struct vfsoptlist *opts;
1518 struct vfsconf *vfsp;
1519 struct mount *mp = NULL;
1522 vfsp = vfs_byname("devfs");
1523 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1527 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
1529 error = VFS_MOUNT(mp);
1530 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1534 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1538 mtx_lock(&mountlist_mtx);
1539 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1540 mtx_unlock(&mountlist_mtx);
1544 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1546 printf("kern_symlink /dev -> / returns %d\n", error);
1550 * Surgically move our devfs to be mounted on /dev.
1554 devfs_fixup(struct thread *td)
1556 struct nameidata nd;
1558 struct vnode *vp, *dvp;
1561 /* Remove our devfs mount from the mountlist and purge the cache */
1562 mtx_lock(&mountlist_mtx);
1563 mp = TAILQ_FIRST(&mountlist);
1564 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1565 mtx_unlock(&mountlist_mtx);
1568 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp);
1570 dvp->v_iflag &= ~VI_MOUNT;
1572 dvp->v_mountedhere = NULL;
1574 /* Set up the real rootvnode, and purge the cache */
1575 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1577 cache_purgevfs(rootvnode->v_mount);
1579 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1582 printf("Lookup of /dev for devfs, error: %d\n", error);
1585 NDFREE(&nd, NDF_ONLY_PNBUF);
1587 if (vp->v_type != VDIR) {
1590 error = vinvalbuf(vp, V_SAVE, 0, 0);
1595 mp->mnt_vnodecovered = vp;
1596 vp->v_mountedhere = mp;
1597 mtx_lock(&mountlist_mtx);
1598 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1599 mtx_unlock(&mountlist_mtx);
1604 /* Unlink the no longer needed /dev/dev -> / symlink */
1605 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1609 * Report errors during filesystem mounting.
1612 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1614 struct vfsoptlist *moptlist = mp->mnt_optnew;
1619 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1620 if (error || errmsg == NULL || len <= 0)
1624 vsnprintf(errmsg, (size_t)len, fmt, ap);
1629 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1635 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1636 if (error || errmsg == NULL || len <= 0)
1640 vsnprintf(errmsg, (size_t)len, fmt, ap);
1645 * Find and mount the root filesystem
1650 char *cp, *cpt, *options, *tmpdev;
1651 int error, i, asked = 0;
1655 root_mount_prepare();
1657 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1658 NULL, NULL, mount_init, mount_fini,
1659 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1663 * We are booted with instructions to prompt for the root filesystem.
1665 if (boothowto & RB_ASKNAME) {
1666 if (!vfs_mountroot_ask())
1671 options = getenv("vfs.root.mountfrom.options");
1674 * The root filesystem information is compiled in, and we are
1675 * booted with instructions to use it.
1677 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1678 if (!vfs_mountroot_try(ctrootdevname, options))
1680 ctrootdevname = NULL;
1684 * We've been given the generic "use CDROM as root" flag. This is
1685 * necessary because one media may be used in many different
1686 * devices, so we need to search for them.
1688 if (boothowto & RB_CDROM) {
1689 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1690 if (!vfs_mountroot_try(cdrom_rootdevnames[i], options))
1696 * Try to use the value read by the loader from /etc/fstab, or
1697 * supplied via some other means. This is the preferred
1700 cp = getenv("vfs.root.mountfrom");
1703 while ((tmpdev = strsep(&cpt, " \t")) != NULL) {
1704 error = vfs_mountroot_try(tmpdev, options);
1714 * Try values that may have been computed by code during boot
1716 if (!vfs_mountroot_try(rootdevnames[0], options))
1718 if (!vfs_mountroot_try(rootdevnames[1], options))
1722 * If we (still) have a compiled-in default, try it.
1724 if (ctrootdevname != NULL)
1725 if (!vfs_mountroot_try(ctrootdevname, options))
1728 * Everything so far has failed, prompt on the console if we haven't
1729 * already tried that.
1732 if (!vfs_mountroot_ask())
1735 panic("Root mount failed, startup aborted.");
1742 static struct mntarg *
1743 parse_mountroot_options(struct mntarg *ma, const char *options)
1746 char *name, *name_arg;
1747 char *val, *val_arg;
1750 if (options == NULL || options[0] == '\0')
1753 p = opts = strdup(options, M_MOUNT);
1758 while((name = strsep(&p, ",")) != NULL) {
1759 if (name[0] == '\0')
1762 val = strchr(name, '=');
1767 if( strcmp(name, "rw") == 0 ||
1768 strcmp(name, "noro") == 0) {
1770 * The first time we mount the root file system,
1771 * we need to mount 'ro', so We need to ignore
1772 * 'rw' and 'noro' mount options.
1776 name_arg = strdup(name, M_MOUNT);
1779 val_arg = strdup(val, M_MOUNT);
1781 ma = mount_arg(ma, name_arg, val_arg,
1782 (val_arg != NULL ? -1 : 0));
1784 free(opts, M_MOUNT);
1789 * Mount (mountfrom) as the root filesystem.
1792 vfs_mountroot_try(const char *mountfrom, const char *options)
1796 char *vfsname, *path;
1807 bzero(errmsg, sizeof(errmsg));
1809 if (mountfrom == NULL)
1810 return (error); /* don't complain */
1811 printf("Trying to mount root from %s\n", mountfrom);
1813 /* parse vfs name and path */
1814 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1815 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1816 vfsname[0] = path[0] = 0;
1817 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1818 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1821 if (path[0] == '\0')
1822 strcpy(path, ROOTNAME);
1824 ma = mount_arg(ma, "fstype", vfsname, -1);
1825 ma = mount_arg(ma, "fspath", "/", -1);
1826 ma = mount_arg(ma, "from", path, -1);
1827 ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
1828 ma = mount_arg(ma, "ro", NULL, 0);
1829 ma = parse_mountroot_options(ma, options);
1830 error = kernel_mount(ma, MNT_ROOTFS);
1834 * We mount devfs prior to mounting the / FS, so the first
1835 * entry will typically be devfs.
1837 mp = TAILQ_FIRST(&mountlist);
1838 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1841 * Iterate over all currently mounted file systems and use
1842 * the time stamp found to check and/or initialize the RTC.
1843 * Typically devfs has no time stamp and the only other FS
1844 * is the actual / FS.
1845 * Call inittodr() only once and pass it the largest of the
1846 * timestamps we encounter.
1850 if (mp->mnt_time > timebase)
1851 timebase = mp->mnt_time;
1852 mp = TAILQ_NEXT(mp, mnt_list);
1853 } while (mp != NULL);
1856 devfs_fixup(curthread);
1860 printf("ROOT MOUNT ERROR: %s\n", errmsg);
1861 printf("If you have invalid mount options, reboot, and ");
1862 printf("first try the following from\n");
1863 printf("the loader prompt:\n\n");
1864 printf(" set vfs.root.mountfrom.options=rw\n\n");
1865 printf("and then remove invalid mount options from ");
1866 printf("/etc/fstab.\n\n");
1869 free(path, M_MOUNT);
1870 free(vfsname, M_MOUNT);
1875 * ---------------------------------------------------------------------
1876 * Interactive root filesystem selection code.
1880 vfs_mountroot_ask(void)
1887 printf("Loader variables:\n");
1888 printf("vfs.root.mountfrom=");
1889 mountfrom = getenv("vfs.root.mountfrom");
1890 if (mountfrom != NULL) {
1891 printf("%s", mountfrom);
1894 printf("vfs.root.mountfrom.options=");
1895 options = getenv("vfs.root.mountfrom.options");
1896 if (options != NULL) {
1897 printf("%s", options);
1902 printf("\nManual root filesystem specification:\n");
1903 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1904 printf(" eg. ufs:/dev/da0s1a\n");
1905 printf(" eg. cd9660:/dev/acd0\n");
1906 printf(" This is equivalent to: ");
1907 printf("mount -t cd9660 /dev/acd0 /\n");
1909 printf(" ? List valid disk boot devices\n");
1910 printf(" <empty line> Abort manual input\n");
1911 printf("\nmountroot> ");
1912 gets(name, sizeof(name), 1);
1913 if (name[0] == '\0')
1915 if (name[0] == '?') {
1916 printf("\nList of GEOM managed disk devices:\n ");
1920 if (!vfs_mountroot_try(name, NULL))
1926 * ---------------------------------------------------------------------
1927 * Functions for querying mount options/arguments from filesystems.
1931 * Check that no unknown options are given
1934 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1938 const char **t, *p, *q;
1941 TAILQ_FOREACH(opt, opts, link) {
1944 if (p[0] == 'n' && p[1] == 'o')
1946 for(t = global_opts; *t != NULL; t++) {
1947 if (strcmp(*t, p) == 0)
1950 if (strcmp(*t, q) == 0)
1956 for(t = legal; *t != NULL; t++) {
1957 if (strcmp(*t, p) == 0)
1960 if (strcmp(*t, q) == 0)
1966 snprintf(errmsg, sizeof(errmsg),
1967 "mount option <%s> is unknown", p);
1968 printf("%s\n", errmsg);
1972 TAILQ_FOREACH(opt, opts, link) {
1973 if (strcmp(opt->name, "errmsg") == 0) {
1974 strncpy((char *)opt->value, errmsg, opt->len);
1982 * Get a mount option by its name.
1984 * Return 0 if the option was found, ENOENT otherwise.
1985 * If len is non-NULL it will be filled with the length
1986 * of the option. If buf is non-NULL, it will be filled
1987 * with the address of the option.
1990 vfs_getopt(opts, name, buf, len)
1991 struct vfsoptlist *opts;
1998 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2000 TAILQ_FOREACH(opt, opts, link) {
2001 if (strcmp(name, opt->name) == 0) {
2014 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2021 TAILQ_FOREACH(opt, opts, link) {
2022 if (strcmp(name, opt->name) == 0) {
2031 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2036 TAILQ_FOREACH(opt, opts, link) {
2037 if (strcmp(name, opt->name) != 0)
2040 if (opt->len == 0 ||
2041 ((char *)opt->value)[opt->len - 1] != '\0') {
2045 return (opt->value);
2052 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
2056 TAILQ_FOREACH(opt, opts, link) {
2057 if (strcmp(name, opt->name) == 0) {
2070 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2076 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2078 TAILQ_FOREACH(opt, opts, link) {
2079 if (strcmp(name, opt->name) != 0)
2082 if (opt->len == 0 || opt->value == NULL)
2084 if (((char *)opt->value)[opt->len - 1] != '\0')
2087 ret = vsscanf(opt->value, fmt, ap);
2095 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2099 TAILQ_FOREACH(opt, opts, link) {
2100 if (strcmp(name, opt->name) != 0)
2103 if (opt->value == NULL)
2106 if (opt->len != len)
2108 bcopy(value, opt->value, len);
2116 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2120 TAILQ_FOREACH(opt, opts, link) {
2121 if (strcmp(name, opt->name) != 0)
2124 if (opt->value == NULL)
2130 bcopy(value, opt->value, len);
2138 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2142 TAILQ_FOREACH(opt, opts, link) {
2143 if (strcmp(name, opt->name) != 0)
2146 if (opt->value == NULL)
2147 opt->len = strlen(value) + 1;
2148 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2156 * Find and copy a mount option.
2158 * The size of the buffer has to be specified
2159 * in len, if it is not the same length as the
2160 * mount option, EINVAL is returned.
2161 * Returns ENOENT if the option is not found.
2164 vfs_copyopt(opts, name, dest, len)
2165 struct vfsoptlist *opts;
2172 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2174 TAILQ_FOREACH(opt, opts, link) {
2175 if (strcmp(name, opt->name) == 0) {
2177 if (len != opt->len)
2179 bcopy(opt->value, dest, opt->len);
2187 * This is a helper function for filesystems to traverse their
2188 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
2192 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
2196 mtx_assert(MNT_MTX(mp), MA_OWNED);
2198 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2199 if ((*mvp)->v_yield++ == 500) {
2201 (*mvp)->v_yield = 0;
2205 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2206 while (vp != NULL && vp->v_type == VMARKER)
2207 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2209 /* Check if we are done */
2211 __mnt_vnode_markerfree(mvp, mp);
2214 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2215 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2220 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2224 mtx_assert(MNT_MTX(mp), MA_OWNED);
2226 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2227 while (vp != NULL && vp->v_type == VMARKER)
2228 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2230 /* Check if we are done */
2237 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
2241 (*mvp)->v_type = VMARKER;
2243 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2244 while (vp != NULL && vp->v_type == VMARKER)
2245 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2247 /* Check if we are done */
2250 free(*mvp, M_VNODE_MARKER);
2256 (*mvp)->v_mount = mp;
2257 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2263 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2269 mtx_assert(MNT_MTX(mp), MA_OWNED);
2271 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2272 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2274 free(*mvp, M_VNODE_MARKER);
2282 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2286 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
2287 if (sbp != &mp->mnt_stat)
2288 *sbp = mp->mnt_stat;
2293 vfs_mountedfrom(struct mount *mp, const char *from)
2296 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2297 strlcpy(mp->mnt_stat.f_mntfromname, from,
2298 sizeof mp->mnt_stat.f_mntfromname);
2302 * ---------------------------------------------------------------------
2303 * This is the api for building mount args and mounting filesystems from
2304 * inside the kernel.
2306 * The API works by accumulation of individual args. First error is
2309 * XXX: should be documented in new manpage kernel_mount(9)
2312 /* A memory allocation which must be freed when we are done */
2314 SLIST_ENTRY(mntaarg) next;
2317 /* The header for the mount arguments */
2322 SLIST_HEAD(, mntaarg) list;
2326 * Add a boolean argument.
2328 * flag is the boolean value.
2329 * name must start with "no".
2332 mount_argb(struct mntarg *ma, int flag, const char *name)
2335 KASSERT(name[0] == 'n' && name[1] == 'o',
2336 ("mount_argb(...,%s): name must start with 'no'", name));
2338 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2342 * Add an argument printf style
2345 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2348 struct mntaarg *maa;
2353 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2354 SLIST_INIT(&ma->list);
2359 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2361 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2362 ma->v[ma->len].iov_len = strlen(name) + 1;
2365 sb = sbuf_new_auto();
2367 sbuf_vprintf(sb, fmt, ap);
2370 len = sbuf_len(sb) + 1;
2371 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2372 SLIST_INSERT_HEAD(&ma->list, maa, next);
2373 bcopy(sbuf_data(sb), maa + 1, len);
2376 ma->v[ma->len].iov_base = maa + 1;
2377 ma->v[ma->len].iov_len = len;
2384 * Add an argument which is a userland string.
2387 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2389 struct mntaarg *maa;
2395 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2396 SLIST_INIT(&ma->list);
2400 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2401 SLIST_INSERT_HEAD(&ma->list, maa, next);
2402 tbuf = (void *)(maa + 1);
2403 ma->error = copyinstr(val, tbuf, len, NULL);
2404 return (mount_arg(ma, name, tbuf, -1));
2410 * If length is -1, treat value as a C string.
2413 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2417 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2418 SLIST_INIT(&ma->list);
2423 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2425 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2426 ma->v[ma->len].iov_len = strlen(name) + 1;
2429 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2431 ma->v[ma->len].iov_len = strlen(val) + 1;
2433 ma->v[ma->len].iov_len = len;
2439 * Free a mntarg structure
2442 free_mntarg(struct mntarg *ma)
2444 struct mntaarg *maa;
2446 while (!SLIST_EMPTY(&ma->list)) {
2447 maa = SLIST_FIRST(&ma->list);
2448 SLIST_REMOVE_HEAD(&ma->list, next);
2451 free(ma->v, M_MOUNT);
2456 * Mount a filesystem
2459 kernel_mount(struct mntarg *ma, int flags)
2464 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2465 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2466 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2468 auio.uio_iov = ma->v;
2469 auio.uio_iovcnt = ma->len;
2470 auio.uio_segflg = UIO_SYSSPACE;
2474 error = vfs_donmount(curthread, flags, &auio);
2480 * A printflike function to mount a filesystem.
2483 kernel_vmount(int flags, ...)
2485 struct mntarg *ma = NULL;
2491 va_start(ap, flags);
2493 cp = va_arg(ap, const char *);
2496 vp = va_arg(ap, const void *);
2497 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2501 error = kernel_mount(ma, flags);