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>
43 #include <sys/kernel.h>
44 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
51 #include <sys/filedesc.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
61 #include <geom/geom.h>
63 #include <machine/stdarg.h>
65 #include "opt_rootdevname.h"
73 #define ROOTNAME "root_device"
74 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
76 static int vfs_domount(struct thread *td, const char *fstype,
77 char *fspath, int fsflags, void *fsdata);
78 static int vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
79 const char *fspath, struct thread *td, struct mount **mpp);
80 static int vfs_mountroot_ask(void);
81 static int vfs_mountroot_try(const char *mountfrom);
82 static int vfs_donmount(struct thread *td, int fsflags,
83 struct uio *fsoptions);
84 static void free_mntarg(struct mntarg *ma);
85 static void vfs_mount_destroy(struct mount *, struct thread *);
87 static int usermount = 0;
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
89 "Unprivileged users may mount and unmount file systems");
91 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
93 /* List of mounted filesystems. */
94 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
96 /* For any iteration/modification of mountlist */
97 struct mtx mountlist_mtx;
99 TAILQ_HEAD(vfsoptlist, vfsopt);
101 TAILQ_ENTRY(vfsopt) link;
108 * The vnode of the system's root (/ in the filesystem, without chroot
111 struct vnode *rootvnode;
114 * The root filesystem is detailed in the kernel environment variable
115 * vfs.root.mountfrom, which is expected to be in the general format
118 * vfsname := the name of a VFS known to the kernel and capable
119 * of being mounted as root
120 * path := disk device name or other data used by the filesystem
121 * to locate its physical store
125 * Global opts, taken by all filesystems
127 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);
167 else if (opt->len != 0)
168 panic("%s: mount option with NULL value but length != 0",
174 /* Release all resources related to the mount options. */
176 vfs_freeopts(struct vfsoptlist *opts)
180 while (!TAILQ_EMPTY(opts)) {
181 opt = TAILQ_FIRST(opts);
182 vfs_freeopt(opts, opt);
188 * Check if options are equal (with or without the "no" prefix).
191 vfs_equalopts(const char *opt1, const char *opt2)
194 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
195 if (strcmp(opt1, opt2) == 0)
197 /* "noopt" vs. "opt" */
198 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
200 /* "opt" vs. "noopt" */
201 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
207 * If a mount option is specified several times,
208 * (with or without the "no" prefix) only keep
209 * the last occurence of it.
212 vfs_sanitizeopts(struct vfsoptlist *opts)
214 struct vfsopt *opt, *opt2, *tmp;
216 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
217 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
218 while (opt2 != NULL) {
219 if (vfs_equalopts(opt->name, opt2->name)) {
220 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
221 vfs_freeopt(opts, opt2);
224 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
231 * Build a linked list of mount options from a struct uio.
234 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
236 struct vfsoptlist *opts;
239 unsigned int i, iovcnt;
240 int error, namelen, optlen;
242 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
245 iovcnt = auio->uio_iovcnt;
246 for (i = 0; i < iovcnt; i += 2) {
247 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
248 namelen = auio->uio_iov[i].iov_len;
249 optlen = auio->uio_iov[i + 1].iov_len;
250 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
255 * Do this early, so jumps to "bad" will free the current
258 TAILQ_INSERT_TAIL(opts, opt, link);
259 memused += sizeof(struct vfsopt) + optlen + namelen;
262 * Avoid consuming too much memory, and attempts to overflow
265 if (memused > VFS_MOUNTARG_SIZE_MAX ||
266 optlen > VFS_MOUNTARG_SIZE_MAX ||
267 namelen > VFS_MOUNTARG_SIZE_MAX) {
272 if (auio->uio_segflg == UIO_SYSSPACE) {
273 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
275 error = copyin(auio->uio_iov[i].iov_base, opt->name,
280 /* Ensure names are null-terminated strings. */
281 if (opt->name[namelen - 1] != '\0') {
287 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
288 if (auio->uio_segflg == UIO_SYSSPACE) {
289 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
292 error = copyin(auio->uio_iov[i + 1].iov_base,
299 vfs_sanitizeopts(opts);
308 * Merge the old mount options with the new ones passed
309 * in the MNT_UPDATE case.
312 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
314 struct vfsopt *opt, *opt2, *new;
316 TAILQ_FOREACH(opt, opts, link) {
318 * Check that this option hasn't been redefined
319 * nor cancelled with a "no" mount option.
321 opt2 = TAILQ_FIRST(toopts);
322 while (opt2 != NULL) {
323 if (strcmp(opt2->name, opt->name) == 0)
325 if (strncmp(opt2->name, "no", 2) == 0 &&
326 strcmp(opt2->name + 2, opt->name) == 0) {
327 vfs_freeopt(toopts, opt2);
330 opt2 = TAILQ_NEXT(opt2, link);
332 /* We want this option, duplicate it. */
333 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
334 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
335 strcpy(new->name, opt->name);
337 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
338 bcopy(opt->value, new->value, opt->len);
343 TAILQ_INSERT_TAIL(toopts, new, link);
350 * ---------------------------------------------------------------------
356 struct nmount_args /* {
368 /* Kick out MNT_ROOTFS early as it is legal internally */
369 if (uap->flags & MNT_ROOTFS)
372 iovcnt = uap->iovcnt;
374 * Check that we have an even number of iovec's
375 * and that we have at least two options.
377 if ((iovcnt & 1) || (iovcnt < 4))
380 error = copyinuio(uap->iovp, iovcnt, &auio);
384 for (i = 0; i < iovcnt; i++) {
385 if (iov->iov_len > MMAXOPTIONLEN) {
391 error = vfs_donmount(td, uap->flags, auio);
397 * ---------------------------------------------------------------------
398 * Various utility functions
402 * Allocate and initialize the mount point struct.
405 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
406 const char *fspath, struct thread *td, struct mount **mpp)
410 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
411 TAILQ_INIT(&mp->mnt_nvnodelist);
412 mp->mnt_nvnodelistsize = 0;
413 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
414 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
415 vfs_busy(mp, LK_NOWAIT, 0, td);
416 mp->mnt_op = vfsp->vfc_vfsops;
418 vfsp->vfc_refcount++;
419 mp->mnt_stat.f_type = vfsp->vfc_typenum;
420 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
421 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
422 mp->mnt_vnodecovered = vp;
423 mp->mnt_cred = crdup(td->td_ucred);
424 mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
425 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
426 mp->mnt_iosize_max = DFLTPHYS;
429 mac_create_mount(td->td_ucred, mp);
431 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
437 * Destroy the mount struct previously allocated by vfs_mount_alloc().
440 vfs_mount_destroy(struct mount *mp, struct thread *td)
443 mp->mnt_vfc->vfc_refcount--;
444 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
445 panic("unmount: dangling vnode");
447 lockdestroy(&mp->mnt_lock);
449 if (mp->mnt_kern_flag & MNTK_MWAIT)
452 mtx_destroy(&mp->mnt_mtx);
454 mac_destroy_mount(mp);
456 if (mp->mnt_opt != NULL)
457 vfs_freeopts(mp->mnt_opt);
458 crfree(mp->mnt_cred);
463 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
465 struct vfsoptlist *optlist;
466 char *fstype, *fspath;
467 int error, fstypelen, fspathlen;
469 error = vfs_buildopts(fsoptions, &optlist);
474 * We need these two options before the others,
475 * and they are mandatory for any filesystem.
476 * Ensure they are NUL terminated as well.
479 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
480 if (error || fstype[fstypelen - 1] != '\0') {
485 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
486 if (error || fspath[fspathlen - 1] != '\0') {
492 * Be ultra-paranoid about making sure the type and fspath
493 * variables will fit in our mp buffers, including the
496 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
497 error = ENAMETOOLONG;
502 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
506 vfs_freeopts(optlist);
511 * ---------------------------------------------------------------------
514 #ifndef _SYS_SYSPROTO_H_
526 struct mount_args /* {
534 struct vfsconf *vfsp = NULL;
535 struct mntarg *ma = NULL;
538 /* Kick out MNT_ROOTFS early as it is legal internally */
539 uap->flags &= ~MNT_ROOTFS;
541 if (uap->data == NULL)
544 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
545 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
547 mtx_lock(&Giant); /* XXX ? */
548 vfsp = vfs_byname_kld(fstype, td, &error);
551 free(fstype, M_TEMP);
556 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
559 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
560 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
561 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
562 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
563 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
565 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
571 * vfs_domount(): actually attempt a filesystem mount.
575 struct thread *td, /* Flags common to all filesystems. */
576 const char *fstype, /* Filesystem type. */
577 char *fspath, /* Mount path. */
578 int fsflags, /* Flags common to all filesystems. */
579 void *fsdata /* Options local to the filesystem. */
584 struct vfsconf *vfsp;
585 int error, flag = 0, kern_flag = 0;
589 mtx_assert(&Giant, MA_OWNED);
592 * Be ultra-paranoid about making sure the type and fspath
593 * variables will fit in our mp buffers, including the
596 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
597 return (ENAMETOOLONG);
599 if (jailed(td->td_ucred))
601 if (usermount == 0) {
602 if ((error = suser(td)) != 0)
607 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
609 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
610 if ((error = suser(td)) != 0)
614 * Silently enforce MNT_NOSUID and MNT_USER for
615 * unprivileged users.
618 fsflags |= MNT_NOSUID | MNT_USER;
620 * Get vnode to be covered
622 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
623 if ((error = namei(&nd)) != 0)
625 NDFREE(&nd, NDF_ONLY_PNBUF);
627 if (fsflags & MNT_UPDATE) {
628 if ((vp->v_vflag & VV_ROOT) == 0) {
634 kern_flag = mp->mnt_kern_flag;
636 * We only allow the filesystem to be reloaded if it
637 * is currently mounted read-only.
639 if ((fsflags & MNT_RELOAD) &&
640 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
642 return (EOPNOTSUPP); /* Needs translation */
645 * Only privileged root, or (if MNT_USER is set) the user that
646 * did the original mount is permitted to update it.
648 error = vfs_suser(mp, td);
653 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
658 if ((vp->v_iflag & VI_MOUNT) != 0 ||
659 vp->v_mountedhere != NULL) {
665 vp->v_iflag |= VI_MOUNT;
667 mp->mnt_flag |= fsflags &
668 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
669 VOP_UNLOCK(vp, 0, td);
670 mp->mnt_optnew = fsdata;
671 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
674 * If the user is not root, ensure that they own the directory
675 * onto which we are attempting to mount.
677 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
682 if (va.va_uid != td->td_ucred->cr_uid) {
683 if ((error = suser(td)) != 0) {
688 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
693 if (vp->v_type != VDIR) {
697 vfsp = vfs_byname_kld(fstype, td, &error);
703 if ((vp->v_iflag & VI_MOUNT) != 0 ||
704 vp->v_mountedhere != NULL) {
709 vp->v_iflag |= VI_MOUNT;
713 * Allocate and initialize the filesystem.
715 error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp);
720 VOP_UNLOCK(vp, 0, td);
722 /* XXXMAC: pass to vfs_mount_alloc? */
723 mp->mnt_optnew = fsdata;
727 * Set the mount level flags.
729 if (fsflags & MNT_RDONLY)
730 mp->mnt_flag |= MNT_RDONLY;
731 mp->mnt_flag &=~ MNT_UPDATEMASK;
732 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
734 * Mount the filesystem.
735 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
736 * get. No freeing of cn_pnbuf.
738 error = VFS_MOUNT(mp, td);
740 if (mp->mnt_opt != NULL)
741 vfs_freeopts(mp->mnt_opt);
742 mp->mnt_opt = mp->mnt_optnew;
743 VFS_STATFS(mp, &mp->mnt_stat, td);
746 * Prevent external consumers of mount options from reading
749 mp->mnt_optnew = NULL;
750 if (mp->mnt_flag & MNT_UPDATE) {
752 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
755 mp->mnt_kern_flag = kern_flag;
757 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
758 if (mp->mnt_syncer == NULL)
759 error = vfs_allocate_syncvnode(mp);
761 if (mp->mnt_syncer != NULL)
762 vrele(mp->mnt_syncer);
763 mp->mnt_syncer = NULL;
767 vp->v_iflag &= ~VI_MOUNT;
772 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
774 * Put the new filesystem on the mount list after root.
781 vp->v_iflag &= ~VI_MOUNT;
783 vp->v_mountedhere = mp;
784 mtx_lock(&mountlist_mtx);
785 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
786 mtx_unlock(&mountlist_mtx);
787 vfs_event_signal(NULL, VQ_MOUNT, 0);
788 if (VFS_ROOT(mp, &newdp, td))
789 panic("mount: lost mount");
790 mountcheckdirs(vp, newdp);
792 VOP_UNLOCK(vp, 0, td);
793 if ((mp->mnt_flag & MNT_RDONLY) == 0)
794 error = vfs_allocate_syncvnode(mp);
800 vp->v_iflag &= ~VI_MOUNT;
802 vfs_mount_destroy(mp, td);
809 * ---------------------------------------------------------------------
810 * Unmount a filesystem.
812 * Note: unmount takes a path to the vnode mounted on as argument,
813 * not special file (as before).
815 #ifndef _SYS_SYSPROTO_H_
816 struct unmount_args {
825 register struct unmount_args /* {
834 if (jailed(td->td_ucred))
836 if (usermount == 0) {
837 if ((error = suser(td)) != 0)
841 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
842 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
844 free(pathbuf, M_TEMP);
847 if (uap->flags & MNT_BYFSID) {
848 /* Decode the filesystem ID. */
849 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
850 free(pathbuf, M_TEMP);
854 mtx_lock(&mountlist_mtx);
855 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
856 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
857 mp->mnt_stat.f_fsid.val[1] == id1)
860 mtx_unlock(&mountlist_mtx);
862 mtx_lock(&mountlist_mtx);
863 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
864 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
867 mtx_unlock(&mountlist_mtx);
869 free(pathbuf, M_TEMP);
872 * Previously we returned ENOENT for a nonexistent path and
873 * EINVAL for a non-mountpoint. We cannot tell these apart
874 * now, so in the !MNT_BYFSID case return the more likely
875 * EINVAL for compatibility.
877 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
881 * Only privileged root, or (if MNT_USER is set) the user that did the
882 * original mount is permitted to unmount this filesystem.
884 error = vfs_suser(mp, td);
889 * Don't allow unmounting the root filesystem.
891 if (mp->mnt_flag & MNT_ROOTFS)
894 error = dounmount(mp, uap->flags, td);
900 * Do the actual filesystem unmount.
903 dounmount(mp, flags, td)
908 struct vnode *coveredvp, *fsrootvp;
912 mtx_assert(&Giant, MA_OWNED);
915 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
919 mp->mnt_kern_flag |= MNTK_UNMOUNT;
920 /* Allow filesystems to detect that a forced unmount is in progress. */
921 if (flags & MNT_FORCE)
922 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
923 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
924 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
927 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
928 if (mp->mnt_kern_flag & MNTK_MWAIT)
933 vn_start_write(NULL, &mp, V_WAIT);
935 if (mp->mnt_flag & MNT_EXPUBLIC)
936 vfs_setpublicfs(NULL, NULL, NULL);
938 vfs_msync(mp, MNT_WAIT);
939 async_flag = mp->mnt_flag & MNT_ASYNC;
940 mp->mnt_flag &= ~MNT_ASYNC;
941 cache_purgevfs(mp); /* remove cache entries for this file sys */
942 if (mp->mnt_syncer != NULL)
943 vrele(mp->mnt_syncer);
945 * For forced unmounts, move process cdir/rdir refs on the fs root
946 * vnode to the covered vnode. For non-forced unmounts we want
947 * such references to cause an EBUSY error.
949 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) {
950 if (mp->mnt_vnodecovered != NULL)
951 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
952 if (fsrootvp == rootvnode) {
958 if (((mp->mnt_flag & MNT_RDONLY) ||
959 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
960 (flags & MNT_FORCE)) {
961 error = VFS_UNMOUNT(mp, flags, td);
963 vn_finished_write(mp);
965 /* Undo cdir/rdir and rootvnode changes made above. */
966 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) {
967 if (mp->mnt_vnodecovered != NULL)
968 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
969 if (rootvnode == NULL) {
970 rootvnode = fsrootvp;
975 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
976 (void) vfs_allocate_syncvnode(mp);
978 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
979 mp->mnt_flag |= async_flag;
980 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
981 if (mp->mnt_kern_flag & MNTK_MWAIT)
986 mtx_lock(&mountlist_mtx);
987 TAILQ_REMOVE(&mountlist, mp, mnt_list);
988 if ((coveredvp = mp->mnt_vnodecovered) != NULL)
989 coveredvp->v_mountedhere = NULL;
990 mtx_unlock(&mountlist_mtx);
991 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
992 vfs_mount_destroy(mp, td);
993 if (coveredvp != NULL)
999 * ---------------------------------------------------------------------
1000 * Mounting of root filesystem
1005 set_rootvnode(struct thread *td)
1009 if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode, td))
1010 panic("Cannot find root vnode");
1013 FILEDESC_LOCK(p->p_fd);
1015 if (p->p_fd->fd_cdir != NULL)
1016 vrele(p->p_fd->fd_cdir);
1017 p->p_fd->fd_cdir = rootvnode;
1020 if (p->p_fd->fd_rdir != NULL)
1021 vrele(p->p_fd->fd_rdir);
1022 p->p_fd->fd_rdir = rootvnode;
1025 FILEDESC_UNLOCK(p->p_fd);
1027 VOP_UNLOCK(rootvnode, 0, td);
1031 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1032 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1035 static struct mount *
1038 struct thread *td = curthread;
1039 struct vfsconf *vfsp;
1040 struct mount *mp = NULL;
1043 vfsp = vfs_byname("devfs");
1044 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1048 error = vfs_mount_alloc(NULLVP, vfsp, "/dev", td, &mp);
1049 KASSERT(error == 0, ("vfs_mount_alloc failed %d", error));
1053 error = VFS_MOUNT(mp, curthread);
1054 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1058 mtx_lock(&mountlist_mtx);
1059 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1060 mtx_unlock(&mountlist_mtx);
1064 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1066 printf("kern_symlink /dev -> / returns %d\n", error);
1072 * Surgically move our devfs to be mounted on /dev.
1076 devfs_fixup(struct thread *td)
1078 struct nameidata nd;
1080 struct vnode *vp, *dvp;
1083 /* Remove our devfs mount from the mountlist and purge the cache */
1084 mtx_lock(&mountlist_mtx);
1085 mp = TAILQ_FIRST(&mountlist);
1086 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1087 mtx_unlock(&mountlist_mtx);
1090 VFS_ROOT(mp, &dvp, td);
1092 dvp->v_iflag &= ~VI_MOUNT;
1093 dvp->v_mountedhere = NULL;
1096 /* Set up the real rootvnode, and purge the cache */
1097 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1099 cache_purgevfs(rootvnode->v_mount);
1101 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1104 printf("Lookup of /dev for devfs, error: %d\n", error);
1107 NDFREE(&nd, NDF_ONLY_PNBUF);
1109 if (vp->v_type != VDIR) {
1112 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1117 mp->mnt_vnodecovered = vp;
1118 vp->v_mountedhere = mp;
1119 mtx_lock(&mountlist_mtx);
1120 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1121 mtx_unlock(&mountlist_mtx);
1122 VOP_UNLOCK(vp, 0, td);
1126 /* Unlink the no longer needed /dev/dev -> / symlink */
1127 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1131 * Find and mount the root filesystem
1137 int error, i, asked = 0;
1141 * Wait for GEOM to settle down
1150 * We are booted with instructions to prompt for the root filesystem.
1152 if (boothowto & RB_ASKNAME) {
1153 if (!vfs_mountroot_ask())
1159 * The root filesystem information is compiled in, and we are
1160 * booted with instructions to use it.
1162 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1163 if (!vfs_mountroot_try(ctrootdevname))
1165 ctrootdevname = NULL;
1169 * We've been given the generic "use CDROM as root" flag. This is
1170 * necessary because one media may be used in many different
1171 * devices, so we need to search for them.
1173 if (boothowto & RB_CDROM) {
1174 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1175 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1181 * Try to use the value read by the loader from /etc/fstab, or
1182 * supplied via some other means. This is the preferred
1185 cp = getenv("vfs.root.mountfrom");
1187 error = vfs_mountroot_try(cp);
1194 * Try values that may have been computed by code during boot
1196 if (!vfs_mountroot_try(rootdevnames[0]))
1198 if (!vfs_mountroot_try(rootdevnames[1]))
1202 * If we (still) have a compiled-in default, try it.
1204 if (ctrootdevname != NULL)
1205 if (!vfs_mountroot_try(ctrootdevname))
1208 * Everything so far has failed, prompt on the console if we haven't
1209 * already tried that.
1212 if (!vfs_mountroot_ask())
1215 panic("Root mount failed, startup aborted.");
1219 * Mount (mountfrom) as the root filesystem.
1222 vfs_mountroot_try(const char *mountfrom)
1225 char *vfsname, *path;
1235 if (mountfrom == NULL)
1236 return (error); /* don't complain */
1238 s = splcam(); /* Overkill, but annoying without it */
1239 printf("Trying to mount root from %s\n", mountfrom);
1242 /* parse vfs name and path */
1243 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1244 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1245 vfsname[0] = path[0] = 0;
1246 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1247 if (sscanf(mountfrom, patt, vfsname, path) < 1) {
1248 free(path, M_MOUNT);
1249 free(vfsname, M_MOUNT);
1253 if (path[0] == '\0')
1254 strcpy(path, ROOTNAME);
1256 error = kernel_vmount(
1257 MNT_RDONLY | MNT_ROOTFS,
1263 mp = TAILQ_FIRST(&mountlist);
1265 /* sanity check system clock against root fs timestamp */
1266 inittodr(mp->mnt_time);
1267 vfs_unbusy(mp, curthread);
1269 devfs_fixup(curthread);
1275 * ---------------------------------------------------------------------
1276 * Interactive root filesystem selection code.
1280 vfs_mountroot_ask(void)
1285 printf("\nManual root filesystem specification:\n");
1286 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1287 #if defined(__i386__) || defined(__ia64__)
1288 printf(" eg. ufs:da0s1a\n");
1290 printf(" eg. ufs:/dev/da0a\n");
1292 printf(" ? List valid disk boot devices\n");
1293 printf(" <empty line> Abort manual input\n");
1294 printf("\nmountroot> ");
1295 gets(name, sizeof(name), 1);
1296 if (name[0] == '\0')
1298 if (name[0] == '?') {
1299 printf("\nList of GEOM managed disk devices:\n ");
1303 if (!vfs_mountroot_try(name))
1309 * ---------------------------------------------------------------------
1310 * Functions for querying mount options/arguments from filesystems.
1314 * Check that no unknown options are given
1317 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1323 TAILQ_FOREACH(opt, opts, link) {
1325 if (p[0] == 'n' && p[1] == 'o')
1327 for(t = global_opts; *t != NULL; t++)
1332 for(t = legal; *t != NULL; t++)
1337 printf("mount option <%s> is unknown\n", p);
1344 * Get a mount option by its name.
1346 * Return 0 if the option was found, ENOENT otherwise.
1347 * If len is non-NULL it will be filled with the length
1348 * of the option. If buf is non-NULL, it will be filled
1349 * with the address of the option.
1352 vfs_getopt(opts, name, buf, len)
1353 struct vfsoptlist *opts;
1360 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1362 TAILQ_FOREACH(opt, opts, link) {
1363 if (strcmp(name, opt->name) == 0) {
1375 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1380 TAILQ_FOREACH(opt, opts, link) {
1381 if (strcmp(name, opt->name) != 0)
1383 if (((char *)opt->value)[opt->len - 1] != '\0') {
1387 return (opt->value);
1393 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1397 TAILQ_FOREACH(opt, opts, link) {
1398 if (strcmp(name, opt->name) == 0) {
1410 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1416 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1418 TAILQ_FOREACH(opt, opts, link) {
1419 if (strcmp(name, opt->name) != 0)
1421 if (((char *)opt->value)[opt->len - 1] != '\0')
1424 ret = vsscanf(opt->value, fmt, ap);
1432 * Find and copy a mount option.
1434 * The size of the buffer has to be specified
1435 * in len, if it is not the same length as the
1436 * mount option, EINVAL is returned.
1437 * Returns ENOENT if the option is not found.
1440 vfs_copyopt(opts, name, dest, len)
1441 struct vfsoptlist *opts;
1448 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1450 TAILQ_FOREACH(opt, opts, link) {
1451 if (strcmp(name, opt->name) == 0) {
1452 if (len != opt->len)
1454 bcopy(opt->value, dest, opt->len);
1462 * This is a helper function for filesystems to traverse their
1463 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
1467 __mnt_vnode_next(struct vnode **nvp, struct mount *mp)
1471 mtx_assert(&mp->mnt_mtx, MA_OWNED);
1474 /* Check if we are done */
1477 /* If our next vnode is no longer ours, start over */
1478 if (vp->v_mount != mp)
1479 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1480 /* Save pointer to next vnode in list */
1482 *nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1489 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1493 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1494 if (sbp != &mp->mnt_stat)
1495 *sbp = mp->mnt_stat;
1500 vfs_mountedfrom(struct mount *mp, const char *from)
1503 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1504 strlcpy(mp->mnt_stat.f_mntfromname, from,
1505 sizeof mp->mnt_stat.f_mntfromname);
1509 * ---------------------------------------------------------------------
1510 * This is the api for building mount args and mounting filesystems from
1511 * inside the kernel.
1513 * The API works by accumulation of individual args. First error is
1516 * XXX: should be documented in new manpage kernel_mount(9)
1519 /* A memory allocation which must be freed when we are done */
1521 SLIST_ENTRY(mntaarg) next;
1524 /* The header for the mount arguments */
1529 SLIST_HEAD(, mntaarg) list;
1533 * Add a boolean argument.
1535 * flag is the boolean value.
1536 * name must start with "no".
1539 mount_argb(struct mntarg *ma, int flag, const char *name)
1542 KASSERT(name[0] == 'n' && name[1] == 'o',
1543 ("mount_argb(...,%s): name must start with 'no'", name));
1545 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1549 * Add an argument printf style
1552 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1555 struct mntaarg *maa;
1560 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1561 SLIST_INIT(&ma->list);
1566 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1568 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1569 ma->v[ma->len].iov_len = strlen(name) + 1;
1572 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1574 sbuf_vprintf(sb, fmt, ap);
1577 len = sbuf_len(sb) + 1;
1578 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1579 SLIST_INSERT_HEAD(&ma->list, maa, next);
1580 bcopy(sbuf_data(sb), maa + 1, len);
1583 ma->v[ma->len].iov_base = maa + 1;
1584 ma->v[ma->len].iov_len = len;
1591 * Add an argument which is a userland string.
1594 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1596 struct mntaarg *maa;
1602 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1603 SLIST_INIT(&ma->list);
1607 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1608 SLIST_INSERT_HEAD(&ma->list, maa, next);
1609 tbuf = (void *)(maa + 1);
1610 ma->error = copyinstr(val, tbuf, len, NULL);
1611 return (mount_arg(ma, name, tbuf, -1));
1617 * If length is -1, use printf.
1620 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1624 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1625 SLIST_INIT(&ma->list);
1630 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1632 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1633 ma->v[ma->len].iov_len = strlen(name) + 1;
1636 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1638 ma->v[ma->len].iov_len = strlen(val) + 1;
1640 ma->v[ma->len].iov_len = len;
1646 * Free a mntarg structure
1649 free_mntarg(struct mntarg *ma)
1651 struct mntaarg *maa;
1653 while (!SLIST_EMPTY(&ma->list)) {
1654 maa = SLIST_FIRST(&ma->list);
1655 SLIST_REMOVE_HEAD(&ma->list, next);
1658 free(ma->v, M_MOUNT);
1663 * Mount a filesystem
1666 kernel_mount(struct mntarg *ma, int flags)
1671 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1672 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1673 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1675 auio.uio_iov = ma->v;
1676 auio.uio_iovcnt = ma->len;
1677 auio.uio_segflg = UIO_SYSSPACE;
1681 error = vfs_donmount(curthread, flags, &auio);
1687 * A printflike function to mount a filesystem.
1690 kernel_vmount(int flags, ...)
1692 struct mntarg *ma = NULL;
1698 va_start(ap, flags);
1700 cp = va_arg(ap, const char *);
1703 vp = va_arg(ap, const void *);
1704 ma = mount_arg(ma, cp, vp, -1);
1708 error = kernel_mount(ma, flags);