2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/kern/vfs_lock.c,v 1.17 2006/05/06 02:43:12 dillon Exp $
38 * External virtual filesystem routines
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
48 #include <sys/vnode.h>
50 #include <sys/sysctl.h>
52 #include <machine/limits.h>
55 #include <vm/vm_object.h>
58 #include <sys/thread2.h>
61 static MALLOC_DEFINE(M_VNODE, "vnodes", "vnode structures");
63 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
66 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD,
68 static int wantfreevnodes = 25;
69 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
70 &wantfreevnodes, 0, "");
72 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
73 &minvnodes, 0, "Minimum number of vnodes");
76 * Called from vfsinit()
81 minvnodes = desiredvnodes / 4;
83 TAILQ_INIT(&vnode_free_list);
87 * Inline helper functions. vbusy() and vfree() must be called while in a
92 __vbusy(struct vnode *vp)
94 KKASSERT(vp->v_flag & VFREE);
95 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
97 vp->v_flag &= ~(VFREE|VAGE);
102 __vfree(struct vnode *vp)
104 KKASSERT((vp->v_flag & VFREE) == 0);
105 if (vp->v_flag & (VAGE|VRECLAIMED))
106 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
108 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
115 * Return 1 if we can immediately place the vnode on the freelist.
118 vshouldfree(struct vnode *vp, int usecount)
120 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
121 return (0); /* other holderse */
123 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
130 * Reference a vnode or release the reference on a vnode. The vnode will
131 * be taken off the freelist if it is on it and cannot be recycled or
132 * deactivated while refd. The last release of a vnode will deactivate the
133 * vnode via VOP_INACTIVE().
135 * Special cases: refing a vnode does not clear VINACTIVE, you have to vget()
136 * the vnode shared or exclusive to do that.
140 __vref(struct vnode *vp)
143 if (vp->v_flag & VFREE)
148 vref(struct vnode *vp)
156 vrele(struct vnode *vp)
159 if (vp->v_usecount == 1) {
160 KASSERT(lockcountnb(&vp->v_lock) == 0, ("last vrele vp %p still locked", vp));
163 * Deactivation requires an exclusive v_lock (vx_lock()), and
164 * only occurs if the usecount is still 1 after locking.
166 if ((vp->v_flag & VINACTIVE) == 0) {
167 if (vx_lock(vp) == 0) {
168 if ((vp->v_flag & VINACTIVE) == 0 &&
169 vp->v_usecount == 1) {
170 vp->v_flag |= VINACTIVE;
176 if (vshouldfree(vp, 1))
179 KKASSERT(vp->v_usecount > 0);
186 * Hold a vnode or drop the hold on a vnode. The vnode will be taken off
187 * the freelist if it is on it and cannot be recycled. However, the
188 * vnode can be deactivated and reactivated while held.
190 * Special cases: The last drop of a vnode does nothing special, allowing it
191 * to be called from an interrupt. vrele() on the otherhand cannot be called
195 vhold(struct vnode *vp)
199 if (vp->v_flag & VFREE)
205 vdrop(struct vnode *vp)
208 if (vp->v_holdcnt == 1) {
210 if (vshouldfree(vp, 0))
214 KKASSERT(vp->v_holdcnt > 0);
219 /****************************************************************
220 * VX LOCKING FUNCTIONS *
221 ****************************************************************
223 * These functions lock vnodes for reclamation and deactivation ops.
224 * Only vp->v_lock, the top layer of the VFS, is locked. You must be
225 * holding a normal reference in order to be able to safely call vx_lock()
226 * and vx_unlock(). vx_get() and vx_put() are combination functions which
227 * vref+vx_lock and vrele+vx_unlock.
230 #define VXLOCKFLAGS (LK_EXCLUSIVE|LK_RETRY)
231 #define VXLOCKFLAGS_NB (LK_EXCLUSIVE|LK_NOWAIT)
234 __vxlock(struct vnode *vp, int flags)
236 return(lockmgr(&vp->v_lock, flags));
240 __vxunlock(struct vnode *vp)
242 lockmgr(&vp->v_lock, LK_RELEASE);
246 vx_lock(struct vnode *vp)
248 return(__vxlock(vp, VXLOCKFLAGS));
252 vx_unlock(struct vnode *vp)
258 vx_get(struct vnode *vp)
263 if ((error = __vxlock(vp, VXLOCKFLAGS)) != 0)
269 vx_get_nonblock(struct vnode *vp)
274 if ((error = __vxlock(vp, VXLOCKFLAGS_NB)) != 0)
280 vx_put(struct vnode *vp)
286 /****************************************************************
287 * VNODE ACQUISITION FUNCTIONS *
288 ****************************************************************
290 * vget() and vput() access a vnode for the intent of executing an
291 * operation other then a reclamation or deactivation. vget() will ref
292 * and lock the vnode, vput() will unlock and deref the vnode.
293 * The VOP_*() locking functions are used.
295 * CALLING VGET IS MANDATORY PRIOR TO ANY MODIFYING OPERATION ON A VNODE.
296 * This is because vget handles the VINACTIVE interlock and is responsible
297 * for clearing the bit. If the bit is not cleared inode updates may not
300 * Special cases: If vget()'s locking operation fails the vrele() call may
301 * cause the vnode to be deactivated (VOP_INACTIVE called). However, this
302 * never occurs if the vnode is in a reclaimed state. Vnodes in reclaimed
303 * states always return an error code of ENOENT.
305 * Special cases: vput() will unlock and, if it is the last reference,
306 * deactivate the vnode. The deactivation uses a separate non-layered
307 * VX lock after the normal unlock. XXX make it more efficient.
310 vget(struct vnode *vp, int flags)
316 if (flags & LK_TYPE_MASK) {
317 if ((error = vn_lock(vp, flags)) != 0) {
319 } else if (vp->v_flag & VRECLAIMED) {
324 vp->v_flag &= ~VINACTIVE;
328 panic("vget() called with no lock specified!");
329 error = ENOENT; /* not reached, compiler opt */
336 vput(struct vnode *vp)
343 vsetflags(struct vnode *vp, int flags)
351 vclrflags(struct vnode *vp, int flags)
354 vp->v_flag &= ~flags;
359 * Obtain a new vnode from the freelist, allocating more if necessary.
360 * The returned vnode is VX locked & refd.
363 allocvnode(int lktimeout, int lkflags)
369 * Try to reuse vnodes if we hit the max. This situation only
370 * occurs in certain large-memory (2G+) situations. We cannot
371 * attempt to directly reclaim vnodes due to nasty recursion
374 while (numvnodes - freevnodes > desiredvnodes)
381 * Attempt to reuse a vnode already on the free list, allocating
382 * a new vnode if we can't find one or if we have not reached a
383 * good minimum for good LRU performance.
385 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
388 for (count = 0; count < freevnodes; count++) {
392 * Pull the next vnode off the free list and do some
393 * sanity checks. Note that regardless of how we
394 * block, if freevnodes is non-zero there had better
395 * be something on the list.
397 vp = TAILQ_FIRST(&vnode_free_list);
399 panic("getnewvnode: free vnode isn't");
402 * Note the lack of a critical section. We vx_get()
403 * the vnode before we check it for validity, reducing
404 * the number of checks we have to make. The vx_get()
405 * will pull it off the freelist.
413 * Can this vnode be recycled? It must be in a
414 * VINACTIVE state with only our reference to it.
415 * (vx_get(), unlike vget(), does not reactivate
416 * the vnode). vx_put() will recycle it onto the
417 * end of the freelist.
419 if ((vp->v_flag & VINACTIVE) == 0 ||
420 vp->v_holdcnt || vp->v_usecount != 1) {
427 * Ok, we can reclaim the vnode if it isn't already
428 * in a reclaimed state. If the reclamation fails,
429 * or if someone else is referencing the vnode after
430 * we have vgone()'d it, we recycle the vnode on the
431 * freelist or hold it (by calling vx_put()).
433 if ((vp->v_flag & VRECLAIMED) == 0) {
435 if ((vp->v_flag & VRECLAIMED) == 0 ||
436 vp->v_holdcnt || vp->v_usecount != 1) {
442 KKASSERT(vp->v_flag & VINACTIVE);
452 * If we have a vp it will be refd and VX locked.
457 panic("cleaned vnode isn't");
458 if (vp->v_track_read.bk_active + vp->v_track_write.bk_active)
459 panic("Clean vnode has pending I/O's");
460 KKASSERT(vp->v_mount == NULL);
469 vp->v_writecount = 0; /* XXX */
470 lockreinit(&vp->v_lock, "vnode", lktimeout, lkflags);
471 KKASSERT(TAILQ_FIRST(&vp->v_namecache) == NULL);
474 * A brand-new vnode (we could use malloc() here I think) XXX
476 vp = malloc(sizeof(struct vnode), M_VNODE, M_WAITOK|M_ZERO);
477 lwkt_token_init(&vp->v_pollinfo.vpi_token);
478 lockinit(&vp->v_lock, "vnode", lktimeout, lkflags);
479 TAILQ_INIT(&vp->v_namecache);
482 * short cut around vfreeing it and looping, just set it up
483 * as if we had pulled a reclaimed vnode off the freelist
484 * and reinitialized it.
487 if (__vxlock(vp, VXLOCKFLAGS))
488 panic("getnewvnode: __vxlock failed");
492 RB_INIT(&vp->v_rbclean_tree);
493 RB_INIT(&vp->v_rbdirty_tree);
494 RB_INIT(&vp->v_rbhash_tree);
495 vp->v_filesize = NOOFFSET;
500 KKASSERT(vp->v_mount == NULL);