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>
45 #include <sys/devctl.h>
46 #include <sys/eventhandler.h>
47 #include <sys/fcntl.h>
49 #include <sys/kernel.h>
51 #include <sys/libkern.h>
52 #include <sys/limits.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.h>
59 #include <sys/filedesc.h>
60 #include <sys/reboot.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysproto.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/systm.h>
68 #include <sys/taskqueue.h>
69 #include <sys/vnode.h>
72 #include <geom/geom.h>
74 #include <machine/stdarg.h>
76 #include <security/audit/audit.h>
77 #include <security/mac/mac_framework.h>
79 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
81 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
82 uint64_t fsflags, struct vfsoptlist **optlist);
83 static void free_mntarg(struct mntarg *ma);
85 static int usermount = 0;
86 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
87 "Unprivileged users may mount and unmount file systems");
89 static bool default_autoro = false;
90 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
91 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
93 static bool recursive_forced_unmount = false;
94 SYSCTL_BOOL(_vfs, OID_AUTO, recursive_forced_unmount, CTLFLAG_RW,
95 &recursive_forced_unmount, 0, "Recursively unmount stacked upper mounts"
96 " when a file system is forcibly unmounted");
98 static SYSCTL_NODE(_vfs, OID_AUTO, deferred_unmount,
99 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "deferred unmount controls");
101 static unsigned int deferred_unmount_retry_limit = 10;
102 SYSCTL_UINT(_vfs_deferred_unmount, OID_AUTO, retry_limit, CTLFLAG_RW,
103 &deferred_unmount_retry_limit, 0,
104 "Maximum number of retries for deferred unmount failure");
106 static int deferred_unmount_retry_delay_hz;
107 SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, retry_delay_hz, CTLFLAG_RW,
108 &deferred_unmount_retry_delay_hz, 0,
109 "Delay in units of [1/kern.hz]s when retrying a failed deferred unmount");
111 static int deferred_unmount_total_retries = 0;
112 SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, total_retries, CTLFLAG_RD,
113 &deferred_unmount_total_retries, 0,
114 "Total number of retried deferred unmounts");
116 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
117 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
118 static uma_zone_t mount_zone;
120 /* List of mounted filesystems. */
121 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
123 /* For any iteration/modification of mountlist */
124 struct mtx_padalign __exclusive_cache_line mountlist_mtx;
126 EVENTHANDLER_LIST_DEFINE(vfs_mounted);
127 EVENTHANDLER_LIST_DEFINE(vfs_unmounted);
129 static void vfs_deferred_unmount(void *arg, int pending);
130 static struct timeout_task deferred_unmount_task;
131 static struct mtx deferred_unmount_lock;
132 MTX_SYSINIT(deferred_unmount, &deferred_unmount_lock, "deferred_unmount",
134 static STAILQ_HEAD(, mount) deferred_unmount_list =
135 STAILQ_HEAD_INITIALIZER(deferred_unmount_list);
136 TASKQUEUE_DEFINE_THREAD(deferred_unmount);
138 static void mount_devctl_event(const char *type, struct mount *mp, bool donew);
141 * Global opts, taken by all filesystems
143 static const char *global_opts[] = {
155 mount_init(void *mem, int size, int flags)
159 mp = (struct mount *)mem;
160 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
161 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
162 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
163 mp->mnt_pcpu = uma_zalloc_pcpu(pcpu_zone_16, M_WAITOK | M_ZERO);
166 mp->mnt_rootvnode = NULL;
171 mount_fini(void *mem, int size)
175 mp = (struct mount *)mem;
176 uma_zfree_pcpu(pcpu_zone_16, mp->mnt_pcpu);
177 lockdestroy(&mp->mnt_explock);
178 mtx_destroy(&mp->mnt_listmtx);
179 mtx_destroy(&mp->mnt_mtx);
183 vfs_mount_init(void *dummy __unused)
185 TIMEOUT_TASK_INIT(taskqueue_deferred_unmount, &deferred_unmount_task,
186 0, vfs_deferred_unmount, NULL);
187 deferred_unmount_retry_delay_hz = hz;
188 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
189 NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
190 mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF);
192 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
195 * ---------------------------------------------------------------------
196 * Functions for building and sanitizing the mount options
199 /* Remove one mount option. */
201 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
204 TAILQ_REMOVE(opts, opt, link);
205 free(opt->name, M_MOUNT);
206 if (opt->value != NULL)
207 free(opt->value, M_MOUNT);
211 /* Release all resources related to the mount options. */
213 vfs_freeopts(struct vfsoptlist *opts)
217 while (!TAILQ_EMPTY(opts)) {
218 opt = TAILQ_FIRST(opts);
219 vfs_freeopt(opts, opt);
225 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
227 struct vfsopt *opt, *temp;
231 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
232 if (strcmp(opt->name, name) == 0)
233 vfs_freeopt(opts, opt);
238 vfs_isopt_ro(const char *opt)
241 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
242 strcmp(opt, "norw") == 0)
248 vfs_isopt_rw(const char *opt)
251 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
257 * Check if options are equal (with or without the "no" prefix).
260 vfs_equalopts(const char *opt1, const char *opt2)
264 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
265 if (strcmp(opt1, opt2) == 0)
267 /* "noopt" vs. "opt" */
268 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
270 /* "opt" vs. "noopt" */
271 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
273 while ((p = strchr(opt1, '.')) != NULL &&
274 !strncmp(opt1, opt2, ++p - opt1)) {
277 /* "foo.noopt" vs. "foo.opt" */
278 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
280 /* "foo.opt" vs. "foo.noopt" */
281 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
284 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
285 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
286 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
292 * If a mount option is specified several times,
293 * (with or without the "no" prefix) only keep
294 * the last occurrence of it.
297 vfs_sanitizeopts(struct vfsoptlist *opts)
299 struct vfsopt *opt, *opt2, *tmp;
301 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
302 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
303 while (opt2 != NULL) {
304 if (vfs_equalopts(opt->name, opt2->name)) {
305 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
306 vfs_freeopt(opts, opt2);
309 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
316 * Build a linked list of mount options from a struct uio.
319 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
321 struct vfsoptlist *opts;
323 size_t memused, namelen, optlen;
324 unsigned int i, iovcnt;
327 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
330 iovcnt = auio->uio_iovcnt;
331 for (i = 0; i < iovcnt; i += 2) {
332 namelen = auio->uio_iov[i].iov_len;
333 optlen = auio->uio_iov[i + 1].iov_len;
334 memused += sizeof(struct vfsopt) + optlen + namelen;
336 * Avoid consuming too much memory, and attempts to overflow
339 if (memused > VFS_MOUNTARG_SIZE_MAX ||
340 optlen > VFS_MOUNTARG_SIZE_MAX ||
341 namelen > VFS_MOUNTARG_SIZE_MAX) {
346 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
347 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
354 * Do this early, so jumps to "bad" will free the current
357 TAILQ_INSERT_TAIL(opts, opt, link);
359 if (auio->uio_segflg == UIO_SYSSPACE) {
360 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
362 error = copyin(auio->uio_iov[i].iov_base, opt->name,
367 /* Ensure names are null-terminated strings. */
368 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
374 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
375 if (auio->uio_segflg == UIO_SYSSPACE) {
376 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
379 error = copyin(auio->uio_iov[i + 1].iov_base,
386 vfs_sanitizeopts(opts);
395 * Merge the old mount options with the new ones passed
396 * in the MNT_UPDATE case.
398 * XXX: This function will keep a "nofoo" option in the new
399 * options. E.g, if the option's canonical name is "foo",
400 * "nofoo" ends up in the mount point's active options.
403 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
405 struct vfsopt *opt, *new;
407 TAILQ_FOREACH(opt, oldopts, link) {
408 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
409 new->name = strdup(opt->name, M_MOUNT);
411 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
412 bcopy(opt->value, new->value, opt->len);
416 new->seen = opt->seen;
417 TAILQ_INSERT_HEAD(toopts, new, link);
419 vfs_sanitizeopts(toopts);
423 * Mount a filesystem.
425 #ifndef _SYS_SYSPROTO_H_
433 sys_nmount(struct thread *td, struct nmount_args *uap)
441 * Mount flags are now 64-bits. On 32-bit archtectures only
442 * 32-bits are passed in, but from here on everything handles
443 * 64-bit flags correctly.
447 AUDIT_ARG_FFLAGS(flags);
448 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
449 uap->iovp, uap->iovcnt, flags);
452 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
453 * userspace to set this flag, but we must filter it out if we want
454 * MNT_UPDATE on the root file system to work.
455 * MNT_ROOTFS should only be set by the kernel when mounting its
458 flags &= ~MNT_ROOTFS;
460 iovcnt = uap->iovcnt;
462 * Check that we have an even number of iovec's
463 * and that we have at least two options.
465 if ((iovcnt & 1) || (iovcnt < 4)) {
466 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
471 error = copyinuio(uap->iovp, iovcnt, &auio);
473 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
477 error = vfs_donmount(td, flags, auio);
484 * ---------------------------------------------------------------------
485 * Various utility functions
489 * Get a reference on a mount point from a vnode.
491 * The vnode is allowed to be passed unlocked and race against dooming. Note in
492 * such case there are no guarantees the referenced mount point will still be
493 * associated with it after the function returns.
496 vfs_ref_from_vp(struct vnode *vp)
499 struct mount_pcpu *mpcpu;
501 mp = atomic_load_ptr(&vp->v_mount);
502 if (__predict_false(mp == NULL)) {
505 if (vfs_op_thread_enter(mp, mpcpu)) {
506 if (__predict_true(mp == vp->v_mount)) {
507 vfs_mp_count_add_pcpu(mpcpu, ref, 1);
508 vfs_op_thread_exit(mp, mpcpu);
510 vfs_op_thread_exit(mp, mpcpu);
515 if (mp == vp->v_mount) {
527 vfs_ref(struct mount *mp)
529 struct mount_pcpu *mpcpu;
531 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
532 if (vfs_op_thread_enter(mp, mpcpu)) {
533 vfs_mp_count_add_pcpu(mpcpu, ref, 1);
534 vfs_op_thread_exit(mp, mpcpu);
544 * Register ump as an upper mount of the mount associated with
545 * vnode vp. This registration will be tracked through
546 * mount_upper_node upper, which should be allocated by the
547 * caller and stored in per-mount data associated with mp.
549 * If successful, this function will return the mount associated
550 * with vp, and will ensure that it cannot be unmounted until
551 * ump has been unregistered as one of its upper mounts.
553 * Upon failure this function will return NULL.
556 vfs_register_upper_from_vp(struct vnode *vp, struct mount *ump,
557 struct mount_upper_node *upper)
561 mp = atomic_load_ptr(&vp->v_mount);
565 if (mp != vp->v_mount ||
566 ((mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_RECURSE)) != 0)) {
570 KASSERT(ump != mp, ("upper and lower mounts are identical"));
573 TAILQ_INSERT_TAIL(&mp->mnt_uppers, upper, mnt_upper_link);
579 * Register upper mount ump to receive vnode unlink/reclaim
580 * notifications from lower mount mp. This registration will
581 * be tracked through mount_upper_node upper, which should be
582 * allocated by the caller and stored in per-mount data
583 * associated with mp.
585 * ump must already be registered as an upper mount of mp
586 * through a call to vfs_register_upper_from_vp().
589 vfs_register_for_notification(struct mount *mp, struct mount *ump,
590 struct mount_upper_node *upper)
594 TAILQ_INSERT_TAIL(&mp->mnt_notify, upper, mnt_upper_link);
599 vfs_drain_upper_locked(struct mount *mp)
601 mtx_assert(MNT_MTX(mp), MA_OWNED);
602 while (mp->mnt_upper_pending != 0) {
603 mp->mnt_kern_flag |= MNTK_UPPER_WAITER;
604 msleep(&mp->mnt_uppers, MNT_MTX(mp), 0, "mntupw", 0);
609 * Undo a previous call to vfs_register_for_notification().
610 * The mount represented by upper must be currently registered
611 * as an upper mount for mp.
614 vfs_unregister_for_notification(struct mount *mp,
615 struct mount_upper_node *upper)
618 vfs_drain_upper_locked(mp);
619 TAILQ_REMOVE(&mp->mnt_notify, upper, mnt_upper_link);
624 * Undo a previous call to vfs_register_upper_from_vp().
625 * This must be done before mp can be unmounted.
628 vfs_unregister_upper(struct mount *mp, struct mount_upper_node *upper)
631 KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0,
632 ("registered upper with pending unmount"));
633 vfs_drain_upper_locked(mp);
634 TAILQ_REMOVE(&mp->mnt_uppers, upper, mnt_upper_link);
635 if ((mp->mnt_kern_flag & MNTK_TASKQUEUE_WAITER) != 0 &&
636 TAILQ_EMPTY(&mp->mnt_uppers)) {
637 mp->mnt_kern_flag &= ~MNTK_TASKQUEUE_WAITER;
638 wakeup(&mp->mnt_taskqueue_link);
645 vfs_rel(struct mount *mp)
647 struct mount_pcpu *mpcpu;
649 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
650 if (vfs_op_thread_enter(mp, mpcpu)) {
651 vfs_mp_count_sub_pcpu(mpcpu, ref, 1);
652 vfs_op_thread_exit(mp, mpcpu);
662 * Allocate and initialize the mount point struct.
665 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
670 mp = uma_zalloc(mount_zone, M_WAITOK);
671 bzero(&mp->mnt_startzero,
672 __rangeof(struct mount, mnt_startzero, mnt_endzero));
673 mp->mnt_kern_flag = 0;
675 mp->mnt_rootvnode = NULL;
676 mp->mnt_vnodecovered = NULL;
679 TAILQ_INIT(&mp->mnt_nvnodelist);
680 mp->mnt_nvnodelistsize = 0;
681 TAILQ_INIT(&mp->mnt_lazyvnodelist);
682 mp->mnt_lazyvnodelistsize = 0;
683 if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 ||
684 mp->mnt_writeopcount != 0)
685 panic("%s: non-zero counters on new mp %p\n", __func__, mp);
686 if (mp->mnt_vfs_ops != 1)
687 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
689 (void) vfs_busy(mp, MBF_NOWAIT);
690 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
691 mp->mnt_op = vfsp->vfc_vfsops;
693 mp->mnt_stat.f_type = vfsp->vfc_typenum;
695 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
696 mp->mnt_vnodecovered = vp;
697 mp->mnt_cred = crdup(cred);
698 mp->mnt_stat.f_owner = cred->cr_uid;
699 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
700 mp->mnt_iosize_max = DFLTPHYS;
703 mac_mount_create(cred, mp);
705 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
706 mp->mnt_upper_pending = 0;
707 TAILQ_INIT(&mp->mnt_uppers);
708 TAILQ_INIT(&mp->mnt_notify);
709 mp->mnt_taskqueue_flags = 0;
710 mp->mnt_unmount_retries = 0;
715 * Destroy the mount struct previously allocated by vfs_mount_alloc().
718 vfs_mount_destroy(struct mount *mp)
721 if (mp->mnt_vfs_ops == 0)
722 panic("%s: entered with zero vfs_ops\n", __func__);
724 vfs_assert_mount_counters(mp);
727 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
728 if (mp->mnt_kern_flag & MNTK_MWAIT) {
729 mp->mnt_kern_flag &= ~MNTK_MWAIT;
733 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
734 KASSERT(mp->mnt_ref == 0,
735 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
736 __FILE__, __LINE__));
737 if (mp->mnt_writeopcount != 0)
738 panic("vfs_mount_destroy: nonzero writeopcount");
739 if (mp->mnt_secondary_writes != 0)
740 panic("vfs_mount_destroy: nonzero secondary_writes");
741 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
742 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
745 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
746 vn_printf(vp, "dangling vnode ");
747 panic("unmount: dangling vnode");
749 KASSERT(mp->mnt_upper_pending == 0, ("mnt_upper_pending"));
750 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
751 KASSERT(TAILQ_EMPTY(&mp->mnt_notify), ("mnt_notify"));
752 if (mp->mnt_nvnodelistsize != 0)
753 panic("vfs_mount_destroy: nonzero nvnodelistsize");
754 if (mp->mnt_lazyvnodelistsize != 0)
755 panic("vfs_mount_destroy: nonzero lazyvnodelistsize");
756 if (mp->mnt_lockref != 0)
757 panic("vfs_mount_destroy: nonzero lock refcount");
760 if (mp->mnt_vfs_ops != 1)
761 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
764 if (mp->mnt_rootvnode != NULL)
765 panic("%s: mount point still has a root vnode %p\n", __func__,
768 if (mp->mnt_vnodecovered != NULL)
769 vrele(mp->mnt_vnodecovered);
771 mac_mount_destroy(mp);
773 if (mp->mnt_opt != NULL)
774 vfs_freeopts(mp->mnt_opt);
775 crfree(mp->mnt_cred);
776 uma_zfree(mount_zone, mp);
780 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
782 /* This is an upgrade of an exisiting mount. */
783 if ((fsflags & MNT_UPDATE) != 0)
785 /* This is already an R/O mount. */
786 if ((fsflags & MNT_RDONLY) != 0)
790 case ENODEV: /* generic, geom, ... */
791 case EACCES: /* cam/scsi, ... */
792 case EROFS: /* md, mmcsd, ... */
794 * These errors can be returned by the storage layer to signal
795 * that the media is read-only. No harm in the R/O mount
796 * attempt if the error was returned for some other reason.
805 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
807 struct vfsoptlist *optlist;
808 struct vfsopt *opt, *tmp_opt;
809 char *fstype, *fspath, *errmsg;
810 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
813 errmsg = fspath = NULL;
814 errmsg_len = fspathlen = 0;
816 autoro = default_autoro;
818 error = vfs_buildopts(fsoptions, &optlist);
822 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
823 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
826 * We need these two options before the others,
827 * and they are mandatory for any filesystem.
828 * Ensure they are NUL terminated as well.
831 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
832 if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
835 strncpy(errmsg, "Invalid fstype", errmsg_len);
839 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
840 if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
843 strncpy(errmsg, "Invalid fspath", errmsg_len);
848 * We need to see if we have the "update" option
849 * before we call vfs_domount(), since vfs_domount() has special
850 * logic based on MNT_UPDATE. This is very important
851 * when we want to update the root filesystem.
853 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
856 if (strcmp(opt->name, "update") == 0) {
857 fsflags |= MNT_UPDATE;
860 else if (strcmp(opt->name, "async") == 0)
861 fsflags |= MNT_ASYNC;
862 else if (strcmp(opt->name, "force") == 0) {
863 fsflags |= MNT_FORCE;
866 else if (strcmp(opt->name, "reload") == 0) {
867 fsflags |= MNT_RELOAD;
870 else if (strcmp(opt->name, "multilabel") == 0)
871 fsflags |= MNT_MULTILABEL;
872 else if (strcmp(opt->name, "noasync") == 0)
873 fsflags &= ~MNT_ASYNC;
874 else if (strcmp(opt->name, "noatime") == 0)
875 fsflags |= MNT_NOATIME;
876 else if (strcmp(opt->name, "atime") == 0) {
877 free(opt->name, M_MOUNT);
878 opt->name = strdup("nonoatime", M_MOUNT);
880 else if (strcmp(opt->name, "noclusterr") == 0)
881 fsflags |= MNT_NOCLUSTERR;
882 else if (strcmp(opt->name, "clusterr") == 0) {
883 free(opt->name, M_MOUNT);
884 opt->name = strdup("nonoclusterr", M_MOUNT);
886 else if (strcmp(opt->name, "noclusterw") == 0)
887 fsflags |= MNT_NOCLUSTERW;
888 else if (strcmp(opt->name, "clusterw") == 0) {
889 free(opt->name, M_MOUNT);
890 opt->name = strdup("nonoclusterw", M_MOUNT);
892 else if (strcmp(opt->name, "noexec") == 0)
893 fsflags |= MNT_NOEXEC;
894 else if (strcmp(opt->name, "exec") == 0) {
895 free(opt->name, M_MOUNT);
896 opt->name = strdup("nonoexec", M_MOUNT);
898 else if (strcmp(opt->name, "nosuid") == 0)
899 fsflags |= MNT_NOSUID;
900 else if (strcmp(opt->name, "suid") == 0) {
901 free(opt->name, M_MOUNT);
902 opt->name = strdup("nonosuid", M_MOUNT);
904 else if (strcmp(opt->name, "nosymfollow") == 0)
905 fsflags |= MNT_NOSYMFOLLOW;
906 else if (strcmp(opt->name, "symfollow") == 0) {
907 free(opt->name, M_MOUNT);
908 opt->name = strdup("nonosymfollow", M_MOUNT);
910 else if (strcmp(opt->name, "noro") == 0) {
911 fsflags &= ~MNT_RDONLY;
914 else if (strcmp(opt->name, "rw") == 0) {
915 fsflags &= ~MNT_RDONLY;
918 else if (strcmp(opt->name, "ro") == 0) {
919 fsflags |= MNT_RDONLY;
922 else if (strcmp(opt->name, "rdonly") == 0) {
923 free(opt->name, M_MOUNT);
924 opt->name = strdup("ro", M_MOUNT);
925 fsflags |= MNT_RDONLY;
928 else if (strcmp(opt->name, "autoro") == 0) {
932 else if (strcmp(opt->name, "suiddir") == 0)
933 fsflags |= MNT_SUIDDIR;
934 else if (strcmp(opt->name, "sync") == 0)
935 fsflags |= MNT_SYNCHRONOUS;
936 else if (strcmp(opt->name, "union") == 0)
937 fsflags |= MNT_UNION;
938 else if (strcmp(opt->name, "automounted") == 0) {
939 fsflags |= MNT_AUTOMOUNTED;
941 } else if (strcmp(opt->name, "nocover") == 0) {
942 fsflags |= MNT_NOCOVER;
944 } else if (strcmp(opt->name, "cover") == 0) {
945 fsflags &= ~MNT_NOCOVER;
947 } else if (strcmp(opt->name, "emptydir") == 0) {
948 fsflags |= MNT_EMPTYDIR;
950 } else if (strcmp(opt->name, "noemptydir") == 0) {
951 fsflags &= ~MNT_EMPTYDIR;
955 vfs_freeopt(optlist, opt);
959 * Be ultra-paranoid about making sure the type and fspath
960 * variables will fit in our mp buffers, including the
963 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
964 error = ENAMETOOLONG;
968 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
969 if (error == ENOENT) {
972 strncpy(errmsg, "Invalid fstype", errmsg_len);
977 * See if we can mount in the read-only mode if the error code suggests
978 * that it could be possible and the mount options allow for that.
979 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
980 * overridden by "autoro".
982 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
983 printf("%s: R/W mount failed, possibly R/O media,"
984 " trying R/O mount\n", __func__);
985 fsflags |= MNT_RDONLY;
986 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
989 /* copyout the errmsg */
990 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
991 && errmsg_len > 0 && errmsg != NULL) {
992 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
994 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
995 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
998 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
999 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
1003 if (optlist != NULL)
1004 vfs_freeopts(optlist);
1011 #ifndef _SYS_SYSPROTO_H_
1021 sys_mount(struct thread *td, struct mount_args *uap)
1024 struct vfsconf *vfsp = NULL;
1025 struct mntarg *ma = NULL;
1030 * Mount flags are now 64-bits. On 32-bit architectures only
1031 * 32-bits are passed in, but from here on everything handles
1032 * 64-bit flags correctly.
1036 AUDIT_ARG_FFLAGS(flags);
1039 * Filter out MNT_ROOTFS. We do not want clients of mount() in
1040 * userspace to set this flag, but we must filter it out if we want
1041 * MNT_UPDATE on the root file system to work.
1042 * MNT_ROOTFS should only be set by the kernel when mounting its
1045 flags &= ~MNT_ROOTFS;
1047 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
1048 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
1050 free(fstype, M_TEMP);
1054 AUDIT_ARG_TEXT(fstype);
1055 vfsp = vfs_byname_kld(fstype, td, &error);
1056 free(fstype, M_TEMP);
1059 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
1060 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
1061 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
1062 vfsp->vfc_vfsops->vfs_cmount == NULL))
1063 return (EOPNOTSUPP);
1065 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
1066 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
1067 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
1068 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
1069 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
1071 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
1072 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
1073 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
1077 * vfs_domount_first(): first file system mount (not update)
1081 struct thread *td, /* Calling thread. */
1082 struct vfsconf *vfsp, /* File system type. */
1083 char *fspath, /* Mount path. */
1084 struct vnode *vp, /* Vnode to be covered. */
1085 uint64_t fsflags, /* Flags common to all filesystems. */
1086 struct vfsoptlist **optlist /* Options local to the filesystem. */
1091 struct vnode *newdp, *rootvp;
1095 ASSERT_VOP_ELOCKED(vp, __func__);
1096 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
1099 * If the jail of the calling thread lacks permission for this type of
1100 * file system, or is trying to cover its own root, deny immediately.
1102 if (jailed(td->td_ucred) && (!prison_allow(td->td_ucred,
1103 vfsp->vfc_prison_flag) || vp == td->td_ucred->cr_prison->pr_root)) {
1109 * If the user is not root, ensure that they own the directory
1110 * onto which we are attempting to mount.
1112 error = VOP_GETATTR(vp, &va, td->td_ucred);
1113 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
1114 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
1116 error = vinvalbuf(vp, V_SAVE, 0, 0);
1117 if (error == 0 && vp->v_type != VDIR)
1119 if (error == 0 && (fsflags & MNT_EMPTYDIR) != 0)
1120 error = vfs_emptydir(vp);
1123 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
1124 vp->v_iflag |= VI_MOUNT;
1133 vn_seqc_write_begin(vp);
1136 /* Allocate and initialize the filesystem. */
1137 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
1138 /* XXXMAC: pass to vfs_mount_alloc? */
1139 mp->mnt_optnew = *optlist;
1140 /* Set the mount level flags. */
1141 mp->mnt_flag = (fsflags &
1142 (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY | MNT_FORCE));
1145 * Mount the filesystem.
1146 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1147 * get. No freeing of cn_pnbuf.
1151 if ((error = VFS_MOUNT(mp)) != 0 ||
1152 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
1153 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
1157 rootvp = vfs_cache_root_clear(mp);
1158 if (rootvp != NULL) {
1162 (void)vn_start_write(NULL, &mp, V_WAIT);
1164 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_UNMOUNTF;
1167 error = VFS_UNMOUNT(mp, 0);
1168 vn_finished_write(mp);
1171 "failed post-mount (%d): rollback unmount returned %d\n",
1178 mp->mnt_vnodecovered = NULL;
1180 /* XXXKIB wait for mnt_lockref drain? */
1181 vfs_mount_destroy(mp);
1184 vp->v_iflag &= ~VI_MOUNT;
1186 if (rootvp != NULL) {
1187 vn_seqc_write_end(rootvp);
1190 vn_seqc_write_end(vp);
1194 vn_seqc_write_begin(newdp);
1197 if (mp->mnt_opt != NULL)
1198 vfs_freeopts(mp->mnt_opt);
1199 mp->mnt_opt = mp->mnt_optnew;
1203 * Prevent external consumers of mount options from reading mnt_optnew.
1205 mp->mnt_optnew = NULL;
1208 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1209 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1210 mp->mnt_kern_flag |= MNTK_ASYNC;
1212 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1216 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
1217 vp->v_mountedhere = mp;
1222 * We need to lock both vnodes.
1224 * Use vn_lock_pair to avoid establishing an ordering between vnodes
1225 * from different filesystems.
1227 vn_lock_pair(vp, false, newdp, false);
1230 vp->v_iflag &= ~VI_MOUNT;
1232 /* Place the new filesystem at the end of the mount list. */
1233 mtx_lock(&mountlist_mtx);
1234 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1235 mtx_unlock(&mountlist_mtx);
1236 vfs_event_signal(NULL, VQ_MOUNT, 0);
1238 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
1240 mount_devctl_event("MOUNT", mp, false);
1241 mountcheckdirs(vp, newdp);
1242 vn_seqc_write_end(vp);
1243 vn_seqc_write_end(newdp);
1245 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1246 vfs_allocate_syncvnode(mp);
1253 * vfs_domount_update(): update of mounted file system
1257 struct thread *td, /* Calling thread. */
1258 struct vnode *vp, /* Mount point vnode. */
1259 uint64_t fsflags, /* Flags common to all filesystems. */
1260 struct vfsoptlist **optlist /* Options local to the filesystem. */
1263 struct export_args export;
1264 struct o2export_args o2export;
1265 struct vnode *rootvp;
1268 int error, export_error, i, len;
1272 ASSERT_VOP_ELOCKED(vp, __func__);
1273 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
1276 if ((vp->v_vflag & VV_ROOT) == 0) {
1277 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
1287 * We only allow the filesystem to be reloaded if it
1288 * is currently mounted read-only.
1290 flag = mp->mnt_flag;
1291 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
1293 return (EOPNOTSUPP); /* Needs translation */
1296 * Only privileged root, or (if MNT_USER is set) the user that
1297 * did the original mount is permitted to update it.
1299 error = vfs_suser(mp, td);
1304 if (vfs_busy(mp, MBF_NOWAIT)) {
1309 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1315 vp->v_iflag |= VI_MOUNT;
1320 vn_seqc_write_begin(vp);
1324 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1329 mp->mnt_flag &= ~MNT_UPDATEMASK;
1330 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1331 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1332 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1333 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1334 rootvp = vfs_cache_root_clear(mp);
1336 mp->mnt_optnew = *optlist;
1337 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1340 * Mount the filesystem.
1341 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1342 * get. No freeing of cn_pnbuf.
1344 error = VFS_MOUNT(mp);
1347 /* Process the export option. */
1348 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1350 /* Assume that there is only 1 ABI for each length. */
1352 case (sizeof(struct oexport_args)):
1353 bzero(&o2export, sizeof(o2export));
1355 case (sizeof(o2export)):
1356 bcopy(bufp, &o2export, len);
1357 export.ex_flags = (uint64_t)o2export.ex_flags;
1358 export.ex_root = o2export.ex_root;
1359 export.ex_uid = o2export.ex_anon.cr_uid;
1360 export.ex_groups = NULL;
1361 export.ex_ngroups = o2export.ex_anon.cr_ngroups;
1362 if (export.ex_ngroups > 0) {
1363 if (export.ex_ngroups <= XU_NGROUPS) {
1364 export.ex_groups = malloc(
1365 export.ex_ngroups * sizeof(gid_t),
1367 for (i = 0; i < export.ex_ngroups; i++)
1368 export.ex_groups[i] =
1369 o2export.ex_anon.cr_groups[i];
1371 export_error = EINVAL;
1372 } else if (export.ex_ngroups < 0)
1373 export_error = EINVAL;
1374 export.ex_addr = o2export.ex_addr;
1375 export.ex_addrlen = o2export.ex_addrlen;
1376 export.ex_mask = o2export.ex_mask;
1377 export.ex_masklen = o2export.ex_masklen;
1378 export.ex_indexfile = o2export.ex_indexfile;
1379 export.ex_numsecflavors = o2export.ex_numsecflavors;
1380 if (export.ex_numsecflavors < MAXSECFLAVORS) {
1381 for (i = 0; i < export.ex_numsecflavors; i++)
1382 export.ex_secflavors[i] =
1383 o2export.ex_secflavors[i];
1385 export_error = EINVAL;
1386 if (export_error == 0)
1387 export_error = vfs_export(mp, &export);
1388 free(export.ex_groups, M_TEMP);
1390 case (sizeof(export)):
1391 bcopy(bufp, &export, len);
1393 if (export.ex_ngroups > 0) {
1394 if (export.ex_ngroups <= NGROUPS_MAX) {
1395 grps = malloc(export.ex_ngroups *
1396 sizeof(gid_t), M_TEMP, M_WAITOK);
1397 export_error = copyin(export.ex_groups,
1398 grps, export.ex_ngroups *
1400 if (export_error == 0)
1401 export.ex_groups = grps;
1403 export_error = EINVAL;
1404 } else if (export.ex_ngroups == 0)
1405 export.ex_groups = NULL;
1407 export_error = EINVAL;
1408 if (export_error == 0)
1409 export_error = vfs_export(mp, &export);
1413 export_error = EINVAL;
1420 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1424 * If we fail, restore old mount flags. MNT_QUOTA is special,
1425 * because it is not part of MNT_UPDATEMASK, but it could have
1426 * changed in the meantime if quotactl(2) was called.
1427 * All in all we want current value of MNT_QUOTA, not the old
1430 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1432 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1433 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1434 mp->mnt_kern_flag |= MNTK_ASYNC;
1436 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1442 mount_devctl_event("REMOUNT", mp, true);
1443 if (mp->mnt_opt != NULL)
1444 vfs_freeopts(mp->mnt_opt);
1445 mp->mnt_opt = mp->mnt_optnew;
1447 (void)VFS_STATFS(mp, &mp->mnt_stat);
1449 * Prevent external consumers of mount options from reading
1452 mp->mnt_optnew = NULL;
1454 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1455 vfs_allocate_syncvnode(mp);
1457 vfs_deallocate_syncvnode(mp);
1460 if (rootvp != NULL) {
1461 vn_seqc_write_end(rootvp);
1464 vn_seqc_write_end(vp);
1467 vp->v_iflag &= ~VI_MOUNT;
1470 return (error != 0 ? error : export_error);
1474 * vfs_domount(): actually attempt a filesystem mount.
1478 struct thread *td, /* Calling thread. */
1479 const char *fstype, /* Filesystem type. */
1480 char *fspath, /* Mount path. */
1481 uint64_t fsflags, /* Flags common to all filesystems. */
1482 struct vfsoptlist **optlist /* Options local to the filesystem. */
1485 struct vfsconf *vfsp;
1486 struct nameidata nd;
1492 * Be ultra-paranoid about making sure the type and fspath
1493 * variables will fit in our mp buffers, including the
1496 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1497 return (ENAMETOOLONG);
1499 if (jailed(td->td_ucred) || usermount == 0) {
1500 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1505 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1507 if (fsflags & MNT_EXPORTED) {
1508 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1512 if (fsflags & MNT_SUIDDIR) {
1513 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1518 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1520 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1521 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1522 fsflags |= MNT_NOSUID | MNT_USER;
1525 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1527 if ((fsflags & MNT_UPDATE) == 0) {
1528 /* Don't try to load KLDs if we're mounting the root. */
1529 if (fsflags & MNT_ROOTFS) {
1530 if ((vfsp = vfs_byname(fstype)) == NULL)
1533 if ((vfsp = vfs_byname_kld(fstype, td, &error)) == NULL)
1539 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1541 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
1546 NDFREE(&nd, NDF_ONLY_PNBUF);
1548 if ((fsflags & MNT_UPDATE) == 0) {
1549 if ((vp->v_vflag & VV_ROOT) != 0 &&
1550 (fsflags & MNT_NOCOVER) != 0) {
1554 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1555 strcpy(pathbuf, fspath);
1556 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1558 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1561 free(pathbuf, M_TEMP);
1563 error = vfs_domount_update(td, vp, fsflags, optlist);
1569 * Unmount a filesystem.
1571 * Note: unmount takes a path to the vnode mounted on as argument, not
1572 * special file (as before).
1574 #ifndef _SYS_SYSPROTO_H_
1575 struct unmount_args {
1582 sys_unmount(struct thread *td, struct unmount_args *uap)
1585 return (kern_unmount(td, uap->path, uap->flags));
1589 kern_unmount(struct thread *td, const char *path, int flags)
1591 struct nameidata nd;
1593 char *fsidbuf, *pathbuf;
1597 AUDIT_ARG_VALUE(flags);
1598 if (jailed(td->td_ucred) || usermount == 0) {
1599 error = priv_check(td, PRIV_VFS_UNMOUNT);
1604 if (flags & MNT_BYFSID) {
1605 fsidbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1606 error = copyinstr(path, fsidbuf, MNAMELEN, NULL);
1608 free(fsidbuf, M_TEMP);
1612 AUDIT_ARG_TEXT(fsidbuf);
1613 /* Decode the filesystem ID. */
1614 if (sscanf(fsidbuf, "FSID:%d:%d", &fsid.val[0], &fsid.val[1]) != 2) {
1615 free(fsidbuf, M_TEMP);
1619 mp = vfs_getvfs(&fsid);
1620 free(fsidbuf, M_TEMP);
1625 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1626 error = copyinstr(path, pathbuf, MNAMELEN, NULL);
1628 free(pathbuf, M_TEMP);
1633 * Try to find global path for path argument.
1635 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1636 UIO_SYSSPACE, pathbuf);
1637 if (namei(&nd) == 0) {
1638 NDFREE(&nd, NDF_ONLY_PNBUF);
1639 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1644 mtx_lock(&mountlist_mtx);
1645 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1646 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1651 mtx_unlock(&mountlist_mtx);
1652 free(pathbuf, M_TEMP);
1655 * Previously we returned ENOENT for a nonexistent path and
1656 * EINVAL for a non-mountpoint. We cannot tell these apart
1657 * now, so in the !MNT_BYFSID case return the more likely
1658 * EINVAL for compatibility.
1665 * Don't allow unmounting the root filesystem.
1667 if (mp->mnt_flag & MNT_ROOTFS) {
1671 error = dounmount(mp, flags, td);
1676 * Return error if any of the vnodes, ignoring the root vnode
1677 * and the syncer vnode, have non-zero usecount.
1679 * This function is purely advisory - it can return false positives
1683 vfs_check_usecounts(struct mount *mp)
1685 struct vnode *vp, *mvp;
1687 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1688 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1689 vp->v_usecount != 0) {
1691 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1701 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1704 mtx_assert(MNT_MTX(mp), MA_OWNED);
1705 mp->mnt_kern_flag &= ~mntkflags;
1706 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1707 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1710 vfs_op_exit_locked(mp);
1712 if (coveredvp != NULL) {
1713 VOP_UNLOCK(coveredvp);
1716 vn_finished_write(mp);
1720 * There are various reference counters associated with the mount point.
1721 * Normally it is permitted to modify them without taking the mnt ilock,
1722 * but this behavior can be temporarily disabled if stable value is needed
1723 * or callers are expected to block (e.g. to not allow new users during
1727 vfs_op_enter(struct mount *mp)
1729 struct mount_pcpu *mpcpu;
1734 if (mp->mnt_vfs_ops > 1) {
1738 vfs_op_barrier_wait(mp);
1740 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1742 mp->mnt_ref += mpcpu->mntp_ref;
1743 mpcpu->mntp_ref = 0;
1745 mp->mnt_lockref += mpcpu->mntp_lockref;
1746 mpcpu->mntp_lockref = 0;
1748 mp->mnt_writeopcount += mpcpu->mntp_writeopcount;
1749 mpcpu->mntp_writeopcount = 0;
1751 if (mp->mnt_ref <= 0 || mp->mnt_lockref < 0 || mp->mnt_writeopcount < 0)
1752 panic("%s: invalid count(s) on mp %p: ref %d lockref %d writeopcount %d\n",
1753 __func__, mp, mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount);
1755 vfs_assert_mount_counters(mp);
1759 vfs_op_exit_locked(struct mount *mp)
1762 mtx_assert(MNT_MTX(mp), MA_OWNED);
1764 if (mp->mnt_vfs_ops <= 0)
1765 panic("%s: invalid vfs_ops count %d for mp %p\n",
1766 __func__, mp->mnt_vfs_ops, mp);
1771 vfs_op_exit(struct mount *mp)
1775 vfs_op_exit_locked(mp);
1779 struct vfs_op_barrier_ipi {
1781 struct smp_rendezvous_cpus_retry_arg srcra;
1785 vfs_op_action_func(void *arg)
1787 struct vfs_op_barrier_ipi *vfsopipi;
1790 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1793 if (!vfs_op_thread_entered(mp))
1794 smp_rendezvous_cpus_done(arg);
1798 vfs_op_wait_func(void *arg, int cpu)
1800 struct vfs_op_barrier_ipi *vfsopipi;
1802 struct mount_pcpu *mpcpu;
1804 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1807 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1808 while (atomic_load_int(&mpcpu->mntp_thread_in_ops))
1813 vfs_op_barrier_wait(struct mount *mp)
1815 struct vfs_op_barrier_ipi vfsopipi;
1819 smp_rendezvous_cpus_retry(all_cpus,
1820 smp_no_rendezvous_barrier,
1822 smp_no_rendezvous_barrier,
1829 vfs_assert_mount_counters(struct mount *mp)
1831 struct mount_pcpu *mpcpu;
1834 if (mp->mnt_vfs_ops == 0)
1838 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1839 if (mpcpu->mntp_ref != 0 ||
1840 mpcpu->mntp_lockref != 0 ||
1841 mpcpu->mntp_writeopcount != 0)
1842 vfs_dump_mount_counters(mp);
1847 vfs_dump_mount_counters(struct mount *mp)
1849 struct mount_pcpu *mpcpu;
1850 int ref, lockref, writeopcount;
1853 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);
1858 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1859 printf("%d ", mpcpu->mntp_ref);
1860 ref += mpcpu->mntp_ref;
1863 printf(" lockref : ");
1864 lockref = mp->mnt_lockref;
1866 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1867 printf("%d ", mpcpu->mntp_lockref);
1868 lockref += mpcpu->mntp_lockref;
1871 printf("writeopcount: ");
1872 writeopcount = mp->mnt_writeopcount;
1874 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1875 printf("%d ", mpcpu->mntp_writeopcount);
1876 writeopcount += mpcpu->mntp_writeopcount;
1880 printf("counter struct total\n");
1881 printf("ref %-5d %-5d\n", mp->mnt_ref, ref);
1882 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref);
1883 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount);
1885 panic("invalid counts on struct mount");
1890 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
1892 struct mount_pcpu *mpcpu;
1899 case MNT_COUNT_LOCKREF:
1900 sum = mp->mnt_lockref;
1902 case MNT_COUNT_WRITEOPCOUNT:
1903 sum = mp->mnt_writeopcount;
1908 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1911 sum += mpcpu->mntp_ref;
1913 case MNT_COUNT_LOCKREF:
1914 sum += mpcpu->mntp_lockref;
1916 case MNT_COUNT_WRITEOPCOUNT:
1917 sum += mpcpu->mntp_writeopcount;
1925 deferred_unmount_enqueue(struct mount *mp, uint64_t flags, bool requeue,
1931 mtx_lock(&deferred_unmount_lock);
1932 if ((mp->mnt_taskqueue_flags & MNT_DEFERRED) == 0 || requeue) {
1933 mp->mnt_taskqueue_flags = flags | MNT_DEFERRED;
1934 STAILQ_INSERT_TAIL(&deferred_unmount_list, mp,
1935 mnt_taskqueue_link);
1938 mtx_unlock(&deferred_unmount_lock);
1941 taskqueue_enqueue_timeout(taskqueue_deferred_unmount,
1942 &deferred_unmount_task, timeout_ticks);
1949 * Taskqueue handler for processing async/recursive unmounts
1952 vfs_deferred_unmount(void *argi __unused, int pending __unused)
1954 STAILQ_HEAD(, mount) local_unmounts;
1956 struct mount *mp, *tmp;
1958 unsigned int retries;
1961 STAILQ_INIT(&local_unmounts);
1962 mtx_lock(&deferred_unmount_lock);
1963 STAILQ_CONCAT(&local_unmounts, &deferred_unmount_list);
1964 mtx_unlock(&deferred_unmount_lock);
1966 STAILQ_FOREACH_SAFE(mp, &local_unmounts, mnt_taskqueue_link, tmp) {
1967 flags = mp->mnt_taskqueue_flags;
1968 KASSERT((flags & MNT_DEFERRED) != 0,
1969 ("taskqueue unmount without MNT_DEFERRED"));
1970 error = dounmount(mp, flags, curthread);
1973 unmounted = ((mp->mnt_kern_flag & MNTK_REFEXPIRE) != 0);
1977 * The deferred unmount thread is the only thread that
1978 * modifies the retry counts, so locking/atomics aren't
1981 retries = (mp->mnt_unmount_retries)++;
1982 deferred_unmount_total_retries++;
1983 if (!unmounted && retries < deferred_unmount_retry_limit) {
1984 deferred_unmount_enqueue(mp, flags, true,
1985 -deferred_unmount_retry_delay_hz);
1987 if (retries >= deferred_unmount_retry_limit) {
1988 printf("giving up on deferred unmount "
1989 "of %s after %d retries, error %d\n",
1990 mp->mnt_stat.f_mntonname, retries, error);
1999 * Do the actual filesystem unmount.
2002 dounmount(struct mount *mp, uint64_t flags, struct thread *td)
2004 struct mount_upper_node *upper;
2005 struct vnode *coveredvp, *rootvp;
2007 uint64_t async_flag;
2009 unsigned int retries;
2011 KASSERT((flags & MNT_DEFERRED) == 0 ||
2012 (flags & (MNT_RECURSE | MNT_FORCE)) == (MNT_RECURSE | MNT_FORCE),
2013 ("MNT_DEFERRED requires MNT_RECURSE | MNT_FORCE"));
2016 * If the caller has explicitly requested the unmount to be handled by
2017 * the taskqueue and we're not already in taskqueue context, queue
2018 * up the unmount request and exit. This is done prior to any
2019 * credential checks; MNT_DEFERRED should be used only for kernel-
2020 * initiated unmounts and will therefore be processed with the
2021 * (kernel) credentials of the taskqueue thread. Still, callers
2022 * should be sure this is the behavior they want.
2024 if ((flags & MNT_DEFERRED) != 0 &&
2025 taskqueue_member(taskqueue_deferred_unmount, curthread) == 0) {
2026 if (!deferred_unmount_enqueue(mp, flags, false, 0))
2028 return (EINPROGRESS);
2032 * Only privileged root, or (if MNT_USER is set) the user that did the
2033 * original mount is permitted to unmount this filesystem.
2034 * This check should be made prior to queueing up any recursive
2035 * unmounts of upper filesystems. Those unmounts will be executed
2036 * with kernel thread credentials and are expected to succeed, so
2037 * we must at least ensure the originating context has sufficient
2038 * privilege to unmount the base filesystem before proceeding with
2041 error = vfs_suser(mp, td);
2043 KASSERT((flags & MNT_DEFERRED) == 0,
2044 ("taskqueue unmount with insufficient privilege"));
2049 if (recursive_forced_unmount && ((flags & MNT_FORCE) != 0))
2050 flags |= MNT_RECURSE;
2052 if ((flags & MNT_RECURSE) != 0) {
2053 KASSERT((flags & MNT_FORCE) != 0,
2054 ("MNT_RECURSE requires MNT_FORCE"));
2058 * Set MNTK_RECURSE to prevent new upper mounts from being
2059 * added, and note that an operation on the uppers list is in
2060 * progress. This will ensure that unregistration from the
2061 * uppers list, and therefore any pending unmount of the upper
2062 * FS, can't complete until after we finish walking the list.
2064 mp->mnt_kern_flag |= MNTK_RECURSE;
2065 mp->mnt_upper_pending++;
2066 TAILQ_FOREACH(upper, &mp->mnt_uppers, mnt_upper_link) {
2067 retries = upper->mp->mnt_unmount_retries;
2068 if (retries > deferred_unmount_retry_limit) {
2075 if (!deferred_unmount_enqueue(upper->mp, flags,
2080 mp->mnt_upper_pending--;
2081 if ((mp->mnt_kern_flag & MNTK_UPPER_WAITER) != 0 &&
2082 mp->mnt_upper_pending == 0) {
2083 mp->mnt_kern_flag &= ~MNTK_UPPER_WAITER;
2084 wakeup(&mp->mnt_uppers);
2088 * If we're not on the taskqueue, wait until the uppers list
2089 * is drained before proceeding with unmount. Otherwise, if
2090 * we are on the taskqueue and there are still pending uppers,
2091 * just re-enqueue on the end of the taskqueue.
2093 if ((flags & MNT_DEFERRED) == 0) {
2094 while (error == 0 && !TAILQ_EMPTY(&mp->mnt_uppers)) {
2095 mp->mnt_kern_flag |= MNTK_TASKQUEUE_WAITER;
2096 error = msleep(&mp->mnt_taskqueue_link,
2097 MNT_MTX(mp), PCATCH, "umntqw", 0);
2104 } else if (!TAILQ_EMPTY(&mp->mnt_uppers)) {
2107 deferred_unmount_enqueue(mp, flags, true, 0);
2111 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers not empty"));
2114 /* Allow the taskqueue to safely re-enqueue on failure */
2115 if ((flags & MNT_DEFERRED) != 0)
2118 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
2119 mnt_gen_r = mp->mnt_gen;
2122 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
2124 * Check for mp being unmounted while waiting for the
2125 * covered vnode lock.
2127 if (coveredvp->v_mountedhere != mp ||
2128 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
2129 VOP_UNLOCK(coveredvp);
2138 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
2140 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
2141 (mp->mnt_flag & MNT_UPDATE) != 0 ||
2142 !TAILQ_EMPTY(&mp->mnt_uppers)) {
2143 dounmount_cleanup(mp, coveredvp, 0);
2146 mp->mnt_kern_flag |= MNTK_UNMOUNT;
2147 rootvp = vfs_cache_root_clear(mp);
2148 if (coveredvp != NULL)
2149 vn_seqc_write_begin(coveredvp);
2150 if (flags & MNT_NONBUSY) {
2152 error = vfs_check_usecounts(mp);
2155 vn_seqc_write_end(coveredvp);
2156 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
2157 if (rootvp != NULL) {
2158 vn_seqc_write_end(rootvp);
2164 /* Allow filesystems to detect that a forced unmount is in progress. */
2165 if (flags & MNT_FORCE) {
2166 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
2169 * Must be done after setting MNTK_UNMOUNTF and before
2170 * waiting for mnt_lockref to become 0.
2176 if (mp->mnt_lockref) {
2177 mp->mnt_kern_flag |= MNTK_DRAINING;
2178 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
2182 KASSERT(mp->mnt_lockref == 0,
2183 ("%s: invalid lock refcount in the drain path @ %s:%d",
2184 __func__, __FILE__, __LINE__));
2186 ("%s: invalid return value for msleep in the drain path @ %s:%d",
2187 __func__, __FILE__, __LINE__));
2190 * We want to keep the vnode around so that we can vn_seqc_write_end
2191 * after we are done with unmount. Downgrade our reference to a mere
2192 * hold count so that we don't interefere with anything.
2194 if (rootvp != NULL) {
2199 if (mp->mnt_flag & MNT_EXPUBLIC)
2200 vfs_setpublicfs(NULL, NULL, NULL);
2202 vfs_periodic(mp, MNT_WAIT);
2204 async_flag = mp->mnt_flag & MNT_ASYNC;
2205 mp->mnt_flag &= ~MNT_ASYNC;
2206 mp->mnt_kern_flag &= ~MNTK_ASYNC;
2208 vfs_deallocate_syncvnode(mp);
2209 error = VFS_UNMOUNT(mp, flags);
2210 vn_finished_write(mp);
2212 * If we failed to flush the dirty blocks for this mount point,
2213 * undo all the cdir/rdir and rootvnode changes we made above.
2214 * Unless we failed to do so because the device is reporting that
2215 * it doesn't exist anymore.
2217 if (error && error != ENXIO) {
2219 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
2221 vfs_allocate_syncvnode(mp);
2224 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
2225 mp->mnt_flag |= async_flag;
2226 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
2227 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
2228 mp->mnt_kern_flag |= MNTK_ASYNC;
2229 if (mp->mnt_kern_flag & MNTK_MWAIT) {
2230 mp->mnt_kern_flag &= ~MNTK_MWAIT;
2233 vfs_op_exit_locked(mp);
2236 vn_seqc_write_end(coveredvp);
2237 VOP_UNLOCK(coveredvp);
2240 if (rootvp != NULL) {
2241 vn_seqc_write_end(rootvp);
2247 mtx_lock(&mountlist_mtx);
2248 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2249 mtx_unlock(&mountlist_mtx);
2250 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
2251 if (coveredvp != NULL) {
2253 vn_irflag_unset_locked(coveredvp, VIRF_MOUNTPOINT);
2254 coveredvp->v_mountedhere = NULL;
2255 vn_seqc_write_end_locked(coveredvp);
2256 VI_UNLOCK(coveredvp);
2257 VOP_UNLOCK(coveredvp);
2260 mount_devctl_event("UNMOUNT", mp, false);
2261 if (rootvp != NULL) {
2262 vn_seqc_write_end(rootvp);
2265 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
2266 if (rootvnode != NULL && mp == rootvnode->v_mount) {
2270 if (mp == rootdevmp)
2272 if ((flags & MNT_DEFERRED) != 0)
2274 vfs_mount_destroy(mp);
2279 * Report errors during filesystem mounting.
2282 vfs_mount_error(struct mount *mp, const char *fmt, ...)
2284 struct vfsoptlist *moptlist = mp->mnt_optnew;
2289 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
2290 if (error || errmsg == NULL || len <= 0)
2294 vsnprintf(errmsg, (size_t)len, fmt, ap);
2299 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
2305 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
2306 if (error || errmsg == NULL || len <= 0)
2310 vsnprintf(errmsg, (size_t)len, fmt, ap);
2315 * ---------------------------------------------------------------------
2316 * Functions for querying mount options/arguments from filesystems.
2320 * Check that no unknown options are given
2323 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
2327 const char **t, *p, *q;
2330 TAILQ_FOREACH(opt, opts, link) {
2333 if (p[0] == 'n' && p[1] == 'o')
2335 for(t = global_opts; *t != NULL; t++) {
2336 if (strcmp(*t, p) == 0)
2339 if (strcmp(*t, q) == 0)
2345 for(t = legal; *t != NULL; t++) {
2346 if (strcmp(*t, p) == 0)
2349 if (strcmp(*t, q) == 0)
2355 snprintf(errmsg, sizeof(errmsg),
2356 "mount option <%s> is unknown", p);
2360 TAILQ_FOREACH(opt, opts, link) {
2361 if (strcmp(opt->name, "errmsg") == 0) {
2362 strncpy((char *)opt->value, errmsg, opt->len);
2367 printf("%s\n", errmsg);
2373 * Get a mount option by its name.
2375 * Return 0 if the option was found, ENOENT otherwise.
2376 * If len is non-NULL it will be filled with the length
2377 * of the option. If buf is non-NULL, it will be filled
2378 * with the address of the option.
2381 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
2385 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2387 TAILQ_FOREACH(opt, opts, link) {
2388 if (strcmp(name, opt->name) == 0) {
2401 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2408 TAILQ_FOREACH(opt, opts, link) {
2409 if (strcmp(name, opt->name) == 0) {
2418 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
2420 char *opt_value, *vtp;
2424 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
2427 if (opt_len == 0 || opt_value == NULL)
2429 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
2431 iv = strtoq(opt_value, &vtp, 0);
2432 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
2459 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2464 TAILQ_FOREACH(opt, opts, link) {
2465 if (strcmp(name, opt->name) != 0)
2468 if (opt->len == 0 ||
2469 ((char *)opt->value)[opt->len - 1] != '\0') {
2473 return (opt->value);
2480 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
2485 TAILQ_FOREACH(opt, opts, link) {
2486 if (strcmp(name, opt->name) == 0) {
2499 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2505 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2507 TAILQ_FOREACH(opt, opts, link) {
2508 if (strcmp(name, opt->name) != 0)
2511 if (opt->len == 0 || opt->value == NULL)
2513 if (((char *)opt->value)[opt->len - 1] != '\0')
2516 ret = vsscanf(opt->value, fmt, ap);
2524 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2528 TAILQ_FOREACH(opt, opts, link) {
2529 if (strcmp(name, opt->name) != 0)
2532 if (opt->value == NULL)
2535 if (opt->len != len)
2537 bcopy(value, opt->value, len);
2545 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2549 TAILQ_FOREACH(opt, opts, link) {
2550 if (strcmp(name, opt->name) != 0)
2553 if (opt->value == NULL)
2559 bcopy(value, opt->value, len);
2567 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2571 TAILQ_FOREACH(opt, opts, link) {
2572 if (strcmp(name, opt->name) != 0)
2575 if (opt->value == NULL)
2576 opt->len = strlen(value) + 1;
2577 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2585 * Find and copy a mount option.
2587 * The size of the buffer has to be specified
2588 * in len, if it is not the same length as the
2589 * mount option, EINVAL is returned.
2590 * Returns ENOENT if the option is not found.
2593 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
2597 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2599 TAILQ_FOREACH(opt, opts, link) {
2600 if (strcmp(name, opt->name) == 0) {
2602 if (len != opt->len)
2604 bcopy(opt->value, dest, opt->len);
2612 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2616 * Filesystems only fill in part of the structure for updates, we
2617 * have to read the entirety first to get all content.
2619 if (sbp != &mp->mnt_stat)
2620 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));
2623 * Set these in case the underlying filesystem fails to do so.
2625 sbp->f_version = STATFS_VERSION;
2626 sbp->f_namemax = NAME_MAX;
2627 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2629 return (mp->mnt_op->vfs_statfs(mp, sbp));
2633 vfs_mountedfrom(struct mount *mp, const char *from)
2636 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2637 strlcpy(mp->mnt_stat.f_mntfromname, from,
2638 sizeof mp->mnt_stat.f_mntfromname);
2642 * ---------------------------------------------------------------------
2643 * This is the api for building mount args and mounting filesystems from
2644 * inside the kernel.
2646 * The API works by accumulation of individual args. First error is
2649 * XXX: should be documented in new manpage kernel_mount(9)
2652 /* A memory allocation which must be freed when we are done */
2654 SLIST_ENTRY(mntaarg) next;
2657 /* The header for the mount arguments */
2662 SLIST_HEAD(, mntaarg) list;
2666 * Add a boolean argument.
2668 * flag is the boolean value.
2669 * name must start with "no".
2672 mount_argb(struct mntarg *ma, int flag, const char *name)
2675 KASSERT(name[0] == 'n' && name[1] == 'o',
2676 ("mount_argb(...,%s): name must start with 'no'", name));
2678 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2682 * Add an argument printf style
2685 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2688 struct mntaarg *maa;
2693 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2694 SLIST_INIT(&ma->list);
2699 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2701 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2702 ma->v[ma->len].iov_len = strlen(name) + 1;
2705 sb = sbuf_new_auto();
2707 sbuf_vprintf(sb, fmt, ap);
2710 len = sbuf_len(sb) + 1;
2711 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2712 SLIST_INSERT_HEAD(&ma->list, maa, next);
2713 bcopy(sbuf_data(sb), maa + 1, len);
2716 ma->v[ma->len].iov_base = maa + 1;
2717 ma->v[ma->len].iov_len = len;
2724 * Add an argument which is a userland string.
2727 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2729 struct mntaarg *maa;
2735 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2736 SLIST_INIT(&ma->list);
2740 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2741 SLIST_INSERT_HEAD(&ma->list, maa, next);
2742 tbuf = (void *)(maa + 1);
2743 ma->error = copyinstr(val, tbuf, len, NULL);
2744 return (mount_arg(ma, name, tbuf, -1));
2750 * If length is -1, treat value as a C string.
2753 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2757 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2758 SLIST_INIT(&ma->list);
2763 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2765 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2766 ma->v[ma->len].iov_len = strlen(name) + 1;
2769 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2771 ma->v[ma->len].iov_len = strlen(val) + 1;
2773 ma->v[ma->len].iov_len = len;
2779 * Free a mntarg structure
2782 free_mntarg(struct mntarg *ma)
2784 struct mntaarg *maa;
2786 while (!SLIST_EMPTY(&ma->list)) {
2787 maa = SLIST_FIRST(&ma->list);
2788 SLIST_REMOVE_HEAD(&ma->list, next);
2791 free(ma->v, M_MOUNT);
2796 * Mount a filesystem
2799 kernel_mount(struct mntarg *ma, uint64_t flags)
2804 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2805 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2806 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2808 auio.uio_iov = ma->v;
2809 auio.uio_iovcnt = ma->len;
2810 auio.uio_segflg = UIO_SYSSPACE;
2814 error = vfs_donmount(curthread, flags, &auio);
2819 /* Map from mount options to printable formats. */
2820 static struct mntoptnames optnames[] = {
2824 #define DEVCTL_LEN 1024
2826 mount_devctl_event(const char *type, struct mount *mp, bool donew)
2829 struct mntoptnames *fp;
2831 struct statfs *sfp = &mp->mnt_stat;
2834 buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT);
2837 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
2838 sbuf_cpy(&sb, "mount-point=\"");
2839 devctl_safe_quote_sb(&sb, sfp->f_mntonname);
2840 sbuf_cat(&sb, "\" mount-dev=\"");
2841 devctl_safe_quote_sb(&sb, sfp->f_mntfromname);
2842 sbuf_cat(&sb, "\" mount-type=\"");
2843 devctl_safe_quote_sb(&sb, sfp->f_fstypename);
2844 sbuf_cat(&sb, "\" fsid=0x");
2845 cp = (const uint8_t *)&sfp->f_fsid.val[0];
2846 for (int i = 0; i < sizeof(sfp->f_fsid); i++)
2847 sbuf_printf(&sb, "%02x", cp[i]);
2848 sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner);
2849 for (fp = optnames; fp->o_opt != 0; fp++) {
2850 if ((mp->mnt_flag & fp->o_opt) != 0) {
2851 sbuf_cat(&sb, fp->o_name);
2852 sbuf_putc(&sb, ';');
2855 sbuf_putc(&sb, '"');
2859 * Options are not published because the form of the options depends on
2860 * the file system and may include binary data. In addition, they don't
2861 * necessarily provide enough useful information to be actionable when
2862 * devd processes them.
2865 if (sbuf_error(&sb) == 0)
2866 devctl_notify("VFS", "FS", type, sbuf_data(&sb));
2872 * Force remount specified mount point to read-only. The argument
2873 * must be busied to avoid parallel unmount attempts.
2875 * Intended use is to prevent further writes if some metadata
2876 * inconsistency is detected. Note that the function still flushes
2877 * all cached metadata and data for the mount point, which might be
2878 * not always suitable.
2881 vfs_remount_ro(struct mount *mp)
2883 struct vfsoptlist *opts;
2885 struct vnode *vp_covered, *rootvp;
2888 KASSERT(mp->mnt_lockref > 0,
2889 ("vfs_remount_ro: mp %p is not busied", mp));
2890 KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0,
2891 ("vfs_remount_ro: mp %p is being unmounted (and busy?)", mp));
2894 vp_covered = mp->mnt_vnodecovered;
2895 error = vget(vp_covered, LK_EXCLUSIVE | LK_NOWAIT);
2898 VI_LOCK(vp_covered);
2899 if ((vp_covered->v_iflag & VI_MOUNT) != 0) {
2900 VI_UNLOCK(vp_covered);
2904 vp_covered->v_iflag |= VI_MOUNT;
2905 VI_UNLOCK(vp_covered);
2907 vn_seqc_write_begin(vp_covered);
2910 if ((mp->mnt_flag & MNT_RDONLY) != 0) {
2915 mp->mnt_flag |= MNT_UPDATE | MNT_FORCE | MNT_RDONLY;
2916 rootvp = vfs_cache_root_clear(mp);
2919 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK | M_ZERO);
2921 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK | M_ZERO);
2922 opt->name = strdup("ro", M_MOUNT);
2924 TAILQ_INSERT_TAIL(opts, opt, link);
2925 vfs_mergeopts(opts, mp->mnt_opt);
2926 mp->mnt_optnew = opts;
2928 error = VFS_MOUNT(mp);
2932 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE);
2934 vfs_deallocate_syncvnode(mp);
2935 if (mp->mnt_opt != NULL)
2936 vfs_freeopts(mp->mnt_opt);
2937 mp->mnt_opt = mp->mnt_optnew;
2940 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE | MNT_RDONLY);
2942 vfs_freeopts(mp->mnt_optnew);
2944 mp->mnt_optnew = NULL;
2948 VI_LOCK(vp_covered);
2949 vp_covered->v_iflag &= ~VI_MOUNT;
2950 VI_UNLOCK(vp_covered);
2952 vn_seqc_write_end(vp_covered);
2953 if (rootvp != NULL) {
2954 vn_seqc_write_end(rootvp);
2961 * Suspend write operations on all local writeable filesystems. Does
2962 * full sync of them in the process.
2964 * Iterate over the mount points in reverse order, suspending most
2965 * recently mounted filesystems first. It handles a case where a
2966 * filesystem mounted from a md(4) vnode-backed device should be
2967 * suspended before the filesystem that owns the vnode.
2970 suspend_all_fs(void)
2975 mtx_lock(&mountlist_mtx);
2976 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
2977 error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT);
2980 if ((mp->mnt_flag & (MNT_RDONLY | MNT_LOCAL)) != MNT_LOCAL ||
2981 (mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
2982 mtx_lock(&mountlist_mtx);
2986 error = vfs_write_suspend(mp, 0);
2989 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0);
2990 mp->mnt_kern_flag |= MNTK_SUSPEND_ALL;
2992 mtx_lock(&mountlist_mtx);
2994 printf("suspend of %s failed, error %d\n",
2995 mp->mnt_stat.f_mntonname, error);
2996 mtx_lock(&mountlist_mtx);
3000 mtx_unlock(&mountlist_mtx);
3008 mtx_lock(&mountlist_mtx);
3009 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3010 if ((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0)
3012 mtx_unlock(&mountlist_mtx);
3014 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) != 0);
3015 mp->mnt_kern_flag &= ~MNTK_SUSPEND_ALL;
3017 vfs_write_resume(mp, 0);
3018 mtx_lock(&mountlist_mtx);
3021 mtx_unlock(&mountlist_mtx);