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 vfs_isopt_ro(const char *opt)
199 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
200 strcmp(opt, "norw") == 0)
206 vfs_isopt_rw(const char *opt)
209 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
215 * Check if options are equal (with or without the "no" prefix).
218 vfs_equalopts(const char *opt1, const char *opt2)
222 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
223 if (strcmp(opt1, opt2) == 0)
225 /* "noopt" vs. "opt" */
226 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
228 /* "opt" vs. "noopt" */
229 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
231 while ((p = strchr(opt1, '.')) != NULL &&
232 !strncmp(opt1, opt2, ++p - opt1)) {
235 /* "foo.noopt" vs. "foo.opt" */
236 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
238 /* "foo.opt" vs. "foo.noopt" */
239 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
242 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
243 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
244 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
250 * If a mount option is specified several times,
251 * (with or without the "no" prefix) only keep
252 * the last occurence of it.
255 vfs_sanitizeopts(struct vfsoptlist *opts)
257 struct vfsopt *opt, *opt2, *tmp;
259 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
260 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
261 while (opt2 != NULL) {
262 if (vfs_equalopts(opt->name, opt2->name)) {
263 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
264 vfs_freeopt(opts, opt2);
267 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
274 * Build a linked list of mount options from a struct uio.
277 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
279 struct vfsoptlist *opts;
281 size_t memused, namelen, optlen;
282 unsigned int i, iovcnt;
285 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
288 iovcnt = auio->uio_iovcnt;
289 for (i = 0; i < iovcnt; i += 2) {
290 namelen = auio->uio_iov[i].iov_len;
291 optlen = auio->uio_iov[i + 1].iov_len;
292 memused += sizeof(struct vfsopt) + optlen + namelen;
294 * Avoid consuming too much memory, and attempts to overflow
297 if (memused > VFS_MOUNTARG_SIZE_MAX ||
298 optlen > VFS_MOUNTARG_SIZE_MAX ||
299 namelen > VFS_MOUNTARG_SIZE_MAX) {
304 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
305 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
312 * Do this early, so jumps to "bad" will free the current
315 TAILQ_INSERT_TAIL(opts, opt, link);
317 if (auio->uio_segflg == UIO_SYSSPACE) {
318 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
320 error = copyin(auio->uio_iov[i].iov_base, opt->name,
325 /* Ensure names are null-terminated strings. */
326 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
332 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
333 if (auio->uio_segflg == UIO_SYSSPACE) {
334 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
337 error = copyin(auio->uio_iov[i + 1].iov_base,
344 vfs_sanitizeopts(opts);
353 * Merge the old mount options with the new ones passed
354 * in the MNT_UPDATE case.
356 * XXX This function will keep a "nofoo" option in the
357 * new options if there is no matching "foo" option
358 * to be cancelled in the old options. This is a bug
359 * if the option's canonical name is "foo". E.g., "noro"
360 * shouldn't end up in the mount point's active options,
364 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
366 struct vfsopt *opt, *opt2, *new;
368 TAILQ_FOREACH(opt, opts, link) {
370 * Check that this option hasn't been redefined
371 * nor cancelled with a "no" mount option.
373 opt2 = TAILQ_FIRST(toopts);
374 while (opt2 != NULL) {
375 if (strcmp(opt2->name, opt->name) == 0)
377 if (strncmp(opt2->name, "no", 2) == 0 &&
378 strcmp(opt2->name + 2, opt->name) == 0) {
379 vfs_freeopt(toopts, opt2);
382 opt2 = TAILQ_NEXT(opt2, link);
384 /* We want this option, duplicate it. */
385 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
386 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
387 strcpy(new->name, opt->name);
389 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
390 bcopy(opt->value, new->value, opt->len);
395 new->seen = opt->seen;
396 TAILQ_INSERT_TAIL(toopts, new, link);
403 * Mount a filesystem.
408 struct nmount_args /* {
418 AUDIT_ARG_FFLAGS(uap->flags);
419 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
420 uap->iovp, uap->iovcnt, uap->flags);
423 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
424 * userspace to set this flag, but we must filter it out if we want
425 * MNT_UPDATE on the root file system to work.
426 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
428 uap->flags &= ~MNT_ROOTFS;
430 iovcnt = uap->iovcnt;
432 * Check that we have an even number of iovec's
433 * and that we have at least two options.
435 if ((iovcnt & 1) || (iovcnt < 4)) {
436 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
441 error = copyinuio(uap->iovp, iovcnt, &auio);
443 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
447 error = vfs_donmount(td, uap->flags, auio);
454 * ---------------------------------------------------------------------
455 * Various utility functions
459 vfs_ref(struct mount *mp)
462 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
469 vfs_rel(struct mount *mp)
472 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
479 mount_init(void *mem, int size, int flags)
483 mp = (struct mount *)mem;
484 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
485 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
490 mount_fini(void *mem, int size)
494 mp = (struct mount *)mem;
495 lockdestroy(&mp->mnt_explock);
496 mtx_destroy(&mp->mnt_mtx);
500 * Allocate and initialize the mount point struct.
503 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
508 mp = uma_zalloc(mount_zone, M_WAITOK);
509 bzero(&mp->mnt_startzero,
510 __rangeof(struct mount, mnt_startzero, mnt_endzero));
511 TAILQ_INIT(&mp->mnt_nvnodelist);
512 mp->mnt_nvnodelistsize = 0;
514 (void) vfs_busy(mp, MBF_NOWAIT);
515 mp->mnt_op = vfsp->vfc_vfsops;
517 vfsp->vfc_refcount++; /* XXX Unlocked */
518 mp->mnt_stat.f_type = vfsp->vfc_typenum;
520 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
521 mp->mnt_vnodecovered = vp;
522 mp->mnt_cred = crdup(cred);
523 mp->mnt_stat.f_owner = cred->cr_uid;
524 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
525 mp->mnt_iosize_max = DFLTPHYS;
528 mac_mount_create(cred, mp);
530 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
535 * Destroy the mount struct previously allocated by vfs_mount_alloc().
538 vfs_mount_destroy(struct mount *mp)
542 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
543 if (mp->mnt_kern_flag & MNTK_MWAIT) {
544 mp->mnt_kern_flag &= ~MNTK_MWAIT;
548 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
549 KASSERT(mp->mnt_ref == 0,
550 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
551 __FILE__, __LINE__));
552 if (mp->mnt_writeopcount != 0)
553 panic("vfs_mount_destroy: nonzero writeopcount");
554 if (mp->mnt_secondary_writes != 0)
555 panic("vfs_mount_destroy: nonzero secondary_writes");
556 mp->mnt_vfc->vfc_refcount--;
557 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
560 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
562 panic("unmount: dangling vnode");
564 if (mp->mnt_nvnodelistsize != 0)
565 panic("vfs_mount_destroy: nonzero nvnodelistsize");
566 if (mp->mnt_lockref != 0)
567 panic("vfs_mount_destroy: nonzero lock refcount");
570 mac_mount_destroy(mp);
572 if (mp->mnt_opt != NULL)
573 vfs_freeopts(mp->mnt_opt);
574 crfree(mp->mnt_cred);
575 uma_zfree(mount_zone, mp);
579 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
581 struct vfsoptlist *optlist;
582 struct vfsopt *opt, *noro_opt, *tmp_opt;
583 char *fstype, *fspath, *errmsg;
584 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
585 int has_rw, has_noro;
587 errmsg = fspath = NULL;
588 errmsg_len = has_noro = has_rw = fspathlen = 0;
591 error = vfs_buildopts(fsoptions, &optlist);
595 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
596 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
599 * We need these two options before the others,
600 * and they are mandatory for any filesystem.
601 * Ensure they are NUL terminated as well.
604 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
605 if (error || fstype[fstypelen - 1] != '\0') {
608 strncpy(errmsg, "Invalid fstype", errmsg_len);
612 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
613 if (error || fspath[fspathlen - 1] != '\0') {
616 strncpy(errmsg, "Invalid fspath", errmsg_len);
621 * We need to see if we have the "update" option
622 * before we call vfs_domount(), since vfs_domount() has special
623 * logic based on MNT_UPDATE. This is very important
624 * when we want to update the root filesystem.
626 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
627 if (strcmp(opt->name, "update") == 0) {
628 fsflags |= MNT_UPDATE;
629 vfs_freeopt(optlist, opt);
631 else if (strcmp(opt->name, "async") == 0)
632 fsflags |= MNT_ASYNC;
633 else if (strcmp(opt->name, "force") == 0) {
634 fsflags |= MNT_FORCE;
635 vfs_freeopt(optlist, opt);
637 else if (strcmp(opt->name, "reload") == 0) {
638 fsflags |= MNT_RELOAD;
639 vfs_freeopt(optlist, opt);
641 else if (strcmp(opt->name, "multilabel") == 0)
642 fsflags |= MNT_MULTILABEL;
643 else if (strcmp(opt->name, "noasync") == 0)
644 fsflags &= ~MNT_ASYNC;
645 else if (strcmp(opt->name, "noatime") == 0)
646 fsflags |= MNT_NOATIME;
647 else if (strcmp(opt->name, "atime") == 0) {
648 free(opt->name, M_MOUNT);
649 opt->name = strdup("nonoatime", M_MOUNT);
651 else if (strcmp(opt->name, "noclusterr") == 0)
652 fsflags |= MNT_NOCLUSTERR;
653 else if (strcmp(opt->name, "clusterr") == 0) {
654 free(opt->name, M_MOUNT);
655 opt->name = strdup("nonoclusterr", M_MOUNT);
657 else if (strcmp(opt->name, "noclusterw") == 0)
658 fsflags |= MNT_NOCLUSTERW;
659 else if (strcmp(opt->name, "clusterw") == 0) {
660 free(opt->name, M_MOUNT);
661 opt->name = strdup("nonoclusterw", M_MOUNT);
663 else if (strcmp(opt->name, "noexec") == 0)
664 fsflags |= MNT_NOEXEC;
665 else if (strcmp(opt->name, "exec") == 0) {
666 free(opt->name, M_MOUNT);
667 opt->name = strdup("nonoexec", M_MOUNT);
669 else if (strcmp(opt->name, "nosuid") == 0)
670 fsflags |= MNT_NOSUID;
671 else if (strcmp(opt->name, "suid") == 0) {
672 free(opt->name, M_MOUNT);
673 opt->name = strdup("nonosuid", M_MOUNT);
675 else if (strcmp(opt->name, "nosymfollow") == 0)
676 fsflags |= MNT_NOSYMFOLLOW;
677 else if (strcmp(opt->name, "symfollow") == 0) {
678 free(opt->name, M_MOUNT);
679 opt->name = strdup("nonosymfollow", M_MOUNT);
681 else if (strcmp(opt->name, "noro") == 0) {
682 fsflags &= ~MNT_RDONLY;
685 else if (strcmp(opt->name, "rw") == 0) {
686 fsflags &= ~MNT_RDONLY;
689 else if (strcmp(opt->name, "ro") == 0)
690 fsflags |= MNT_RDONLY;
691 else if (strcmp(opt->name, "rdonly") == 0) {
692 free(opt->name, M_MOUNT);
693 opt->name = strdup("ro", M_MOUNT);
694 fsflags |= MNT_RDONLY;
696 else if (strcmp(opt->name, "suiddir") == 0)
697 fsflags |= MNT_SUIDDIR;
698 else if (strcmp(opt->name, "sync") == 0)
699 fsflags |= MNT_SYNCHRONOUS;
700 else if (strcmp(opt->name, "union") == 0)
701 fsflags |= MNT_UNION;
705 * If "rw" was specified as a mount option, and we
706 * are trying to update a mount-point from "ro" to "rw",
707 * we need a mount option "noro", since in vfs_mergeopts(),
708 * "noro" will cancel "ro", but "rw" will not do anything.
710 if (has_rw && !has_noro) {
711 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
712 noro_opt->name = strdup("noro", M_MOUNT);
713 noro_opt->value = NULL;
717 TAILQ_INSERT_TAIL(optlist, noro_opt, link);
721 * Be ultra-paranoid about making sure the type and fspath
722 * variables will fit in our mp buffers, including the
725 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
726 error = ENAMETOOLONG;
731 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
734 /* copyout the errmsg */
735 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
736 && errmsg_len > 0 && errmsg != NULL) {
737 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
739 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
740 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
743 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
744 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
749 vfs_freeopts(optlist);
756 #ifndef _SYS_SYSPROTO_H_
768 struct mount_args /* {
776 struct vfsconf *vfsp = NULL;
777 struct mntarg *ma = NULL;
780 AUDIT_ARG_FFLAGS(uap->flags);
783 * Filter out MNT_ROOTFS. We do not want clients of mount() in
784 * userspace to set this flag, but we must filter it out if we want
785 * MNT_UPDATE on the root file system to work.
786 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
788 uap->flags &= ~MNT_ROOTFS;
790 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
791 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
793 free(fstype, M_TEMP);
797 AUDIT_ARG_TEXT(fstype);
799 vfsp = vfs_byname_kld(fstype, td, &error);
800 free(fstype, M_TEMP);
805 if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
810 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
811 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
812 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
813 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
814 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
816 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags);
823 * vfs_domount(): actually attempt a filesystem mount.
827 struct thread *td, /* Calling thread. */
828 const char *fstype, /* Filesystem type. */
829 char *fspath, /* Mount path. */
830 int fsflags, /* Flags common to all filesystems. */
831 void *fsdata /* Options local to the filesystem. */
836 struct vfsconf *vfsp;
837 struct oexport_args oexport;
838 struct export_args export;
843 mtx_assert(&Giant, MA_OWNED);
845 * Be ultra-paranoid about making sure the type and fspath
846 * variables will fit in our mp buffers, including the
849 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
850 return (ENAMETOOLONG);
852 if (jailed(td->td_ucred) || usermount == 0) {
853 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
858 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
860 if (fsflags & MNT_EXPORTED) {
861 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
865 if (fsflags & MNT_SUIDDIR) {
866 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
871 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
873 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
874 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
875 fsflags |= MNT_NOSUID | MNT_USER;
878 /* Load KLDs before we lock the covered vnode to avoid reversals. */
880 if ((fsflags & MNT_UPDATE) == 0) {
881 /* Don't try to load KLDs if we're mounting the root. */
882 if (fsflags & MNT_ROOTFS)
883 vfsp = vfs_byname(fstype);
885 vfsp = vfs_byname_kld(fstype, td, &error);
888 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
892 * Get vnode to be covered
894 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
896 if ((error = namei(&nd)) != 0)
898 NDFREE(&nd, NDF_ONLY_PNBUF);
900 if (fsflags & MNT_UPDATE) {
901 if ((vp->v_vflag & VV_ROOT) == 0) {
909 * We only allow the filesystem to be reloaded if it
910 * is currently mounted read-only.
912 if ((fsflags & MNT_RELOAD) &&
913 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
916 return (EOPNOTSUPP); /* Needs translation */
920 * Only privileged root, or (if MNT_USER is set) the user that
921 * did the original mount is permitted to update it.
923 error = vfs_suser(mp, td);
928 if (vfs_busy(mp, MBF_NOWAIT)) {
933 if ((vp->v_iflag & VI_MOUNT) != 0 ||
934 vp->v_mountedhere != NULL) {
940 vp->v_iflag |= VI_MOUNT;
943 mp->mnt_flag |= fsflags &
944 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
947 mp->mnt_optnew = fsdata;
948 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
951 * If the user is not root, ensure that they own the directory
952 * onto which we are attempting to mount.
954 error = VOP_GETATTR(vp, &va, td->td_ucred);
959 if (va.va_uid != td->td_ucred->cr_uid) {
960 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
967 error = vinvalbuf(vp, V_SAVE, 0, 0);
972 if (vp->v_type != VDIR) {
977 if ((vp->v_iflag & VI_MOUNT) != 0 ||
978 vp->v_mountedhere != NULL) {
983 vp->v_iflag |= VI_MOUNT;
987 * Allocate and initialize the filesystem.
989 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
992 /* XXXMAC: pass to vfs_mount_alloc? */
993 mp->mnt_optnew = fsdata;
997 * Set the mount level flags.
1000 mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
1001 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
1003 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1004 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1007 * Mount the filesystem.
1008 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1009 * get. No freeing of cn_pnbuf.
1011 error = VFS_MOUNT(mp);
1014 * Process the export option only if we are
1015 * updating mount options.
1017 if (!error && (fsflags & MNT_UPDATE)) {
1018 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1019 sizeof(export)) == 0)
1020 error = vfs_export(mp, &export);
1021 else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
1022 sizeof(oexport)) == 0) {
1023 export.ex_flags = oexport.ex_flags;
1024 export.ex_root = oexport.ex_root;
1025 export.ex_anon = oexport.ex_anon;
1026 export.ex_addr = oexport.ex_addr;
1027 export.ex_addrlen = oexport.ex_addrlen;
1028 export.ex_mask = oexport.ex_mask;
1029 export.ex_masklen = oexport.ex_masklen;
1030 export.ex_indexfile = oexport.ex_indexfile;
1031 export.ex_numsecflavors = 0;
1032 error = vfs_export(mp, &export);
1037 if (mp->mnt_opt != NULL)
1038 vfs_freeopts(mp->mnt_opt);
1039 mp->mnt_opt = mp->mnt_optnew;
1040 (void)VFS_STATFS(mp, &mp->mnt_stat);
1043 * Prevent external consumers of mount options from reading
1046 mp->mnt_optnew = NULL;
1047 if (mp->mnt_flag & MNT_UPDATE) {
1050 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1051 (flag & ~MNT_QUOTA);
1053 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD |
1054 MNT_FORCE | MNT_SNAPSHOT);
1055 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1056 mp->mnt_kern_flag |= MNTK_ASYNC;
1058 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1060 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1061 if (mp->mnt_syncer == NULL)
1062 error = vfs_allocate_syncvnode(mp);
1064 if (mp->mnt_syncer != NULL)
1065 vrele(mp->mnt_syncer);
1066 mp->mnt_syncer = NULL;
1070 vp->v_iflag &= ~VI_MOUNT;
1076 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1077 mp->mnt_kern_flag |= MNTK_ASYNC;
1079 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1081 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1083 * Put the new filesystem on the mount list after root.
1087 struct vnode *newdp;
1090 vp->v_iflag &= ~VI_MOUNT;
1092 vp->v_mountedhere = mp;
1093 mtx_lock(&mountlist_mtx);
1094 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1095 mtx_unlock(&mountlist_mtx);
1096 vfs_event_signal(NULL, VQ_MOUNT, 0);
1097 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
1098 panic("mount: lost mount");
1099 VOP_UNLOCK(newdp, 0);
1101 mountcheckdirs(vp, newdp);
1103 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1104 error = vfs_allocate_syncvnode(mp);
1110 vp->v_iflag &= ~VI_MOUNT;
1113 vfs_mount_destroy(mp);
1120 * Unmount a filesystem.
1122 * Note: unmount takes a path to the vnode mounted on as argument, not
1123 * special file (as before).
1125 #ifndef _SYS_SYSPROTO_H_
1126 struct unmount_args {
1135 register struct unmount_args /* {
1142 int error, id0, id1;
1144 AUDIT_ARG_VALUE(uap->flags);
1145 if (jailed(td->td_ucred) || usermount == 0) {
1146 error = priv_check(td, PRIV_VFS_UNMOUNT);
1151 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1152 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1154 free(pathbuf, M_TEMP);
1158 if (uap->flags & MNT_BYFSID) {
1159 AUDIT_ARG_TEXT(pathbuf);
1160 /* Decode the filesystem ID. */
1161 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1163 free(pathbuf, M_TEMP);
1167 mtx_lock(&mountlist_mtx);
1168 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1169 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1170 mp->mnt_stat.f_fsid.val[1] == id1)
1173 mtx_unlock(&mountlist_mtx);
1175 AUDIT_ARG_UPATH1(td, pathbuf);
1176 mtx_lock(&mountlist_mtx);
1177 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1178 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1181 mtx_unlock(&mountlist_mtx);
1183 free(pathbuf, M_TEMP);
1186 * Previously we returned ENOENT for a nonexistent path and
1187 * EINVAL for a non-mountpoint. We cannot tell these apart
1188 * now, so in the !MNT_BYFSID case return the more likely
1189 * EINVAL for compatibility.
1192 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1196 * Don't allow unmounting the root filesystem.
1198 if (mp->mnt_flag & MNT_ROOTFS) {
1202 error = dounmount(mp, uap->flags, td);
1208 * Do the actual filesystem unmount.
1211 dounmount(mp, flags, td)
1216 struct vnode *coveredvp, *fsrootvp;
1221 mtx_assert(&Giant, MA_OWNED);
1223 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1224 mnt_gen_r = mp->mnt_gen;
1227 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1230 * Check for mp being unmounted while waiting for the
1231 * covered vnode lock.
1233 if (coveredvp->v_mountedhere != mp ||
1234 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1235 VOP_UNLOCK(coveredvp, 0);
1240 * Only privileged root, or (if MNT_USER is set) the user that did the
1241 * original mount is permitted to unmount this filesystem.
1243 error = vfs_suser(mp, td);
1246 VOP_UNLOCK(coveredvp, 0);
1250 vn_start_write(NULL, &mp, V_WAIT);
1252 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1255 VOP_UNLOCK(coveredvp, 0);
1256 vn_finished_write(mp);
1259 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1260 /* Allow filesystems to detect that a forced unmount is in progress. */
1261 if (flags & MNT_FORCE)
1262 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1264 if (mp->mnt_lockref) {
1265 mp->mnt_kern_flag |= MNTK_DRAINING;
1266 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1270 KASSERT(mp->mnt_lockref == 0,
1271 ("%s: invalid lock refcount in the drain path @ %s:%d",
1272 __func__, __FILE__, __LINE__));
1274 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1275 __func__, __FILE__, __LINE__));
1277 if (mp->mnt_flag & MNT_EXPUBLIC)
1278 vfs_setpublicfs(NULL, NULL, NULL);
1280 vfs_msync(mp, MNT_WAIT);
1282 async_flag = mp->mnt_flag & MNT_ASYNC;
1283 mp->mnt_flag &= ~MNT_ASYNC;
1284 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1286 cache_purgevfs(mp); /* remove cache entries for this file sys */
1287 if (mp->mnt_syncer != NULL)
1288 vrele(mp->mnt_syncer);
1290 * For forced unmounts, move process cdir/rdir refs on the fs root
1291 * vnode to the covered vnode. For non-forced unmounts we want
1292 * such references to cause an EBUSY error.
1294 if ((flags & MNT_FORCE) &&
1295 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1296 if (mp->mnt_vnodecovered != NULL)
1297 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1298 if (fsrootvp == rootvnode) {
1304 if (((mp->mnt_flag & MNT_RDONLY) ||
1305 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
1306 error = VFS_UNMOUNT(mp, flags);
1307 vn_finished_write(mp);
1309 * If we failed to flush the dirty blocks for this mount point,
1310 * undo all the cdir/rdir and rootvnode changes we made above.
1311 * Unless we failed to do so because the device is reporting that
1312 * it doesn't exist anymore.
1314 if (error && error != ENXIO) {
1315 if ((flags & MNT_FORCE) &&
1316 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1317 if (mp->mnt_vnodecovered != NULL)
1318 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1319 if (rootvnode == NULL) {
1320 rootvnode = fsrootvp;
1326 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1327 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1329 (void) vfs_allocate_syncvnode(mp);
1332 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1333 mp->mnt_flag |= async_flag;
1334 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1335 mp->mnt_kern_flag |= MNTK_ASYNC;
1336 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1337 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1342 VOP_UNLOCK(coveredvp, 0);
1345 mtx_lock(&mountlist_mtx);
1346 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1347 mtx_unlock(&mountlist_mtx);
1348 if (coveredvp != NULL) {
1349 coveredvp->v_mountedhere = NULL;
1352 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1353 vfs_mount_destroy(mp);
1358 * ---------------------------------------------------------------------
1359 * Mounting of root filesystem
1363 struct root_hold_token {
1365 LIST_ENTRY(root_hold_token) list;
1368 static LIST_HEAD(, root_hold_token) root_holds =
1369 LIST_HEAD_INITIALIZER(root_holds);
1371 static int root_mount_complete;
1376 struct root_hold_token *
1377 root_mount_hold(const char *identifier)
1379 struct root_hold_token *h;
1384 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1385 h->who = identifier;
1386 mtx_lock(&mountlist_mtx);
1387 LIST_INSERT_HEAD(&root_holds, h, list);
1388 mtx_unlock(&mountlist_mtx);
1393 * Release root mount.
1396 root_mount_rel(struct root_hold_token *h)
1401 mtx_lock(&mountlist_mtx);
1402 LIST_REMOVE(h, list);
1403 wakeup(&root_holds);
1404 mtx_unlock(&mountlist_mtx);
1409 * Wait for all subsystems to release root mount.
1412 root_mount_prepare(void)
1414 struct root_hold_token *h;
1415 struct timeval lastfail;
1422 mtx_lock(&mountlist_mtx);
1423 if (LIST_EMPTY(&root_holds)) {
1424 mtx_unlock(&mountlist_mtx);
1427 if (ppsratecheck(&lastfail, &curfail, 1)) {
1428 printf("Root mount waiting for:");
1429 LIST_FOREACH(h, &root_holds, list)
1430 printf(" %s", h->who);
1433 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1439 * Root was mounted, share the good news.
1442 root_mount_done(void)
1445 /* Keep prison0's root in sync with the global rootvnode. */
1446 mtx_lock(&prison0.pr_mtx);
1447 prison0.pr_root = rootvnode;
1448 vref(prison0.pr_root);
1449 mtx_unlock(&prison0.pr_mtx);
1451 * Use a mutex to prevent the wakeup being missed and waiting for
1452 * an extra 1 second sleep.
1454 mtx_lock(&mountlist_mtx);
1455 root_mount_complete = 1;
1456 wakeup(&root_mount_complete);
1457 mtx_unlock(&mountlist_mtx);
1461 * Return true if root is already mounted.
1467 /* No mutex is acquired here because int stores are atomic. */
1468 return (root_mount_complete);
1472 * Wait until root is mounted.
1475 root_mount_wait(void)
1479 * Panic on an obvious deadlock - the function can't be called from
1480 * a thread which is doing the whole SYSINIT stuff.
1482 KASSERT(curthread->td_proc->p_pid != 0,
1483 ("root_mount_wait: cannot be called from the swapper thread"));
1484 mtx_lock(&mountlist_mtx);
1485 while (!root_mount_complete) {
1486 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1489 mtx_unlock(&mountlist_mtx);
1497 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
1498 panic("Cannot find root vnode");
1500 VOP_UNLOCK(rootvnode, 0);
1502 p = curthread->td_proc;
1503 FILEDESC_XLOCK(p->p_fd);
1505 if (p->p_fd->fd_cdir != NULL)
1506 vrele(p->p_fd->fd_cdir);
1507 p->p_fd->fd_cdir = rootvnode;
1510 if (p->p_fd->fd_rdir != NULL)
1511 vrele(p->p_fd->fd_rdir);
1512 p->p_fd->fd_rdir = rootvnode;
1515 FILEDESC_XUNLOCK(p->p_fd);
1517 EVENTHANDLER_INVOKE(mountroot);
1521 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1522 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1528 struct thread *td = curthread;
1529 struct vfsoptlist *opts;
1530 struct vfsconf *vfsp;
1531 struct mount *mp = NULL;
1534 vfsp = vfs_byname("devfs");
1535 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1539 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
1541 error = VFS_MOUNT(mp);
1542 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1546 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1550 mtx_lock(&mountlist_mtx);
1551 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1552 mtx_unlock(&mountlist_mtx);
1556 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1558 printf("kern_symlink /dev -> / returns %d\n", error);
1562 * Surgically move our devfs to be mounted on /dev.
1566 devfs_fixup(struct thread *td)
1568 struct nameidata nd;
1570 struct vnode *vp, *dvp;
1573 /* Remove our devfs mount from the mountlist and purge the cache */
1574 mtx_lock(&mountlist_mtx);
1575 mp = TAILQ_FIRST(&mountlist);
1576 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1577 mtx_unlock(&mountlist_mtx);
1580 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp);
1582 dvp->v_iflag &= ~VI_MOUNT;
1584 dvp->v_mountedhere = NULL;
1586 /* Set up the real rootvnode, and purge the cache */
1587 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1589 cache_purgevfs(rootvnode->v_mount);
1591 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1594 printf("Lookup of /dev for devfs, error: %d\n", error);
1597 NDFREE(&nd, NDF_ONLY_PNBUF);
1599 if (vp->v_type != VDIR) {
1602 error = vinvalbuf(vp, V_SAVE, 0, 0);
1607 mp->mnt_vnodecovered = vp;
1608 vp->v_mountedhere = mp;
1609 mtx_lock(&mountlist_mtx);
1610 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1611 mtx_unlock(&mountlist_mtx);
1616 /* Unlink the no longer needed /dev/dev -> / symlink */
1617 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1621 * Report errors during filesystem mounting.
1624 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1626 struct vfsoptlist *moptlist = mp->mnt_optnew;
1631 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1632 if (error || errmsg == NULL || len <= 0)
1636 vsnprintf(errmsg, (size_t)len, fmt, ap);
1641 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1647 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1648 if (error || errmsg == NULL || len <= 0)
1652 vsnprintf(errmsg, (size_t)len, fmt, ap);
1657 * Find and mount the root filesystem
1662 char *cp, *cpt, *options, *tmpdev;
1663 int error, i, asked = 0;
1667 root_mount_prepare();
1669 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1670 NULL, NULL, mount_init, mount_fini,
1671 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1675 * We are booted with instructions to prompt for the root filesystem.
1677 if (boothowto & RB_ASKNAME) {
1678 if (!vfs_mountroot_ask())
1683 options = getenv("vfs.root.mountfrom.options");
1686 * The root filesystem information is compiled in, and we are
1687 * booted with instructions to use it.
1689 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1690 if (!vfs_mountroot_try(ctrootdevname, options))
1692 ctrootdevname = NULL;
1696 * We've been given the generic "use CDROM as root" flag. This is
1697 * necessary because one media may be used in many different
1698 * devices, so we need to search for them.
1700 if (boothowto & RB_CDROM) {
1701 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1702 if (!vfs_mountroot_try(cdrom_rootdevnames[i], options))
1708 * Try to use the value read by the loader from /etc/fstab, or
1709 * supplied via some other means. This is the preferred
1712 cp = getenv("vfs.root.mountfrom");
1715 while ((tmpdev = strsep(&cpt, " \t")) != NULL) {
1716 error = vfs_mountroot_try(tmpdev, options);
1726 * Try values that may have been computed by code during boot
1728 if (!vfs_mountroot_try(rootdevnames[0], options))
1730 if (!vfs_mountroot_try(rootdevnames[1], options))
1734 * If we (still) have a compiled-in default, try it.
1736 if (ctrootdevname != NULL)
1737 if (!vfs_mountroot_try(ctrootdevname, options))
1740 * Everything so far has failed, prompt on the console if we haven't
1741 * already tried that.
1744 if (!vfs_mountroot_ask())
1747 panic("Root mount failed, startup aborted.");
1754 static struct mntarg *
1755 parse_mountroot_options(struct mntarg *ma, const char *options)
1758 char *name, *name_arg;
1759 char *val, *val_arg;
1762 if (options == NULL || options[0] == '\0')
1765 p = opts = strdup(options, M_MOUNT);
1770 while((name = strsep(&p, ",")) != NULL) {
1771 if (name[0] == '\0')
1774 val = strchr(name, '=');
1779 if( strcmp(name, "rw") == 0 ||
1780 strcmp(name, "noro") == 0) {
1782 * The first time we mount the root file system,
1783 * we need to mount 'ro', so We need to ignore
1784 * 'rw' and 'noro' mount options.
1788 name_arg = strdup(name, M_MOUNT);
1791 val_arg = strdup(val, M_MOUNT);
1793 ma = mount_arg(ma, name_arg, val_arg,
1794 (val_arg != NULL ? -1 : 0));
1796 free(opts, M_MOUNT);
1801 * Mount (mountfrom) as the root filesystem.
1804 vfs_mountroot_try(const char *mountfrom, const char *options)
1808 char *vfsname, *path;
1819 bzero(errmsg, sizeof(errmsg));
1821 if (mountfrom == NULL)
1822 return (error); /* don't complain */
1823 printf("Trying to mount root from %s\n", mountfrom);
1825 /* parse vfs name and path */
1826 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1827 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1828 vfsname[0] = path[0] = 0;
1829 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1830 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1833 if (path[0] == '\0')
1834 strcpy(path, ROOTNAME);
1836 ma = mount_arg(ma, "fstype", vfsname, -1);
1837 ma = mount_arg(ma, "fspath", "/", -1);
1838 ma = mount_arg(ma, "from", path, -1);
1839 ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
1840 ma = mount_arg(ma, "ro", NULL, 0);
1841 ma = parse_mountroot_options(ma, options);
1842 error = kernel_mount(ma, MNT_ROOTFS);
1846 * We mount devfs prior to mounting the / FS, so the first
1847 * entry will typically be devfs.
1849 mp = TAILQ_FIRST(&mountlist);
1850 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1853 * Iterate over all currently mounted file systems and use
1854 * the time stamp found to check and/or initialize the RTC.
1855 * Typically devfs has no time stamp and the only other FS
1856 * is the actual / FS.
1857 * Call inittodr() only once and pass it the largest of the
1858 * timestamps we encounter.
1862 if (mp->mnt_time > timebase)
1863 timebase = mp->mnt_time;
1864 mp = TAILQ_NEXT(mp, mnt_list);
1865 } while (mp != NULL);
1868 devfs_fixup(curthread);
1872 printf("ROOT MOUNT ERROR: %s\n", errmsg);
1873 printf("If you have invalid mount options, reboot, and ");
1874 printf("first try the following from\n");
1875 printf("the loader prompt:\n\n");
1876 printf(" set vfs.root.mountfrom.options=rw\n\n");
1877 printf("and then remove invalid mount options from ");
1878 printf("/etc/fstab.\n\n");
1881 free(path, M_MOUNT);
1882 free(vfsname, M_MOUNT);
1887 * ---------------------------------------------------------------------
1888 * Interactive root filesystem selection code.
1892 vfs_mountroot_ask(void)
1899 printf("Loader variables:\n");
1900 printf("vfs.root.mountfrom=");
1901 mountfrom = getenv("vfs.root.mountfrom");
1902 if (mountfrom != NULL) {
1903 printf("%s", mountfrom);
1906 printf("vfs.root.mountfrom.options=");
1907 options = getenv("vfs.root.mountfrom.options");
1908 if (options != NULL) {
1909 printf("%s", options);
1914 printf("\nManual root filesystem specification:\n");
1915 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1916 printf(" eg. ufs:/dev/da0s1a\n");
1917 printf(" eg. cd9660:/dev/acd0\n");
1918 printf(" This is equivalent to: ");
1919 printf("mount -t cd9660 /dev/acd0 /\n");
1921 printf(" ? List valid disk boot devices\n");
1922 printf(" <empty line> Abort manual input\n");
1923 printf("\nmountroot> ");
1924 gets(name, sizeof(name), 1);
1925 if (name[0] == '\0')
1927 if (name[0] == '?') {
1928 printf("\nList of GEOM managed disk devices:\n ");
1932 if (!vfs_mountroot_try(name, NULL))
1938 * ---------------------------------------------------------------------
1939 * Functions for querying mount options/arguments from filesystems.
1943 * Check that no unknown options are given
1946 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1950 const char **t, *p, *q;
1953 TAILQ_FOREACH(opt, opts, link) {
1956 if (p[0] == 'n' && p[1] == 'o')
1958 for(t = global_opts; *t != NULL; t++) {
1959 if (strcmp(*t, p) == 0)
1962 if (strcmp(*t, q) == 0)
1968 for(t = legal; *t != NULL; t++) {
1969 if (strcmp(*t, p) == 0)
1972 if (strcmp(*t, q) == 0)
1978 snprintf(errmsg, sizeof(errmsg),
1979 "mount option <%s> is unknown", p);
1980 printf("%s\n", errmsg);
1984 TAILQ_FOREACH(opt, opts, link) {
1985 if (strcmp(opt->name, "errmsg") == 0) {
1986 strncpy((char *)opt->value, errmsg, opt->len);
1994 * Get a mount option by its name.
1996 * Return 0 if the option was found, ENOENT otherwise.
1997 * If len is non-NULL it will be filled with the length
1998 * of the option. If buf is non-NULL, it will be filled
1999 * with the address of the option.
2002 vfs_getopt(opts, name, buf, len)
2003 struct vfsoptlist *opts;
2010 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2012 TAILQ_FOREACH(opt, opts, link) {
2013 if (strcmp(name, opt->name) == 0) {
2026 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2033 TAILQ_FOREACH(opt, opts, link) {
2034 if (strcmp(name, opt->name) == 0) {
2043 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2048 TAILQ_FOREACH(opt, opts, link) {
2049 if (strcmp(name, opt->name) != 0)
2052 if (opt->len == 0 ||
2053 ((char *)opt->value)[opt->len - 1] != '\0') {
2057 return (opt->value);
2064 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
2068 TAILQ_FOREACH(opt, opts, link) {
2069 if (strcmp(name, opt->name) == 0) {
2082 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2088 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2090 TAILQ_FOREACH(opt, opts, link) {
2091 if (strcmp(name, opt->name) != 0)
2094 if (opt->len == 0 || opt->value == NULL)
2096 if (((char *)opt->value)[opt->len - 1] != '\0')
2099 ret = vsscanf(opt->value, fmt, ap);
2107 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2111 TAILQ_FOREACH(opt, opts, link) {
2112 if (strcmp(name, opt->name) != 0)
2115 if (opt->value == NULL)
2118 if (opt->len != len)
2120 bcopy(value, opt->value, len);
2128 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2132 TAILQ_FOREACH(opt, opts, link) {
2133 if (strcmp(name, opt->name) != 0)
2136 if (opt->value == NULL)
2142 bcopy(value, opt->value, len);
2150 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2154 TAILQ_FOREACH(opt, opts, link) {
2155 if (strcmp(name, opt->name) != 0)
2158 if (opt->value == NULL)
2159 opt->len = strlen(value) + 1;
2160 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2168 * Find and copy a mount option.
2170 * The size of the buffer has to be specified
2171 * in len, if it is not the same length as the
2172 * mount option, EINVAL is returned.
2173 * Returns ENOENT if the option is not found.
2176 vfs_copyopt(opts, name, dest, len)
2177 struct vfsoptlist *opts;
2184 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2186 TAILQ_FOREACH(opt, opts, link) {
2187 if (strcmp(name, opt->name) == 0) {
2189 if (len != opt->len)
2191 bcopy(opt->value, dest, opt->len);
2199 * This is a helper function for filesystems to traverse their
2200 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
2204 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
2208 mtx_assert(MNT_MTX(mp), MA_OWNED);
2210 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2211 if ((*mvp)->v_yield++ == 500) {
2213 (*mvp)->v_yield = 0;
2217 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2218 while (vp != NULL && vp->v_type == VMARKER)
2219 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2221 /* Check if we are done */
2223 __mnt_vnode_markerfree(mvp, mp);
2226 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2227 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2232 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2236 mtx_assert(MNT_MTX(mp), MA_OWNED);
2238 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2239 while (vp != NULL && vp->v_type == VMARKER)
2240 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2242 /* Check if we are done */
2249 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
2253 (*mvp)->v_type = VMARKER;
2255 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2256 while (vp != NULL && vp->v_type == VMARKER)
2257 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2259 /* Check if we are done */
2262 free(*mvp, M_VNODE_MARKER);
2268 (*mvp)->v_mount = mp;
2269 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2275 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2281 mtx_assert(MNT_MTX(mp), MA_OWNED);
2283 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2284 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2286 free(*mvp, M_VNODE_MARKER);
2294 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2298 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
2299 if (sbp != &mp->mnt_stat)
2300 *sbp = mp->mnt_stat;
2305 vfs_mountedfrom(struct mount *mp, const char *from)
2308 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2309 strlcpy(mp->mnt_stat.f_mntfromname, from,
2310 sizeof mp->mnt_stat.f_mntfromname);
2314 * ---------------------------------------------------------------------
2315 * This is the api for building mount args and mounting filesystems from
2316 * inside the kernel.
2318 * The API works by accumulation of individual args. First error is
2321 * XXX: should be documented in new manpage kernel_mount(9)
2324 /* A memory allocation which must be freed when we are done */
2326 SLIST_ENTRY(mntaarg) next;
2329 /* The header for the mount arguments */
2334 SLIST_HEAD(, mntaarg) list;
2338 * Add a boolean argument.
2340 * flag is the boolean value.
2341 * name must start with "no".
2344 mount_argb(struct mntarg *ma, int flag, const char *name)
2347 KASSERT(name[0] == 'n' && name[1] == 'o',
2348 ("mount_argb(...,%s): name must start with 'no'", name));
2350 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2354 * Add an argument printf style
2357 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2360 struct mntaarg *maa;
2365 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2366 SLIST_INIT(&ma->list);
2371 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2373 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2374 ma->v[ma->len].iov_len = strlen(name) + 1;
2377 sb = sbuf_new_auto();
2379 sbuf_vprintf(sb, fmt, ap);
2382 len = sbuf_len(sb) + 1;
2383 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2384 SLIST_INSERT_HEAD(&ma->list, maa, next);
2385 bcopy(sbuf_data(sb), maa + 1, len);
2388 ma->v[ma->len].iov_base = maa + 1;
2389 ma->v[ma->len].iov_len = len;
2396 * Add an argument which is a userland string.
2399 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2401 struct mntaarg *maa;
2407 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2408 SLIST_INIT(&ma->list);
2412 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2413 SLIST_INSERT_HEAD(&ma->list, maa, next);
2414 tbuf = (void *)(maa + 1);
2415 ma->error = copyinstr(val, tbuf, len, NULL);
2416 return (mount_arg(ma, name, tbuf, -1));
2422 * If length is -1, treat value as a C string.
2425 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2429 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2430 SLIST_INIT(&ma->list);
2435 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2437 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2438 ma->v[ma->len].iov_len = strlen(name) + 1;
2441 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2443 ma->v[ma->len].iov_len = strlen(val) + 1;
2445 ma->v[ma->len].iov_len = len;
2451 * Free a mntarg structure
2454 free_mntarg(struct mntarg *ma)
2456 struct mntaarg *maa;
2458 while (!SLIST_EMPTY(&ma->list)) {
2459 maa = SLIST_FIRST(&ma->list);
2460 SLIST_REMOVE_HEAD(&ma->list, next);
2463 free(ma->v, M_MOUNT);
2468 * Mount a filesystem
2471 kernel_mount(struct mntarg *ma, int flags)
2476 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2477 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2478 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2480 auio.uio_iov = ma->v;
2481 auio.uio_iovcnt = ma->len;
2482 auio.uio_segflg = UIO_SYSSPACE;
2486 error = vfs_donmount(curthread, flags, &auio);
2492 * A printflike function to mount a filesystem.
2495 kernel_vmount(int flags, ...)
2497 struct mntarg *ma = NULL;
2503 va_start(ap, flags);
2505 cp = va_arg(ap, const char *);
2508 vp = va_arg(ap, const void *);
2509 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2513 error = kernel_mount(ma, flags);
2518 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2521 bcopy(oexp, exp, sizeof(*oexp));
2522 exp->ex_numsecflavors = 0;