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 * 3. 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>
42 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/filedesc.h>
53 #include <sys/reboot.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
64 #include <geom/geom.h>
66 #include <machine/stdarg.h>
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
71 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
73 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
74 uint64_t fsflags, struct vfsoptlist **optlist);
75 static void free_mntarg(struct mntarg *ma);
77 static int usermount = 0;
78 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
79 "Unprivileged users may mount and unmount file systems");
81 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
82 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
83 static uma_zone_t mount_zone;
85 /* List of mounted filesystems. */
86 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
88 /* For any iteration/modification of mountlist */
89 struct mtx mountlist_mtx;
90 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
93 * Global opts, taken by all filesystems
95 static const char *global_opts[] = {
107 mount_init(void *mem, int size, int flags)
111 mp = (struct mount *)mem;
112 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
113 mtx_init(&mp->mnt_listmtx, "struct mount vlist 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_listmtx);
126 mtx_destroy(&mp->mnt_mtx);
130 vfs_mount_init(void *dummy __unused)
133 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
134 NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
136 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
139 * ---------------------------------------------------------------------
140 * Functions for building and sanitizing the mount options
143 /* Remove one mount option. */
145 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
148 TAILQ_REMOVE(opts, opt, link);
149 free(opt->name, M_MOUNT);
150 if (opt->value != NULL)
151 free(opt->value, M_MOUNT);
155 /* Release all resources related to the mount options. */
157 vfs_freeopts(struct vfsoptlist *opts)
161 while (!TAILQ_EMPTY(opts)) {
162 opt = TAILQ_FIRST(opts);
163 vfs_freeopt(opts, opt);
169 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
171 struct vfsopt *opt, *temp;
175 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
176 if (strcmp(opt->name, name) == 0)
177 vfs_freeopt(opts, opt);
182 vfs_isopt_ro(const char *opt)
185 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
186 strcmp(opt, "norw") == 0)
192 vfs_isopt_rw(const char *opt)
195 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
201 * Check if options are equal (with or without the "no" prefix).
204 vfs_equalopts(const char *opt1, const char *opt2)
208 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
209 if (strcmp(opt1, opt2) == 0)
211 /* "noopt" vs. "opt" */
212 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
214 /* "opt" vs. "noopt" */
215 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
217 while ((p = strchr(opt1, '.')) != NULL &&
218 !strncmp(opt1, opt2, ++p - opt1)) {
221 /* "foo.noopt" vs. "foo.opt" */
222 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
224 /* "foo.opt" vs. "foo.noopt" */
225 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
228 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
229 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
230 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
236 * If a mount option is specified several times,
237 * (with or without the "no" prefix) only keep
238 * the last occurrence of it.
241 vfs_sanitizeopts(struct vfsoptlist *opts)
243 struct vfsopt *opt, *opt2, *tmp;
245 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
246 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
247 while (opt2 != NULL) {
248 if (vfs_equalopts(opt->name, opt2->name)) {
249 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
250 vfs_freeopt(opts, opt2);
253 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
260 * Build a linked list of mount options from a struct uio.
263 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
265 struct vfsoptlist *opts;
267 size_t memused, namelen, optlen;
268 unsigned int i, iovcnt;
271 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
274 iovcnt = auio->uio_iovcnt;
275 for (i = 0; i < iovcnt; i += 2) {
276 namelen = auio->uio_iov[i].iov_len;
277 optlen = auio->uio_iov[i + 1].iov_len;
278 memused += sizeof(struct vfsopt) + optlen + namelen;
280 * Avoid consuming too much memory, and attempts to overflow
283 if (memused > VFS_MOUNTARG_SIZE_MAX ||
284 optlen > VFS_MOUNTARG_SIZE_MAX ||
285 namelen > VFS_MOUNTARG_SIZE_MAX) {
290 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
291 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
298 * Do this early, so jumps to "bad" will free the current
301 TAILQ_INSERT_TAIL(opts, opt, link);
303 if (auio->uio_segflg == UIO_SYSSPACE) {
304 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
306 error = copyin(auio->uio_iov[i].iov_base, opt->name,
311 /* Ensure names are null-terminated strings. */
312 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
318 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
319 if (auio->uio_segflg == UIO_SYSSPACE) {
320 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
323 error = copyin(auio->uio_iov[i + 1].iov_base,
330 vfs_sanitizeopts(opts);
339 * Merge the old mount options with the new ones passed
340 * in the MNT_UPDATE case.
342 * XXX: This function will keep a "nofoo" option in the new
343 * options. E.g, if the option's canonical name is "foo",
344 * "nofoo" ends up in the mount point's active options.
347 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
349 struct vfsopt *opt, *new;
351 TAILQ_FOREACH(opt, oldopts, link) {
352 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
353 new->name = strdup(opt->name, M_MOUNT);
355 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
356 bcopy(opt->value, new->value, opt->len);
360 new->seen = opt->seen;
361 TAILQ_INSERT_HEAD(toopts, new, link);
363 vfs_sanitizeopts(toopts);
367 * Mount a filesystem.
372 struct nmount_args /* {
384 * Mount flags are now 64-bits. On 32-bit archtectures only
385 * 32-bits are passed in, but from here on everything handles
386 * 64-bit flags correctly.
390 AUDIT_ARG_FFLAGS(flags);
391 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
392 uap->iovp, uap->iovcnt, flags);
395 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
396 * userspace to set this flag, but we must filter it out if we want
397 * MNT_UPDATE on the root file system to work.
398 * MNT_ROOTFS should only be set by the kernel when mounting its
401 flags &= ~MNT_ROOTFS;
403 iovcnt = uap->iovcnt;
405 * Check that we have an even number of iovec's
406 * and that we have at least two options.
408 if ((iovcnt & 1) || (iovcnt < 4)) {
409 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
414 error = copyinuio(uap->iovp, iovcnt, &auio);
416 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
420 error = vfs_donmount(td, flags, auio);
427 * ---------------------------------------------------------------------
428 * Various utility functions
432 vfs_ref(struct mount *mp)
435 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
442 vfs_rel(struct mount *mp)
445 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
452 * Allocate and initialize the mount point struct.
455 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
460 mp = uma_zalloc(mount_zone, M_WAITOK);
461 bzero(&mp->mnt_startzero,
462 __rangeof(struct mount, mnt_startzero, mnt_endzero));
463 TAILQ_INIT(&mp->mnt_nvnodelist);
464 mp->mnt_nvnodelistsize = 0;
465 TAILQ_INIT(&mp->mnt_activevnodelist);
466 mp->mnt_activevnodelistsize = 0;
467 TAILQ_INIT(&mp->mnt_tmpfreevnodelist);
468 mp->mnt_tmpfreevnodelistsize = 0;
470 (void) vfs_busy(mp, MBF_NOWAIT);
471 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
472 mp->mnt_op = vfsp->vfc_vfsops;
474 mp->mnt_stat.f_type = vfsp->vfc_typenum;
476 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
477 mp->mnt_vnodecovered = vp;
478 mp->mnt_cred = crdup(cred);
479 mp->mnt_stat.f_owner = cred->cr_uid;
480 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
481 mp->mnt_iosize_max = DFLTPHYS;
484 mac_mount_create(cred, mp);
486 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
487 TAILQ_INIT(&mp->mnt_uppers);
492 * Destroy the mount struct previously allocated by vfs_mount_alloc().
495 vfs_mount_destroy(struct mount *mp)
499 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
500 if (mp->mnt_kern_flag & MNTK_MWAIT) {
501 mp->mnt_kern_flag &= ~MNTK_MWAIT;
505 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
506 KASSERT(mp->mnt_ref == 0,
507 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
508 __FILE__, __LINE__));
509 if (mp->mnt_writeopcount != 0)
510 panic("vfs_mount_destroy: nonzero writeopcount");
511 if (mp->mnt_secondary_writes != 0)
512 panic("vfs_mount_destroy: nonzero secondary_writes");
513 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
514 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
517 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
518 vn_printf(vp, "dangling vnode ");
519 panic("unmount: dangling vnode");
521 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
522 if (mp->mnt_nvnodelistsize != 0)
523 panic("vfs_mount_destroy: nonzero nvnodelistsize");
524 if (mp->mnt_activevnodelistsize != 0)
525 panic("vfs_mount_destroy: nonzero activevnodelistsize");
526 if (mp->mnt_tmpfreevnodelistsize != 0)
527 panic("vfs_mount_destroy: nonzero tmpfreevnodelistsize");
528 if (mp->mnt_lockref != 0)
529 panic("vfs_mount_destroy: nonzero lock refcount");
532 mac_mount_destroy(mp);
534 if (mp->mnt_opt != NULL)
535 vfs_freeopts(mp->mnt_opt);
536 crfree(mp->mnt_cred);
537 uma_zfree(mount_zone, mp);
541 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
543 struct vfsoptlist *optlist;
544 struct vfsopt *opt, *tmp_opt;
545 char *fstype, *fspath, *errmsg;
546 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
548 errmsg = fspath = NULL;
549 errmsg_len = fspathlen = 0;
552 error = vfs_buildopts(fsoptions, &optlist);
556 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
557 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_SAFE(opt, optlist, link, tmp_opt) {
588 if (strcmp(opt->name, "update") == 0) {
589 fsflags |= MNT_UPDATE;
590 vfs_freeopt(optlist, opt);
592 else if (strcmp(opt->name, "async") == 0)
593 fsflags |= MNT_ASYNC;
594 else if (strcmp(opt->name, "force") == 0) {
595 fsflags |= MNT_FORCE;
596 vfs_freeopt(optlist, opt);
598 else if (strcmp(opt->name, "reload") == 0) {
599 fsflags |= MNT_RELOAD;
600 vfs_freeopt(optlist, opt);
602 else if (strcmp(opt->name, "multilabel") == 0)
603 fsflags |= MNT_MULTILABEL;
604 else if (strcmp(opt->name, "noasync") == 0)
605 fsflags &= ~MNT_ASYNC;
606 else if (strcmp(opt->name, "noatime") == 0)
607 fsflags |= MNT_NOATIME;
608 else if (strcmp(opt->name, "atime") == 0) {
609 free(opt->name, M_MOUNT);
610 opt->name = strdup("nonoatime", M_MOUNT);
612 else if (strcmp(opt->name, "noclusterr") == 0)
613 fsflags |= MNT_NOCLUSTERR;
614 else if (strcmp(opt->name, "clusterr") == 0) {
615 free(opt->name, M_MOUNT);
616 opt->name = strdup("nonoclusterr", M_MOUNT);
618 else if (strcmp(opt->name, "noclusterw") == 0)
619 fsflags |= MNT_NOCLUSTERW;
620 else if (strcmp(opt->name, "clusterw") == 0) {
621 free(opt->name, M_MOUNT);
622 opt->name = strdup("nonoclusterw", M_MOUNT);
624 else if (strcmp(opt->name, "noexec") == 0)
625 fsflags |= MNT_NOEXEC;
626 else if (strcmp(opt->name, "exec") == 0) {
627 free(opt->name, M_MOUNT);
628 opt->name = strdup("nonoexec", M_MOUNT);
630 else if (strcmp(opt->name, "nosuid") == 0)
631 fsflags |= MNT_NOSUID;
632 else if (strcmp(opt->name, "suid") == 0) {
633 free(opt->name, M_MOUNT);
634 opt->name = strdup("nonosuid", M_MOUNT);
636 else if (strcmp(opt->name, "nosymfollow") == 0)
637 fsflags |= MNT_NOSYMFOLLOW;
638 else if (strcmp(opt->name, "symfollow") == 0) {
639 free(opt->name, M_MOUNT);
640 opt->name = strdup("nonosymfollow", M_MOUNT);
642 else if (strcmp(opt->name, "noro") == 0)
643 fsflags &= ~MNT_RDONLY;
644 else if (strcmp(opt->name, "rw") == 0)
645 fsflags &= ~MNT_RDONLY;
646 else if (strcmp(opt->name, "ro") == 0)
647 fsflags |= MNT_RDONLY;
648 else if (strcmp(opt->name, "rdonly") == 0) {
649 free(opt->name, M_MOUNT);
650 opt->name = strdup("ro", M_MOUNT);
651 fsflags |= MNT_RDONLY;
653 else if (strcmp(opt->name, "suiddir") == 0)
654 fsflags |= MNT_SUIDDIR;
655 else if (strcmp(opt->name, "sync") == 0)
656 fsflags |= MNT_SYNCHRONOUS;
657 else if (strcmp(opt->name, "union") == 0)
658 fsflags |= MNT_UNION;
659 else if (strcmp(opt->name, "automounted") == 0) {
660 fsflags |= MNT_AUTOMOUNTED;
661 vfs_freeopt(optlist, opt);
666 * Be ultra-paranoid about making sure the type and fspath
667 * variables will fit in our mp buffers, including the
670 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
671 error = ENAMETOOLONG;
675 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
677 /* copyout the errmsg */
678 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
679 && errmsg_len > 0 && errmsg != NULL) {
680 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
682 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
683 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
686 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
687 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
692 vfs_freeopts(optlist);
699 #ifndef _SYS_SYSPROTO_H_
711 struct mount_args /* {
719 struct vfsconf *vfsp = NULL;
720 struct mntarg *ma = NULL;
725 * Mount flags are now 64-bits. On 32-bit architectures only
726 * 32-bits are passed in, but from here on everything handles
727 * 64-bit flags correctly.
731 AUDIT_ARG_FFLAGS(flags);
734 * Filter out MNT_ROOTFS. We do not want clients of mount() in
735 * userspace to set this flag, but we must filter it out if we want
736 * MNT_UPDATE on the root file system to work.
737 * MNT_ROOTFS should only be set by the kernel when mounting its
740 flags &= ~MNT_ROOTFS;
742 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
743 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
745 free(fstype, M_TEMP);
749 AUDIT_ARG_TEXT(fstype);
750 vfsp = vfs_byname_kld(fstype, td, &error);
751 free(fstype, M_TEMP);
754 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
757 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
758 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
759 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
760 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
761 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
763 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags);
768 * vfs_domount_first(): first file system mount (not update)
772 struct thread *td, /* Calling thread. */
773 struct vfsconf *vfsp, /* File system type. */
774 char *fspath, /* Mount path. */
775 struct vnode *vp, /* Vnode to be covered. */
776 uint64_t fsflags, /* Flags common to all filesystems. */
777 struct vfsoptlist **optlist /* Options local to the filesystem. */
785 ASSERT_VOP_ELOCKED(vp, __func__);
786 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
789 * If the user is not root, ensure that they own the directory
790 * onto which we are attempting to mount.
792 error = VOP_GETATTR(vp, &va, td->td_ucred);
793 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
794 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 0);
796 error = vinvalbuf(vp, V_SAVE, 0, 0);
797 if (error == 0 && vp->v_type != VDIR)
801 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
802 vp->v_iflag |= VI_MOUNT;
813 /* Allocate and initialize the filesystem. */
814 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
815 /* XXXMAC: pass to vfs_mount_alloc? */
816 mp->mnt_optnew = *optlist;
817 /* Set the mount level flags. */
818 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
821 * Mount the filesystem.
822 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
823 * get. No freeing of cn_pnbuf.
825 error = VFS_MOUNT(mp);
828 vfs_mount_destroy(mp);
830 vp->v_iflag &= ~VI_MOUNT;
836 if (mp->mnt_opt != NULL)
837 vfs_freeopts(mp->mnt_opt);
838 mp->mnt_opt = mp->mnt_optnew;
840 (void)VFS_STATFS(mp, &mp->mnt_stat);
843 * Prevent external consumers of mount options from reading mnt_optnew.
845 mp->mnt_optnew = NULL;
848 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
849 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
850 mp->mnt_kern_flag |= MNTK_ASYNC;
852 mp->mnt_kern_flag &= ~MNTK_ASYNC;
855 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
858 vp->v_iflag &= ~VI_MOUNT;
860 vp->v_mountedhere = mp;
861 /* Place the new filesystem at the end of the mount list. */
862 mtx_lock(&mountlist_mtx);
863 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
864 mtx_unlock(&mountlist_mtx);
865 vfs_event_signal(NULL, VQ_MOUNT, 0);
866 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
867 panic("mount: lost mount");
869 EVENTHANDLER_INVOKE(vfs_mounted, mp, newdp, td);
870 VOP_UNLOCK(newdp, 0);
871 mountcheckdirs(vp, newdp);
873 if ((mp->mnt_flag & MNT_RDONLY) == 0)
874 vfs_allocate_syncvnode(mp);
880 * vfs_domount_update(): update of mounted file system
884 struct thread *td, /* Calling thread. */
885 struct vnode *vp, /* Mount point vnode. */
886 uint64_t fsflags, /* Flags common to all filesystems. */
887 struct vfsoptlist **optlist /* Options local to the filesystem. */
890 struct export_args export;
893 int error, export_error, len;
896 ASSERT_VOP_ELOCKED(vp, __func__);
897 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
900 if ((vp->v_vflag & VV_ROOT) == 0) {
901 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
911 * We only allow the filesystem to be reloaded if it
912 * is currently mounted read-only.
915 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
917 return (EOPNOTSUPP); /* Needs translation */
920 * Only privileged root, or (if MNT_USER is set) the user that
921 * did the original mount is permitted to update it.
923 error = vfs_suser(mp, td);
928 if (vfs_busy(mp, MBF_NOWAIT)) {
933 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
939 vp->v_iflag |= VI_MOUNT;
944 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
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);
966 /* Process the export option. */
967 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
969 /* Assume that there is only 1 ABI for each length. */
971 case (sizeof(struct oexport_args)):
972 bzero(&export, sizeof(export));
974 case (sizeof(export)):
975 bcopy(bufp, &export, len);
976 export_error = vfs_export(mp, &export);
979 export_error = EINVAL;
986 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
990 * If we fail, restore old mount flags. MNT_QUOTA is special,
991 * because it is not part of MNT_UPDATEMASK, but it could have
992 * changed in the meantime if quotactl(2) was called.
993 * All in all we want current value of MNT_QUOTA, not the old
996 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
998 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
999 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1000 mp->mnt_kern_flag |= MNTK_ASYNC;
1002 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1008 if (mp->mnt_opt != NULL)
1009 vfs_freeopts(mp->mnt_opt);
1010 mp->mnt_opt = mp->mnt_optnew;
1012 (void)VFS_STATFS(mp, &mp->mnt_stat);
1014 * Prevent external consumers of mount options from reading
1017 mp->mnt_optnew = NULL;
1019 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1020 vfs_allocate_syncvnode(mp);
1022 vfs_deallocate_syncvnode(mp);
1026 vp->v_iflag &= ~VI_MOUNT;
1029 return (error != 0 ? error : export_error);
1033 * vfs_domount(): actually attempt a filesystem mount.
1037 struct thread *td, /* Calling thread. */
1038 const char *fstype, /* Filesystem type. */
1039 char *fspath, /* Mount path. */
1040 uint64_t fsflags, /* Flags common to all filesystems. */
1041 struct vfsoptlist **optlist /* Options local to the filesystem. */
1044 struct vfsconf *vfsp;
1045 struct nameidata nd;
1051 * Be ultra-paranoid about making sure the type and fspath
1052 * variables will fit in our mp buffers, including the
1055 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1056 return (ENAMETOOLONG);
1058 if (jailed(td->td_ucred) || usermount == 0) {
1059 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1064 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1066 if (fsflags & MNT_EXPORTED) {
1067 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1071 if (fsflags & MNT_SUIDDIR) {
1072 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1077 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1079 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1080 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1081 fsflags |= MNT_NOSUID | MNT_USER;
1084 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1086 if ((fsflags & MNT_UPDATE) == 0) {
1087 /* Don't try to load KLDs if we're mounting the root. */
1088 if (fsflags & MNT_ROOTFS)
1089 vfsp = vfs_byname(fstype);
1091 vfsp = vfs_byname_kld(fstype, td, &error);
1094 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
1099 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1101 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1102 UIO_SYSSPACE, fspath, td);
1106 NDFREE(&nd, NDF_ONLY_PNBUF);
1108 if ((fsflags & MNT_UPDATE) == 0) {
1109 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1110 strcpy(pathbuf, fspath);
1111 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1112 /* debug.disablefullpath == 1 results in ENODEV */
1113 if (error == 0 || error == ENODEV) {
1114 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1117 free(pathbuf, M_TEMP);
1119 error = vfs_domount_update(td, vp, fsflags, optlist);
1125 * Unmount a filesystem.
1127 * Note: unmount takes a path to the vnode mounted on as argument, not
1128 * special file (as before).
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct unmount_args {
1138 sys_unmount(struct thread *td, struct unmount_args *uap)
1140 struct nameidata nd;
1143 int error, id0, id1;
1145 AUDIT_ARG_VALUE(uap->flags);
1146 if (jailed(td->td_ucred) || usermount == 0) {
1147 error = priv_check(td, PRIV_VFS_UNMOUNT);
1152 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1153 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1155 free(pathbuf, M_TEMP);
1158 if (uap->flags & MNT_BYFSID) {
1159 AUDIT_ARG_TEXT(pathbuf);
1160 /* Decode the filesystem ID. */
1161 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1162 free(pathbuf, M_TEMP);
1166 mtx_lock(&mountlist_mtx);
1167 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1168 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1169 mp->mnt_stat.f_fsid.val[1] == id1) {
1174 mtx_unlock(&mountlist_mtx);
1177 * Try to find global path for path argument.
1179 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1180 UIO_SYSSPACE, pathbuf, td);
1181 if (namei(&nd) == 0) {
1182 NDFREE(&nd, NDF_ONLY_PNBUF);
1183 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1185 if (error == 0 || error == ENODEV)
1188 mtx_lock(&mountlist_mtx);
1189 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1190 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1195 mtx_unlock(&mountlist_mtx);
1197 free(pathbuf, M_TEMP);
1200 * Previously we returned ENOENT for a nonexistent path and
1201 * EINVAL for a non-mountpoint. We cannot tell these apart
1202 * now, so in the !MNT_BYFSID case return the more likely
1203 * EINVAL for compatibility.
1205 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1209 * Don't allow unmounting the root filesystem.
1211 if (mp->mnt_flag & MNT_ROOTFS) {
1215 error = dounmount(mp, uap->flags, td);
1220 * Return error if any of the vnodes, ignoring the root vnode
1221 * and the syncer vnode, have non-zero usecount.
1223 * This function is purely advisory - it can return false positives
1227 vfs_check_usecounts(struct mount *mp)
1229 struct vnode *vp, *mvp;
1231 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1232 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1233 vp->v_usecount != 0) {
1235 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1245 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1248 mtx_assert(MNT_MTX(mp), MA_OWNED);
1249 mp->mnt_kern_flag &= ~mntkflags;
1250 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1251 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1255 if (coveredvp != NULL) {
1256 VOP_UNLOCK(coveredvp, 0);
1259 vn_finished_write(mp);
1263 * Do the actual filesystem unmount.
1266 dounmount(struct mount *mp, int flags, struct thread *td)
1268 struct vnode *coveredvp, *fsrootvp;
1270 uint64_t async_flag;
1273 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1274 mnt_gen_r = mp->mnt_gen;
1277 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1279 * Check for mp being unmounted while waiting for the
1280 * covered vnode lock.
1282 if (coveredvp->v_mountedhere != mp ||
1283 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1284 VOP_UNLOCK(coveredvp, 0);
1292 * Only privileged root, or (if MNT_USER is set) the user that did the
1293 * original mount is permitted to unmount this filesystem.
1295 error = vfs_suser(mp, td);
1297 if (coveredvp != NULL) {
1298 VOP_UNLOCK(coveredvp, 0);
1305 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1307 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1308 (mp->mnt_flag & MNT_UPDATE) != 0 ||
1309 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1310 dounmount_cleanup(mp, coveredvp, 0);
1313 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1314 if (flags & MNT_NONBUSY) {
1316 error = vfs_check_usecounts(mp);
1319 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT |
1324 /* Allow filesystems to detect that a forced unmount is in progress. */
1325 if (flags & MNT_FORCE) {
1326 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1329 * Must be done after setting MNTK_UNMOUNTF and before
1330 * waiting for mnt_lockref to become 0.
1336 if (mp->mnt_lockref) {
1337 mp->mnt_kern_flag |= MNTK_DRAINING;
1338 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1342 KASSERT(mp->mnt_lockref == 0,
1343 ("%s: invalid lock refcount in the drain path @ %s:%d",
1344 __func__, __FILE__, __LINE__));
1346 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1347 __func__, __FILE__, __LINE__));
1349 if (mp->mnt_flag & MNT_EXPUBLIC)
1350 vfs_setpublicfs(NULL, NULL, NULL);
1353 * From now, we can claim that the use reference on the
1354 * coveredvp is ours, and the ref can be released only by
1355 * successfull unmount by us, or left for later unmount
1356 * attempt. The previously acquired hold reference is no
1357 * longer needed to protect the vnode from reuse.
1359 if (coveredvp != NULL)
1362 vfs_msync(mp, MNT_WAIT);
1364 async_flag = mp->mnt_flag & MNT_ASYNC;
1365 mp->mnt_flag &= ~MNT_ASYNC;
1366 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1368 cache_purgevfs(mp, false); /* remove cache entries for this file sys */
1369 vfs_deallocate_syncvnode(mp);
1371 * For forced unmounts, move process cdir/rdir refs on the fs root
1372 * vnode to the covered vnode. For non-forced unmounts we want
1373 * such references to cause an EBUSY error.
1375 if ((flags & MNT_FORCE) &&
1376 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1377 if (mp->mnt_vnodecovered != NULL &&
1378 (mp->mnt_flag & MNT_IGNORE) == 0)
1379 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1380 if (fsrootvp == rootvnode) {
1386 if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 ||
1387 (error = VFS_SYNC(mp, MNT_WAIT)) == 0)
1388 error = VFS_UNMOUNT(mp, flags);
1389 vn_finished_write(mp);
1391 * If we failed to flush the dirty blocks for this mount point,
1392 * undo all the cdir/rdir and rootvnode changes we made above.
1393 * Unless we failed to do so because the device is reporting that
1394 * it doesn't exist anymore.
1396 if (error && error != ENXIO) {
1397 if ((flags & MNT_FORCE) &&
1398 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1399 if (mp->mnt_vnodecovered != NULL &&
1400 (mp->mnt_flag & MNT_IGNORE) == 0)
1401 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1402 if (rootvnode == NULL) {
1403 rootvnode = fsrootvp;
1409 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1410 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1412 vfs_allocate_syncvnode(mp);
1415 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1416 mp->mnt_flag |= async_flag;
1417 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1418 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1419 mp->mnt_kern_flag |= MNTK_ASYNC;
1420 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1421 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1426 VOP_UNLOCK(coveredvp, 0);
1429 mtx_lock(&mountlist_mtx);
1430 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1431 mtx_unlock(&mountlist_mtx);
1432 EVENTHANDLER_INVOKE(vfs_unmounted, mp, td);
1433 if (coveredvp != NULL) {
1434 coveredvp->v_mountedhere = NULL;
1437 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1438 if (mp == rootdevmp)
1440 vfs_mount_destroy(mp);
1445 * Report errors during filesystem mounting.
1448 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1450 struct vfsoptlist *moptlist = mp->mnt_optnew;
1455 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1456 if (error || errmsg == NULL || len <= 0)
1460 vsnprintf(errmsg, (size_t)len, fmt, ap);
1465 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1471 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1472 if (error || errmsg == NULL || len <= 0)
1476 vsnprintf(errmsg, (size_t)len, fmt, ap);
1481 * ---------------------------------------------------------------------
1482 * Functions for querying mount options/arguments from filesystems.
1486 * Check that no unknown options are given
1489 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1493 const char **t, *p, *q;
1496 TAILQ_FOREACH(opt, opts, link) {
1499 if (p[0] == 'n' && p[1] == 'o')
1501 for(t = global_opts; *t != NULL; t++) {
1502 if (strcmp(*t, p) == 0)
1505 if (strcmp(*t, q) == 0)
1511 for(t = legal; *t != NULL; t++) {
1512 if (strcmp(*t, p) == 0)
1515 if (strcmp(*t, q) == 0)
1521 snprintf(errmsg, sizeof(errmsg),
1522 "mount option <%s> is unknown", p);
1526 TAILQ_FOREACH(opt, opts, link) {
1527 if (strcmp(opt->name, "errmsg") == 0) {
1528 strncpy((char *)opt->value, errmsg, opt->len);
1533 printf("%s\n", errmsg);
1539 * Get a mount option by its name.
1541 * Return 0 if the option was found, ENOENT otherwise.
1542 * If len is non-NULL it will be filled with the length
1543 * of the option. If buf is non-NULL, it will be filled
1544 * with the address of the option.
1547 vfs_getopt(opts, name, buf, len)
1548 struct vfsoptlist *opts;
1555 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1557 TAILQ_FOREACH(opt, opts, link) {
1558 if (strcmp(name, opt->name) == 0) {
1571 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1578 TAILQ_FOREACH(opt, opts, link) {
1579 if (strcmp(name, opt->name) == 0) {
1588 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1590 char *opt_value, *vtp;
1594 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1597 if (opt_len == 0 || opt_value == NULL)
1599 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1601 iv = strtoq(opt_value, &vtp, 0);
1602 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
1630 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1635 TAILQ_FOREACH(opt, opts, link) {
1636 if (strcmp(name, opt->name) != 0)
1639 if (opt->len == 0 ||
1640 ((char *)opt->value)[opt->len - 1] != '\0') {
1644 return (opt->value);
1651 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1656 TAILQ_FOREACH(opt, opts, link) {
1657 if (strcmp(name, opt->name) == 0) {
1670 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1676 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1678 TAILQ_FOREACH(opt, opts, link) {
1679 if (strcmp(name, opt->name) != 0)
1682 if (opt->len == 0 || opt->value == NULL)
1684 if (((char *)opt->value)[opt->len - 1] != '\0')
1687 ret = vsscanf(opt->value, fmt, ap);
1695 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1699 TAILQ_FOREACH(opt, opts, link) {
1700 if (strcmp(name, opt->name) != 0)
1703 if (opt->value == NULL)
1706 if (opt->len != len)
1708 bcopy(value, opt->value, len);
1716 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1720 TAILQ_FOREACH(opt, opts, link) {
1721 if (strcmp(name, opt->name) != 0)
1724 if (opt->value == NULL)
1730 bcopy(value, opt->value, len);
1738 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1742 TAILQ_FOREACH(opt, opts, link) {
1743 if (strcmp(name, opt->name) != 0)
1746 if (opt->value == NULL)
1747 opt->len = strlen(value) + 1;
1748 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1756 * Find and copy a mount option.
1758 * The size of the buffer has to be specified
1759 * in len, if it is not the same length as the
1760 * mount option, EINVAL is returned.
1761 * Returns ENOENT if the option is not found.
1764 vfs_copyopt(opts, name, dest, len)
1765 struct vfsoptlist *opts;
1772 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1774 TAILQ_FOREACH(opt, opts, link) {
1775 if (strcmp(name, opt->name) == 0) {
1777 if (len != opt->len)
1779 bcopy(opt->value, dest, opt->len);
1787 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1791 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1792 if (sbp != &mp->mnt_stat)
1793 *sbp = mp->mnt_stat;
1798 vfs_mountedfrom(struct mount *mp, const char *from)
1801 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1802 strlcpy(mp->mnt_stat.f_mntfromname, from,
1803 sizeof mp->mnt_stat.f_mntfromname);
1807 * ---------------------------------------------------------------------
1808 * This is the api for building mount args and mounting filesystems from
1809 * inside the kernel.
1811 * The API works by accumulation of individual args. First error is
1814 * XXX: should be documented in new manpage kernel_mount(9)
1817 /* A memory allocation which must be freed when we are done */
1819 SLIST_ENTRY(mntaarg) next;
1822 /* The header for the mount arguments */
1827 SLIST_HEAD(, mntaarg) list;
1831 * Add a boolean argument.
1833 * flag is the boolean value.
1834 * name must start with "no".
1837 mount_argb(struct mntarg *ma, int flag, const char *name)
1840 KASSERT(name[0] == 'n' && name[1] == 'o',
1841 ("mount_argb(...,%s): name must start with 'no'", name));
1843 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1847 * Add an argument printf style
1850 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1853 struct mntaarg *maa;
1858 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1859 SLIST_INIT(&ma->list);
1864 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1866 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1867 ma->v[ma->len].iov_len = strlen(name) + 1;
1870 sb = sbuf_new_auto();
1872 sbuf_vprintf(sb, fmt, ap);
1875 len = sbuf_len(sb) + 1;
1876 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1877 SLIST_INSERT_HEAD(&ma->list, maa, next);
1878 bcopy(sbuf_data(sb), maa + 1, len);
1881 ma->v[ma->len].iov_base = maa + 1;
1882 ma->v[ma->len].iov_len = len;
1889 * Add an argument which is a userland string.
1892 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1894 struct mntaarg *maa;
1900 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1901 SLIST_INIT(&ma->list);
1905 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1906 SLIST_INSERT_HEAD(&ma->list, maa, next);
1907 tbuf = (void *)(maa + 1);
1908 ma->error = copyinstr(val, tbuf, len, NULL);
1909 return (mount_arg(ma, name, tbuf, -1));
1915 * If length is -1, treat value as a C string.
1918 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1922 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1923 SLIST_INIT(&ma->list);
1928 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1930 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1931 ma->v[ma->len].iov_len = strlen(name) + 1;
1934 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1936 ma->v[ma->len].iov_len = strlen(val) + 1;
1938 ma->v[ma->len].iov_len = len;
1944 * Free a mntarg structure
1947 free_mntarg(struct mntarg *ma)
1949 struct mntaarg *maa;
1951 while (!SLIST_EMPTY(&ma->list)) {
1952 maa = SLIST_FIRST(&ma->list);
1953 SLIST_REMOVE_HEAD(&ma->list, next);
1956 free(ma->v, M_MOUNT);
1961 * Mount a filesystem
1964 kernel_mount(struct mntarg *ma, uint64_t flags)
1969 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1970 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1971 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1973 auio.uio_iov = ma->v;
1974 auio.uio_iovcnt = ma->len;
1975 auio.uio_segflg = UIO_SYSSPACE;
1979 error = vfs_donmount(curthread, flags, &auio);
1985 * A printflike function to mount a filesystem.
1988 kernel_vmount(int flags, ...)
1990 struct mntarg *ma = NULL;
1996 va_start(ap, flags);
1998 cp = va_arg(ap, const char *);
2001 vp = va_arg(ap, const void *);
2002 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2006 error = kernel_mount(ma, flags);
2011 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2014 bcopy(oexp, exp, sizeof(*oexp));
2015 exp->ex_numsecflavors = 0;