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 "opt_vfs_allow_nonmpsafe.h"
42 #include <sys/param.h>
44 #include <sys/fcntl.h>
46 #include <sys/kernel.h>
47 #include <sys/libkern.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/reboot.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysproto.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/systm.h>
63 #include <sys/vnode.h>
66 #include <geom/geom.h>
68 #include <machine/stdarg.h>
70 #include <security/audit/audit.h>
71 #include <security/mac/mac_framework.h>
73 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
75 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
76 uint64_t fsflags, struct vfsoptlist **optlist);
77 static void free_mntarg(struct mntarg *ma);
79 static int usermount = 0;
80 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
81 "Unprivileged users may mount and unmount file systems");
83 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
84 static uma_zone_t mount_zone;
86 /* List of mounted filesystems. */
87 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
89 /* For any iteration/modification of mountlist */
90 struct mtx mountlist_mtx;
91 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
94 * Global opts, taken by all filesystems
96 static const char *global_opts[] = {
108 mount_init(void *mem, int size, int flags)
112 mp = (struct mount *)mem;
113 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
114 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
119 mount_fini(void *mem, int size)
123 mp = (struct mount *)mem;
124 lockdestroy(&mp->mnt_explock);
125 mtx_destroy(&mp->mnt_mtx);
129 vfs_mount_init(void *dummy __unused)
132 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
133 NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
135 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
138 * ---------------------------------------------------------------------
139 * Functions for building and sanitizing the mount options
142 /* Remove one mount option. */
144 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
147 TAILQ_REMOVE(opts, opt, link);
148 free(opt->name, M_MOUNT);
149 if (opt->value != NULL)
150 free(opt->value, M_MOUNT);
154 /* Release all resources related to the mount options. */
156 vfs_freeopts(struct vfsoptlist *opts)
160 while (!TAILQ_EMPTY(opts)) {
161 opt = TAILQ_FIRST(opts);
162 vfs_freeopt(opts, opt);
168 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
170 struct vfsopt *opt, *temp;
174 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
175 if (strcmp(opt->name, name) == 0)
176 vfs_freeopt(opts, opt);
181 vfs_isopt_ro(const char *opt)
184 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
185 strcmp(opt, "norw") == 0)
191 vfs_isopt_rw(const char *opt)
194 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
200 * Check if options are equal (with or without the "no" prefix).
203 vfs_equalopts(const char *opt1, const char *opt2)
207 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
208 if (strcmp(opt1, opt2) == 0)
210 /* "noopt" vs. "opt" */
211 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
213 /* "opt" vs. "noopt" */
214 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
216 while ((p = strchr(opt1, '.')) != NULL &&
217 !strncmp(opt1, opt2, ++p - opt1)) {
220 /* "foo.noopt" vs. "foo.opt" */
221 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
223 /* "foo.opt" vs. "foo.noopt" */
224 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
227 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
228 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
229 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
235 * If a mount option is specified several times,
236 * (with or without the "no" prefix) only keep
237 * the last occurence of it.
240 vfs_sanitizeopts(struct vfsoptlist *opts)
242 struct vfsopt *opt, *opt2, *tmp;
244 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
245 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
246 while (opt2 != NULL) {
247 if (vfs_equalopts(opt->name, opt2->name)) {
248 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
249 vfs_freeopt(opts, opt2);
252 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
259 * Build a linked list of mount options from a struct uio.
262 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
264 struct vfsoptlist *opts;
266 size_t memused, namelen, optlen;
267 unsigned int i, iovcnt;
270 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
273 iovcnt = auio->uio_iovcnt;
274 for (i = 0; i < iovcnt; i += 2) {
275 namelen = auio->uio_iov[i].iov_len;
276 optlen = auio->uio_iov[i + 1].iov_len;
277 memused += sizeof(struct vfsopt) + optlen + namelen;
279 * Avoid consuming too much memory, and attempts to overflow
282 if (memused > VFS_MOUNTARG_SIZE_MAX ||
283 optlen > VFS_MOUNTARG_SIZE_MAX ||
284 namelen > VFS_MOUNTARG_SIZE_MAX) {
289 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
290 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
297 * Do this early, so jumps to "bad" will free the current
300 TAILQ_INSERT_TAIL(opts, opt, link);
302 if (auio->uio_segflg == UIO_SYSSPACE) {
303 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
305 error = copyin(auio->uio_iov[i].iov_base, opt->name,
310 /* Ensure names are null-terminated strings. */
311 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
317 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
318 if (auio->uio_segflg == UIO_SYSSPACE) {
319 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
322 error = copyin(auio->uio_iov[i + 1].iov_base,
329 vfs_sanitizeopts(opts);
338 * Merge the old mount options with the new ones passed
339 * in the MNT_UPDATE case.
341 * XXX: This function will keep a "nofoo" option in the new
342 * options. E.g, if the option's canonical name is "foo",
343 * "nofoo" ends up in the mount point's active options.
346 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
348 struct vfsopt *opt, *new;
350 TAILQ_FOREACH(opt, oldopts, link) {
351 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
352 new->name = strdup(opt->name, M_MOUNT);
354 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
355 bcopy(opt->value, new->value, opt->len);
359 new->seen = opt->seen;
360 TAILQ_INSERT_HEAD(toopts, new, link);
362 vfs_sanitizeopts(toopts);
366 * Mount a filesystem.
371 struct nmount_args /* {
383 * Mount flags are now 64-bits. On 32-bit archtectures only
384 * 32-bits are passed in, but from here on everything handles
385 * 64-bit flags correctly.
389 AUDIT_ARG_FFLAGS(flags);
390 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
391 uap->iovp, uap->iovcnt, flags);
394 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
395 * userspace to set this flag, but we must filter it out if we want
396 * MNT_UPDATE on the root file system to work.
397 * MNT_ROOTFS should only be set by the kernel when mounting its
400 flags &= ~MNT_ROOTFS;
402 iovcnt = uap->iovcnt;
404 * Check that we have an even number of iovec's
405 * and that we have at least two options.
407 if ((iovcnt & 1) || (iovcnt < 4)) {
408 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
413 error = copyinuio(uap->iovp, iovcnt, &auio);
415 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
419 error = vfs_donmount(td, flags, auio);
426 * ---------------------------------------------------------------------
427 * Various utility functions
431 vfs_ref(struct mount *mp)
434 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
441 vfs_rel(struct mount *mp)
444 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
451 * Allocate and initialize the mount point struct.
454 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
459 mp = uma_zalloc(mount_zone, M_WAITOK);
460 bzero(&mp->mnt_startzero,
461 __rangeof(struct mount, mnt_startzero, mnt_endzero));
462 TAILQ_INIT(&mp->mnt_nvnodelist);
463 mp->mnt_nvnodelistsize = 0;
464 TAILQ_INIT(&mp->mnt_activevnodelist);
465 mp->mnt_activevnodelistsize = 0;
467 (void) vfs_busy(mp, MBF_NOWAIT);
468 mp->mnt_op = vfsp->vfc_vfsops;
470 vfsp->vfc_refcount++; /* XXX Unlocked */
471 mp->mnt_stat.f_type = vfsp->vfc_typenum;
473 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
474 mp->mnt_vnodecovered = vp;
475 mp->mnt_cred = crdup(cred);
476 mp->mnt_stat.f_owner = cred->cr_uid;
477 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
478 mp->mnt_iosize_max = DFLTPHYS;
481 mac_mount_create(cred, mp);
483 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
484 TAILQ_INIT(&mp->mnt_uppers);
489 * Destroy the mount struct previously allocated by vfs_mount_alloc().
492 vfs_mount_destroy(struct mount *mp)
496 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
497 if (mp->mnt_kern_flag & MNTK_MWAIT) {
498 mp->mnt_kern_flag &= ~MNTK_MWAIT;
502 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
503 KASSERT(mp->mnt_ref == 0,
504 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
505 __FILE__, __LINE__));
506 if (mp->mnt_writeopcount != 0)
507 panic("vfs_mount_destroy: nonzero writeopcount");
508 if (mp->mnt_secondary_writes != 0)
509 panic("vfs_mount_destroy: nonzero secondary_writes");
510 mp->mnt_vfc->vfc_refcount--;
511 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
514 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
516 panic("unmount: dangling vnode");
518 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
519 if (mp->mnt_nvnodelistsize != 0)
520 panic("vfs_mount_destroy: nonzero nvnodelistsize");
521 if (mp->mnt_activevnodelistsize != 0)
522 panic("vfs_mount_destroy: nonzero activevnodelistsize");
523 if (mp->mnt_lockref != 0)
524 panic("vfs_mount_destroy: nonzero lock refcount");
527 mac_mount_destroy(mp);
529 if (mp->mnt_opt != NULL)
530 vfs_freeopts(mp->mnt_opt);
531 crfree(mp->mnt_cred);
532 uma_zfree(mount_zone, mp);
536 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
538 struct vfsoptlist *optlist;
539 struct vfsopt *opt, *tmp_opt;
540 char *fstype, *fspath, *errmsg;
541 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
543 errmsg = fspath = NULL;
544 errmsg_len = fspathlen = 0;
547 error = vfs_buildopts(fsoptions, &optlist);
551 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
552 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
555 * We need these two options before the others,
556 * and they are mandatory for any filesystem.
557 * Ensure they are NUL terminated as well.
560 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
561 if (error || fstype[fstypelen - 1] != '\0') {
564 strncpy(errmsg, "Invalid fstype", errmsg_len);
568 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
569 if (error || fspath[fspathlen - 1] != '\0') {
572 strncpy(errmsg, "Invalid fspath", errmsg_len);
577 * We need to see if we have the "update" option
578 * before we call vfs_domount(), since vfs_domount() has special
579 * logic based on MNT_UPDATE. This is very important
580 * when we want to update the root filesystem.
582 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
583 if (strcmp(opt->name, "update") == 0) {
584 fsflags |= MNT_UPDATE;
585 vfs_freeopt(optlist, opt);
587 else if (strcmp(opt->name, "async") == 0)
588 fsflags |= MNT_ASYNC;
589 else if (strcmp(opt->name, "force") == 0) {
590 fsflags |= MNT_FORCE;
591 vfs_freeopt(optlist, opt);
593 else if (strcmp(opt->name, "reload") == 0) {
594 fsflags |= MNT_RELOAD;
595 vfs_freeopt(optlist, opt);
597 else if (strcmp(opt->name, "multilabel") == 0)
598 fsflags |= MNT_MULTILABEL;
599 else if (strcmp(opt->name, "noasync") == 0)
600 fsflags &= ~MNT_ASYNC;
601 else if (strcmp(opt->name, "noatime") == 0)
602 fsflags |= MNT_NOATIME;
603 else if (strcmp(opt->name, "atime") == 0) {
604 free(opt->name, M_MOUNT);
605 opt->name = strdup("nonoatime", M_MOUNT);
607 else if (strcmp(opt->name, "noclusterr") == 0)
608 fsflags |= MNT_NOCLUSTERR;
609 else if (strcmp(opt->name, "clusterr") == 0) {
610 free(opt->name, M_MOUNT);
611 opt->name = strdup("nonoclusterr", M_MOUNT);
613 else if (strcmp(opt->name, "noclusterw") == 0)
614 fsflags |= MNT_NOCLUSTERW;
615 else if (strcmp(opt->name, "clusterw") == 0) {
616 free(opt->name, M_MOUNT);
617 opt->name = strdup("nonoclusterw", M_MOUNT);
619 else if (strcmp(opt->name, "noexec") == 0)
620 fsflags |= MNT_NOEXEC;
621 else if (strcmp(opt->name, "exec") == 0) {
622 free(opt->name, M_MOUNT);
623 opt->name = strdup("nonoexec", M_MOUNT);
625 else if (strcmp(opt->name, "nosuid") == 0)
626 fsflags |= MNT_NOSUID;
627 else if (strcmp(opt->name, "suid") == 0) {
628 free(opt->name, M_MOUNT);
629 opt->name = strdup("nonosuid", M_MOUNT);
631 else if (strcmp(opt->name, "nosymfollow") == 0)
632 fsflags |= MNT_NOSYMFOLLOW;
633 else if (strcmp(opt->name, "symfollow") == 0) {
634 free(opt->name, M_MOUNT);
635 opt->name = strdup("nonosymfollow", M_MOUNT);
637 else if (strcmp(opt->name, "noro") == 0)
638 fsflags &= ~MNT_RDONLY;
639 else if (strcmp(opt->name, "rw") == 0)
640 fsflags &= ~MNT_RDONLY;
641 else if (strcmp(opt->name, "ro") == 0)
642 fsflags |= MNT_RDONLY;
643 else if (strcmp(opt->name, "rdonly") == 0) {
644 free(opt->name, M_MOUNT);
645 opt->name = strdup("ro", M_MOUNT);
646 fsflags |= MNT_RDONLY;
648 else if (strcmp(opt->name, "suiddir") == 0)
649 fsflags |= MNT_SUIDDIR;
650 else if (strcmp(opt->name, "sync") == 0)
651 fsflags |= MNT_SYNCHRONOUS;
652 else if (strcmp(opt->name, "union") == 0)
653 fsflags |= MNT_UNION;
657 * Be ultra-paranoid about making sure the type and fspath
658 * variables will fit in our mp buffers, including the
661 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
662 error = ENAMETOOLONG;
666 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
668 /* copyout the errmsg */
669 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
670 && errmsg_len > 0 && errmsg != NULL) {
671 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
673 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
674 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
677 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
678 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
683 vfs_freeopts(optlist);
690 #ifndef _SYS_SYSPROTO_H_
702 struct mount_args /* {
710 struct vfsconf *vfsp = NULL;
711 struct mntarg *ma = NULL;
716 * Mount flags are now 64-bits. On 32-bit archtectures only
717 * 32-bits are passed in, but from here on everything handles
718 * 64-bit flags correctly.
722 AUDIT_ARG_FFLAGS(flags);
725 * Filter out MNT_ROOTFS. We do not want clients of mount() in
726 * userspace to set this flag, but we must filter it out if we want
727 * MNT_UPDATE on the root file system to work.
728 * MNT_ROOTFS should only be set by the kernel when mounting its
731 flags &= ~MNT_ROOTFS;
733 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
734 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
736 free(fstype, M_TEMP);
740 AUDIT_ARG_TEXT(fstype);
742 vfsp = vfs_byname_kld(fstype, td, &error);
743 free(fstype, M_TEMP);
748 if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
753 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
754 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
755 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
756 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
757 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
759 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags);
765 * vfs_domount_first(): first file system mount (not update)
769 struct thread *td, /* Calling thread. */
770 struct vfsconf *vfsp, /* File system type. */
771 char *fspath, /* Mount path. */
772 struct vnode *vp, /* Vnode to be covered. */
773 uint64_t fsflags, /* Flags common to all filesystems. */
774 struct vfsoptlist **optlist /* Options local to the filesystem. */
782 mtx_assert(&Giant, MA_OWNED);
783 ASSERT_VOP_ELOCKED(vp, __func__);
784 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
787 * If the user is not root, ensure that they own the directory
788 * onto which we are attempting to mount.
790 error = VOP_GETATTR(vp, &va, td->td_ucred);
791 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
792 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 0);
794 error = vinvalbuf(vp, V_SAVE, 0, 0);
795 if (error == 0 && vp->v_type != VDIR)
799 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
800 vp->v_iflag |= VI_MOUNT;
811 /* Allocate and initialize the filesystem. */
812 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
813 /* XXXMAC: pass to vfs_mount_alloc? */
814 mp->mnt_optnew = *optlist;
815 /* Set the mount level flags. */
816 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
819 * Mount the filesystem.
820 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
821 * get. No freeing of cn_pnbuf.
823 error = VFS_MOUNT(mp);
824 #ifndef VFS_ALLOW_NONMPSAFE
825 if (error == 0 && VFS_NEEDSGIANT(mp)) {
826 (void)VFS_UNMOUNT(mp, fsflags);
828 printf("%s: Mounting non-MPSAFE fs (%s) is disabled\n",
829 __func__, mp->mnt_vfc->vfc_name);
834 vfs_mount_destroy(mp);
836 vp->v_iflag &= ~VI_MOUNT;
841 #ifdef VFS_ALLOW_NONMPSAFE
842 if (VFS_NEEDSGIANT(mp))
843 printf("%s: Mounting non-MPSAFE fs (%s) is deprecated\n",
844 __func__, mp->mnt_vfc->vfc_name);
847 if (mp->mnt_opt != NULL)
848 vfs_freeopts(mp->mnt_opt);
849 mp->mnt_opt = mp->mnt_optnew;
851 (void)VFS_STATFS(mp, &mp->mnt_stat);
854 * Prevent external consumers of mount options from reading mnt_optnew.
856 mp->mnt_optnew = NULL;
859 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
860 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
861 mp->mnt_kern_flag |= MNTK_ASYNC;
863 mp->mnt_kern_flag &= ~MNTK_ASYNC;
866 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
869 vp->v_iflag &= ~VI_MOUNT;
871 vp->v_mountedhere = mp;
872 /* Place the new filesystem at the end of the mount list. */
873 mtx_lock(&mountlist_mtx);
874 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
875 mtx_unlock(&mountlist_mtx);
876 vfs_event_signal(NULL, VQ_MOUNT, 0);
877 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
878 panic("mount: lost mount");
879 VOP_UNLOCK(newdp, 0);
881 mountcheckdirs(vp, newdp);
883 if ((mp->mnt_flag & MNT_RDONLY) == 0)
884 vfs_allocate_syncvnode(mp);
890 * vfs_domount_update(): update of mounted file system
894 struct thread *td, /* Calling thread. */
895 struct vnode *vp, /* Mount point vnode. */
896 uint64_t fsflags, /* Flags common to all filesystems. */
897 struct vfsoptlist **optlist /* Options local to the filesystem. */
900 struct oexport_args oexport;
901 struct export_args export;
903 int error, export_error;
906 mtx_assert(&Giant, MA_OWNED);
907 ASSERT_VOP_ELOCKED(vp, __func__);
908 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
910 if ((vp->v_vflag & VV_ROOT) == 0) {
916 * We only allow the filesystem to be reloaded if it
917 * is currently mounted read-only.
920 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
922 return (EOPNOTSUPP); /* Needs translation */
925 * Only privileged root, or (if MNT_USER is set) the user that
926 * did the original mount is permitted to update it.
928 error = vfs_suser(mp, td);
933 if (vfs_busy(mp, MBF_NOWAIT)) {
938 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
944 vp->v_iflag |= VI_MOUNT;
949 mp->mnt_flag &= ~MNT_UPDATEMASK;
950 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
951 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
952 if ((mp->mnt_flag & MNT_ASYNC) == 0)
953 mp->mnt_kern_flag &= ~MNTK_ASYNC;
955 mp->mnt_optnew = *optlist;
956 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
959 * Mount the filesystem.
960 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
961 * get. No freeing of cn_pnbuf.
963 error = VFS_MOUNT(mp);
967 /* Process the export option. */
968 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
969 sizeof(export)) == 0) {
970 export_error = vfs_export(mp, &export);
971 } else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
972 sizeof(oexport)) == 0) {
973 export.ex_flags = oexport.ex_flags;
974 export.ex_root = oexport.ex_root;
975 export.ex_anon = oexport.ex_anon;
976 export.ex_addr = oexport.ex_addr;
977 export.ex_addrlen = oexport.ex_addrlen;
978 export.ex_mask = oexport.ex_mask;
979 export.ex_masklen = oexport.ex_masklen;
980 export.ex_indexfile = oexport.ex_indexfile;
981 export.ex_numsecflavors = 0;
982 export_error = vfs_export(mp, &export);
988 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
992 * If we fail, restore old mount flags. MNT_QUOTA is special,
993 * because it is not part of MNT_UPDATEMASK, but it could have
994 * changed in the meantime if quotactl(2) was called.
995 * All in all we want current value of MNT_QUOTA, not the old
998 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1000 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1001 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1002 mp->mnt_kern_flag |= MNTK_ASYNC;
1004 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1010 if (mp->mnt_opt != NULL)
1011 vfs_freeopts(mp->mnt_opt);
1012 mp->mnt_opt = mp->mnt_optnew;
1014 (void)VFS_STATFS(mp, &mp->mnt_stat);
1016 * Prevent external consumers of mount options from reading
1019 mp->mnt_optnew = NULL;
1021 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1022 vfs_allocate_syncvnode(mp);
1024 vfs_deallocate_syncvnode(mp);
1028 vp->v_iflag &= ~VI_MOUNT;
1031 return (error != 0 ? error : export_error);
1035 * vfs_domount(): actually attempt a filesystem mount.
1039 struct thread *td, /* Calling thread. */
1040 const char *fstype, /* Filesystem type. */
1041 char *fspath, /* Mount path. */
1042 uint64_t fsflags, /* Flags common to all filesystems. */
1043 struct vfsoptlist **optlist /* Options local to the filesystem. */
1046 struct vfsconf *vfsp;
1047 struct nameidata nd;
1053 * Be ultra-paranoid about making sure the type and fspath
1054 * variables will fit in our mp buffers, including the
1057 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1058 return (ENAMETOOLONG);
1060 if (jailed(td->td_ucred) || usermount == 0) {
1061 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1066 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1068 if (fsflags & MNT_EXPORTED) {
1069 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1073 if (fsflags & MNT_SUIDDIR) {
1074 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1079 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1081 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1082 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1083 fsflags |= MNT_NOSUID | MNT_USER;
1086 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1088 if ((fsflags & MNT_UPDATE) == 0) {
1089 /* Don't try to load KLDs if we're mounting the root. */
1090 if (fsflags & MNT_ROOTFS)
1091 vfsp = vfs_byname(fstype);
1093 vfsp = vfs_byname_kld(fstype, td, &error);
1096 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
1101 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1103 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1104 UIO_SYSSPACE, fspath, td);
1108 if (!NDHASGIANT(&nd))
1110 NDFREE(&nd, NDF_ONLY_PNBUF);
1112 if ((fsflags & MNT_UPDATE) == 0) {
1113 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1114 strcpy(pathbuf, fspath);
1115 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1116 /* debug.disablefullpath == 1 results in ENODEV */
1117 if (error == 0 || error == ENODEV) {
1118 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1121 free(pathbuf, M_TEMP);
1123 error = vfs_domount_update(td, vp, fsflags, optlist);
1126 ASSERT_VI_UNLOCKED(vp, __func__);
1127 ASSERT_VOP_UNLOCKED(vp, __func__);
1133 * Unmount a filesystem.
1135 * Note: unmount takes a path to the vnode mounted on as argument, not
1136 * special file (as before).
1138 #ifndef _SYS_SYSPROTO_H_
1139 struct unmount_args {
1146 sys_unmount(td, uap)
1148 register struct unmount_args /* {
1153 struct nameidata nd;
1156 int error, id0, id1, vfslocked;
1158 AUDIT_ARG_VALUE(uap->flags);
1159 if (jailed(td->td_ucred) || usermount == 0) {
1160 error = priv_check(td, PRIV_VFS_UNMOUNT);
1165 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1166 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1168 free(pathbuf, M_TEMP);
1172 if (uap->flags & MNT_BYFSID) {
1173 AUDIT_ARG_TEXT(pathbuf);
1174 /* Decode the filesystem ID. */
1175 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1177 free(pathbuf, M_TEMP);
1181 mtx_lock(&mountlist_mtx);
1182 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1183 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1184 mp->mnt_stat.f_fsid.val[1] == id1)
1187 mtx_unlock(&mountlist_mtx);
1189 AUDIT_ARG_UPATH1(td, pathbuf);
1191 * Try to find global path for path argument.
1194 FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
1195 UIO_SYSSPACE, pathbuf, td);
1196 if (namei(&nd) == 0) {
1197 vfslocked = NDHASGIANT(&nd);
1198 NDFREE(&nd, NDF_ONLY_PNBUF);
1199 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1201 if (error == 0 || error == ENODEV)
1203 VFS_UNLOCK_GIANT(vfslocked);
1205 mtx_lock(&mountlist_mtx);
1206 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1207 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1210 mtx_unlock(&mountlist_mtx);
1212 free(pathbuf, M_TEMP);
1215 * Previously we returned ENOENT for a nonexistent path and
1216 * EINVAL for a non-mountpoint. We cannot tell these apart
1217 * now, so in the !MNT_BYFSID case return the more likely
1218 * EINVAL for compatibility.
1221 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1225 * Don't allow unmounting the root filesystem.
1227 if (mp->mnt_flag & MNT_ROOTFS) {
1231 error = dounmount(mp, uap->flags, td);
1237 * Do the actual filesystem unmount.
1240 dounmount(mp, flags, td)
1245 struct vnode *coveredvp, *fsrootvp;
1247 uint64_t async_flag;
1250 mtx_assert(&Giant, MA_OWNED);
1252 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1253 mnt_gen_r = mp->mnt_gen;
1256 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1259 * Check for mp being unmounted while waiting for the
1260 * covered vnode lock.
1262 if (coveredvp->v_mountedhere != mp ||
1263 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1264 VOP_UNLOCK(coveredvp, 0);
1269 * Only privileged root, or (if MNT_USER is set) the user that did the
1270 * original mount is permitted to unmount this filesystem.
1272 error = vfs_suser(mp, td);
1275 VOP_UNLOCK(coveredvp, 0);
1280 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1281 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1284 VOP_UNLOCK(coveredvp, 0);
1287 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1288 /* Allow filesystems to detect that a forced unmount is in progress. */
1289 if (flags & MNT_FORCE)
1290 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1292 if (mp->mnt_lockref) {
1293 mp->mnt_kern_flag |= MNTK_DRAINING;
1294 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1298 KASSERT(mp->mnt_lockref == 0,
1299 ("%s: invalid lock refcount in the drain path @ %s:%d",
1300 __func__, __FILE__, __LINE__));
1302 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1303 __func__, __FILE__, __LINE__));
1304 vn_start_write(NULL, &mp, V_WAIT);
1306 if (mp->mnt_flag & MNT_EXPUBLIC)
1307 vfs_setpublicfs(NULL, NULL, NULL);
1309 vfs_msync(mp, MNT_WAIT);
1311 async_flag = mp->mnt_flag & MNT_ASYNC;
1312 mp->mnt_flag &= ~MNT_ASYNC;
1313 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1315 cache_purgevfs(mp); /* remove cache entries for this file sys */
1316 vfs_deallocate_syncvnode(mp);
1318 * For forced unmounts, move process cdir/rdir refs on the fs root
1319 * vnode to the covered vnode. For non-forced unmounts we want
1320 * such references to cause an EBUSY error.
1322 if ((flags & MNT_FORCE) &&
1323 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1324 if (mp->mnt_vnodecovered != NULL)
1325 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1326 if (fsrootvp == rootvnode) {
1332 if (((mp->mnt_flag & MNT_RDONLY) ||
1333 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
1334 error = VFS_UNMOUNT(mp, flags);
1335 vn_finished_write(mp);
1337 * If we failed to flush the dirty blocks for this mount point,
1338 * undo all the cdir/rdir and rootvnode changes we made above.
1339 * Unless we failed to do so because the device is reporting that
1340 * it doesn't exist anymore.
1342 if (error && error != ENXIO) {
1343 if ((flags & MNT_FORCE) &&
1344 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1345 if (mp->mnt_vnodecovered != NULL)
1346 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1347 if (rootvnode == NULL) {
1348 rootvnode = fsrootvp;
1354 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1355 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1357 vfs_allocate_syncvnode(mp);
1360 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1361 mp->mnt_flag |= async_flag;
1362 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1363 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1364 mp->mnt_kern_flag |= MNTK_ASYNC;
1365 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1366 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1371 VOP_UNLOCK(coveredvp, 0);
1374 mtx_lock(&mountlist_mtx);
1375 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1376 mtx_unlock(&mountlist_mtx);
1377 if (coveredvp != NULL) {
1378 coveredvp->v_mountedhere = NULL;
1381 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1382 vfs_mount_destroy(mp);
1387 * Report errors during filesystem mounting.
1390 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1392 struct vfsoptlist *moptlist = mp->mnt_optnew;
1397 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1398 if (error || errmsg == NULL || len <= 0)
1402 vsnprintf(errmsg, (size_t)len, fmt, ap);
1407 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1413 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1414 if (error || errmsg == NULL || len <= 0)
1418 vsnprintf(errmsg, (size_t)len, fmt, ap);
1423 * ---------------------------------------------------------------------
1424 * Functions for querying mount options/arguments from filesystems.
1428 * Check that no unknown options are given
1431 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1435 const char **t, *p, *q;
1438 TAILQ_FOREACH(opt, opts, link) {
1441 if (p[0] == 'n' && p[1] == 'o')
1443 for(t = global_opts; *t != NULL; t++) {
1444 if (strcmp(*t, p) == 0)
1447 if (strcmp(*t, q) == 0)
1453 for(t = legal; *t != NULL; t++) {
1454 if (strcmp(*t, p) == 0)
1457 if (strcmp(*t, q) == 0)
1463 snprintf(errmsg, sizeof(errmsg),
1464 "mount option <%s> is unknown", p);
1468 TAILQ_FOREACH(opt, opts, link) {
1469 if (strcmp(opt->name, "errmsg") == 0) {
1470 strncpy((char *)opt->value, errmsg, opt->len);
1475 printf("%s\n", errmsg);
1481 * Get a mount option by its name.
1483 * Return 0 if the option was found, ENOENT otherwise.
1484 * If len is non-NULL it will be filled with the length
1485 * of the option. If buf is non-NULL, it will be filled
1486 * with the address of the option.
1489 vfs_getopt(opts, name, buf, len)
1490 struct vfsoptlist *opts;
1497 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1499 TAILQ_FOREACH(opt, opts, link) {
1500 if (strcmp(name, opt->name) == 0) {
1513 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1520 TAILQ_FOREACH(opt, opts, link) {
1521 if (strcmp(name, opt->name) == 0) {
1530 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1532 char *opt_value, *vtp;
1536 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1539 if (opt_len == 0 || opt_value == NULL)
1541 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1543 iv = strtoq(opt_value, &vtp, 0);
1544 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
1572 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1577 TAILQ_FOREACH(opt, opts, link) {
1578 if (strcmp(name, opt->name) != 0)
1581 if (opt->len == 0 ||
1582 ((char *)opt->value)[opt->len - 1] != '\0') {
1586 return (opt->value);
1593 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1598 TAILQ_FOREACH(opt, opts, link) {
1599 if (strcmp(name, opt->name) == 0) {
1612 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1618 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1620 TAILQ_FOREACH(opt, opts, link) {
1621 if (strcmp(name, opt->name) != 0)
1624 if (opt->len == 0 || opt->value == NULL)
1626 if (((char *)opt->value)[opt->len - 1] != '\0')
1629 ret = vsscanf(opt->value, fmt, ap);
1637 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1641 TAILQ_FOREACH(opt, opts, link) {
1642 if (strcmp(name, opt->name) != 0)
1645 if (opt->value == NULL)
1648 if (opt->len != len)
1650 bcopy(value, opt->value, len);
1658 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1662 TAILQ_FOREACH(opt, opts, link) {
1663 if (strcmp(name, opt->name) != 0)
1666 if (opt->value == NULL)
1672 bcopy(value, opt->value, len);
1680 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1684 TAILQ_FOREACH(opt, opts, link) {
1685 if (strcmp(name, opt->name) != 0)
1688 if (opt->value == NULL)
1689 opt->len = strlen(value) + 1;
1690 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1698 * Find and copy a mount option.
1700 * The size of the buffer has to be specified
1701 * in len, if it is not the same length as the
1702 * mount option, EINVAL is returned.
1703 * Returns ENOENT if the option is not found.
1706 vfs_copyopt(opts, name, dest, len)
1707 struct vfsoptlist *opts;
1714 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1716 TAILQ_FOREACH(opt, opts, link) {
1717 if (strcmp(name, opt->name) == 0) {
1719 if (len != opt->len)
1721 bcopy(opt->value, dest, opt->len);
1729 * These are helper functions for filesystems to traverse all
1730 * their vnodes. See MNT_VNODE_FOREACH() in sys/mount.h.
1732 * This interface has been deprecated in favor of MNT_VNODE_FOREACH_ALL.
1735 MALLOC_DECLARE(M_VNODE_MARKER);
1738 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1742 mtx_assert(MNT_MTX(mp), MA_OWNED);
1744 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1745 if (should_yield()) {
1747 kern_yield(PRI_UNCHANGED);
1750 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1751 while (vp != NULL && vp->v_type == VMARKER)
1752 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1754 /* Check if we are done */
1756 __mnt_vnode_markerfree(mvp, mp);
1759 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1760 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1765 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1769 mtx_assert(MNT_MTX(mp), MA_OWNED);
1771 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1772 while (vp != NULL && vp->v_type == VMARKER)
1773 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1775 /* Check if we are done */
1782 *mvp = (struct vnode *) malloc(sizeof(struct vnode),
1786 (*mvp)->v_type = VMARKER;
1788 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1789 while (vp != NULL && vp->v_type == VMARKER)
1790 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1792 /* Check if we are done */
1795 free(*mvp, M_VNODE_MARKER);
1801 (*mvp)->v_mount = mp;
1802 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1808 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1814 mtx_assert(MNT_MTX(mp), MA_OWNED);
1816 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1817 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1819 free(*mvp, M_VNODE_MARKER);
1826 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1830 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1831 if (sbp != &mp->mnt_stat)
1832 *sbp = mp->mnt_stat;
1837 vfs_mountedfrom(struct mount *mp, const char *from)
1840 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1841 strlcpy(mp->mnt_stat.f_mntfromname, from,
1842 sizeof mp->mnt_stat.f_mntfromname);
1846 * ---------------------------------------------------------------------
1847 * This is the api for building mount args and mounting filesystems from
1848 * inside the kernel.
1850 * The API works by accumulation of individual args. First error is
1853 * XXX: should be documented in new manpage kernel_mount(9)
1856 /* A memory allocation which must be freed when we are done */
1858 SLIST_ENTRY(mntaarg) next;
1861 /* The header for the mount arguments */
1866 SLIST_HEAD(, mntaarg) list;
1870 * Add a boolean argument.
1872 * flag is the boolean value.
1873 * name must start with "no".
1876 mount_argb(struct mntarg *ma, int flag, const char *name)
1879 KASSERT(name[0] == 'n' && name[1] == 'o',
1880 ("mount_argb(...,%s): name must start with 'no'", name));
1882 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1886 * Add an argument printf style
1889 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1892 struct mntaarg *maa;
1897 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1898 SLIST_INIT(&ma->list);
1903 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1905 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1906 ma->v[ma->len].iov_len = strlen(name) + 1;
1909 sb = sbuf_new_auto();
1911 sbuf_vprintf(sb, fmt, ap);
1914 len = sbuf_len(sb) + 1;
1915 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1916 SLIST_INSERT_HEAD(&ma->list, maa, next);
1917 bcopy(sbuf_data(sb), maa + 1, len);
1920 ma->v[ma->len].iov_base = maa + 1;
1921 ma->v[ma->len].iov_len = len;
1928 * Add an argument which is a userland string.
1931 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1933 struct mntaarg *maa;
1939 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1940 SLIST_INIT(&ma->list);
1944 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1945 SLIST_INSERT_HEAD(&ma->list, maa, next);
1946 tbuf = (void *)(maa + 1);
1947 ma->error = copyinstr(val, tbuf, len, NULL);
1948 return (mount_arg(ma, name, tbuf, -1));
1954 * If length is -1, treat value as a C string.
1957 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1961 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1962 SLIST_INIT(&ma->list);
1967 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1969 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1970 ma->v[ma->len].iov_len = strlen(name) + 1;
1973 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1975 ma->v[ma->len].iov_len = strlen(val) + 1;
1977 ma->v[ma->len].iov_len = len;
1983 * Free a mntarg structure
1986 free_mntarg(struct mntarg *ma)
1988 struct mntaarg *maa;
1990 while (!SLIST_EMPTY(&ma->list)) {
1991 maa = SLIST_FIRST(&ma->list);
1992 SLIST_REMOVE_HEAD(&ma->list, next);
1995 free(ma->v, M_MOUNT);
2000 * Mount a filesystem
2003 kernel_mount(struct mntarg *ma, uint64_t flags)
2008 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2009 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2010 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2012 auio.uio_iov = ma->v;
2013 auio.uio_iovcnt = ma->len;
2014 auio.uio_segflg = UIO_SYSSPACE;
2018 error = vfs_donmount(curthread, flags, &auio);
2024 * A printflike function to mount a filesystem.
2027 kernel_vmount(int flags, ...)
2029 struct mntarg *ma = NULL;
2035 va_start(ap, flags);
2037 cp = va_arg(ap, const char *);
2040 vp = va_arg(ap, const void *);
2041 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2045 error = kernel_mount(ma, flags);
2050 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2053 bcopy(oexp, exp, sizeof(*oexp));
2054 exp->ex_numsecflavors = 0;