1 /* $NetBSD: vfs_subr.c,v 1.394 2010/01/08 11:35:10 pooka Exp $ */
4 * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
70 * Note on v_usecount and locking:
72 * At nearly all points it is known that v_usecount could be zero, the
73 * vnode interlock will be held.
75 * To change v_usecount away from zero, the interlock must be held. To
76 * change from a non-zero value to zero, again the interlock must be
79 * There's a flag bit, VC_XLOCK, embedded in v_usecount.
80 * To raise v_usecount, if the VC_XLOCK bit is set in it, the interlock
82 * To modify the VC_XLOCK bit, the interlock must be held.
83 * We always keep the usecount (v_usecount & VC_MASK) non-zero while the
84 * VC_XLOCK bit is set.
86 * Unless the VC_XLOCK bit is set, changing the usecount from a non-zero
87 * value to a non-zero value can safely be done using atomic operations,
88 * without the interlock held.
89 * Even if the VC_XLOCK bit is set, decreasing the usecount to a non-zero
90 * value can be done using atomic operations, without the interlock held.
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.394 2010/01/08 11:35:10 pooka Exp $");
97 #include "opt_compat_netbsd.h"
98 #include "opt_compat_43.h"
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/conf.h>
103 #include <sys/proc.h>
104 #include <sys/kernel.h>
105 #include <sys/mount.h>
106 #include <sys/fcntl.h>
107 #include <sys/vnode.h>
108 #include <sys/stat.h>
109 #include <sys/namei.h>
110 #include <sys/ucred.h>
112 #include <sys/errno.h>
113 #include <sys/kmem.h>
114 #include <sys/syscallargs.h>
115 #include <sys/device.h>
116 #include <sys/filedesc.h>
117 #include <sys/kauth.h>
118 #include <sys/atomic.h>
119 #include <sys/kthread.h>
120 #include <sys/wapbl.h>
122 #include <miscfs/genfs/genfs.h>
123 #include <miscfs/specfs/specdev.h>
124 #include <miscfs/syncfs/syncfs.h>
127 #include <uvm/uvm_readahead.h>
128 #include <uvm/uvm_ddb.h>
130 #include <sys/sysctl.h>
132 const enum vtype iftovt_tab
[16] = {
133 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
134 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
136 const int vttoif_tab
[9] = {
137 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
138 S_IFSOCK
, S_IFIFO
, S_IFMT
,
142 * Insq/Remq for the vnode usage lists.
144 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs)
145 #define bufremvn(bp) { \
146 LIST_REMOVE(bp, b_vnbufs); \
147 (bp)->b_vnbufs.le_next = NOLIST; \
150 int doforce
= 1; /* 1 => permit forcible unmounting */
151 int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
153 static vnodelst_t vnode_free_list
= TAILQ_HEAD_INITIALIZER(vnode_free_list
);
154 static vnodelst_t vnode_hold_list
= TAILQ_HEAD_INITIALIZER(vnode_hold_list
);
155 static vnodelst_t vrele_list
= TAILQ_HEAD_INITIALIZER(vrele_list
);
157 struct mntlist mountlist
= /* mounted filesystem list */
158 CIRCLEQ_HEAD_INITIALIZER(mountlist
);
161 static specificdata_domain_t mount_specificdata_domain
;
163 static int vrele_pending
;
164 static int vrele_gen
;
165 static kmutex_t vrele_lock
;
166 static kcondvar_t vrele_cv
;
167 static lwp_t
*vrele_lwp
;
169 static uint64_t mountgen
= 0;
170 static kmutex_t mountgen_lock
;
172 kmutex_t mountlist_lock
;
174 kmutex_t mntvnode_lock
;
175 kmutex_t vnode_free_list_lock
;
176 kmutex_t vfs_list_lock
;
178 static pool_cache_t vnode_cache
;
181 * These define the root filesystem and device.
183 struct vnode
*rootvnode
;
184 struct device
*root_device
; /* root device */
187 * Local declarations.
190 static void vrele_thread(void *);
191 static void insmntque(vnode_t
*, struct mount
*);
192 static int getdevvp(dev_t
, vnode_t
**, enum vtype
);
193 static vnode_t
*getcleanvnode(void);
194 void vpanic(vnode_t
*, const char *);
195 static void vfs_shutdown1(struct lwp
*);
198 void printlockedvnodes(void);
203 vpanic(vnode_t
*vp
, const char *msg
)
210 #define vpanic(vp, msg) /* nothing */
217 vnode_cache
= pool_cache_init(sizeof(struct vnode
), 0, 0, 0, "vnodepl",
218 NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
219 KASSERT(vnode_cache
!= NULL
);
221 /* Create deferred release thread. */
222 mutex_init(&vrele_lock
, MUTEX_DEFAULT
, IPL_NONE
);
223 cv_init(&vrele_cv
, "vrele");
224 if (kthread_create(PRI_VM
, KTHREAD_MPSAFE
, NULL
, vrele_thread
,
225 NULL
, &vrele_lwp
, "vrele"))
230 * Initialize the vnode management data structures.
236 mutex_init(&mountgen_lock
, MUTEX_DEFAULT
, IPL_NONE
);
237 mutex_init(&mountlist_lock
, MUTEX_DEFAULT
, IPL_NONE
);
238 mutex_init(&mntid_lock
, MUTEX_DEFAULT
, IPL_NONE
);
239 mutex_init(&mntvnode_lock
, MUTEX_DEFAULT
, IPL_NONE
);
240 mutex_init(&vnode_free_list_lock
, MUTEX_DEFAULT
, IPL_NONE
);
241 mutex_init(&vfs_list_lock
, MUTEX_DEFAULT
, IPL_NONE
);
243 mount_specificdata_domain
= specificdata_domain_create();
245 /* Initialize the filesystem syncer. */
246 vn_initialize_syncerd();
251 vfs_drainvnodes(long target
, struct lwp
*l
)
254 while (numvnodes
> target
) {
257 mutex_enter(&vnode_free_list_lock
);
258 vp
= getcleanvnode();
260 return EBUSY
; /* give up */
268 * Lookup a mount point by filesystem identifier.
270 * XXX Needs to add a reference to the mount point.
273 vfs_getvfs(fsid_t
*fsid
)
277 mutex_enter(&mountlist_lock
);
278 CIRCLEQ_FOREACH(mp
, &mountlist
, mnt_list
) {
279 if (mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] == fsid
->__fsid_val
[0] &&
280 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1] == fsid
->__fsid_val
[1]) {
281 mutex_exit(&mountlist_lock
);
285 mutex_exit(&mountlist_lock
);
286 return ((struct mount
*)0);
290 * Drop a reference to a mount structure, freeing if the last reference.
293 vfs_destroy(struct mount
*mp
)
296 if (__predict_true((int)atomic_dec_uint_nv(&mp
->mnt_refcnt
) > 0)) {
301 * Nothing else has visibility of the mount: we can now
302 * free the data structures.
304 KASSERT(mp
->mnt_refcnt
== 0);
305 specificdata_fini(mount_specificdata_domain
, &mp
->mnt_specdataref
);
306 rw_destroy(&mp
->mnt_unmounting
);
307 mutex_destroy(&mp
->mnt_updating
);
308 mutex_destroy(&mp
->mnt_renamelock
);
309 if (mp
->mnt_op
!= NULL
) {
310 vfs_delref(mp
->mnt_op
);
312 kmem_free(mp
, sizeof(*mp
));
316 * grab a vnode from freelist and clean it.
324 KASSERT(mutex_owned(&vnode_free_list_lock
));
327 listhd
= &vnode_free_list
;
329 TAILQ_FOREACH(vp
, listhd
, v_freelist
) {
331 * It's safe to test v_usecount and v_iflag
332 * without holding the interlock here, since
333 * these vnodes should never appear on the
336 if (vp
->v_usecount
!= 0) {
337 vpanic(vp
, "free vnode isn't");
339 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
340 vpanic(vp
, "clean vnode on freelist");
342 if (vp
->v_freelisthd
!= listhd
) {
343 printf("vnode sez %p, listhd %p\n", vp
->v_freelisthd
, listhd
);
344 vpanic(vp
, "list head mismatch");
346 if (!mutex_tryenter(&vp
->v_interlock
))
349 * Our lwp might hold the underlying vnode
350 * locked, so don't try to reclaim a VI_LAYER
351 * node if it's locked.
353 if ((vp
->v_iflag
& VI_XLOCK
) == 0 &&
354 ((vp
->v_iflag
& VI_LAYER
) == 0 || VOP_ISLOCKED(vp
) == 0)) {
357 mutex_exit(&vp
->v_interlock
);
361 if (listhd
== &vnode_free_list
) {
362 listhd
= &vnode_hold_list
;
365 mutex_exit(&vnode_free_list_lock
);
369 /* Remove it from the freelist. */
370 TAILQ_REMOVE(listhd
, vp
, v_freelist
);
371 vp
->v_freelisthd
= NULL
;
372 mutex_exit(&vnode_free_list_lock
);
374 if (vp
->v_usecount
!= 0) {
376 * was referenced again before we got the interlock
377 * Don't return to freelist - the holder of the last
378 * reference will destroy it.
380 mutex_exit(&vp
->v_interlock
);
381 mutex_enter(&vnode_free_list_lock
);
386 * The vnode is still associated with a file system, so we must
387 * clean it out before reusing it. We need to add a reference
388 * before doing this. If the vnode gains another reference while
389 * being cleaned out then we lose - retry.
391 atomic_add_int(&vp
->v_usecount
, 1 + VC_XLOCK
);
393 KASSERT(vp
->v_usecount
>= 1 + VC_XLOCK
);
394 atomic_add_int(&vp
->v_usecount
, -VC_XLOCK
);
395 if (vp
->v_usecount
== 1) {
396 /* We're about to dirty it. */
397 vp
->v_iflag
&= ~VI_CLEAN
;
398 mutex_exit(&vp
->v_interlock
);
399 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
400 spec_node_destroy(vp
);
405 * Don't return to freelist - the holder of the last
406 * reference will destroy it.
408 vrelel(vp
, 0); /* releases vp->v_interlock */
409 mutex_enter(&vnode_free_list_lock
);
413 if (vp
->v_data
!= NULL
|| vp
->v_uobj
.uo_npages
!= 0 ||
414 !TAILQ_EMPTY(&vp
->v_uobj
.memq
)) {
415 vpanic(vp
, "cleaned vnode isn't");
417 if (vp
->v_numoutput
!= 0) {
418 vpanic(vp
, "clean vnode has pending I/O's");
420 if ((vp
->v_iflag
& VI_ONWORKLST
) != 0) {
421 vpanic(vp
, "clean vnode on syncer list");
428 * Mark a mount point as busy, and gain a new reference to it. Used to
429 * prevent the file system from being unmounted during critical sections.
431 * => The caller must hold a pre-existing reference to the mount.
432 * => Will fail if the file system is being unmounted, or is unmounted.
435 vfs_busy(struct mount
*mp
, struct mount
**nextp
)
438 KASSERT(mp
->mnt_refcnt
> 0);
440 if (__predict_false(!rw_tryenter(&mp
->mnt_unmounting
, RW_READER
))) {
442 KASSERT(mutex_owned(&mountlist_lock
));
443 *nextp
= CIRCLEQ_NEXT(mp
, mnt_list
);
447 if (__predict_false((mp
->mnt_iflag
& IMNT_GONE
) != 0)) {
448 rw_exit(&mp
->mnt_unmounting
);
450 KASSERT(mutex_owned(&mountlist_lock
));
451 *nextp
= CIRCLEQ_NEXT(mp
, mnt_list
);
456 mutex_exit(&mountlist_lock
);
458 atomic_inc_uint(&mp
->mnt_refcnt
);
463 * Unbusy a busy filesystem.
465 * => If keepref is true, preserve reference added by vfs_busy().
466 * => If nextp != NULL, acquire mountlist_lock.
469 vfs_unbusy(struct mount
*mp
, bool keepref
, struct mount
**nextp
)
472 KASSERT(mp
->mnt_refcnt
> 0);
475 mutex_enter(&mountlist_lock
);
477 rw_exit(&mp
->mnt_unmounting
);
482 KASSERT(mutex_owned(&mountlist_lock
));
483 *nextp
= CIRCLEQ_NEXT(mp
, mnt_list
);
488 vfs_mountalloc(struct vfsops
*vfsops
, struct vnode
*vp
)
493 mp
= kmem_zalloc(sizeof(*mp
), KM_SLEEP
);
499 TAILQ_INIT(&mp
->mnt_vnodelist
);
500 rw_init(&mp
->mnt_unmounting
);
501 mutex_init(&mp
->mnt_renamelock
, MUTEX_DEFAULT
, IPL_NONE
);
502 mutex_init(&mp
->mnt_updating
, MUTEX_DEFAULT
, IPL_NONE
);
503 error
= vfs_busy(mp
, NULL
);
505 mp
->mnt_vnodecovered
= vp
;
506 mount_initspecific(mp
);
508 mutex_enter(&mountgen_lock
);
509 mp
->mnt_gen
= mountgen
++;
510 mutex_exit(&mountgen_lock
);
516 * Lookup a filesystem type, and if found allocate and initialize
517 * a mount structure for it.
519 * Devname is usually updated by mount(8) after booting.
522 vfs_rootmountalloc(const char *fstypename
, const char *devname
,
525 struct vfsops
*vfsp
= NULL
;
528 mutex_enter(&vfs_list_lock
);
529 LIST_FOREACH(vfsp
, &vfs_list
, vfs_list
)
530 if (!strncmp(vfsp
->vfs_name
, fstypename
,
531 sizeof(mp
->mnt_stat
.f_fstypename
)))
534 mutex_exit(&vfs_list_lock
);
537 vfsp
->vfs_refcount
++;
538 mutex_exit(&vfs_list_lock
);
540 if ((mp
= vfs_mountalloc(vfsp
, NULL
)) == NULL
)
542 mp
->mnt_flag
= MNT_RDONLY
;
543 (void)strlcpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfs_name
,
544 sizeof(mp
->mnt_stat
.f_fstypename
));
545 mp
->mnt_stat
.f_mntonname
[0] = '/';
546 mp
->mnt_stat
.f_mntonname
[1] = '\0';
547 mp
->mnt_stat
.f_mntfromname
[sizeof(mp
->mnt_stat
.f_mntfromname
) - 1] =
549 (void)copystr(devname
, mp
->mnt_stat
.f_mntfromname
,
550 sizeof(mp
->mnt_stat
.f_mntfromname
) - 1, 0);
556 * Routines having to do with the management of the vnode table.
558 extern int (**dead_vnodeop_p
)(void *);
561 * Return the next vnode from the free list.
564 getnewvnode(enum vtagtype tag
, struct mount
*mp
, int (**vops
)(void *),
567 struct uvm_object
*uobj
;
570 int error
= 0, tryalloc
;
575 * Mark filesystem busy while we're creating a
576 * vnode. If unmount is in progress, this will
579 error
= vfs_busy(mp
, NULL
);
585 * We must choose whether to allocate a new vnode or recycle an
586 * existing one. The criterion for allocating a new one is that
587 * the total number of vnodes is less than the number desired or
588 * there are no vnodes on either free list. Generally we only
589 * want to recycle vnodes that have no buffers associated with
590 * them, so we look first on the vnode_free_list. If it is empty,
591 * we next consider vnodes with referencing buffers on the
592 * vnode_hold_list. The toggle ensures that half the time we
593 * will use a buffer from the vnode_hold_list, and half the time
594 * we will allocate a new one unless the list has grown to twice
595 * the desired size. We are reticent to recycle vnodes from the
596 * vnode_hold_list because we will lose the identity of all its
597 * referencing buffers.
602 mutex_enter(&vnode_free_list_lock
);
605 if (numvnodes
> 2 * desiredvnodes
)
608 tryalloc
= numvnodes
< desiredvnodes
||
609 (TAILQ_FIRST(&vnode_free_list
) == NULL
&&
610 (TAILQ_FIRST(&vnode_hold_list
) == NULL
|| toggle
));
614 mutex_exit(&vnode_free_list_lock
);
615 if ((vp
= vnalloc(NULL
)) == NULL
) {
616 mutex_enter(&vnode_free_list_lock
);
623 vp
= getcleanvnode();
626 vfs_unbusy(mp
, false, NULL
);
629 printf("WARNING: unable to allocate new "
630 "vnode, retrying...\n");
631 kpause("newvn", false, hz
, NULL
);
634 tablefull("vnode", "increase kern.maxvnodes or NVNODE");
644 KASSERT(vp
->v_usecount
== 1);
645 KASSERT(vp
->v_freelisthd
== NULL
);
646 KASSERT(LIST_EMPTY(&vp
->v_nclist
));
647 KASSERT(LIST_EMPTY(&vp
->v_dnclist
));
650 vp
->v_vnlock
= &vp
->v_lock
;
658 * initialize uvm_object within vnode.
662 KASSERT(uobj
->pgops
== &uvm_vnodeops
);
663 KASSERT(uobj
->uo_npages
== 0);
664 KASSERT(TAILQ_FIRST(&uobj
->memq
) == NULL
);
665 vp
->v_size
= vp
->v_writesize
= VSIZENOTSET
;
668 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) != 0)
669 vp
->v_vflag
|= VV_MPSAFE
;
670 vfs_unbusy(mp
, true, NULL
);
677 * This is really just the reverse of getnewvnode(). Needed for
678 * VFS_VGET functions who may need to push back a vnode in case
682 ungetnewvnode(vnode_t
*vp
)
685 KASSERT(vp
->v_usecount
== 1);
686 KASSERT(vp
->v_data
== NULL
);
687 KASSERT(vp
->v_freelisthd
== NULL
);
689 mutex_enter(&vp
->v_interlock
);
690 vp
->v_iflag
|= VI_CLEAN
;
695 * Allocate a new, uninitialized vnode. If 'mp' is non-NULL, this is a
696 * marker vnode and we are prepared to wait for the allocation.
699 vnalloc(struct mount
*mp
)
703 vp
= pool_cache_get(vnode_cache
, (mp
!= NULL
? PR_WAITOK
: PR_NOWAIT
));
708 memset(vp
, 0, sizeof(*vp
));
709 UVM_OBJ_INIT(&vp
->v_uobj
, &uvm_vnodeops
, 0);
710 cv_init(&vp
->v_cv
, "vnode");
712 * done by memset() above.
713 * LIST_INIT(&vp->v_nclist);
714 * LIST_INIT(&vp->v_dnclist);
720 vp
->v_iflag
= VI_MARKER
;
722 rw_init(&vp
->v_lock
.vl_lock
);
729 * Free an unused, unreferenced vnode.
735 KASSERT(vp
->v_usecount
== 0);
737 if ((vp
->v_iflag
& VI_MARKER
) == 0) {
738 rw_destroy(&vp
->v_lock
.vl_lock
);
739 mutex_enter(&vnode_free_list_lock
);
741 mutex_exit(&vnode_free_list_lock
);
744 UVM_OBJ_DESTROY(&vp
->v_uobj
);
745 cv_destroy(&vp
->v_cv
);
746 pool_cache_put(vnode_cache
, vp
);
750 * Remove a vnode from its freelist.
753 vremfree(vnode_t
*vp
)
756 KASSERT(mutex_owned(&vp
->v_interlock
));
757 KASSERT(vp
->v_usecount
== 0);
760 * Note that the reference count must not change until
761 * the vnode is removed.
763 mutex_enter(&vnode_free_list_lock
);
764 if (vp
->v_holdcnt
> 0) {
765 KASSERT(vp
->v_freelisthd
== &vnode_hold_list
);
767 KASSERT(vp
->v_freelisthd
== &vnode_free_list
);
769 TAILQ_REMOVE(vp
->v_freelisthd
, vp
, v_freelist
);
770 vp
->v_freelisthd
= NULL
;
771 mutex_exit(&vnode_free_list_lock
);
775 * Move a vnode from one mount queue to another.
778 insmntque(vnode_t
*vp
, struct mount
*mp
)
784 (mp
->mnt_iflag
& IMNT_UNMOUNT
) &&
785 vp
->v_tag
!= VT_VFS
) {
786 panic("insmntque into dying filesystem");
790 mutex_enter(&mntvnode_lock
);
792 * Delete from old mount point vnode list, if on one.
794 if ((omp
= vp
->v_mount
) != NULL
)
795 TAILQ_REMOVE(&vp
->v_mount
->mnt_vnodelist
, vp
, v_mntvnodes
);
797 * Insert into list of vnodes for the new mount point, if
798 * available. The caller must take a reference on the mount
799 * structure and donate to the vnode.
801 if ((vp
->v_mount
= mp
) != NULL
)
802 TAILQ_INSERT_TAIL(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
803 mutex_exit(&mntvnode_lock
);
806 /* Release reference to old mount. */
812 * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
816 vwait(vnode_t
*vp
, int flags
)
819 KASSERT(mutex_owned(&vp
->v_interlock
));
820 KASSERT(vp
->v_usecount
!= 0);
822 while ((vp
->v_iflag
& flags
) != 0)
823 cv_wait(&vp
->v_cv
, &vp
->v_interlock
);
827 * Insert a marker vnode into a mount's vnode list, after the
828 * specified vnode. mntvnode_lock must be held.
831 vmark(vnode_t
*mvp
, vnode_t
*vp
)
837 KASSERT(mutex_owned(&mntvnode_lock
));
838 KASSERT((mvp
->v_iflag
& VI_MARKER
) != 0);
839 KASSERT(vp
->v_mount
== mp
);
841 TAILQ_INSERT_AFTER(&mp
->mnt_vnodelist
, vp
, mvp
, v_mntvnodes
);
845 * Remove a marker vnode from a mount's vnode list, and return
846 * a pointer to the next vnode in the list. mntvnode_lock must
850 vunmark(vnode_t
*mvp
)
857 KASSERT(mutex_owned(&mntvnode_lock
));
858 KASSERT((mvp
->v_iflag
& VI_MARKER
) != 0);
860 vp
= TAILQ_NEXT(mvp
, v_mntvnodes
);
861 TAILQ_REMOVE(&mp
->mnt_vnodelist
, mvp
, v_mntvnodes
);
863 KASSERT(vp
== NULL
|| vp
->v_mount
== mp
);
869 * Update outstanding I/O count and do wakeup if requested.
872 vwakeup(struct buf
*bp
)
876 if ((vp
= bp
->b_vp
) == NULL
)
879 KASSERT(bp
->b_objlock
== &vp
->v_interlock
);
880 KASSERT(mutex_owned(bp
->b_objlock
));
882 if (--vp
->v_numoutput
< 0)
883 panic("vwakeup: neg numoutput, vp %p", vp
);
884 if (vp
->v_numoutput
== 0)
885 cv_broadcast(&vp
->v_cv
);
889 * Flush out and invalidate all buffers associated with a vnode.
890 * Called with the underlying vnode locked, which should prevent new dirty
891 * buffers from being queued.
894 vinvalbuf(struct vnode
*vp
, int flags
, kauth_cred_t cred
, struct lwp
*l
,
895 bool catch, int slptimeo
)
897 struct buf
*bp
, *nbp
;
899 int flushflags
= PGO_ALLPAGES
| PGO_FREE
| PGO_SYNCIO
|
900 (flags
& V_SAVE
? PGO_CLEANIT
| PGO_RECLAIM
: 0);
902 /* XXXUBC this doesn't look at flags or slp* */
903 mutex_enter(&vp
->v_interlock
);
904 error
= VOP_PUTPAGES(vp
, 0, 0, flushflags
);
909 if (flags
& V_SAVE
) {
910 error
= VOP_FSYNC(vp
, cred
, FSYNC_WAIT
|FSYNC_RECLAIM
, 0, 0);
913 KASSERT(LIST_EMPTY(&vp
->v_dirtyblkhd
));
916 mutex_enter(&bufcache_lock
);
918 for (bp
= LIST_FIRST(&vp
->v_dirtyblkhd
); bp
; bp
= nbp
) {
919 nbp
= LIST_NEXT(bp
, b_vnbufs
);
920 error
= bbusy(bp
, catch, slptimeo
, NULL
);
922 if (error
== EPASSTHROUGH
)
924 mutex_exit(&bufcache_lock
);
927 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
930 for (bp
= LIST_FIRST(&vp
->v_cleanblkhd
); bp
; bp
= nbp
) {
931 nbp
= LIST_NEXT(bp
, b_vnbufs
);
932 error
= bbusy(bp
, catch, slptimeo
, NULL
);
934 if (error
== EPASSTHROUGH
)
936 mutex_exit(&bufcache_lock
);
940 * XXX Since there are no node locks for NFS, I believe
941 * there is a slight chance that a delayed write will
942 * occur while sleeping just above, so check for it.
944 if ((bp
->b_oflags
& BO_DELWRI
) && (flags
& V_SAVE
)) {
946 printf("buffer still DELWRI\n");
948 bp
->b_cflags
|= BC_BUSY
| BC_VFLUSH
;
949 mutex_exit(&bufcache_lock
);
951 mutex_enter(&bufcache_lock
);
954 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
958 if (!LIST_EMPTY(&vp
->v_cleanblkhd
) || !LIST_EMPTY(&vp
->v_dirtyblkhd
))
959 panic("vinvalbuf: flush failed, vp %p", vp
);
962 mutex_exit(&bufcache_lock
);
968 * Destroy any in core blocks past the truncation length.
969 * Called with the underlying vnode locked, which should prevent new dirty
970 * buffers from being queued.
973 vtruncbuf(struct vnode
*vp
, daddr_t lbn
, bool catch, int slptimeo
)
975 struct buf
*bp
, *nbp
;
979 off
= round_page((voff_t
)lbn
<< vp
->v_mount
->mnt_fs_bshift
);
980 mutex_enter(&vp
->v_interlock
);
981 error
= VOP_PUTPAGES(vp
, off
, 0, PGO_FREE
| PGO_SYNCIO
);
986 mutex_enter(&bufcache_lock
);
988 for (bp
= LIST_FIRST(&vp
->v_dirtyblkhd
); bp
; bp
= nbp
) {
989 nbp
= LIST_NEXT(bp
, b_vnbufs
);
990 if (bp
->b_lblkno
< lbn
)
992 error
= bbusy(bp
, catch, slptimeo
, NULL
);
994 if (error
== EPASSTHROUGH
)
996 mutex_exit(&bufcache_lock
);
999 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
1002 for (bp
= LIST_FIRST(&vp
->v_cleanblkhd
); bp
; bp
= nbp
) {
1003 nbp
= LIST_NEXT(bp
, b_vnbufs
);
1004 if (bp
->b_lblkno
< lbn
)
1006 error
= bbusy(bp
, catch, slptimeo
, NULL
);
1008 if (error
== EPASSTHROUGH
)
1010 mutex_exit(&bufcache_lock
);
1013 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
1015 mutex_exit(&bufcache_lock
);
1021 * Flush all dirty buffers from a vnode.
1022 * Called with the underlying vnode locked, which should prevent new dirty
1023 * buffers from being queued.
1026 vflushbuf(struct vnode
*vp
, int sync
)
1028 struct buf
*bp
, *nbp
;
1029 int flags
= PGO_CLEANIT
| PGO_ALLPAGES
| (sync
? PGO_SYNCIO
: 0);
1032 mutex_enter(&vp
->v_interlock
);
1033 (void) VOP_PUTPAGES(vp
, 0, 0, flags
);
1036 mutex_enter(&bufcache_lock
);
1037 for (bp
= LIST_FIRST(&vp
->v_dirtyblkhd
); bp
; bp
= nbp
) {
1038 nbp
= LIST_NEXT(bp
, b_vnbufs
);
1039 if ((bp
->b_cflags
& BC_BUSY
))
1041 if ((bp
->b_oflags
& BO_DELWRI
) == 0)
1042 panic("vflushbuf: not dirty, bp %p", bp
);
1043 bp
->b_cflags
|= BC_BUSY
| BC_VFLUSH
;
1044 mutex_exit(&bufcache_lock
);
1046 * Wait for I/O associated with indirect blocks to complete,
1047 * since there is no way to quickly wait for them below.
1049 if (bp
->b_vp
== vp
|| sync
== 0)
1055 mutex_exit(&bufcache_lock
);
1060 mutex_enter(&vp
->v_interlock
);
1061 while (vp
->v_numoutput
!= 0)
1062 cv_wait(&vp
->v_cv
, &vp
->v_interlock
);
1063 dirty
= !LIST_EMPTY(&vp
->v_dirtyblkhd
);
1064 mutex_exit(&vp
->v_interlock
);
1067 vprint("vflushbuf: dirty", vp
);
1073 * Create a vnode for a block device.
1074 * Used for root filesystem and swap areas.
1075 * Also used for memory file system special devices.
1078 bdevvp(dev_t dev
, vnode_t
**vpp
)
1081 return (getdevvp(dev
, vpp
, VBLK
));
1085 * Create a vnode for a character device.
1086 * Used for kernfs and some console handling.
1089 cdevvp(dev_t dev
, vnode_t
**vpp
)
1092 return (getdevvp(dev
, vpp
, VCHR
));
1096 * Associate a buffer with a vnode. There must already be a hold on
1100 bgetvp(struct vnode
*vp
, struct buf
*bp
)
1103 KASSERT(bp
->b_vp
== NULL
);
1104 KASSERT(bp
->b_objlock
== &buffer_lock
);
1105 KASSERT(mutex_owned(&vp
->v_interlock
));
1106 KASSERT(mutex_owned(&bufcache_lock
));
1107 KASSERT((bp
->b_cflags
& BC_BUSY
) != 0);
1108 KASSERT(!cv_has_waiters(&bp
->b_done
));
1112 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1113 bp
->b_dev
= vp
->v_rdev
;
1118 * Insert onto list for new vnode.
1120 bufinsvn(bp
, &vp
->v_cleanblkhd
);
1121 bp
->b_objlock
= &vp
->v_interlock
;
1125 * Disassociate a buffer from a vnode.
1128 brelvp(struct buf
*bp
)
1130 struct vnode
*vp
= bp
->b_vp
;
1132 KASSERT(vp
!= NULL
);
1133 KASSERT(bp
->b_objlock
== &vp
->v_interlock
);
1134 KASSERT(mutex_owned(&vp
->v_interlock
));
1135 KASSERT(mutex_owned(&bufcache_lock
));
1136 KASSERT((bp
->b_cflags
& BC_BUSY
) != 0);
1137 KASSERT(!cv_has_waiters(&bp
->b_done
));
1140 * Delete from old vnode list, if on one.
1142 if (LIST_NEXT(bp
, b_vnbufs
) != NOLIST
)
1145 if (TAILQ_EMPTY(&vp
->v_uobj
.memq
) && (vp
->v_iflag
& VI_ONWORKLST
) &&
1146 LIST_FIRST(&vp
->v_dirtyblkhd
) == NULL
) {
1147 vp
->v_iflag
&= ~VI_WRMAPDIRTY
;
1148 vn_syncer_remove_from_worklist(vp
);
1151 bp
->b_objlock
= &buffer_lock
;
1157 * Reassign a buffer from one vnode list to another.
1158 * The list reassignment must be within the same vnode.
1159 * Used to assign file specific control information
1160 * (indirect blocks) to the list to which they belong.
1163 reassignbuf(struct buf
*bp
, struct vnode
*vp
)
1165 struct buflists
*listheadp
;
1168 KASSERT(mutex_owned(&bufcache_lock
));
1169 KASSERT(bp
->b_objlock
== &vp
->v_interlock
);
1170 KASSERT(mutex_owned(&vp
->v_interlock
));
1171 KASSERT((bp
->b_cflags
& BC_BUSY
) != 0);
1174 * Delete from old vnode list, if on one.
1176 if (LIST_NEXT(bp
, b_vnbufs
) != NOLIST
)
1180 * If dirty, put on list of dirty buffers;
1181 * otherwise insert onto list of clean buffers.
1183 if ((bp
->b_oflags
& BO_DELWRI
) == 0) {
1184 listheadp
= &vp
->v_cleanblkhd
;
1185 if (TAILQ_EMPTY(&vp
->v_uobj
.memq
) &&
1186 (vp
->v_iflag
& VI_ONWORKLST
) &&
1187 LIST_FIRST(&vp
->v_dirtyblkhd
) == NULL
) {
1188 vp
->v_iflag
&= ~VI_WRMAPDIRTY
;
1189 vn_syncer_remove_from_worklist(vp
);
1192 listheadp
= &vp
->v_dirtyblkhd
;
1193 if ((vp
->v_iflag
& VI_ONWORKLST
) == 0) {
1194 switch (vp
->v_type
) {
1199 if (vp
->v_specmountpoint
!= NULL
) {
1209 (vp
->v_mount
->mnt_flag
& MNT_ASYNC
) == 0)
1210 vn_syncer_add_to_worklist(vp
, delayx
);
1213 bufinsvn(bp
, listheadp
);
1217 * Create a vnode for a device.
1218 * Used by bdevvp (block device) for root file system etc.,
1219 * and by cdevvp (character device) for console and kernfs.
1222 getdevvp(dev_t dev
, vnode_t
**vpp
, enum vtype type
)
1232 error
= getnewvnode(VT_NON
, NULL
, spec_vnodeop_p
, &nvp
);
1239 vp
->v_vflag
|= VV_MPSAFE
;
1240 uvm_vnp_setsize(vp
, 0);
1241 spec_node_init(vp
, dev
);
1247 * Try to gain a reference to a vnode, without acquiring its interlock.
1248 * The caller must hold a lock that will prevent the vnode from being
1249 * recycled or freed.
1252 vtryget(vnode_t
*vp
)
1257 * If the vnode is being freed, don't make life any harder
1258 * for vclean() by adding another reference without waiting.
1259 * This is not strictly necessary, but we'll do it anyway.
1261 if (__predict_false((vp
->v_iflag
& (VI_XLOCK
| VI_FREEING
)) != 0)) {
1264 for (use
= vp
->v_usecount
;; use
= next
) {
1265 if (use
== 0 || __predict_false((use
& VC_XLOCK
) != 0)) {
1266 /* Need interlock held if first reference. */
1269 next
= atomic_cas_uint(&vp
->v_usecount
, use
, use
+ 1);
1270 if (__predict_true(next
== use
)) {
1277 * Grab a particular vnode from the free list, increment its
1278 * reference count and lock it. If the vnode lock bit is set the
1279 * vnode is being eliminated in vgone. In that case, we can not
1280 * grab the vnode, so the process is awakened when the transition is
1281 * completed, and an error returned to indicate that the vnode is no
1282 * longer usable (possibly having been changed to a new file system type).
1285 vget(vnode_t
*vp
, int flags
)
1289 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1291 if ((flags
& LK_INTERLOCK
) == 0)
1292 mutex_enter(&vp
->v_interlock
);
1295 * Before adding a reference, we must remove the vnode
1296 * from its freelist.
1298 if (vp
->v_usecount
== 0) {
1302 atomic_inc_uint(&vp
->v_usecount
);
1306 * If the vnode is in the process of being cleaned out for
1307 * another use, we wait for the cleaning to finish and then
1308 * return failure. Cleaning is determined by checking if
1309 * the VI_XLOCK or VI_FREEING flags are set.
1311 if ((vp
->v_iflag
& (VI_XLOCK
| VI_FREEING
)) != 0) {
1312 if ((flags
& LK_NOWAIT
) != 0) {
1316 vwait(vp
, VI_XLOCK
| VI_FREEING
);
1321 if ((vp
->v_iflag
& VI_INACTNOW
) != 0) {
1323 * if it's being desactived, wait for it to complete.
1324 * Make sure to not return a clean vnode.
1326 if ((flags
& LK_NOWAIT
) != 0) {
1330 vwait(vp
, VI_INACTNOW
);
1331 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
1336 if (flags
& LK_TYPE_MASK
) {
1337 error
= vn_lock(vp
, flags
| LK_INTERLOCK
);
1343 mutex_exit(&vp
->v_interlock
);
1348 * vput(), just unlock and vrele()
1354 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1361 * Try to drop reference on a vnode. Abort if we are releasing the
1362 * last reference. Note: this _must_ succeed if not the last reference.
1365 vtryrele(vnode_t
*vp
)
1369 for (use
= vp
->v_usecount
;; use
= next
) {
1373 KASSERT((use
& VC_MASK
) > 1);
1374 next
= atomic_cas_uint(&vp
->v_usecount
, use
, use
- 1);
1375 if (__predict_true(next
== use
)) {
1382 * Vnode release. If reference count drops to zero, call inactive
1383 * routine and either return to freelist or free to the pool.
1386 vrelel(vnode_t
*vp
, int flags
)
1388 bool recycle
, defer
;
1391 KASSERT(mutex_owned(&vp
->v_interlock
));
1392 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1393 KASSERT(vp
->v_freelisthd
== NULL
);
1395 if (__predict_false(vp
->v_op
== dead_vnodeop_p
&&
1396 (vp
->v_iflag
& (VI_CLEAN
|VI_XLOCK
)) == 0)) {
1397 vpanic(vp
, "dead but not clean");
1401 * If not the last reference, just drop the reference count
1405 vp
->v_iflag
|= VI_INACTREDO
;
1406 mutex_exit(&vp
->v_interlock
);
1409 if (vp
->v_usecount
<= 0 || vp
->v_writecount
!= 0) {
1410 vpanic(vp
, "vrelel: bad ref count");
1413 KASSERT((vp
->v_iflag
& VI_XLOCK
) == 0);
1416 * If not clean, deactivate the vnode, but preserve
1417 * our reference across the call to VOP_INACTIVE().
1420 if ((vp
->v_iflag
& VI_CLEAN
) == 0) {
1422 vp
->v_iflag
|= VI_INACTNOW
;
1425 * XXX This ugly block can be largely eliminated if
1426 * locking is pushed down into the file systems.
1428 if (curlwp
== uvm
.pagedaemon_lwp
) {
1429 /* The pagedaemon can't wait around; defer. */
1431 } else if (curlwp
== vrele_lwp
) {
1433 * We have to try harder. But we can't sleep
1434 * with VI_INACTNOW as vget() may be waiting on it.
1436 vp
->v_iflag
&= ~(VI_INACTREDO
|VI_INACTNOW
);
1437 cv_broadcast(&vp
->v_cv
);
1438 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
|
1442 vpanic(vp
, "vrele: unable to lock %p");
1444 mutex_enter(&vp
->v_interlock
);
1446 * if we did get another reference while
1447 * sleeping, don't try to inactivate it yet.
1449 if (__predict_false(vtryrele(vp
))) {
1451 mutex_exit(&vp
->v_interlock
);
1454 vp
->v_iflag
|= VI_INACTNOW
;
1455 mutex_exit(&vp
->v_interlock
);
1457 } else if ((vp
->v_iflag
& VI_LAYER
) != 0) {
1459 * Acquiring the stack's lock in vclean() even
1460 * for an honest vput/vrele is dangerous because
1461 * our caller may hold other vnode locks; defer.
1465 /* If we can't acquire the lock, then defer. */
1466 vp
->v_iflag
&= ~VI_INACTREDO
;
1467 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
|
1471 mutex_enter(&vp
->v_interlock
);
1479 * Defer reclaim to the kthread; it's not safe to
1480 * clean it here. We donate it our last reference.
1482 KASSERT(mutex_owned(&vp
->v_interlock
));
1483 KASSERT((vp
->v_iflag
& VI_INACTPEND
) == 0);
1484 vp
->v_iflag
&= ~VI_INACTNOW
;
1485 vp
->v_iflag
|= VI_INACTPEND
;
1486 mutex_enter(&vrele_lock
);
1487 TAILQ_INSERT_TAIL(&vrele_list
, vp
, v_freelist
);
1488 if (++vrele_pending
> (desiredvnodes
>> 8))
1489 cv_signal(&vrele_cv
);
1490 mutex_exit(&vrele_lock
);
1491 cv_broadcast(&vp
->v_cv
);
1492 mutex_exit(&vp
->v_interlock
);
1497 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) &&
1498 vp
->v_specnode
!= NULL
&& vp
->v_specnode
->sn_opencnt
!= 0) {
1499 vprint("vrelel: missing VOP_CLOSE()", vp
);
1504 * The vnode can gain another reference while being
1505 * deactivated. If VOP_INACTIVE() indicates that
1506 * the described file has been deleted, then recycle
1507 * the vnode irrespective of additional references.
1508 * Another thread may be waiting to re-use the on-disk
1511 * Note that VOP_INACTIVE() will drop the vnode lock.
1513 VOP_INACTIVE(vp
, &recycle
);
1514 mutex_enter(&vp
->v_interlock
);
1515 vp
->v_iflag
&= ~VI_INACTNOW
;
1516 cv_broadcast(&vp
->v_cv
);
1519 mutex_exit(&vp
->v_interlock
);
1524 * If we grew another reference while
1525 * VOP_INACTIVE() was underway, retry.
1527 if ((vp
->v_iflag
& VI_INACTREDO
) != 0) {
1532 /* Take care of space accounting. */
1533 if (vp
->v_iflag
& VI_EXECMAP
) {
1534 atomic_add_int(&uvmexp
.execpages
,
1535 -vp
->v_uobj
.uo_npages
);
1536 atomic_add_int(&uvmexp
.filepages
,
1537 vp
->v_uobj
.uo_npages
);
1539 vp
->v_iflag
&= ~(VI_TEXT
|VI_EXECMAP
|VI_WRMAP
);
1540 vp
->v_vflag
&= ~VV_MAPPED
;
1543 * Recycle the vnode if the file is now unused (unlinked),
1544 * otherwise just free it.
1547 vclean(vp
, DOCLOSE
);
1549 KASSERT(vp
->v_usecount
> 0);
1552 if (atomic_dec_uint_nv(&vp
->v_usecount
) != 0) {
1553 /* Gained another reference while being reclaimed. */
1554 mutex_exit(&vp
->v_interlock
);
1558 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
1560 * It's clean so destroy it. It isn't referenced
1561 * anywhere since it has been reclaimed.
1563 KASSERT(vp
->v_holdcnt
== 0);
1564 KASSERT(vp
->v_writecount
== 0);
1565 mutex_exit(&vp
->v_interlock
);
1566 insmntque(vp
, NULL
);
1567 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
1568 spec_node_destroy(vp
);
1573 * Otherwise, put it back onto the freelist. It
1574 * can't be destroyed while still associated with
1577 mutex_enter(&vnode_free_list_lock
);
1578 if (vp
->v_holdcnt
> 0) {
1579 vp
->v_freelisthd
= &vnode_hold_list
;
1581 vp
->v_freelisthd
= &vnode_free_list
;
1583 TAILQ_INSERT_TAIL(vp
->v_freelisthd
, vp
, v_freelist
);
1584 mutex_exit(&vnode_free_list_lock
);
1585 mutex_exit(&vp
->v_interlock
);
1593 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1595 if ((vp
->v_iflag
& VI_INACTNOW
) == 0 && vtryrele(vp
)) {
1598 mutex_enter(&vp
->v_interlock
);
1603 vrele_thread(void *cookie
)
1608 mutex_enter(&vrele_lock
);
1609 while (TAILQ_EMPTY(&vrele_list
)) {
1611 cv_broadcast(&vrele_cv
);
1612 cv_timedwait(&vrele_cv
, &vrele_lock
, hz
);
1614 vp
= TAILQ_FIRST(&vrele_list
);
1615 TAILQ_REMOVE(&vrele_list
, vp
, v_freelist
);
1617 mutex_exit(&vrele_lock
);
1620 * If not the last reference, then ignore the vnode
1621 * and look for more work.
1623 mutex_enter(&vp
->v_interlock
);
1624 KASSERT((vp
->v_iflag
& VI_INACTPEND
) != 0);
1625 vp
->v_iflag
&= ~VI_INACTPEND
;
1631 * Page or buffer structure gets a reference.
1632 * Called with v_interlock held.
1638 KASSERT(mutex_owned(&vp
->v_interlock
));
1639 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1641 if (vp
->v_holdcnt
++ == 0 && vp
->v_usecount
== 0) {
1642 mutex_enter(&vnode_free_list_lock
);
1643 KASSERT(vp
->v_freelisthd
== &vnode_free_list
);
1644 TAILQ_REMOVE(vp
->v_freelisthd
, vp
, v_freelist
);
1645 vp
->v_freelisthd
= &vnode_hold_list
;
1646 TAILQ_INSERT_TAIL(vp
->v_freelisthd
, vp
, v_freelist
);
1647 mutex_exit(&vnode_free_list_lock
);
1652 * Page or buffer structure frees a reference.
1653 * Called with v_interlock held.
1656 holdrelel(vnode_t
*vp
)
1659 KASSERT(mutex_owned(&vp
->v_interlock
));
1660 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1662 if (vp
->v_holdcnt
<= 0) {
1663 vpanic(vp
, "holdrelel: holdcnt vp %p");
1667 if (vp
->v_holdcnt
== 0 && vp
->v_usecount
== 0) {
1668 mutex_enter(&vnode_free_list_lock
);
1669 KASSERT(vp
->v_freelisthd
== &vnode_hold_list
);
1670 TAILQ_REMOVE(vp
->v_freelisthd
, vp
, v_freelist
);
1671 vp
->v_freelisthd
= &vnode_free_list
;
1672 TAILQ_INSERT_TAIL(vp
->v_freelisthd
, vp
, v_freelist
);
1673 mutex_exit(&vnode_free_list_lock
);
1678 * Vnode reference, where a reference is already held by some other
1679 * object (for example, a file structure).
1685 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1686 KASSERT(vp
->v_usecount
!= 0);
1688 atomic_inc_uint(&vp
->v_usecount
);
1692 * Remove any vnodes in the vnode table belonging to mount point mp.
1694 * If FORCECLOSE is not specified, there should not be any active ones,
1695 * return error if any are found (nb: this is a user error, not a
1696 * system error). If FORCECLOSE is specified, detach any active vnodes
1699 * If WRITECLOSE is set, only flush out regular file vnodes open for
1702 * SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped.
1705 int busyprt
= 0; /* print out busy vnodes */
1706 struct ctldebug debug1
= { "busyprt", &busyprt
};
1710 vflushnext(vnode_t
*mvp
, int *when
)
1713 if (hardclock_ticks
> *when
) {
1714 mutex_exit(&mntvnode_lock
);
1716 mutex_enter(&mntvnode_lock
);
1717 *when
= hardclock_ticks
+ hz
/ 10;
1720 return vunmark(mvp
);
1724 vflush(struct mount
*mp
, vnode_t
*skipvp
, int flags
)
1727 int busy
= 0, when
= 0, gen
;
1730 * First, flush out any vnode references from vrele_list.
1732 mutex_enter(&vrele_lock
);
1734 while (vrele_pending
&& gen
== vrele_gen
) {
1735 cv_broadcast(&vrele_cv
);
1736 cv_wait(&vrele_cv
, &vrele_lock
);
1738 mutex_exit(&vrele_lock
);
1740 /* Allocate a marker vnode. */
1741 if ((mvp
= vnalloc(mp
)) == NULL
)
1745 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1746 * and vclean() are called
1748 mutex_enter(&mntvnode_lock
);
1749 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
!= NULL
;
1750 vp
= vflushnext(mvp
, &when
)) {
1752 if (vp
->v_mount
!= mp
|| vismarker(vp
))
1755 * Skip over a selected vnode.
1759 mutex_enter(&vp
->v_interlock
);
1761 * Ignore clean but still referenced vnodes.
1763 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
1764 mutex_exit(&vp
->v_interlock
);
1768 * Skip over a vnodes marked VSYSTEM.
1770 if ((flags
& SKIPSYSTEM
) && (vp
->v_vflag
& VV_SYSTEM
)) {
1771 mutex_exit(&vp
->v_interlock
);
1775 * If WRITECLOSE is set, only flush out regular file
1776 * vnodes open for writing.
1778 if ((flags
& WRITECLOSE
) &&
1779 (vp
->v_writecount
== 0 || vp
->v_type
!= VREG
)) {
1780 mutex_exit(&vp
->v_interlock
);
1784 * With v_usecount == 0, all we need to do is clear
1785 * out the vnode data structures and we are done.
1787 if (vp
->v_usecount
== 0) {
1788 mutex_exit(&mntvnode_lock
);
1791 vclean(vp
, DOCLOSE
);
1793 mutex_enter(&mntvnode_lock
);
1797 * If FORCECLOSE is set, forcibly close the vnode.
1798 * For block or character devices, revert to an
1799 * anonymous device. For all other files, just
1802 if (flags
& FORCECLOSE
) {
1803 mutex_exit(&mntvnode_lock
);
1804 atomic_inc_uint(&vp
->v_usecount
);
1805 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
1806 vclean(vp
, DOCLOSE
);
1810 vp
->v_op
= spec_vnodeop_p
; /* XXXSMP */
1811 mutex_exit(&vp
->v_interlock
);
1813 * The vnode isn't clean, but still resides
1814 * on the mount list. Remove it. XXX This
1817 insmntque(vp
, NULL
);
1820 mutex_enter(&mntvnode_lock
);
1825 vprint("vflush: busy vnode", vp
);
1827 mutex_exit(&vp
->v_interlock
);
1830 mutex_exit(&mntvnode_lock
);
1838 * Disassociate the underlying file system from a vnode.
1840 * Must be called with the interlock held, and will return with it held.
1843 vclean(vnode_t
*vp
, int flags
)
1846 bool recycle
, active
;
1849 KASSERT(mutex_owned(&vp
->v_interlock
));
1850 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1851 KASSERT(vp
->v_usecount
!= 0);
1853 /* If cleaning is already in progress wait until done and return. */
1854 if (vp
->v_iflag
& VI_XLOCK
) {
1855 vwait(vp
, VI_XLOCK
);
1859 /* If already clean, nothing to do. */
1860 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
1865 * Prevent the vnode from being recycled or brought into use
1866 * while we clean it out.
1868 vp
->v_iflag
|= VI_XLOCK
;
1869 if (vp
->v_iflag
& VI_EXECMAP
) {
1870 atomic_add_int(&uvmexp
.execpages
, -vp
->v_uobj
.uo_npages
);
1871 atomic_add_int(&uvmexp
.filepages
, vp
->v_uobj
.uo_npages
);
1873 vp
->v_iflag
&= ~(VI_TEXT
|VI_EXECMAP
);
1874 active
= (vp
->v_usecount
> 1);
1876 /* XXXAD should not lock vnode under layer */
1877 VOP_LOCK(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
);
1880 * Clean out any cached data associated with the vnode.
1881 * If purging an active vnode, it must be closed and
1882 * deactivated before being reclaimed. Note that the
1883 * VOP_INACTIVE will unlock the vnode.
1885 if (flags
& DOCLOSE
) {
1886 error
= vinvalbuf(vp
, V_SAVE
, NOCRED
, l
, 0, 0);
1888 /* XXX, fix vn_start_write's grab of mp and use that. */
1890 if (wapbl_vphaswapbl(vp
))
1891 WAPBL_DISCARD(wapbl_vptomp(vp
));
1892 error
= vinvalbuf(vp
, 0, NOCRED
, l
, 0, 0);
1894 KASSERT(error
== 0);
1895 KASSERT((vp
->v_iflag
& VI_ONWORKLST
) == 0);
1896 if (active
&& (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)) {
1897 spec_node_revoke(vp
);
1901 VOP_INACTIVE(vp
, &recycle
);
1904 * Any other processes trying to obtain this lock must first
1905 * wait for VI_XLOCK to clear, then call the new lock operation.
1910 /* Disassociate the underlying file system from the vnode. */
1911 if (VOP_RECLAIM(vp
)) {
1912 vpanic(vp
, "vclean: cannot reclaim");
1915 KASSERT(vp
->v_uobj
.uo_npages
== 0);
1916 if (vp
->v_type
== VREG
&& vp
->v_ractx
!= NULL
) {
1917 uvm_ra_freectx(vp
->v_ractx
);
1922 /* Done with purge, notify sleepers of the grim news. */
1923 mutex_enter(&vp
->v_interlock
);
1924 vp
->v_op
= dead_vnodeop_p
;
1926 vp
->v_vnlock
= &vp
->v_lock
;
1927 KNOTE(&vp
->v_klist
, NOTE_REVOKE
);
1928 vp
->v_iflag
&= ~(VI_XLOCK
| VI_FREEING
);
1929 vp
->v_vflag
&= ~VV_LOCKSWORK
;
1930 if ((flags
& DOCLOSE
) != 0) {
1931 vp
->v_iflag
|= VI_CLEAN
;
1933 cv_broadcast(&vp
->v_cv
);
1935 KASSERT((vp
->v_iflag
& VI_ONWORKLST
) == 0);
1939 * Recycle an unused vnode to the front of the free list.
1940 * Release the passed interlock if the vnode will be recycled.
1943 vrecycle(vnode_t
*vp
, kmutex_t
*inter_lkp
, struct lwp
*l
)
1946 KASSERT((vp
->v_iflag
& VI_MARKER
) == 0);
1948 mutex_enter(&vp
->v_interlock
);
1949 if (vp
->v_usecount
!= 0) {
1950 mutex_exit(&vp
->v_interlock
);
1954 mutex_exit(inter_lkp
);
1957 vclean(vp
, DOCLOSE
);
1963 * Eliminate all activity associated with a vnode in preparation for
1964 * reuse. Drops a reference from the vnode.
1970 mutex_enter(&vp
->v_interlock
);
1971 vclean(vp
, DOCLOSE
);
1976 * Lookup a vnode by device number.
1979 vfinddev(dev_t dev
, enum vtype type
, vnode_t
**vpp
)
1984 mutex_enter(&device_lock
);
1985 for (vp
= specfs_hash
[SPECHASH(dev
)]; vp
; vp
= vp
->v_specnext
) {
1986 if (dev
!= vp
->v_rdev
|| type
!= vp
->v_type
)
1992 mutex_exit(&device_lock
);
1997 * Revoke all the vnodes corresponding to the specified minor number
1998 * range (endpoints inclusive) of the specified major.
2001 vdevgone(int maj
, int minl
, int minh
, enum vtype type
)
2007 vp
= NULL
; /* XXX gcc */
2009 mutex_enter(&device_lock
);
2010 for (mn
= minl
; mn
<= minh
; mn
++) {
2011 dev
= makedev(maj
, mn
);
2012 vpp
= &specfs_hash
[SPECHASH(dev
)];
2013 for (vp
= *vpp
; vp
!= NULL
;) {
2014 mutex_enter(&vp
->v_interlock
);
2015 if ((vp
->v_iflag
& VI_CLEAN
) != 0 ||
2016 dev
!= vp
->v_rdev
|| type
!= vp
->v_type
) {
2017 mutex_exit(&vp
->v_interlock
);
2018 vp
= vp
->v_specnext
;
2021 mutex_exit(&device_lock
);
2022 if (vget(vp
, LK_INTERLOCK
) == 0) {
2023 VOP_REVOKE(vp
, REVOKEALL
);
2026 mutex_enter(&device_lock
);
2030 mutex_exit(&device_lock
);
2034 * Eliminate all activity associated with the requested vnode
2035 * and with all vnodes aliased to the requested vnode.
2038 vrevoke(vnode_t
*vp
)
2044 KASSERT(vp
->v_usecount
> 0);
2046 mutex_enter(&vp
->v_interlock
);
2047 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
2048 mutex_exit(&vp
->v_interlock
);
2050 } else if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
) {
2051 atomic_inc_uint(&vp
->v_usecount
);
2052 vclean(vp
, DOCLOSE
);
2058 mutex_exit(&vp
->v_interlock
);
2061 vpp
= &specfs_hash
[SPECHASH(dev
)];
2062 mutex_enter(&device_lock
);
2063 for (vq
= *vpp
; vq
!= NULL
;) {
2064 /* If clean or being cleaned, then ignore it. */
2065 mutex_enter(&vq
->v_interlock
);
2066 if ((vq
->v_iflag
& (VI_CLEAN
| VI_XLOCK
)) != 0 ||
2067 vq
->v_rdev
!= dev
|| vq
->v_type
!= type
) {
2068 mutex_exit(&vq
->v_interlock
);
2069 vq
= vq
->v_specnext
;
2072 mutex_exit(&device_lock
);
2073 if (vq
->v_usecount
== 0) {
2077 atomic_inc_uint(&vq
->v_usecount
);
2079 vclean(vq
, DOCLOSE
);
2081 mutex_enter(&device_lock
);
2084 mutex_exit(&device_lock
);
2088 * sysctl helper routine to return list of supported fstypes
2091 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS
)
2093 char bf
[sizeof(((struct statvfs
*)NULL
)->f_fstypename
)];
2096 size_t needed
, left
, slen
;
2110 mutex_enter(&vfs_list_lock
);
2111 LIST_FOREACH(v
, &vfs_list
, vfs_list
) {
2113 needed
+= strlen(v
->vfs_name
) + 1;
2115 memset(bf
, 0, sizeof(bf
));
2117 strncpy(bf
, v
->vfs_name
, sizeof(bf
));
2121 strncpy(bf
+ 1, v
->vfs_name
, sizeof(bf
) - 1);
2123 bf
[sizeof(bf
)-1] = '\0';
2125 if (left
< slen
+ 1)
2128 mutex_exit(&vfs_list_lock
);
2129 /* +1 to copy out the trailing NUL byte */
2130 error
= copyout(bf
, where
, slen
+ 1);
2131 mutex_enter(&vfs_list_lock
);
2140 mutex_exit(&vfs_list_lock
);
2147 int kinfo_vdebug
= 1;
2148 int kinfo_vgetfailed
;
2149 #define KINFO_VNODESLOP 10
2151 * Dump vnode list (via sysctl).
2152 * Copyout address of vnode followed by vnode.
2156 sysctl_kern_vnode(SYSCTLFN_ARGS
)
2159 size_t *sizep
= oldlenp
;
2160 struct mount
*mp
, *nmp
;
2161 vnode_t
*vp
, *mvp
, vbuf
;
2167 return (EOPNOTSUPP
);
2171 #define VPTRSZ sizeof(vnode_t *)
2172 #define VNODESZ sizeof(vnode_t)
2173 if (where
== NULL
) {
2174 *sizep
= (numvnodes
+ KINFO_VNODESLOP
) * (VPTRSZ
+ VNODESZ
);
2177 ewhere
= where
+ *sizep
;
2180 mutex_enter(&mountlist_lock
);
2181 for (mp
= CIRCLEQ_FIRST(&mountlist
); mp
!= (void *)&mountlist
;
2183 if (vfs_busy(mp
, &nmp
)) {
2186 /* Allocate a marker vnode. */
2188 /* Should never fail for mp != NULL */
2189 KASSERT(mvp
!= NULL
);
2190 mutex_enter(&mntvnode_lock
);
2191 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
;
2192 vp
= vunmark(mvp
)) {
2195 * Check that the vp is still associated with
2196 * this filesystem. RACE: could have been
2197 * recycled onto the same filesystem.
2199 if (vp
->v_mount
!= mp
|| vismarker(vp
))
2201 if (bp
+ VPTRSZ
+ VNODESZ
> ewhere
) {
2203 mutex_exit(&mntvnode_lock
);
2205 vfs_unbusy(mp
, false, NULL
);
2207 *sizep
= bp
- where
;
2210 memcpy(&vbuf
, vp
, VNODESZ
);
2211 mutex_exit(&mntvnode_lock
);
2212 if ((error
= copyout(&vp
, bp
, VPTRSZ
)) ||
2213 (error
= copyout(&vbuf
, bp
+ VPTRSZ
, VNODESZ
))) {
2214 mutex_enter(&mntvnode_lock
);
2216 mutex_exit(&mntvnode_lock
);
2218 vfs_unbusy(mp
, false, NULL
);
2222 bp
+= VPTRSZ
+ VNODESZ
;
2223 mutex_enter(&mntvnode_lock
);
2225 mutex_exit(&mntvnode_lock
);
2227 vfs_unbusy(mp
, false, &nmp
);
2229 mutex_exit(&mountlist_lock
);
2232 *sizep
= bp
- where
;
2237 * Remove clean vnodes from a mountpoint's vnode list.
2240 vfs_scrubvnlist(struct mount
*mp
)
2245 mutex_enter(&mntvnode_lock
);
2246 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= nvp
) {
2247 nvp
= TAILQ_NEXT(vp
, v_mntvnodes
);
2248 mutex_enter(&vp
->v_interlock
);
2249 if ((vp
->v_iflag
& VI_CLEAN
) != 0) {
2250 TAILQ_REMOVE(&mp
->mnt_vnodelist
, vp
, v_mntvnodes
);
2252 mutex_exit(&mntvnode_lock
);
2253 mutex_exit(&vp
->v_interlock
);
2257 mutex_exit(&vp
->v_interlock
);
2259 mutex_exit(&mntvnode_lock
);
2263 * Check to see if a filesystem is mounted on a block device.
2266 vfs_mountedon(vnode_t
*vp
)
2271 if (vp
->v_type
!= VBLK
)
2273 if (vp
->v_specmountpoint
!= NULL
)
2275 mutex_enter(&device_lock
);
2276 for (vq
= specfs_hash
[SPECHASH(vp
->v_rdev
)]; vq
!= NULL
;
2277 vq
= vq
->v_specnext
) {
2278 if (vq
->v_rdev
!= vp
->v_rdev
|| vq
->v_type
!= vp
->v_type
)
2280 if (vq
->v_specmountpoint
!= NULL
) {
2285 mutex_exit(&device_lock
);
2290 * Unmount all file systems.
2291 * We traverse the list in reverse order under the assumption that doing so
2292 * will avoid needing to worry about dependencies.
2295 vfs_unmountall(struct lwp
*l
)
2297 printf("unmounting file systems...");
2298 return vfs_unmountall1(l
, true, true);
2302 vfs_unmount_print(struct mount
*mp
, const char *pfx
)
2304 printf("%sunmounted %s on %s type %s\n", pfx
,
2305 mp
->mnt_stat
.f_mntfromname
, mp
->mnt_stat
.f_mntonname
,
2306 mp
->mnt_stat
.f_fstypename
);
2310 vfs_unmount_forceone(struct lwp
*l
)
2312 struct mount
*mp
, *nmp
= NULL
;
2315 CIRCLEQ_FOREACH_REVERSE(mp
, &mountlist
, mnt_list
) {
2316 if (nmp
== NULL
|| mp
->mnt_gen
> nmp
->mnt_gen
)
2324 printf("\nforcefully unmounting %s (%s)...",
2325 nmp
->mnt_stat
.f_mntonname
, nmp
->mnt_stat
.f_mntfromname
);
2327 atomic_inc_uint(&nmp
->mnt_refcnt
);
2328 if ((error
= dounmount(nmp
, MNT_FORCE
, l
)) == 0) {
2329 vfs_unmount_print(nmp
, "forcefully ");
2332 atomic_dec_uint(&nmp
->mnt_refcnt
);
2335 printf("forceful unmount of %s failed with error %d\n",
2336 nmp
->mnt_stat
.f_mntonname
, error
);
2343 vfs_unmountall1(struct lwp
*l
, bool force
, bool verbose
)
2345 struct mount
*mp
, *nmp
;
2346 bool any_error
= false, progress
= false;
2349 for (mp
= CIRCLEQ_LAST(&mountlist
);
2350 mp
!= (void *)&mountlist
;
2352 nmp
= CIRCLEQ_PREV(mp
, mnt_list
);
2354 printf("\nunmounting %p %s (%s)...",
2355 (void *)mp
, mp
->mnt_stat
.f_mntonname
,
2356 mp
->mnt_stat
.f_mntfromname
);
2358 atomic_inc_uint(&mp
->mnt_refcnt
);
2359 if ((error
= dounmount(mp
, force
? MNT_FORCE
: 0, l
)) == 0) {
2360 vfs_unmount_print(mp
, "");
2363 atomic_dec_uint(&mp
->mnt_refcnt
);
2365 printf("unmount of %s failed with error %d\n",
2366 mp
->mnt_stat
.f_mntonname
, error
);
2373 if (any_error
&& verbose
)
2374 printf("WARNING: some file systems would not unmount\n");
2379 * Sync and unmount file systems before shutting down.
2386 /* XXX we're certainly not running in lwp0's context! */
2387 l
= (curlwp
== NULL
) ? &lwp0
: curlwp
;
2393 vfs_sync_all(struct lwp
*l
)
2395 printf("syncing disks... ");
2397 /* remove user processes from run queue */
2401 /* avoid coming back this way again if we panic. */
2404 sys_sync(l
, NULL
, NULL
);
2406 /* Wait for sync to finish. */
2407 if (buf_syncwait() != 0) {
2408 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
2411 printf("giving up\n");
2418 vfs_shutdown1(struct lwp
*l
)
2424 * If we've panic'd, don't make the situation potentially
2425 * worse by unmounting the file systems.
2427 if (panicstr
!= NULL
)
2430 /* Release inodes held by texts before update. */
2434 /* Unmount file systems. */
2439 * Print a list of supported file system types (used by vfs_mountroot)
2442 vfs_print_fstypes(void)
2447 mutex_enter(&vfs_list_lock
);
2448 LIST_FOREACH(v
, &vfs_list
, vfs_list
)
2450 mutex_exit(&vfs_list_lock
);
2453 printf("WARNING: No file system modules have been loaded.\n");
2457 printf("Supported file systems:");
2458 mutex_enter(&vfs_list_lock
);
2459 LIST_FOREACH(v
, &vfs_list
, vfs_list
) {
2460 printf(" %s", v
->vfs_name
);
2462 mutex_exit(&vfs_list_lock
);
2467 * Mount the root file system. If the operator didn't specify a
2468 * file system to use, try all possible file systems until one
2477 if (root_device
== NULL
)
2478 panic("vfs_mountroot: root device unknown");
2480 switch (device_class(root_device
)) {
2482 if (rootdev
!= NODEV
)
2483 panic("vfs_mountroot: rootdev set for DV_IFNET "
2484 "(0x%llx -> %llu,%llu)",
2485 (unsigned long long)rootdev
,
2486 (unsigned long long)major(rootdev
),
2487 (unsigned long long)minor(rootdev
));
2491 if (rootdev
== NODEV
)
2492 panic("vfs_mountroot: rootdev not set for DV_DISK");
2493 if (bdevvp(rootdev
, &rootvp
))
2494 panic("vfs_mountroot: can't get vnode for rootdev");
2495 error
= VOP_OPEN(rootvp
, FREAD
, FSCRED
);
2497 printf("vfs_mountroot: can't open root device\n");
2506 printf("%s: inappropriate for root file system\n",
2507 device_xname(root_device
));
2512 * If user specified a root fs type, use it. Make sure the
2513 * specified type exists and has a mount_root()
2515 if (strcmp(rootfstype
, ROOT_FSTYPE_ANY
) != 0) {
2516 v
= vfs_getopsbyname(rootfstype
);
2519 if (v
->vfs_mountroot
!= NULL
) {
2520 error
= (v
->vfs_mountroot
)();
2528 * Try each file system currently configured into the kernel.
2530 mutex_enter(&vfs_list_lock
);
2531 LIST_FOREACH(v
, &vfs_list
, vfs_list
) {
2532 if (v
->vfs_mountroot
== NULL
)
2535 aprint_normal("mountroot: trying %s...\n", v
->vfs_name
);
2538 mutex_exit(&vfs_list_lock
);
2539 error
= (*v
->vfs_mountroot
)();
2540 mutex_enter(&vfs_list_lock
);
2543 aprint_normal("root file system type: %s\n",
2548 mutex_exit(&vfs_list_lock
);
2551 vfs_print_fstypes();
2552 printf("no file system for %s", device_xname(root_device
));
2553 if (device_class(root_device
) == DV_DISK
)
2554 printf(" (dev 0x%llx)", (unsigned long long)rootdev
);
2560 if (error
&& device_class(root_device
) == DV_DISK
) {
2561 VOP_CLOSE(rootvp
, FREAD
, FSCRED
);
2565 extern struct cwdinfo cwdi0
;
2567 CIRCLEQ_FIRST(&mountlist
)->mnt_flag
|= MNT_ROOTFS
;
2568 CIRCLEQ_FIRST(&mountlist
)->mnt_op
->vfs_refcount
++;
2571 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to
2574 error
= VFS_ROOT(CIRCLEQ_FIRST(&mountlist
), &rootvnode
);
2576 panic("cannot find root vnode, error=%d", error
);
2577 cwdi0
.cwdi_cdir
= rootvnode
;
2578 vref(cwdi0
.cwdi_cdir
);
2579 VOP_UNLOCK(rootvnode
, 0);
2580 cwdi0
.cwdi_rdir
= NULL
;
2583 * Now that root is mounted, we can fixup initproc's CWD
2584 * info. All other processes are kthreads, which merely
2585 * share proc0's CWD info.
2587 initproc
->p_cwdi
->cwdi_cdir
= rootvnode
;
2588 vref(initproc
->p_cwdi
->cwdi_cdir
);
2589 initproc
->p_cwdi
->cwdi_rdir
= NULL
;
2595 * Get a new unique fsid
2598 vfs_getnewfsid(struct mount
*mp
)
2600 static u_short xxxfs_mntid
;
2604 mutex_enter(&mntid_lock
);
2605 mtype
= makefstype(mp
->mnt_op
->vfs_name
);
2606 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] = makedev(mtype
, 0);
2607 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1] = mtype
;
2608 mp
->mnt_stat
.f_fsid
= mp
->mnt_stat
.f_fsidx
.__fsid_val
[0];
2609 if (xxxfs_mntid
== 0)
2611 tfsid
.__fsid_val
[0] = makedev(mtype
& 0xff, xxxfs_mntid
);
2612 tfsid
.__fsid_val
[1] = mtype
;
2613 if (!CIRCLEQ_EMPTY(&mountlist
)) {
2614 while (vfs_getvfs(&tfsid
)) {
2615 tfsid
.__fsid_val
[0]++;
2619 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] = tfsid
.__fsid_val
[0];
2620 mp
->mnt_stat
.f_fsid
= mp
->mnt_stat
.f_fsidx
.__fsid_val
[0];
2621 mutex_exit(&mntid_lock
);
2625 * Make a 'unique' number from a mount type name.
2628 makefstype(const char *type
)
2632 for (rv
= 0; *type
; type
++) {
2640 * Set vnode attributes to VNOVAL
2643 vattr_null(struct vattr
*vap
)
2646 memset(vap
, 0, sizeof(*vap
));
2648 vap
->va_type
= VNON
;
2651 * Assign individually so that it is safe even if size and
2652 * sign of each member are varied.
2654 vap
->va_mode
= VNOVAL
;
2655 vap
->va_nlink
= VNOVAL
;
2656 vap
->va_uid
= VNOVAL
;
2657 vap
->va_gid
= VNOVAL
;
2658 vap
->va_fsid
= VNOVAL
;
2659 vap
->va_fileid
= VNOVAL
;
2660 vap
->va_size
= VNOVAL
;
2661 vap
->va_blocksize
= VNOVAL
;
2662 vap
->va_atime
.tv_sec
=
2663 vap
->va_mtime
.tv_sec
=
2664 vap
->va_ctime
.tv_sec
=
2665 vap
->va_birthtime
.tv_sec
= VNOVAL
;
2666 vap
->va_atime
.tv_nsec
=
2667 vap
->va_mtime
.tv_nsec
=
2668 vap
->va_ctime
.tv_nsec
=
2669 vap
->va_birthtime
.tv_nsec
= VNOVAL
;
2670 vap
->va_gen
= VNOVAL
;
2671 vap
->va_flags
= VNOVAL
;
2672 vap
->va_rdev
= VNOVAL
;
2673 vap
->va_bytes
= VNOVAL
;
2676 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
2677 #define ARRAY_PRINT(idx, arr) \
2678 ((unsigned int)(idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN")
2680 const char * const vnode_tags
[] = { VNODE_TAGS
};
2681 const char * const vnode_types
[] = { VNODE_TYPES
};
2682 const char vnode_flagbits
[] = VNODE_FLAGBITS
;
2685 * Print out a description of a vnode.
2688 vprint(const char *label
, struct vnode
*vp
)
2694 vl
= (vp
->v_vnlock
!= NULL
? vp
->v_vnlock
: &vp
->v_lock
);
2695 flag
= vp
->v_iflag
| vp
->v_vflag
| vp
->v_uflag
;
2696 snprintb(bf
, sizeof(bf
), vnode_flagbits
, flag
);
2699 printf("%s: ", label
);
2700 printf("vnode @ %p, flags (%s)\n\ttag %s(%d), type %s(%d), "
2701 "usecount %d, writecount %d, holdcount %d\n"
2702 "\tfreelisthd %p, mount %p, data %p lock %p recursecnt %d\n",
2703 vp
, bf
, ARRAY_PRINT(vp
->v_tag
, vnode_tags
), vp
->v_tag
,
2704 ARRAY_PRINT(vp
->v_type
, vnode_types
), vp
->v_type
,
2705 vp
->v_usecount
, vp
->v_writecount
, vp
->v_holdcnt
,
2706 vp
->v_freelisthd
, vp
->v_mount
, vp
->v_data
, vl
, vl
->vl_recursecnt
);
2707 if (vp
->v_data
!= NULL
) {
2715 * List all of the locked vnodes in the system.
2716 * Called when debugging the kernel.
2719 printlockedvnodes(void)
2721 struct mount
*mp
, *nmp
;
2724 printf("Locked vnodes\n");
2725 mutex_enter(&mountlist_lock
);
2726 for (mp
= CIRCLEQ_FIRST(&mountlist
); mp
!= (void *)&mountlist
;
2728 if (vfs_busy(mp
, &nmp
)) {
2731 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
2732 if (VOP_ISLOCKED(vp
))
2735 mutex_enter(&mountlist_lock
);
2736 vfs_unbusy(mp
, false, &nmp
);
2738 mutex_exit(&mountlist_lock
);
2742 /* Deprecated. Kept for KPI compatibility. */
2744 vaccess(enum vtype type
, mode_t file_mode
, uid_t uid
, gid_t gid
,
2745 mode_t acc_mode
, kauth_cred_t cred
)
2749 printf("vaccess: deprecated interface used.\n");
2750 #endif /* DIAGNOSTIC */
2752 return genfs_can_access(type
, file_mode
, uid
, gid
, acc_mode
, cred
);
2756 * Given a file system name, look up the vfsops for that
2757 * file system, or return NULL if file system isn't present
2761 vfs_getopsbyname(const char *name
)
2765 mutex_enter(&vfs_list_lock
);
2766 LIST_FOREACH(v
, &vfs_list
, vfs_list
) {
2767 if (strcmp(v
->vfs_name
, name
) == 0)
2772 mutex_exit(&vfs_list_lock
);
2778 copy_statvfs_info(struct statvfs
*sbp
, const struct mount
*mp
)
2780 const struct statvfs
*mbp
;
2782 if (sbp
== (mbp
= &mp
->mnt_stat
))
2785 (void)memcpy(&sbp
->f_fsidx
, &mbp
->f_fsidx
, sizeof(sbp
->f_fsidx
));
2786 sbp
->f_fsid
= mbp
->f_fsid
;
2787 sbp
->f_owner
= mbp
->f_owner
;
2788 sbp
->f_flag
= mbp
->f_flag
;
2789 sbp
->f_syncwrites
= mbp
->f_syncwrites
;
2790 sbp
->f_asyncwrites
= mbp
->f_asyncwrites
;
2791 sbp
->f_syncreads
= mbp
->f_syncreads
;
2792 sbp
->f_asyncreads
= mbp
->f_asyncreads
;
2793 (void)memcpy(sbp
->f_spare
, mbp
->f_spare
, sizeof(mbp
->f_spare
));
2794 (void)memcpy(sbp
->f_fstypename
, mbp
->f_fstypename
,
2795 sizeof(sbp
->f_fstypename
));
2796 (void)memcpy(sbp
->f_mntonname
, mbp
->f_mntonname
,
2797 sizeof(sbp
->f_mntonname
));
2798 (void)memcpy(sbp
->f_mntfromname
, mp
->mnt_stat
.f_mntfromname
,
2799 sizeof(sbp
->f_mntfromname
));
2800 sbp
->f_namemax
= mbp
->f_namemax
;
2804 set_statvfs_info(const char *onp
, int ukon
, const char *fromp
, int ukfrom
,
2805 const char *vfsname
, struct mount
*mp
, struct lwp
*l
)
2809 struct statvfs
*sfs
= &mp
->mnt_stat
;
2810 int (*fun
)(const void *, void *, size_t, size_t *);
2812 (void)strlcpy(mp
->mnt_stat
.f_fstypename
, vfsname
,
2813 sizeof(mp
->mnt_stat
.f_fstypename
));
2816 struct cwdinfo
*cwdi
= l
->l_proc
->p_cwdi
;
2817 fun
= (ukon
== UIO_SYSSPACE
) ? copystr
: copyinstr
;
2818 if (cwdi
->cwdi_rdir
!= NULL
) {
2821 char *path
= PNBUF_GET();
2823 bp
= path
+ MAXPATHLEN
;
2825 rw_enter(&cwdi
->cwdi_lock
, RW_READER
);
2826 error
= getcwd_common(cwdi
->cwdi_rdir
, rootvnode
, &bp
,
2827 path
, MAXPATHLEN
/ 2, 0, l
);
2828 rw_exit(&cwdi
->cwdi_lock
);
2835 if (len
> sizeof(sfs
->f_mntonname
) - 1)
2836 len
= sizeof(sfs
->f_mntonname
) - 1;
2837 (void)strncpy(sfs
->f_mntonname
, bp
, len
);
2840 if (len
< sizeof(sfs
->f_mntonname
) - 1) {
2841 error
= (*fun
)(onp
, &sfs
->f_mntonname
[len
],
2842 sizeof(sfs
->f_mntonname
) - len
- 1, &size
);
2850 error
= (*fun
)(onp
, &sfs
->f_mntonname
,
2851 sizeof(sfs
->f_mntonname
) - 1, &size
);
2855 (void)memset(sfs
->f_mntonname
+ size
, 0,
2856 sizeof(sfs
->f_mntonname
) - size
);
2860 fun
= (ukfrom
== UIO_SYSSPACE
) ? copystr
: copyinstr
;
2861 error
= (*fun
)(fromp
, sfs
->f_mntfromname
,
2862 sizeof(sfs
->f_mntfromname
) - 1, &size
);
2865 (void)memset(sfs
->f_mntfromname
+ size
, 0,
2866 sizeof(sfs
->f_mntfromname
) - size
);
2872 vfs_timestamp(struct timespec
*ts
)
2878 time_t rootfstime
; /* recorded root fs time, if known */
2880 setrootfstime(time_t t
)
2886 * Sham lock manager for vnodes. This is a temporary measure.
2889 vlockmgr(struct vnlock
*vl
, int flags
)
2892 KASSERT((flags
& ~(LK_CANRECURSE
| LK_NOWAIT
| LK_TYPE_MASK
)) == 0);
2894 switch (flags
& LK_TYPE_MASK
) {
2896 if (rw_tryenter(&vl
->vl_lock
, RW_READER
)) {
2899 if ((flags
& LK_NOWAIT
) != 0) {
2902 rw_enter(&vl
->vl_lock
, RW_READER
);
2906 if (rw_tryenter(&vl
->vl_lock
, RW_WRITER
)) {
2909 if ((vl
->vl_canrecurse
|| (flags
& LK_CANRECURSE
) != 0) &&
2910 rw_write_held(&vl
->vl_lock
)) {
2911 vl
->vl_recursecnt
++;
2914 if ((flags
& LK_NOWAIT
) != 0) {
2917 rw_enter(&vl
->vl_lock
, RW_WRITER
);
2921 if (vl
->vl_recursecnt
!= 0) {
2922 KASSERT(rw_write_held(&vl
->vl_lock
));
2923 vl
->vl_recursecnt
--;
2926 rw_exit(&vl
->vl_lock
);
2930 panic("vlockmgr: flags %x", flags
);
2935 vlockstatus(struct vnlock
*vl
)
2938 if (rw_write_held(&vl
->vl_lock
)) {
2939 return LK_EXCLUSIVE
;
2941 if (rw_read_held(&vl
->vl_lock
)) {
2948 * mount_specific_key_create --
2949 * Create a key for subsystem mount-specific data.
2952 mount_specific_key_create(specificdata_key_t
*keyp
, specificdata_dtor_t dtor
)
2955 return (specificdata_key_create(mount_specificdata_domain
, keyp
, dtor
));
2959 * mount_specific_key_delete --
2960 * Delete a key for subsystem mount-specific data.
2963 mount_specific_key_delete(specificdata_key_t key
)
2966 specificdata_key_delete(mount_specificdata_domain
, key
);
2970 * mount_initspecific --
2971 * Initialize a mount's specificdata container.
2974 mount_initspecific(struct mount
*mp
)
2978 error
= specificdata_init(mount_specificdata_domain
,
2979 &mp
->mnt_specdataref
);
2980 KASSERT(error
== 0);
2984 * mount_finispecific --
2985 * Finalize a mount's specificdata container.
2988 mount_finispecific(struct mount
*mp
)
2991 specificdata_fini(mount_specificdata_domain
, &mp
->mnt_specdataref
);
2995 * mount_getspecific --
2996 * Return mount-specific data corresponding to the specified key.
2999 mount_getspecific(struct mount
*mp
, specificdata_key_t key
)
3002 return (specificdata_getspecific(mount_specificdata_domain
,
3003 &mp
->mnt_specdataref
, key
));
3007 * mount_setspecific --
3008 * Set mount-specific data corresponding to the specified key.
3011 mount_setspecific(struct mount
*mp
, specificdata_key_t key
, void *data
)
3014 specificdata_setspecific(mount_specificdata_domain
,
3015 &mp
->mnt_specdataref
, key
, data
);
3019 VFS_MOUNT(struct mount
*mp
, const char *a
, void *b
, size_t *c
)
3023 KERNEL_LOCK(1, NULL
);
3024 error
= (*(mp
->mnt_op
->vfs_mount
))(mp
, a
, b
, c
);
3025 KERNEL_UNLOCK_ONE(NULL
);
3031 VFS_START(struct mount
*mp
, int a
)
3035 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3036 KERNEL_LOCK(1, NULL
);
3038 error
= (*(mp
->mnt_op
->vfs_start
))(mp
, a
);
3039 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3040 KERNEL_UNLOCK_ONE(NULL
);
3047 VFS_UNMOUNT(struct mount
*mp
, int a
)
3051 KERNEL_LOCK(1, NULL
);
3052 error
= (*(mp
->mnt_op
->vfs_unmount
))(mp
, a
);
3053 KERNEL_UNLOCK_ONE(NULL
);
3059 VFS_ROOT(struct mount
*mp
, struct vnode
**a
)
3063 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3064 KERNEL_LOCK(1, NULL
);
3066 error
= (*(mp
->mnt_op
->vfs_root
))(mp
, a
);
3067 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3068 KERNEL_UNLOCK_ONE(NULL
);
3075 VFS_QUOTACTL(struct mount
*mp
, int a
, uid_t b
, void *c
)
3079 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3080 KERNEL_LOCK(1, NULL
);
3082 error
= (*(mp
->mnt_op
->vfs_quotactl
))(mp
, a
, b
, c
);
3083 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3084 KERNEL_UNLOCK_ONE(NULL
);
3091 VFS_STATVFS(struct mount
*mp
, struct statvfs
*a
)
3095 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3096 KERNEL_LOCK(1, NULL
);
3098 error
= (*(mp
->mnt_op
->vfs_statvfs
))(mp
, a
);
3099 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3100 KERNEL_UNLOCK_ONE(NULL
);
3107 VFS_SYNC(struct mount
*mp
, int a
, struct kauth_cred
*b
)
3111 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3112 KERNEL_LOCK(1, NULL
);
3114 error
= (*(mp
->mnt_op
->vfs_sync
))(mp
, a
, b
);
3115 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3116 KERNEL_UNLOCK_ONE(NULL
);
3123 VFS_FHTOVP(struct mount
*mp
, struct fid
*a
, struct vnode
**b
)
3127 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3128 KERNEL_LOCK(1, NULL
);
3130 error
= (*(mp
->mnt_op
->vfs_fhtovp
))(mp
, a
, b
);
3131 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3132 KERNEL_UNLOCK_ONE(NULL
);
3139 VFS_VPTOFH(struct vnode
*vp
, struct fid
*a
, size_t *b
)
3143 if ((vp
->v_vflag
& VV_MPSAFE
) == 0) {
3144 KERNEL_LOCK(1, NULL
);
3146 error
= (*(vp
->v_mount
->mnt_op
->vfs_vptofh
))(vp
, a
, b
);
3147 if ((vp
->v_vflag
& VV_MPSAFE
) == 0) {
3148 KERNEL_UNLOCK_ONE(NULL
);
3155 VFS_SNAPSHOT(struct mount
*mp
, struct vnode
*a
, struct timespec
*b
)
3159 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3160 KERNEL_LOCK(1, NULL
);
3162 error
= (*(mp
->mnt_op
->vfs_snapshot
))(mp
, a
, b
);
3163 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3164 KERNEL_UNLOCK_ONE(NULL
);
3171 VFS_EXTATTRCTL(struct mount
*mp
, int a
, struct vnode
*b
, int c
, const char *d
)
3175 KERNEL_LOCK(1, NULL
); /* XXXSMP check ffs */
3176 error
= (*(mp
->mnt_op
->vfs_extattrctl
))(mp
, a
, b
, c
, d
);
3177 KERNEL_UNLOCK_ONE(NULL
); /* XXX */
3183 VFS_SUSPENDCTL(struct mount
*mp
, int a
)
3187 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3188 KERNEL_LOCK(1, NULL
);
3190 error
= (*(mp
->mnt_op
->vfs_suspendctl
))(mp
, a
);
3191 if ((mp
->mnt_iflag
& IMNT_MPSAFE
) == 0) {
3192 KERNEL_UNLOCK_ONE(NULL
);
3198 #if defined(DDB) || defined(DEBUGPRINT)
3199 static const char buf_flagbits
[] = BUF_FLAGBITS
;
3202 vfs_buf_print(struct buf
*bp
, int full
, void (*pr
)(const char *, ...))
3206 (*pr
)(" vp %p lblkno 0x%"PRIx64
" blkno 0x%"PRIx64
" rawblkno 0x%"
3207 PRIx64
" dev 0x%x\n",
3208 bp
->b_vp
, bp
->b_lblkno
, bp
->b_blkno
, bp
->b_rawblkno
, bp
->b_dev
);
3210 snprintb(bf
, sizeof(bf
),
3211 buf_flagbits
, bp
->b_flags
| bp
->b_oflags
| bp
->b_cflags
);
3212 (*pr
)(" error %d flags 0x%s\n", bp
->b_error
, bf
);
3214 (*pr
)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n",
3215 bp
->b_bufsize
, bp
->b_bcount
, bp
->b_resid
);
3216 (*pr
)(" data %p saveaddr %p\n",
3217 bp
->b_data
, bp
->b_saveaddr
);
3218 (*pr
)(" iodone %p objlock %p\n", bp
->b_iodone
, bp
->b_objlock
);
3223 vfs_vnode_print(struct vnode
*vp
, int full
, void (*pr
)(const char *, ...))
3227 uvm_object_printit(&vp
->v_uobj
, full
, pr
);
3228 snprintb(bf
, sizeof(bf
),
3229 vnode_flagbits
, vp
->v_iflag
| vp
->v_vflag
| vp
->v_uflag
);
3230 (*pr
)("\nVNODE flags %s\n", bf
);
3231 (*pr
)("mp %p numoutput %d size 0x%llx writesize 0x%llx\n",
3232 vp
->v_mount
, vp
->v_numoutput
, vp
->v_size
, vp
->v_writesize
);
3234 (*pr
)("data %p writecount %ld holdcnt %ld\n",
3235 vp
->v_data
, vp
->v_writecount
, vp
->v_holdcnt
);
3237 (*pr
)("tag %s(%d) type %s(%d) mount %p typedata %p\n",
3238 ARRAY_PRINT(vp
->v_tag
, vnode_tags
), vp
->v_tag
,
3239 ARRAY_PRINT(vp
->v_type
, vnode_types
), vp
->v_type
,
3240 vp
->v_mount
, vp
->v_mountedhere
);
3242 (*pr
)("v_lock %p v_vnlock %p\n", &vp
->v_lock
, vp
->v_vnlock
);
3247 (*pr
)("clean bufs:\n");
3248 LIST_FOREACH(bp
, &vp
->v_cleanblkhd
, b_vnbufs
) {
3249 (*pr
)(" bp %p\n", bp
);
3250 vfs_buf_print(bp
, full
, pr
);
3253 (*pr
)("dirty bufs:\n");
3254 LIST_FOREACH(bp
, &vp
->v_dirtyblkhd
, b_vnbufs
) {
3255 (*pr
)(" bp %p\n", bp
);
3256 vfs_buf_print(bp
, full
, pr
);
3262 vfs_mount_print(struct mount
*mp
, int full
, void (*pr
)(const char *, ...))
3266 (*pr
)("vnodecovered = %p syncer = %p data = %p\n",
3267 mp
->mnt_vnodecovered
,mp
->mnt_syncer
,mp
->mnt_data
);
3269 (*pr
)("fs_bshift %d dev_bshift = %d\n",
3270 mp
->mnt_fs_bshift
,mp
->mnt_dev_bshift
);
3272 snprintb(sbuf
, sizeof(sbuf
), __MNT_FLAG_BITS
, mp
->mnt_flag
);
3273 (*pr
)("flag = %s\n", sbuf
);
3275 snprintb(sbuf
, sizeof(sbuf
), __IMNT_FLAG_BITS
, mp
->mnt_iflag
);
3276 (*pr
)("iflag = %s\n", sbuf
);
3278 (*pr
)("refcnt = %d unmounting @ %p updating @ %p\n", mp
->mnt_refcnt
,
3279 &mp
->mnt_unmounting
, &mp
->mnt_updating
);
3281 (*pr
)("statvfs cache:\n");
3282 (*pr
)("\tbsize = %lu\n",mp
->mnt_stat
.f_bsize
);
3283 (*pr
)("\tfrsize = %lu\n",mp
->mnt_stat
.f_frsize
);
3284 (*pr
)("\tiosize = %lu\n",mp
->mnt_stat
.f_iosize
);
3286 (*pr
)("\tblocks = %"PRIu64
"\n",mp
->mnt_stat
.f_blocks
);
3287 (*pr
)("\tbfree = %"PRIu64
"\n",mp
->mnt_stat
.f_bfree
);
3288 (*pr
)("\tbavail = %"PRIu64
"\n",mp
->mnt_stat
.f_bavail
);
3289 (*pr
)("\tbresvd = %"PRIu64
"\n",mp
->mnt_stat
.f_bresvd
);
3291 (*pr
)("\tfiles = %"PRIu64
"\n",mp
->mnt_stat
.f_files
);
3292 (*pr
)("\tffree = %"PRIu64
"\n",mp
->mnt_stat
.f_ffree
);
3293 (*pr
)("\tfavail = %"PRIu64
"\n",mp
->mnt_stat
.f_favail
);
3294 (*pr
)("\tfresvd = %"PRIu64
"\n",mp
->mnt_stat
.f_fresvd
);
3296 (*pr
)("\tf_fsidx = { 0x%"PRIx32
", 0x%"PRIx32
" }\n",
3297 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0],
3298 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1]);
3300 (*pr
)("\towner = %"PRIu32
"\n",mp
->mnt_stat
.f_owner
);
3301 (*pr
)("\tnamemax = %lu\n",mp
->mnt_stat
.f_namemax
);
3303 snprintb(sbuf
, sizeof(sbuf
), __MNT_FLAG_BITS
, mp
->mnt_stat
.f_flag
);
3305 (*pr
)("\tflag = %s\n",sbuf
);
3306 (*pr
)("\tsyncwrites = %" PRIu64
"\n",mp
->mnt_stat
.f_syncwrites
);
3307 (*pr
)("\tasyncwrites = %" PRIu64
"\n",mp
->mnt_stat
.f_asyncwrites
);
3308 (*pr
)("\tsyncreads = %" PRIu64
"\n",mp
->mnt_stat
.f_syncreads
);
3309 (*pr
)("\tasyncreads = %" PRIu64
"\n",mp
->mnt_stat
.f_asyncreads
);
3310 (*pr
)("\tfstypename = %s\n",mp
->mnt_stat
.f_fstypename
);
3311 (*pr
)("\tmntonname = %s\n",mp
->mnt_stat
.f_mntonname
);
3312 (*pr
)("\tmntfromname = %s\n",mp
->mnt_stat
.f_mntfromname
);
3317 (*pr
)("locked vnodes =");
3318 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
3319 if (VOP_ISLOCKED(vp
)) {
3320 if ((++cnt
% 6) == 0) {
3321 (*pr
)(" %p,\n\t", vp
);
3333 (*pr
)("all vnodes =");
3334 TAILQ_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
3335 if (!TAILQ_NEXT(vp
, v_mntvnodes
)) {
3337 } else if ((++cnt
% 6) == 0) {
3338 (*pr
)(" %p,\n\t", vp
);
3346 #endif /* DDB || DEBUGPRINT */
3349 * Check if a device pointed to by vp is mounted.
3352 * EINVAL if it's not a disk
3353 * EBUSY if it's a disk and mounted
3354 * 0 if it's a disk and not mounted
3357 rawdev_mounted(struct vnode
*vp
, struct vnode
**bvpp
)
3370 switch (vp
->v_type
) {
3372 const struct cdevsw
*cdev
;
3374 cdev
= cdevsw_lookup(dev
);
3378 blkdev
= devsw_chr2blk(dev
);
3379 if (blkdev
!= NODEV
) {
3380 vfinddev(blkdev
, VBLK
, &bvp
);
3382 d_type
= (cdev
->d_flag
& D_TYPEMASK
);
3390 const struct bdevsw
*bdev
;
3392 bdev
= bdevsw_lookup(dev
);
3394 d_type
= (bdev
->d_flag
& D_TYPEMASK
);
3405 if (d_type
!= D_DISK
)
3412 * XXX: This is bogus. We should be failing the request
3413 * XXX: not only if this specific slice is mounted, but
3414 * XXX: if it's on a disk with any other mounted slice.
3416 if (vfs_mountedon(bvp
))