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/eventhandler.h>
46 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
50 #include <sys/libkern.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/reboot.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysproto.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/systm.h>
66 #include <sys/vnode.h>
69 #include <geom/geom.h>
71 #include <machine/stdarg.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
76 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64)
78 static int vfs_domount(struct thread *td, const char *fstype, char *fspath,
79 uint64_t fsflags, struct vfsoptlist **optlist);
80 static void free_mntarg(struct mntarg *ma);
82 static int usermount = 0;
83 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
84 "Unprivileged users may mount and unmount file systems");
86 static bool default_autoro = false;
87 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
88 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
90 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
91 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
92 static uma_zone_t mount_zone;
94 /* List of mounted filesystems. */
95 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
97 /* For any iteration/modification of mountlist */
98 struct mtx mountlist_mtx;
99 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
101 EVENTHANDLER_LIST_DEFINE(vfs_mounted);
102 EVENTHANDLER_LIST_DEFINE(vfs_unmounted);
105 * Global opts, taken by all filesystems
107 static const char *global_opts[] = {
119 mount_init(void *mem, int size, int flags)
123 mp = (struct mount *)mem;
124 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
125 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
126 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
127 mp->mnt_thread_in_ops_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
129 mp->mnt_ref_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
131 mp->mnt_lockref_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
133 mp->mnt_writeopcount_pcpu = uma_zalloc_pcpu(pcpu_zone_int,
137 mp->mnt_rootvnode = NULL;
142 mount_fini(void *mem, int size)
146 mp = (struct mount *)mem;
147 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_writeopcount_pcpu);
148 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_lockref_pcpu);
149 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_ref_pcpu);
150 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_thread_in_ops_pcpu);
151 lockdestroy(&mp->mnt_explock);
152 mtx_destroy(&mp->mnt_listmtx);
153 mtx_destroy(&mp->mnt_mtx);
157 vfs_mount_init(void *dummy __unused)
160 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
161 NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
163 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
166 * ---------------------------------------------------------------------
167 * Functions for building and sanitizing the mount options
170 /* Remove one mount option. */
172 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
175 TAILQ_REMOVE(opts, opt, link);
176 free(opt->name, M_MOUNT);
177 if (opt->value != NULL)
178 free(opt->value, M_MOUNT);
182 /* Release all resources related to the mount options. */
184 vfs_freeopts(struct vfsoptlist *opts)
188 while (!TAILQ_EMPTY(opts)) {
189 opt = TAILQ_FIRST(opts);
190 vfs_freeopt(opts, opt);
196 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
198 struct vfsopt *opt, *temp;
202 TAILQ_FOREACH_SAFE(opt, opts, link, temp) {
203 if (strcmp(opt->name, name) == 0)
204 vfs_freeopt(opts, opt);
209 vfs_isopt_ro(const char *opt)
212 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
213 strcmp(opt, "norw") == 0)
219 vfs_isopt_rw(const char *opt)
222 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
228 * Check if options are equal (with or without the "no" prefix).
231 vfs_equalopts(const char *opt1, const char *opt2)
235 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
236 if (strcmp(opt1, opt2) == 0)
238 /* "noopt" vs. "opt" */
239 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
241 /* "opt" vs. "noopt" */
242 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
244 while ((p = strchr(opt1, '.')) != NULL &&
245 !strncmp(opt1, opt2, ++p - opt1)) {
248 /* "foo.noopt" vs. "foo.opt" */
249 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
251 /* "foo.opt" vs. "foo.noopt" */
252 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
255 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
256 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
257 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
263 * If a mount option is specified several times,
264 * (with or without the "no" prefix) only keep
265 * the last occurrence of it.
268 vfs_sanitizeopts(struct vfsoptlist *opts)
270 struct vfsopt *opt, *opt2, *tmp;
272 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
273 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
274 while (opt2 != NULL) {
275 if (vfs_equalopts(opt->name, opt2->name)) {
276 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
277 vfs_freeopt(opts, opt2);
280 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
287 * Build a linked list of mount options from a struct uio.
290 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
292 struct vfsoptlist *opts;
294 size_t memused, namelen, optlen;
295 unsigned int i, iovcnt;
298 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
301 iovcnt = auio->uio_iovcnt;
302 for (i = 0; i < iovcnt; i += 2) {
303 namelen = auio->uio_iov[i].iov_len;
304 optlen = auio->uio_iov[i + 1].iov_len;
305 memused += sizeof(struct vfsopt) + optlen + namelen;
307 * Avoid consuming too much memory, and attempts to overflow
310 if (memused > VFS_MOUNTARG_SIZE_MAX ||
311 optlen > VFS_MOUNTARG_SIZE_MAX ||
312 namelen > VFS_MOUNTARG_SIZE_MAX) {
317 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
318 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
325 * Do this early, so jumps to "bad" will free the current
328 TAILQ_INSERT_TAIL(opts, opt, link);
330 if (auio->uio_segflg == UIO_SYSSPACE) {
331 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
333 error = copyin(auio->uio_iov[i].iov_base, opt->name,
338 /* Ensure names are null-terminated strings. */
339 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
345 opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
346 if (auio->uio_segflg == UIO_SYSSPACE) {
347 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
350 error = copyin(auio->uio_iov[i + 1].iov_base,
357 vfs_sanitizeopts(opts);
366 * Merge the old mount options with the new ones passed
367 * in the MNT_UPDATE case.
369 * XXX: This function will keep a "nofoo" option in the new
370 * options. E.g, if the option's canonical name is "foo",
371 * "nofoo" ends up in the mount point's active options.
374 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
376 struct vfsopt *opt, *new;
378 TAILQ_FOREACH(opt, oldopts, link) {
379 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
380 new->name = strdup(opt->name, M_MOUNT);
382 new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
383 bcopy(opt->value, new->value, opt->len);
387 new->seen = opt->seen;
388 TAILQ_INSERT_HEAD(toopts, new, link);
390 vfs_sanitizeopts(toopts);
394 * Mount a filesystem.
396 #ifndef _SYS_SYSPROTO_H_
404 sys_nmount(struct thread *td, struct nmount_args *uap)
412 * Mount flags are now 64-bits. On 32-bit archtectures only
413 * 32-bits are passed in, but from here on everything handles
414 * 64-bit flags correctly.
418 AUDIT_ARG_FFLAGS(flags);
419 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
420 uap->iovp, uap->iovcnt, flags);
423 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
424 * userspace to set this flag, but we must filter it out if we want
425 * MNT_UPDATE on the root file system to work.
426 * MNT_ROOTFS should only be set by the kernel when mounting its
429 flags &= ~MNT_ROOTFS;
431 iovcnt = uap->iovcnt;
433 * Check that we have an even number of iovec's
434 * and that we have at least two options.
436 if ((iovcnt & 1) || (iovcnt < 4)) {
437 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
442 error = copyinuio(uap->iovp, iovcnt, &auio);
444 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
448 error = vfs_donmount(td, flags, auio);
455 * ---------------------------------------------------------------------
456 * Various utility functions
460 vfs_ref(struct mount *mp)
463 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
464 if (vfs_op_thread_enter(mp)) {
465 vfs_mp_count_add_pcpu(mp, ref, 1);
466 vfs_op_thread_exit(mp);
476 vfs_rel(struct mount *mp)
479 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
480 if (vfs_op_thread_enter(mp)) {
481 vfs_mp_count_sub_pcpu(mp, ref, 1);
482 vfs_op_thread_exit(mp);
492 * Allocate and initialize the mount point struct.
495 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
500 mp = uma_zalloc(mount_zone, M_WAITOK);
501 bzero(&mp->mnt_startzero,
502 __rangeof(struct mount, mnt_startzero, mnt_endzero));
503 TAILQ_INIT(&mp->mnt_nvnodelist);
504 mp->mnt_nvnodelistsize = 0;
505 TAILQ_INIT(&mp->mnt_lazyvnodelist);
506 mp->mnt_lazyvnodelistsize = 0;
507 if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 ||
508 mp->mnt_writeopcount != 0)
509 panic("%s: non-zero counters on new mp %p\n", __func__, mp);
510 if (mp->mnt_vfs_ops != 1)
511 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
513 (void) vfs_busy(mp, MBF_NOWAIT);
514 atomic_add_acq_int(&vfsp->vfc_refcount, 1);
515 mp->mnt_op = vfsp->vfc_vfsops;
517 mp->mnt_stat.f_type = vfsp->vfc_typenum;
519 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
520 mp->mnt_vnodecovered = vp;
521 mp->mnt_cred = crdup(cred);
522 mp->mnt_stat.f_owner = cred->cr_uid;
523 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
524 mp->mnt_iosize_max = DFLTPHYS;
527 mac_mount_create(cred, mp);
529 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
530 TAILQ_INIT(&mp->mnt_uppers);
535 * Destroy the mount struct previously allocated by vfs_mount_alloc().
538 vfs_mount_destroy(struct mount *mp)
541 if (mp->mnt_vfs_ops == 0)
542 panic("%s: entered with zero vfs_ops\n", __func__);
544 vfs_assert_mount_counters(mp);
547 mp->mnt_kern_flag |= MNTK_REFEXPIRE;
548 if (mp->mnt_kern_flag & MNTK_MWAIT) {
549 mp->mnt_kern_flag &= ~MNTK_MWAIT;
553 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
554 KASSERT(mp->mnt_ref == 0,
555 ("%s: invalid refcount in the drain path @ %s:%d", __func__,
556 __FILE__, __LINE__));
557 if (mp->mnt_writeopcount != 0)
558 panic("vfs_mount_destroy: nonzero writeopcount");
559 if (mp->mnt_secondary_writes != 0)
560 panic("vfs_mount_destroy: nonzero secondary_writes");
561 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
562 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
565 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
566 vn_printf(vp, "dangling vnode ");
567 panic("unmount: dangling vnode");
569 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
570 if (mp->mnt_nvnodelistsize != 0)
571 panic("vfs_mount_destroy: nonzero nvnodelistsize");
572 if (mp->mnt_lazyvnodelistsize != 0)
573 panic("vfs_mount_destroy: nonzero lazyvnodelistsize");
574 if (mp->mnt_lockref != 0)
575 panic("vfs_mount_destroy: nonzero lock refcount");
578 if (mp->mnt_vfs_ops != 1)
579 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
582 if (mp->mnt_rootvnode != NULL)
583 panic("%s: mount point still has a root vnode %p\n", __func__,
586 if (mp->mnt_vnodecovered != NULL)
587 vrele(mp->mnt_vnodecovered);
589 mac_mount_destroy(mp);
591 if (mp->mnt_opt != NULL)
592 vfs_freeopts(mp->mnt_opt);
593 crfree(mp->mnt_cred);
594 uma_zfree(mount_zone, mp);
598 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
600 /* This is an upgrade of an exisiting mount. */
601 if ((fsflags & MNT_UPDATE) != 0)
603 /* This is already an R/O mount. */
604 if ((fsflags & MNT_RDONLY) != 0)
608 case ENODEV: /* generic, geom, ... */
609 case EACCES: /* cam/scsi, ... */
610 case EROFS: /* md, mmcsd, ... */
612 * These errors can be returned by the storage layer to signal
613 * that the media is read-only. No harm in the R/O mount
614 * attempt if the error was returned for some other reason.
623 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
625 struct vfsoptlist *optlist;
626 struct vfsopt *opt, *tmp_opt;
627 char *fstype, *fspath, *errmsg;
628 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
631 errmsg = fspath = NULL;
632 errmsg_len = fspathlen = 0;
634 autoro = default_autoro;
636 error = vfs_buildopts(fsoptions, &optlist);
640 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
641 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
644 * We need these two options before the others,
645 * and they are mandatory for any filesystem.
646 * Ensure they are NUL terminated as well.
649 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
650 if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
653 strncpy(errmsg, "Invalid fstype", errmsg_len);
657 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
658 if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
661 strncpy(errmsg, "Invalid fspath", errmsg_len);
666 * We need to see if we have the "update" option
667 * before we call vfs_domount(), since vfs_domount() has special
668 * logic based on MNT_UPDATE. This is very important
669 * when we want to update the root filesystem.
671 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
674 if (strcmp(opt->name, "update") == 0) {
675 fsflags |= MNT_UPDATE;
678 else if (strcmp(opt->name, "async") == 0)
679 fsflags |= MNT_ASYNC;
680 else if (strcmp(opt->name, "force") == 0) {
681 fsflags |= MNT_FORCE;
684 else if (strcmp(opt->name, "reload") == 0) {
685 fsflags |= MNT_RELOAD;
688 else if (strcmp(opt->name, "multilabel") == 0)
689 fsflags |= MNT_MULTILABEL;
690 else if (strcmp(opt->name, "noasync") == 0)
691 fsflags &= ~MNT_ASYNC;
692 else if (strcmp(opt->name, "noatime") == 0)
693 fsflags |= MNT_NOATIME;
694 else if (strcmp(opt->name, "atime") == 0) {
695 free(opt->name, M_MOUNT);
696 opt->name = strdup("nonoatime", M_MOUNT);
698 else if (strcmp(opt->name, "noclusterr") == 0)
699 fsflags |= MNT_NOCLUSTERR;
700 else if (strcmp(opt->name, "clusterr") == 0) {
701 free(opt->name, M_MOUNT);
702 opt->name = strdup("nonoclusterr", M_MOUNT);
704 else if (strcmp(opt->name, "noclusterw") == 0)
705 fsflags |= MNT_NOCLUSTERW;
706 else if (strcmp(opt->name, "clusterw") == 0) {
707 free(opt->name, M_MOUNT);
708 opt->name = strdup("nonoclusterw", M_MOUNT);
710 else if (strcmp(opt->name, "noexec") == 0)
711 fsflags |= MNT_NOEXEC;
712 else if (strcmp(opt->name, "exec") == 0) {
713 free(opt->name, M_MOUNT);
714 opt->name = strdup("nonoexec", M_MOUNT);
716 else if (strcmp(opt->name, "nosuid") == 0)
717 fsflags |= MNT_NOSUID;
718 else if (strcmp(opt->name, "suid") == 0) {
719 free(opt->name, M_MOUNT);
720 opt->name = strdup("nonosuid", M_MOUNT);
722 else if (strcmp(opt->name, "nosymfollow") == 0)
723 fsflags |= MNT_NOSYMFOLLOW;
724 else if (strcmp(opt->name, "symfollow") == 0) {
725 free(opt->name, M_MOUNT);
726 opt->name = strdup("nonosymfollow", M_MOUNT);
728 else if (strcmp(opt->name, "noro") == 0) {
729 fsflags &= ~MNT_RDONLY;
732 else if (strcmp(opt->name, "rw") == 0) {
733 fsflags &= ~MNT_RDONLY;
736 else if (strcmp(opt->name, "ro") == 0) {
737 fsflags |= MNT_RDONLY;
740 else if (strcmp(opt->name, "rdonly") == 0) {
741 free(opt->name, M_MOUNT);
742 opt->name = strdup("ro", M_MOUNT);
743 fsflags |= MNT_RDONLY;
746 else if (strcmp(opt->name, "autoro") == 0) {
750 else if (strcmp(opt->name, "suiddir") == 0)
751 fsflags |= MNT_SUIDDIR;
752 else if (strcmp(opt->name, "sync") == 0)
753 fsflags |= MNT_SYNCHRONOUS;
754 else if (strcmp(opt->name, "union") == 0)
755 fsflags |= MNT_UNION;
756 else if (strcmp(opt->name, "automounted") == 0) {
757 fsflags |= MNT_AUTOMOUNTED;
759 } else if (strcmp(opt->name, "nocover") == 0) {
760 fsflags |= MNT_NOCOVER;
762 } else if (strcmp(opt->name, "cover") == 0) {
763 fsflags &= ~MNT_NOCOVER;
765 } else if (strcmp(opt->name, "emptydir") == 0) {
766 fsflags |= MNT_EMPTYDIR;
768 } else if (strcmp(opt->name, "noemptydir") == 0) {
769 fsflags &= ~MNT_EMPTYDIR;
773 vfs_freeopt(optlist, opt);
777 * Be ultra-paranoid about making sure the type and fspath
778 * variables will fit in our mp buffers, including the
781 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
782 error = ENAMETOOLONG;
786 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
789 * See if we can mount in the read-only mode if the error code suggests
790 * that it could be possible and the mount options allow for that.
791 * Never try it if "[no]{ro|rw}" has been explicitly requested and not
792 * overridden by "autoro".
794 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
795 printf("%s: R/W mount failed, possibly R/O media,"
796 " trying R/O mount\n", __func__);
797 fsflags |= MNT_RDONLY;
798 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
801 /* copyout the errmsg */
802 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
803 && errmsg_len > 0 && errmsg != NULL) {
804 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
806 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
807 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
810 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
811 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
816 vfs_freeopts(optlist);
823 #ifndef _SYS_SYSPROTO_H_
833 sys_mount(struct thread *td, struct mount_args *uap)
836 struct vfsconf *vfsp = NULL;
837 struct mntarg *ma = NULL;
842 * Mount flags are now 64-bits. On 32-bit architectures only
843 * 32-bits are passed in, but from here on everything handles
844 * 64-bit flags correctly.
848 AUDIT_ARG_FFLAGS(flags);
851 * Filter out MNT_ROOTFS. We do not want clients of mount() in
852 * userspace to set this flag, but we must filter it out if we want
853 * MNT_UPDATE on the root file system to work.
854 * MNT_ROOTFS should only be set by the kernel when mounting its
857 flags &= ~MNT_ROOTFS;
859 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
860 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
862 free(fstype, M_TEMP);
866 AUDIT_ARG_TEXT(fstype);
867 vfsp = vfs_byname_kld(fstype, td, &error);
868 free(fstype, M_TEMP);
871 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
872 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
873 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
874 vfsp->vfc_vfsops->vfs_cmount == NULL))
877 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
878 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
879 ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
880 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
881 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
883 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
884 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
885 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
889 * vfs_domount_first(): first file system mount (not update)
893 struct thread *td, /* Calling thread. */
894 struct vfsconf *vfsp, /* File system type. */
895 char *fspath, /* Mount path. */
896 struct vnode *vp, /* Vnode to be covered. */
897 uint64_t fsflags, /* Flags common to all filesystems. */
898 struct vfsoptlist **optlist /* Options local to the filesystem. */
903 struct vnode *newdp, *rootvp;
906 ASSERT_VOP_ELOCKED(vp, __func__);
907 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
909 if ((fsflags & MNT_EMPTYDIR) != 0) {
910 error = vfs_emptydir(vp);
918 * If the jail of the calling thread lacks permission for this type of
919 * file system, deny immediately.
921 if (jailed(td->td_ucred) && !prison_allow(td->td_ucred,
922 vfsp->vfc_prison_flag)) {
928 * If the user is not root, ensure that they own the directory
929 * onto which we are attempting to mount.
931 error = VOP_GETATTR(vp, &va, td->td_ucred);
932 if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
933 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
935 error = vinvalbuf(vp, V_SAVE, 0, 0);
936 if (error == 0 && vp->v_type != VDIR)
940 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
941 vp->v_iflag |= VI_MOUNT;
952 /* Allocate and initialize the filesystem. */
953 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
954 /* XXXMAC: pass to vfs_mount_alloc? */
955 mp->mnt_optnew = *optlist;
956 /* Set the mount level flags. */
957 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
960 * Mount the filesystem.
961 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
962 * get. No freeing of cn_pnbuf.
965 if ((error = VFS_MOUNT(mp)) != 0 ||
966 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
967 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
970 rootvp = vfs_cache_root_clear(mp);
973 if ((error1 = VFS_UNMOUNT(mp, 0)) != 0)
974 printf("VFS_UNMOUNT returned %d\n", error1);
977 mp->mnt_vnodecovered = NULL;
978 vfs_mount_destroy(mp);
980 vp->v_iflag &= ~VI_MOUNT;
987 if (mp->mnt_opt != NULL)
988 vfs_freeopts(mp->mnt_opt);
989 mp->mnt_opt = mp->mnt_optnew;
993 * Prevent external consumers of mount options from reading mnt_optnew.
995 mp->mnt_optnew = NULL;
998 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
999 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1000 mp->mnt_kern_flag |= MNTK_ASYNC;
1002 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1005 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1008 vp->v_iflag &= ~VI_MOUNT;
1010 vp->v_mountedhere = mp;
1011 /* Place the new filesystem at the end of the mount list. */
1012 mtx_lock(&mountlist_mtx);
1013 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1014 mtx_unlock(&mountlist_mtx);
1015 vfs_event_signal(NULL, VQ_MOUNT, 0);
1016 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
1018 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
1020 mountcheckdirs(vp, newdp);
1022 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1023 vfs_allocate_syncvnode(mp);
1030 * vfs_domount_update(): update of mounted file system
1034 struct thread *td, /* Calling thread. */
1035 struct vnode *vp, /* Mount point vnode. */
1036 uint64_t fsflags, /* Flags common to all filesystems. */
1037 struct vfsoptlist **optlist /* Options local to the filesystem. */
1040 struct export_args export;
1041 struct o2export_args o2export;
1042 struct vnode *rootvp;
1045 int error, export_error, i, len;
1049 ASSERT_VOP_ELOCKED(vp, __func__);
1050 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
1053 if ((vp->v_vflag & VV_ROOT) == 0) {
1054 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
1064 * We only allow the filesystem to be reloaded if it
1065 * is currently mounted read-only.
1067 flag = mp->mnt_flag;
1068 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
1070 return (EOPNOTSUPP); /* Needs translation */
1073 * Only privileged root, or (if MNT_USER is set) the user that
1074 * did the original mount is permitted to update it.
1076 error = vfs_suser(mp, td);
1081 if (vfs_busy(mp, MBF_NOWAIT)) {
1086 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1092 vp->v_iflag |= VI_MOUNT;
1099 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1104 mp->mnt_flag &= ~MNT_UPDATEMASK;
1105 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1106 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1107 if ((mp->mnt_flag & MNT_ASYNC) == 0)
1108 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1109 rootvp = vfs_cache_root_clear(mp);
1113 mp->mnt_optnew = *optlist;
1114 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1117 * Mount the filesystem.
1118 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1119 * get. No freeing of cn_pnbuf.
1121 error = VFS_MOUNT(mp);
1124 /* Process the export option. */
1125 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1127 /* Assume that there is only 1 ABI for each length. */
1129 case (sizeof(struct oexport_args)):
1130 bzero(&o2export, sizeof(o2export));
1132 case (sizeof(o2export)):
1133 bcopy(bufp, &o2export, len);
1134 export.ex_flags = (uint64_t)o2export.ex_flags;
1135 export.ex_root = o2export.ex_root;
1136 export.ex_uid = o2export.ex_anon.cr_uid;
1137 export.ex_groups = NULL;
1138 export.ex_ngroups = o2export.ex_anon.cr_ngroups;
1139 if (export.ex_ngroups > 0) {
1140 if (export.ex_ngroups <= XU_NGROUPS) {
1141 export.ex_groups = malloc(
1142 export.ex_ngroups * sizeof(gid_t),
1144 for (i = 0; i < export.ex_ngroups; i++)
1145 export.ex_groups[i] =
1146 o2export.ex_anon.cr_groups[i];
1148 export_error = EINVAL;
1149 } else if (export.ex_ngroups < 0)
1150 export_error = EINVAL;
1151 export.ex_addr = o2export.ex_addr;
1152 export.ex_addrlen = o2export.ex_addrlen;
1153 export.ex_mask = o2export.ex_mask;
1154 export.ex_masklen = o2export.ex_masklen;
1155 export.ex_indexfile = o2export.ex_indexfile;
1156 export.ex_numsecflavors = o2export.ex_numsecflavors;
1157 if (export.ex_numsecflavors < MAXSECFLAVORS) {
1158 for (i = 0; i < export.ex_numsecflavors; i++)
1159 export.ex_secflavors[i] =
1160 o2export.ex_secflavors[i];
1162 export_error = EINVAL;
1163 if (export_error == 0)
1164 export_error = vfs_export(mp, &export);
1165 free(export.ex_groups, M_TEMP);
1167 case (sizeof(export)):
1168 bcopy(bufp, &export, len);
1170 if (export.ex_ngroups > 0) {
1171 if (export.ex_ngroups <= NGROUPS_MAX) {
1172 grps = malloc(export.ex_ngroups *
1173 sizeof(gid_t), M_TEMP, M_WAITOK);
1174 export_error = copyin(export.ex_groups,
1175 grps, export.ex_ngroups *
1177 if (export_error == 0)
1178 export.ex_groups = grps;
1180 export_error = EINVAL;
1181 } else if (export.ex_ngroups == 0)
1182 export.ex_groups = NULL;
1184 export_error = EINVAL;
1185 if (export_error == 0)
1186 export_error = vfs_export(mp, &export);
1190 export_error = EINVAL;
1197 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1201 * If we fail, restore old mount flags. MNT_QUOTA is special,
1202 * because it is not part of MNT_UPDATEMASK, but it could have
1203 * changed in the meantime if quotactl(2) was called.
1204 * All in all we want current value of MNT_QUOTA, not the old
1207 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1209 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1210 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1211 mp->mnt_kern_flag |= MNTK_ASYNC;
1213 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1219 if (mp->mnt_opt != NULL)
1220 vfs_freeopts(mp->mnt_opt);
1221 mp->mnt_opt = mp->mnt_optnew;
1223 (void)VFS_STATFS(mp, &mp->mnt_stat);
1225 * Prevent external consumers of mount options from reading
1228 mp->mnt_optnew = NULL;
1230 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1231 vfs_allocate_syncvnode(mp);
1233 vfs_deallocate_syncvnode(mp);
1238 vp->v_iflag &= ~VI_MOUNT;
1241 return (error != 0 ? error : export_error);
1245 * vfs_domount(): actually attempt a filesystem mount.
1249 struct thread *td, /* Calling thread. */
1250 const char *fstype, /* Filesystem type. */
1251 char *fspath, /* Mount path. */
1252 uint64_t fsflags, /* Flags common to all filesystems. */
1253 struct vfsoptlist **optlist /* Options local to the filesystem. */
1256 struct vfsconf *vfsp;
1257 struct nameidata nd;
1263 * Be ultra-paranoid about making sure the type and fspath
1264 * variables will fit in our mp buffers, including the
1267 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1268 return (ENAMETOOLONG);
1270 if (jailed(td->td_ucred) || usermount == 0) {
1271 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1276 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1278 if (fsflags & MNT_EXPORTED) {
1279 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1283 if (fsflags & MNT_SUIDDIR) {
1284 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1289 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1291 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1292 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1293 fsflags |= MNT_NOSUID | MNT_USER;
1296 /* Load KLDs before we lock the covered vnode to avoid reversals. */
1298 if ((fsflags & MNT_UPDATE) == 0) {
1299 /* Don't try to load KLDs if we're mounting the root. */
1300 if (fsflags & MNT_ROOTFS)
1301 vfsp = vfs_byname(fstype);
1303 vfsp = vfs_byname_kld(fstype, td, &error);
1309 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1311 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1312 UIO_SYSSPACE, fspath, td);
1316 NDFREE(&nd, NDF_ONLY_PNBUF);
1318 if ((fsflags & MNT_UPDATE) == 0) {
1319 if ((vp->v_vflag & VV_ROOT) != 0 &&
1320 (fsflags & MNT_NOCOVER) != 0) {
1324 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1325 strcpy(pathbuf, fspath);
1326 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1328 error = vfs_domount_first(td, vfsp, pathbuf, vp,
1331 free(pathbuf, M_TEMP);
1333 error = vfs_domount_update(td, vp, fsflags, optlist);
1339 * Unmount a filesystem.
1341 * Note: unmount takes a path to the vnode mounted on as argument, not
1342 * special file (as before).
1344 #ifndef _SYS_SYSPROTO_H_
1345 struct unmount_args {
1352 sys_unmount(struct thread *td, struct unmount_args *uap)
1355 return (kern_unmount(td, uap->path, uap->flags));
1359 kern_unmount(struct thread *td, const char *path, int flags)
1361 struct nameidata nd;
1364 int error, id0, id1;
1366 AUDIT_ARG_VALUE(flags);
1367 if (jailed(td->td_ucred) || usermount == 0) {
1368 error = priv_check(td, PRIV_VFS_UNMOUNT);
1373 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1374 error = copyinstr(path, pathbuf, MNAMELEN, NULL);
1376 free(pathbuf, M_TEMP);
1379 if (flags & MNT_BYFSID) {
1380 AUDIT_ARG_TEXT(pathbuf);
1381 /* Decode the filesystem ID. */
1382 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1383 free(pathbuf, M_TEMP);
1387 mtx_lock(&mountlist_mtx);
1388 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1389 if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1390 mp->mnt_stat.f_fsid.val[1] == id1) {
1395 mtx_unlock(&mountlist_mtx);
1398 * Try to find global path for path argument.
1400 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1401 UIO_SYSSPACE, pathbuf, td);
1402 if (namei(&nd) == 0) {
1403 NDFREE(&nd, NDF_ONLY_PNBUF);
1404 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1409 mtx_lock(&mountlist_mtx);
1410 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1411 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1416 mtx_unlock(&mountlist_mtx);
1418 free(pathbuf, M_TEMP);
1421 * Previously we returned ENOENT for a nonexistent path and
1422 * EINVAL for a non-mountpoint. We cannot tell these apart
1423 * now, so in the !MNT_BYFSID case return the more likely
1424 * EINVAL for compatibility.
1426 return ((flags & MNT_BYFSID) ? ENOENT : EINVAL);
1430 * Don't allow unmounting the root filesystem.
1432 if (mp->mnt_flag & MNT_ROOTFS) {
1436 error = dounmount(mp, flags, td);
1441 * Return error if any of the vnodes, ignoring the root vnode
1442 * and the syncer vnode, have non-zero usecount.
1444 * This function is purely advisory - it can return false positives
1448 vfs_check_usecounts(struct mount *mp)
1450 struct vnode *vp, *mvp;
1452 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1453 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1454 vp->v_usecount != 0) {
1456 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1466 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1469 mtx_assert(MNT_MTX(mp), MA_OWNED);
1470 mp->mnt_kern_flag &= ~mntkflags;
1471 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1472 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1475 vfs_op_exit_locked(mp);
1477 if (coveredvp != NULL) {
1478 VOP_UNLOCK(coveredvp);
1481 vn_finished_write(mp);
1485 * There are various reference counters associated with the mount point.
1486 * Normally it is permitted to modify them without taking the mnt ilock,
1487 * but this behavior can be temporarily disabled if stable value is needed
1488 * or callers are expected to block (e.g. to not allow new users during
1492 vfs_op_enter(struct mount *mp)
1498 if (mp->mnt_vfs_ops > 1) {
1502 vfs_op_barrier_wait(mp);
1505 zpcpu_replace_cpu(mp->mnt_ref_pcpu, 0, cpu);
1507 zpcpu_replace_cpu(mp->mnt_lockref_pcpu, 0, cpu);
1508 mp->mnt_writeopcount +=
1509 zpcpu_replace_cpu(mp->mnt_writeopcount_pcpu, 0, cpu);
1512 vfs_assert_mount_counters(mp);
1516 vfs_op_exit_locked(struct mount *mp)
1519 mtx_assert(MNT_MTX(mp), MA_OWNED);
1521 if (mp->mnt_vfs_ops <= 0)
1522 panic("%s: invalid vfs_ops count %d for mp %p\n",
1523 __func__, mp->mnt_vfs_ops, mp);
1528 vfs_op_exit(struct mount *mp)
1532 vfs_op_exit_locked(mp);
1536 struct vfs_op_barrier_ipi {
1538 struct smp_rendezvous_cpus_retry_arg srcra;
1542 vfs_op_action_func(void *arg)
1544 struct vfs_op_barrier_ipi *vfsopipi;
1547 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1550 if (!vfs_op_thread_entered(mp))
1551 smp_rendezvous_cpus_done(arg);
1555 vfs_op_wait_func(void *arg, int cpu)
1557 struct vfs_op_barrier_ipi *vfsopipi;
1561 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1564 in_op = zpcpu_get_cpu(mp->mnt_thread_in_ops_pcpu, cpu);
1565 while (atomic_load_int(in_op))
1570 vfs_op_barrier_wait(struct mount *mp)
1572 struct vfs_op_barrier_ipi vfsopipi;
1576 smp_rendezvous_cpus_retry(all_cpus,
1577 smp_no_rendezvous_barrier,
1579 smp_no_rendezvous_barrier,
1586 vfs_assert_mount_counters(struct mount *mp)
1590 if (mp->mnt_vfs_ops == 0)
1594 if (*zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu) != 0 ||
1595 *zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu) != 0 ||
1596 *zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu) != 0)
1597 vfs_dump_mount_counters(mp);
1602 vfs_dump_mount_counters(struct mount *mp)
1605 int ref, lockref, writeopcount;
1607 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);
1612 count = zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu);
1613 printf("%d ", *count);
1617 printf(" lockref : ");
1618 lockref = mp->mnt_lockref;
1620 count = zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu);
1621 printf("%d ", *count);
1625 printf("writeopcount: ");
1626 writeopcount = mp->mnt_writeopcount;
1628 count = zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu);
1629 printf("%d ", *count);
1630 writeopcount += *count;
1634 printf("counter struct total\n");
1635 printf("ref %-5d %-5d\n", mp->mnt_ref, ref);
1636 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref);
1637 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount);
1639 panic("invalid counts on struct mount");
1644 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
1651 base = &mp->mnt_ref;
1652 pcpu = mp->mnt_ref_pcpu;
1654 case MNT_COUNT_LOCKREF:
1655 base = &mp->mnt_lockref;
1656 pcpu = mp->mnt_lockref_pcpu;
1658 case MNT_COUNT_WRITEOPCOUNT:
1659 base = &mp->mnt_writeopcount;
1660 pcpu = mp->mnt_writeopcount_pcpu;
1666 sum += *zpcpu_get_cpu(pcpu, cpu);
1672 * Do the actual filesystem unmount.
1675 dounmount(struct mount *mp, int flags, struct thread *td)
1677 struct vnode *coveredvp, *rootvp;
1679 uint64_t async_flag;
1682 if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1683 mnt_gen_r = mp->mnt_gen;
1686 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1688 * Check for mp being unmounted while waiting for the
1689 * covered vnode lock.
1691 if (coveredvp->v_mountedhere != mp ||
1692 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1693 VOP_UNLOCK(coveredvp);
1701 * Only privileged root, or (if MNT_USER is set) the user that did the
1702 * original mount is permitted to unmount this filesystem.
1704 error = vfs_suser(mp, td);
1706 if (coveredvp != NULL) {
1707 VOP_UNLOCK(coveredvp);
1716 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1718 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1719 (mp->mnt_flag & MNT_UPDATE) != 0 ||
1720 !TAILQ_EMPTY(&mp->mnt_uppers)) {
1721 dounmount_cleanup(mp, coveredvp, 0);
1724 mp->mnt_kern_flag |= MNTK_UNMOUNT;
1725 rootvp = vfs_cache_root_clear(mp);
1726 if (flags & MNT_NONBUSY) {
1728 error = vfs_check_usecounts(mp);
1731 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
1737 /* Allow filesystems to detect that a forced unmount is in progress. */
1738 if (flags & MNT_FORCE) {
1739 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1742 * Must be done after setting MNTK_UNMOUNTF and before
1743 * waiting for mnt_lockref to become 0.
1749 if (mp->mnt_lockref) {
1750 mp->mnt_kern_flag |= MNTK_DRAINING;
1751 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1755 KASSERT(mp->mnt_lockref == 0,
1756 ("%s: invalid lock refcount in the drain path @ %s:%d",
1757 __func__, __FILE__, __LINE__));
1759 ("%s: invalid return value for msleep in the drain path @ %s:%d",
1760 __func__, __FILE__, __LINE__));
1765 if (mp->mnt_flag & MNT_EXPUBLIC)
1766 vfs_setpublicfs(NULL, NULL, NULL);
1769 * From now, we can claim that the use reference on the
1770 * coveredvp is ours, and the ref can be released only by
1771 * successfull unmount by us, or left for later unmount
1772 * attempt. The previously acquired hold reference is no
1773 * longer needed to protect the vnode from reuse.
1775 if (coveredvp != NULL)
1778 vfs_periodic(mp, MNT_WAIT);
1780 async_flag = mp->mnt_flag & MNT_ASYNC;
1781 mp->mnt_flag &= ~MNT_ASYNC;
1782 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1784 cache_purgevfs(mp, false); /* remove cache entries for this file sys */
1785 vfs_deallocate_syncvnode(mp);
1786 error = VFS_UNMOUNT(mp, flags);
1787 vn_finished_write(mp);
1789 * If we failed to flush the dirty blocks for this mount point,
1790 * undo all the cdir/rdir and rootvnode changes we made above.
1791 * Unless we failed to do so because the device is reporting that
1792 * it doesn't exist anymore.
1794 if (error && error != ENXIO) {
1796 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1798 vfs_allocate_syncvnode(mp);
1801 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1802 mp->mnt_flag |= async_flag;
1803 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1804 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1805 mp->mnt_kern_flag |= MNTK_ASYNC;
1806 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1807 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1810 vfs_op_exit_locked(mp);
1813 VOP_UNLOCK(coveredvp);
1816 mtx_lock(&mountlist_mtx);
1817 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1818 mtx_unlock(&mountlist_mtx);
1819 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
1820 if (coveredvp != NULL) {
1821 coveredvp->v_mountedhere = NULL;
1822 VOP_UNLOCK(coveredvp);
1824 vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1825 if (rootvnode != NULL && mp == rootvnode->v_mount) {
1829 if (mp == rootdevmp)
1831 vfs_mount_destroy(mp);
1836 * Report errors during filesystem mounting.
1839 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1841 struct vfsoptlist *moptlist = mp->mnt_optnew;
1846 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1847 if (error || errmsg == NULL || len <= 0)
1851 vsnprintf(errmsg, (size_t)len, fmt, ap);
1856 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1862 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1863 if (error || errmsg == NULL || len <= 0)
1867 vsnprintf(errmsg, (size_t)len, fmt, ap);
1872 * ---------------------------------------------------------------------
1873 * Functions for querying mount options/arguments from filesystems.
1877 * Check that no unknown options are given
1880 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1884 const char **t, *p, *q;
1887 TAILQ_FOREACH(opt, opts, link) {
1890 if (p[0] == 'n' && p[1] == 'o')
1892 for(t = global_opts; *t != NULL; t++) {
1893 if (strcmp(*t, p) == 0)
1896 if (strcmp(*t, q) == 0)
1902 for(t = legal; *t != NULL; t++) {
1903 if (strcmp(*t, p) == 0)
1906 if (strcmp(*t, q) == 0)
1912 snprintf(errmsg, sizeof(errmsg),
1913 "mount option <%s> is unknown", p);
1917 TAILQ_FOREACH(opt, opts, link) {
1918 if (strcmp(opt->name, "errmsg") == 0) {
1919 strncpy((char *)opt->value, errmsg, opt->len);
1924 printf("%s\n", errmsg);
1930 * Get a mount option by its name.
1932 * Return 0 if the option was found, ENOENT otherwise.
1933 * If len is non-NULL it will be filled with the length
1934 * of the option. If buf is non-NULL, it will be filled
1935 * with the address of the option.
1938 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
1942 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1944 TAILQ_FOREACH(opt, opts, link) {
1945 if (strcmp(name, opt->name) == 0) {
1958 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1965 TAILQ_FOREACH(opt, opts, link) {
1966 if (strcmp(name, opt->name) == 0) {
1975 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
1977 char *opt_value, *vtp;
1981 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
1984 if (opt_len == 0 || opt_value == NULL)
1986 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
1988 iv = strtoq(opt_value, &vtp, 0);
1989 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
2016 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2021 TAILQ_FOREACH(opt, opts, link) {
2022 if (strcmp(name, opt->name) != 0)
2025 if (opt->len == 0 ||
2026 ((char *)opt->value)[opt->len - 1] != '\0') {
2030 return (opt->value);
2037 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
2042 TAILQ_FOREACH(opt, opts, link) {
2043 if (strcmp(name, opt->name) == 0) {
2056 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2062 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2064 TAILQ_FOREACH(opt, opts, link) {
2065 if (strcmp(name, opt->name) != 0)
2068 if (opt->len == 0 || opt->value == NULL)
2070 if (((char *)opt->value)[opt->len - 1] != '\0')
2073 ret = vsscanf(opt->value, fmt, ap);
2081 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2085 TAILQ_FOREACH(opt, opts, link) {
2086 if (strcmp(name, opt->name) != 0)
2089 if (opt->value == NULL)
2092 if (opt->len != len)
2094 bcopy(value, opt->value, len);
2102 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2106 TAILQ_FOREACH(opt, opts, link) {
2107 if (strcmp(name, opt->name) != 0)
2110 if (opt->value == NULL)
2116 bcopy(value, opt->value, len);
2124 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2128 TAILQ_FOREACH(opt, opts, link) {
2129 if (strcmp(name, opt->name) != 0)
2132 if (opt->value == NULL)
2133 opt->len = strlen(value) + 1;
2134 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2142 * Find and copy a mount option.
2144 * The size of the buffer has to be specified
2145 * in len, if it is not the same length as the
2146 * mount option, EINVAL is returned.
2147 * Returns ENOENT if the option is not found.
2150 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
2154 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2156 TAILQ_FOREACH(opt, opts, link) {
2157 if (strcmp(name, opt->name) == 0) {
2159 if (len != opt->len)
2161 bcopy(opt->value, dest, opt->len);
2169 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2173 * Filesystems only fill in part of the structure for updates, we
2174 * have to read the entirety first to get all content.
2176 if (sbp != &mp->mnt_stat)
2177 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));
2180 * Set these in case the underlying filesystem fails to do so.
2182 sbp->f_version = STATFS_VERSION;
2183 sbp->f_namemax = NAME_MAX;
2184 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2186 return (mp->mnt_op->vfs_statfs(mp, sbp));
2190 vfs_mountedfrom(struct mount *mp, const char *from)
2193 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2194 strlcpy(mp->mnt_stat.f_mntfromname, from,
2195 sizeof mp->mnt_stat.f_mntfromname);
2199 * ---------------------------------------------------------------------
2200 * This is the api for building mount args and mounting filesystems from
2201 * inside the kernel.
2203 * The API works by accumulation of individual args. First error is
2206 * XXX: should be documented in new manpage kernel_mount(9)
2209 /* A memory allocation which must be freed when we are done */
2211 SLIST_ENTRY(mntaarg) next;
2214 /* The header for the mount arguments */
2219 SLIST_HEAD(, mntaarg) list;
2223 * Add a boolean argument.
2225 * flag is the boolean value.
2226 * name must start with "no".
2229 mount_argb(struct mntarg *ma, int flag, const char *name)
2232 KASSERT(name[0] == 'n' && name[1] == 'o',
2233 ("mount_argb(...,%s): name must start with 'no'", name));
2235 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2239 * Add an argument printf style
2242 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2245 struct mntaarg *maa;
2250 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2251 SLIST_INIT(&ma->list);
2256 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2258 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2259 ma->v[ma->len].iov_len = strlen(name) + 1;
2262 sb = sbuf_new_auto();
2264 sbuf_vprintf(sb, fmt, ap);
2267 len = sbuf_len(sb) + 1;
2268 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2269 SLIST_INSERT_HEAD(&ma->list, maa, next);
2270 bcopy(sbuf_data(sb), maa + 1, len);
2273 ma->v[ma->len].iov_base = maa + 1;
2274 ma->v[ma->len].iov_len = len;
2281 * Add an argument which is a userland string.
2284 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2286 struct mntaarg *maa;
2292 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2293 SLIST_INIT(&ma->list);
2297 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2298 SLIST_INSERT_HEAD(&ma->list, maa, next);
2299 tbuf = (void *)(maa + 1);
2300 ma->error = copyinstr(val, tbuf, len, NULL);
2301 return (mount_arg(ma, name, tbuf, -1));
2307 * If length is -1, treat value as a C string.
2310 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2314 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2315 SLIST_INIT(&ma->list);
2320 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2322 ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2323 ma->v[ma->len].iov_len = strlen(name) + 1;
2326 ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2328 ma->v[ma->len].iov_len = strlen(val) + 1;
2330 ma->v[ma->len].iov_len = len;
2336 * Free a mntarg structure
2339 free_mntarg(struct mntarg *ma)
2341 struct mntaarg *maa;
2343 while (!SLIST_EMPTY(&ma->list)) {
2344 maa = SLIST_FIRST(&ma->list);
2345 SLIST_REMOVE_HEAD(&ma->list, next);
2348 free(ma->v, M_MOUNT);
2353 * Mount a filesystem
2356 kernel_mount(struct mntarg *ma, uint64_t flags)
2361 KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2362 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2363 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2365 auio.uio_iov = ma->v;
2366 auio.uio_iovcnt = ma->len;
2367 auio.uio_segflg = UIO_SYSSPACE;
2371 error = vfs_donmount(curthread, flags, &auio);
2377 * A printflike function to mount a filesystem.
2380 kernel_vmount(int flags, ...)
2382 struct mntarg *ma = NULL;
2388 va_start(ap, flags);
2390 cp = va_arg(ap, const char *);
2393 vp = va_arg(ap, const void *);
2394 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2398 error = kernel_mount(ma, flags);