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;
98 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
100 TAILQ_HEAD(vfsoptlist, vfsopt);
102 TAILQ_ENTRY(vfsopt) link;
109 * The vnode of the system's root (/ in the filesystem, without chroot
112 struct vnode *rootvnode;
115 * The root filesystem is detailed in the kernel environment variable
116 * vfs.root.mountfrom, which is expected to be in the general format
119 * vfsname := the name of a VFS known to the kernel and capable
120 * of being mounted as root
121 * path := disk device name or other data used by the filesystem
122 * to locate its physical store
126 * Global opts, taken by all filesystems
128 static const char *global_opts[] = {
138 * The root specifiers we will try if RB_CDROM is specified.
140 static char *cdrom_rootdevnames[] = {
146 /* legacy find-root code */
147 char *rootdevnames[2] = {NULL, NULL};
149 # define ROOTDEVNAME NULL
151 static const char *ctrootdevname = ROOTDEVNAME;
154 * ---------------------------------------------------------------------
155 * Functions for building and sanitizing the mount options
158 /* Remove one mount option. */
160 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
163 TAILQ_REMOVE(opts, opt, link);
164 free(opt->name, M_MOUNT);
165 if (opt->value != NULL)
166 free(opt->value, M_MOUNT);
168 else if (opt->len != 0)
169 panic("%s: mount option with NULL value but length != 0",
175 /* Release all resources related to the mount options. */
177 vfs_freeopts(struct vfsoptlist *opts)
181 while (!TAILQ_EMPTY(opts)) {
182 opt = TAILQ_FIRST(opts);
183 vfs_freeopt(opts, opt);
189 * Check if options are equal (with or without the "no" prefix).
192 vfs_equalopts(const char *opt1, const char *opt2)
195 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
196 if (strcmp(opt1, opt2) == 0)
198 /* "noopt" vs. "opt" */
199 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
201 /* "opt" vs. "noopt" */
202 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
208 * If a mount option is specified several times,
209 * (with or without the "no" prefix) only keep
210 * the last occurence of it.
213 vfs_sanitizeopts(struct vfsoptlist *opts)
215 struct vfsopt *opt, *opt2, *tmp;
217 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
218 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
219 while (opt2 != NULL) {
220 if (vfs_equalopts(opt->name, opt2->name)) {
221 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
222 vfs_freeopt(opts, opt2);
225 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
232 * Build a linked list of mount options from a struct uio.
235 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
237 struct vfsoptlist *opts;
240 unsigned int i, iovcnt;
241 int error, namelen, optlen;
243 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
246 iovcnt = auio->uio_iovcnt;
247 for (i = 0; i < iovcnt; i += 2) {
248 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
249 namelen = auio->uio_iov[i].iov_len;
250 optlen = auio->uio_iov[i + 1].iov_len;
251 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
256 * Do this early, so jumps to "bad" will free the current
259 TAILQ_INSERT_TAIL(opts, opt, link);
260 memused += sizeof(struct vfsopt) + optlen + namelen;
263 * Avoid consuming too much memory, and attempts to overflow
266 if (memused > VFS_MOUNTARG_SIZE_MAX ||
267 optlen > VFS_MOUNTARG_SIZE_MAX ||
268 namelen > VFS_MOUNTARG_SIZE_MAX) {
273 if (auio->uio_segflg == UIO_SYSSPACE) {
274 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
276 error = copyin(auio->uio_iov[i].iov_base, opt->name,
281 /* Ensure names are null-terminated strings. */
282 if (opt->name[namelen - 1] != '\0') {
288 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
289 if (auio->uio_segflg == UIO_SYSSPACE) {
290 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
293 error = copyin(auio->uio_iov[i + 1].iov_base,
300 vfs_sanitizeopts(opts);
309 * Merge the old mount options with the new ones passed
310 * in the MNT_UPDATE case.
313 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
315 struct vfsopt *opt, *opt2, *new;
317 TAILQ_FOREACH(opt, opts, link) {
319 * Check that this option hasn't been redefined
320 * nor cancelled with a "no" mount option.
322 opt2 = TAILQ_FIRST(toopts);
323 while (opt2 != NULL) {
324 if (strcmp(opt2->name, opt->name) == 0)
326 if (strncmp(opt2->name, "no", 2) == 0 &&
327 strcmp(opt2->name + 2, opt->name) == 0) {
328 vfs_freeopt(toopts, opt2);
331 opt2 = TAILQ_NEXT(opt2, link);
333 /* We want this option, duplicate it. */
334 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
335 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
336 strcpy(new->name, opt->name);
338 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
339 bcopy(opt->value, new->value, opt->len);
344 TAILQ_INSERT_TAIL(toopts, new, link);
351 * ---------------------------------------------------------------------
357 struct nmount_args /* {
369 /* Kick out MNT_ROOTFS early as it is legal internally */
370 if (uap->flags & MNT_ROOTFS)
373 iovcnt = uap->iovcnt;
375 * Check that we have an even number of iovec's
376 * and that we have at least two options.
378 if ((iovcnt & 1) || (iovcnt < 4))
381 error = copyinuio(uap->iovp, iovcnt, &auio);
385 for (i = 0; i < iovcnt; i++) {
386 if (iov->iov_len > MMAXOPTIONLEN) {
392 error = vfs_donmount(td, uap->flags, auio);
398 * ---------------------------------------------------------------------
399 * Various utility functions
403 * Allocate and initialize the mount point struct.
406 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
407 const char *fspath, struct thread *td, struct mount **mpp)
411 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
412 TAILQ_INIT(&mp->mnt_nvnodelist);
413 mp->mnt_nvnodelistsize = 0;
414 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
415 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
416 vfs_busy(mp, LK_NOWAIT, 0, td);
417 mp->mnt_op = vfsp->vfc_vfsops;
419 vfsp->vfc_refcount++;
420 mp->mnt_stat.f_type = vfsp->vfc_typenum;
421 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
422 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
423 mp->mnt_vnodecovered = vp;
424 mp->mnt_cred = crdup(td->td_ucred);
425 mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
426 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
427 mp->mnt_iosize_max = DFLTPHYS;
430 mac_create_mount(td->td_ucred, mp);
432 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
438 * Destroy the mount struct previously allocated by vfs_mount_alloc().
441 vfs_mount_destroy(struct mount *mp, struct thread *td)
444 mp->mnt_vfc->vfc_refcount--;
445 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
446 panic("unmount: dangling vnode");
448 lockdestroy(&mp->mnt_lock);
450 if (mp->mnt_kern_flag & MNTK_MWAIT)
453 mtx_destroy(&mp->mnt_mtx);
455 mac_destroy_mount(mp);
457 if (mp->mnt_opt != NULL)
458 vfs_freeopts(mp->mnt_opt);
459 crfree(mp->mnt_cred);
464 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
466 struct vfsoptlist *optlist;
467 char *fstype, *fspath;
468 int error, fstypelen, fspathlen;
470 error = vfs_buildopts(fsoptions, &optlist);
475 * We need these two options before the others,
476 * and they are mandatory for any filesystem.
477 * Ensure they are NUL terminated as well.
480 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
481 if (error || fstype[fstypelen - 1] != '\0') {
486 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
487 if (error || fspath[fspathlen - 1] != '\0') {
493 * Be ultra-paranoid about making sure the type and fspath
494 * variables will fit in our mp buffers, including the
497 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
498 error = ENAMETOOLONG;
503 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
507 vfs_freeopts(optlist);
512 * ---------------------------------------------------------------------
515 #ifndef _SYS_SYSPROTO_H_
527 struct mount_args /* {
535 struct vfsconf *vfsp = NULL;
536 struct mntarg *ma = NULL;
539 /* Kick out MNT_ROOTFS early as it is legal internally */
540 uap->flags &= ~MNT_ROOTFS;
542 if (uap->data == NULL)
545 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
546 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
548 mtx_lock(&Giant); /* XXX ? */
549 vfsp = vfs_byname_kld(fstype, td, &error);
552 free(fstype, M_TEMP);
557 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
560 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
561 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
562 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
563 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
564 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
566 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
572 * vfs_domount(): actually attempt a filesystem mount.
576 struct thread *td, /* Flags common to all filesystems. */
577 const char *fstype, /* Filesystem type. */
578 char *fspath, /* Mount path. */
579 int fsflags, /* Flags common to all filesystems. */
580 void *fsdata /* Options local to the filesystem. */
585 struct vfsconf *vfsp;
586 int error, flag = 0, kern_flag = 0;
590 mtx_assert(&Giant, MA_OWNED);
593 * Be ultra-paranoid about making sure the type and fspath
594 * variables will fit in our mp buffers, including the
597 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
598 return (ENAMETOOLONG);
600 if (jailed(td->td_ucred))
602 if (usermount == 0) {
603 if ((error = suser(td)) != 0)
608 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
610 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
611 if ((error = suser(td)) != 0)
615 * Silently enforce MNT_NOSUID and MNT_USER for
616 * unprivileged users.
619 fsflags |= MNT_NOSUID | MNT_USER;
621 * Get vnode to be covered
623 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
624 if ((error = namei(&nd)) != 0)
626 NDFREE(&nd, NDF_ONLY_PNBUF);
628 if (fsflags & MNT_UPDATE) {
629 if ((vp->v_vflag & VV_ROOT) == 0) {
635 kern_flag = mp->mnt_kern_flag;
637 * We only allow the filesystem to be reloaded if it
638 * is currently mounted read-only.
640 if ((fsflags & MNT_RELOAD) &&
641 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
643 return (EOPNOTSUPP); /* Needs translation */
646 * Only privileged root, or (if MNT_USER is set) the user that
647 * did the original mount is permitted to update it.
649 error = vfs_suser(mp, td);
654 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
659 if ((vp->v_iflag & VI_MOUNT) != 0 ||
660 vp->v_mountedhere != NULL) {
666 vp->v_iflag |= VI_MOUNT;
668 mp->mnt_flag |= fsflags &
669 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
670 VOP_UNLOCK(vp, 0, td);
671 mp->mnt_optnew = fsdata;
672 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
675 * If the user is not root, ensure that they own the directory
676 * onto which we are attempting to mount.
678 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
683 if (va.va_uid != td->td_ucred->cr_uid) {
684 if ((error = suser(td)) != 0) {
689 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
694 if (vp->v_type != VDIR) {
698 vfsp = vfs_byname_kld(fstype, td, &error);
704 if ((vp->v_iflag & VI_MOUNT) != 0 ||
705 vp->v_mountedhere != NULL) {
710 vp->v_iflag |= VI_MOUNT;
714 * Allocate and initialize the filesystem.
716 error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp);
721 VOP_UNLOCK(vp, 0, td);
723 /* XXXMAC: pass to vfs_mount_alloc? */
724 mp->mnt_optnew = fsdata;
728 * Set the mount level flags.
730 if (fsflags & MNT_RDONLY)
731 mp->mnt_flag |= MNT_RDONLY;
732 mp->mnt_flag &=~ MNT_UPDATEMASK;
733 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
735 * Mount the filesystem.
736 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
737 * get. No freeing of cn_pnbuf.
739 error = VFS_MOUNT(mp, td);
741 if (mp->mnt_opt != NULL)
742 vfs_freeopts(mp->mnt_opt);
743 mp->mnt_opt = mp->mnt_optnew;
744 VFS_STATFS(mp, &mp->mnt_stat, td);
747 * Prevent external consumers of mount options from reading
750 mp->mnt_optnew = NULL;
751 if (mp->mnt_flag & MNT_UPDATE) {
753 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
756 mp->mnt_kern_flag = kern_flag;
758 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
759 if (mp->mnt_syncer == NULL)
760 error = vfs_allocate_syncvnode(mp);
762 if (mp->mnt_syncer != NULL)
763 vrele(mp->mnt_syncer);
764 mp->mnt_syncer = NULL;
768 vp->v_iflag &= ~VI_MOUNT;
773 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
775 * Put the new filesystem on the mount list after root.
782 vp->v_iflag &= ~VI_MOUNT;
784 vp->v_mountedhere = mp;
785 mtx_lock(&mountlist_mtx);
786 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
787 mtx_unlock(&mountlist_mtx);
788 vfs_event_signal(NULL, VQ_MOUNT, 0);
789 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
790 panic("mount: lost mount");
791 mountcheckdirs(vp, newdp);
793 VOP_UNLOCK(vp, 0, td);
794 if ((mp->mnt_flag & MNT_RDONLY) == 0)
795 error = vfs_allocate_syncvnode(mp);
801 vp->v_iflag &= ~VI_MOUNT;
803 vfs_mount_destroy(mp, td);
810 * ---------------------------------------------------------------------
811 * Unmount a filesystem.
813 * Note: unmount takes a path to the vnode mounted on as argument,
814 * not special file (as before).
816 #ifndef _SYS_SYSPROTO_H_
817 struct unmount_args {
826 register struct unmount_args /* {
835 if (jailed(td->td_ucred))
837 if (usermount == 0) {
838 if ((error = suser(td)) != 0)
842 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
843 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
845 free(pathbuf, M_TEMP);
848 if (uap->flags & MNT_BYFSID) {
849 /* Decode the filesystem ID. */
850 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
851 free(pathbuf, M_TEMP);
855 mtx_lock(&mountlist_mtx);
856 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
857 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
858 mp->mnt_stat.f_fsid.val[1] == id1)
861 mtx_unlock(&mountlist_mtx);
863 mtx_lock(&mountlist_mtx);
864 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
865 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
868 mtx_unlock(&mountlist_mtx);
870 free(pathbuf, M_TEMP);
873 * Previously we returned ENOENT for a nonexistent path and
874 * EINVAL for a non-mountpoint. We cannot tell these apart
875 * now, so in the !MNT_BYFSID case return the more likely
876 * EINVAL for compatibility.
878 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
882 * Only privileged root, or (if MNT_USER is set) the user that did the
883 * original mount is permitted to unmount this filesystem.
885 error = vfs_suser(mp, td);
890 * Don't allow unmounting the root filesystem.
892 if (mp->mnt_flag & MNT_ROOTFS)
895 error = dounmount(mp, uap->flags, td);
901 * Do the actual filesystem unmount.
904 dounmount(mp, flags, td)
909 struct vnode *coveredvp, *fsrootvp;
913 mtx_assert(&Giant, MA_OWNED);
916 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
920 mp->mnt_kern_flag |= MNTK_UNMOUNT;
921 /* Allow filesystems to detect that a forced unmount is in progress. */
922 if (flags & MNT_FORCE)
923 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
924 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
925 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
928 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
929 if (mp->mnt_kern_flag & MNTK_MWAIT)
934 vn_start_write(NULL, &mp, V_WAIT);
936 if (mp->mnt_flag & MNT_EXPUBLIC)
937 vfs_setpublicfs(NULL, NULL, NULL);
939 vfs_msync(mp, MNT_WAIT);
940 async_flag = mp->mnt_flag & MNT_ASYNC;
941 mp->mnt_flag &= ~MNT_ASYNC;
942 cache_purgevfs(mp); /* remove cache entries for this file sys */
943 if (mp->mnt_syncer != NULL)
944 vrele(mp->mnt_syncer);
946 * For forced unmounts, move process cdir/rdir refs on the fs root
947 * vnode to the covered vnode. For non-forced unmounts we want
948 * such references to cause an EBUSY error.
950 if ((flags & MNT_FORCE) &&
951 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
952 if (mp->mnt_vnodecovered != NULL)
953 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
954 if (fsrootvp == rootvnode) {
960 if (((mp->mnt_flag & MNT_RDONLY) ||
961 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
962 (flags & MNT_FORCE)) {
963 error = VFS_UNMOUNT(mp, flags, td);
965 vn_finished_write(mp);
967 /* Undo cdir/rdir and rootvnode changes made above. */
968 if ((flags & MNT_FORCE) &&
969 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
970 if (mp->mnt_vnodecovered != NULL)
971 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
972 if (rootvnode == NULL) {
973 rootvnode = fsrootvp;
978 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
979 (void) vfs_allocate_syncvnode(mp);
981 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
982 mp->mnt_flag |= async_flag;
983 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
984 if (mp->mnt_kern_flag & MNTK_MWAIT)
989 mtx_lock(&mountlist_mtx);
990 TAILQ_REMOVE(&mountlist, mp, mnt_list);
991 if ((coveredvp = mp->mnt_vnodecovered) != NULL)
992 coveredvp->v_mountedhere = NULL;
993 mtx_unlock(&mountlist_mtx);
994 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
995 vfs_mount_destroy(mp, td);
996 if (coveredvp != NULL)
1002 * ---------------------------------------------------------------------
1003 * Mounting of root filesystem
1007 struct root_hold_token {
1009 LIST_ENTRY(root_hold_token) list;
1012 static LIST_HEAD(, root_hold_token) root_holds =
1013 LIST_HEAD_INITIALIZER(&root_holds);
1015 struct root_hold_token *
1016 root_mount_hold(const char *identifier)
1018 struct root_hold_token *h;
1020 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1021 h->who = identifier;
1022 mtx_lock(&mountlist_mtx);
1023 LIST_INSERT_HEAD(&root_holds, h, list);
1024 mtx_unlock(&mountlist_mtx);
1029 root_mount_rel(struct root_hold_token *h)
1032 mtx_lock(&mountlist_mtx);
1033 LIST_REMOVE(h, list);
1034 wakeup(&root_holds);
1035 mtx_unlock(&mountlist_mtx);
1040 root_mount_wait(void)
1042 struct root_hold_token *h;
1048 mtx_lock(&mountlist_mtx);
1049 if (LIST_EMPTY(&root_holds)) {
1050 mtx_unlock(&mountlist_mtx);
1053 printf("Root mount waiting for:");
1054 LIST_FOREACH(h, &root_holds, list)
1055 printf(" %s", h->who);
1057 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1063 set_rootvnode(struct thread *td)
1067 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1068 panic("Cannot find root vnode");
1071 FILEDESC_LOCK(p->p_fd);
1073 if (p->p_fd->fd_cdir != NULL)
1074 vrele(p->p_fd->fd_cdir);
1075 p->p_fd->fd_cdir = rootvnode;
1078 if (p->p_fd->fd_rdir != NULL)
1079 vrele(p->p_fd->fd_rdir);
1080 p->p_fd->fd_rdir = rootvnode;
1083 FILEDESC_UNLOCK(p->p_fd);
1085 VOP_UNLOCK(rootvnode, 0, td);
1089 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1090 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1096 struct thread *td = curthread;
1097 struct vfsconf *vfsp;
1098 struct mount *mp = NULL;
1101 vfsp = vfs_byname("devfs");
1102 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1106 error = vfs_mount_alloc(NULLVP, vfsp, "/dev", td, &mp);
1107 KASSERT(error == 0, ("vfs_mount_alloc failed %d", error));
1111 error = VFS_MOUNT(mp, curthread);
1112 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1116 mtx_lock(&mountlist_mtx);
1117 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1118 mtx_unlock(&mountlist_mtx);
1122 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1124 printf("kern_symlink /dev -> / returns %d\n", error);
1128 * Surgically move our devfs to be mounted on /dev.
1132 devfs_fixup(struct thread *td)
1134 struct nameidata nd;
1136 struct vnode *vp, *dvp;
1139 /* Remove our devfs mount from the mountlist and purge the cache */
1140 mtx_lock(&mountlist_mtx);
1141 mp = TAILQ_FIRST(&mountlist);
1142 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1143 mtx_unlock(&mountlist_mtx);
1146 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1148 dvp->v_iflag &= ~VI_MOUNT;
1149 dvp->v_mountedhere = NULL;
1152 /* Set up the real rootvnode, and purge the cache */
1153 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1155 cache_purgevfs(rootvnode->v_mount);
1157 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1160 printf("Lookup of /dev for devfs, error: %d\n", error);
1163 NDFREE(&nd, NDF_ONLY_PNBUF);
1165 if (vp->v_type != VDIR) {
1168 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1173 mp->mnt_vnodecovered = vp;
1174 vp->v_mountedhere = mp;
1175 mtx_lock(&mountlist_mtx);
1176 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1177 mtx_unlock(&mountlist_mtx);
1178 VOP_UNLOCK(vp, 0, td);
1182 /* Unlink the no longer needed /dev/dev -> / symlink */
1183 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1187 * Find and mount the root filesystem
1193 int error, i, asked = 0;
1200 * We are booted with instructions to prompt for the root filesystem.
1202 if (boothowto & RB_ASKNAME) {
1203 if (!vfs_mountroot_ask())
1209 * The root filesystem information is compiled in, and we are
1210 * booted with instructions to use it.
1212 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1213 if (!vfs_mountroot_try(ctrootdevname))
1215 ctrootdevname = NULL;
1219 * We've been given the generic "use CDROM as root" flag. This is
1220 * necessary because one media may be used in many different
1221 * devices, so we need to search for them.
1223 if (boothowto & RB_CDROM) {
1224 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1225 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1231 * Try to use the value read by the loader from /etc/fstab, or
1232 * supplied via some other means. This is the preferred
1235 cp = getenv("vfs.root.mountfrom");
1237 error = vfs_mountroot_try(cp);
1244 * Try values that may have been computed by code during boot
1246 if (!vfs_mountroot_try(rootdevnames[0]))
1248 if (!vfs_mountroot_try(rootdevnames[1]))
1252 * If we (still) have a compiled-in default, try it.
1254 if (ctrootdevname != NULL)
1255 if (!vfs_mountroot_try(ctrootdevname))
1258 * Everything so far has failed, prompt on the console if we haven't
1259 * already tried that.
1262 if (!vfs_mountroot_ask())
1265 panic("Root mount failed, startup aborted.");
1269 * Mount (mountfrom) as the root filesystem.
1272 vfs_mountroot_try(const char *mountfrom)
1275 char *vfsname, *path;
1284 if (mountfrom == NULL)
1285 return (error); /* don't complain */
1286 printf("Trying to mount root from %s\n", mountfrom);
1288 /* parse vfs name and path */
1289 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1290 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1291 vfsname[0] = path[0] = 0;
1292 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1293 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1296 if (path[0] == '\0')
1297 strcpy(path, ROOTNAME);
1299 error = kernel_vmount(
1300 MNT_RDONLY | MNT_ROOTFS,
1307 * We mount devfs prior to mounting the / FS, so the first
1308 * entry will typically be devfs.
1310 mp = TAILQ_FIRST(&mountlist);
1311 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1312 vfs_unbusy(mp, curthread);
1315 * Iterate over all currently mounted file systems and use
1316 * the time stamp found to check and/or initialize the RTC.
1317 * Typically devfs has no time stamp and the only other FS
1318 * is the actual / FS.
1321 if (mp->mnt_time != 0)
1322 inittodr(mp->mnt_time);
1323 mp = TAILQ_NEXT(mp, mnt_list);
1324 } while (mp != NULL);
1326 devfs_fixup(curthread);
1329 free(path, M_MOUNT);
1330 free(vfsname, M_MOUNT);
1335 * ---------------------------------------------------------------------
1336 * Interactive root filesystem selection code.
1340 vfs_mountroot_ask(void)
1345 printf("\nManual root filesystem specification:\n");
1346 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1347 #if defined(__i386__) || defined(__ia64__)
1348 printf(" eg. ufs:da0s1a\n");
1350 printf(" eg. ufs:/dev/da0a\n");
1352 printf(" ? List valid disk boot devices\n");
1353 printf(" <empty line> Abort manual input\n");
1354 printf("\nmountroot> ");
1355 gets(name, sizeof(name), 1);
1356 if (name[0] == '\0')
1358 if (name[0] == '?') {
1359 printf("\nList of GEOM managed disk devices:\n ");
1363 if (!vfs_mountroot_try(name))
1369 * ---------------------------------------------------------------------
1370 * Functions for querying mount options/arguments from filesystems.
1374 * Check that no unknown options are given
1377 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1383 TAILQ_FOREACH(opt, opts, link) {
1385 if (p[0] == 'n' && p[1] == 'o')
1387 for(t = global_opts; *t != NULL; t++)
1392 for(t = legal; *t != NULL; t++)
1397 printf("mount option <%s> is unknown\n", p);
1404 * Get a mount option by its name.
1406 * Return 0 if the option was found, ENOENT otherwise.
1407 * If len is non-NULL it will be filled with the length
1408 * of the option. If buf is non-NULL, it will be filled
1409 * with the address of the option.
1412 vfs_getopt(opts, name, buf, len)
1413 struct vfsoptlist *opts;
1420 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1422 TAILQ_FOREACH(opt, opts, link) {
1423 if (strcmp(name, opt->name) == 0) {
1435 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1440 TAILQ_FOREACH(opt, opts, link) {
1441 if (strcmp(name, opt->name) != 0)
1443 if (((char *)opt->value)[opt->len - 1] != '\0') {
1447 return (opt->value);
1453 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1457 TAILQ_FOREACH(opt, opts, link) {
1458 if (strcmp(name, opt->name) == 0) {
1470 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1476 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1478 TAILQ_FOREACH(opt, opts, link) {
1479 if (strcmp(name, opt->name) != 0)
1481 if (((char *)opt->value)[opt->len - 1] != '\0')
1484 ret = vsscanf(opt->value, fmt, ap);
1492 * Find and copy a mount option.
1494 * The size of the buffer has to be specified
1495 * in len, if it is not the same length as the
1496 * mount option, EINVAL is returned.
1497 * Returns ENOENT if the option is not found.
1500 vfs_copyopt(opts, name, dest, len)
1501 struct vfsoptlist *opts;
1508 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1510 TAILQ_FOREACH(opt, opts, link) {
1511 if (strcmp(name, opt->name) == 0) {
1512 if (len != opt->len)
1514 bcopy(opt->value, dest, opt->len);
1522 * This is a helper function for filesystems to traverse their
1523 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
1527 __mnt_vnode_next(struct vnode **nvp, struct mount *mp)
1531 mtx_assert(&mp->mnt_mtx, MA_OWNED);
1534 /* Check if we are done */
1537 /* If our next vnode is no longer ours, start over */
1538 if (vp->v_mount != mp)
1539 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1540 /* Save pointer to next vnode in list */
1542 *nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1549 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1553 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1554 if (sbp != &mp->mnt_stat)
1555 *sbp = mp->mnt_stat;
1560 vfs_mountedfrom(struct mount *mp, const char *from)
1563 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1564 strlcpy(mp->mnt_stat.f_mntfromname, from,
1565 sizeof mp->mnt_stat.f_mntfromname);
1569 * ---------------------------------------------------------------------
1570 * This is the api for building mount args and mounting filesystems from
1571 * inside the kernel.
1573 * The API works by accumulation of individual args. First error is
1576 * XXX: should be documented in new manpage kernel_mount(9)
1579 /* A memory allocation which must be freed when we are done */
1581 SLIST_ENTRY(mntaarg) next;
1584 /* The header for the mount arguments */
1589 SLIST_HEAD(, mntaarg) list;
1593 * Add a boolean argument.
1595 * flag is the boolean value.
1596 * name must start with "no".
1599 mount_argb(struct mntarg *ma, int flag, const char *name)
1602 KASSERT(name[0] == 'n' && name[1] == 'o',
1603 ("mount_argb(...,%s): name must start with 'no'", name));
1605 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1609 * Add an argument printf style
1612 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1615 struct mntaarg *maa;
1620 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1621 SLIST_INIT(&ma->list);
1626 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1628 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1629 ma->v[ma->len].iov_len = strlen(name) + 1;
1632 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1634 sbuf_vprintf(sb, fmt, ap);
1637 len = sbuf_len(sb) + 1;
1638 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1639 SLIST_INSERT_HEAD(&ma->list, maa, next);
1640 bcopy(sbuf_data(sb), maa + 1, len);
1643 ma->v[ma->len].iov_base = maa + 1;
1644 ma->v[ma->len].iov_len = len;
1651 * Add an argument which is a userland string.
1654 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1656 struct mntaarg *maa;
1662 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1663 SLIST_INIT(&ma->list);
1667 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1668 SLIST_INSERT_HEAD(&ma->list, maa, next);
1669 tbuf = (void *)(maa + 1);
1670 ma->error = copyinstr(val, tbuf, len, NULL);
1671 return (mount_arg(ma, name, tbuf, -1));
1677 * If length is -1, use printf.
1680 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1684 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1685 SLIST_INIT(&ma->list);
1690 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1692 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1693 ma->v[ma->len].iov_len = strlen(name) + 1;
1696 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1698 ma->v[ma->len].iov_len = strlen(val) + 1;
1700 ma->v[ma->len].iov_len = len;
1706 * Free a mntarg structure
1709 free_mntarg(struct mntarg *ma)
1711 struct mntaarg *maa;
1713 while (!SLIST_EMPTY(&ma->list)) {
1714 maa = SLIST_FIRST(&ma->list);
1715 SLIST_REMOVE_HEAD(&ma->list, next);
1718 free(ma->v, M_MOUNT);
1723 * Mount a filesystem
1726 kernel_mount(struct mntarg *ma, int flags)
1731 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1732 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1733 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1735 auio.uio_iov = ma->v;
1736 auio.uio_iovcnt = ma->len;
1737 auio.uio_segflg = UIO_SYSSPACE;
1741 error = vfs_donmount(curthread, flags, &auio);
1747 * A printflike function to mount a filesystem.
1750 kernel_vmount(int flags, ...)
1752 struct mntarg *ma = NULL;
1758 va_start(ap, flags);
1760 cp = va_arg(ap, const char *);
1763 vp = va_arg(ap, const void *);
1764 ma = mount_arg(ma, cp, vp, -1);
1768 error = kernel_mount(ma, flags);