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>
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 static int default_autoro = false;
82 SYSCTL_INT(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
83 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
85 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
86 static uma_zone_t mount_zone;
88 /* List of mounted filesystems. */
89 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
91 /* For any iteration/modification of mountlist */
92 struct mtx mountlist_mtx;
93 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
96 * Global opts, taken by all filesystems
98 static const char *global_opts[] = {
110 mount_init(void *mem, int size, int flags)
114 mp = (struct mount *)mem;
115 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
116 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
121 mount_fini(void *mem, int size)
125 mp = (struct mount *)mem;
126 lockdestroy(&mp->mnt_explock);
127 mtx_destroy(&mp->mnt_mtx);
131 vfs_mount_init(void *dummy __unused)
134 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
135 NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
137 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
140 * ---------------------------------------------------------------------
141 * Functions for building and sanitizing the mount options
144 /* Remove one mount option. */
146 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
149 TAILQ_REMOVE(opts, opt, link);
150 free(opt->name, M_MOUNT);
151 if (opt->value != NULL)
152 free(opt->value, M_MOUNT);
156 /* Release all resources related to the mount options. */
158 vfs_freeopts(struct vfsoptlist *opts)
162 while (!TAILQ_EMPTY(opts)) {
163 opt = TAILQ_FIRST(opts);
164 vfs_freeopt(opts, opt);
170 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
172 struct vfsopt *opt, *temp;
176 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
177 if (strcmp(opt->name, name) == 0)
178 vfs_freeopt(opts, opt);
183 vfs_isopt_ro(const char *opt)
186 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
187 strcmp(opt, "norw") == 0)
193 vfs_isopt_rw(const char *opt)
196 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
202 * Check if options are equal (with or without the "no" prefix).
205 vfs_equalopts(const char *opt1, const char *opt2)
209 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
210 if (strcmp(opt1, opt2) == 0)
212 /* "noopt" vs. "opt" */
213 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
215 /* "opt" vs. "noopt" */
216 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
218 while ((p = strchr(opt1, '.')) != NULL &&
219 !strncmp(opt1, opt2, ++p - opt1)) {
222 /* "foo.noopt" vs. "foo.opt" */
223 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
225 /* "foo.opt" vs. "foo.noopt" */
226 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
229 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
230 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
231 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
237 * If a mount option is specified several times,
238 * (with or without the "no" prefix) only keep
239 * the last occurrence of it.
242 vfs_sanitizeopts(struct vfsoptlist *opts)
244 struct vfsopt *opt, *opt2, *tmp;
246 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
247 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
248 while (opt2 != NULL) {
249 if (vfs_equalopts(opt->name, opt2->name)) {
250 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
251 vfs_freeopt(opts, opt2);
254 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
261 * Build a linked list of mount options from a struct uio.
264 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
266 struct vfsoptlist *opts;
268 size_t memused, namelen, optlen;
269 unsigned int i, iovcnt;
272 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
275 iovcnt = auio->uio_iovcnt;
276 for (i = 0; i < iovcnt; i += 2) {
277 namelen = auio->uio_iov[i].iov_len;
278 optlen = auio->uio_iov[i + 1].iov_len;
279 memused += sizeof(struct vfsopt) + optlen + namelen;
281 * Avoid consuming too much memory, and attempts to overflow
284 if (memused > VFS_MOUNTARG_SIZE_MAX ||
285 optlen > VFS_MOUNTARG_SIZE_MAX ||
286 namelen > VFS_MOUNTARG_SIZE_MAX) {
291 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
292 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
299 * Do this early, so jumps to "bad" will free the current
302 TAILQ_INSERT_TAIL(opts, opt, link);
304 if (auio->uio_segflg == UIO_SYSSPACE) {
305 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
307 error = copyin(auio->uio_iov[i].iov_base, opt->name,
312 /* Ensure names are null-terminated strings. */
313 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
319 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
320 if (auio->uio_segflg == UIO_SYSSPACE) {
321 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
324 error = copyin(auio->uio_iov[i + 1].iov_base,
331 vfs_sanitizeopts(opts);
340 * Merge the old mount options with the new ones passed
341 * in the MNT_UPDATE case.
343 * XXX: This function will keep a "nofoo" option in the new
344 * options. E.g, if the option's canonical name is "foo",
345 * "nofoo" ends up in the mount point's active options.
348 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
350 struct vfsopt *opt, *new;
352 TAILQ_FOREACH(opt, oldopts, link) {
353 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
354 new->name = strdup(opt->name, M_MOUNT);
356 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
357 bcopy(opt->value, new->value, opt->len);
361 new->seen = opt->seen;
362 TAILQ_INSERT_HEAD(toopts, new, link);
364 vfs_sanitizeopts(toopts);
368 * Mount a filesystem.
373 struct nmount_args /* {
385 * Mount flags are now 64-bits. On 32-bit archtectures only
386 * 32-bits are passed in, but from here on everything handles
387 * 64-bit flags correctly.
391 AUDIT_ARG_FFLAGS(flags);
392 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
393 uap->iovp, uap->iovcnt, flags);
396 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
397 * userspace to set this flag, but we must filter it out if we want
398 * MNT_UPDATE on the root file system to work.
399 * MNT_ROOTFS should only be set by the kernel when mounting its
402 flags &= ~MNT_ROOTFS;
404 iovcnt = uap->iovcnt;
406 * Check that we have an even number of iovec's
407 * and that we have at least two options.
409 if ((iovcnt & 1) || (iovcnt < 4)) {
410 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
415 error = copyinuio(uap->iovp, iovcnt, &auio);
417 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
421 error = vfs_donmount(td, flags, auio);
428 * ---------------------------------------------------------------------
429 * Various utility functions
433 vfs_ref(struct mount *mp)
436 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
443 vfs_rel(struct mount *mp)
446 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
453 * Allocate and initialize the mount point struct.
456 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
461 mp = uma_zalloc(mount_zone, M_WAITOK);
462 bzero(&mp->mnt_startzero,
463 __rangeof(struct mount, mnt_startzero, mnt_endzero));
464 TAILQ_INIT(&mp->mnt_nvnodelist);
465 mp->mnt_nvnodelistsize = 0;
466 TAILQ_INIT(&mp->mnt_activevnodelist);
467 mp->mnt_activevnodelistsize = 0;
469 (void) vfs_busy(mp, MBF_NOWAIT);
470 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
471 mp->mnt_op = vfsp->vfc_vfsops;
473 mp->mnt_stat.f_type = vfsp->vfc_typenum;
475 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
476 mp->mnt_vnodecovered = vp;
477 mp->mnt_cred = crdup(cred);
478 mp->mnt_stat.f_owner = cred->cr_uid;
479 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
480 mp->mnt_iosize_max = DFLTPHYS;
483 mac_mount_create(cred, mp);
485 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
486 TAILQ_INIT(&mp->mnt_uppers);
491 * Destroy the mount struct previously allocated by vfs_mount_alloc().
494 vfs_mount_destroy(struct mount *mp)
498 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
499 if (mp->mnt_kern_flag & MNTK_MWAIT) {
500 mp->mnt_kern_flag &= ~MNTK_MWAIT;
504 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
505 KASSERT(mp->mnt_ref == 0,
506 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
507 __FILE__, __LINE__));
508 if (mp->mnt_writeopcount != 0)
509 panic("vfs_mount_destroy: nonzero writeopcount");
510 if (mp->mnt_secondary_writes != 0)
511 panic("vfs_mount_destroy: nonzero secondary_writes");
512 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
513 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
516 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
518 panic("unmount: dangling vnode");
520 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
521 if (mp->mnt_nvnodelistsize != 0)
522 panic("vfs_mount_destroy: nonzero nvnodelistsize");
523 if (mp->mnt_activevnodelistsize != 0)
524 panic("vfs_mount_destroy: nonzero activevnodelistsize");
525 if (mp->mnt_lockref != 0)
526 panic("vfs_mount_destroy: nonzero lock refcount");
528 if (mp->mnt_vnodecovered != NULL)
529 vrele(mp->mnt_vnodecovered);
531 mac_mount_destroy(mp);
533 if (mp->mnt_opt != NULL)
534 vfs_freeopts(mp->mnt_opt);
535 crfree(mp->mnt_cred);
536 uma_zfree(mount_zone, mp);
540 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
542 /* This is an upgrade of an exisiting mount. */
543 if ((fsflags & MNT_UPDATE) != 0)
545 /* This is already an R/O mount. */
546 if ((fsflags & MNT_RDONLY) != 0)
550 case ENODEV: /* generic, geom, ... */
551 case EACCES: /* cam/scsi, ... */
552 case EROFS: /* md, mmcsd, ... */
554 * These errors can be returned by the storage layer to signal
555 * that the media is read-only. No harm in the R/O mount
556 * attempt if the error was returned for some other reason.
565 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
567 struct vfsoptlist *optlist;
568 struct vfsopt *opt, *tmp_opt;
569 char *fstype, *fspath, *errmsg;
570 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
573 errmsg = fspath = NULL;
574 errmsg_len = fspathlen = 0;
576 autoro = default_autoro;
578 error = vfs_buildopts(fsoptions, &optlist);
582 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
583 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
586 * We need these two options before the others,
587 * and they are mandatory for any filesystem.
588 * Ensure they are NUL terminated as well.
591 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
592 if (error || fstype[fstypelen - 1] != '\0') {
595 strncpy(errmsg, "Invalid fstype", errmsg_len);
599 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
600 if (error || fspath[fspathlen - 1] != '\0') {
603 strncpy(errmsg, "Invalid fspath", errmsg_len);
608 * We need to see if we have the "update" option
609 * before we call vfs_domount(), since vfs_domount() has special
610 * logic based on MNT_UPDATE. This is very important
611 * when we want to update the root filesystem.
613 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
614 if (strcmp(opt->name, "update") == 0) {
615 fsflags |= MNT_UPDATE;
616 vfs_freeopt(optlist, opt);
618 else if (strcmp(opt->name, "async") == 0)
619 fsflags |= MNT_ASYNC;
620 else if (strcmp(opt->name, "force") == 0) {
621 fsflags |= MNT_FORCE;
622 vfs_freeopt(optlist, opt);
624 else if (strcmp(opt->name, "reload") == 0) {
625 fsflags |= MNT_RELOAD;
626 vfs_freeopt(optlist, opt);
628 else if (strcmp(opt->name, "multilabel") == 0)
629 fsflags |= MNT_MULTILABEL;
630 else if (strcmp(opt->name, "noasync") == 0)
631 fsflags &= ~MNT_ASYNC;
632 else if (strcmp(opt->name, "noatime") == 0)
633 fsflags |= MNT_NOATIME;
634 else if (strcmp(opt->name, "atime") == 0) {
635 free(opt->name, M_MOUNT);
636 opt->name = strdup("nonoatime", M_MOUNT);
638 else if (strcmp(opt->name, "noclusterr") == 0)
639 fsflags |= MNT_NOCLUSTERR;
640 else if (strcmp(opt->name, "clusterr") == 0) {
641 free(opt->name, M_MOUNT);
642 opt->name = strdup("nonoclusterr", M_MOUNT);
644 else if (strcmp(opt->name, "noclusterw") == 0)
645 fsflags |= MNT_NOCLUSTERW;
646 else if (strcmp(opt->name, "clusterw") == 0) {
647 free(opt->name, M_MOUNT);
648 opt->name = strdup("nonoclusterw", M_MOUNT);
650 else if (strcmp(opt->name, "noexec") == 0)
651 fsflags |= MNT_NOEXEC;
652 else if (strcmp(opt->name, "exec") == 0) {
653 free(opt->name, M_MOUNT);
654 opt->name = strdup("nonoexec", M_MOUNT);
656 else if (strcmp(opt->name, "nosuid") == 0)
657 fsflags |= MNT_NOSUID;
658 else if (strcmp(opt->name, "suid") == 0) {
659 free(opt->name, M_MOUNT);
660 opt->name = strdup("nonosuid", M_MOUNT);
662 else if (strcmp(opt->name, "nosymfollow") == 0)
663 fsflags |= MNT_NOSYMFOLLOW;
664 else if (strcmp(opt->name, "symfollow") == 0) {
665 free(opt->name, M_MOUNT);
666 opt->name = strdup("nonosymfollow", M_MOUNT);
668 else if (strcmp(opt->name, "noro") == 0) {
669 fsflags &= ~MNT_RDONLY;
672 else if (strcmp(opt->name, "rw") == 0) {
673 fsflags &= ~MNT_RDONLY;
676 else if (strcmp(opt->name, "ro") == 0) {
677 fsflags |= MNT_RDONLY;
680 else if (strcmp(opt->name, "rdonly") == 0) {
681 free(opt->name, M_MOUNT);
682 opt->name = strdup("ro", M_MOUNT);
683 fsflags |= MNT_RDONLY;
686 else if (strcmp(opt->name, "autoro") == 0) {
687 vfs_freeopt(optlist, opt);
690 else if (strcmp(opt->name, "suiddir") == 0)
691 fsflags |= MNT_SUIDDIR;
692 else if (strcmp(opt->name, "sync") == 0)
693 fsflags |= MNT_SYNCHRONOUS;
694 else if (strcmp(opt->name, "union") == 0)
695 fsflags |= MNT_UNION;
696 else if (strcmp(opt->name, "automounted") == 0) {
697 fsflags |= MNT_AUTOMOUNTED;
698 vfs_freeopt(optlist, opt);
703 * Be ultra-paranoid about making sure the type and fspath
704 * variables will fit in our mp buffers, including the
707 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
708 error = ENAMETOOLONG;
712 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
715 * See if we can mount in the read-only mode if the error code suggests
716 * that it could be possible and the mount options allow for that.
717 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
718 * overridden by "autoro".
720 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
721 printf("%s: R/W mount failed, possibly R/O media,"
722 " trying R/O mount\n", __func__);
723 fsflags |= MNT_RDONLY;
724 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
727 /* copyout the errmsg */
728 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
729 && errmsg_len > 0 && errmsg != NULL) {
730 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
732 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
733 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
736 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
737 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
742 vfs_freeopts(optlist);
749 #ifndef _SYS_SYSPROTO_H_
761 struct mount_args /* {
769 struct vfsconf *vfsp = NULL;
770 struct mntarg *ma = NULL;
775 * Mount flags are now 64-bits. On 32-bit architectures only
776 * 32-bits are passed in, but from here on everything handles
777 * 64-bit flags correctly.
781 AUDIT_ARG_FFLAGS(flags);
784 * Filter out MNT_ROOTFS. We do not want clients of mount() in
785 * userspace to set this flag, but we must filter it out if we want
786 * MNT_UPDATE on the root file system to work.
787 * MNT_ROOTFS should only be set by the kernel when mounting its
790 flags &= ~MNT_ROOTFS;
792 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
793 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
795 free(fstype, M_TEMP);
799 AUDIT_ARG_TEXT(fstype);
800 vfsp = vfs_byname_kld(fstype, td, &error);
801 free(fstype, M_TEMP);
804 if (vfsp->vfc_vfsops->vfs_cmount == NULL)
807 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
808 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
809 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
810 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
811 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
813 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags);
818 * vfs_domount_first(): first file system mount (not update)
822 struct thread *td, /* Calling thread. */
823 struct vfsconf *vfsp, /* File system type. */
824 char *fspath, /* Mount path. */
825 struct vnode *vp, /* Vnode to be covered. */
826 uint64_t fsflags, /* Flags common to all filesystems. */
827 struct vfsoptlist **optlist /* Options local to the filesystem. */
835 ASSERT_VOP_ELOCKED(vp, __func__);
836 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
839 * If the user is not root, ensure that they own the directory
840 * onto which we are attempting to mount.
842 error = VOP_GETATTR(vp, &va, td->td_ucred);
843 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
844 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 0);
846 error = vinvalbuf(vp, V_SAVE, 0, 0);
847 if (error == 0 && vp->v_type != VDIR)
851 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
852 vp->v_iflag |= VI_MOUNT;
863 /* Allocate and initialize the filesystem. */
864 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
865 /* XXXMAC: pass to vfs_mount_alloc? */
866 mp->mnt_optnew = *optlist;
867 /* Set the mount level flags. */
868 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
871 * Mount the filesystem.
872 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
873 * get. No freeing of cn_pnbuf.
875 error = VFS_MOUNT(mp);
878 mp->mnt_vnodecovered = NULL;
879 vfs_mount_destroy(mp);
881 vp->v_iflag &= ~VI_MOUNT;
887 if (mp->mnt_opt != NULL)
888 vfs_freeopts(mp->mnt_opt);
889 mp->mnt_opt = mp->mnt_optnew;
891 (void)VFS_STATFS(mp, &mp->mnt_stat);
894 * Prevent external consumers of mount options from reading mnt_optnew.
896 mp->mnt_optnew = NULL;
899 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
900 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
901 mp->mnt_kern_flag |= MNTK_ASYNC;
903 mp->mnt_kern_flag &= ~MNTK_ASYNC;
906 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
909 vp->v_iflag &= ~VI_MOUNT;
911 vp->v_mountedhere = mp;
912 /* Place the new filesystem at the end of the mount list. */
913 mtx_lock(&mountlist_mtx);
914 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
915 mtx_unlock(&mountlist_mtx);
916 vfs_event_signal(NULL, VQ_MOUNT, 0);
917 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
918 panic("mount: lost mount");
920 EVENTHANDLER_INVOKE(vfs_mounted, mp, newdp, td);
921 VOP_UNLOCK(newdp, 0);
922 mountcheckdirs(vp, newdp);
924 if ((mp->mnt_flag & MNT_RDONLY) == 0)
925 vfs_allocate_syncvnode(mp);
931 * vfs_domount_update(): update of mounted file system
935 struct thread *td, /* Calling thread. */
936 struct vnode *vp, /* Mount point vnode. */
937 uint64_t fsflags, /* Flags common to all filesystems. */
938 struct vfsoptlist **optlist /* Options local to the filesystem. */
941 struct oexport_args oexport;
942 struct export_args export;
944 int error, export_error;
947 ASSERT_VOP_ELOCKED(vp, __func__);
948 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
951 if ((vp->v_vflag & VV_ROOT) == 0) {
952 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
962 * We only allow the filesystem to be reloaded if it
963 * is currently mounted read-only.
966 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
968 return (EOPNOTSUPP); /* Needs translation */
971 * Only privileged root, or (if MNT_USER is set) the user that
972 * did the original mount is permitted to update it.
974 error = vfs_suser(mp, td);
979 if (vfs_busy(mp, MBF_NOWAIT)) {
984 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
990 vp->v_iflag |= VI_MOUNT;
995 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1000 mp->mnt_flag &= ~MNT_UPDATEMASK;
1001 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1002 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1003 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1004 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1006 mp->mnt_optnew = *optlist;
1007 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1010 * Mount the filesystem.
1011 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1012 * get. No freeing of cn_pnbuf.
1014 error = VFS_MOUNT(mp);
1018 /* Process the export option. */
1019 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1020 sizeof(export)) == 0) {
1021 export_error = vfs_export(mp, &export);
1022 } else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
1023 sizeof(oexport)) == 0) {
1024 export.ex_flags = oexport.ex_flags;
1025 export.ex_root = oexport.ex_root;
1026 export.ex_anon = oexport.ex_anon;
1027 export.ex_addr = oexport.ex_addr;
1028 export.ex_addrlen = oexport.ex_addrlen;
1029 export.ex_mask = oexport.ex_mask;
1030 export.ex_masklen = oexport.ex_masklen;
1031 export.ex_indexfile = oexport.ex_indexfile;
1032 export.ex_numsecflavors = 0;
1033 export_error = vfs_export(mp, &export);
1039 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1043 * If we fail, restore old mount flags. MNT_QUOTA is special,
1044 * because it is not part of MNT_UPDATEMASK, but it could have
1045 * changed in the meantime if quotactl(2) was called.
1046 * All in all we want current value of MNT_QUOTA, not the old
1049 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1051 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1052 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1053 mp->mnt_kern_flag |= MNTK_ASYNC;
1055 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1061 if (mp->mnt_opt != NULL)
1062 vfs_freeopts(mp->mnt_opt);
1063 mp->mnt_opt = mp->mnt_optnew;
1065 (void)VFS_STATFS(mp, &mp->mnt_stat);
1067 * Prevent external consumers of mount options from reading
1070 mp->mnt_optnew = NULL;
1072 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1073 vfs_allocate_syncvnode(mp);
1075 vfs_deallocate_syncvnode(mp);
1079 vp->v_iflag &= ~VI_MOUNT;
1082 return (error != 0 ? error : export_error);
1086 * vfs_domount(): actually attempt a filesystem mount.
1090 struct thread *td, /* Calling thread. */
1091 const char *fstype, /* Filesystem type. */
1092 char *fspath, /* Mount path. */
1093 uint64_t fsflags, /* Flags common to all filesystems. */
1094 struct vfsoptlist **optlist /* Options local to the filesystem. */
1097 struct vfsconf *vfsp;
1098 struct nameidata nd;
1104 * Be ultra-paranoid about making sure the type and fspath
1105 * variables will fit in our mp buffers, including the
1108 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1109 return (ENAMETOOLONG);
1111 if (jailed(td->td_ucred) || usermount == 0) {
1112 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1117 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1119 if (fsflags & MNT_EXPORTED) {
1120 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1124 if (fsflags & MNT_SUIDDIR) {
1125 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1130 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1132 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1133 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1134 fsflags |= MNT_NOSUID | MNT_USER;
1137 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1139 if ((fsflags & MNT_UPDATE) == 0) {
1140 /* Don't try to load KLDs if we're mounting the root. */
1141 if (fsflags & MNT_ROOTFS)
1142 vfsp = vfs_byname(fstype);
1144 vfsp = vfs_byname_kld(fstype, td, &error);
1147 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
1152 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1154 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1155 UIO_SYSSPACE, fspath, td);
1159 NDFREE(&nd, NDF_ONLY_PNBUF);
1161 if ((fsflags & MNT_UPDATE) == 0) {
1162 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1163 strcpy(pathbuf, fspath);
1164 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1165 /* debug.disablefullpath == 1 results in ENODEV */
1166 if (error == 0 || error == ENODEV) {
1167 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1170 free(pathbuf, M_TEMP);
1172 error = vfs_domount_update(td, vp, fsflags, optlist);
1178 * Unmount a filesystem.
1180 * Note: unmount takes a path to the vnode mounted on as argument, not
1181 * special file (as before).
1183 #ifndef _SYS_SYSPROTO_H_
1184 struct unmount_args {
1191 sys_unmount(struct thread *td, struct unmount_args *uap)
1193 struct nameidata nd;
1196 int error, id0, id1;
1198 AUDIT_ARG_VALUE(uap->flags);
1199 if (jailed(td->td_ucred) || usermount == 0) {
1200 error = priv_check(td, PRIV_VFS_UNMOUNT);
1205 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1206 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1208 free(pathbuf, M_TEMP);
1211 if (uap->flags & MNT_BYFSID) {
1212 AUDIT_ARG_TEXT(pathbuf);
1213 /* Decode the filesystem ID. */
1214 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1215 free(pathbuf, M_TEMP);
1219 mtx_lock(&mountlist_mtx);
1220 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1221 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1222 mp->mnt_stat.f_fsid.val[1] == id1) {
1227 mtx_unlock(&mountlist_mtx);
1230 * Try to find global path for path argument.
1232 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1233 UIO_SYSSPACE, pathbuf, td);
1234 if (namei(&nd) == 0) {
1235 NDFREE(&nd, NDF_ONLY_PNBUF);
1236 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1238 if (error == 0 || error == ENODEV)
1241 mtx_lock(&mountlist_mtx);
1242 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1243 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1248 mtx_unlock(&mountlist_mtx);
1250 free(pathbuf, M_TEMP);
1253 * Previously we returned ENOENT for a nonexistent path and
1254 * EINVAL for a non-mountpoint. We cannot tell these apart
1255 * now, so in the !MNT_BYFSID case return the more likely
1256 * EINVAL for compatibility.
1258 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1262 * Don't allow unmounting the root filesystem.
1264 if (mp->mnt_flag & MNT_ROOTFS) {
1268 error = dounmount(mp, uap->flags, td);
1273 * Do the actual filesystem unmount.
1276 dounmount(struct mount *mp, int flags, struct thread *td)
1278 struct vnode *coveredvp, *fsrootvp;
1280 uint64_t async_flag;
1283 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1284 mnt_gen_r = mp->mnt_gen;
1287 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1289 * Check for mp being unmounted while waiting for the
1290 * covered vnode lock.
1292 if (coveredvp->v_mountedhere != mp ||
1293 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1294 VOP_UNLOCK(coveredvp, 0);
1302 * Only privileged root, or (if MNT_USER is set) the user that did the
1303 * original mount is permitted to unmount this filesystem.
1305 error = vfs_suser(mp, td);
1307 if (coveredvp != NULL) {
1308 VOP_UNLOCK(coveredvp, 0);
1315 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1317 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1318 (mp->mnt_flag & MNT_UPDATE) != 0 ||
1319 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1321 if (coveredvp != NULL) {
1322 VOP_UNLOCK(coveredvp, 0);
1325 vn_finished_write(mp);
1328 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1329 /* Allow filesystems to detect that a forced unmount is in progress. */
1330 if (flags & MNT_FORCE) {
1331 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1334 * Must be done after setting MNTK_UNMOUNTF and before
1335 * waiting for mnt_lockref to become 0.
1341 if (mp->mnt_lockref) {
1342 mp->mnt_kern_flag |= MNTK_DRAINING;
1343 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1347 KASSERT(mp->mnt_lockref == 0,
1348 ("%s: invalid lock refcount in the drain path @ %s:%d",
1349 __func__, __FILE__, __LINE__));
1351 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1352 __func__, __FILE__, __LINE__));
1354 if (mp->mnt_flag & MNT_EXPUBLIC)
1355 vfs_setpublicfs(NULL, NULL, NULL);
1358 * From now, we can claim that the use reference on the
1359 * coveredvp is ours, and the ref can be released only by
1360 * successfull unmount by us, or left for later unmount
1361 * attempt. The previously acquired hold reference is no
1362 * longer needed to protect the vnode from reuse.
1364 if (coveredvp != NULL)
1367 vfs_msync(mp, MNT_WAIT);
1369 async_flag = mp->mnt_flag & MNT_ASYNC;
1370 mp->mnt_flag &= ~MNT_ASYNC;
1371 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1373 cache_purgevfs(mp); /* remove cache entries for this file sys */
1374 vfs_deallocate_syncvnode(mp);
1376 * For forced unmounts, move process cdir/rdir refs on the fs root
1377 * vnode to the covered vnode. For non-forced unmounts we want
1378 * such references to cause an EBUSY error.
1380 if ((flags & MNT_FORCE) &&
1381 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1382 if (mp->mnt_vnodecovered != NULL &&
1383 (mp->mnt_flag & MNT_IGNORE) == 0)
1384 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1385 if (fsrootvp == rootvnode) {
1391 if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 ||
1392 (error = VFS_SYNC(mp, MNT_WAIT)) == 0)
1393 error = VFS_UNMOUNT(mp, flags);
1394 vn_finished_write(mp);
1396 * If we failed to flush the dirty blocks for this mount point,
1397 * undo all the cdir/rdir and rootvnode changes we made above.
1398 * Unless we failed to do so because the device is reporting that
1399 * it doesn't exist anymore.
1401 if (error && error != ENXIO) {
1402 if ((flags & MNT_FORCE) &&
1403 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1404 if (mp->mnt_vnodecovered != NULL &&
1405 (mp->mnt_flag & MNT_IGNORE) == 0)
1406 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1407 if (rootvnode == NULL) {
1408 rootvnode = fsrootvp;
1414 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1415 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1417 vfs_allocate_syncvnode(mp);
1420 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1421 mp->mnt_flag |= async_flag;
1422 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1423 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1424 mp->mnt_kern_flag |= MNTK_ASYNC;
1425 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1426 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1431 VOP_UNLOCK(coveredvp, 0);
1434 mtx_lock(&mountlist_mtx);
1435 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1436 mtx_unlock(&mountlist_mtx);
1437 EVENTHANDLER_INVOKE(vfs_unmounted, mp, td);
1438 if (coveredvp != NULL) {
1439 coveredvp->v_mountedhere = NULL;
1440 VOP_UNLOCK(coveredvp, 0);
1442 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1443 if (mp == rootdevmp)
1445 vfs_mount_destroy(mp);
1450 * Report errors during filesystem mounting.
1453 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1455 struct vfsoptlist *moptlist = mp->mnt_optnew;
1460 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1461 if (error || errmsg == NULL || len <= 0)
1465 vsnprintf(errmsg, (size_t)len, fmt, ap);
1470 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1476 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1477 if (error || errmsg == NULL || len <= 0)
1481 vsnprintf(errmsg, (size_t)len, fmt, ap);
1486 * ---------------------------------------------------------------------
1487 * Functions for querying mount options/arguments from filesystems.
1491 * Check that no unknown options are given
1494 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1498 const char **t, *p, *q;
1501 TAILQ_FOREACH(opt, opts, link) {
1504 if (p[0] == 'n' && p[1] == 'o')
1506 for(t = global_opts; *t != NULL; t++) {
1507 if (strcmp(*t, p) == 0)
1510 if (strcmp(*t, q) == 0)
1516 for(t = legal; *t != NULL; t++) {
1517 if (strcmp(*t, p) == 0)
1520 if (strcmp(*t, q) == 0)
1526 snprintf(errmsg, sizeof(errmsg),
1527 "mount option <%s> is unknown", p);
1531 TAILQ_FOREACH(opt, opts, link) {
1532 if (strcmp(opt->name, "errmsg") == 0) {
1533 strncpy((char *)opt->value, errmsg, opt->len);
1538 printf("%s\n", errmsg);
1544 * Get a mount option by its name.
1546 * Return 0 if the option was found, ENOENT otherwise.
1547 * If len is non-NULL it will be filled with the length
1548 * of the option. If buf is non-NULL, it will be filled
1549 * with the address of the option.
1552 vfs_getopt(opts, name, buf, len)
1553 struct vfsoptlist *opts;
1560 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1562 TAILQ_FOREACH(opt, opts, link) {
1563 if (strcmp(name, opt->name) == 0) {
1576 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1583 TAILQ_FOREACH(opt, opts, link) {
1584 if (strcmp(name, opt->name) == 0) {
1593 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1595 char *opt_value, *vtp;
1599 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1602 if (opt_len == 0 || opt_value == NULL)
1604 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1606 iv = strtoq(opt_value, &vtp, 0);
1607 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
1635 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1640 TAILQ_FOREACH(opt, opts, link) {
1641 if (strcmp(name, opt->name) != 0)
1644 if (opt->len == 0 ||
1645 ((char *)opt->value)[opt->len - 1] != '\0') {
1649 return (opt->value);
1656 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1661 TAILQ_FOREACH(opt, opts, link) {
1662 if (strcmp(name, opt->name) == 0) {
1675 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1681 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1683 TAILQ_FOREACH(opt, opts, link) {
1684 if (strcmp(name, opt->name) != 0)
1687 if (opt->len == 0 || opt->value == NULL)
1689 if (((char *)opt->value)[opt->len - 1] != '\0')
1692 ret = vsscanf(opt->value, fmt, ap);
1700 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1704 TAILQ_FOREACH(opt, opts, link) {
1705 if (strcmp(name, opt->name) != 0)
1708 if (opt->value == NULL)
1711 if (opt->len != len)
1713 bcopy(value, opt->value, len);
1721 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1725 TAILQ_FOREACH(opt, opts, link) {
1726 if (strcmp(name, opt->name) != 0)
1729 if (opt->value == NULL)
1735 bcopy(value, opt->value, len);
1743 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1747 TAILQ_FOREACH(opt, opts, link) {
1748 if (strcmp(name, opt->name) != 0)
1751 if (opt->value == NULL)
1752 opt->len = strlen(value) + 1;
1753 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1761 * Find and copy a mount option.
1763 * The size of the buffer has to be specified
1764 * in len, if it is not the same length as the
1765 * mount option, EINVAL is returned.
1766 * Returns ENOENT if the option is not found.
1769 vfs_copyopt(opts, name, dest, len)
1770 struct vfsoptlist *opts;
1777 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1779 TAILQ_FOREACH(opt, opts, link) {
1780 if (strcmp(name, opt->name) == 0) {
1782 if (len != opt->len)
1784 bcopy(opt->value, dest, opt->len);
1792 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1796 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1797 if (sbp != &mp->mnt_stat)
1798 *sbp = mp->mnt_stat;
1803 vfs_mountedfrom(struct mount *mp, const char *from)
1806 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1807 strlcpy(mp->mnt_stat.f_mntfromname, from,
1808 sizeof mp->mnt_stat.f_mntfromname);
1812 * ---------------------------------------------------------------------
1813 * This is the api for building mount args and mounting filesystems from
1814 * inside the kernel.
1816 * The API works by accumulation of individual args. First error is
1819 * XXX: should be documented in new manpage kernel_mount(9)
1822 /* A memory allocation which must be freed when we are done */
1824 SLIST_ENTRY(mntaarg) next;
1827 /* The header for the mount arguments */
1832 SLIST_HEAD(, mntaarg) list;
1836 * Add a boolean argument.
1838 * flag is the boolean value.
1839 * name must start with "no".
1842 mount_argb(struct mntarg *ma, int flag, const char *name)
1845 KASSERT(name[0] == 'n' && name[1] == 'o',
1846 ("mount_argb(...,%s): name must start with 'no'", name));
1848 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1852 * Add an argument printf style
1855 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1858 struct mntaarg *maa;
1863 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1864 SLIST_INIT(&ma->list);
1869 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1871 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1872 ma->v[ma->len].iov_len = strlen(name) + 1;
1875 sb = sbuf_new_auto();
1877 sbuf_vprintf(sb, fmt, ap);
1880 len = sbuf_len(sb) + 1;
1881 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1882 SLIST_INSERT_HEAD(&ma->list, maa, next);
1883 bcopy(sbuf_data(sb), maa + 1, len);
1886 ma->v[ma->len].iov_base = maa + 1;
1887 ma->v[ma->len].iov_len = len;
1894 * Add an argument which is a userland string.
1897 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1899 struct mntaarg *maa;
1905 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1906 SLIST_INIT(&ma->list);
1910 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1911 SLIST_INSERT_HEAD(&ma->list, maa, next);
1912 tbuf = (void *)(maa + 1);
1913 ma->error = copyinstr(val, tbuf, len, NULL);
1914 return (mount_arg(ma, name, tbuf, -1));
1920 * If length is -1, treat value as a C string.
1923 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1927 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1928 SLIST_INIT(&ma->list);
1933 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1935 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1936 ma->v[ma->len].iov_len = strlen(name) + 1;
1939 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1941 ma->v[ma->len].iov_len = strlen(val) + 1;
1943 ma->v[ma->len].iov_len = len;
1949 * Free a mntarg structure
1952 free_mntarg(struct mntarg *ma)
1954 struct mntaarg *maa;
1956 while (!SLIST_EMPTY(&ma->list)) {
1957 maa = SLIST_FIRST(&ma->list);
1958 SLIST_REMOVE_HEAD(&ma->list, next);
1961 free(ma->v, M_MOUNT);
1966 * Mount a filesystem
1969 kernel_mount(struct mntarg *ma, uint64_t flags)
1974 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
1975 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
1976 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
1978 auio.uio_iov = ma->v;
1979 auio.uio_iovcnt = ma->len;
1980 auio.uio_segflg = UIO_SYSSPACE;
1984 error = vfs_donmount(curthread, flags, &auio);
1990 * A printflike function to mount a filesystem.
1993 kernel_vmount(int flags, ...)
1995 struct mntarg *ma = NULL;
2001 va_start(ap, flags);
2003 cp = va_arg(ap, const char *);
2006 vp = va_arg(ap, const void *);
2007 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2011 error = kernel_mount(ma, flags);
2016 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2019 bcopy(oexp, exp, sizeof(*oexp));
2020 exp->ex_numsecflavors = 0;