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");
501 if (mp->mnt_secondary_writes > 0) {
502 printf("Waiting for mount point secondary write ops\n");
503 while (mp->mnt_secondary_writes > 0) {
504 mp->mnt_kern_flag |= MNTK_SUSPEND;
505 msleep(&mp->mnt_secondary_writes,
507 PZERO, "mntdestroy3", 0);
509 printf("mount point secondary write ops completed\n");
512 mp->mnt_vfc->vfc_refcount--;
513 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
514 panic("unmount: dangling vnode");
515 lockdestroy(&mp->mnt_lock);
517 if (mp->mnt_kern_flag & MNTK_MWAIT)
519 if (mp->mnt_writeopcount != 0)
520 panic("vfs_mount_destroy: nonzero writeopcount");
521 if (mp->mnt_secondary_writes != 0)
522 panic("vfs_mount_destroy: nonzero secondary_writes");
523 if (mp->mnt_nvnodelistsize != 0)
524 panic("vfs_mount_destroy: nonzero nvnodelistsize");
525 mp->mnt_writeopcount = -1000;
526 mp->mnt_nvnodelistsize = -1000;
527 mp->mnt_secondary_writes = -1000;
529 mtx_destroy(&mp->mnt_mtx);
531 mac_destroy_mount(mp);
533 if (mp->mnt_opt != NULL)
534 vfs_freeopts(mp->mnt_opt);
535 crfree(mp->mnt_cred);
540 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
542 struct vfsoptlist *optlist;
544 char *fstype, *fspath, *errmsg;
545 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
550 error = vfs_buildopts(fsoptions, &optlist);
554 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
555 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
560 * We need these two options before the others,
561 * and they are mandatory for any filesystem.
562 * Ensure they are NUL terminated as well.
565 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
566 if (error || fstype[fstypelen - 1] != '\0') {
569 strncpy(errmsg, "Invalid fstype", errmsg_len);
573 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
574 if (error || fspath[fspathlen - 1] != '\0') {
577 strncpy(errmsg, "Invalid fspath", errmsg_len);
582 * We need to see if we have the "update" option
583 * before we call vfs_domount(), since vfs_domount() has special
584 * logic based on MNT_UPDATE. This is very important
585 * when we want to update the root filesystem.
587 TAILQ_FOREACH(opt, optlist, link) {
588 if (strcmp(opt->name, "update") == 0)
589 fsflags |= MNT_UPDATE;
590 else if (strcmp(opt->name, "async") == 0)
591 fsflags |= MNT_ASYNC;
592 else if (strcmp(opt->name, "force") == 0)
593 fsflags |= MNT_FORCE;
594 else if (strcmp(opt->name, "multilabel") == 0)
595 fsflags |= MNT_MULTILABEL;
596 else if (strcmp(opt->name, "noasync") == 0)
597 fsflags &= ~MNT_ASYNC;
598 else if (strcmp(opt->name, "noatime") == 0)
599 fsflags |= MNT_NOATIME;
600 else if (strcmp(opt->name, "noclusterr") == 0)
601 fsflags |= MNT_NOCLUSTERR;
602 else if (strcmp(opt->name, "noclusterw") == 0)
603 fsflags |= MNT_NOCLUSTERW;
604 else if (strcmp(opt->name, "noexec") == 0)
605 fsflags |= MNT_NOEXEC;
606 else if (strcmp(opt->name, "nosuid") == 0)
607 fsflags |= MNT_NOSUID;
608 else if (strcmp(opt->name, "nosymfollow") == 0)
609 fsflags |= MNT_NOSYMFOLLOW;
610 else if (strcmp(opt->name, "noro") == 0 ||
611 strcmp(opt->name, "rw") == 0)
612 fsflags &= ~MNT_RDONLY;
613 else if (strcmp(opt->name, "ro") == 0 ||
614 strcmp(opt->name, "rdonly") == 0)
615 fsflags |= MNT_RDONLY;
616 else if (strcmp(opt->name, "snapshot") == 0)
617 fsflags |= MNT_SNAPSHOT;
618 else if (strcmp(opt->name, "suiddir") == 0)
619 fsflags |= MNT_SUIDDIR;
620 else if (strcmp(opt->name, "sync") == 0)
621 fsflags |= MNT_SYNCHRONOUS;
622 else if (strcmp(opt->name, "union") == 0)
623 fsflags |= MNT_UNION;
627 * Be ultra-paranoid about making sure the type and fspath
628 * variables will fit in our mp buffers, including the
631 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
632 error = ENAMETOOLONG;
637 error = vfs_domount(td, fstype, fspath, fsflags, optlist);
640 /* copyout the errmsg */
641 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
642 && errmsg_len > 0 && errmsg != NULL) {
643 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
644 strncpy(fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
646 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
649 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
650 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len,
656 vfs_freeopts(optlist);
661 * ---------------------------------------------------------------------
664 #ifndef _SYS_SYSPROTO_H_
676 struct mount_args /* {
684 struct vfsconf *vfsp = NULL;
685 struct mntarg *ma = NULL;
688 /* Kick out MNT_ROOTFS early as it is legal internally */
689 uap->flags &= ~MNT_ROOTFS;
691 if (uap->data == NULL)
694 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
695 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
697 mtx_lock(&Giant); /* XXX ? */
698 vfsp = vfs_byname_kld(fstype, td, &error);
701 free(fstype, M_TEMP);
706 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
709 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
710 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
711 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
712 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
713 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
715 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
721 * vfs_domount(): actually attempt a filesystem mount.
725 struct thread *td, /* Flags common to all filesystems. */
726 const char *fstype, /* Filesystem type. */
727 char *fspath, /* Mount path. */
728 int fsflags, /* Flags common to all filesystems. */
729 void *fsdata /* Options local to the filesystem. */
734 struct vfsconf *vfsp;
735 int error, flag = 0, kern_flag = 0;
739 mtx_assert(&Giant, MA_OWNED);
741 * Be ultra-paranoid about making sure the type and fspath
742 * variables will fit in our mp buffers, including the
745 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
746 return (ENAMETOOLONG);
748 if (jailed(td->td_ucred))
750 if (usermount == 0) {
751 if ((error = suser(td)) != 0)
756 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
758 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
759 if ((error = suser(td)) != 0)
763 * Silently enforce MNT_NOSUID and MNT_USER for
764 * unprivileged users.
767 fsflags |= MNT_NOSUID | MNT_USER;
769 /* Load KLDs before we lock the covered vnode to avoid reversals. */
771 if ((fsflags & MNT_UPDATE) == 0) {
772 /* Don't try to load KLDs if we're mounting the root. */
773 if (fsflags & MNT_ROOTFS)
774 vfsp = vfs_byname(fstype);
776 vfsp = vfs_byname_kld(fstype, td, &error);
781 * Get vnode to be covered
783 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
784 if ((error = namei(&nd)) != 0)
786 NDFREE(&nd, NDF_ONLY_PNBUF);
788 if (fsflags & MNT_UPDATE) {
789 if ((vp->v_vflag & VV_ROOT) == 0) {
795 kern_flag = mp->mnt_kern_flag;
797 * We only allow the filesystem to be reloaded if it
798 * is currently mounted read-only.
800 if ((fsflags & MNT_RELOAD) &&
801 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
803 return (EOPNOTSUPP); /* Needs translation */
806 * Only privileged root, or (if MNT_USER is set) the user that
807 * did the original mount is permitted to update it.
809 error = vfs_suser(mp, td);
814 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
819 if ((vp->v_iflag & VI_MOUNT) != 0 ||
820 vp->v_mountedhere != NULL) {
826 vp->v_iflag |= VI_MOUNT;
828 mp->mnt_flag |= fsflags &
829 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
830 VOP_UNLOCK(vp, 0, td);
831 mp->mnt_optnew = fsdata;
832 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
835 * If the user is not root, ensure that they own the directory
836 * onto which we are attempting to mount.
838 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
843 if (va.va_uid != td->td_ucred->cr_uid) {
844 if ((error = suser(td)) != 0) {
849 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
854 if (vp->v_type != VDIR) {
859 if ((vp->v_iflag & VI_MOUNT) != 0 ||
860 vp->v_mountedhere != NULL) {
865 vp->v_iflag |= VI_MOUNT;
869 * Allocate and initialize the filesystem.
871 mp = vfs_mount_alloc(vp, vfsp, fspath, td);
872 VOP_UNLOCK(vp, 0, td);
874 /* XXXMAC: pass to vfs_mount_alloc? */
875 mp->mnt_optnew = fsdata;
879 * Set the mount level flags.
881 if (fsflags & MNT_RDONLY)
882 mp->mnt_flag |= MNT_RDONLY;
883 mp->mnt_flag &=~ MNT_UPDATEMASK;
884 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
886 * Mount the filesystem.
887 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
888 * get. No freeing of cn_pnbuf.
890 error = VFS_MOUNT(mp, td);
892 if (mp->mnt_opt != NULL)
893 vfs_freeopts(mp->mnt_opt);
894 mp->mnt_opt = mp->mnt_optnew;
895 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
898 * Prevent external consumers of mount options from reading
901 mp->mnt_optnew = NULL;
902 if (mp->mnt_flag & MNT_UPDATE) {
904 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
907 mp->mnt_kern_flag = kern_flag;
909 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
910 if (mp->mnt_syncer == NULL)
911 error = vfs_allocate_syncvnode(mp);
913 if (mp->mnt_syncer != NULL)
914 vrele(mp->mnt_syncer);
915 mp->mnt_syncer = NULL;
919 vp->v_iflag &= ~VI_MOUNT;
924 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
926 * Put the new filesystem on the mount list after root.
933 vp->v_iflag &= ~VI_MOUNT;
935 vp->v_mountedhere = mp;
936 mtx_lock(&mountlist_mtx);
937 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
938 mtx_unlock(&mountlist_mtx);
939 vfs_event_signal(NULL, VQ_MOUNT, 0);
940 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
941 panic("mount: lost mount");
942 mountcheckdirs(vp, newdp);
944 VOP_UNLOCK(vp, 0, td);
945 if ((mp->mnt_flag & MNT_RDONLY) == 0)
946 error = vfs_allocate_syncvnode(mp);
952 vp->v_iflag &= ~VI_MOUNT;
954 vfs_mount_destroy(mp, td);
961 * ---------------------------------------------------------------------
962 * Unmount a filesystem.
964 * Note: unmount takes a path to the vnode mounted on as argument,
965 * not special file (as before).
967 #ifndef _SYS_SYSPROTO_H_
968 struct unmount_args {
977 register struct unmount_args /* {
986 if (jailed(td->td_ucred))
988 if (usermount == 0) {
989 if ((error = suser(td)) != 0)
993 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
994 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
996 free(pathbuf, M_TEMP);
999 if (uap->flags & MNT_BYFSID) {
1000 /* Decode the filesystem ID. */
1001 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1002 free(pathbuf, M_TEMP);
1006 mtx_lock(&mountlist_mtx);
1007 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1008 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1009 mp->mnt_stat.f_fsid.val[1] == id1)
1012 mtx_unlock(&mountlist_mtx);
1014 mtx_lock(&mountlist_mtx);
1015 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1016 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1019 mtx_unlock(&mountlist_mtx);
1021 free(pathbuf, M_TEMP);
1024 * Previously we returned ENOENT for a nonexistent path and
1025 * EINVAL for a non-mountpoint. We cannot tell these apart
1026 * now, so in the !MNT_BYFSID case return the more likely
1027 * EINVAL for compatibility.
1029 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1033 * Only privileged root, or (if MNT_USER is set) the user that did the
1034 * original mount is permitted to unmount this filesystem.
1036 error = vfs_suser(mp, td);
1041 * Don't allow unmounting the root filesystem.
1043 if (mp->mnt_flag & MNT_ROOTFS)
1046 error = dounmount(mp, uap->flags, td);
1052 * Do the actual filesystem unmount.
1055 dounmount(mp, flags, td)
1060 struct vnode *coveredvp, *fsrootvp;
1064 mtx_assert(&Giant, MA_OWNED);
1066 if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1067 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1069 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1072 VOP_UNLOCK(coveredvp, 0, td);
1075 mp->mnt_kern_flag |= MNTK_UNMOUNT;
1076 /* Allow filesystems to detect that a forced unmount is in progress. */
1077 if (flags & MNT_FORCE)
1078 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1079 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1080 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1083 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1084 if (mp->mnt_kern_flag & MNTK_MWAIT)
1088 VOP_UNLOCK(coveredvp, 0, td);
1091 vn_start_write(NULL, &mp, V_WAIT);
1093 if (mp->mnt_flag & MNT_EXPUBLIC)
1094 vfs_setpublicfs(NULL, NULL, NULL);
1096 vfs_msync(mp, MNT_WAIT);
1097 async_flag = mp->mnt_flag & MNT_ASYNC;
1098 mp->mnt_flag &= ~MNT_ASYNC;
1099 cache_purgevfs(mp); /* remove cache entries for this file sys */
1100 if (mp->mnt_syncer != NULL)
1101 vrele(mp->mnt_syncer);
1103 * For forced unmounts, move process cdir/rdir refs on the fs root
1104 * vnode to the covered vnode. For non-forced unmounts we want
1105 * such references to cause an EBUSY error.
1107 if ((flags & MNT_FORCE) &&
1108 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1109 if (mp->mnt_vnodecovered != NULL)
1110 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1111 if (fsrootvp == rootvnode) {
1117 if (((mp->mnt_flag & MNT_RDONLY) ||
1118 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1119 (flags & MNT_FORCE)) {
1120 error = VFS_UNMOUNT(mp, flags, td);
1122 vn_finished_write(mp);
1124 /* Undo cdir/rdir and rootvnode changes made above. */
1125 if ((flags & MNT_FORCE) &&
1126 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1127 if (mp->mnt_vnodecovered != NULL)
1128 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1129 if (rootvnode == NULL) {
1130 rootvnode = fsrootvp;
1135 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1136 (void) vfs_allocate_syncvnode(mp);
1138 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1139 mp->mnt_flag |= async_flag;
1140 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1141 if (mp->mnt_kern_flag & MNTK_MWAIT)
1145 VOP_UNLOCK(coveredvp, 0, td);
1148 mtx_lock(&mountlist_mtx);
1149 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1150 mtx_unlock(&mountlist_mtx);
1151 if (coveredvp != NULL) {
1152 coveredvp->v_mountedhere = NULL;
1155 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1156 vfs_mount_destroy(mp, td);
1161 * ---------------------------------------------------------------------
1162 * Mounting of root filesystem
1166 struct root_hold_token {
1168 LIST_ENTRY(root_hold_token) list;
1171 static LIST_HEAD(, root_hold_token) root_holds =
1172 LIST_HEAD_INITIALIZER(&root_holds);
1174 struct root_hold_token *
1175 root_mount_hold(const char *identifier)
1177 struct root_hold_token *h;
1179 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1180 h->who = identifier;
1181 mtx_lock(&mountlist_mtx);
1182 LIST_INSERT_HEAD(&root_holds, h, list);
1183 mtx_unlock(&mountlist_mtx);
1188 root_mount_rel(struct root_hold_token *h)
1191 mtx_lock(&mountlist_mtx);
1192 LIST_REMOVE(h, list);
1193 wakeup(&root_holds);
1194 mtx_unlock(&mountlist_mtx);
1199 root_mount_wait(void)
1201 struct root_hold_token *h;
1207 mtx_lock(&mountlist_mtx);
1208 if (LIST_EMPTY(&root_holds)) {
1209 mtx_unlock(&mountlist_mtx);
1212 printf("Root mount waiting for:");
1213 LIST_FOREACH(h, &root_holds, list)
1214 printf(" %s", h->who);
1216 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1222 set_rootvnode(struct thread *td)
1226 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1227 panic("Cannot find root vnode");
1230 FILEDESC_LOCK(p->p_fd);
1232 if (p->p_fd->fd_cdir != NULL)
1233 vrele(p->p_fd->fd_cdir);
1234 p->p_fd->fd_cdir = rootvnode;
1237 if (p->p_fd->fd_rdir != NULL)
1238 vrele(p->p_fd->fd_rdir);
1239 p->p_fd->fd_rdir = rootvnode;
1242 FILEDESC_UNLOCK(p->p_fd);
1244 VOP_UNLOCK(rootvnode, 0, td);
1248 * Mount /devfs as our root filesystem, but do not put it on the mountlist
1249 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup.
1255 struct thread *td = curthread;
1256 struct vfsoptlist *opts;
1257 struct vfsconf *vfsp;
1258 struct mount *mp = NULL;
1261 vfsp = vfs_byname("devfs");
1262 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1266 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1268 error = VFS_MOUNT(mp, td);
1269 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1273 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1277 mtx_lock(&mountlist_mtx);
1278 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1279 mtx_unlock(&mountlist_mtx);
1283 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1285 printf("kern_symlink /dev -> / returns %d\n", error);
1289 * Surgically move our devfs to be mounted on /dev.
1293 devfs_fixup(struct thread *td)
1295 struct nameidata nd;
1297 struct vnode *vp, *dvp;
1300 /* Remove our devfs mount from the mountlist and purge the cache */
1301 mtx_lock(&mountlist_mtx);
1302 mp = TAILQ_FIRST(&mountlist);
1303 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1304 mtx_unlock(&mountlist_mtx);
1307 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1309 dvp->v_iflag &= ~VI_MOUNT;
1310 dvp->v_mountedhere = NULL;
1313 /* Set up the real rootvnode, and purge the cache */
1314 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1316 cache_purgevfs(rootvnode->v_mount);
1318 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1321 printf("Lookup of /dev for devfs, error: %d\n", error);
1324 NDFREE(&nd, NDF_ONLY_PNBUF);
1326 if (vp->v_type != VDIR) {
1329 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1334 mp->mnt_vnodecovered = vp;
1335 vp->v_mountedhere = mp;
1336 mtx_lock(&mountlist_mtx);
1337 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1338 mtx_unlock(&mountlist_mtx);
1339 VOP_UNLOCK(vp, 0, td);
1343 /* Unlink the no longer needed /dev/dev -> / symlink */
1344 kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1348 * Report errors during filesystem mounting.
1351 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1353 struct vfsoptlist *moptlist = mp->mnt_optnew;
1358 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1359 if (error || errmsg == NULL || len <= 0)
1363 vsnprintf(errmsg, (size_t)len, fmt, ap);
1368 * Find and mount the root filesystem
1374 int error, i, asked = 0;
1381 * We are booted with instructions to prompt for the root filesystem.
1383 if (boothowto & RB_ASKNAME) {
1384 if (!vfs_mountroot_ask())
1390 * The root filesystem information is compiled in, and we are
1391 * booted with instructions to use it.
1393 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1394 if (!vfs_mountroot_try(ctrootdevname))
1396 ctrootdevname = NULL;
1400 * We've been given the generic "use CDROM as root" flag. This is
1401 * necessary because one media may be used in many different
1402 * devices, so we need to search for them.
1404 if (boothowto & RB_CDROM) {
1405 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1406 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1412 * Try to use the value read by the loader from /etc/fstab, or
1413 * supplied via some other means. This is the preferred
1416 cp = getenv("vfs.root.mountfrom");
1418 error = vfs_mountroot_try(cp);
1425 * Try values that may have been computed by code during boot
1427 if (!vfs_mountroot_try(rootdevnames[0]))
1429 if (!vfs_mountroot_try(rootdevnames[1]))
1433 * If we (still) have a compiled-in default, try it.
1435 if (ctrootdevname != NULL)
1436 if (!vfs_mountroot_try(ctrootdevname))
1439 * Everything so far has failed, prompt on the console if we haven't
1440 * already tried that.
1443 if (!vfs_mountroot_ask())
1446 panic("Root mount failed, startup aborted.");
1450 * Mount (mountfrom) as the root filesystem.
1453 vfs_mountroot_try(const char *mountfrom)
1456 char *vfsname, *path;
1466 if (mountfrom == NULL)
1467 return (error); /* don't complain */
1468 printf("Trying to mount root from %s\n", mountfrom);
1470 /* parse vfs name and path */
1471 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1472 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1473 vfsname[0] = path[0] = 0;
1474 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1475 if (sscanf(mountfrom, patt, vfsname, path) < 1)
1478 if (path[0] == '\0')
1479 strcpy(path, ROOTNAME);
1481 error = kernel_vmount(
1482 MNT_RDONLY | MNT_ROOTFS,
1489 * We mount devfs prior to mounting the / FS, so the first
1490 * entry will typically be devfs.
1492 mp = TAILQ_FIRST(&mountlist);
1493 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1496 * Iterate over all currently mounted file systems and use
1497 * the time stamp found to check and/or initialize the RTC.
1498 * Typically devfs has no time stamp and the only other FS
1499 * is the actual / FS.
1500 * Call inittodr() only once and pass it the largest of the
1501 * timestamps we encounter.
1505 if (mp->mnt_time > timebase)
1506 timebase = mp->mnt_time;
1507 mp = TAILQ_NEXT(mp, mnt_list);
1508 } while (mp != NULL);
1511 devfs_fixup(curthread);
1514 free(path, M_MOUNT);
1515 free(vfsname, M_MOUNT);
1520 * ---------------------------------------------------------------------
1521 * Interactive root filesystem selection code.
1525 vfs_mountroot_ask(void)
1530 printf("\nManual root filesystem specification:\n");
1531 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1532 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1533 printf(" eg. ufs:da0s1a\n");
1535 printf(" eg. ufs:/dev/da0a\n");
1537 printf(" ? List valid disk boot devices\n");
1538 printf(" <empty line> Abort manual input\n");
1539 printf("\nmountroot> ");
1540 gets(name, sizeof(name), 1);
1541 if (name[0] == '\0')
1543 if (name[0] == '?') {
1544 printf("\nList of GEOM managed disk devices:\n ");
1548 if (!vfs_mountroot_try(name))
1554 * ---------------------------------------------------------------------
1555 * Functions for querying mount options/arguments from filesystems.
1559 * Check that no unknown options are given
1562 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1568 TAILQ_FOREACH(opt, opts, link) {
1570 if (p[0] == 'n' && p[1] == 'o')
1572 for(t = global_opts; *t != NULL; t++)
1577 for(t = legal; *t != NULL; t++)
1582 printf("mount option <%s> is unknown\n", p);
1589 * Get a mount option by its name.
1591 * Return 0 if the option was found, ENOENT otherwise.
1592 * If len is non-NULL it will be filled with the length
1593 * of the option. If buf is non-NULL, it will be filled
1594 * with the address of the option.
1597 vfs_getopt(opts, name, buf, len)
1598 struct vfsoptlist *opts;
1605 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1607 TAILQ_FOREACH(opt, opts, link) {
1608 if (strcmp(name, opt->name) == 0) {
1620 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1629 TAILQ_FOREACH(opt, opts, link) {
1630 if (strcmp(name, opt->name) == 0)
1638 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1643 TAILQ_FOREACH(opt, opts, link) {
1644 if (strcmp(name, opt->name) != 0)
1646 if (((char *)opt->value)[opt->len - 1] != '\0') {
1650 return (opt->value);
1656 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1660 TAILQ_FOREACH(opt, opts, link) {
1661 if (strcmp(name, opt->name) == 0) {
1673 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1679 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1681 TAILQ_FOREACH(opt, opts, link) {
1682 if (strcmp(name, opt->name) != 0)
1684 if (((char *)opt->value)[opt->len - 1] != '\0')
1687 ret = vsscanf(opt->value, fmt, ap);
1695 * Find and copy a mount option.
1697 * The size of the buffer has to be specified
1698 * in len, if it is not the same length as the
1699 * mount option, EINVAL is returned.
1700 * Returns ENOENT if the option is not found.
1703 vfs_copyopt(opts, name, dest, len)
1704 struct vfsoptlist *opts;
1711 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1713 TAILQ_FOREACH(opt, opts, link) {
1714 if (strcmp(name, opt->name) == 0) {
1715 if (len != opt->len)
1717 bcopy(opt->value, dest, opt->len);
1725 * This is a helper function for filesystems to traverse their
1726 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h
1730 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1734 mtx_assert(MNT_MTX(mp), MA_OWNED);
1736 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1737 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1738 while (vp != NULL && vp->v_type == VMARKER)
1739 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1741 /* Check if we are done */
1743 __mnt_vnode_markerfree(mvp, mp);
1746 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1747 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1752 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1756 mtx_assert(MNT_MTX(mp), MA_OWNED);
1758 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1759 while (vp != NULL && vp->v_type == VMARKER)
1760 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1762 /* Check if we are done */
1769 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
1773 (*mvp)->v_type = VMARKER;
1775 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1776 while (vp != NULL && vp->v_type == VMARKER)
1777 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1779 /* Check if we are done */
1782 free(*mvp, M_VNODE_MARKER);
1786 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1787 wakeup(&mp->mnt_holdcnt);
1790 mp->mnt_markercnt++;
1791 (*mvp)->v_mount = mp;
1792 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1798 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1804 mtx_assert(MNT_MTX(mp), MA_OWNED);
1806 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1807 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1809 free(*mvp, M_VNODE_MARKER);
1813 mp->mnt_markercnt--;
1815 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1816 wakeup(&mp->mnt_holdcnt);
1821 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1825 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1826 if (sbp != &mp->mnt_stat)
1827 *sbp = mp->mnt_stat;
1832 vfs_mountedfrom(struct mount *mp, const char *from)
1835 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1836 strlcpy(mp->mnt_stat.f_mntfromname, from,
1837 sizeof mp->mnt_stat.f_mntfromname);
1841 * ---------------------------------------------------------------------
1842 * This is the api for building mount args and mounting filesystems from
1843 * inside the kernel.
1845 * The API works by accumulation of individual args. First error is
1848 * XXX: should be documented in new manpage kernel_mount(9)
1851 /* A memory allocation which must be freed when we are done */
1853 SLIST_ENTRY(mntaarg) next;
1856 /* The header for the mount arguments */
1861 SLIST_HEAD(, mntaarg) list;
1865 * Add a boolean argument.
1867 * flag is the boolean value.
1868 * name must start with "no".
1871 mount_argb(struct mntarg *ma, int flag, const char *name)
1874 KASSERT(name[0] == 'n' && name[1] == 'o',
1875 ("mount_argb(...,%s): name must start with 'no'", name));
1877 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1881 * Add an argument printf style
1884 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1887 struct mntaarg *maa;
1892 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1893 SLIST_INIT(&ma->list);
1898 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1900 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1901 ma->v[ma->len].iov_len = strlen(name) + 1;
1904 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1906 sbuf_vprintf(sb, fmt, ap);
1909 len = sbuf_len(sb) + 1;
1910 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1911 SLIST_INSERT_HEAD(&ma->list, maa, next);
1912 bcopy(sbuf_data(sb), maa + 1, len);
1915 ma->v[ma->len].iov_base = maa + 1;
1916 ma->v[ma->len].iov_len = len;
1923 * Add an argument which is a userland string.
1926 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1928 struct mntaarg *maa;
1934 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1935 SLIST_INIT(&ma->list);
1939 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1940 SLIST_INSERT_HEAD(&ma->list, maa, next);
1941 tbuf = (void *)(maa + 1);
1942 ma->error = copyinstr(val, tbuf, len, NULL);
1943 return (mount_arg(ma, name, tbuf, -1));
1949 * If length is -1, use printf.
1952 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1956 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1957 SLIST_INIT(&ma->list);
1962 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1964 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1965 ma->v[ma->len].iov_len = strlen(name) + 1;
1968 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1970 ma->v[ma->len].iov_len = strlen(val) + 1;
1972 ma->v[ma->len].iov_len = len;
1978 * Free a mntarg structure
1981 free_mntarg(struct mntarg *ma)
1983 struct mntaarg *maa;
1985 while (!SLIST_EMPTY(&ma->list)) {
1986 maa = SLIST_FIRST(&ma->list);
1987 SLIST_REMOVE_HEAD(&ma->list, next);
1990 free(ma->v, M_MOUNT);
1995 * Mount a filesystem
1998 kernel_mount(struct mntarg *ma, int flags)
2003 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2004 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2005 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2007 auio.uio_iov = ma->v;
2008 auio.uio_iovcnt = ma->len;
2009 auio.uio_segflg = UIO_SYSSPACE;
2013 error = vfs_donmount(curthread, flags, &auio);
2019 * A printflike function to mount a filesystem.
2022 kernel_vmount(int flags, ...)
2024 struct mntarg *ma = NULL;
2030 va_start(ap, flags);
2032 cp = va_arg(ap, const char *);
2035 vp = va_arg(ap, const void *);
2036 ma = mount_arg(ma, cp, vp, -1);
2040 error = kernel_mount(ma, flags);