Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / kern / vfs_lock.c
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/kern/vfs_lock.c,v 1.30 2008/06/30 03:57:41 dillon Exp $
35  */
36
37 /*
38  * External virtual filesystem routines
39  */
40 #include "opt_ddb.h"
41
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>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/buf.h>
50 #include <sys/sysctl.h>
51
52 #include <machine/limits.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_object.h>
56
57 #include <sys/buf2.h>
58 #include <sys/thread2.h>
59 #include <sys/sysref2.h>
60
61 static void vnode_terminate(struct vnode *vp);
62 static boolean_t vnode_ctor(void *obj, void *private, int ocflags);
63 static void vnode_dtor(void *obj, void *private);
64
65 static MALLOC_DEFINE(M_VNODE, "vnodes", "vnode structures");
66 static struct sysref_class vnode_sysref_class = {
67         .name =         "vnode",
68         .mtype =        M_VNODE,
69         .proto =        SYSREF_PROTO_VNODE,
70         .offset =       offsetof(struct vnode, v_sysref),
71         .objsize =      sizeof(struct vnode),
72         .mag_capacity = 256,
73         .flags =        SRC_MANAGEDINIT,
74         .ctor =         vnode_ctor,
75         .dtor =         vnode_dtor,
76         .ops = {
77                 .terminate = (sysref_terminate_func_t)vnode_terminate
78         }
79 };
80
81 /*
82  * The vnode free list hold inactive vnodes.  Aged inactive vnodes
83  * are inserted prior to the mid point, and otherwise inserted
84  * at the tail.
85  */
86 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
87 static struct vnode     vnode_free_mid;
88 static struct spinlock  vfs_spin = SPINLOCK_INITIALIZER(vfs_spin);
89
90 int  freevnodes = 0;
91 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD,
92                 &freevnodes, 0, "");
93 static int wantfreevnodes = 25;
94 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
95                 &wantfreevnodes, 0, "");
96 #ifdef TRACKVNODE
97 static ulong trackvnode;
98 SYSCTL_ULONG(_debug, OID_AUTO, trackvnode, CTLFLAG_RW,
99                 &trackvnode, 0, "");
100 #endif
101
102 /*
103  * Called from vfsinit()
104  */
105 void
106 vfs_lock_init(void)
107 {
108         TAILQ_INIT(&vnode_free_list);
109         TAILQ_INSERT_HEAD(&vnode_free_list, &vnode_free_mid, v_freelist);
110         spin_init(&vfs_spin);
111         kmalloc_raise_limit(M_VNODE, 0);        /* unlimited */
112 }
113
114 /*
115  * Misc functions
116  */
117 static __inline
118 void
119 _vsetflags(struct vnode *vp, int flags)
120 {
121         atomic_set_int(&vp->v_flag, flags);
122 }
123
124 static __inline
125 void
126 _vclrflags(struct vnode *vp, int flags)
127 {
128         atomic_clear_int(&vp->v_flag, flags);
129 }
130
131 void
132 vsetflags(struct vnode *vp, int flags)
133 {
134         _vsetflags(vp, flags);
135 }
136
137 void
138 vclrflags(struct vnode *vp, int flags)
139 {
140         _vclrflags(vp, flags);
141 }
142
143 /*
144  * Inline helper functions.
145  *
146  * WARNING: vbusy() may only be called while the vnode lock or VX lock
147  *          is held.  The vnode spinlock need not be held.
148  *
149  * MPSAFE
150  */
151 static __inline
152 void
153 __vbusy_interlocked(struct vnode *vp)
154 {
155         TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
156         freevnodes--;
157         _vclrflags(vp, VFREE);
158 }
159
160 static __inline 
161 void
162 __vbusy(struct vnode *vp)
163 {
164 #ifdef TRACKVNODE
165         if ((ulong)vp == trackvnode)
166                 kprintf("__vbusy %p %08x\n", vp, vp->v_flag);
167 #endif
168         spin_lock_wr(&vfs_spin);
169         __vbusy_interlocked(vp);
170         spin_unlock_wr(&vfs_spin);
171 }
172
173 /*
174  * Put a vnode on the free list.  The caller has cleared VCACHED or owns the
175  * implied sysref related to having removed the vnode from the freelist
176  * (and VCACHED is already clear in that case).
177  *
178  * MPSAFE
179  */
180 static __inline
181 void
182 __vfree(struct vnode *vp)
183 {
184 #ifdef TRACKVNODE
185         if ((ulong)vp == trackvnode) {
186                 kprintf("__vfree %p %08x\n", vp, vp->v_flag);
187                 print_backtrace();
188         }
189 #endif
190         spin_lock_wr(&vfs_spin);
191         if (vp->v_flag & VRECLAIMED)
192                 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
193         else if (vp->v_flag & (VAGE0 | VAGE1))
194                 TAILQ_INSERT_BEFORE(&vnode_free_mid, vp, v_freelist);
195         else
196                 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
197         freevnodes++;
198         _vsetflags(vp, VFREE);
199         spin_unlock_wr(&vfs_spin);
200 }
201
202 /*
203  * Put a vnode on the free list.  The caller has cleared VCACHED or owns the
204  * implied sysref related to having removed the vnode from the freelist
205  * (and VCACHED is already clear in that case).
206  *
207  * MPSAFE
208  */
209 static __inline
210 void
211 __vfreetail(struct vnode *vp)
212 {
213 #ifdef TRACKVNODE
214         if ((ulong)vp == trackvnode)
215                 kprintf("__vfreetail %p %08x\n", vp, vp->v_flag);
216 #endif
217         spin_lock_wr(&vfs_spin);
218         TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
219         freevnodes++;
220         _vsetflags(vp, VFREE);
221         spin_unlock_wr(&vfs_spin);
222 }
223
224 /*
225  * Return a C boolean if we should put the vnode on the freelist (VFREE),
226  * or leave it / mark it as VCACHED.
227  *
228  * This routine is only valid if the vnode is already either VFREE or
229  * VCACHED, or if it can become VFREE or VCACHED via vnode_terminate().
230  *
231  * WARNING!  This functions is typically called with v_spinlock held.
232  *
233  * MPSAFE
234  */
235 static __inline boolean_t
236 vshouldfree(struct vnode *vp)
237 {
238         return (vp->v_auxrefs == 0 &&
239             (vp->v_object == NULL || vp->v_object->resident_page_count == 0));
240 }
241
242 /*
243  * Add a ref to an active vnode.  This function should never be called
244  * with an inactive vnode (use vget() instead).
245  *
246  * MPSAFE
247  */
248 void
249 vref(struct vnode *vp)
250 {
251         KKASSERT(vp->v_sysref.refcnt > 0 && 
252                  (vp->v_flag & (VFREE|VINACTIVE)) == 0);
253         sysref_get(&vp->v_sysref);
254 }
255
256 /*
257  * Release a ref on an active or inactive vnode.  The sysref termination
258  * function will be called when the active last active reference is released,
259  * and the vnode is returned to the objcache when the last inactive
260  * reference is released.
261  */
262 void
263 vrele(struct vnode *vp)
264 {
265         sysref_put(&vp->v_sysref);
266 }
267
268 /*
269  * Add an auxiliary data structure reference to the vnode.  Auxiliary
270  * references do not change the state of the vnode or prevent them
271  * from being deactivated, reclaimed, or placed on or removed from
272  * the free list.
273  *
274  * An auxiliary reference DOES prevent the vnode from being destroyed,
275  * allowing you to vx_lock() it, test state, etc.
276  *
277  * An auxiliary reference DOES NOT move a vnode out of the VFREE state
278  * once it has entered it.
279  *
280  * WARNING!  vhold() and vhold_interlocked() must not acquire v_spinlock.
281  *           The spinlock may or may not already be held by the caller.
282  *           vdrop() will clean up the free list state.
283  *
284  * MPSAFE
285  */
286 void
287 vhold(struct vnode *vp)
288 {
289         KKASSERT(vp->v_sysref.refcnt != 0);
290         atomic_add_int(&vp->v_auxrefs, 1);
291 }
292
293 void
294 vhold_interlocked(struct vnode *vp)
295 {
296         atomic_add_int(&vp->v_auxrefs, 1);
297 }
298
299 /*
300  * Remove an auxiliary reference from the vnode.
301  *
302  * vdrop needs to check for a VCACHE->VFREE transition to catch cases
303  * where a vnode is held past its reclamation.  We use v_spinlock to
304  * interlock VCACHED -> !VCACHED transitions.
305  *
306  * MPSAFE
307  */
308 void
309 vdrop(struct vnode *vp)
310 {
311         KKASSERT(vp->v_sysref.refcnt != 0 && vp->v_auxrefs > 0);
312         spin_lock_wr(&vp->v_spinlock);
313         atomic_subtract_int(&vp->v_auxrefs, 1);
314         if ((vp->v_flag & VCACHED) && vshouldfree(vp)) {
315                 _vclrflags(vp, VCACHED);
316                 __vfree(vp);
317         }
318         spin_unlock_wr(&vp->v_spinlock);
319 }
320
321 /*
322  * This function is called when the last active reference on the vnode
323  * is released, typically via vrele().  SYSREF will give the vnode a
324  * negative ref count, indicating that it is undergoing termination or
325  * is being set aside for the cache, and one final sysref_put() is
326  * required to actually return it to the memory subsystem.
327  *
328  * However, because vnodes may have auxiliary structural references via
329  * v_auxrefs, we must interlock auxiliary references against termination
330  * via the VX lock mechanism.  It is possible for a vnode to be reactivated
331  * while we were blocked on the lock.
332  *
333  * MPSAFE
334  */
335 void
336 vnode_terminate(struct vnode *vp)
337 {
338         vx_lock(vp);
339         if (sysref_isinactive(&vp->v_sysref)) {
340                 /*
341                  * Deactivate the vnode by marking it VFREE or VCACHED.
342                  * The vnode can be reactivated from either state until
343                  * reclaimed.  These states inherit the 'last' sysref on the
344                  * vnode.
345                  *
346                  * NOTE: There may be additional inactive references from
347                  * other entities blocking on the VX lock while we hold it,
348                  * but this does not prevent us from changing the vnode's
349                  * state.
350                  *
351                  * NOTE: The vnode could already be marked inactive.  XXX
352                  *       how?
353                  *
354                  * NOTE: v_mount may be NULL due to assignment to
355                  *       dead_vnode_vops
356                  *
357                  * NOTE: The vnode may be marked inactive with dirty buffers
358                  *       or dirty pages in its cached VM object still present.
359                  *
360                  * NOTE: VCACHED should not be set on entry.  We lose control
361                  *       of the sysref the instant the vnode is placed on the
362                  *       free list or when VCACHED is set.
363                  *
364                  *       The VX lock is sufficient when transitioning
365                  *       to +VCACHED but not sufficient for the vshouldfree()
366                  *       interlocked test.
367                  */
368                 if ((vp->v_flag & VINACTIVE) == 0) {
369                         _vsetflags(vp, VINACTIVE);
370                         if (vp->v_mount)
371                                 VOP_INACTIVE(vp);
372                 }
373                 spin_lock_wr(&vp->v_spinlock);
374                 KKASSERT((vp->v_flag & (VFREE|VCACHED)) == 0);
375                 if (vshouldfree(vp))
376                         __vfree(vp);
377                 else
378                         _vsetflags(vp, VCACHED); /* inactive but not yet free*/
379                 spin_unlock_wr(&vp->v_spinlock);
380                 vx_unlock(vp);
381         } else {
382                 /*
383                  * Someone reactivated the vnode while were blocked on the
384                  * VX lock.  Release the VX lock and release the (now active)
385                  * last reference which is no longer last.
386                  */
387                 vx_unlock(vp);
388                 vrele(vp);
389         }
390 }
391
392 /*
393  * Physical vnode constructor / destructor.  These are only executed on
394  * the backend of the objcache.  They are NOT executed on every vnode
395  * allocation or deallocation.
396  *
397  * MPSAFE
398  */
399 boolean_t
400 vnode_ctor(void *obj, void *private, int ocflags)
401 {
402         struct vnode *vp = obj;
403
404         lwkt_token_init(&vp->v_token);
405         lockinit(&vp->v_lock, "vnode", 0, 0);
406         ccms_dataspace_init(&vp->v_ccms);
407         TAILQ_INIT(&vp->v_namecache);
408         RB_INIT(&vp->v_rbclean_tree);
409         RB_INIT(&vp->v_rbdirty_tree);
410         RB_INIT(&vp->v_rbhash_tree);
411         return(TRUE);
412 }
413
414 /*
415  * MPSAFE
416  */
417 void
418 vnode_dtor(void *obj, void *private)
419 {
420         struct vnode *vp = obj;
421
422         ccms_dataspace_destroy(&vp->v_ccms);
423 }
424
425 /****************************************************************
426  *                      VX LOCKING FUNCTIONS                    *
427  ****************************************************************
428  *
429  * These functions lock vnodes for reclamation and deactivation related
430  * activities.  The caller must already be holding some sort of reference
431  * on the vnode.
432  *
433  * MPSAFE
434  */
435 void
436 vx_lock(struct vnode *vp)
437 {
438         lockmgr(&vp->v_lock, LK_EXCLUSIVE);
439 }
440
441 /*
442  * The non-blocking version also uses a slightly different mechanic.
443  * This function will explicitly fail not only if it cannot acquire
444  * the lock normally, but also if the caller already holds a lock.
445  *
446  * The adjusted mechanic is used to close a loophole where complex
447  * VOP_RECLAIM code can circle around recursively and allocate the
448  * same vnode it is trying to destroy from the freelist.
449  *
450  * Any filesystem (aka UFS) which puts LK_CANRECURSE in lk_flags can
451  * cause the incorrect behavior to occur.  If not for that lockmgr()
452  * would do the right thing.
453  */
454 static int
455 vx_lock_nonblock(struct vnode *vp)
456 {
457         if (lockcountnb(&vp->v_lock))
458                 return(EBUSY);
459         return(lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT | LK_NOSPINWAIT));
460 }
461
462 void
463 vx_unlock(struct vnode *vp)
464 {
465         lockmgr(&vp->v_lock, LK_RELEASE);
466 }
467
468 /****************************************************************
469  *                      VNODE ACQUISITION FUNCTIONS             *
470  ****************************************************************
471  *
472  * These functions must be used when accessing a vnode via an auxiliary
473  * reference such as the namecache or free list, or when you wish to
474  * do a combo ref+lock sequence.
475  *
476  * These functions are MANDATORY for any code chain accessing a vnode
477  * whos activation state is not known.
478  *
479  * vget() can be called with LK_NOWAIT and will return EBUSY if the
480  * lock cannot be immediately acquired.
481  *
482  * vget()/vput() are used when reactivation is desired.
483  *
484  * vx_get() and vx_put() are used when reactivation is not desired.
485  */
486 int
487 vget(struct vnode *vp, int flags)
488 {
489         int error;
490
491         /*
492          * A lock type must be passed
493          */
494         if ((flags & LK_TYPE_MASK) == 0) {
495                 panic("vget() called with no lock specified!");
496                 /* NOT REACHED */
497         }
498
499         /*
500          * Reference the structure and then acquire the lock.  0->1
501          * transitions and refs during termination are allowed here so
502          * call sysref directly.
503          *
504          * NOTE: The requested lock might be a shared lock and does
505          *       not protect our access to the refcnt or other fields.
506          */
507         sysref_get(&vp->v_sysref);
508         if ((error = vn_lock(vp, flags)) != 0) {
509                 /*
510                  * The lock failed, undo and return an error.
511                  */
512                 sysref_put(&vp->v_sysref);
513         } else if (vp->v_flag & VRECLAIMED) {
514                 /*
515                  * The node is being reclaimed and cannot be reactivated
516                  * any more, undo and return ENOENT.
517                  */
518                 vn_unlock(vp);
519                 vrele(vp);
520                 error = ENOENT;
521         } else {
522                 /*
523                  * If the vnode is marked VFREE or VCACHED it needs to be
524                  * reactivated, otherwise it had better already be active.
525                  * VINACTIVE must also be cleared.
526                  *
527                  * In the VFREE/VCACHED case we have to throw away the
528                  * sysref that was earmarking those cases and preventing
529                  * the vnode from being destroyed.  Our sysref is still held.
530                  *
531                  * The spinlock is our only real protection here.
532                  */
533                 spin_lock_wr(&vp->v_spinlock);
534                 if (vp->v_flag & VFREE) {
535                         __vbusy(vp);
536                         sysref_activate(&vp->v_sysref);
537                         spin_unlock_wr(&vp->v_spinlock);
538                         sysref_put(&vp->v_sysref);
539                 } else if (vp->v_flag & VCACHED) {
540                         _vclrflags(vp, VCACHED);
541                         sysref_activate(&vp->v_sysref);
542                         spin_unlock_wr(&vp->v_spinlock);
543                         sysref_put(&vp->v_sysref);
544                 } else {
545                         if (sysref_isinactive(&vp->v_sysref)) {
546                                 sysref_activate(&vp->v_sysref);
547                                 kprintf("Warning vp %p reactivation race\n",
548                                         vp);
549                         }
550                         spin_unlock_wr(&vp->v_spinlock);
551                 }
552                 _vclrflags(vp, VINACTIVE);
553                 error = 0;
554         }
555         return(error);
556 }
557
558 /*
559  * MPSAFE
560  */
561 void
562 vput(struct vnode *vp)
563 {
564         vn_unlock(vp);
565         vrele(vp);
566 }
567
568 /*
569  * XXX The vx_*() locks should use auxrefs, not the main reference counter.
570  *
571  * MPSAFE
572  */
573 void
574 vx_get(struct vnode *vp)
575 {
576         sysref_get(&vp->v_sysref);
577         lockmgr(&vp->v_lock, LK_EXCLUSIVE);
578 }
579
580 /*
581  * MPSAFE
582  */
583 int
584 vx_get_nonblock(struct vnode *vp)
585 {
586         int error;
587
588         sysref_get(&vp->v_sysref);
589         error = lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT);
590         if (error)
591                 sysref_put(&vp->v_sysref);
592         return(error);
593 }
594
595 /*
596  * Relase a VX lock that also held a ref on the vnode.
597  *
598  * vx_put needs to check for a VCACHE->VFREE transition to catch the
599  * case where e.g. vnlru issues a vgone*().
600  *
601  * MPSAFE
602  */
603 void
604 vx_put(struct vnode *vp)
605 {
606         spin_lock_wr(&vp->v_spinlock);
607         if ((vp->v_flag & VCACHED) && vshouldfree(vp)) {
608                 _vclrflags(vp, VCACHED);
609                 __vfree(vp);
610         }
611         spin_unlock_wr(&vp->v_spinlock);
612         lockmgr(&vp->v_lock, LK_RELEASE);
613         sysref_put(&vp->v_sysref);
614 }
615
616 /*
617  * Try to reuse a vnode from the free list.
618  *
619  * NOTE: The returned vnode is not completely initialized.
620  *
621  * WARNING: The freevnodes count can race, NULL can be returned even if
622  *          freevnodes != 0.
623  *
624  * MPSAFE
625  */
626 static
627 struct vnode *
628 allocfreevnode(void)
629 {
630         struct vnode *vp;
631         int count;
632
633         for (count = 0; count < freevnodes; count++) {
634                 /*
635                  * Try to lock the first vnode on the free list.
636                  * Cycle if we can't.
637                  *
638                  * We use a bad hack in vx_lock_nonblock() which avoids
639                  * the lock order reversal between vfs_spin and v_spinlock.
640                  * This is very fragile code and I don't want to use
641                  * vhold here.
642                  */
643                 spin_lock_wr(&vfs_spin);
644                 vp = TAILQ_FIRST(&vnode_free_list);
645                 if (vp == &vnode_free_mid)
646                         vp = TAILQ_NEXT(vp, v_freelist);
647                 if (vx_lock_nonblock(vp)) {
648                         KKASSERT(vp->v_flag & VFREE);
649                         TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
650                         TAILQ_INSERT_TAIL(&vnode_free_list,
651                                           vp, v_freelist);
652                         spin_unlock_wr(&vfs_spin);
653                         continue;
654                 }
655
656                 /*
657                  * We inherit the sysref associated the vnode on the free
658                  * list.  Because VCACHED is clear the vnode will not
659                  * be placed back on the free list.  We own the sysref
660                  * free and clear and thus control the disposition of
661                  * the vnode.
662                  */
663                 __vbusy_interlocked(vp);
664                 spin_unlock_wr(&vfs_spin);
665 #ifdef TRACKVNODE
666                 if ((ulong)vp == trackvnode)
667                         kprintf("allocfreevnode %p %08x\n", vp, vp->v_flag);
668 #endif
669                 /*
670                  * Do not reclaim/reuse a vnode while auxillary refs exists.
671                  * This includes namecache refs due to a related ncp being
672                  * locked or having children.
673                  *
674                  * We will make this test several times as auxrefs can
675                  * get incremented on us without any spinlocks being held
676                  * until we have removed all namecache and inode references
677                  * to the vnode.
678                  *
679                  * Because VCACHED is already in the correct state (cleared)
680                  * we cannot race other vdrop()s occuring at the same time
681                  * and can safely place vp on the free list.
682                  *
683                  * The free list association reinherits the sysref.
684                  */
685                 if (vp->v_auxrefs) {
686                         __vfreetail(vp);
687                         vx_unlock(vp);
688                         continue;
689                 }
690
691                 /*
692                  * We inherit the reference that was previously associated
693                  * with the vnode being on the free list.  VCACHED had better
694                  * not be set because the reference and VX lock prevents
695                  * the sysref from transitioning to an active state.
696                  */
697                 KKASSERT((vp->v_flag & (VINACTIVE|VCACHED)) == VINACTIVE);
698                 KKASSERT(sysref_isinactive(&vp->v_sysref));
699
700                 /*
701                  * Holding the VX lock on an inactive vnode prevents it
702                  * from being reactivated or reused.  New namecache
703                  * associations can only be made using active vnodes.
704                  *
705                  * Another thread may be blocked on our vnode lock while
706                  * holding a namecache lock.  We can only reuse this vnode
707                  * if we can clear all namecache associations without
708                  * blocking.
709                  *
710                  * Because VCACHED is already in the correct state (cleared)
711                  * we cannot race other vdrop()s occuring at the same time
712                  * and can safely place vp on the free list.
713                  */
714                 if ((vp->v_flag & VRECLAIMED) == 0) {
715                         if (cache_inval_vp_nonblock(vp)) {
716                                 __vfreetail(vp);
717                                 vx_unlock(vp);
718                                 continue;
719                         }
720                         vgone_vxlocked(vp);
721                         /* vnode is still VX locked */
722                 }
723
724                 /*
725                  * We can reuse the vnode if no primary or auxiliary
726                  * references remain other then ours, else put it
727                  * back on the free list and keep looking.
728                  *
729                  * Either the free list inherits the last reference
730                  * or we fall through and sysref_activate() the last
731                  * reference.
732                  *
733                  * Since the vnode is in a VRECLAIMED state, no new
734                  * namecache associations could have been made.
735                  */
736                 KKASSERT(TAILQ_EMPTY(&vp->v_namecache));
737                 if (vp->v_auxrefs ||
738                     !sysref_islastdeactivation(&vp->v_sysref)) {
739                         __vfreetail(vp);
740                         vx_unlock(vp);
741                         continue;
742                 }
743
744                 /*
745                  * Return a VX locked vnode suitable for reuse.  The caller
746                  * inherits the sysref.
747                  */
748                 return(vp);
749         }
750         return(NULL);
751 }
752
753 /*
754  * Obtain a new vnode from the freelist, allocating more if necessary.
755  * The returned vnode is VX locked & refd.
756  *
757  * All new vnodes set the VAGE flags.  An open() of the vnode will
758  * decrement the (2-bit) flags.  Vnodes which are opened several times
759  * are thus retained in the cache over vnodes which are merely stat()d.
760  *
761  * MPSAFE
762  */
763 struct vnode *
764 allocvnode(int lktimeout, int lkflags)
765 {
766         struct vnode *vp;
767
768         /*
769          * Try to reuse vnodes if we hit the max.  This situation only
770          * occurs in certain large-memory (2G+) situations.  We cannot
771          * attempt to directly reclaim vnodes due to nasty recursion
772          * problems.
773          */
774         while (numvnodes - freevnodes > desiredvnodes)
775                 vnlru_proc_wait();
776
777         /*
778          * Try to build up as many vnodes as we can before reallocating
779          * from the free list.  A vnode on the free list simply means
780          * that it is inactive with no resident pages.  It may or may not
781          * have been reclaimed and could have valuable information associated 
782          * with it that we shouldn't throw away unless we really need to.
783          *
784          * HAMMER NOTE: Re-establishing a vnode is a fairly expensive
785          * operation for HAMMER but this should benefit UFS as well.
786          */
787         if (freevnodes >= wantfreevnodes && numvnodes >= desiredvnodes)
788                 vp = allocfreevnode();
789         else
790                 vp = NULL;
791         if (vp == NULL) {
792                 vp = sysref_alloc(&vnode_sysref_class);
793                 lockmgr(&vp->v_lock, LK_EXCLUSIVE);
794                 numvnodes++;
795         }
796
797         /*
798          * We are using a managed sysref class, vnode fields are only
799          * zerod on initial allocation from the backing store, not
800          * on reallocation.  Thus we have to clear these fields for both
801          * reallocation and reuse.
802          */
803 #ifdef INVARIANTS
804         if (vp->v_data)
805                 panic("cleaned vnode isn't");
806         if (bio_track_active(&vp->v_track_read) ||
807             bio_track_active(&vp->v_track_write)) {
808                 panic("Clean vnode has pending I/O's");
809         }
810         if (vp->v_flag & VONWORKLST)
811                 panic("Clean vnode still pending on syncer worklist!");
812         if (!RB_EMPTY(&vp->v_rbdirty_tree))
813                 panic("Clean vnode still has dirty buffers!");
814         if (!RB_EMPTY(&vp->v_rbclean_tree))
815                 panic("Clean vnode still has clean buffers!");
816         if (!RB_EMPTY(&vp->v_rbhash_tree))
817                 panic("Clean vnode still on hash tree!");
818         KKASSERT(vp->v_mount == NULL);
819 #endif
820         vp->v_flag = VAGE0 | VAGE1;
821         vp->v_lastw = 0;
822         vp->v_lasta = 0;
823         vp->v_cstart = 0;
824         vp->v_clen = 0;
825         vp->v_socket = 0;
826         vp->v_opencount = 0;
827         vp->v_writecount = 0;   /* XXX */
828
829         /*
830          * lktimeout only applies when LK_TIMELOCK is used, and only
831          * the pageout daemon uses it.  The timeout may not be zero
832          * or the pageout daemon can deadlock in low-VM situations.
833          */
834         if (lktimeout == 0)
835                 lktimeout = hz / 10;
836         lockreinit(&vp->v_lock, "vnode", lktimeout, lkflags);
837         KKASSERT(TAILQ_EMPTY(&vp->v_namecache));
838         /* exclusive lock still held */
839
840         /*
841          * Note: sysref needs to be activated to convert -0x40000000 to +1.
842          * The -0x40000000 comes from the last ref on reuse, and from
843          * sysref_init() on allocate.
844          */
845         sysref_activate(&vp->v_sysref);
846         vp->v_filesize = NOOFFSET;
847         vp->v_type = VNON;
848         vp->v_tag = 0;
849         vp->v_ops = NULL;
850         vp->v_data = NULL;
851         KKASSERT(vp->v_mount == NULL);
852
853         return (vp);
854 }
855
856 /*
857  * MPSAFE
858  */
859 int
860 freesomevnodes(int n)
861 {
862         struct vnode *vp;
863         int count = 0;
864
865         while (n) {
866                 --n;
867                 if ((vp = allocfreevnode()) == NULL)
868                         break;
869                 vx_put(vp);
870                 --numvnodes;
871         }
872         return(count);
873 }
874