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/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/namei.h>
58 #include <sys/filedesc.h>
59 #include <sys/reboot.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysproto.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/systm.h>
67 #include <sys/vnode.h>
70 #include <geom/geom.h>
72 #include <machine/stdarg.h>
74 #include <security/audit/audit.h>
75 #include <security/mac/mac_framework.h>
77 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
79 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
80 uint64_t fsflags, struct vfsoptlist **optlist);
81 static void free_mntarg(struct mntarg *ma);
83 static int usermount = 0;
84 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
85 "Unprivileged users may mount and unmount file systems");
87 static bool default_autoro = false;
88 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
89 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
91 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
92 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
93 static uma_zone_t mount_zone;
95 /* List of mounted filesystems. */
96 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
98 /* For any iteration/modification of mountlist */
99 struct mtx mountlist_mtx;
100 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
102 EVENTHANDLER_LIST_DEFINE(vfs_mounted);
103 EVENTHANDLER_LIST_DEFINE(vfs_unmounted);
105 static void mount_devctl_event(const char *type, struct mount *mp, bool donew);
108 * Global opts, taken by all filesystems
110 static const char *global_opts[] = {
122 mount_init(void *mem, int size, int flags)
126 mp = (struct mount *)mem;
127 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
128 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
129 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
130 mp->mnt_thread_in_ops_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
132 mp->mnt_ref_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
134 mp->mnt_lockref_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
136 mp->mnt_writeopcount_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
140 mp->mnt_rootvnode = NULL;
145 mount_fini(void *mem, int size)
149 mp = (struct mount *)mem;
150 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_writeopcount_pcpu);
151 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_lockref_pcpu);
152 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_ref_pcpu);
153 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_thread_in_ops_pcpu);
154 lockdestroy(&mp->mnt_explock);
155 mtx_destroy(&mp->mnt_listmtx);
156 mtx_destroy(&mp->mnt_mtx);
160 vfs_mount_init(void *dummy __unused)
163 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
164 NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
166 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
169 * ---------------------------------------------------------------------
170 * Functions for building and sanitizing the mount options
173 /* Remove one mount option. */
175 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
178 TAILQ_REMOVE(opts, opt, link);
179 free(opt->name, M_MOUNT);
180 if (opt->value != NULL)
181 free(opt->value, M_MOUNT);
185 /* Release all resources related to the mount options. */
187 vfs_freeopts(struct vfsoptlist *opts)
191 while (!TAILQ_EMPTY(opts)) {
192 opt = TAILQ_FIRST(opts);
193 vfs_freeopt(opts, opt);
199 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
201 struct vfsopt *opt, *temp;
205 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
206 if (strcmp(opt->name, name) == 0)
207 vfs_freeopt(opts, opt);
212 vfs_isopt_ro(const char *opt)
215 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
216 strcmp(opt, "norw") == 0)
222 vfs_isopt_rw(const char *opt)
225 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
231 * Check if options are equal (with or without the "no" prefix).
234 vfs_equalopts(const char *opt1, const char *opt2)
238 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
239 if (strcmp(opt1, opt2) == 0)
241 /* "noopt" vs. "opt" */
242 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
244 /* "opt" vs. "noopt" */
245 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
247 while ((p = strchr(opt1, '.')) != NULL &&
248 !strncmp(opt1, opt2, ++p - opt1)) {
251 /* "foo.noopt" vs. "foo.opt" */
252 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
254 /* "foo.opt" vs. "foo.noopt" */
255 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
258 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
259 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
260 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
266 * If a mount option is specified several times,
267 * (with or without the "no" prefix) only keep
268 * the last occurrence of it.
271 vfs_sanitizeopts(struct vfsoptlist *opts)
273 struct vfsopt *opt, *opt2, *tmp;
275 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
276 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
277 while (opt2 != NULL) {
278 if (vfs_equalopts(opt->name, opt2->name)) {
279 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
280 vfs_freeopt(opts, opt2);
283 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
290 * Build a linked list of mount options from a struct uio.
293 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
295 struct vfsoptlist *opts;
297 size_t memused, namelen, optlen;
298 unsigned int i, iovcnt;
301 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
304 iovcnt = auio->uio_iovcnt;
305 for (i = 0; i < iovcnt; i += 2) {
306 namelen = auio->uio_iov[i].iov_len;
307 optlen = auio->uio_iov[i + 1].iov_len;
308 memused += sizeof(struct vfsopt) + optlen + namelen;
310 * Avoid consuming too much memory, and attempts to overflow
313 if (memused > VFS_MOUNTARG_SIZE_MAX ||
314 optlen > VFS_MOUNTARG_SIZE_MAX ||
315 namelen > VFS_MOUNTARG_SIZE_MAX) {
320 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
321 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
328 * Do this early, so jumps to "bad" will free the current
331 TAILQ_INSERT_TAIL(opts, opt, link);
333 if (auio->uio_segflg == UIO_SYSSPACE) {
334 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
336 error = copyin(auio->uio_iov[i].iov_base, opt->name,
341 /* Ensure names are null-terminated strings. */
342 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
348 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
349 if (auio->uio_segflg == UIO_SYSSPACE) {
350 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
353 error = copyin(auio->uio_iov[i + 1].iov_base,
360 vfs_sanitizeopts(opts);
369 * Merge the old mount options with the new ones passed
370 * in the MNT_UPDATE case.
372 * XXX: This function will keep a "nofoo" option in the new
373 * options. E.g, if the option's canonical name is "foo",
374 * "nofoo" ends up in the mount point's active options.
377 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
379 struct vfsopt *opt, *new;
381 TAILQ_FOREACH(opt, oldopts, link) {
382 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
383 new->name = strdup(opt->name, M_MOUNT);
385 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
386 bcopy(opt->value, new->value, opt->len);
390 new->seen = opt->seen;
391 TAILQ_INSERT_HEAD(toopts, new, link);
393 vfs_sanitizeopts(toopts);
397 * Mount a filesystem.
399 #ifndef _SYS_SYSPROTO_H_
407 sys_nmount(struct thread *td, struct nmount_args *uap)
415 * Mount flags are now 64-bits. On 32-bit archtectures only
416 * 32-bits are passed in, but from here on everything handles
417 * 64-bit flags correctly.
421 AUDIT_ARG_FFLAGS(flags);
422 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
423 uap->iovp, uap->iovcnt, flags);
426 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
427 * userspace to set this flag, but we must filter it out if we want
428 * MNT_UPDATE on the root file system to work.
429 * MNT_ROOTFS should only be set by the kernel when mounting its
432 flags &= ~MNT_ROOTFS;
434 iovcnt = uap->iovcnt;
436 * Check that we have an even number of iovec's
437 * and that we have at least two options.
439 if ((iovcnt & 1) || (iovcnt < 4)) {
440 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
445 error = copyinuio(uap->iovp, iovcnt, &auio);
447 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
451 error = vfs_donmount(td, flags, auio);
458 * ---------------------------------------------------------------------
459 * Various utility functions
463 vfs_ref(struct mount *mp)
466 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
467 if (vfs_op_thread_enter(mp)) {
468 vfs_mp_count_add_pcpu(mp, ref, 1);
469 vfs_op_thread_exit(mp);
479 vfs_rel(struct mount *mp)
482 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
483 if (vfs_op_thread_enter(mp)) {
484 vfs_mp_count_sub_pcpu(mp, ref, 1);
485 vfs_op_thread_exit(mp);
495 * Allocate and initialize the mount point struct.
498 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
503 mp = uma_zalloc(mount_zone, M_WAITOK);
504 bzero(&mp->mnt_startzero,
505 __rangeof(struct mount, mnt_startzero, mnt_endzero));
506 TAILQ_INIT(&mp->mnt_nvnodelist);
507 mp->mnt_nvnodelistsize = 0;
508 TAILQ_INIT(&mp->mnt_lazyvnodelist);
509 mp->mnt_lazyvnodelistsize = 0;
510 if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 ||
511 mp->mnt_writeopcount != 0)
512 panic("%s: non-zero counters on new mp %p\n", __func__, mp);
513 if (mp->mnt_vfs_ops != 1)
514 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
516 (void) vfs_busy(mp, MBF_NOWAIT);
517 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
518 mp->mnt_op = vfsp->vfc_vfsops;
520 mp->mnt_stat.f_type = vfsp->vfc_typenum;
522 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
523 mp->mnt_vnodecovered = vp;
524 mp->mnt_cred = crdup(cred);
525 mp->mnt_stat.f_owner = cred->cr_uid;
526 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
527 mp->mnt_iosize_max = DFLTPHYS;
530 mac_mount_create(cred, mp);
532 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
533 TAILQ_INIT(&mp->mnt_uppers);
538 * Destroy the mount struct previously allocated by vfs_mount_alloc().
541 vfs_mount_destroy(struct mount *mp)
544 if (mp->mnt_vfs_ops == 0)
545 panic("%s: entered with zero vfs_ops\n", __func__);
547 vfs_assert_mount_counters(mp);
550 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
551 if (mp->mnt_kern_flag & MNTK_MWAIT) {
552 mp->mnt_kern_flag &= ~MNTK_MWAIT;
556 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
557 KASSERT(mp->mnt_ref == 0,
558 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
559 __FILE__, __LINE__));
560 if (mp->mnt_writeopcount != 0)
561 panic("vfs_mount_destroy: nonzero writeopcount");
562 if (mp->mnt_secondary_writes != 0)
563 panic("vfs_mount_destroy: nonzero secondary_writes");
564 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
565 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
568 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
569 vn_printf(vp, "dangling vnode ");
570 panic("unmount: dangling vnode");
572 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
573 if (mp->mnt_nvnodelistsize != 0)
574 panic("vfs_mount_destroy: nonzero nvnodelistsize");
575 if (mp->mnt_lazyvnodelistsize != 0)
576 panic("vfs_mount_destroy: nonzero lazyvnodelistsize");
577 if (mp->mnt_lockref != 0)
578 panic("vfs_mount_destroy: nonzero lock refcount");
581 if (mp->mnt_vfs_ops != 1)
582 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
585 if (mp->mnt_rootvnode != NULL)
586 panic("%s: mount point still has a root vnode %p\n", __func__,
589 if (mp->mnt_vnodecovered != NULL)
590 vrele(mp->mnt_vnodecovered);
592 mac_mount_destroy(mp);
594 if (mp->mnt_opt != NULL)
595 vfs_freeopts(mp->mnt_opt);
596 crfree(mp->mnt_cred);
597 uma_zfree(mount_zone, mp);
601 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
603 /* This is an upgrade of an exisiting mount. */
604 if ((fsflags & MNT_UPDATE) != 0)
606 /* This is already an R/O mount. */
607 if ((fsflags & MNT_RDONLY) != 0)
611 case ENODEV: /* generic, geom, ... */
612 case EACCES: /* cam/scsi, ... */
613 case EROFS: /* md, mmcsd, ... */
615 * These errors can be returned by the storage layer to signal
616 * that the media is read-only. No harm in the R/O mount
617 * attempt if the error was returned for some other reason.
626 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
628 struct vfsoptlist *optlist;
629 struct vfsopt *opt, *tmp_opt;
630 char *fstype, *fspath, *errmsg;
631 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
634 errmsg = fspath = NULL;
635 errmsg_len = fspathlen = 0;
637 autoro = default_autoro;
639 error = vfs_buildopts(fsoptions, &optlist);
643 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
644 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
647 * We need these two options before the others,
648 * and they are mandatory for any filesystem.
649 * Ensure they are NUL terminated as well.
652 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
653 if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
656 strncpy(errmsg, "Invalid fstype", errmsg_len);
660 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
661 if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
664 strncpy(errmsg, "Invalid fspath", errmsg_len);
669 * We need to see if we have the "update" option
670 * before we call vfs_domount(), since vfs_domount() has special
671 * logic based on MNT_UPDATE. This is very important
672 * when we want to update the root filesystem.
674 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
677 if (strcmp(opt->name, "update") == 0) {
678 fsflags |= MNT_UPDATE;
681 else if (strcmp(opt->name, "async") == 0)
682 fsflags |= MNT_ASYNC;
683 else if (strcmp(opt->name, "force") == 0) {
684 fsflags |= MNT_FORCE;
687 else if (strcmp(opt->name, "reload") == 0) {
688 fsflags |= MNT_RELOAD;
691 else if (strcmp(opt->name, "multilabel") == 0)
692 fsflags |= MNT_MULTILABEL;
693 else if (strcmp(opt->name, "noasync") == 0)
694 fsflags &= ~MNT_ASYNC;
695 else if (strcmp(opt->name, "noatime") == 0)
696 fsflags |= MNT_NOATIME;
697 else if (strcmp(opt->name, "atime") == 0) {
698 free(opt->name, M_MOUNT);
699 opt->name = strdup("nonoatime", M_MOUNT);
701 else if (strcmp(opt->name, "noclusterr") == 0)
702 fsflags |= MNT_NOCLUSTERR;
703 else if (strcmp(opt->name, "clusterr") == 0) {
704 free(opt->name, M_MOUNT);
705 opt->name = strdup("nonoclusterr", M_MOUNT);
707 else if (strcmp(opt->name, "noclusterw") == 0)
708 fsflags |= MNT_NOCLUSTERW;
709 else if (strcmp(opt->name, "clusterw") == 0) {
710 free(opt->name, M_MOUNT);
711 opt->name = strdup("nonoclusterw", M_MOUNT);
713 else if (strcmp(opt->name, "noexec") == 0)
714 fsflags |= MNT_NOEXEC;
715 else if (strcmp(opt->name, "exec") == 0) {
716 free(opt->name, M_MOUNT);
717 opt->name = strdup("nonoexec", M_MOUNT);
719 else if (strcmp(opt->name, "nosuid") == 0)
720 fsflags |= MNT_NOSUID;
721 else if (strcmp(opt->name, "suid") == 0) {
722 free(opt->name, M_MOUNT);
723 opt->name = strdup("nonosuid", M_MOUNT);
725 else if (strcmp(opt->name, "nosymfollow") == 0)
726 fsflags |= MNT_NOSYMFOLLOW;
727 else if (strcmp(opt->name, "symfollow") == 0) {
728 free(opt->name, M_MOUNT);
729 opt->name = strdup("nonosymfollow", M_MOUNT);
731 else if (strcmp(opt->name, "noro") == 0) {
732 fsflags &= ~MNT_RDONLY;
735 else if (strcmp(opt->name, "rw") == 0) {
736 fsflags &= ~MNT_RDONLY;
739 else if (strcmp(opt->name, "ro") == 0) {
740 fsflags |= MNT_RDONLY;
743 else if (strcmp(opt->name, "rdonly") == 0) {
744 free(opt->name, M_MOUNT);
745 opt->name = strdup("ro", M_MOUNT);
746 fsflags |= MNT_RDONLY;
749 else if (strcmp(opt->name, "autoro") == 0) {
753 else if (strcmp(opt->name, "suiddir") == 0)
754 fsflags |= MNT_SUIDDIR;
755 else if (strcmp(opt->name, "sync") == 0)
756 fsflags |= MNT_SYNCHRONOUS;
757 else if (strcmp(opt->name, "union") == 0)
758 fsflags |= MNT_UNION;
759 else if (strcmp(opt->name, "automounted") == 0) {
760 fsflags |= MNT_AUTOMOUNTED;
762 } else if (strcmp(opt->name, "nocover") == 0) {
763 fsflags |= MNT_NOCOVER;
765 } else if (strcmp(opt->name, "cover") == 0) {
766 fsflags &= ~MNT_NOCOVER;
768 } else if (strcmp(opt->name, "emptydir") == 0) {
769 fsflags |= MNT_EMPTYDIR;
771 } else if (strcmp(opt->name, "noemptydir") == 0) {
772 fsflags &= ~MNT_EMPTYDIR;
776 vfs_freeopt(optlist, opt);
780 * Be ultra-paranoid about making sure the type and fspath
781 * variables will fit in our mp buffers, including the
784 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
785 error = ENAMETOOLONG;
789 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
792 * See if we can mount in the read-only mode if the error code suggests
793 * that it could be possible and the mount options allow for that.
794 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
795 * overridden by "autoro".
797 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
798 printf("%s: R/W mount failed, possibly R/O media,"
799 " trying R/O mount\n", __func__);
800 fsflags |= MNT_RDONLY;
801 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
804 /* copyout the errmsg */
805 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
806 && errmsg_len > 0 && errmsg != NULL) {
807 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
809 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
810 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
813 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
814 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
819 vfs_freeopts(optlist);
826 #ifndef _SYS_SYSPROTO_H_
836 sys_mount(struct thread *td, struct mount_args *uap)
839 struct vfsconf *vfsp = NULL;
840 struct mntarg *ma = NULL;
845 * Mount flags are now 64-bits. On 32-bit architectures only
846 * 32-bits are passed in, but from here on everything handles
847 * 64-bit flags correctly.
851 AUDIT_ARG_FFLAGS(flags);
854 * Filter out MNT_ROOTFS. We do not want clients of mount() in
855 * userspace to set this flag, but we must filter it out if we want
856 * MNT_UPDATE on the root file system to work.
857 * MNT_ROOTFS should only be set by the kernel when mounting its
860 flags &= ~MNT_ROOTFS;
862 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
863 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
865 free(fstype, M_TEMP);
869 AUDIT_ARG_TEXT(fstype);
870 vfsp = vfs_byname_kld(fstype, td, &error);
871 free(fstype, M_TEMP);
874 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
875 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
876 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
877 vfsp->vfc_vfsops->vfs_cmount == NULL))
880 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
881 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
882 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
883 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
884 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
886 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
887 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
888 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
892 * vfs_domount_first(): first file system mount (not update)
896 struct thread *td, /* Calling thread. */
897 struct vfsconf *vfsp, /* File system type. */
898 char *fspath, /* Mount path. */
899 struct vnode *vp, /* Vnode to be covered. */
900 uint64_t fsflags, /* Flags common to all filesystems. */
901 struct vfsoptlist **optlist /* Options local to the filesystem. */
906 struct vnode *newdp, *rootvp;
909 ASSERT_VOP_ELOCKED(vp, __func__);
910 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
912 if ((fsflags & MNT_EMPTYDIR) != 0) {
913 error = vfs_emptydir(vp);
921 * If the jail of the calling thread lacks permission for this type of
922 * file system, deny immediately.
924 if (jailed(td->td_ucred) && !prison_allow(td->td_ucred,
925 vfsp->vfc_prison_flag)) {
931 * If the user is not root, ensure that they own the directory
932 * onto which we are attempting to mount.
934 error = VOP_GETATTR(vp, &va, td->td_ucred);
935 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
936 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
938 error = vinvalbuf(vp, V_SAVE, 0, 0);
939 if (error == 0 && vp->v_type != VDIR)
943 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
944 vp->v_iflag |= VI_MOUNT;
953 vn_seqc_write_begin(vp);
956 /* Allocate and initialize the filesystem. */
957 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
958 /* XXXMAC: pass to vfs_mount_alloc? */
959 mp->mnt_optnew = *optlist;
960 /* Set the mount level flags. */
961 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
964 * Mount the filesystem.
965 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
966 * get. No freeing of cn_pnbuf.
969 if ((error = VFS_MOUNT(mp)) != 0 ||
970 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
971 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
975 rootvp = vfs_cache_root_clear(mp);
976 if (rootvp != NULL) {
980 if ((error1 = VFS_UNMOUNT(mp, 0)) != 0)
981 printf("VFS_UNMOUNT returned %d\n", error1);
984 mp->mnt_vnodecovered = NULL;
985 vfs_mount_destroy(mp);
987 vp->v_iflag &= ~VI_MOUNT;
989 if (rootvp != NULL) {
990 vn_seqc_write_end(rootvp);
993 vn_seqc_write_end(vp);
997 vn_seqc_write_begin(newdp);
1000 if (mp->mnt_opt != NULL)
1001 vfs_freeopts(mp->mnt_opt);
1002 mp->mnt_opt = mp->mnt_optnew;
1006 * Prevent external consumers of mount options from reading mnt_optnew.
1008 mp->mnt_optnew = NULL;
1011 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1012 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1013 mp->mnt_kern_flag |= MNTK_ASYNC;
1015 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1018 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1021 vp->v_iflag &= ~VI_MOUNT;
1023 vp->v_mountedhere = mp;
1024 /* Place the new filesystem at the end of the mount list. */
1025 mtx_lock(&mountlist_mtx);
1026 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1027 mtx_unlock(&mountlist_mtx);
1028 vfs_event_signal(NULL, VQ_MOUNT, 0);
1029 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
1031 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
1033 mount_devctl_event("MOUNT", mp, false);
1034 mountcheckdirs(vp, newdp);
1035 vn_seqc_write_end(vp);
1036 vn_seqc_write_end(newdp);
1038 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1039 vfs_allocate_syncvnode(mp);
1046 * vfs_domount_update(): update of mounted file system
1050 struct thread *td, /* Calling thread. */
1051 struct vnode *vp, /* Mount point vnode. */
1052 uint64_t fsflags, /* Flags common to all filesystems. */
1053 struct vfsoptlist **optlist /* Options local to the filesystem. */
1056 struct export_args export;
1057 struct o2export_args o2export;
1058 struct vnode *rootvp;
1061 int error, export_error, i, len;
1065 ASSERT_VOP_ELOCKED(vp, __func__);
1066 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
1069 if ((vp->v_vflag & VV_ROOT) == 0) {
1070 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
1080 * We only allow the filesystem to be reloaded if it
1081 * is currently mounted read-only.
1083 flag = mp->mnt_flag;
1084 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
1086 return (EOPNOTSUPP); /* Needs translation */
1089 * Only privileged root, or (if MNT_USER is set) the user that
1090 * did the original mount is permitted to update it.
1092 error = vfs_suser(mp, td);
1097 if (vfs_busy(mp, MBF_NOWAIT)) {
1102 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1108 vp->v_iflag |= VI_MOUNT;
1113 vn_seqc_write_begin(vp);
1117 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1122 mp->mnt_flag &= ~MNT_UPDATEMASK;
1123 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1124 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1125 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1126 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1127 rootvp = vfs_cache_root_clear(mp);
1129 mp->mnt_optnew = *optlist;
1130 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1133 * Mount the filesystem.
1134 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1135 * get. No freeing of cn_pnbuf.
1137 error = VFS_MOUNT(mp);
1140 /* Process the export option. */
1141 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1143 /* Assume that there is only 1 ABI for each length. */
1145 case (sizeof(struct oexport_args)):
1146 bzero(&o2export, sizeof(o2export));
1148 case (sizeof(o2export)):
1149 bcopy(bufp, &o2export, len);
1150 export.ex_flags = (uint64_t)o2export.ex_flags;
1151 export.ex_root = o2export.ex_root;
1152 export.ex_uid = o2export.ex_anon.cr_uid;
1153 export.ex_groups = NULL;
1154 export.ex_ngroups = o2export.ex_anon.cr_ngroups;
1155 if (export.ex_ngroups > 0) {
1156 if (export.ex_ngroups <= XU_NGROUPS) {
1157 export.ex_groups = malloc(
1158 export.ex_ngroups * sizeof(gid_t),
1160 for (i = 0; i < export.ex_ngroups; i++)
1161 export.ex_groups[i] =
1162 o2export.ex_anon.cr_groups[i];
1164 export_error = EINVAL;
1165 } else if (export.ex_ngroups < 0)
1166 export_error = EINVAL;
1167 export.ex_addr = o2export.ex_addr;
1168 export.ex_addrlen = o2export.ex_addrlen;
1169 export.ex_mask = o2export.ex_mask;
1170 export.ex_masklen = o2export.ex_masklen;
1171 export.ex_indexfile = o2export.ex_indexfile;
1172 export.ex_numsecflavors = o2export.ex_numsecflavors;
1173 if (export.ex_numsecflavors < MAXSECFLAVORS) {
1174 for (i = 0; i < export.ex_numsecflavors; i++)
1175 export.ex_secflavors[i] =
1176 o2export.ex_secflavors[i];
1178 export_error = EINVAL;
1179 if (export_error == 0)
1180 export_error = vfs_export(mp, &export);
1181 free(export.ex_groups, M_TEMP);
1183 case (sizeof(export)):
1184 bcopy(bufp, &export, len);
1186 if (export.ex_ngroups > 0) {
1187 if (export.ex_ngroups <= NGROUPS_MAX) {
1188 grps = malloc(export.ex_ngroups *
1189 sizeof(gid_t), M_TEMP, M_WAITOK);
1190 export_error = copyin(export.ex_groups,
1191 grps, export.ex_ngroups *
1193 if (export_error == 0)
1194 export.ex_groups = grps;
1196 export_error = EINVAL;
1197 } else if (export.ex_ngroups == 0)
1198 export.ex_groups = NULL;
1200 export_error = EINVAL;
1201 if (export_error == 0)
1202 export_error = vfs_export(mp, &export);
1206 export_error = EINVAL;
1213 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1217 * If we fail, restore old mount flags. MNT_QUOTA is special,
1218 * because it is not part of MNT_UPDATEMASK, but it could have
1219 * changed in the meantime if quotactl(2) was called.
1220 * All in all we want current value of MNT_QUOTA, not the old
1223 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1225 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1226 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1227 mp->mnt_kern_flag |= MNTK_ASYNC;
1229 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1235 mount_devctl_event("REMOUNT", mp, true);
1236 if (mp->mnt_opt != NULL)
1237 vfs_freeopts(mp->mnt_opt);
1238 mp->mnt_opt = mp->mnt_optnew;
1240 (void)VFS_STATFS(mp, &mp->mnt_stat);
1242 * Prevent external consumers of mount options from reading
1245 mp->mnt_optnew = NULL;
1247 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1248 vfs_allocate_syncvnode(mp);
1250 vfs_deallocate_syncvnode(mp);
1253 if (rootvp != NULL) {
1254 vn_seqc_write_end(rootvp);
1257 vn_seqc_write_end(vp);
1260 vp->v_iflag &= ~VI_MOUNT;
1263 return (error != 0 ? error : export_error);
1267 * vfs_domount(): actually attempt a filesystem mount.
1271 struct thread *td, /* Calling thread. */
1272 const char *fstype, /* Filesystem type. */
1273 char *fspath, /* Mount path. */
1274 uint64_t fsflags, /* Flags common to all filesystems. */
1275 struct vfsoptlist **optlist /* Options local to the filesystem. */
1278 struct vfsconf *vfsp;
1279 struct nameidata nd;
1285 * Be ultra-paranoid about making sure the type and fspath
1286 * variables will fit in our mp buffers, including the
1289 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1290 return (ENAMETOOLONG);
1292 if (jailed(td->td_ucred) || usermount == 0) {
1293 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1298 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1300 if (fsflags & MNT_EXPORTED) {
1301 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1305 if (fsflags & MNT_SUIDDIR) {
1306 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1311 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1313 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1314 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1315 fsflags |= MNT_NOSUID | MNT_USER;
1318 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1320 if ((fsflags & MNT_UPDATE) == 0) {
1321 /* Don't try to load KLDs if we're mounting the root. */
1322 if (fsflags & MNT_ROOTFS)
1323 vfsp = vfs_byname(fstype);
1325 vfsp = vfs_byname_kld(fstype, td, &error);
1331 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1333 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1334 UIO_SYSSPACE, fspath, td);
1338 NDFREE(&nd, NDF_ONLY_PNBUF);
1340 if ((fsflags & MNT_UPDATE) == 0) {
1341 if ((vp->v_vflag & VV_ROOT) != 0 &&
1342 (fsflags & MNT_NOCOVER) != 0) {
1346 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1347 strcpy(pathbuf, fspath);
1348 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1350 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1353 free(pathbuf, M_TEMP);
1355 error = vfs_domount_update(td, vp, fsflags, optlist);
1361 * Unmount a filesystem.
1363 * Note: unmount takes a path to the vnode mounted on as argument, not
1364 * special file (as before).
1366 #ifndef _SYS_SYSPROTO_H_
1367 struct unmount_args {
1374 sys_unmount(struct thread *td, struct unmount_args *uap)
1377 return (kern_unmount(td, uap->path, uap->flags));
1381 kern_unmount(struct thread *td, const char *path, int flags)
1383 struct nameidata nd;
1386 int error, id0, id1;
1388 AUDIT_ARG_VALUE(flags);
1389 if (jailed(td->td_ucred) || usermount == 0) {
1390 error = priv_check(td, PRIV_VFS_UNMOUNT);
1395 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1396 error = copyinstr(path, pathbuf, MNAMELEN, NULL);
1398 free(pathbuf, M_TEMP);
1401 if (flags & MNT_BYFSID) {
1402 AUDIT_ARG_TEXT(pathbuf);
1403 /* Decode the filesystem ID. */
1404 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1405 free(pathbuf, M_TEMP);
1409 mtx_lock(&mountlist_mtx);
1410 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1411 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1412 mp->mnt_stat.f_fsid.val[1] == id1) {
1417 mtx_unlock(&mountlist_mtx);
1420 * Try to find global path for path argument.
1422 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1423 UIO_SYSSPACE, pathbuf, td);
1424 if (namei(&nd) == 0) {
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1426 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1431 mtx_lock(&mountlist_mtx);
1432 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1433 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1438 mtx_unlock(&mountlist_mtx);
1440 free(pathbuf, M_TEMP);
1443 * Previously we returned ENOENT for a nonexistent path and
1444 * EINVAL for a non-mountpoint. We cannot tell these apart
1445 * now, so in the !MNT_BYFSID case return the more likely
1446 * EINVAL for compatibility.
1448 return ((flags & MNT_BYFSID) ? ENOENT : EINVAL);
1452 * Don't allow unmounting the root filesystem.
1454 if (mp->mnt_flag & MNT_ROOTFS) {
1458 error = dounmount(mp, flags, td);
1463 * Return error if any of the vnodes, ignoring the root vnode
1464 * and the syncer vnode, have non-zero usecount.
1466 * This function is purely advisory - it can return false positives
1470 vfs_check_usecounts(struct mount *mp)
1472 struct vnode *vp, *mvp;
1474 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1475 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1476 vp->v_usecount != 0) {
1478 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1488 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1491 mtx_assert(MNT_MTX(mp), MA_OWNED);
1492 mp->mnt_kern_flag &= ~mntkflags;
1493 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1494 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1497 vfs_op_exit_locked(mp);
1499 if (coveredvp != NULL) {
1500 VOP_UNLOCK(coveredvp);
1503 vn_finished_write(mp);
1507 * There are various reference counters associated with the mount point.
1508 * Normally it is permitted to modify them without taking the mnt ilock,
1509 * but this behavior can be temporarily disabled if stable value is needed
1510 * or callers are expected to block (e.g. to not allow new users during
1514 vfs_op_enter(struct mount *mp)
1520 if (mp->mnt_vfs_ops > 1) {
1524 vfs_op_barrier_wait(mp);
1527 zpcpu_replace_cpu(mp->mnt_ref_pcpu, 0, cpu);
1529 zpcpu_replace_cpu(mp->mnt_lockref_pcpu, 0, cpu);
1530 mp->mnt_writeopcount +=
1531 zpcpu_replace_cpu(mp->mnt_writeopcount_pcpu, 0, cpu);
1533 if (mp->mnt_ref <= 0 || mp->mnt_lockref < 0 || mp->mnt_writeopcount < 0)
1534 panic("%s: invalid count(s) on mp %p: ref %d lockref %d writeopcount %d\n",
1535 __func__, mp, mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount);
1537 vfs_assert_mount_counters(mp);
1541 vfs_op_exit_locked(struct mount *mp)
1544 mtx_assert(MNT_MTX(mp), MA_OWNED);
1546 if (mp->mnt_vfs_ops <= 0)
1547 panic("%s: invalid vfs_ops count %d for mp %p\n",
1548 __func__, mp->mnt_vfs_ops, mp);
1553 vfs_op_exit(struct mount *mp)
1557 vfs_op_exit_locked(mp);
1561 struct vfs_op_barrier_ipi {
1563 struct smp_rendezvous_cpus_retry_arg srcra;
1567 vfs_op_action_func(void *arg)
1569 struct vfs_op_barrier_ipi *vfsopipi;
1572 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1575 if (!vfs_op_thread_entered(mp))
1576 smp_rendezvous_cpus_done(arg);
1580 vfs_op_wait_func(void *arg, int cpu)
1582 struct vfs_op_barrier_ipi *vfsopipi;
1586 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1589 in_op = zpcpu_get_cpu(mp->mnt_thread_in_ops_pcpu, cpu);
1590 while (atomic_load_int(in_op))
1595 vfs_op_barrier_wait(struct mount *mp)
1597 struct vfs_op_barrier_ipi vfsopipi;
1601 smp_rendezvous_cpus_retry(all_cpus,
1602 smp_no_rendezvous_barrier,
1604 smp_no_rendezvous_barrier,
1611 vfs_assert_mount_counters(struct mount *mp)
1615 if (mp->mnt_vfs_ops == 0)
1619 if (*zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu) != 0 ||
1620 *zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu) != 0 ||
1621 *zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu) != 0)
1622 vfs_dump_mount_counters(mp);
1627 vfs_dump_mount_counters(struct mount *mp)
1630 int ref, lockref, writeopcount;
1632 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);
1637 count = zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu);
1638 printf("%d ", *count);
1642 printf(" lockref : ");
1643 lockref = mp->mnt_lockref;
1645 count = zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu);
1646 printf("%d ", *count);
1650 printf("writeopcount: ");
1651 writeopcount = mp->mnt_writeopcount;
1653 count = zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu);
1654 printf("%d ", *count);
1655 writeopcount += *count;
1659 printf("counter struct total\n");
1660 printf("ref %-5d %-5d\n", mp->mnt_ref, ref);
1661 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref);
1662 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount);
1664 panic("invalid counts on struct mount");
1669 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
1676 base = &mp->mnt_ref;
1677 pcpu = mp->mnt_ref_pcpu;
1679 case MNT_COUNT_LOCKREF:
1680 base = &mp->mnt_lockref;
1681 pcpu = mp->mnt_lockref_pcpu;
1683 case MNT_COUNT_WRITEOPCOUNT:
1684 base = &mp->mnt_writeopcount;
1685 pcpu = mp->mnt_writeopcount_pcpu;
1691 sum += *zpcpu_get_cpu(pcpu, cpu);
1697 * Do the actual filesystem unmount.
1700 dounmount(struct mount *mp, int flags, struct thread *td)
1702 struct vnode *coveredvp, *rootvp;
1704 uint64_t async_flag;
1707 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1708 mnt_gen_r = mp->mnt_gen;
1711 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1713 * Check for mp being unmounted while waiting for the
1714 * covered vnode lock.
1716 if (coveredvp->v_mountedhere != mp ||
1717 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1718 VOP_UNLOCK(coveredvp);
1726 * Only privileged root, or (if MNT_USER is set) the user that did the
1727 * original mount is permitted to unmount this filesystem.
1729 error = vfs_suser(mp, td);
1731 if (coveredvp != NULL) {
1732 VOP_UNLOCK(coveredvp);
1741 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1743 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1744 (mp->mnt_flag & MNT_UPDATE) != 0 ||
1745 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1746 dounmount_cleanup(mp, coveredvp, 0);
1749 mp->mnt_kern_flag |= MNTK_UNMOUNT;
1750 rootvp = vfs_cache_root_clear(mp);
1751 if (coveredvp != NULL)
1752 vn_seqc_write_begin(coveredvp);
1753 if (flags & MNT_NONBUSY) {
1755 error = vfs_check_usecounts(mp);
1758 vn_seqc_write_end(coveredvp);
1759 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
1760 if (rootvp != NULL) {
1761 vn_seqc_write_end(rootvp);
1767 /* Allow filesystems to detect that a forced unmount is in progress. */
1768 if (flags & MNT_FORCE) {
1769 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1772 * Must be done after setting MNTK_UNMOUNTF and before
1773 * waiting for mnt_lockref to become 0.
1779 if (mp->mnt_lockref) {
1780 mp->mnt_kern_flag |= MNTK_DRAINING;
1781 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1785 KASSERT(mp->mnt_lockref == 0,
1786 ("%s: invalid lock refcount in the drain path @ %s:%d",
1787 __func__, __FILE__, __LINE__));
1789 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1790 __func__, __FILE__, __LINE__));
1793 * We want to keep the vnode around so that we can vn_seqc_write_end
1794 * after we are done with unmount. Downgrade our reference to a mere
1795 * hold count so that we don't interefere with anything.
1797 if (rootvp != NULL) {
1802 if (mp->mnt_flag & MNT_EXPUBLIC)
1803 vfs_setpublicfs(NULL, NULL, NULL);
1805 vfs_periodic(mp, MNT_WAIT);
1807 async_flag = mp->mnt_flag & MNT_ASYNC;
1808 mp->mnt_flag &= ~MNT_ASYNC;
1809 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1811 cache_purgevfs(mp, false); /* remove cache entries for this file sys */
1812 vfs_deallocate_syncvnode(mp);
1813 error = VFS_UNMOUNT(mp, flags);
1814 vn_finished_write(mp);
1816 * If we failed to flush the dirty blocks for this mount point,
1817 * undo all the cdir/rdir and rootvnode changes we made above.
1818 * Unless we failed to do so because the device is reporting that
1819 * it doesn't exist anymore.
1821 if (error && error != ENXIO) {
1823 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1825 vfs_allocate_syncvnode(mp);
1828 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1829 mp->mnt_flag |= async_flag;
1830 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1831 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1832 mp->mnt_kern_flag |= MNTK_ASYNC;
1833 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1834 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1837 vfs_op_exit_locked(mp);
1840 vn_seqc_write_end(coveredvp);
1841 VOP_UNLOCK(coveredvp);
1844 if (rootvp != NULL) {
1845 vn_seqc_write_end(rootvp);
1850 mtx_lock(&mountlist_mtx);
1851 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1852 mtx_unlock(&mountlist_mtx);
1853 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
1854 if (coveredvp != NULL) {
1855 coveredvp->v_mountedhere = NULL;
1856 vn_seqc_write_end(coveredvp);
1857 VOP_UNLOCK(coveredvp);
1860 mount_devctl_event("UNMOUNT", mp, false);
1861 if (rootvp != NULL) {
1862 vn_seqc_write_end(rootvp);
1865 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1866 if (rootvnode != NULL && mp == rootvnode->v_mount) {
1870 if (mp == rootdevmp)
1872 vfs_mount_destroy(mp);
1877 * Report errors during filesystem mounting.
1880 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1882 struct vfsoptlist *moptlist = mp->mnt_optnew;
1887 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1888 if (error || errmsg == NULL || len <= 0)
1892 vsnprintf(errmsg, (size_t)len, fmt, ap);
1897 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1903 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1904 if (error || errmsg == NULL || len <= 0)
1908 vsnprintf(errmsg, (size_t)len, fmt, ap);
1913 * ---------------------------------------------------------------------
1914 * Functions for querying mount options/arguments from filesystems.
1918 * Check that no unknown options are given
1921 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1925 const char **t, *p, *q;
1928 TAILQ_FOREACH(opt, opts, link) {
1931 if (p[0] == 'n' && p[1] == 'o')
1933 for(t = global_opts; *t != NULL; t++) {
1934 if (strcmp(*t, p) == 0)
1937 if (strcmp(*t, q) == 0)
1943 for(t = legal; *t != NULL; t++) {
1944 if (strcmp(*t, p) == 0)
1947 if (strcmp(*t, q) == 0)
1953 snprintf(errmsg, sizeof(errmsg),
1954 "mount option <%s> is unknown", p);
1958 TAILQ_FOREACH(opt, opts, link) {
1959 if (strcmp(opt->name, "errmsg") == 0) {
1960 strncpy((char *)opt->value, errmsg, opt->len);
1965 printf("%s\n", errmsg);
1971 * Get a mount option by its name.
1973 * Return 0 if the option was found, ENOENT otherwise.
1974 * If len is non-NULL it will be filled with the length
1975 * of the option. If buf is non-NULL, it will be filled
1976 * with the address of the option.
1979 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
1983 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1985 TAILQ_FOREACH(opt, opts, link) {
1986 if (strcmp(name, opt->name) == 0) {
1999 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2006 TAILQ_FOREACH(opt, opts, link) {
2007 if (strcmp(name, opt->name) == 0) {
2016 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
2018 char *opt_value, *vtp;
2022 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
2025 if (opt_len == 0 || opt_value == NULL)
2027 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
2029 iv = strtoq(opt_value, &vtp, 0);
2030 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
2057 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2062 TAILQ_FOREACH(opt, opts, link) {
2063 if (strcmp(name, opt->name) != 0)
2066 if (opt->len == 0 ||
2067 ((char *)opt->value)[opt->len - 1] != '\0') {
2071 return (opt->value);
2078 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
2083 TAILQ_FOREACH(opt, opts, link) {
2084 if (strcmp(name, opt->name) == 0) {
2097 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2103 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2105 TAILQ_FOREACH(opt, opts, link) {
2106 if (strcmp(name, opt->name) != 0)
2109 if (opt->len == 0 || opt->value == NULL)
2111 if (((char *)opt->value)[opt->len - 1] != '\0')
2114 ret = vsscanf(opt->value, fmt, ap);
2122 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2126 TAILQ_FOREACH(opt, opts, link) {
2127 if (strcmp(name, opt->name) != 0)
2130 if (opt->value == NULL)
2133 if (opt->len != len)
2135 bcopy(value, opt->value, len);
2143 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2147 TAILQ_FOREACH(opt, opts, link) {
2148 if (strcmp(name, opt->name) != 0)
2151 if (opt->value == NULL)
2157 bcopy(value, opt->value, len);
2165 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2169 TAILQ_FOREACH(opt, opts, link) {
2170 if (strcmp(name, opt->name) != 0)
2173 if (opt->value == NULL)
2174 opt->len = strlen(value) + 1;
2175 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2183 * Find and copy a mount option.
2185 * The size of the buffer has to be specified
2186 * in len, if it is not the same length as the
2187 * mount option, EINVAL is returned.
2188 * Returns ENOENT if the option is not found.
2191 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
2195 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2197 TAILQ_FOREACH(opt, opts, link) {
2198 if (strcmp(name, opt->name) == 0) {
2200 if (len != opt->len)
2202 bcopy(opt->value, dest, opt->len);
2210 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2214 * Filesystems only fill in part of the structure for updates, we
2215 * have to read the entirety first to get all content.
2217 if (sbp != &mp->mnt_stat)
2218 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));
2221 * Set these in case the underlying filesystem fails to do so.
2223 sbp->f_version = STATFS_VERSION;
2224 sbp->f_namemax = NAME_MAX;
2225 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2227 return (mp->mnt_op->vfs_statfs(mp, sbp));
2231 vfs_mountedfrom(struct mount *mp, const char *from)
2234 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2235 strlcpy(mp->mnt_stat.f_mntfromname, from,
2236 sizeof mp->mnt_stat.f_mntfromname);
2240 * ---------------------------------------------------------------------
2241 * This is the api for building mount args and mounting filesystems from
2242 * inside the kernel.
2244 * The API works by accumulation of individual args. First error is
2247 * XXX: should be documented in new manpage kernel_mount(9)
2250 /* A memory allocation which must be freed when we are done */
2252 SLIST_ENTRY(mntaarg) next;
2255 /* The header for the mount arguments */
2260 SLIST_HEAD(, mntaarg) list;
2264 * Add a boolean argument.
2266 * flag is the boolean value.
2267 * name must start with "no".
2270 mount_argb(struct mntarg *ma, int flag, const char *name)
2273 KASSERT(name[0] == 'n' && name[1] == 'o',
2274 ("mount_argb(...,%s): name must start with 'no'", name));
2276 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2280 * Add an argument printf style
2283 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2286 struct mntaarg *maa;
2291 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2292 SLIST_INIT(&ma->list);
2297 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2299 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2300 ma->v[ma->len].iov_len = strlen(name) + 1;
2303 sb = sbuf_new_auto();
2305 sbuf_vprintf(sb, fmt, ap);
2308 len = sbuf_len(sb) + 1;
2309 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2310 SLIST_INSERT_HEAD(&ma->list, maa, next);
2311 bcopy(sbuf_data(sb), maa + 1, len);
2314 ma->v[ma->len].iov_base = maa + 1;
2315 ma->v[ma->len].iov_len = len;
2322 * Add an argument which is a userland string.
2325 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2327 struct mntaarg *maa;
2333 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2334 SLIST_INIT(&ma->list);
2338 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2339 SLIST_INSERT_HEAD(&ma->list, maa, next);
2340 tbuf = (void *)(maa + 1);
2341 ma->error = copyinstr(val, tbuf, len, NULL);
2342 return (mount_arg(ma, name, tbuf, -1));
2348 * If length is -1, treat value as a C string.
2351 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2355 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2356 SLIST_INIT(&ma->list);
2361 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2363 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2364 ma->v[ma->len].iov_len = strlen(name) + 1;
2367 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2369 ma->v[ma->len].iov_len = strlen(val) + 1;
2371 ma->v[ma->len].iov_len = len;
2377 * Free a mntarg structure
2380 free_mntarg(struct mntarg *ma)
2382 struct mntaarg *maa;
2384 while (!SLIST_EMPTY(&ma->list)) {
2385 maa = SLIST_FIRST(&ma->list);
2386 SLIST_REMOVE_HEAD(&ma->list, next);
2389 free(ma->v, M_MOUNT);
2394 * Mount a filesystem
2397 kernel_mount(struct mntarg *ma, uint64_t flags)
2402 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2403 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2404 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2406 auio.uio_iov = ma->v;
2407 auio.uio_iovcnt = ma->len;
2408 auio.uio_segflg = UIO_SYSSPACE;
2412 error = vfs_donmount(curthread, flags, &auio);
2418 * A printflike function to mount a filesystem.
2421 kernel_vmount(int flags, ...)
2423 struct mntarg *ma = NULL;
2429 va_start(ap, flags);
2431 cp = va_arg(ap, const char *);
2434 vp = va_arg(ap, const void *);
2435 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2439 error = kernel_mount(ma, flags);
2443 /* Map from mount options to printable formats. */
2444 static struct mntoptnames optnames[] = {
2449 mount_devctl_event_mntopt(struct sbuf *sb, const char *what, struct vfsoptlist *opts)
2453 if (opts == NULL || TAILQ_EMPTY(opts))
2455 sbuf_printf(sb, " %s=\"", what);
2456 TAILQ_FOREACH(opt, opts, link) {
2457 if (opt->name[0] == '\0' || (opt->len > 0 && *(char *)opt->value == '\0'))
2459 devctl_safe_quote_sb(sb, opt->name);
2462 devctl_safe_quote_sb(sb, opt->value);
2469 #define DEVCTL_LEN 1024
2471 mount_devctl_event(const char *type, struct mount *mp, bool donew)
2474 struct mntoptnames *fp;
2476 struct statfs *sfp = &mp->mnt_stat;
2479 buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT);
2482 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
2483 sbuf_cpy(&sb, "mount-point=\"");
2484 devctl_safe_quote_sb(&sb, sfp->f_mntonname);
2485 sbuf_cat(&sb, "\" mount-dev=\"");
2486 devctl_safe_quote_sb(&sb, sfp->f_mntfromname);
2487 sbuf_cat(&sb, "\" mount-type=\"");
2488 devctl_safe_quote_sb(&sb, sfp->f_fstypename);
2489 sbuf_cat(&sb, "\" fsid=0x");
2490 cp = (const uint8_t *)&sfp->f_fsid.val[0];
2491 for (int i = 0; i < sizeof(sfp->f_fsid); i++)
2492 sbuf_printf(&sb, "%02x", cp[i]);
2493 sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner);
2494 for (fp = optnames; fp->o_opt != 0; fp++) {
2495 if ((mp->mnt_flag & fp->o_opt) != 0) {
2496 sbuf_cat(&sb, fp->o_name);
2497 sbuf_putc(&sb, ';');
2500 sbuf_putc(&sb, '"');
2501 mount_devctl_event_mntopt(&sb, "opt", mp->mnt_opt);
2503 mount_devctl_event_mntopt(&sb, "optnew", mp->mnt_optnew);
2506 if (sbuf_error(&sb) == 0)
2507 devctl_notify("VFS", "FS", type, sbuf_data(&sb));