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/systm.h>
67 #include <sys/taskqueue.h>
68 #include <sys/vnode.h>
71 #include <geom/geom.h>
73 #include <machine/stdarg.h>
75 #include <security/audit/audit.h>
76 #include <security/mac/mac_framework.h>
78 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
80 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
81 uint64_t fsflags, bool jail_export,
82 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 MPPASS(mp->mnt_ref == 0 && mp->mnt_lockref == 0 &&
684 mp->mnt_writeopcount == 0, mp);
685 MPASSERT(mp->mnt_vfs_ops == 1, mp,
686 ("vfs_ops should be 1 but %d found", mp->mnt_vfs_ops));
687 (void) vfs_busy(mp, MBF_NOWAIT);
688 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
689 mp->mnt_op = vfsp->vfc_vfsops;
691 mp->mnt_stat.f_type = vfsp->vfc_typenum;
693 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
694 mp->mnt_vnodecovered = vp;
695 mp->mnt_cred = crdup(cred);
696 mp->mnt_stat.f_owner = cred->cr_uid;
697 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
698 mp->mnt_iosize_max = DFLTPHYS;
701 mac_mount_create(cred, mp);
703 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
704 mp->mnt_upper_pending = 0;
705 TAILQ_INIT(&mp->mnt_uppers);
706 TAILQ_INIT(&mp->mnt_notify);
707 mp->mnt_taskqueue_flags = 0;
708 mp->mnt_unmount_retries = 0;
713 * Destroy the mount struct previously allocated by vfs_mount_alloc().
716 vfs_mount_destroy(struct mount *mp)
719 MPPASS(mp->mnt_vfs_ops != 0, mp);
721 vfs_assert_mount_counters(mp);
724 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
725 if (mp->mnt_kern_flag & MNTK_MWAIT) {
726 mp->mnt_kern_flag &= ~MNTK_MWAIT;
730 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
731 KASSERT(mp->mnt_ref == 0,
732 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
733 __FILE__, __LINE__));
734 MPPASS(mp->mnt_writeopcount == 0, mp);
735 MPPASS(mp->mnt_secondary_writes == 0, mp);
736 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
737 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
740 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
741 vn_printf(vp, "dangling vnode ");
742 panic("unmount: dangling vnode");
744 KASSERT(mp->mnt_upper_pending == 0, ("mnt_upper_pending"));
745 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
746 KASSERT(TAILQ_EMPTY(&mp->mnt_notify), ("mnt_notify"));
747 MPPASS(mp->mnt_nvnodelistsize == 0, mp);
748 MPPASS(mp->mnt_lazyvnodelistsize == 0, mp);
749 MPPASS(mp->mnt_lockref == 0, mp);
752 MPASSERT(mp->mnt_vfs_ops == 1, mp,
753 ("vfs_ops should be 1 but %d found", mp->mnt_vfs_ops));
755 MPASSERT(mp->mnt_rootvnode == NULL, mp,
756 ("mount point still has a root vnode %p", mp->mnt_rootvnode));
758 if (mp->mnt_vnodecovered != NULL)
759 vrele(mp->mnt_vnodecovered);
761 mac_mount_destroy(mp);
763 if (mp->mnt_opt != NULL)
764 vfs_freeopts(mp->mnt_opt);
765 if (mp->mnt_exjail != NULL) {
766 atomic_subtract_int(&mp->mnt_exjail->cr_prison->pr_exportcnt,
768 crfree(mp->mnt_exjail);
770 if (mp->mnt_export != NULL) {
771 vfs_free_addrlist(mp->mnt_export);
772 free(mp->mnt_export, M_MOUNT);
774 crfree(mp->mnt_cred);
775 uma_zfree(mount_zone, mp);
779 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
781 /* This is an upgrade of an exisiting mount. */
782 if ((fsflags & MNT_UPDATE) != 0)
784 /* This is already an R/O mount. */
785 if ((fsflags & MNT_RDONLY) != 0)
789 case ENODEV: /* generic, geom, ... */
790 case EACCES: /* cam/scsi, ... */
791 case EROFS: /* md, mmcsd, ... */
793 * These errors can be returned by the storage layer to signal
794 * that the media is read-only. No harm in the R/O mount
795 * attempt if the error was returned for some other reason.
804 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
806 struct vfsoptlist *optlist;
807 struct vfsopt *opt, *tmp_opt;
808 char *fstype, *fspath, *errmsg;
809 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
810 bool autoro, has_nonexport, jail_export;
812 errmsg = fspath = NULL;
813 errmsg_len = fspathlen = 0;
815 autoro = default_autoro;
817 error = vfs_buildopts(fsoptions, &optlist);
821 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
822 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
825 * We need these two options before the others,
826 * and they are mandatory for any filesystem.
827 * Ensure they are NUL terminated as well.
830 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
831 if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
834 strncpy(errmsg, "Invalid fstype", errmsg_len);
838 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
839 if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
842 strncpy(errmsg, "Invalid fspath", errmsg_len);
847 * Check to see that "export" is only used with the "update", "fstype",
848 * "fspath", "from" and "errmsg" options when in a vnet jail.
849 * These are the ones used to set/update exports by mountd(8).
850 * If only the above options are set in a jail that can run mountd(8),
851 * then the jail_export argument of vfs_domount() will be true.
852 * When jail_export is true, the vfs_suser() check does not cause
853 * failure, but limits the update to exports only.
854 * This allows mountd(8) running within the vnet jail
855 * to export file systems visible within the jail, but
856 * mounted outside of the jail.
859 * We need to see if we have the "update" option
860 * before we call vfs_domount(), since vfs_domount() has special
861 * logic based on MNT_UPDATE. This is very important
862 * when we want to update the root filesystem.
864 has_nonexport = false;
866 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
869 if (jailed(td->td_ucred) &&
870 strcmp(opt->name, "export") != 0 &&
871 strcmp(opt->name, "update") != 0 &&
872 strcmp(opt->name, "fstype") != 0 &&
873 strcmp(opt->name, "fspath") != 0 &&
874 strcmp(opt->name, "from") != 0 &&
875 strcmp(opt->name, "errmsg") != 0)
876 has_nonexport = true;
877 if (strcmp(opt->name, "update") == 0) {
878 fsflags |= MNT_UPDATE;
881 else if (strcmp(opt->name, "async") == 0)
882 fsflags |= MNT_ASYNC;
883 else if (strcmp(opt->name, "force") == 0) {
884 fsflags |= MNT_FORCE;
887 else if (strcmp(opt->name, "reload") == 0) {
888 fsflags |= MNT_RELOAD;
891 else if (strcmp(opt->name, "multilabel") == 0)
892 fsflags |= MNT_MULTILABEL;
893 else if (strcmp(opt->name, "noasync") == 0)
894 fsflags &= ~MNT_ASYNC;
895 else if (strcmp(opt->name, "noatime") == 0)
896 fsflags |= MNT_NOATIME;
897 else if (strcmp(opt->name, "atime") == 0) {
898 free(opt->name, M_MOUNT);
899 opt->name = strdup("nonoatime", M_MOUNT);
901 else if (strcmp(opt->name, "noclusterr") == 0)
902 fsflags |= MNT_NOCLUSTERR;
903 else if (strcmp(opt->name, "clusterr") == 0) {
904 free(opt->name, M_MOUNT);
905 opt->name = strdup("nonoclusterr", M_MOUNT);
907 else if (strcmp(opt->name, "noclusterw") == 0)
908 fsflags |= MNT_NOCLUSTERW;
909 else if (strcmp(opt->name, "clusterw") == 0) {
910 free(opt->name, M_MOUNT);
911 opt->name = strdup("nonoclusterw", M_MOUNT);
913 else if (strcmp(opt->name, "noexec") == 0)
914 fsflags |= MNT_NOEXEC;
915 else if (strcmp(opt->name, "exec") == 0) {
916 free(opt->name, M_MOUNT);
917 opt->name = strdup("nonoexec", M_MOUNT);
919 else if (strcmp(opt->name, "nosuid") == 0)
920 fsflags |= MNT_NOSUID;
921 else if (strcmp(opt->name, "suid") == 0) {
922 free(opt->name, M_MOUNT);
923 opt->name = strdup("nonosuid", M_MOUNT);
925 else if (strcmp(opt->name, "nosymfollow") == 0)
926 fsflags |= MNT_NOSYMFOLLOW;
927 else if (strcmp(opt->name, "symfollow") == 0) {
928 free(opt->name, M_MOUNT);
929 opt->name = strdup("nonosymfollow", M_MOUNT);
931 else if (strcmp(opt->name, "noro") == 0) {
932 fsflags &= ~MNT_RDONLY;
935 else if (strcmp(opt->name, "rw") == 0) {
936 fsflags &= ~MNT_RDONLY;
939 else if (strcmp(opt->name, "ro") == 0) {
940 fsflags |= MNT_RDONLY;
943 else if (strcmp(opt->name, "rdonly") == 0) {
944 free(opt->name, M_MOUNT);
945 opt->name = strdup("ro", M_MOUNT);
946 fsflags |= MNT_RDONLY;
949 else if (strcmp(opt->name, "autoro") == 0) {
953 else if (strcmp(opt->name, "suiddir") == 0)
954 fsflags |= MNT_SUIDDIR;
955 else if (strcmp(opt->name, "sync") == 0)
956 fsflags |= MNT_SYNCHRONOUS;
957 else if (strcmp(opt->name, "union") == 0)
958 fsflags |= MNT_UNION;
959 else if (strcmp(opt->name, "export") == 0) {
960 fsflags |= MNT_EXPORTED;
962 } else if (strcmp(opt->name, "automounted") == 0) {
963 fsflags |= MNT_AUTOMOUNTED;
965 } else if (strcmp(opt->name, "nocover") == 0) {
966 fsflags |= MNT_NOCOVER;
968 } else if (strcmp(opt->name, "cover") == 0) {
969 fsflags &= ~MNT_NOCOVER;
971 } else if (strcmp(opt->name, "emptydir") == 0) {
972 fsflags |= MNT_EMPTYDIR;
974 } else if (strcmp(opt->name, "noemptydir") == 0) {
975 fsflags &= ~MNT_EMPTYDIR;
979 vfs_freeopt(optlist, opt);
983 * Be ultra-paranoid about making sure the type and fspath
984 * variables will fit in our mp buffers, including the
987 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
988 error = ENAMETOOLONG;
993 * If has_nonexport is true or the caller is not running within a
994 * vnet prison that can run mountd(8), set jail_export false.
996 if (has_nonexport || !jailed(td->td_ucred) ||
997 !prison_check_nfsd(td->td_ucred))
1000 error = vfs_domount(td, fstype, fspath, fsflags, jail_export, &optlist);
1001 if (error == ENOENT) {
1004 strncpy(errmsg, "Invalid fstype", errmsg_len);
1009 * See if we can mount in the read-only mode if the error code suggests
1010 * that it could be possible and the mount options allow for that.
1011 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
1012 * overridden by "autoro".
1014 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
1015 printf("%s: R/W mount failed, possibly R/O media,"
1016 " trying R/O mount\n", __func__);
1017 fsflags |= MNT_RDONLY;
1018 error = vfs_domount(td, fstype, fspath, fsflags, jail_export,
1022 /* copyout the errmsg */
1023 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
1024 && errmsg_len > 0 && errmsg != NULL) {
1025 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
1027 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
1028 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
1031 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
1032 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
1036 if (optlist != NULL)
1037 vfs_freeopts(optlist);
1044 #ifndef _SYS_SYSPROTO_H_
1054 sys_mount(struct thread *td, struct mount_args *uap)
1057 struct vfsconf *vfsp = NULL;
1058 struct mntarg *ma = NULL;
1063 * Mount flags are now 64-bits. On 32-bit architectures only
1064 * 32-bits are passed in, but from here on everything handles
1065 * 64-bit flags correctly.
1069 AUDIT_ARG_FFLAGS(flags);
1072 * Filter out MNT_ROOTFS. We do not want clients of mount() in
1073 * userspace to set this flag, but we must filter it out if we want
1074 * MNT_UPDATE on the root file system to work.
1075 * MNT_ROOTFS should only be set by the kernel when mounting its
1078 flags &= ~MNT_ROOTFS;
1080 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
1081 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
1083 free(fstype, M_TEMP);
1087 AUDIT_ARG_TEXT(fstype);
1088 vfsp = vfs_byname_kld(fstype, td, &error);
1089 free(fstype, M_TEMP);
1092 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
1093 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
1094 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
1095 vfsp->vfc_vfsops->vfs_cmount == NULL))
1096 return (EOPNOTSUPP);
1098 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
1099 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
1100 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
1101 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
1102 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
1104 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
1105 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
1106 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
1110 * vfs_domount_first(): first file system mount (not update)
1114 struct thread *td, /* Calling thread. */
1115 struct vfsconf *vfsp, /* File system type. */
1116 char *fspath, /* Mount path. */
1117 struct vnode *vp, /* Vnode to be covered. */
1118 uint64_t fsflags, /* Flags common to all filesystems. */
1119 struct vfsoptlist **optlist /* Options local to the filesystem. */
1124 struct vnode *newdp, *rootvp;
1128 ASSERT_VOP_ELOCKED(vp, __func__);
1129 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
1132 * If the jail of the calling thread lacks permission for this type of
1133 * file system, or is trying to cover its own root, deny immediately.
1135 if (jailed(td->td_ucred) && (!prison_allow(td->td_ucred,
1136 vfsp->vfc_prison_flag) || vp == td->td_ucred->cr_prison->pr_root)) {
1142 * If the user is not root, ensure that they own the directory
1143 * onto which we are attempting to mount.
1145 error = VOP_GETATTR(vp, &va, td->td_ucred);
1146 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
1147 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
1149 error = vinvalbuf(vp, V_SAVE, 0, 0);
1150 if (vfsp->vfc_flags & VFCF_FILEMOUNT) {
1151 if (error == 0 && vp->v_type != VDIR && vp->v_type != VREG)
1154 * For file mounts, ensure that there is only one hardlink to the file.
1156 if (error == 0 && vp->v_type == VREG && va.va_nlink != 1)
1159 if (error == 0 && vp->v_type != VDIR)
1162 if (error == 0 && (fsflags & MNT_EMPTYDIR) != 0)
1163 error = vn_dir_check_empty(vp);
1166 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
1167 vp->v_iflag |= VI_MOUNT;
1176 vn_seqc_write_begin(vp);
1179 /* Allocate and initialize the filesystem. */
1180 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
1181 /* XXXMAC: pass to vfs_mount_alloc? */
1182 mp->mnt_optnew = *optlist;
1183 /* Set the mount level flags. */
1184 mp->mnt_flag = (fsflags &
1185 (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY | MNT_FORCE));
1188 * Mount the filesystem.
1189 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1190 * get. No freeing of cn_pnbuf.
1194 if ((error = VFS_MOUNT(mp)) != 0 ||
1195 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
1196 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
1200 rootvp = vfs_cache_root_clear(mp);
1201 if (rootvp != NULL) {
1205 (void)vn_start_write(NULL, &mp, V_WAIT);
1207 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_UNMOUNTF;
1210 error = VFS_UNMOUNT(mp, 0);
1211 vn_finished_write(mp);
1214 "failed post-mount (%d): rollback unmount returned %d\n",
1221 mp->mnt_vnodecovered = NULL;
1223 /* XXXKIB wait for mnt_lockref drain? */
1224 vfs_mount_destroy(mp);
1227 vp->v_iflag &= ~VI_MOUNT;
1229 if (rootvp != NULL) {
1230 vn_seqc_write_end(rootvp);
1233 vn_seqc_write_end(vp);
1237 vn_seqc_write_begin(newdp);
1240 if (mp->mnt_opt != NULL)
1241 vfs_freeopts(mp->mnt_opt);
1242 mp->mnt_opt = mp->mnt_optnew;
1246 * Prevent external consumers of mount options from reading mnt_optnew.
1248 mp->mnt_optnew = NULL;
1251 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1252 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1253 mp->mnt_kern_flag |= MNTK_ASYNC;
1255 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1259 * VIRF_MOUNTPOINT and v_mountedhere need to be set under the
1260 * vp lock to satisfy vfs_lookup() requirements.
1262 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1264 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
1265 vp->v_mountedhere = mp;
1271 * We need to lock both vnodes.
1273 * Use vn_lock_pair to avoid establishing an ordering between vnodes
1274 * from different filesystems.
1276 vn_lock_pair(vp, false, LK_EXCLUSIVE, newdp, false, LK_EXCLUSIVE);
1279 vp->v_iflag &= ~VI_MOUNT;
1281 /* Place the new filesystem at the end of the mount list. */
1282 mtx_lock(&mountlist_mtx);
1283 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1284 mtx_unlock(&mountlist_mtx);
1285 vfs_event_signal(NULL, VQ_MOUNT, 0);
1287 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
1289 mount_devctl_event("MOUNT", mp, false);
1290 mountcheckdirs(vp, newdp);
1291 vn_seqc_write_end(vp);
1292 vn_seqc_write_end(newdp);
1294 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1295 vfs_allocate_syncvnode(mp);
1302 * vfs_domount_update(): update of mounted file system
1306 struct thread *td, /* Calling thread. */
1307 struct vnode *vp, /* Mount point vnode. */
1308 uint64_t fsflags, /* Flags common to all filesystems. */
1309 bool jail_export, /* Got export option in vnet prison. */
1310 struct vfsoptlist **optlist /* Options local to the filesystem. */
1313 struct export_args export;
1314 struct o2export_args o2export;
1315 struct vnode *rootvp;
1318 int error, export_error, i, len;
1321 bool vfs_suser_failed;
1323 ASSERT_VOP_ELOCKED(vp, __func__);
1324 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
1327 if ((vp->v_vflag & VV_ROOT) == 0) {
1328 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
1338 * We only allow the filesystem to be reloaded if it
1339 * is currently mounted read-only.
1341 flag = mp->mnt_flag;
1342 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
1344 return (EOPNOTSUPP); /* Needs translation */
1347 * Only privileged root, or (if MNT_USER is set) the user that
1348 * did the original mount is permitted to update it.
1351 * For the case of mountd(8) doing exports in a jail, the vfs_suser()
1352 * call does not cause failure. vfs_domount() has already checked
1353 * that "root" is doing this and vfs_suser() will fail when
1354 * the file system has been mounted outside the jail.
1355 * jail_export set true indicates that "export" is not mixed
1356 * with other options that change mount behaviour.
1358 vfs_suser_failed = false;
1359 error = vfs_suser(mp, td);
1360 if (jail_export && error != 0) {
1362 vfs_suser_failed = true;
1368 if (vfs_busy(mp, MBF_NOWAIT)) {
1373 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1379 vp->v_iflag |= VI_MOUNT;
1384 vn_seqc_write_begin(vp);
1388 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1393 if (vfs_suser_failed) {
1394 KASSERT((fsflags & (MNT_EXPORTED | MNT_UPDATE)) ==
1395 (MNT_EXPORTED | MNT_UPDATE),
1396 ("%s: jailed export did not set expected fsflags",
1399 * For this case, only MNT_UPDATE and
1400 * MNT_EXPORTED have been set in fsflags
1401 * by the options. Only set MNT_UPDATE,
1402 * since that is the one that would be set
1403 * when set in fsflags, below.
1405 mp->mnt_flag |= MNT_UPDATE;
1407 mp->mnt_flag &= ~MNT_UPDATEMASK;
1408 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1409 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1410 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1411 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1413 rootvp = vfs_cache_root_clear(mp);
1415 mp->mnt_optnew = *optlist;
1416 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1419 * Mount the filesystem.
1420 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1421 * get. No freeing of cn_pnbuf.
1424 * For the case of mountd(8) doing exports from within a vnet jail,
1425 * "from" is typically not set correctly such that VFS_MOUNT() will
1426 * return ENOENT. It is not obvious that VFS_MOUNT() ever needs to be
1427 * called when mountd is doing exports, but this check only applies to
1428 * the specific case where it is running inside a vnet jail, to
1429 * avoid any POLA violation.
1433 error = VFS_MOUNT(mp);
1436 /* Process the export option. */
1437 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1439 /* Assume that there is only 1 ABI for each length. */
1441 case (sizeof(struct oexport_args)):
1442 bzero(&o2export, sizeof(o2export));
1444 case (sizeof(o2export)):
1445 bcopy(bufp, &o2export, len);
1446 export.ex_flags = (uint64_t)o2export.ex_flags;
1447 export.ex_root = o2export.ex_root;
1448 export.ex_uid = o2export.ex_anon.cr_uid;
1449 export.ex_groups = NULL;
1450 export.ex_ngroups = o2export.ex_anon.cr_ngroups;
1451 if (export.ex_ngroups > 0) {
1452 if (export.ex_ngroups <= XU_NGROUPS) {
1453 export.ex_groups = malloc(
1454 export.ex_ngroups * sizeof(gid_t),
1456 for (i = 0; i < export.ex_ngroups; i++)
1457 export.ex_groups[i] =
1458 o2export.ex_anon.cr_groups[i];
1460 export_error = EINVAL;
1461 } else if (export.ex_ngroups < 0)
1462 export_error = EINVAL;
1463 export.ex_addr = o2export.ex_addr;
1464 export.ex_addrlen = o2export.ex_addrlen;
1465 export.ex_mask = o2export.ex_mask;
1466 export.ex_masklen = o2export.ex_masklen;
1467 export.ex_indexfile = o2export.ex_indexfile;
1468 export.ex_numsecflavors = o2export.ex_numsecflavors;
1469 if (export.ex_numsecflavors < MAXSECFLAVORS) {
1470 for (i = 0; i < export.ex_numsecflavors; i++)
1471 export.ex_secflavors[i] =
1472 o2export.ex_secflavors[i];
1474 export_error = EINVAL;
1475 if (export_error == 0)
1476 export_error = vfs_export(mp, &export, true);
1477 free(export.ex_groups, M_TEMP);
1479 case (sizeof(export)):
1480 bcopy(bufp, &export, len);
1482 if (export.ex_ngroups > 0) {
1483 if (export.ex_ngroups <= NGROUPS_MAX) {
1484 grps = malloc(export.ex_ngroups *
1485 sizeof(gid_t), M_TEMP, M_WAITOK);
1486 export_error = copyin(export.ex_groups,
1487 grps, export.ex_ngroups *
1489 if (export_error == 0)
1490 export.ex_groups = grps;
1492 export_error = EINVAL;
1493 } else if (export.ex_ngroups == 0)
1494 export.ex_groups = NULL;
1496 export_error = EINVAL;
1497 if (export_error == 0)
1498 export_error = vfs_export(mp, &export, true);
1502 export_error = EINVAL;
1509 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1513 * If we fail, restore old mount flags. MNT_QUOTA is special,
1514 * because it is not part of MNT_UPDATEMASK, but it could have
1515 * changed in the meantime if quotactl(2) was called.
1516 * All in all we want current value of MNT_QUOTA, not the old
1519 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1521 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1522 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1523 mp->mnt_kern_flag |= MNTK_ASYNC;
1525 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1531 mount_devctl_event("REMOUNT", mp, true);
1532 if (mp->mnt_opt != NULL)
1533 vfs_freeopts(mp->mnt_opt);
1534 mp->mnt_opt = mp->mnt_optnew;
1536 (void)VFS_STATFS(mp, &mp->mnt_stat);
1538 * Prevent external consumers of mount options from reading
1541 mp->mnt_optnew = NULL;
1543 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1544 vfs_allocate_syncvnode(mp);
1546 vfs_deallocate_syncvnode(mp);
1549 if (rootvp != NULL) {
1550 vn_seqc_write_end(rootvp);
1553 vn_seqc_write_end(vp);
1556 vp->v_iflag &= ~VI_MOUNT;
1559 return (error != 0 ? error : export_error);
1563 * vfs_domount(): actually attempt a filesystem mount.
1567 struct thread *td, /* Calling thread. */
1568 const char *fstype, /* Filesystem type. */
1569 char *fspath, /* Mount path. */
1570 uint64_t fsflags, /* Flags common to all filesystems. */
1571 bool jail_export, /* Got export option in vnet prison. */
1572 struct vfsoptlist **optlist /* Options local to the filesystem. */
1575 struct vfsconf *vfsp;
1576 struct nameidata nd;
1582 * Be ultra-paranoid about making sure the type and fspath
1583 * variables will fit in our mp buffers, including the
1586 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1587 return (ENAMETOOLONG);
1590 error = priv_check(td, PRIV_NFS_DAEMON);
1593 } else if (jailed(td->td_ucred) || usermount == 0) {
1594 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1599 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1601 if (fsflags & MNT_EXPORTED) {
1602 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1606 if (fsflags & MNT_SUIDDIR) {
1607 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1612 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1614 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1615 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1616 fsflags |= MNT_NOSUID | MNT_USER;
1619 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1621 if ((fsflags & MNT_UPDATE) == 0) {
1622 /* Don't try to load KLDs if we're mounting the root. */
1623 if (fsflags & MNT_ROOTFS) {
1624 if ((vfsp = vfs_byname(fstype)) == NULL)
1627 if ((vfsp = vfs_byname_kld(fstype, td, &error)) == NULL)
1633 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1635 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1 | WANTPARENT,
1636 UIO_SYSSPACE, fspath);
1642 * Don't allow stacking file mounts to work around problems with the way
1643 * that namei sets nd.ni_dvp to vp_crossmp for these.
1645 if (vp->v_type == VREG)
1646 fsflags |= MNT_NOCOVER;
1647 if ((fsflags & MNT_UPDATE) == 0) {
1648 if ((vp->v_vflag & VV_ROOT) != 0 &&
1649 (fsflags & MNT_NOCOVER) != 0) {
1654 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1655 strcpy(pathbuf, fspath);
1657 * Note: we allow any vnode type here. If the path sanity check
1658 * succeeds, the type will be validated in vfs_domount_first
1661 if (vp->v_type == VDIR)
1662 error = vn_path_to_global_path(td, vp, pathbuf,
1665 error = vn_path_to_global_path_hardlink(td, vp,
1666 nd.ni_dvp, pathbuf, MNAMELEN,
1667 nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen);
1669 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1672 free(pathbuf, M_TEMP);
1674 error = vfs_domount_update(td, vp, fsflags, jail_export,
1685 * Unmount a filesystem.
1687 * Note: unmount takes a path to the vnode mounted on as argument, not
1688 * special file (as before).
1690 #ifndef _SYS_SYSPROTO_H_
1691 struct unmount_args {
1698 sys_unmount(struct thread *td, struct unmount_args *uap)
1701 return (kern_unmount(td, uap->path, uap->flags));
1705 kern_unmount(struct thread *td, const char *path, int flags)
1707 struct nameidata nd;
1709 char *fsidbuf, *pathbuf;
1713 AUDIT_ARG_VALUE(flags);
1714 if (jailed(td->td_ucred) || usermount == 0) {
1715 error = priv_check(td, PRIV_VFS_UNMOUNT);
1720 if (flags & MNT_BYFSID) {
1721 fsidbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1722 error = copyinstr(path, fsidbuf, MNAMELEN, NULL);
1724 free(fsidbuf, M_TEMP);
1728 AUDIT_ARG_TEXT(fsidbuf);
1729 /* Decode the filesystem ID. */
1730 if (sscanf(fsidbuf, "FSID:%d:%d", &fsid.val[0], &fsid.val[1]) != 2) {
1731 free(fsidbuf, M_TEMP);
1735 mp = vfs_getvfs(&fsid);
1736 free(fsidbuf, M_TEMP);
1741 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1742 error = copyinstr(path, pathbuf, MNAMELEN, NULL);
1744 free(pathbuf, M_TEMP);
1749 * Try to find global path for path argument.
1751 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1752 UIO_SYSSPACE, pathbuf);
1753 if (namei(&nd) == 0) {
1755 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1760 mtx_lock(&mountlist_mtx);
1761 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1762 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1767 mtx_unlock(&mountlist_mtx);
1768 free(pathbuf, M_TEMP);
1771 * Previously we returned ENOENT for a nonexistent path and
1772 * EINVAL for a non-mountpoint. We cannot tell these apart
1773 * now, so in the !MNT_BYFSID case return the more likely
1774 * EINVAL for compatibility.
1781 * Don't allow unmounting the root filesystem.
1783 if (mp->mnt_flag & MNT_ROOTFS) {
1787 error = dounmount(mp, flags, td);
1792 * Return error if any of the vnodes, ignoring the root vnode
1793 * and the syncer vnode, have non-zero usecount.
1795 * This function is purely advisory - it can return false positives
1799 vfs_check_usecounts(struct mount *mp)
1801 struct vnode *vp, *mvp;
1803 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1804 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1805 vp->v_usecount != 0) {
1807 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1817 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1820 mtx_assert(MNT_MTX(mp), MA_OWNED);
1821 mp->mnt_kern_flag &= ~mntkflags;
1822 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1823 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1826 vfs_op_exit_locked(mp);
1828 if (coveredvp != NULL) {
1829 VOP_UNLOCK(coveredvp);
1832 vn_finished_write(mp);
1837 * There are various reference counters associated with the mount point.
1838 * Normally it is permitted to modify them without taking the mnt ilock,
1839 * but this behavior can be temporarily disabled if stable value is needed
1840 * or callers are expected to block (e.g. to not allow new users during
1844 vfs_op_enter(struct mount *mp)
1846 struct mount_pcpu *mpcpu;
1851 if (mp->mnt_vfs_ops > 1) {
1855 vfs_op_barrier_wait(mp);
1857 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1859 mp->mnt_ref += mpcpu->mntp_ref;
1860 mpcpu->mntp_ref = 0;
1862 mp->mnt_lockref += mpcpu->mntp_lockref;
1863 mpcpu->mntp_lockref = 0;
1865 mp->mnt_writeopcount += mpcpu->mntp_writeopcount;
1866 mpcpu->mntp_writeopcount = 0;
1868 MPASSERT(mp->mnt_ref > 0 && mp->mnt_lockref >= 0 &&
1869 mp->mnt_writeopcount >= 0, mp,
1870 ("invalid count(s): ref %d lockref %d writeopcount %d",
1871 mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount));
1873 vfs_assert_mount_counters(mp);
1877 vfs_op_exit_locked(struct mount *mp)
1880 mtx_assert(MNT_MTX(mp), MA_OWNED);
1882 MPASSERT(mp->mnt_vfs_ops > 0, mp,
1883 ("invalid vfs_ops count %d", mp->mnt_vfs_ops));
1884 MPASSERT(mp->mnt_vfs_ops > 1 ||
1885 (mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_SUSPEND)) == 0, mp,
1886 ("vfs_ops too low %d in unmount or suspend", mp->mnt_vfs_ops));
1891 vfs_op_exit(struct mount *mp)
1895 vfs_op_exit_locked(mp);
1899 struct vfs_op_barrier_ipi {
1901 struct smp_rendezvous_cpus_retry_arg srcra;
1905 vfs_op_action_func(void *arg)
1907 struct vfs_op_barrier_ipi *vfsopipi;
1910 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1913 if (!vfs_op_thread_entered(mp))
1914 smp_rendezvous_cpus_done(arg);
1918 vfs_op_wait_func(void *arg, int cpu)
1920 struct vfs_op_barrier_ipi *vfsopipi;
1922 struct mount_pcpu *mpcpu;
1924 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1927 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1928 while (atomic_load_int(&mpcpu->mntp_thread_in_ops))
1933 vfs_op_barrier_wait(struct mount *mp)
1935 struct vfs_op_barrier_ipi vfsopipi;
1939 smp_rendezvous_cpus_retry(all_cpus,
1940 smp_no_rendezvous_barrier,
1942 smp_no_rendezvous_barrier,
1949 vfs_assert_mount_counters(struct mount *mp)
1951 struct mount_pcpu *mpcpu;
1954 if (mp->mnt_vfs_ops == 0)
1958 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1959 if (mpcpu->mntp_ref != 0 ||
1960 mpcpu->mntp_lockref != 0 ||
1961 mpcpu->mntp_writeopcount != 0)
1962 vfs_dump_mount_counters(mp);
1967 vfs_dump_mount_counters(struct mount *mp)
1969 struct mount_pcpu *mpcpu;
1970 int ref, lockref, writeopcount;
1973 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);
1978 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1979 printf("%d ", mpcpu->mntp_ref);
1980 ref += mpcpu->mntp_ref;
1983 printf(" lockref : ");
1984 lockref = mp->mnt_lockref;
1986 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1987 printf("%d ", mpcpu->mntp_lockref);
1988 lockref += mpcpu->mntp_lockref;
1991 printf("writeopcount: ");
1992 writeopcount = mp->mnt_writeopcount;
1994 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1995 printf("%d ", mpcpu->mntp_writeopcount);
1996 writeopcount += mpcpu->mntp_writeopcount;
2000 printf("counter struct total\n");
2001 printf("ref %-5d %-5d\n", mp->mnt_ref, ref);
2002 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref);
2003 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount);
2005 panic("invalid counts on struct mount");
2010 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
2012 struct mount_pcpu *mpcpu;
2019 case MNT_COUNT_LOCKREF:
2020 sum = mp->mnt_lockref;
2022 case MNT_COUNT_WRITEOPCOUNT:
2023 sum = mp->mnt_writeopcount;
2028 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
2031 sum += mpcpu->mntp_ref;
2033 case MNT_COUNT_LOCKREF:
2034 sum += mpcpu->mntp_lockref;
2036 case MNT_COUNT_WRITEOPCOUNT:
2037 sum += mpcpu->mntp_writeopcount;
2045 deferred_unmount_enqueue(struct mount *mp, uint64_t flags, bool requeue,
2051 mtx_lock(&deferred_unmount_lock);
2052 if ((mp->mnt_taskqueue_flags & MNT_DEFERRED) == 0 || requeue) {
2053 mp->mnt_taskqueue_flags = flags | MNT_DEFERRED;
2054 STAILQ_INSERT_TAIL(&deferred_unmount_list, mp,
2055 mnt_taskqueue_link);
2058 mtx_unlock(&deferred_unmount_lock);
2061 taskqueue_enqueue_timeout(taskqueue_deferred_unmount,
2062 &deferred_unmount_task, timeout_ticks);
2069 * Taskqueue handler for processing async/recursive unmounts
2072 vfs_deferred_unmount(void *argi __unused, int pending __unused)
2074 STAILQ_HEAD(, mount) local_unmounts;
2076 struct mount *mp, *tmp;
2078 unsigned int retries;
2081 STAILQ_INIT(&local_unmounts);
2082 mtx_lock(&deferred_unmount_lock);
2083 STAILQ_CONCAT(&local_unmounts, &deferred_unmount_list);
2084 mtx_unlock(&deferred_unmount_lock);
2086 STAILQ_FOREACH_SAFE(mp, &local_unmounts, mnt_taskqueue_link, tmp) {
2087 flags = mp->mnt_taskqueue_flags;
2088 KASSERT((flags & MNT_DEFERRED) != 0,
2089 ("taskqueue unmount without MNT_DEFERRED"));
2090 error = dounmount(mp, flags, curthread);
2093 unmounted = ((mp->mnt_kern_flag & MNTK_REFEXPIRE) != 0);
2097 * The deferred unmount thread is the only thread that
2098 * modifies the retry counts, so locking/atomics aren't
2101 retries = (mp->mnt_unmount_retries)++;
2102 deferred_unmount_total_retries++;
2103 if (!unmounted && retries < deferred_unmount_retry_limit) {
2104 deferred_unmount_enqueue(mp, flags, true,
2105 -deferred_unmount_retry_delay_hz);
2107 if (retries >= deferred_unmount_retry_limit) {
2108 printf("giving up on deferred unmount "
2109 "of %s after %d retries, error %d\n",
2110 mp->mnt_stat.f_mntonname, retries, error);
2119 * Do the actual filesystem unmount.
2122 dounmount(struct mount *mp, uint64_t flags, struct thread *td)
2124 struct mount_upper_node *upper;
2125 struct vnode *coveredvp, *rootvp;
2127 uint64_t async_flag;
2129 unsigned int retries;
2131 KASSERT((flags & MNT_DEFERRED) == 0 ||
2132 (flags & (MNT_RECURSE | MNT_FORCE)) == (MNT_RECURSE | MNT_FORCE),
2133 ("MNT_DEFERRED requires MNT_RECURSE | MNT_FORCE"));
2136 * If the caller has explicitly requested the unmount to be handled by
2137 * the taskqueue and we're not already in taskqueue context, queue
2138 * up the unmount request and exit. This is done prior to any
2139 * credential checks; MNT_DEFERRED should be used only for kernel-
2140 * initiated unmounts and will therefore be processed with the
2141 * (kernel) credentials of the taskqueue thread. Still, callers
2142 * should be sure this is the behavior they want.
2144 if ((flags & MNT_DEFERRED) != 0 &&
2145 taskqueue_member(taskqueue_deferred_unmount, curthread) == 0) {
2146 if (!deferred_unmount_enqueue(mp, flags, false, 0))
2148 return (EINPROGRESS);
2152 * Only privileged root, or (if MNT_USER is set) the user that did the
2153 * original mount is permitted to unmount this filesystem.
2154 * This check should be made prior to queueing up any recursive
2155 * unmounts of upper filesystems. Those unmounts will be executed
2156 * with kernel thread credentials and are expected to succeed, so
2157 * we must at least ensure the originating context has sufficient
2158 * privilege to unmount the base filesystem before proceeding with
2161 error = vfs_suser(mp, td);
2163 KASSERT((flags & MNT_DEFERRED) == 0,
2164 ("taskqueue unmount with insufficient privilege"));
2169 if (recursive_forced_unmount && ((flags & MNT_FORCE) != 0))
2170 flags |= MNT_RECURSE;
2172 if ((flags & MNT_RECURSE) != 0) {
2173 KASSERT((flags & MNT_FORCE) != 0,
2174 ("MNT_RECURSE requires MNT_FORCE"));
2178 * Set MNTK_RECURSE to prevent new upper mounts from being
2179 * added, and note that an operation on the uppers list is in
2180 * progress. This will ensure that unregistration from the
2181 * uppers list, and therefore any pending unmount of the upper
2182 * FS, can't complete until after we finish walking the list.
2184 mp->mnt_kern_flag |= MNTK_RECURSE;
2185 mp->mnt_upper_pending++;
2186 TAILQ_FOREACH(upper, &mp->mnt_uppers, mnt_upper_link) {
2187 retries = upper->mp->mnt_unmount_retries;
2188 if (retries > deferred_unmount_retry_limit) {
2195 if (!deferred_unmount_enqueue(upper->mp, flags,
2200 mp->mnt_upper_pending--;
2201 if ((mp->mnt_kern_flag & MNTK_UPPER_WAITER) != 0 &&
2202 mp->mnt_upper_pending == 0) {
2203 mp->mnt_kern_flag &= ~MNTK_UPPER_WAITER;
2204 wakeup(&mp->mnt_uppers);
2208 * If we're not on the taskqueue, wait until the uppers list
2209 * is drained before proceeding with unmount. Otherwise, if
2210 * we are on the taskqueue and there are still pending uppers,
2211 * just re-enqueue on the end of the taskqueue.
2213 if ((flags & MNT_DEFERRED) == 0) {
2214 while (error == 0 && !TAILQ_EMPTY(&mp->mnt_uppers)) {
2215 mp->mnt_kern_flag |= MNTK_TASKQUEUE_WAITER;
2216 error = msleep(&mp->mnt_taskqueue_link,
2217 MNT_MTX(mp), PCATCH, "umntqw", 0);
2224 } else if (!TAILQ_EMPTY(&mp->mnt_uppers)) {
2227 deferred_unmount_enqueue(mp, flags, true, 0);
2231 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers not empty"));
2234 /* Allow the taskqueue to safely re-enqueue on failure */
2235 if ((flags & MNT_DEFERRED) != 0)
2238 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
2239 mnt_gen_r = mp->mnt_gen;
2242 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
2244 * Check for mp being unmounted while waiting for the
2245 * covered vnode lock.
2247 if (coveredvp->v_mountedhere != mp ||
2248 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
2249 VOP_UNLOCK(coveredvp);
2258 vn_start_write(NULL, &mp, V_WAIT);
2260 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
2261 (mp->mnt_flag & MNT_UPDATE) != 0 ||
2262 !TAILQ_EMPTY(&mp->mnt_uppers)) {
2263 dounmount_cleanup(mp, coveredvp, 0);
2266 mp->mnt_kern_flag |= MNTK_UNMOUNT;
2267 rootvp = vfs_cache_root_clear(mp);
2268 if (coveredvp != NULL)
2269 vn_seqc_write_begin(coveredvp);
2270 if (flags & MNT_NONBUSY) {
2272 error = vfs_check_usecounts(mp);
2275 vn_seqc_write_end(coveredvp);
2276 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
2277 if (rootvp != NULL) {
2278 vn_seqc_write_end(rootvp);
2284 /* Allow filesystems to detect that a forced unmount is in progress. */
2285 if (flags & MNT_FORCE) {
2286 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
2289 * Must be done after setting MNTK_UNMOUNTF and before
2290 * waiting for mnt_lockref to become 0.
2296 if (mp->mnt_lockref) {
2297 mp->mnt_kern_flag |= MNTK_DRAINING;
2298 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
2302 KASSERT(mp->mnt_lockref == 0,
2303 ("%s: invalid lock refcount in the drain path @ %s:%d",
2304 __func__, __FILE__, __LINE__));
2306 ("%s: invalid return value for msleep in the drain path @ %s:%d",
2307 __func__, __FILE__, __LINE__));
2310 * We want to keep the vnode around so that we can vn_seqc_write_end
2311 * after we are done with unmount. Downgrade our reference to a mere
2312 * hold count so that we don't interefere with anything.
2314 if (rootvp != NULL) {
2319 if (mp->mnt_flag & MNT_EXPUBLIC)
2320 vfs_setpublicfs(NULL, NULL, NULL);
2322 vfs_periodic(mp, MNT_WAIT);
2324 async_flag = mp->mnt_flag & MNT_ASYNC;
2325 mp->mnt_flag &= ~MNT_ASYNC;
2326 mp->mnt_kern_flag &= ~MNTK_ASYNC;
2328 vfs_deallocate_syncvnode(mp);
2329 error = VFS_UNMOUNT(mp, flags);
2330 vn_finished_write(mp);
2333 * If we failed to flush the dirty blocks for this mount point,
2334 * undo all the cdir/rdir and rootvnode changes we made above.
2335 * Unless we failed to do so because the device is reporting that
2336 * it doesn't exist anymore.
2338 if (error && error != ENXIO) {
2340 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
2342 vfs_allocate_syncvnode(mp);
2345 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
2346 mp->mnt_flag |= async_flag;
2347 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
2348 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
2349 mp->mnt_kern_flag |= MNTK_ASYNC;
2350 if (mp->mnt_kern_flag & MNTK_MWAIT) {
2351 mp->mnt_kern_flag &= ~MNTK_MWAIT;
2354 vfs_op_exit_locked(mp);
2357 vn_seqc_write_end(coveredvp);
2358 VOP_UNLOCK(coveredvp);
2361 if (rootvp != NULL) {
2362 vn_seqc_write_end(rootvp);
2368 mtx_lock(&mountlist_mtx);
2369 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2370 mtx_unlock(&mountlist_mtx);
2371 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
2372 if (coveredvp != NULL) {
2374 vn_irflag_unset_locked(coveredvp, VIRF_MOUNTPOINT);
2375 coveredvp->v_mountedhere = NULL;
2376 vn_seqc_write_end_locked(coveredvp);
2377 VI_UNLOCK(coveredvp);
2378 VOP_UNLOCK(coveredvp);
2381 mount_devctl_event("UNMOUNT", mp, false);
2382 if (rootvp != NULL) {
2383 vn_seqc_write_end(rootvp);
2386 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
2387 if (rootvnode != NULL && mp == rootvnode->v_mount) {
2391 if (mp == rootdevmp)
2393 if ((flags & MNT_DEFERRED) != 0)
2395 vfs_mount_destroy(mp);
2400 * Report errors during filesystem mounting.
2403 vfs_mount_error(struct mount *mp, const char *fmt, ...)
2405 struct vfsoptlist *moptlist = mp->mnt_optnew;
2410 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
2411 if (error || errmsg == NULL || len <= 0)
2415 vsnprintf(errmsg, (size_t)len, fmt, ap);
2420 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
2426 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
2427 if (error || errmsg == NULL || len <= 0)
2431 vsnprintf(errmsg, (size_t)len, fmt, ap);
2436 * ---------------------------------------------------------------------
2437 * Functions for querying mount options/arguments from filesystems.
2441 * Check that no unknown options are given
2444 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
2448 const char **t, *p, *q;
2451 TAILQ_FOREACH(opt, opts, link) {
2454 if (p[0] == 'n' && p[1] == 'o')
2456 for(t = global_opts; *t != NULL; t++) {
2457 if (strcmp(*t, p) == 0)
2460 if (strcmp(*t, q) == 0)
2466 for(t = legal; *t != NULL; t++) {
2467 if (strcmp(*t, p) == 0)
2470 if (strcmp(*t, q) == 0)
2476 snprintf(errmsg, sizeof(errmsg),
2477 "mount option <%s> is unknown", p);
2481 TAILQ_FOREACH(opt, opts, link) {
2482 if (strcmp(opt->name, "errmsg") == 0) {
2483 strncpy((char *)opt->value, errmsg, opt->len);
2488 printf("%s\n", errmsg);
2494 * Get a mount option by its name.
2496 * Return 0 if the option was found, ENOENT otherwise.
2497 * If len is non-NULL it will be filled with the length
2498 * of the option. If buf is non-NULL, it will be filled
2499 * with the address of the option.
2502 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
2506 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2508 TAILQ_FOREACH(opt, opts, link) {
2509 if (strcmp(name, opt->name) == 0) {
2522 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2529 TAILQ_FOREACH(opt, opts, link) {
2530 if (strcmp(name, opt->name) == 0) {
2539 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
2541 char *opt_value, *vtp;
2545 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
2548 if (opt_len == 0 || opt_value == NULL)
2550 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
2552 iv = strtoq(opt_value, &vtp, 0);
2553 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
2580 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2585 TAILQ_FOREACH(opt, opts, link) {
2586 if (strcmp(name, opt->name) != 0)
2589 if (opt->len == 0 ||
2590 ((char *)opt->value)[opt->len - 1] != '\0') {
2594 return (opt->value);
2601 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
2606 TAILQ_FOREACH(opt, opts, link) {
2607 if (strcmp(name, opt->name) == 0) {
2620 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2626 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2628 TAILQ_FOREACH(opt, opts, link) {
2629 if (strcmp(name, opt->name) != 0)
2632 if (opt->len == 0 || opt->value == NULL)
2634 if (((char *)opt->value)[opt->len - 1] != '\0')
2637 ret = vsscanf(opt->value, fmt, ap);
2645 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2649 TAILQ_FOREACH(opt, opts, link) {
2650 if (strcmp(name, opt->name) != 0)
2653 if (opt->value == NULL)
2656 if (opt->len != len)
2658 bcopy(value, opt->value, len);
2666 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2670 TAILQ_FOREACH(opt, opts, link) {
2671 if (strcmp(name, opt->name) != 0)
2674 if (opt->value == NULL)
2680 bcopy(value, opt->value, len);
2688 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2692 TAILQ_FOREACH(opt, opts, link) {
2693 if (strcmp(name, opt->name) != 0)
2696 if (opt->value == NULL)
2697 opt->len = strlen(value) + 1;
2698 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2706 * Find and copy a mount option.
2708 * The size of the buffer has to be specified
2709 * in len, if it is not the same length as the
2710 * mount option, EINVAL is returned.
2711 * Returns ENOENT if the option is not found.
2714 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
2718 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2720 TAILQ_FOREACH(opt, opts, link) {
2721 if (strcmp(name, opt->name) == 0) {
2723 if (len != opt->len)
2725 bcopy(opt->value, dest, opt->len);
2733 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2736 * Filesystems only fill in part of the structure for updates, we
2737 * have to read the entirety first to get all content.
2739 if (sbp != &mp->mnt_stat)
2740 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));
2743 * Set these in case the underlying filesystem fails to do so.
2745 sbp->f_version = STATFS_VERSION;
2746 sbp->f_namemax = NAME_MAX;
2747 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2748 sbp->f_nvnodelistsize = mp->mnt_nvnodelistsize;
2750 return (mp->mnt_op->vfs_statfs(mp, sbp));
2754 vfs_mountedfrom(struct mount *mp, const char *from)
2757 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2758 strlcpy(mp->mnt_stat.f_mntfromname, from,
2759 sizeof mp->mnt_stat.f_mntfromname);
2763 * ---------------------------------------------------------------------
2764 * This is the api for building mount args and mounting filesystems from
2765 * inside the kernel.
2767 * The API works by accumulation of individual args. First error is
2770 * XXX: should be documented in new manpage kernel_mount(9)
2773 /* A memory allocation which must be freed when we are done */
2775 SLIST_ENTRY(mntaarg) next;
2778 /* The header for the mount arguments */
2783 SLIST_HEAD(, mntaarg) list;
2787 * Add a boolean argument.
2789 * flag is the boolean value.
2790 * name must start with "no".
2793 mount_argb(struct mntarg *ma, int flag, const char *name)
2796 KASSERT(name[0] == 'n' && name[1] == 'o',
2797 ("mount_argb(...,%s): name must start with 'no'", name));
2799 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2803 * Add an argument printf style
2806 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2809 struct mntaarg *maa;
2814 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2815 SLIST_INIT(&ma->list);
2820 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2822 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2823 ma->v[ma->len].iov_len = strlen(name) + 1;
2826 sb = sbuf_new_auto();
2828 sbuf_vprintf(sb, fmt, ap);
2831 len = sbuf_len(sb) + 1;
2832 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2833 SLIST_INSERT_HEAD(&ma->list, maa, next);
2834 bcopy(sbuf_data(sb), maa + 1, len);
2837 ma->v[ma->len].iov_base = maa + 1;
2838 ma->v[ma->len].iov_len = len;
2845 * Add an argument which is a userland string.
2848 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2850 struct mntaarg *maa;
2856 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2857 SLIST_INIT(&ma->list);
2861 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2862 SLIST_INSERT_HEAD(&ma->list, maa, next);
2863 tbuf = (void *)(maa + 1);
2864 ma->error = copyinstr(val, tbuf, len, NULL);
2865 return (mount_arg(ma, name, tbuf, -1));
2871 * If length is -1, treat value as a C string.
2874 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2878 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2879 SLIST_INIT(&ma->list);
2884 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2886 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2887 ma->v[ma->len].iov_len = strlen(name) + 1;
2890 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2892 ma->v[ma->len].iov_len = strlen(val) + 1;
2894 ma->v[ma->len].iov_len = len;
2900 * Free a mntarg structure
2903 free_mntarg(struct mntarg *ma)
2905 struct mntaarg *maa;
2907 while (!SLIST_EMPTY(&ma->list)) {
2908 maa = SLIST_FIRST(&ma->list);
2909 SLIST_REMOVE_HEAD(&ma->list, next);
2912 free(ma->v, M_MOUNT);
2917 * Mount a filesystem
2920 kernel_mount(struct mntarg *ma, uint64_t flags)
2925 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2926 KASSERT(ma->error != 0 || ma->v != NULL, ("kernel_mount NULL ma->v"));
2927 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2931 auio.uio_iov = ma->v;
2932 auio.uio_iovcnt = ma->len;
2933 auio.uio_segflg = UIO_SYSSPACE;
2934 error = vfs_donmount(curthread, flags, &auio);
2940 /* Map from mount options to printable formats. */
2941 static struct mntoptnames optnames[] = {
2945 #define DEVCTL_LEN 1024
2947 mount_devctl_event(const char *type, struct mount *mp, bool donew)
2950 struct mntoptnames *fp;
2952 struct statfs *sfp = &mp->mnt_stat;
2955 buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT);
2958 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
2959 sbuf_cpy(&sb, "mount-point=\"");
2960 devctl_safe_quote_sb(&sb, sfp->f_mntonname);
2961 sbuf_cat(&sb, "\" mount-dev=\"");
2962 devctl_safe_quote_sb(&sb, sfp->f_mntfromname);
2963 sbuf_cat(&sb, "\" mount-type=\"");
2964 devctl_safe_quote_sb(&sb, sfp->f_fstypename);
2965 sbuf_cat(&sb, "\" fsid=0x");
2966 cp = (const uint8_t *)&sfp->f_fsid.val[0];
2967 for (int i = 0; i < sizeof(sfp->f_fsid); i++)
2968 sbuf_printf(&sb, "%02x", cp[i]);
2969 sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner);
2970 for (fp = optnames; fp->o_opt != 0; fp++) {
2971 if ((mp->mnt_flag & fp->o_opt) != 0) {
2972 sbuf_cat(&sb, fp->o_name);
2973 sbuf_putc(&sb, ';');
2976 sbuf_putc(&sb, '"');
2980 * Options are not published because the form of the options depends on
2981 * the file system and may include binary data. In addition, they don't
2982 * necessarily provide enough useful information to be actionable when
2983 * devd processes them.
2986 if (sbuf_error(&sb) == 0)
2987 devctl_notify("VFS", "FS", type, sbuf_data(&sb));
2993 * Force remount specified mount point to read-only. The argument
2994 * must be busied to avoid parallel unmount attempts.
2996 * Intended use is to prevent further writes if some metadata
2997 * inconsistency is detected. Note that the function still flushes
2998 * all cached metadata and data for the mount point, which might be
2999 * not always suitable.
3002 vfs_remount_ro(struct mount *mp)
3004 struct vfsoptlist *opts;
3006 struct vnode *vp_covered, *rootvp;
3009 KASSERT(mp->mnt_lockref > 0,
3010 ("vfs_remount_ro: mp %p is not busied", mp));
3011 KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0,
3012 ("vfs_remount_ro: mp %p is being unmounted (and busy?)", mp));
3015 vp_covered = mp->mnt_vnodecovered;
3016 error = vget(vp_covered, LK_EXCLUSIVE | LK_NOWAIT);
3019 VI_LOCK(vp_covered);
3020 if ((vp_covered->v_iflag & VI_MOUNT) != 0) {
3021 VI_UNLOCK(vp_covered);
3025 vp_covered->v_iflag |= VI_MOUNT;
3026 VI_UNLOCK(vp_covered);
3028 vn_seqc_write_begin(vp_covered);
3031 if ((mp->mnt_flag & MNT_RDONLY) != 0) {
3036 mp->mnt_flag |= MNT_UPDATE | MNT_FORCE | MNT_RDONLY;
3037 rootvp = vfs_cache_root_clear(mp);
3040 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK | M_ZERO);
3042 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK | M_ZERO);
3043 opt->name = strdup("ro", M_MOUNT);
3045 TAILQ_INSERT_TAIL(opts, opt, link);
3046 vfs_mergeopts(opts, mp->mnt_opt);
3047 mp->mnt_optnew = opts;
3049 error = VFS_MOUNT(mp);
3053 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE);
3055 vfs_deallocate_syncvnode(mp);
3056 if (mp->mnt_opt != NULL)
3057 vfs_freeopts(mp->mnt_opt);
3058 mp->mnt_opt = mp->mnt_optnew;
3061 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE | MNT_RDONLY);
3063 vfs_freeopts(mp->mnt_optnew);
3065 mp->mnt_optnew = NULL;
3069 VI_LOCK(vp_covered);
3070 vp_covered->v_iflag &= ~VI_MOUNT;
3071 VI_UNLOCK(vp_covered);
3073 vn_seqc_write_end(vp_covered);
3074 if (rootvp != NULL) {
3075 vn_seqc_write_end(rootvp);
3082 * Suspend write operations on all local writeable filesystems. Does
3083 * full sync of them in the process.
3085 * Iterate over the mount points in reverse order, suspending most
3086 * recently mounted filesystems first. It handles a case where a
3087 * filesystem mounted from a md(4) vnode-backed device should be
3088 * suspended before the filesystem that owns the vnode.
3091 suspend_all_fs(void)
3096 mtx_lock(&mountlist_mtx);
3097 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
3098 error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT);
3101 if ((mp->mnt_flag & (MNT_RDONLY | MNT_LOCAL)) != MNT_LOCAL ||
3102 (mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
3103 mtx_lock(&mountlist_mtx);
3107 error = vfs_write_suspend(mp, 0);
3110 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0);
3111 mp->mnt_kern_flag |= MNTK_SUSPEND_ALL;
3113 mtx_lock(&mountlist_mtx);
3115 printf("suspend of %s failed, error %d\n",
3116 mp->mnt_stat.f_mntonname, error);
3117 mtx_lock(&mountlist_mtx);
3121 mtx_unlock(&mountlist_mtx);
3129 mtx_lock(&mountlist_mtx);
3130 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3131 if ((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0)
3133 mtx_unlock(&mountlist_mtx);
3135 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) != 0);
3136 mp->mnt_kern_flag &= ~MNTK_SUSPEND_ALL;
3138 vfs_write_resume(mp, 0);
3139 mtx_lock(&mountlist_mtx);
3142 mtx_unlock(&mountlist_mtx);