kernel - fine-grained namecache and partial vnode MPSAFE work
[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 }
112
113 /*
114  * Misc functions
115  */
116 static __inline
117 void
118 _vsetflags(struct vnode *vp, int flags)
119 {
120         atomic_set_int(&vp->v_flag, flags);
121 }
122
123 static __inline
124 void
125 _vclrflags(struct vnode *vp, int flags)
126 {
127         atomic_clear_int(&vp->v_flag, flags);
128 }
129
130 void
131 vsetflags(struct vnode *vp, int flags)
132 {
133         _vsetflags(vp, flags);
134 }
135
136 void
137 vclrflags(struct vnode *vp, int flags)
138 {
139         _vclrflags(vp, flags);
140 }
141
142 /*
143  * Inline helper functions.  vbusy() and vfree() must be called while
144  * vp->v_spinlock is held.
145  *
146  * WARNING!  This functions is typically called with v_spinlock held.
147  *
148  * MPSAFE
149  */
150 static __inline 
151 void
152 __vbusy(struct vnode *vp)
153 {
154 #ifdef TRACKVNODE
155         if ((ulong)vp == trackvnode)
156                 kprintf("__vbusy %p %08x\n", vp, vp->v_flag);
157 #endif
158         spin_lock_wr(&vfs_spin);
159         TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
160         freevnodes--;
161         _vclrflags(vp, VFREE);
162         spin_unlock_wr(&vfs_spin);
163 }
164
165 /*
166  * WARNING!  This functions is typically called with v_spinlock held.
167  *
168  * MPSAFE
169  */
170 static __inline
171 void
172 __vfree(struct vnode *vp)
173 {
174 #ifdef TRACKVNODE
175         if ((ulong)vp == trackvnode) {
176                 kprintf("__vfree %p %08x\n", vp, vp->v_flag);
177                 print_backtrace();
178         }
179 #endif
180         spin_lock_wr(&vfs_spin);
181         if (vp->v_flag & VRECLAIMED)
182                 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
183         else if (vp->v_flag & (VAGE0 | VAGE1))
184                 TAILQ_INSERT_BEFORE(&vnode_free_mid, vp, v_freelist);
185         else
186                 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
187         freevnodes++;
188         _vsetflags(vp, VFREE);
189         spin_unlock_wr(&vfs_spin);
190 }
191
192 /*
193  * WARNING!  This functions is typically called with v_spinlock held.
194  *
195  * MPSAFE
196  */
197 static __inline
198 void
199 __vfreetail(struct vnode *vp)
200 {
201 #ifdef TRACKVNODE
202         if ((ulong)vp == trackvnode)
203                 kprintf("__vfreetail %p %08x\n", vp, vp->v_flag);
204 #endif
205         spin_lock_wr(&vfs_spin);
206         TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
207         freevnodes++;
208         _vsetflags(vp, VFREE);
209         spin_unlock_wr(&vfs_spin);
210 }
211
212 /*
213  * Return a C boolean if we should put the vnode on the freelist (VFREE),
214  * or leave it / mark it as VCACHED.
215  *
216  * This routine is only valid if the vnode is already either VFREE or
217  * VCACHED, or if it can become VFREE or VCACHED via vnode_terminate().
218  *
219  * WARNING!  This functions is typically called with v_spinlock held.
220  *
221  * MPSAFE
222  */
223 static __inline boolean_t
224 vshouldfree(struct vnode *vp)
225 {
226         return (vp->v_auxrefs == 0 &&
227             (vp->v_object == NULL || vp->v_object->resident_page_count == 0));
228 }
229
230 /*
231  * Add a ref to an active vnode.  This function should never be called
232  * with an inactive vnode (use vget() instead).
233  *
234  * MPSAFE
235  */
236 void
237 vref(struct vnode *vp)
238 {
239         KKASSERT(vp->v_sysref.refcnt > 0 && 
240                  (vp->v_flag & (VFREE|VINACTIVE)) == 0);
241         sysref_get(&vp->v_sysref);
242 }
243
244 /*
245  * Release a ref on an active or inactive vnode.  The sysref termination
246  * function will be called when the active last active reference is released,
247  * and the vnode is returned to the objcache when the last inactive
248  * reference is released.
249  */
250 void
251 vrele(struct vnode *vp)
252 {
253         sysref_put(&vp->v_sysref);
254 }
255
256 /*
257  * Add an auxiliary data structure reference to the vnode.  Auxiliary
258  * references do not change the state of the vnode or prevent them
259  * from being deactivated, reclaimed, or placed on the free list.
260  *
261  * An auxiliary reference DOES prevent the vnode from being destroyed,
262  * allowing you to vx_lock() it, test state, etc.
263  *
264  * An auxiliary reference DOES NOT move a vnode out of the VFREE state
265  * once it has entered it.
266  *
267  * MPSAFE
268  */
269 void
270 vhold(struct vnode *vp)
271 {
272         KKASSERT(vp->v_sysref.refcnt != 0);
273         atomic_add_int(&vp->v_auxrefs, 1);
274 }
275
276 /*
277  * Remove an auxiliary reference from the vnode.
278  *
279  * vdrop needs to check for a VCACHE->VFREE transition to catch cases
280  * where a vnode is held past its reclamation.
281  *
282  * MPSAFE
283  */
284 void
285 vdrop(struct vnode *vp)
286 {
287         KKASSERT(vp->v_sysref.refcnt != 0 && vp->v_auxrefs > 0);
288         spin_lock_wr(&vp->v_spinlock);
289         atomic_subtract_int(&vp->v_auxrefs, 1);
290         if ((vp->v_flag & VCACHED) && vshouldfree(vp)) {
291                 _vclrflags(vp, VCACHED);
292                 __vfree(vp);
293         }
294         spin_unlock_wr(&vp->v_spinlock);
295 }
296
297 /*
298  * This function is called when the last active reference on the vnode
299  * is released, typically via vrele().  SYSREF will give the vnode a
300  * negative ref count, indicating that it is undergoing termination or
301  * is being set aside for the cache, and one final sysref_put() is
302  * required to actually return it to the memory subsystem.
303  *
304  * However, because vnodes may have auxiliary structural references via
305  * v_auxrefs, we must interlock auxiliary references against termination
306  * via the VX lock mechanism.  It is possible for a vnode to be reactivated
307  * while we were blocked on the lock.
308  *
309  * MPSAFE
310  */
311 void
312 vnode_terminate(struct vnode *vp)
313 {
314         vx_lock(vp);
315         if (sysref_isinactive(&vp->v_sysref)) {
316                 /*
317                  * Deactivate the vnode by marking it VFREE or VCACHED.
318                  * The vnode can be reactivated from either state until
319                  * reclaimed.  These states inherit the 'last' sysref on the
320                  * vnode.
321                  *
322                  * NOTE: There may be additional inactive references from
323                  * other entities blocking on the VX lock while we hold it,
324                  * but this does not prevent us from changing the vnode's
325                  * state.
326                  *
327                  * NOTE: The vnode could already be marked inactive.  XXX
328                  *       how?
329                  *
330                  * NOTE: v_mount may be NULL due to assignment to
331                  *       dead_vnode_vops
332                  *
333                  * NOTE: The vnode may be marked inactive with dirty buffers
334                  *       or dirty pages in its cached VM object still present.
335                  */
336                 if ((vp->v_flag & VINACTIVE) == 0) {
337                         _vsetflags(vp, VINACTIVE);
338                         if (vp->v_mount)
339                                 VOP_INACTIVE(vp);
340                 }
341                 spin_lock_wr(&vp->v_spinlock);
342                 KKASSERT((vp->v_flag & (VFREE|VCACHED)) == 0);
343                 if (vshouldfree(vp))
344                         __vfree(vp);
345                 else
346                         _vsetflags(vp, VCACHED); /* inactive but not yet free*/
347                 spin_unlock_wr(&vp->v_spinlock);
348                 vx_unlock(vp);
349         } else {
350                 /*
351                  * Someone reactivated the vnode while were blocked on the
352                  * VX lock.  Release the VX lock and release the (now active)
353                  * last reference which is no longer last.
354                  */
355                 vx_unlock(vp);
356                 vrele(vp);
357         }
358 }
359
360 /*
361  * Physical vnode constructor / destructor.  These are only executed on
362  * the backend of the objcache.  They are NOT executed on every vnode
363  * allocation or deallocation.
364  *
365  * MPSAFE
366  */
367 boolean_t
368 vnode_ctor(void *obj, void *private, int ocflags)
369 {
370         struct vnode *vp = obj;
371
372         lwkt_token_init(&vp->v_token);
373         lockinit(&vp->v_lock, "vnode", 0, 0);
374         ccms_dataspace_init(&vp->v_ccms);
375         TAILQ_INIT(&vp->v_namecache);
376         RB_INIT(&vp->v_rbclean_tree);
377         RB_INIT(&vp->v_rbdirty_tree);
378         RB_INIT(&vp->v_rbhash_tree);
379         return(TRUE);
380 }
381
382 /*
383  * MPSAFE
384  */
385 void
386 vnode_dtor(void *obj, void *private)
387 {
388         struct vnode *vp = obj;
389
390         ccms_dataspace_destroy(&vp->v_ccms);
391 }
392
393 /****************************************************************
394  *                      VX LOCKING FUNCTIONS                    *
395  ****************************************************************
396  *
397  * These functions lock vnodes for reclamation and deactivation related
398  * activities.  The caller must already be holding some sort of reference
399  * on the vnode.
400  *
401  * MPSAFE
402  */
403 void
404 vx_lock(struct vnode *vp)
405 {
406         lockmgr(&vp->v_lock, LK_EXCLUSIVE);
407 }
408
409 static int
410 vx_lock_nonblock(struct vnode *vp)
411 {
412         return(lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT));
413 }
414
415 void
416 vx_unlock(struct vnode *vp)
417 {
418         lockmgr(&vp->v_lock, LK_RELEASE);
419 }
420
421 /****************************************************************
422  *                      VNODE ACQUISITION FUNCTIONS             *
423  ****************************************************************
424  *
425  * These functions must be used when accessing a vnode via an auxiliary
426  * reference such as the namecache or free list, or when you wish to
427  * do a combo ref+lock sequence.
428  *
429  * These functions are MANDATORY for any code chain accessing a vnode
430  * whos activation state is not known.
431  *
432  * vget() can be called with LK_NOWAIT and will return EBUSY if the
433  * lock cannot be immediately acquired.
434  *
435  * vget()/vput() are used when reactivation is desired.
436  *
437  * vx_get() and vx_put() are used when reactivation is not desired.
438  */
439 int
440 vget(struct vnode *vp, int flags)
441 {
442         int error;
443
444         /*
445          * A lock type must be passed
446          */
447         if ((flags & LK_TYPE_MASK) == 0) {
448                 panic("vget() called with no lock specified!");
449                 /* NOT REACHED */
450         }
451
452         /*
453          * Reference the structure and then acquire the lock.  0->1
454          * transitions and refs during termination are allowed here so
455          * call sysref directly.
456          */
457         sysref_get(&vp->v_sysref);
458         if ((error = vn_lock(vp, flags)) != 0) {
459                 /*
460                  * The lock failed, undo and return an error.
461                  */
462                 sysref_put(&vp->v_sysref);
463         } else if (vp->v_flag & VRECLAIMED) {
464                 /*
465                  * The node is being reclaimed and cannot be reactivated
466                  * any more, undo and return ENOENT.
467                  */
468                 vn_unlock(vp);
469                 vrele(vp);
470                 error = ENOENT;
471         } else {
472                 /*
473                  * If the vnode is marked VFREE or VCACHED it needs to be
474                  * reactivated, otherwise it had better already be active.
475                  * VINACTIVE must also be cleared.
476                  *
477                  * In the VFREE/VCACHED case we have to throw away the
478                  * sysref that was earmarking those cases and preventing
479                  * the vnode from being destroyed.  Our sysref is still held.
480                  */
481                 spin_lock_wr(&vp->v_spinlock);
482                 if (vp->v_flag & VFREE) {
483                         __vbusy(vp);
484                         spin_unlock_wr(&vp->v_spinlock);
485                         sysref_put(&vp->v_sysref);
486                         sysref_activate(&vp->v_sysref);
487                 } else if (vp->v_flag & VCACHED) {
488                         _vclrflags(vp, VCACHED);
489                         spin_unlock_wr(&vp->v_spinlock);
490                         sysref_put(&vp->v_sysref);
491                         sysref_activate(&vp->v_sysref);
492                 } else {
493                         spin_unlock_wr(&vp->v_spinlock);
494                         if (sysref_isinactive(&vp->v_sysref)) {
495                                 sysref_activate(&vp->v_sysref);
496                                 kprintf("Warning vp %p reactivation race\n",
497                                         vp);
498                         }
499                 }
500                 _vclrflags(vp, VINACTIVE);
501                 error = 0;
502         }
503         return(error);
504 }
505
506 /*
507  * MPSAFE
508  */
509 void
510 vput(struct vnode *vp)
511 {
512         vn_unlock(vp);
513         vrele(vp);
514 }
515
516 /*
517  * XXX The vx_*() locks should use auxrefs, not the main reference counter.
518  *
519  * MPSAFE
520  */
521 void
522 vx_get(struct vnode *vp)
523 {
524         sysref_get(&vp->v_sysref);
525         lockmgr(&vp->v_lock, LK_EXCLUSIVE);
526 }
527
528 /*
529  * MPSAFE
530  */
531 int
532 vx_get_nonblock(struct vnode *vp)
533 {
534         int error;
535
536         sysref_get(&vp->v_sysref);
537         error = lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT);
538         if (error)
539                 sysref_put(&vp->v_sysref);
540         return(error);
541 }
542
543 /*
544  * Relase a VX lock that also held a ref on the vnode.
545  *
546  * vx_put needs to check for a VCACHE->VFREE transition to catch the
547  * case where e.g. vnlru issues a vgone*().
548  *
549  * MPSAFE
550  */
551 void
552 vx_put(struct vnode *vp)
553 {
554         spin_lock_wr(&vp->v_spinlock);
555         if ((vp->v_flag & VCACHED) && vshouldfree(vp)) {
556                 _vclrflags(vp, VCACHED);
557                 __vfree(vp);
558         }
559         spin_unlock_wr(&vp->v_spinlock);
560         lockmgr(&vp->v_lock, LK_RELEASE);
561         sysref_put(&vp->v_sysref);
562 }
563
564 /*
565  * Try to reuse a vnode from the free list.  NOTE: The returned vnode
566  * is not completely initialized.
567  *
568  * MPSAFE
569  */
570 static
571 struct vnode *
572 allocfreevnode(void)
573 {
574         struct vnode *vp;
575         int count;
576
577         for (count = 0; count < freevnodes; count++) {
578                 /*
579                  * Note that regardless of how we block in this loop,
580                  * we only get here if freevnodes != 0 so there
581                  * had better be something on the list.
582                  *
583                  * Try to lock the first vnode on the free list.
584                  * Cycle if we can't.
585                  *
586                  * XXX NOT MP SAFE
587                  */
588                 spin_lock_wr(&vfs_spin);
589                 vp = TAILQ_FIRST(&vnode_free_list);
590                 if (vp == &vnode_free_mid)
591                         vp = TAILQ_NEXT(vp, v_freelist);
592                 if (vx_lock_nonblock(vp)) {
593                         KKASSERT(vp->v_flag & VFREE);
594                         TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
595                         TAILQ_INSERT_TAIL(&vnode_free_list,
596                                           vp, v_freelist);
597                         spin_unlock_wr(&vfs_spin);
598                         continue;
599                 }
600                 spin_unlock_wr(&vfs_spin);
601 #ifdef TRACKVNODE
602                 if ((ulong)vp == trackvnode)
603                         kprintf("allocfreevnode %p %08x\n", vp, vp->v_flag);
604 #endif
605
606                 /*
607                  * With the vnode locked we can safely remove it
608                  * from the free list.  We inherit the reference
609                  * that was previously associated with the vnode
610                  * being on the free list.
611                  */
612                 KKASSERT((vp->v_flag & (VFREE|VINACTIVE)) ==
613                           (VFREE|VINACTIVE));
614                 KKASSERT(sysref_isinactive(&vp->v_sysref));
615                 __vbusy(vp);
616
617                 /*
618                  * Holding the VX lock on an inactive vnode prevents it
619                  * from being reactivated or reused.  New namecache
620                  * associations can only be made using active vnodes.
621                  *
622                  * Another thread may be blocked on our vnode lock while
623                  * holding a namecache lock.  We can only reuse this vnode
624                  * if we can clear all namecache associations without
625                  * blocking.
626                  */
627                 if ((vp->v_flag & VRECLAIMED) == 0) {
628                         if (cache_inval_vp_nonblock(vp)) {
629                                 __vfreetail(vp);
630                                 vx_unlock(vp);
631                                 continue;
632                         }
633                         vgone_vxlocked(vp);
634                         /* vnode is still VX locked */
635                 }
636
637                 /*
638                  * We can reuse the vnode if no primary or auxiliary
639                  * references remain other then ours, else put it
640                  * back on the free list and keep looking.
641                  *
642                  * Either the free list inherits the last reference
643                  * or we fall through and sysref_activate() the last
644                  * reference.
645                  *
646                  * Since the vnode is in a VRECLAIMED state, no new
647                  * namecache associations could have been made.
648                  */
649                 KKASSERT(TAILQ_EMPTY(&vp->v_namecache));
650                 if (vp->v_auxrefs ||
651                     !sysref_islastdeactivation(&vp->v_sysref)) {
652                         __vfreetail(vp);
653                         vx_unlock(vp);
654                         continue;
655                 }
656
657                 /*
658                  * Return a VX locked vnode suitable for reuse.  The caller
659                  * inherits the sysref.
660                  */
661                 return(vp);
662         }
663         return(NULL);
664 }
665
666 /*
667  * Obtain a new vnode from the freelist, allocating more if necessary.
668  * The returned vnode is VX locked & refd.
669  *
670  * All new vnodes set the VAGE flags.  An open() of the vnode will
671  * decrement the (2-bit) flags.  Vnodes which are opened several times
672  * are thus retained in the cache over vnodes which are merely stat()d.
673  *
674  * MPSAFE
675  */
676 struct vnode *
677 allocvnode(int lktimeout, int lkflags)
678 {
679         struct vnode *vp;
680
681         /*
682          * Try to reuse vnodes if we hit the max.  This situation only
683          * occurs in certain large-memory (2G+) situations.  We cannot
684          * attempt to directly reclaim vnodes due to nasty recursion
685          * problems.
686          */
687         while (numvnodes - freevnodes > desiredvnodes)
688                 vnlru_proc_wait();
689
690         /*
691          * Try to build up as many vnodes as we can before reallocating
692          * from the free list.  A vnode on the free list simply means
693          * that it is inactive with no resident pages.  It may or may not
694          * have been reclaimed and could have valuable information associated 
695          * with it that we shouldn't throw away unless we really need to.
696          *
697          * HAMMER NOTE: Re-establishing a vnode is a fairly expensive
698          * operation for HAMMER but this should benefit UFS as well.
699          */
700         if (freevnodes >= wantfreevnodes && numvnodes >= desiredvnodes)
701                 vp = allocfreevnode();
702         else
703                 vp = NULL;
704         if (vp == NULL) {
705                 vp = sysref_alloc(&vnode_sysref_class);
706                 lockmgr(&vp->v_lock, LK_EXCLUSIVE);
707                 numvnodes++;
708         }
709
710         /*
711          * We are using a managed sysref class, vnode fields are only
712          * zerod on initial allocation from the backing store, not
713          * on reallocation.  Thus we have to clear these fields for both
714          * reallocation and reuse.
715          */
716 #ifdef INVARIANTS
717         if (vp->v_data)
718                 panic("cleaned vnode isn't");
719         if (bio_track_active(&vp->v_track_read) ||
720             bio_track_active(&vp->v_track_write)) {
721                 panic("Clean vnode has pending I/O's");
722         }
723         if (vp->v_flag & VONWORKLST)
724                 panic("Clean vnode still pending on syncer worklist!");
725         if (!RB_EMPTY(&vp->v_rbdirty_tree))
726                 panic("Clean vnode still has dirty buffers!");
727         if (!RB_EMPTY(&vp->v_rbclean_tree))
728                 panic("Clean vnode still has clean buffers!");
729         if (!RB_EMPTY(&vp->v_rbhash_tree))
730                 panic("Clean vnode still on hash tree!");
731         KKASSERT(vp->v_mount == NULL);
732 #endif
733         vp->v_flag = VAGE0 | VAGE1;
734         vp->v_lastw = 0;
735         vp->v_lasta = 0;
736         vp->v_cstart = 0;
737         vp->v_clen = 0;
738         vp->v_socket = 0;
739         vp->v_opencount = 0;
740         vp->v_writecount = 0;   /* XXX */
741
742         /*
743          * lktimeout only applies when LK_TIMELOCK is used, and only
744          * the pageout daemon uses it.  The timeout may not be zero
745          * or the pageout daemon can deadlock in low-VM situations.
746          */
747         if (lktimeout == 0)
748                 lktimeout = hz / 10;
749         lockreinit(&vp->v_lock, "vnode", lktimeout, lkflags);
750         KKASSERT(TAILQ_EMPTY(&vp->v_namecache));
751         /* exclusive lock still held */
752
753         /*
754          * Note: sysref needs to be activated to convert -0x40000000 to +1.
755          * The -0x40000000 comes from the last ref on reuse, and from
756          * sysref_init() on allocate.
757          */
758         sysref_activate(&vp->v_sysref);
759         vp->v_filesize = NOOFFSET;
760         vp->v_type = VNON;
761         vp->v_tag = 0;
762         vp->v_ops = NULL;
763         vp->v_data = NULL;
764         KKASSERT(vp->v_mount == NULL);
765
766         return (vp);
767 }
768
769 /*
770  * MPSAFE
771  */
772 int
773 freesomevnodes(int n)
774 {
775         struct vnode *vp;
776         int count = 0;
777
778         while (n) {
779                 --n;
780                 if ((vp = allocfreevnode()) == NULL)
781                         break;
782                 vx_put(vp);
783                 --numvnodes;
784         }
785         return(count);
786 }
787