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 struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
79 const char *fspath, struct thread *td);
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 *);
86 static int vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
88 static int usermount = 0;
89 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
90 "Unprivileged users may mount and unmount file systems");
92 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
93 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
95 /* List of mounted filesystems. */
96 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
98 /* For any iteration/modification of mountlist */
99 struct mtx mountlist_mtx;
100 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
102 TAILQ_HEAD(vfsoptlist, vfsopt);
104 TAILQ_ENTRY(vfsopt) link;
111 * The vnode of the system's root (/ in the filesystem, without chroot
114 struct vnode *rootvnode;
117 * The root filesystem is detailed in the kernel environment variable
118 * vfs.root.mountfrom, which is expected to be in the general format
121 * vfsname := the name of a VFS known to the kernel and capable
122 * of being mounted as root
123 * path := disk device name or other data used by the filesystem
124 * to locate its physical store
128 * Global opts, taken by all filesystems
130 static const char *global_opts[] = {
143 * The root specifiers we will try if RB_CDROM is specified.
145 static char *cdrom_rootdevnames[] = {
151 /* legacy find-root code */
152 char *rootdevnames[2] = {NULL, NULL};
154 # define ROOTDEVNAME NULL
156 static const char *ctrootdevname = ROOTDEVNAME;
159 * ---------------------------------------------------------------------
160 * Functions for building and sanitizing the mount options
163 /* Remove one mount option. */
165 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
168 TAILQ_REMOVE(opts, opt, link);
169 free(opt->name, M_MOUNT);
170 if (opt->value != NULL)
171 free(opt->value, M_MOUNT);
173 else if (opt->len != 0)
174 panic("%s: mount option with NULL value but length != 0",
180 /* Release all resources related to the mount options. */
182 vfs_freeopts(struct vfsoptlist *opts)
186 while (!TAILQ_EMPTY(opts)) {
187 opt = TAILQ_FIRST(opts);
188 vfs_freeopt(opts, opt);
194 * Check if options are equal (with or without the "no" prefix).
197 vfs_equalopts(const char *opt1, const char *opt2)
200 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
201 if (strcmp(opt1, opt2) == 0)
203 /* "noopt" vs. "opt" */
204 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
206 /* "opt" vs. "noopt" */
207 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
213 * If a mount option is specified several times,
214 * (with or without the "no" prefix) only keep
215 * the last occurence of it.
218 vfs_sanitizeopts(struct vfsoptlist *opts)
220 struct vfsopt *opt, *opt2, *tmp;
222 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
223 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
224 while (opt2 != NULL) {
225 if (vfs_equalopts(opt->name, opt2->name)) {
226 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
227 vfs_freeopt(opts, opt2);
230 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
237 * Build a linked list of mount options from a struct uio.
240 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
242 struct vfsoptlist *opts;
245 unsigned int i, iovcnt;
246 int error, namelen, optlen;
248 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
251 iovcnt = auio->uio_iovcnt;
252 for (i = 0; i < iovcnt; i += 2) {
253 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
254 namelen = auio->uio_iov[i].iov_len;
255 optlen = auio->uio_iov[i + 1].iov_len;
256 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
261 * Do this early, so jumps to "bad" will free the current
264 TAILQ_INSERT_TAIL(opts, opt, link);
265 memused += sizeof(struct vfsopt) + optlen + namelen;
268 * Avoid consuming too much memory, and attempts to overflow
271 if (memused > VFS_MOUNTARG_SIZE_MAX ||
272 optlen > VFS_MOUNTARG_SIZE_MAX ||
273 namelen > VFS_MOUNTARG_SIZE_MAX) {
278 if (auio->uio_segflg == UIO_SYSSPACE) {
279 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
281 error = copyin(auio->uio_iov[i].iov_base, opt->name,
286 /* Ensure names are null-terminated strings. */
287 if (opt->name[namelen - 1] != '\0') {
293 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
294 if (auio->uio_segflg == UIO_SYSSPACE) {
295 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
298 error = copyin(auio->uio_iov[i + 1].iov_base,
305 vfs_sanitizeopts(opts);
314 * Merge the old mount options with the new ones passed
315 * in the MNT_UPDATE case.
318 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
320 struct vfsopt *opt, *opt2, *new;
322 TAILQ_FOREACH(opt, opts, link) {
324 * Check that this option hasn't been redefined
325 * nor cancelled with a "no" mount option.
327 opt2 = TAILQ_FIRST(toopts);
328 while (opt2 != NULL) {
329 if (strcmp(opt2->name, opt->name) == 0)
331 if (strncmp(opt2->name, "no", 2) == 0 &&
332 strcmp(opt2->name + 2, opt->name) == 0) {
333 vfs_freeopt(toopts, opt2);
336 opt2 = TAILQ_NEXT(opt2, link);
338 /* We want this option, duplicate it. */
339 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
340 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
341 strcpy(new->name, opt->name);
343 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
344 bcopy(opt->value, new->value, opt->len);
349 TAILQ_INSERT_TAIL(toopts, new, link);
356 * ---------------------------------------------------------------------
362 struct nmount_args /* {
374 /* Kick out MNT_ROOTFS early as it is legal internally */
375 if (uap->flags & MNT_ROOTFS)
378 iovcnt = uap->iovcnt;
380 * Check that we have an even number of iovec's
381 * and that we have at least two options.
383 if ((iovcnt & 1) || (iovcnt < 4))
386 error = copyinuio(uap->iovp, iovcnt, &auio);
390 for (i = 0; i < iovcnt; i++) {
391 if (iov->iov_len > MMAXOPTIONLEN) {
397 error = vfs_donmount(td, uap->flags, auio);
404 * ---------------------------------------------------------------------
405 * Various utility functions
409 vfs_ref(struct mount *mp)
418 vfs_rel(struct mount *mp)
427 * Allocate and initialize the mount point struct.
429 static struct mount *
430 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
431 const char *fspath, struct thread *td)
435 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
436 TAILQ_INIT(&mp->mnt_nvnodelist);
437 mp->mnt_nvnodelistsize = 0;
438 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
439 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
440 (void) vfs_busy(mp, LK_NOWAIT, 0, td);
442 mp->mnt_op = vfsp->vfc_vfsops;
444 vfsp->vfc_refcount++; /* XXX Unlocked */
445 mp->mnt_stat.f_type = vfsp->vfc_typenum;
446 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
447 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
448 mp->mnt_vnodecovered = vp;
449 mp->mnt_cred = crdup(td->td_ucred);
450 mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
451 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
452 mp->mnt_iosize_max = DFLTPHYS;
455 mac_create_mount(td->td_ucred, mp);
457 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
462 * Destroy the mount struct previously allocated by vfs_mount_alloc().
465 vfs_mount_destroy(struct mount *mp, struct thread *td)
471 for (i = 0; mp->mnt_ref && i < 3; i++)
472 msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
474 * This will always cause a 3 second delay in rebooting due to
475 * refs on the root mountpoint that never go away. Most of these
476 * are held by init which never exits.
478 if (i == 3 && (!rebooting || bootverbose))
479 printf("Mount point %s had %d dangling refs\n",
480 mp->mnt_stat.f_mntonname, mp->mnt_ref);
481 if (mp->mnt_holdcnt != 0) {
482 printf("Waiting for mount point to be unheld\n");
483 while (mp->mnt_holdcnt != 0) {
484 mp->mnt_holdcntwaiters++;
485 msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
486 PZERO, "mntdestroy", 0);
487 mp->mnt_holdcntwaiters--;
489 printf("mount point unheld\n");
491 if (mp->mnt_writeopcount > 0) {
492 printf("Waiting for mount point write ops\n");
493 while (mp->mnt_writeopcount > 0) {
494 mp->mnt_kern_flag |= MNTK_SUSPEND;
495 msleep(&mp->mnt_writeopcount,
497 PZERO, "mntdestroy2", 0);
499 printf("mount point write ops completed\n");
502 mp->mnt_vfc->vfc_refcount--;
503 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
504 panic("unmount: dangling vnode");
505 lockdestroy(&mp->mnt_lock);
507 if (mp->mnt_kern_flag & MNTK_MWAIT)
509 if (mp->mnt_writeopcount != 0)
510 panic("vfs_mount_destroy: nonzero writeopcount");
511 if (mp->mnt_nvnodelistsize != 0)
512 panic("vfs_mount_destroy: nonzero nvnodelistsize");
513 mp->mnt_writeopcount = -1000;
514 mp->mnt_nvnodelistsize = -1000;
516 mtx_destroy(&mp->mnt_mtx);
518 mac_destroy_mount(mp);
520 if (mp->mnt_opt != NULL)
521 vfs_freeopts(mp->mnt_opt);
522 crfree(mp->mnt_cred);
527 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
529 struct vfsoptlist *optlist;
530 char *fstype, *fspath, *errmsg;
531 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
536 error = vfs_buildopts(fsoptions, &optlist);
540 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
541 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
546 * We need these two options before the others,
547 * and they are mandatory for any filesystem.
548 * Ensure they are NUL terminated as well.
551 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
552 if (error || fstype[fstypelen - 1] != '\0') {
555 strncpy(errmsg, "Invalid fstype", errmsg_len);
559 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
560 if (error || fspath[fspathlen - 1] != '\0') {
563 strncpy(errmsg, "Invalid fspath", errmsg_len);
568 * We need to see if we have the "update" option
569 * before we call vfs_domount(), since vfs_domount() has special
570 * logic based on MNT_UPDATE. This is very important
571 * when we want to update the root filesystem.
573 if (vfs_getopt(optlist, "update", NULL, NULL) == 0)
574 fsflags |= MNT_UPDATE;
576 if (vfs_getopt(optlist, "async", NULL, NULL) == 0)
577 fsflags |= MNT_ASYNC;
579 if (vfs_getopt(optlist, "force", NULL, NULL) == 0)
580 fsflags |= MNT_FORCE;
582 if (vfs_getopt(optlist, "multilabel", NULL, NULL) == 0)
583 fsflags |= MNT_MULTILABEL;
585 if (vfs_getopt(optlist, "noasync", NULL, NULL) == 0)
586 fsflags &= ~MNT_ASYNC;
588 if (vfs_getopt(optlist, "noatime", NULL, NULL) == 0)
589 fsflags |= MNT_NOATIME;
591 if (vfs_getopt(optlist, "noclusterr", NULL, NULL) == 0)
592 fsflags |= MNT_NOCLUSTERR;
594 if (vfs_getopt(optlist, "noclusterw", NULL, NULL) == 0)
595 fsflags |= MNT_NOCLUSTERW;
597 if (vfs_getopt(optlist, "noexec", NULL, NULL) == 0)
598 fsflags |= MNT_NOEXEC;
600 if (vfs_getopt(optlist, "nosuid", NULL, NULL) == 0)
601 fsflags |= MNT_NOSUID;
603 if (vfs_getopt(optlist, "nosymfollow", NULL, NULL) == 0)
604 fsflags |= MNT_NOSYMFOLLOW;
606 if (vfs_getopt(optlist, "noro", NULL, NULL) == 0)
607 fsflags &= ~MNT_RDONLY;
609 if (vfs_getopt(optlist, "ro", NULL, NULL) == 0)
610 fsflags |= MNT_RDONLY;
612 if (vfs_getopt(optlist, "rdonly", NULL, NULL) == 0)
613 fsflags |= MNT_RDONLY;
615 if (vfs_getopt(optlist, "rw", NULL, NULL) == 0)
616 fsflags &= ~MNT_RDONLY;
618 if (vfs_getopt(optlist, "snapshot", NULL, NULL) == 0)
619 fsflags |= MNT_SNAPSHOT;
621 if (vfs_getopt(optlist, "suiddir", NULL, NULL) == 0)
622 fsflags |= MNT_SUIDDIR;
624 if (vfs_getopt(optlist, "sync", NULL, NULL) == 0)
625 fsflags |= MNT_SYNCHRONOUS;
627 if (vfs_getopt(optlist, "union", NULL, NULL) == 0)
628 fsflags |= MNT_UNION;
631 * Be ultra-paranoid about making sure the type and fspath
632 * variables will fit in our mp buffers, including the
635 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
636 error = ENAMETOOLONG;
641 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
644 /* copyout the errmsg */
645 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
646 && errmsg_len > 0 && errmsg != NULL) {
647 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
648 strncpy(fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
650 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
653 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
654 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len,
660 vfs_freeopts(optlist);
665 * ---------------------------------------------------------------------
668 #ifndef _SYS_SYSPROTO_H_
680 struct mount_args /* {
688 struct vfsconf *vfsp = NULL;
689 struct mntarg *ma = NULL;
692 /* Kick out MNT_ROOTFS early as it is legal internally */
693 uap->flags &= ~MNT_ROOTFS;
695 if (uap->data == NULL)
698 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
699 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
701 mtx_lock(&Giant); /* XXX ? */
702 vfsp = vfs_byname_kld(fstype, td, &error);
705 free(fstype, M_TEMP);
710 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
713 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
714 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
715 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
716 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
717 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
719 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
725 * vfs_domount(): actually attempt a filesystem mount.
729 struct thread *td, /* Flags common to all filesystems. */
730 const char *fstype, /* Filesystem type. */
731 char *fspath, /* Mount path. */
732 int fsflags, /* Flags common to all filesystems. */
733 void *fsdata /* Options local to the filesystem. */
738 struct vfsconf *vfsp;
739 int error, flag = 0, kern_flag = 0;
743 mtx_assert(&Giant, MA_OWNED);
745 * Be ultra-paranoid about making sure the type and fspath
746 * variables will fit in our mp buffers, including the
749 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
750 return (ENAMETOOLONG);
752 if (jailed(td->td_ucred))
754 if (usermount == 0) {
755 if ((error = suser(td)) != 0)
760 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
762 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
763 if ((error = suser(td)) != 0)
767 * Silently enforce MNT_NOSUID and MNT_USER for
768 * unprivileged users.
771 fsflags |= MNT_NOSUID | MNT_USER;
773 /* Load KLDs before we lock the covered vnode to avoid reversals. */
775 if ((fsflags & MNT_UPDATE) == 0) {
776 /* Don't try to load KLDs if we're mounting the root. */
777 if (fsflags & MNT_ROOTFS)
778 vfsp = vfs_byname(fstype);
780 vfsp = vfs_byname_kld(fstype, td, &error);
785 * Get vnode to be covered
787 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
788 if ((error = namei(&nd)) != 0)
790 NDFREE(&nd, NDF_ONLY_PNBUF);
792 if (fsflags & MNT_UPDATE) {
793 if ((vp->v_vflag & VV_ROOT) == 0) {
799 kern_flag = mp->mnt_kern_flag;
801 * We only allow the filesystem to be reloaded if it
802 * is currently mounted read-only.
804 if ((fsflags & MNT_RELOAD) &&
805 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
807 return (EOPNOTSUPP); /* Needs translation */
810 * Only privileged root, or (if MNT_USER is set) the user that
811 * did the original mount is permitted to update it.
813 error = vfs_suser(mp, td);
818 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
823 if ((vp->v_iflag & VI_MOUNT) != 0 ||
824 vp->v_mountedhere != NULL) {
830 vp->v_iflag |= VI_MOUNT;
832 mp->mnt_flag |= fsflags &
833 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
834 VOP_UNLOCK(vp, 0, td);
835 mp->mnt_optnew = fsdata;
836 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
839 * If the user is not root, ensure that they own the directory
840 * onto which we are attempting to mount.
842 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
847 if (va.va_uid != td->td_ucred->cr_uid) {
848 if ((error = suser(td)) != 0) {
853 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
858 if (vp->v_type != VDIR) {
863 if ((vp->v_iflag & VI_MOUNT) != 0 ||
864 vp->v_mountedhere != NULL) {
869 vp->v_iflag |= VI_MOUNT;
873 * Allocate and initialize the filesystem.
875 mp = vfs_mount_alloc(vp, vfsp, fspath, td);
876 VOP_UNLOCK(vp, 0, td);
878 /* XXXMAC: pass to vfs_mount_alloc? */
879 mp->mnt_optnew = fsdata;
883 * Set the mount level flags.
885 if (fsflags & MNT_RDONLY)
886 mp->mnt_flag |= MNT_RDONLY;
887 mp->mnt_flag &=~ MNT_UPDATEMASK;
888 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
890 * Mount the filesystem.
891 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
892 * get. No freeing of cn_pnbuf.
894 error = VFS_MOUNT(mp, td);
896 if (mp->mnt_opt != NULL)
897 vfs_freeopts(mp->mnt_opt);
898 mp->mnt_opt = mp->mnt_optnew;
899 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
902 * Prevent external consumers of mount options from reading
905 mp->mnt_optnew = NULL;
906 if (mp->mnt_flag & MNT_UPDATE) {
908 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
911 mp->mnt_kern_flag = kern_flag;
913 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
914 if (mp->mnt_syncer == NULL)
915 error = vfs_allocate_syncvnode(mp);
917 if (mp->mnt_syncer != NULL)
918 vrele(mp->mnt_syncer);
919 mp->mnt_syncer = NULL;
923 vp->v_iflag &= ~VI_MOUNT;
928 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
930 * Put the new filesystem on the mount list after root.
937 vp->v_iflag &= ~VI_MOUNT;
939 vp->v_mountedhere = mp;
940 mtx_lock(&mountlist_mtx);
941 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
942 mtx_unlock(&mountlist_mtx);
943 vfs_event_signal(NULL, VQ_MOUNT, 0);
944 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
945 panic("mount: lost mount");
946 mountcheckdirs(vp, newdp);
948 VOP_UNLOCK(vp, 0, td);
949 if ((mp->mnt_flag & MNT_RDONLY) == 0)
950 error = vfs_allocate_syncvnode(mp);
956 vp->v_iflag &= ~VI_MOUNT;
958 vfs_mount_destroy(mp, td);
965 * ---------------------------------------------------------------------
966 * Unmount a filesystem.
968 * Note: unmount takes a path to the vnode mounted on as argument,
969 * not special file (as before).
971 #ifndef _SYS_SYSPROTO_H_
972 struct unmount_args {
981 register struct unmount_args /* {
990 if (jailed(td->td_ucred))
992 if (usermount == 0) {
993 if ((error = suser(td)) != 0)
997 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
998 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1000 free(pathbuf, M_TEMP);
1003 if (uap->flags & MNT_BYFSID) {
1004 /* Decode the filesystem ID. */
1005 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1006 free(pathbuf, M_TEMP);
1010 mtx_lock(&mountlist_mtx);
1011 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1012 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1013 mp->mnt_stat.f_fsid.val[1] == id1)
1016 mtx_unlock(&mountlist_mtx);
1018 mtx_lock(&mountlist_mtx);
1019 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1020 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1023 mtx_unlock(&mountlist_mtx);
1025 free(pathbuf, M_TEMP);
1028 * Previously we returned ENOENT for a nonexistent path and
1029 * EINVAL for a non-mountpoint. We cannot tell these apart
1030 * now, so in the !MNT_BYFSID case return the more likely
1031 * EINVAL for compatibility.
1033 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1037 * Only privileged root, or (if MNT_USER is set) the user that did the
1038 * original mount is permitted to unmount this filesystem.
1040 error = vfs_suser(mp, td);
1045 * Don't allow unmounting the root filesystem.
1047 if (mp->mnt_flag & MNT_ROOTFS)
1050 error = dounmount(mp, uap->flags, td);
1056 * Do the actual filesystem unmount.
1059 dounmount(mp, flags, td)
1064 struct vnode *coveredvp, *fsrootvp;
1068 mtx_assert(&Giant, MA_OWNED);
1070 if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1071 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1073 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1076 VOP_UNLOCK(coveredvp, 0, td);
1079 mp->mnt_kern_flag |= MNTK_UNMOUNT;
1080 /* Allow filesystems to detect that a forced unmount is in progress. */
1081 if (flags & MNT_FORCE)
1082 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1083 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1084 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1087 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1088 if (mp->mnt_kern_flag & MNTK_MWAIT)
1092 VOP_UNLOCK(coveredvp, 0, td);
1095 vn_start_write(NULL, &mp, V_WAIT);
1097 if (mp->mnt_flag & MNT_EXPUBLIC)
1098 vfs_setpublicfs(NULL, NULL, NULL);
1100 vfs_msync(mp, MNT_WAIT);
1101 async_flag = mp->mnt_flag & MNT_ASYNC;
1102 mp->mnt_flag &= ~MNT_ASYNC;
1103 cache_purgevfs(mp); /* remove cache entries for this file sys */
1104 if (mp->mnt_syncer != NULL)
1105 vrele(mp->mnt_syncer);
1107 * For forced unmounts, move process cdir/rdir refs on the fs root
1108 * vnode to the covered vnode. For non-forced unmounts we want
1109 * such references to cause an EBUSY error.
1111 if ((flags & MNT_FORCE) &&
1112 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1113 if (mp->mnt_vnodecovered != NULL)
1114 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1115 if (fsrootvp == rootvnode) {
1121 if (((mp->mnt_flag & MNT_RDONLY) ||
1122 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1123 (flags & MNT_FORCE)) {
1124 error = VFS_UNMOUNT(mp, flags, td);
1126 vn_finished_write(mp);
1128 /* Undo cdir/rdir and rootvnode changes made above. */
1129 if ((flags & MNT_FORCE) &&
1130 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1131 if (mp->mnt_vnodecovered != NULL)
1132 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1133 if (rootvnode == NULL) {
1134 rootvnode = fsrootvp;
1139 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1140 (void) vfs_allocate_syncvnode(mp);
1142 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1143 mp->mnt_flag |= async_flag;
1144 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1145 if (mp->mnt_kern_flag & MNTK_MWAIT)
1149 VOP_UNLOCK(coveredvp, 0, td);
1152 mtx_lock(&mountlist_mtx);
1153 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1154 mtx_unlock(&mountlist_mtx);
1155 if (coveredvp != NULL) {
1156 coveredvp->v_mountedhere = NULL;
1159 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1160 vfs_mount_destroy(mp, td);
1165 * ---------------------------------------------------------------------
1166 * Mounting of root filesystem
1170 struct root_hold_token {
1172 LIST_ENTRY(root_hold_token) list;
1175 static LIST_HEAD(, root_hold_token) root_holds =
1176 LIST_HEAD_INITIALIZER(&root_holds);
1178 struct root_hold_token *
1179 root_mount_hold(const char *identifier)
1181 struct root_hold_token *h;
1183 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1184 h->who = identifier;
1185 mtx_lock(&mountlist_mtx);
1186 LIST_INSERT_HEAD(&root_holds, h, list);
1187 mtx_unlock(&mountlist_mtx);
1192 root_mount_rel(struct root_hold_token *h)
1195 mtx_lock(&mountlist_mtx);
1196 LIST_REMOVE(h, list);
1197 wakeup(&root_holds);
1198 mtx_unlock(&mountlist_mtx);
1203 root_mount_wait(void)
1205 struct root_hold_token *h;
1211 mtx_lock(&mountlist_mtx);
1212 if (LIST_EMPTY(&root_holds)) {
1213 mtx_unlock(&mountlist_mtx);
1216 printf("Root mount waiting for:");
1217 LIST_FOREACH(h, &root_holds, list)
1218 printf(" %s", h->who);
1220 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1226 set_rootvnode(struct thread *td)
1230 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1231 panic("Cannot find root vnode");
1234 FILEDESC_LOCK(p->p_fd);
1236 if (p->p_fd->fd_cdir != NULL)
1237 vrele(p->p_fd->fd_cdir);
1238 p->p_fd->fd_cdir = rootvnode;
1241 if (p->p_fd->fd_rdir != NULL)
1242 vrele(p->p_fd->fd_rdir);
1243 p->p_fd->fd_rdir = rootvnode;
1246 FILEDESC_UNLOCK(p->p_fd);
1248 VOP_UNLOCK(rootvnode, 0, td);
1252 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1253 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1259 struct thread *td = curthread;
1260 struct vfsoptlist *opts;
1261 struct vfsconf *vfsp;
1262 struct mount *mp = NULL;
1265 vfsp = vfs_byname("devfs");
1266 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1270 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1272 error = VFS_MOUNT(mp, td);
1273 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1277 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1281 mtx_lock(&mountlist_mtx);
1282 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1283 mtx_unlock(&mountlist_mtx);
1287 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1289 printf("kern_symlink /dev -> / returns %d\n", error);
1293 * Surgically move our devfs to be mounted on /dev.
1297 devfs_fixup(struct thread *td)
1299 struct nameidata nd;
1301 struct vnode *vp, *dvp;
1304 /* Remove our devfs mount from the mountlist and purge the cache */
1305 mtx_lock(&mountlist_mtx);
1306 mp = TAILQ_FIRST(&mountlist);
1307 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1308 mtx_unlock(&mountlist_mtx);
1311 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1313 dvp->v_iflag &= ~VI_MOUNT;
1314 dvp->v_mountedhere = NULL;
1317 /* Set up the real rootvnode, and purge the cache */
1318 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1320 cache_purgevfs(rootvnode->v_mount);
1322 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1325 printf("Lookup of /dev for devfs, error: %d\n", error);
1328 NDFREE(&nd, NDF_ONLY_PNBUF);
1330 if (vp->v_type != VDIR) {
1333 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1338 mp->mnt_vnodecovered = vp;
1339 vp->v_mountedhere = mp;
1340 mtx_lock(&mountlist_mtx);
1341 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1342 mtx_unlock(&mountlist_mtx);
1343 VOP_UNLOCK(vp, 0, td);
1347 /* Unlink the no longer needed /dev/dev -> / symlink */
1348 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1352 * Report errors during filesystem mounting.
1355 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1357 struct vfsoptlist *moptlist = mp->mnt_optnew;
1362 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1363 if (error || errmsg == NULL || len <= 0)
1367 vsnprintf(errmsg, (size_t)len, fmt, ap);
1372 * Find and mount the root filesystem
1378 int error, i, asked = 0;
1385 * We are booted with instructions to prompt for the root filesystem.
1387 if (boothowto & RB_ASKNAME) {
1388 if (!vfs_mountroot_ask())
1394 * The root filesystem information is compiled in, and we are
1395 * booted with instructions to use it.
1397 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1398 if (!vfs_mountroot_try(ctrootdevname))
1400 ctrootdevname = NULL;
1404 * We've been given the generic "use CDROM as root" flag. This is
1405 * necessary because one media may be used in many different
1406 * devices, so we need to search for them.
1408 if (boothowto & RB_CDROM) {
1409 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1410 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1416 * Try to use the value read by the loader from /etc/fstab, or
1417 * supplied via some other means. This is the preferred
1420 cp = getenv("vfs.root.mountfrom");
1422 error = vfs_mountroot_try(cp);
1429 * Try values that may have been computed by code during boot
1431 if (!vfs_mountroot_try(rootdevnames[0]))
1433 if (!vfs_mountroot_try(rootdevnames[1]))
1437 * If we (still) have a compiled-in default, try it.
1439 if (ctrootdevname != NULL)
1440 if (!vfs_mountroot_try(ctrootdevname))
1443 * Everything so far has failed, prompt on the console if we haven't
1444 * already tried that.
1447 if (!vfs_mountroot_ask())
1450 panic("Root mount failed, startup aborted.");
1454 * Mount (mountfrom) as the root filesystem.
1457 vfs_mountroot_try(const char *mountfrom)
1460 char *vfsname, *path;
1470 if (mountfrom == NULL)
1471 return (error); /* don't complain */
1472 printf("Trying to mount root from %s\n", mountfrom);
1474 /* parse vfs name and path */
1475 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1476 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1477 vfsname[0] = path[0] = 0;
1478 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1479 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1482 if (path[0] == '\0')
1483 strcpy(path, ROOTNAME);
1485 error = kernel_vmount(
1486 MNT_RDONLY | MNT_ROOTFS,
1493 * We mount devfs prior to mounting the / FS, so the first
1494 * entry will typically be devfs.
1496 mp = TAILQ_FIRST(&mountlist);
1497 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1500 * Iterate over all currently mounted file systems and use
1501 * the time stamp found to check and/or initialize the RTC.
1502 * Typically devfs has no time stamp and the only other FS
1503 * is the actual / FS.
1504 * Call inittodr() only once and pass it the largest of the
1505 * timestamps we encounter.
1509 if (mp->mnt_time > timebase)
1510 timebase = mp->mnt_time;
1511 mp = TAILQ_NEXT(mp, mnt_list);
1512 } while (mp != NULL);
1515 devfs_fixup(curthread);
1518 free(path, M_MOUNT);
1519 free(vfsname, M_MOUNT);
1524 * ---------------------------------------------------------------------
1525 * Interactive root filesystem selection code.
1529 vfs_mountroot_ask(void)
1534 printf("\nManual root filesystem specification:\n");
1535 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1536 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1537 printf(" eg. ufs:da0s1a\n");
1539 printf(" eg. ufs:/dev/da0a\n");
1541 printf(" ? List valid disk boot devices\n");
1542 printf(" <empty line> Abort manual input\n");
1543 printf("\nmountroot> ");
1544 gets(name, sizeof(name), 1);
1545 if (name[0] == '\0')
1547 if (name[0] == '?') {
1548 printf("\nList of GEOM managed disk devices:\n ");
1552 if (!vfs_mountroot_try(name))
1558 * ---------------------------------------------------------------------
1559 * Functions for querying mount options/arguments from filesystems.
1563 * Check that no unknown options are given
1566 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1572 TAILQ_FOREACH(opt, opts, link) {
1574 if (p[0] == 'n' && p[1] == 'o')
1576 for(t = global_opts; *t != NULL; t++)
1581 for(t = legal; *t != NULL; t++)
1586 printf("mount option <%s> is unknown\n", p);
1593 * Get a mount option by its name.
1595 * Return 0 if the option was found, ENOENT otherwise.
1596 * If len is non-NULL it will be filled with the length
1597 * of the option. If buf is non-NULL, it will be filled
1598 * with the address of the option.
1601 vfs_getopt(opts, name, buf, len)
1602 struct vfsoptlist *opts;
1609 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1611 TAILQ_FOREACH(opt, opts, link) {
1612 if (strcmp(name, opt->name) == 0) {
1624 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1633 TAILQ_FOREACH(opt, opts, link) {
1634 if (strcmp(name, opt->name) == 0)
1642 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1647 TAILQ_FOREACH(opt, opts, link) {
1648 if (strcmp(name, opt->name) != 0)
1650 if (((char *)opt->value)[opt->len - 1] != '\0') {
1654 return (opt->value);
1660 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1664 TAILQ_FOREACH(opt, opts, link) {
1665 if (strcmp(name, opt->name) == 0) {
1677 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1683 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1685 TAILQ_FOREACH(opt, opts, link) {
1686 if (strcmp(name, opt->name) != 0)
1688 if (((char *)opt->value)[opt->len - 1] != '\0')
1691 ret = vsscanf(opt->value, fmt, ap);
1699 * Find and copy a mount option.
1701 * The size of the buffer has to be specified
1702 * in len, if it is not the same length as the
1703 * mount option, EINVAL is returned.
1704 * Returns ENOENT if the option is not found.
1707 vfs_copyopt(opts, name, dest, len)
1708 struct vfsoptlist *opts;
1715 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1717 TAILQ_FOREACH(opt, opts, link) {
1718 if (strcmp(name, opt->name) == 0) {
1719 if (len != opt->len)
1721 bcopy(opt->value, dest, opt->len);
1729 * This is a helper function for filesystems to traverse their
1730 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
1734 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1738 mtx_assert(MNT_MTX(mp), MA_OWNED);
1740 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1741 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1742 while (vp != NULL && vp->v_type == VMARKER)
1743 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1745 /* Check if we are done */
1747 __mnt_vnode_markerfree(mvp, mp);
1750 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1751 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1756 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1760 mtx_assert(MNT_MTX(mp), MA_OWNED);
1762 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1763 while (vp != NULL && vp->v_type == VMARKER)
1764 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1766 /* Check if we are done */
1773 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
1777 (*mvp)->v_type = VMARKER;
1779 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1780 while (vp != NULL && vp->v_type == VMARKER)
1781 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1783 /* Check if we are done */
1786 free(*mvp, M_VNODE_MARKER);
1790 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1791 wakeup(&mp->mnt_holdcnt);
1794 mp->mnt_markercnt++;
1795 (*mvp)->v_mount = mp;
1796 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1802 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1808 mtx_assert(MNT_MTX(mp), MA_OWNED);
1810 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1811 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1813 free(*mvp, M_VNODE_MARKER);
1817 mp->mnt_markercnt--;
1819 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1820 wakeup(&mp->mnt_holdcnt);
1825 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1829 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1830 if (sbp != &mp->mnt_stat)
1831 *sbp = mp->mnt_stat;
1836 vfs_mountedfrom(struct mount *mp, const char *from)
1839 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1840 strlcpy(mp->mnt_stat.f_mntfromname, from,
1841 sizeof mp->mnt_stat.f_mntfromname);
1845 * ---------------------------------------------------------------------
1846 * This is the api for building mount args and mounting filesystems from
1847 * inside the kernel.
1849 * The API works by accumulation of individual args. First error is
1852 * XXX: should be documented in new manpage kernel_mount(9)
1855 /* A memory allocation which must be freed when we are done */
1857 SLIST_ENTRY(mntaarg) next;
1860 /* The header for the mount arguments */
1865 SLIST_HEAD(, mntaarg) list;
1869 * Add a boolean argument.
1871 * flag is the boolean value.
1872 * name must start with "no".
1875 mount_argb(struct mntarg *ma, int flag, const char *name)
1878 KASSERT(name[0] == 'n' && name[1] == 'o',
1879 ("mount_argb(...,%s): name must start with 'no'", name));
1881 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1885 * Add an argument printf style
1888 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1891 struct mntaarg *maa;
1896 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1897 SLIST_INIT(&ma->list);
1902 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1904 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1905 ma->v[ma->len].iov_len = strlen(name) + 1;
1908 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1910 sbuf_vprintf(sb, fmt, ap);
1913 len = sbuf_len(sb) + 1;
1914 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1915 SLIST_INSERT_HEAD(&ma->list, maa, next);
1916 bcopy(sbuf_data(sb), maa + 1, len);
1919 ma->v[ma->len].iov_base = maa + 1;
1920 ma->v[ma->len].iov_len = len;
1927 * Add an argument which is a userland string.
1930 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1932 struct mntaarg *maa;
1938 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1939 SLIST_INIT(&ma->list);
1943 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1944 SLIST_INSERT_HEAD(&ma->list, maa, next);
1945 tbuf = (void *)(maa + 1);
1946 ma->error = copyinstr(val, tbuf, len, NULL);
1947 return (mount_arg(ma, name, tbuf, -1));
1953 * If length is -1, use printf.
1956 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1960 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1961 SLIST_INIT(&ma->list);
1966 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1968 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1969 ma->v[ma->len].iov_len = strlen(name) + 1;
1972 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1974 ma->v[ma->len].iov_len = strlen(val) + 1;
1976 ma->v[ma->len].iov_len = len;
1982 * Free a mntarg structure
1985 free_mntarg(struct mntarg *ma)
1987 struct mntaarg *maa;
1989 while (!SLIST_EMPTY(&ma->list)) {
1990 maa = SLIST_FIRST(&ma->list);
1991 SLIST_REMOVE_HEAD(&ma->list, next);
1994 free(ma->v, M_MOUNT);
1999 * Mount a filesystem
2002 kernel_mount(struct mntarg *ma, int flags)
2007 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2008 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2009 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2011 auio.uio_iov = ma->v;
2012 auio.uio_iovcnt = ma->len;
2013 auio.uio_segflg = UIO_SYSSPACE;
2017 error = vfs_donmount(curthread, flags, &auio);
2023 * A printflike function to mount a filesystem.
2026 kernel_vmount(int flags, ...)
2028 struct mntarg *ma = NULL;
2034 va_start(ap, flags);
2036 cp = va_arg(ap, const char *);
2039 vp = va_arg(ap, const void *);
2040 ma = mount_arg(ma, cp, vp, -1);
2044 error = kernel_mount(ma, flags);