2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999-2004 Poul-Henning Kamp
5 * Copyright (c) 1999 Michael Smith
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
44 #include <sys/eventhandler.h>
45 #include <sys/fcntl.h>
47 #include <sys/kernel.h>
49 #include <sys/libkern.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/reboot.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysproto.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/systm.h>
65 #include <sys/vnode.h>
68 #include <geom/geom.h>
70 #include <machine/stdarg.h>
72 #include <security/audit/audit.h>
73 #include <security/mac/mac_framework.h>
75 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
77 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
78 uint64_t fsflags, struct vfsoptlist **optlist);
79 static void free_mntarg(struct mntarg *ma);
81 static int usermount = 0;
82 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
83 "Unprivileged users may mount and unmount file systems");
85 static bool default_autoro = false;
86 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
87 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
89 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
90 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
91 static uma_zone_t mount_zone;
93 /* List of mounted filesystems. */
94 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
96 /* For any iteration/modification of mountlist */
97 struct mtx mountlist_mtx;
98 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
100 EVENTHANDLER_LIST_DEFINE(vfs_mounted);
101 EVENTHANDLER_LIST_DEFINE(vfs_unmounted);
104 * Global opts, taken by all filesystems
106 static const char *global_opts[] = {
118 mount_init(void *mem, int size, int flags)
122 mp = (struct mount *)mem;
123 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
124 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
125 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
130 mount_fini(void *mem, int size)
134 mp = (struct mount *)mem;
135 lockdestroy(&mp->mnt_explock);
136 mtx_destroy(&mp->mnt_listmtx);
137 mtx_destroy(&mp->mnt_mtx);
141 vfs_mount_init(void *dummy __unused)
144 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
145 NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
147 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
150 * ---------------------------------------------------------------------
151 * Functions for building and sanitizing the mount options
154 /* Remove one mount option. */
156 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
159 TAILQ_REMOVE(opts, opt, link);
160 free(opt->name, M_MOUNT);
161 if (opt->value != NULL)
162 free(opt->value, M_MOUNT);
166 /* Release all resources related to the mount options. */
168 vfs_freeopts(struct vfsoptlist *opts)
172 while (!TAILQ_EMPTY(opts)) {
173 opt = TAILQ_FIRST(opts);
174 vfs_freeopt(opts, opt);
180 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
182 struct vfsopt *opt, *temp;
186 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
187 if (strcmp(opt->name, name) == 0)
188 vfs_freeopt(opts, opt);
193 vfs_isopt_ro(const char *opt)
196 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
197 strcmp(opt, "norw") == 0)
203 vfs_isopt_rw(const char *opt)
206 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
212 * Check if options are equal (with or without the "no" prefix).
215 vfs_equalopts(const char *opt1, const char *opt2)
219 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
220 if (strcmp(opt1, opt2) == 0)
222 /* "noopt" vs. "opt" */
223 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
225 /* "opt" vs. "noopt" */
226 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
228 while ((p = strchr(opt1, '.')) != NULL &&
229 !strncmp(opt1, opt2, ++p - opt1)) {
232 /* "foo.noopt" vs. "foo.opt" */
233 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
235 /* "foo.opt" vs. "foo.noopt" */
236 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
239 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
240 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
241 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
247 * If a mount option is specified several times,
248 * (with or without the "no" prefix) only keep
249 * the last occurrence of it.
252 vfs_sanitizeopts(struct vfsoptlist *opts)
254 struct vfsopt *opt, *opt2, *tmp;
256 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
257 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
258 while (opt2 != NULL) {
259 if (vfs_equalopts(opt->name, opt2->name)) {
260 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
261 vfs_freeopt(opts, opt2);
264 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
271 * Build a linked list of mount options from a struct uio.
274 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
276 struct vfsoptlist *opts;
278 size_t memused, namelen, optlen;
279 unsigned int i, iovcnt;
282 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
285 iovcnt = auio->uio_iovcnt;
286 for (i = 0; i < iovcnt; i += 2) {
287 namelen = auio->uio_iov[i].iov_len;
288 optlen = auio->uio_iov[i + 1].iov_len;
289 memused += sizeof(struct vfsopt) + optlen + namelen;
291 * Avoid consuming too much memory, and attempts to overflow
294 if (memused > VFS_MOUNTARG_SIZE_MAX ||
295 optlen > VFS_MOUNTARG_SIZE_MAX ||
296 namelen > VFS_MOUNTARG_SIZE_MAX) {
301 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
302 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
309 * Do this early, so jumps to "bad" will free the current
312 TAILQ_INSERT_TAIL(opts, opt, link);
314 if (auio->uio_segflg == UIO_SYSSPACE) {
315 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
317 error = copyin(auio->uio_iov[i].iov_base, opt->name,
322 /* Ensure names are null-terminated strings. */
323 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
329 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
330 if (auio->uio_segflg == UIO_SYSSPACE) {
331 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
334 error = copyin(auio->uio_iov[i + 1].iov_base,
341 vfs_sanitizeopts(opts);
350 * Merge the old mount options with the new ones passed
351 * in the MNT_UPDATE case.
353 * XXX: This function will keep a "nofoo" option in the new
354 * options. E.g, if the option's canonical name is "foo",
355 * "nofoo" ends up in the mount point's active options.
358 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
360 struct vfsopt *opt, *new;
362 TAILQ_FOREACH(opt, oldopts, link) {
363 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
364 new->name = strdup(opt->name, M_MOUNT);
366 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
367 bcopy(opt->value, new->value, opt->len);
371 new->seen = opt->seen;
372 TAILQ_INSERT_HEAD(toopts, new, link);
374 vfs_sanitizeopts(toopts);
378 * Mount a filesystem.
380 #ifndef _SYS_SYSPROTO_H_
388 sys_nmount(struct thread *td, struct nmount_args *uap)
396 * Mount flags are now 64-bits. On 32-bit archtectures only
397 * 32-bits are passed in, but from here on everything handles
398 * 64-bit flags correctly.
402 AUDIT_ARG_FFLAGS(flags);
403 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
404 uap->iovp, uap->iovcnt, flags);
407 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
408 * userspace to set this flag, but we must filter it out if we want
409 * MNT_UPDATE on the root file system to work.
410 * MNT_ROOTFS should only be set by the kernel when mounting its
413 flags &= ~MNT_ROOTFS;
415 iovcnt = uap->iovcnt;
417 * Check that we have an even number of iovec's
418 * and that we have at least two options.
420 if ((iovcnt & 1) || (iovcnt < 4)) {
421 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
426 error = copyinuio(uap->iovp, iovcnt, &auio);
428 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
432 error = vfs_donmount(td, flags, auio);
439 * ---------------------------------------------------------------------
440 * Various utility functions
444 vfs_ref(struct mount *mp)
447 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
454 vfs_rel(struct mount *mp)
457 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
464 * Allocate and initialize the mount point struct.
467 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
472 mp = uma_zalloc(mount_zone, M_WAITOK);
473 bzero(&mp->mnt_startzero,
474 __rangeof(struct mount, mnt_startzero, mnt_endzero));
475 TAILQ_INIT(&mp->mnt_nvnodelist);
476 mp->mnt_nvnodelistsize = 0;
477 TAILQ_INIT(&mp->mnt_activevnodelist);
478 mp->mnt_activevnodelistsize = 0;
479 TAILQ_INIT(&mp->mnt_tmpfreevnodelist);
480 mp->mnt_tmpfreevnodelistsize = 0;
482 (void) vfs_busy(mp, MBF_NOWAIT);
483 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
484 mp->mnt_op = vfsp->vfc_vfsops;
486 mp->mnt_stat.f_type = vfsp->vfc_typenum;
488 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
489 mp->mnt_vnodecovered = vp;
490 mp->mnt_cred = crdup(cred);
491 mp->mnt_stat.f_owner = cred->cr_uid;
492 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
493 mp->mnt_iosize_max = DFLTPHYS;
496 mac_mount_create(cred, mp);
498 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
499 TAILQ_INIT(&mp->mnt_uppers);
504 * Destroy the mount struct previously allocated by vfs_mount_alloc().
507 vfs_mount_destroy(struct mount *mp)
511 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
512 if (mp->mnt_kern_flag & MNTK_MWAIT) {
513 mp->mnt_kern_flag &= ~MNTK_MWAIT;
517 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
518 KASSERT(mp->mnt_ref == 0,
519 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
520 __FILE__, __LINE__));
521 if (mp->mnt_writeopcount != 0)
522 panic("vfs_mount_destroy: nonzero writeopcount");
523 if (mp->mnt_secondary_writes != 0)
524 panic("vfs_mount_destroy: nonzero secondary_writes");
525 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
526 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
529 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
530 vn_printf(vp, "dangling vnode ");
531 panic("unmount: dangling vnode");
533 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
534 if (mp->mnt_nvnodelistsize != 0)
535 panic("vfs_mount_destroy: nonzero nvnodelistsize");
536 if (mp->mnt_activevnodelistsize != 0)
537 panic("vfs_mount_destroy: nonzero activevnodelistsize");
538 if (mp->mnt_tmpfreevnodelistsize != 0)
539 panic("vfs_mount_destroy: nonzero tmpfreevnodelistsize");
540 if (mp->mnt_lockref != 0)
541 panic("vfs_mount_destroy: nonzero lock refcount");
543 if (mp->mnt_vnodecovered != NULL)
544 vrele(mp->mnt_vnodecovered);
546 mac_mount_destroy(mp);
548 if (mp->mnt_opt != NULL)
549 vfs_freeopts(mp->mnt_opt);
550 crfree(mp->mnt_cred);
551 uma_zfree(mount_zone, mp);
555 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
557 /* This is an upgrade of an exisiting mount. */
558 if ((fsflags & MNT_UPDATE) != 0)
560 /* This is already an R/O mount. */
561 if ((fsflags & MNT_RDONLY) != 0)
565 case ENODEV: /* generic, geom, ... */
566 case EACCES: /* cam/scsi, ... */
567 case EROFS: /* md, mmcsd, ... */
569 * These errors can be returned by the storage layer to signal
570 * that the media is read-only. No harm in the R/O mount
571 * attempt if the error was returned for some other reason.
580 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
582 struct vfsoptlist *optlist;
583 struct vfsopt *opt, *tmp_opt;
584 char *fstype, *fspath, *errmsg;
585 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
588 errmsg = fspath = NULL;
589 errmsg_len = fspathlen = 0;
591 autoro = default_autoro;
593 error = vfs_buildopts(fsoptions, &optlist);
597 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
598 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
601 * We need these two options before the others,
602 * and they are mandatory for any filesystem.
603 * Ensure they are NUL terminated as well.
606 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
607 if (error || fstype[fstypelen - 1] != '\0') {
610 strncpy(errmsg, "Invalid fstype", errmsg_len);
614 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
615 if (error || fspath[fspathlen - 1] != '\0') {
618 strncpy(errmsg, "Invalid fspath", errmsg_len);
623 * We need to see if we have the "update" option
624 * before we call vfs_domount(), since vfs_domount() has special
625 * logic based on MNT_UPDATE. This is very important
626 * when we want to update the root filesystem.
628 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
629 if (strcmp(opt->name, "update") == 0) {
630 fsflags |= MNT_UPDATE;
631 vfs_freeopt(optlist, opt);
633 else if (strcmp(opt->name, "async") == 0)
634 fsflags |= MNT_ASYNC;
635 else if (strcmp(opt->name, "force") == 0) {
636 fsflags |= MNT_FORCE;
637 vfs_freeopt(optlist, opt);
639 else if (strcmp(opt->name, "reload") == 0) {
640 fsflags |= MNT_RELOAD;
641 vfs_freeopt(optlist, opt);
643 else if (strcmp(opt->name, "multilabel") == 0)
644 fsflags |= MNT_MULTILABEL;
645 else if (strcmp(opt->name, "noasync") == 0)
646 fsflags &= ~MNT_ASYNC;
647 else if (strcmp(opt->name, "noatime") == 0)
648 fsflags |= MNT_NOATIME;
649 else if (strcmp(opt->name, "atime") == 0) {
650 free(opt->name, M_MOUNT);
651 opt->name = strdup("nonoatime", M_MOUNT);
653 else if (strcmp(opt->name, "noclusterr") == 0)
654 fsflags |= MNT_NOCLUSTERR;
655 else if (strcmp(opt->name, "clusterr") == 0) {
656 free(opt->name, M_MOUNT);
657 opt->name = strdup("nonoclusterr", M_MOUNT);
659 else if (strcmp(opt->name, "noclusterw") == 0)
660 fsflags |= MNT_NOCLUSTERW;
661 else if (strcmp(opt->name, "clusterw") == 0) {
662 free(opt->name, M_MOUNT);
663 opt->name = strdup("nonoclusterw", M_MOUNT);
665 else if (strcmp(opt->name, "noexec") == 0)
666 fsflags |= MNT_NOEXEC;
667 else if (strcmp(opt->name, "exec") == 0) {
668 free(opt->name, M_MOUNT);
669 opt->name = strdup("nonoexec", M_MOUNT);
671 else if (strcmp(opt->name, "nosuid") == 0)
672 fsflags |= MNT_NOSUID;
673 else if (strcmp(opt->name, "suid") == 0) {
674 free(opt->name, M_MOUNT);
675 opt->name = strdup("nonosuid", M_MOUNT);
677 else if (strcmp(opt->name, "nosymfollow") == 0)
678 fsflags |= MNT_NOSYMFOLLOW;
679 else if (strcmp(opt->name, "symfollow") == 0) {
680 free(opt->name, M_MOUNT);
681 opt->name = strdup("nonosymfollow", M_MOUNT);
683 else if (strcmp(opt->name, "noro") == 0) {
684 fsflags &= ~MNT_RDONLY;
687 else if (strcmp(opt->name, "rw") == 0) {
688 fsflags &= ~MNT_RDONLY;
691 else if (strcmp(opt->name, "ro") == 0) {
692 fsflags |= MNT_RDONLY;
695 else if (strcmp(opt->name, "rdonly") == 0) {
696 free(opt->name, M_MOUNT);
697 opt->name = strdup("ro", M_MOUNT);
698 fsflags |= MNT_RDONLY;
701 else if (strcmp(opt->name, "autoro") == 0) {
702 vfs_freeopt(optlist, opt);
705 else if (strcmp(opt->name, "suiddir") == 0)
706 fsflags |= MNT_SUIDDIR;
707 else if (strcmp(opt->name, "sync") == 0)
708 fsflags |= MNT_SYNCHRONOUS;
709 else if (strcmp(opt->name, "union") == 0)
710 fsflags |= MNT_UNION;
711 else if (strcmp(opt->name, "automounted") == 0) {
712 fsflags |= MNT_AUTOMOUNTED;
713 vfs_freeopt(optlist, opt);
718 * Be ultra-paranoid about making sure the type and fspath
719 * variables will fit in our mp buffers, including the
722 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
723 error = ENAMETOOLONG;
727 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
730 * See if we can mount in the read-only mode if the error code suggests
731 * that it could be possible and the mount options allow for that.
732 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
733 * overridden by "autoro".
735 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
736 printf("%s: R/W mount failed, possibly R/O media,"
737 " trying R/O mount\n", __func__);
738 fsflags |= MNT_RDONLY;
739 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
742 /* copyout the errmsg */
743 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
744 && errmsg_len > 0 && errmsg != NULL) {
745 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
747 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
748 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
751 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
752 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
757 vfs_freeopts(optlist);
764 #ifndef _SYS_SYSPROTO_H_
774 sys_mount(struct thread *td, struct mount_args *uap)
777 struct vfsconf *vfsp = NULL;
778 struct mntarg *ma = NULL;
783 * Mount flags are now 64-bits. On 32-bit architectures only
784 * 32-bits are passed in, but from here on everything handles
785 * 64-bit flags correctly.
789 AUDIT_ARG_FFLAGS(flags);
792 * Filter out MNT_ROOTFS. We do not want clients of mount() in
793 * userspace to set this flag, but we must filter it out if we want
794 * MNT_UPDATE on the root file system to work.
795 * MNT_ROOTFS should only be set by the kernel when mounting its
798 flags &= ~MNT_ROOTFS;
800 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
801 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
803 free(fstype, M_TEMP);
807 AUDIT_ARG_TEXT(fstype);
808 vfsp = vfs_byname_kld(fstype, td, &error);
809 free(fstype, M_TEMP);
812 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
813 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
814 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
815 vfsp->vfc_vfsops->vfs_cmount == NULL))
818 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
819 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
820 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
821 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
822 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
824 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
825 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
826 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
830 * vfs_domount_first(): first file system mount (not update)
834 struct thread *td, /* Calling thread. */
835 struct vfsconf *vfsp, /* File system type. */
836 char *fspath, /* Mount path. */
837 struct vnode *vp, /* Vnode to be covered. */
838 uint64_t fsflags, /* Flags common to all filesystems. */
839 struct vfsoptlist **optlist /* Options local to the filesystem. */
847 ASSERT_VOP_ELOCKED(vp, __func__);
848 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
851 * If the jail of the calling thread lacks permission for this type of
852 * file system, deny immediately.
854 if (jailed(td->td_ucred) && !prison_allow(td->td_ucred,
855 vfsp->vfc_prison_flag)) {
861 * If the user is not root, ensure that they own the directory
862 * onto which we are attempting to mount.
864 error = VOP_GETATTR(vp, &va, td->td_ucred);
865 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
866 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
868 error = vinvalbuf(vp, V_SAVE, 0, 0);
869 if (error == 0 && vp->v_type != VDIR)
873 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
874 vp->v_iflag |= VI_MOUNT;
885 /* Allocate and initialize the filesystem. */
886 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
887 /* XXXMAC: pass to vfs_mount_alloc? */
888 mp->mnt_optnew = *optlist;
889 /* Set the mount level flags. */
890 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
893 * Mount the filesystem.
894 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
895 * get. No freeing of cn_pnbuf.
898 if ((error = VFS_MOUNT(mp)) != 0 ||
899 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
900 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
903 if ((error1 = VFS_UNMOUNT(mp, 0)) != 0)
904 printf("VFS_UNMOUNT returned %d\n", error1);
907 mp->mnt_vnodecovered = NULL;
908 vfs_mount_destroy(mp);
910 vp->v_iflag &= ~VI_MOUNT;
915 VOP_UNLOCK(newdp, 0);
917 if (mp->mnt_opt != NULL)
918 vfs_freeopts(mp->mnt_opt);
919 mp->mnt_opt = mp->mnt_optnew;
923 * Prevent external consumers of mount options from reading mnt_optnew.
925 mp->mnt_optnew = NULL;
928 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
929 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
930 mp->mnt_kern_flag |= MNTK_ASYNC;
932 mp->mnt_kern_flag &= ~MNTK_ASYNC;
935 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
938 vp->v_iflag &= ~VI_MOUNT;
940 vp->v_mountedhere = mp;
941 /* Place the new filesystem at the end of the mount list. */
942 mtx_lock(&mountlist_mtx);
943 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
944 mtx_unlock(&mountlist_mtx);
945 vfs_event_signal(NULL, VQ_MOUNT, 0);
946 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
948 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
949 VOP_UNLOCK(newdp, 0);
950 mountcheckdirs(vp, newdp);
952 if ((mp->mnt_flag & MNT_RDONLY) == 0)
953 vfs_allocate_syncvnode(mp);
959 * vfs_domount_update(): update of mounted file system
963 struct thread *td, /* Calling thread. */
964 struct vnode *vp, /* Mount point vnode. */
965 uint64_t fsflags, /* Flags common to all filesystems. */
966 struct vfsoptlist **optlist /* Options local to the filesystem. */
969 struct export_args export;
972 int error, export_error, len;
975 ASSERT_VOP_ELOCKED(vp, __func__);
976 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
979 if ((vp->v_vflag & VV_ROOT) == 0) {
980 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
990 * We only allow the filesystem to be reloaded if it
991 * is currently mounted read-only.
994 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
996 return (EOPNOTSUPP); /* Needs translation */
999 * Only privileged root, or (if MNT_USER is set) the user that
1000 * did the original mount is permitted to update it.
1002 error = vfs_suser(mp, td);
1007 if (vfs_busy(mp, MBF_NOWAIT)) {
1012 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1018 vp->v_iflag |= VI_MOUNT;
1023 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1028 mp->mnt_flag &= ~MNT_UPDATEMASK;
1029 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1030 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1031 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1032 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1034 mp->mnt_optnew = *optlist;
1035 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1038 * Mount the filesystem.
1039 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1040 * get. No freeing of cn_pnbuf.
1042 error = VFS_MOUNT(mp);
1045 /* Process the export option. */
1046 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1048 /* Assume that there is only 1 ABI for each length. */
1050 case (sizeof(struct oexport_args)):
1051 bzero(&export, sizeof(export));
1053 case (sizeof(export)):
1054 bcopy(bufp, &export, len);
1055 export_error = vfs_export(mp, &export);
1058 export_error = EINVAL;
1065 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1069 * If we fail, restore old mount flags. MNT_QUOTA is special,
1070 * because it is not part of MNT_UPDATEMASK, but it could have
1071 * changed in the meantime if quotactl(2) was called.
1072 * All in all we want current value of MNT_QUOTA, not the old
1075 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1077 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1078 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1079 mp->mnt_kern_flag |= MNTK_ASYNC;
1081 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1087 if (mp->mnt_opt != NULL)
1088 vfs_freeopts(mp->mnt_opt);
1089 mp->mnt_opt = mp->mnt_optnew;
1091 (void)VFS_STATFS(mp, &mp->mnt_stat);
1093 * Prevent external consumers of mount options from reading
1096 mp->mnt_optnew = NULL;
1098 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1099 vfs_allocate_syncvnode(mp);
1101 vfs_deallocate_syncvnode(mp);
1105 vp->v_iflag &= ~VI_MOUNT;
1108 return (error != 0 ? error : export_error);
1112 * vfs_domount(): actually attempt a filesystem mount.
1116 struct thread *td, /* Calling thread. */
1117 const char *fstype, /* Filesystem type. */
1118 char *fspath, /* Mount path. */
1119 uint64_t fsflags, /* Flags common to all filesystems. */
1120 struct vfsoptlist **optlist /* Options local to the filesystem. */
1123 struct vfsconf *vfsp;
1124 struct nameidata nd;
1130 * Be ultra-paranoid about making sure the type and fspath
1131 * variables will fit in our mp buffers, including the
1134 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1135 return (ENAMETOOLONG);
1137 if (jailed(td->td_ucred) || usermount == 0) {
1138 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1143 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1145 if (fsflags & MNT_EXPORTED) {
1146 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1150 if (fsflags & MNT_SUIDDIR) {
1151 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1156 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1158 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1159 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1160 fsflags |= MNT_NOSUID | MNT_USER;
1163 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1165 if ((fsflags & MNT_UPDATE) == 0) {
1166 /* Don't try to load KLDs if we're mounting the root. */
1167 if (fsflags & MNT_ROOTFS)
1168 vfsp = vfs_byname(fstype);
1170 vfsp = vfs_byname_kld(fstype, td, &error);
1176 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1178 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1179 UIO_SYSSPACE, fspath, td);
1183 NDFREE(&nd, NDF_ONLY_PNBUF);
1185 if ((fsflags & MNT_UPDATE) == 0) {
1186 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1187 strcpy(pathbuf, fspath);
1188 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1189 /* debug.disablefullpath == 1 results in ENODEV */
1190 if (error == 0 || error == ENODEV) {
1191 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1194 free(pathbuf, M_TEMP);
1196 error = vfs_domount_update(td, vp, fsflags, optlist);
1202 * Unmount a filesystem.
1204 * Note: unmount takes a path to the vnode mounted on as argument, not
1205 * special file (as before).
1207 #ifndef _SYS_SYSPROTO_H_
1208 struct unmount_args {
1215 sys_unmount(struct thread *td, struct unmount_args *uap)
1217 struct nameidata nd;
1220 int error, id0, id1;
1222 AUDIT_ARG_VALUE(uap->flags);
1223 if (jailed(td->td_ucred) || usermount == 0) {
1224 error = priv_check(td, PRIV_VFS_UNMOUNT);
1229 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1230 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1232 free(pathbuf, M_TEMP);
1235 if (uap->flags & MNT_BYFSID) {
1236 AUDIT_ARG_TEXT(pathbuf);
1237 /* Decode the filesystem ID. */
1238 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1239 free(pathbuf, M_TEMP);
1243 mtx_lock(&mountlist_mtx);
1244 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1245 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1246 mp->mnt_stat.f_fsid.val[1] == id1) {
1251 mtx_unlock(&mountlist_mtx);
1254 * Try to find global path for path argument.
1256 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1257 UIO_SYSSPACE, pathbuf, td);
1258 if (namei(&nd) == 0) {
1259 NDFREE(&nd, NDF_ONLY_PNBUF);
1260 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1262 if (error == 0 || error == ENODEV)
1265 mtx_lock(&mountlist_mtx);
1266 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1267 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1272 mtx_unlock(&mountlist_mtx);
1274 free(pathbuf, M_TEMP);
1277 * Previously we returned ENOENT for a nonexistent path and
1278 * EINVAL for a non-mountpoint. We cannot tell these apart
1279 * now, so in the !MNT_BYFSID case return the more likely
1280 * EINVAL for compatibility.
1282 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1286 * Don't allow unmounting the root filesystem.
1288 if (mp->mnt_flag & MNT_ROOTFS) {
1292 error = dounmount(mp, uap->flags, td);
1297 * Return error if any of the vnodes, ignoring the root vnode
1298 * and the syncer vnode, have non-zero usecount.
1300 * This function is purely advisory - it can return false positives
1304 vfs_check_usecounts(struct mount *mp)
1306 struct vnode *vp, *mvp;
1308 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1309 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1310 vp->v_usecount != 0) {
1312 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1322 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1325 mtx_assert(MNT_MTX(mp), MA_OWNED);
1326 mp->mnt_kern_flag &= ~mntkflags;
1327 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1328 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1332 if (coveredvp != NULL) {
1333 VOP_UNLOCK(coveredvp, 0);
1336 vn_finished_write(mp);
1340 * Do the actual filesystem unmount.
1343 dounmount(struct mount *mp, int flags, struct thread *td)
1345 struct vnode *coveredvp;
1347 uint64_t async_flag;
1350 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1351 mnt_gen_r = mp->mnt_gen;
1354 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1356 * Check for mp being unmounted while waiting for the
1357 * covered vnode lock.
1359 if (coveredvp->v_mountedhere != mp ||
1360 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1361 VOP_UNLOCK(coveredvp, 0);
1369 * Only privileged root, or (if MNT_USER is set) the user that did the
1370 * original mount is permitted to unmount this filesystem.
1372 error = vfs_suser(mp, td);
1374 if (coveredvp != NULL) {
1375 VOP_UNLOCK(coveredvp, 0);
1382 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1384 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1385 (mp->mnt_flag & MNT_UPDATE) != 0 ||
1386 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1387 dounmount_cleanup(mp, coveredvp, 0);
1390 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1391 if (flags & MNT_NONBUSY) {
1393 error = vfs_check_usecounts(mp);
1396 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT |
1401 /* Allow filesystems to detect that a forced unmount is in progress. */
1402 if (flags & MNT_FORCE) {
1403 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1406 * Must be done after setting MNTK_UNMOUNTF and before
1407 * waiting for mnt_lockref to become 0.
1413 if (mp->mnt_lockref) {
1414 mp->mnt_kern_flag |= MNTK_DRAINING;
1415 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1419 KASSERT(mp->mnt_lockref == 0,
1420 ("%s: invalid lock refcount in the drain path @ %s:%d",
1421 __func__, __FILE__, __LINE__));
1423 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1424 __func__, __FILE__, __LINE__));
1426 if (mp->mnt_flag & MNT_EXPUBLIC)
1427 vfs_setpublicfs(NULL, NULL, NULL);
1430 * From now, we can claim that the use reference on the
1431 * coveredvp is ours, and the ref can be released only by
1432 * successfull unmount by us, or left for later unmount
1433 * attempt. The previously acquired hold reference is no
1434 * longer needed to protect the vnode from reuse.
1436 if (coveredvp != NULL)
1439 vfs_msync(mp, MNT_WAIT);
1441 async_flag = mp->mnt_flag & MNT_ASYNC;
1442 mp->mnt_flag &= ~MNT_ASYNC;
1443 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1445 cache_purgevfs(mp, false); /* remove cache entries for this file sys */
1446 vfs_deallocate_syncvnode(mp);
1447 if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 ||
1448 (error = VFS_SYNC(mp, MNT_WAIT)) == 0)
1449 error = VFS_UNMOUNT(mp, flags);
1450 vn_finished_write(mp);
1452 * If we failed to flush the dirty blocks for this mount point,
1453 * undo all the cdir/rdir and rootvnode changes we made above.
1454 * Unless we failed to do so because the device is reporting that
1455 * it doesn't exist anymore.
1457 if (error && error != ENXIO) {
1459 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1460 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1462 vfs_allocate_syncvnode(mp);
1465 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1466 mp->mnt_flag |= async_flag;
1467 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1468 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1469 mp->mnt_kern_flag |= MNTK_ASYNC;
1470 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1471 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1476 VOP_UNLOCK(coveredvp, 0);
1479 mtx_lock(&mountlist_mtx);
1480 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1481 mtx_unlock(&mountlist_mtx);
1482 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
1483 if (coveredvp != NULL) {
1484 coveredvp->v_mountedhere = NULL;
1485 VOP_UNLOCK(coveredvp, 0);
1487 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1488 if (rootvnode != NULL && mp == rootvnode->v_mount) {
1492 if (mp == rootdevmp)
1494 vfs_mount_destroy(mp);
1499 * Report errors during filesystem mounting.
1502 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1504 struct vfsoptlist *moptlist = mp->mnt_optnew;
1509 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1510 if (error || errmsg == NULL || len <= 0)
1514 vsnprintf(errmsg, (size_t)len, fmt, ap);
1519 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1525 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1526 if (error || errmsg == NULL || len <= 0)
1530 vsnprintf(errmsg, (size_t)len, fmt, ap);
1535 * ---------------------------------------------------------------------
1536 * Functions for querying mount options/arguments from filesystems.
1540 * Check that no unknown options are given
1543 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1547 const char **t, *p, *q;
1550 TAILQ_FOREACH(opt, opts, link) {
1553 if (p[0] == 'n' && p[1] == 'o')
1555 for(t = global_opts; *t != NULL; t++) {
1556 if (strcmp(*t, p) == 0)
1559 if (strcmp(*t, q) == 0)
1565 for(t = legal; *t != NULL; t++) {
1566 if (strcmp(*t, p) == 0)
1569 if (strcmp(*t, q) == 0)
1575 snprintf(errmsg, sizeof(errmsg),
1576 "mount option <%s> is unknown", p);
1580 TAILQ_FOREACH(opt, opts, link) {
1581 if (strcmp(opt->name, "errmsg") == 0) {
1582 strncpy((char *)opt->value, errmsg, opt->len);
1587 printf("%s\n", errmsg);
1593 * Get a mount option by its name.
1595 * Return 0 if the option was found, ENOENT otherwise.
1596 * If len is non-NULL it will be filled with the length
1597 * of the option. If buf is non-NULL, it will be filled
1598 * with the address of the option.
1601 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
1605 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1607 TAILQ_FOREACH(opt, opts, link) {
1608 if (strcmp(name, opt->name) == 0) {
1621 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1628 TAILQ_FOREACH(opt, opts, link) {
1629 if (strcmp(name, opt->name) == 0) {
1638 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1640 char *opt_value, *vtp;
1644 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1647 if (opt_len == 0 || opt_value == NULL)
1649 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1651 iv = strtoq(opt_value, &vtp, 0);
1652 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
1679 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1684 TAILQ_FOREACH(opt, opts, link) {
1685 if (strcmp(name, opt->name) != 0)
1688 if (opt->len == 0 ||
1689 ((char *)opt->value)[opt->len - 1] != '\0') {
1693 return (opt->value);
1700 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
1705 TAILQ_FOREACH(opt, opts, link) {
1706 if (strcmp(name, opt->name) == 0) {
1719 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1725 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1727 TAILQ_FOREACH(opt, opts, link) {
1728 if (strcmp(name, opt->name) != 0)
1731 if (opt->len == 0 || opt->value == NULL)
1733 if (((char *)opt->value)[opt->len - 1] != '\0')
1736 ret = vsscanf(opt->value, fmt, ap);
1744 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
1748 TAILQ_FOREACH(opt, opts, link) {
1749 if (strcmp(name, opt->name) != 0)
1752 if (opt->value == NULL)
1755 if (opt->len != len)
1757 bcopy(value, opt->value, len);
1765 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
1769 TAILQ_FOREACH(opt, opts, link) {
1770 if (strcmp(name, opt->name) != 0)
1773 if (opt->value == NULL)
1779 bcopy(value, opt->value, len);
1787 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
1791 TAILQ_FOREACH(opt, opts, link) {
1792 if (strcmp(name, opt->name) != 0)
1795 if (opt->value == NULL)
1796 opt->len = strlen(value) + 1;
1797 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
1805 * Find and copy a mount option.
1807 * The size of the buffer has to be specified
1808 * in len, if it is not the same length as the
1809 * mount option, EINVAL is returned.
1810 * Returns ENOENT if the option is not found.
1813 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
1817 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1819 TAILQ_FOREACH(opt, opts, link) {
1820 if (strcmp(name, opt->name) == 0) {
1822 if (len != opt->len)
1824 bcopy(opt->value, dest, opt->len);
1832 __vfs_statfs(struct mount *mp, struct statfs *sbp)
1836 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
1837 if (sbp != &mp->mnt_stat)
1838 *sbp = mp->mnt_stat;
1843 vfs_mountedfrom(struct mount *mp, const char *from)
1846 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1847 strlcpy(mp->mnt_stat.f_mntfromname, from,
1848 sizeof mp->mnt_stat.f_mntfromname);
1852 * ---------------------------------------------------------------------
1853 * This is the api for building mount args and mounting filesystems from
1854 * inside the kernel.
1856 * The API works by accumulation of individual args. First error is
1859 * XXX: should be documented in new manpage kernel_mount(9)
1862 /* A memory allocation which must be freed when we are done */
1864 SLIST_ENTRY(mntaarg) next;
1867 /* The header for the mount arguments */
1872 SLIST_HEAD(, mntaarg) list;
1876 * Add a boolean argument.
1878 * flag is the boolean value.
1879 * name must start with "no".
1882 mount_argb(struct mntarg *ma, int flag, const char *name)
1885 KASSERT(name[0] == 'n' && name[1] == 'o',
1886 ("mount_argb(...,%s): name must start with 'no'", name));
1888 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1892 * Add an argument printf style
1895 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1898 struct mntaarg *maa;
1903 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1904 SLIST_INIT(&ma->list);
1909 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1911 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1912 ma->v[ma->len].iov_len = strlen(name) + 1;
1915 sb = sbuf_new_auto();
1917 sbuf_vprintf(sb, fmt, ap);
1920 len = sbuf_len(sb) + 1;
1921 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1922 SLIST_INSERT_HEAD(&ma->list, maa, next);
1923 bcopy(sbuf_data(sb), maa + 1, len);
1926 ma->v[ma->len].iov_base = maa + 1;
1927 ma->v[ma->len].iov_len = len;
1934 * Add an argument which is a userland string.
1937 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1939 struct mntaarg *maa;
1945 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1946 SLIST_INIT(&ma->list);
1950 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1951 SLIST_INSERT_HEAD(&ma->list, maa, next);
1952 tbuf = (void *)(maa + 1);
1953 ma->error = copyinstr(val, tbuf, len, NULL);
1954 return (mount_arg(ma, name, tbuf, -1));
1960 * If length is -1, treat value as a C string.
1963 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1967 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1968 SLIST_INIT(&ma->list);
1973 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1975 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1976 ma->v[ma->len].iov_len = strlen(name) + 1;
1979 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1981 ma->v[ma->len].iov_len = strlen(val) + 1;
1983 ma->v[ma->len].iov_len = len;
1989 * Free a mntarg structure
1992 free_mntarg(struct mntarg *ma)
1994 struct mntaarg *maa;
1996 while (!SLIST_EMPTY(&ma->list)) {
1997 maa = SLIST_FIRST(&ma->list);
1998 SLIST_REMOVE_HEAD(&ma->list, next);
2001 free(ma->v, M_MOUNT);
2006 * Mount a filesystem
2009 kernel_mount(struct mntarg *ma, uint64_t flags)
2014 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2015 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2016 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2018 auio.uio_iov = ma->v;
2019 auio.uio_iovcnt = ma->len;
2020 auio.uio_segflg = UIO_SYSSPACE;
2024 error = vfs_donmount(curthread, flags, &auio);
2030 * A printflike function to mount a filesystem.
2033 kernel_vmount(int flags, ...)
2035 struct mntarg *ma = NULL;
2041 va_start(ap, flags);
2043 cp = va_arg(ap, const char *);
2046 vp = va_arg(ap, const void *);
2047 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2051 error = kernel_mount(ma, flags);
2056 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2059 bcopy(oexp, exp, sizeof(*oexp));
2060 exp->ex_numsecflavors = 0;