vfs_vnode.c revision 1.118 1 /* $NetBSD: vfs_vnode.c,v 1.118 2020/04/04 20:54:42 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2011, 2019, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
19 *
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.
31 */
32
33 /*
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.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
67 */
68
69 /*
70 * The vnode cache subsystem.
71 *
72 * Life-cycle
73 *
74 * Normally, there are two points where new vnodes are created:
75 * VOP_CREATE(9) and VOP_LOOKUP(9). The life-cycle of a vnode
76 * starts in one of the following ways:
77 *
78 * - Allocation, via vcache_get(9) or vcache_new(9).
79 * - Reclamation of inactive vnode, via vcache_vget(9).
80 *
81 * Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9)
82 * was another, traditional way. Currently, only the draining thread
83 * recycles the vnodes. This behaviour might be revisited.
84 *
85 * The life-cycle ends when the last reference is dropped, usually
86 * in VOP_REMOVE(9). In such case, VOP_INACTIVE(9) is called to inform
87 * the file system that vnode is inactive. Via this call, file system
88 * indicates whether vnode can be recycled (usually, it checks its own
89 * references, e.g. count of links, whether the file was removed).
90 *
91 * Depending on indication, vnode can be put into a free list (cache),
92 * or cleaned via vcache_reclaim, which calls VOP_RECLAIM(9) to
93 * disassociate underlying file system from the vnode, and finally
94 * destroyed.
95 *
96 * Vnode state
97 *
98 * Vnode is always in one of six states:
99 * - MARKER This is a marker vnode to help list traversal. It
100 * will never change its state.
101 * - LOADING Vnode is associating underlying file system and not
102 * yet ready to use.
103 * - LOADED Vnode has associated underlying file system and is
104 * ready to use.
105 * - BLOCKED Vnode is active but cannot get new references.
106 * - RECLAIMING Vnode is disassociating from the underlying file
107 * system.
108 * - RECLAIMED Vnode has disassociated from underlying file system
109 * and is dead.
110 *
111 * Valid state changes are:
112 * LOADING -> LOADED
113 * Vnode has been initialised in vcache_get() or
114 * vcache_new() and is ready to use.
115 * LOADED -> RECLAIMING
116 * Vnode starts disassociation from underlying file
117 * system in vcache_reclaim().
118 * RECLAIMING -> RECLAIMED
119 * Vnode finished disassociation from underlying file
120 * system in vcache_reclaim().
121 * LOADED -> BLOCKED
122 * Either vcache_rekey*() is changing the vnode key or
123 * vrelel() is about to call VOP_INACTIVE().
124 * BLOCKED -> LOADED
125 * The block condition is over.
126 * LOADING -> RECLAIMED
127 * Either vcache_get() or vcache_new() failed to
128 * associate the underlying file system or vcache_rekey*()
129 * drops a vnode used as placeholder.
130 *
131 * Of these states LOADING, BLOCKED and RECLAIMING are intermediate
132 * and it is possible to wait for state change.
133 *
134 * State is protected with v_interlock with one exception:
135 * to change from LOADING both v_interlock and vcache_lock must be held
136 * so it is possible to check "state == LOADING" without holding
137 * v_interlock. See vcache_get() for details.
138 *
139 * Reference counting
140 *
141 * Vnode is considered active, if reference count (vnode_t::v_usecount)
142 * is non-zero. It is maintained using: vref(9) and vrele(9), as well
143 * as vput(9), routines. Common points holding references are e.g.
144 * file openings, current working directory, mount points, etc.
145 *
146 * Note on v_usecount and its locking
147 *
148 * At nearly all points it is known that v_usecount could be zero,
149 * the vnode_t::v_interlock will be held. To change the count away
150 * from zero, the interlock must be held. To change from a non-zero
151 * value to zero, again the interlock must be held.
152 *
153 * Changing the usecount from a non-zero value to a non-zero value can
154 * safely be done using atomic operations, without the interlock held.
155 */
156
157 #include <sys/cdefs.h>
158 __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.118 2020/04/04 20:54:42 ad Exp $");
159
160 #ifdef _KERNEL_OPT
161 #include "opt_pax.h"
162 #endif
163
164 #include <sys/param.h>
165 #include <sys/kernel.h>
166
167 #include <sys/atomic.h>
168 #include <sys/buf.h>
169 #include <sys/conf.h>
170 #include <sys/device.h>
171 #include <sys/hash.h>
172 #include <sys/kauth.h>
173 #include <sys/kmem.h>
174 #include <sys/kthread.h>
175 #include <sys/module.h>
176 #include <sys/mount.h>
177 #include <sys/namei.h>
178 #include <sys/pax.h>
179 #include <sys/syscallargs.h>
180 #include <sys/sysctl.h>
181 #include <sys/systm.h>
182 #include <sys/vnode_impl.h>
183 #include <sys/wapbl.h>
184 #include <sys/fstrans.h>
185
186 #include <uvm/uvm.h>
187 #include <uvm/uvm_readahead.h>
188 #include <uvm/uvm_stat.h>
189
190 /* Flags to vrelel. */
191 #define VRELEL_ASYNC 0x0001 /* Always defer to vrele thread. */
192
193 #define LRU_VRELE 0
194 #define LRU_FREE 1
195 #define LRU_HOLD 2
196 #define LRU_COUNT 3
197
198 /*
199 * There are three lru lists: one holds vnodes waiting for async release,
200 * one is for vnodes which have no buffer/page references and one for those
201 * which do (i.e. v_holdcnt is non-zero). We put the lists into a single,
202 * private cache line as vnodes migrate between them while under the same
203 * lock (vdrain_lock).
204 */
205 u_int numvnodes __cacheline_aligned;
206 static vnodelst_t lru_list[LRU_COUNT] __cacheline_aligned;
207 static kmutex_t vdrain_lock __cacheline_aligned;
208 static kcondvar_t vdrain_cv;
209 static int vdrain_gen;
210 static kcondvar_t vdrain_gen_cv;
211 static bool vdrain_retry;
212 static lwp_t * vdrain_lwp;
213 SLIST_HEAD(hashhead, vnode_impl);
214 static kmutex_t vcache_lock __cacheline_aligned;
215 static kcondvar_t vcache_cv;
216 static u_int vcache_hashsize;
217 static u_long vcache_hashmask;
218 static struct hashhead *vcache_hashtab;
219 static pool_cache_t vcache_pool;
220 static void lru_requeue(vnode_t *, vnodelst_t *);
221 static vnodelst_t * lru_which(vnode_t *);
222 static vnode_impl_t * vcache_alloc(void);
223 static void vcache_dealloc(vnode_impl_t *);
224 static void vcache_free(vnode_impl_t *);
225 static void vcache_init(void);
226 static void vcache_reinit(void);
227 static void vcache_reclaim(vnode_t *);
228 static void vrelel(vnode_t *, int, int);
229 static void vdrain_thread(void *);
230 static void vnpanic(vnode_t *, const char *, ...)
231 __printflike(2, 3);
232
233 /* Routines having to do with the management of the vnode table. */
234 extern struct mount *dead_rootmount;
235 extern int (**dead_vnodeop_p)(void *);
236 extern int (**spec_vnodeop_p)(void *);
237 extern struct vfsops dead_vfsops;
238
239 /* Vnode state operations and diagnostics. */
240
241 #if defined(DIAGNOSTIC)
242
243 #define VSTATE_VALID(state) \
244 ((state) != VS_ACTIVE && (state) != VS_MARKER)
245 #define VSTATE_GET(vp) \
246 vstate_assert_get((vp), __func__, __LINE__)
247 #define VSTATE_CHANGE(vp, from, to) \
248 vstate_assert_change((vp), (from), (to), __func__, __LINE__)
249 #define VSTATE_WAIT_STABLE(vp) \
250 vstate_assert_wait_stable((vp), __func__, __LINE__)
251
252 void
253 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line,
254 bool has_lock)
255 {
256 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
257
258 if (!has_lock) {
259 /*
260 * Prevent predictive loads from the CPU, but check the state
261 * without loooking first.
262 */
263 membar_enter();
264 if (state == VS_ACTIVE && vp->v_usecount > 0 &&
265 (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED))
266 return;
267 if (vip->vi_state == state)
268 return;
269 mutex_enter((vp)->v_interlock);
270 }
271
272 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
273
274 if ((state == VS_ACTIVE && vp->v_usecount > 0 &&
275 (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED)) ||
276 vip->vi_state == state) {
277 if (!has_lock)
278 mutex_exit((vp)->v_interlock);
279 return;
280 }
281 vnpanic(vp, "state is %s, usecount %d, expected %s at %s:%d",
282 vstate_name(vip->vi_state), vp->v_usecount,
283 vstate_name(state), func, line);
284 }
285
286 static enum vnode_state
287 vstate_assert_get(vnode_t *vp, const char *func, int line)
288 {
289 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
290
291 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
292 if (! VSTATE_VALID(vip->vi_state))
293 vnpanic(vp, "state is %s at %s:%d",
294 vstate_name(vip->vi_state), func, line);
295
296 return vip->vi_state;
297 }
298
299 static void
300 vstate_assert_wait_stable(vnode_t *vp, const char *func, int line)
301 {
302 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
303
304 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
305 if (! VSTATE_VALID(vip->vi_state))
306 vnpanic(vp, "state is %s at %s:%d",
307 vstate_name(vip->vi_state), func, line);
308
309 while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED)
310 cv_wait(&vp->v_cv, vp->v_interlock);
311
312 if (! VSTATE_VALID(vip->vi_state))
313 vnpanic(vp, "state is %s at %s:%d",
314 vstate_name(vip->vi_state), func, line);
315 }
316
317 static void
318 vstate_assert_change(vnode_t *vp, enum vnode_state from, enum vnode_state to,
319 const char *func, int line)
320 {
321 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
322
323 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
324 if (from == VS_LOADING)
325 KASSERTMSG(mutex_owned(&vcache_lock), "at %s:%d", func, line);
326
327 if (! VSTATE_VALID(from))
328 vnpanic(vp, "from is %s at %s:%d",
329 vstate_name(from), func, line);
330 if (! VSTATE_VALID(to))
331 vnpanic(vp, "to is %s at %s:%d",
332 vstate_name(to), func, line);
333 if (vip->vi_state != from)
334 vnpanic(vp, "from is %s, expected %s at %s:%d\n",
335 vstate_name(vip->vi_state), vstate_name(from), func, line);
336 if ((from == VS_BLOCKED || to == VS_BLOCKED) && vp->v_usecount != 1)
337 vnpanic(vp, "%s to %s with usecount %d at %s:%d",
338 vstate_name(from), vstate_name(to), vp->v_usecount,
339 func, line);
340
341 vip->vi_state = to;
342 if (from == VS_LOADING)
343 cv_broadcast(&vcache_cv);
344 if (to == VS_LOADED || to == VS_RECLAIMED)
345 cv_broadcast(&vp->v_cv);
346 }
347
348 #else /* defined(DIAGNOSTIC) */
349
350 #define VSTATE_GET(vp) \
351 (VNODE_TO_VIMPL((vp))->vi_state)
352 #define VSTATE_CHANGE(vp, from, to) \
353 vstate_change((vp), (from), (to))
354 #define VSTATE_WAIT_STABLE(vp) \
355 vstate_wait_stable((vp))
356 void
357 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line,
358 bool has_lock)
359 {
360
361 }
362
363 static void
364 vstate_wait_stable(vnode_t *vp)
365 {
366 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
367
368 while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED)
369 cv_wait(&vp->v_cv, vp->v_interlock);
370 }
371
372 static void
373 vstate_change(vnode_t *vp, enum vnode_state from, enum vnode_state to)
374 {
375 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
376
377 vip->vi_state = to;
378 if (from == VS_LOADING)
379 cv_broadcast(&vcache_cv);
380 if (to == VS_LOADED || to == VS_RECLAIMED)
381 cv_broadcast(&vp->v_cv);
382 }
383
384 #endif /* defined(DIAGNOSTIC) */
385
386 void
387 vfs_vnode_sysinit(void)
388 {
389 int error __diagused, i;
390
391 dead_rootmount = vfs_mountalloc(&dead_vfsops, NULL);
392 KASSERT(dead_rootmount != NULL);
393 dead_rootmount->mnt_iflag |= IMNT_MPSAFE;
394
395 mutex_init(&vdrain_lock, MUTEX_DEFAULT, IPL_NONE);
396 for (i = 0; i < LRU_COUNT; i++) {
397 TAILQ_INIT(&lru_list[i]);
398 }
399 vcache_init();
400
401 cv_init(&vdrain_cv, "vdrain");
402 cv_init(&vdrain_gen_cv, "vdrainwt");
403 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread,
404 NULL, &vdrain_lwp, "vdrain");
405 KASSERTMSG((error == 0), "kthread_create(vdrain) failed: %d", error);
406 }
407
408 /*
409 * Allocate a new marker vnode.
410 */
411 vnode_t *
412 vnalloc_marker(struct mount *mp)
413 {
414 vnode_impl_t *vip;
415 vnode_t *vp;
416
417 vip = pool_cache_get(vcache_pool, PR_WAITOK);
418 memset(vip, 0, sizeof(*vip));
419 vp = VIMPL_TO_VNODE(vip);
420 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 1);
421 vp->v_mount = mp;
422 vp->v_type = VBAD;
423 vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
424 vip->vi_state = VS_MARKER;
425
426 return vp;
427 }
428
429 /*
430 * Free a marker vnode.
431 */
432 void
433 vnfree_marker(vnode_t *vp)
434 {
435 vnode_impl_t *vip;
436
437 vip = VNODE_TO_VIMPL(vp);
438 KASSERT(vip->vi_state == VS_MARKER);
439 mutex_obj_free(vp->v_interlock);
440 uvm_obj_destroy(&vp->v_uobj, true);
441 pool_cache_put(vcache_pool, vip);
442 }
443
444 /*
445 * Test a vnode for being a marker vnode.
446 */
447 bool
448 vnis_marker(vnode_t *vp)
449 {
450
451 return (VNODE_TO_VIMPL(vp)->vi_state == VS_MARKER);
452 }
453
454 /*
455 * Return the lru list this node should be on.
456 */
457 static vnodelst_t *
458 lru_which(vnode_t *vp)
459 {
460
461 KASSERT(mutex_owned(vp->v_interlock));
462
463 if (vp->v_holdcnt > 0)
464 return &lru_list[LRU_HOLD];
465 else
466 return &lru_list[LRU_FREE];
467 }
468
469 /*
470 * Put vnode to end of given list.
471 * Both the current and the new list may be NULL, used on vnode alloc/free.
472 * Adjust numvnodes and signal vdrain thread if there is work.
473 */
474 static void
475 lru_requeue(vnode_t *vp, vnodelst_t *listhd)
476 {
477 vnode_impl_t *vip;
478 int d;
479
480 /*
481 * If the vnode is on the correct list, and was put there recently,
482 * then leave it be, thus avoiding huge cache and lock contention.
483 */
484 vip = VNODE_TO_VIMPL(vp);
485 if (listhd == vip->vi_lrulisthd &&
486 (hardclock_ticks - vip->vi_lrulisttm) < hz) {
487 return;
488 }
489
490 mutex_enter(&vdrain_lock);
491 d = 0;
492 if (vip->vi_lrulisthd != NULL)
493 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
494 else
495 d++;
496 vip->vi_lrulisthd = listhd;
497 vip->vi_lrulisttm = hardclock_ticks;
498 if (vip->vi_lrulisthd != NULL)
499 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
500 else
501 d--;
502 if (d != 0) {
503 /*
504 * Looks strange? This is not a bug. Don't store
505 * numvnodes unless there is a change - avoid false
506 * sharing on MP.
507 */
508 numvnodes += d;
509 }
510 if (numvnodes > desiredvnodes || listhd == &lru_list[LRU_VRELE])
511 cv_broadcast(&vdrain_cv);
512 mutex_exit(&vdrain_lock);
513 }
514
515 /*
516 * Release deferred vrele vnodes for this mount.
517 * Called with file system suspended.
518 */
519 void
520 vrele_flush(struct mount *mp)
521 {
522 vnode_impl_t *vip, *marker;
523 vnode_t *vp;
524
525 KASSERT(fstrans_is_owner(mp));
526
527 marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
528
529 mutex_enter(&vdrain_lock);
530 TAILQ_INSERT_HEAD(&lru_list[LRU_VRELE], marker, vi_lrulist);
531
532 while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
533 TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist);
534 TAILQ_INSERT_AFTER(&lru_list[LRU_VRELE], vip, marker,
535 vi_lrulist);
536 vp = VIMPL_TO_VNODE(vip);
537 if (vnis_marker(vp))
538 continue;
539
540 KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]);
541 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
542 vip->vi_lrulisthd = &lru_list[LRU_HOLD];
543 vip->vi_lrulisttm = hardclock_ticks;
544 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
545 mutex_exit(&vdrain_lock);
546
547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
548 mutex_enter(vp->v_interlock);
549 vrelel(vp, 0, LK_EXCLUSIVE);
550
551 mutex_enter(&vdrain_lock);
552 }
553
554 TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist);
555 mutex_exit(&vdrain_lock);
556
557 vnfree_marker(VIMPL_TO_VNODE(marker));
558 }
559
560 /*
561 * Reclaim a cached vnode. Used from vdrain_thread only.
562 */
563 static __inline void
564 vdrain_remove(vnode_t *vp)
565 {
566 struct mount *mp;
567
568 KASSERT(mutex_owned(&vdrain_lock));
569
570 /* Probe usecount (unlocked). */
571 if (vp->v_usecount > 0)
572 return;
573 /* Try v_interlock -- we lock the wrong direction! */
574 if (!mutex_tryenter(vp->v_interlock))
575 return;
576 /* Probe usecount and state. */
577 if (vp->v_usecount > 0 || VSTATE_GET(vp) != VS_LOADED) {
578 mutex_exit(vp->v_interlock);
579 return;
580 }
581 mp = vp->v_mount;
582 if (fstrans_start_nowait(mp) != 0) {
583 mutex_exit(vp->v_interlock);
584 return;
585 }
586 vdrain_retry = true;
587 mutex_exit(&vdrain_lock);
588
589 if (vcache_vget(vp) == 0) {
590 if (!vrecycle(vp)) {
591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
592 mutex_enter(vp->v_interlock);
593 vrelel(vp, 0, LK_EXCLUSIVE);
594 }
595 }
596 fstrans_done(mp);
597
598 mutex_enter(&vdrain_lock);
599 }
600
601 /*
602 * Release a cached vnode. Used from vdrain_thread only.
603 */
604 static __inline void
605 vdrain_vrele(vnode_t *vp)
606 {
607 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
608 struct mount *mp;
609
610 KASSERT(mutex_owned(&vdrain_lock));
611
612 mp = vp->v_mount;
613 if (fstrans_start_nowait(mp) != 0)
614 return;
615
616 /*
617 * First remove the vnode from the vrele list.
618 * Put it on the last lru list, the last vrele()
619 * will put it back onto the right list before
620 * its v_usecount reaches zero.
621 */
622 KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]);
623 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
624 vip->vi_lrulisthd = &lru_list[LRU_HOLD];
625 vip->vi_lrulisttm = hardclock_ticks;
626 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
627
628 vdrain_retry = true;
629 mutex_exit(&vdrain_lock);
630
631 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
632 mutex_enter(vp->v_interlock);
633 vrelel(vp, 0, LK_EXCLUSIVE);
634 fstrans_done(mp);
635
636 mutex_enter(&vdrain_lock);
637 }
638
639 /*
640 * Helper thread to keep the number of vnodes below desiredvnodes
641 * and release vnodes from asynchronous vrele.
642 */
643 static void
644 vdrain_thread(void *cookie)
645 {
646 int i;
647 u_int target;
648 vnode_impl_t *vip, *marker;
649
650 marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
651
652 mutex_enter(&vdrain_lock);
653
654 for (;;) {
655 vdrain_retry = false;
656 target = desiredvnodes - desiredvnodes/10;
657
658 for (i = 0; i < LRU_COUNT; i++) {
659 TAILQ_INSERT_HEAD(&lru_list[i], marker, vi_lrulist);
660 while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
661 TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist);
662 TAILQ_INSERT_AFTER(&lru_list[i], vip, marker,
663 vi_lrulist);
664 if (vnis_marker(VIMPL_TO_VNODE(vip)))
665 continue;
666 if (i == LRU_VRELE)
667 vdrain_vrele(VIMPL_TO_VNODE(vip));
668 else if (numvnodes < target)
669 break;
670 else
671 vdrain_remove(VIMPL_TO_VNODE(vip));
672 }
673 TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist);
674 }
675
676 if (vdrain_retry) {
677 mutex_exit(&vdrain_lock);
678 yield();
679 mutex_enter(&vdrain_lock);
680 } else {
681 vdrain_gen++;
682 cv_broadcast(&vdrain_gen_cv);
683 cv_wait(&vdrain_cv, &vdrain_lock);
684 }
685 }
686 }
687
688 /*
689 * Try to drop reference on a vnode. Abort if we are releasing the
690 * last reference. Note: this _must_ succeed if not the last reference.
691 */
692 static bool
693 vtryrele(vnode_t *vp)
694 {
695 u_int use, next;
696
697 for (use = atomic_load_relaxed(&vp->v_usecount);; use = next) {
698 if (__predict_false(use == 1)) {
699 return false;
700 }
701 KASSERT(use > 1);
702 next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
703 if (__predict_true(next == use)) {
704 return true;
705 }
706 }
707 }
708
709 /*
710 * vput: unlock and release the reference.
711 */
712 void
713 vput(vnode_t *vp)
714 {
715 int lktype;
716
717 /*
718 * Do an unlocked check of v_usecount. If it looks like we're not
719 * about to drop the last reference, then unlock the vnode and try
720 * to drop the reference. If it ends up being the last reference
721 * after all, vrelel() can fix it all up. Most of the time this
722 * will all go to plan.
723 */
724 if (atomic_load_relaxed(&vp->v_usecount) > 1) {
725 VOP_UNLOCK(vp);
726 if (vtryrele(vp)) {
727 return;
728 }
729 lktype = LK_NONE;
730 } else if ((vp->v_vflag & VV_LOCKSWORK) == 0) {
731 lktype = LK_EXCLUSIVE;
732 } else {
733 lktype = VOP_ISLOCKED(vp);
734 KASSERT(lktype != LK_NONE);
735 }
736 mutex_enter(vp->v_interlock);
737 vrelel(vp, 0, lktype);
738 }
739
740 /*
741 * Vnode release. If reference count drops to zero, call inactive
742 * routine and either return to freelist or free to the pool.
743 */
744 static void
745 vrelel(vnode_t *vp, int flags, int lktype)
746 {
747 const bool async = ((flags & VRELEL_ASYNC) != 0);
748 bool recycle, defer;
749 int error;
750
751 KASSERT(mutex_owned(vp->v_interlock));
752
753 if (__predict_false(vp->v_op == dead_vnodeop_p &&
754 VSTATE_GET(vp) != VS_RECLAIMED)) {
755 vnpanic(vp, "dead but not clean");
756 }
757
758 /*
759 * If not the last reference, just drop the reference count and
760 * unlock. VOP_UNLOCK() is called here without a vnode reference
761 * held, but is ok as the hold of v_interlock will stop the vnode
762 * from disappearing.
763 */
764 if (vtryrele(vp)) {
765 if (lktype != LK_NONE) {
766 VOP_UNLOCK(vp);
767 }
768 mutex_exit(vp->v_interlock);
769 return;
770 }
771 if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
772 vnpanic(vp, "%s: bad ref count", __func__);
773 }
774
775 #ifdef DIAGNOSTIC
776 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
777 vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
778 vprint("vrelel: missing VOP_CLOSE()", vp);
779 }
780 #endif
781
782 /*
783 * First try to get the vnode locked for VOP_INACTIVE().
784 * Defer vnode release to vdrain_thread if caller requests
785 * it explicitly, is the pagedaemon or the lock failed.
786 */
787 defer = false;
788 if ((curlwp == uvm.pagedaemon_lwp) || async) {
789 defer = true;
790 } else if (lktype == LK_SHARED) {
791 /* Excellent chance of getting, if the last ref. */
792 error = vn_lock(vp, LK_UPGRADE | LK_RETRY |
793 LK_NOWAIT);
794 if (error != 0) {
795 defer = true;
796 } else {
797 lktype = LK_EXCLUSIVE;
798 }
799 } else if (lktype == LK_NONE) {
800 /* Excellent chance of getting, if the last ref. */
801 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY |
802 LK_NOWAIT);
803 if (error != 0) {
804 defer = true;
805 } else {
806 lktype = LK_EXCLUSIVE;
807 }
808 }
809 KASSERT(mutex_owned(vp->v_interlock));
810 if (defer) {
811 /*
812 * Defer reclaim to the kthread; it's not safe to
813 * clean it here. We donate it our last reference.
814 */
815 if (lktype != LK_NONE) {
816 VOP_UNLOCK(vp);
817 }
818 lru_requeue(vp, &lru_list[LRU_VRELE]);
819 mutex_exit(vp->v_interlock);
820 return;
821 }
822 KASSERT(lktype == LK_EXCLUSIVE);
823
824 /*
825 * If not clean, deactivate the vnode, but preserve
826 * our reference across the call to VOP_INACTIVE().
827 */
828 if (VSTATE_GET(vp) == VS_RECLAIMED) {
829 VOP_UNLOCK(vp);
830 } else {
831 /*
832 * The vnode must not gain another reference while being
833 * deactivated. If VOP_INACTIVE() indicates that
834 * the described file has been deleted, then recycle
835 * the vnode.
836 *
837 * Note that VOP_INACTIVE() will not drop the vnode lock.
838 */
839 mutex_exit(vp->v_interlock);
840 recycle = false;
841 VOP_INACTIVE(vp, &recycle);
842 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
843 mutex_enter(vp->v_interlock);
844 if (vtryrele(vp)) {
845 VOP_UNLOCK(vp);
846 mutex_exit(vp->v_interlock);
847 rw_exit(vp->v_uobj.vmobjlock);
848 return;
849 }
850
851 /* Take care of space accounting. */
852 if ((vp->v_iflag & VI_EXECMAP) != 0 &&
853 vp->v_uobj.uo_npages != 0) {
854 cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
855 cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
856 }
857 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
858 vp->v_vflag &= ~VV_MAPPED;
859 rw_exit(vp->v_uobj.vmobjlock);
860
861 /*
862 * Recycle the vnode if the file is now unused (unlinked),
863 * otherwise just free it.
864 */
865 if (recycle) {
866 VSTATE_ASSERT(vp, VS_LOADED);
867 /* vcache_reclaim drops the lock. */
868 vcache_reclaim(vp);
869 } else {
870 VOP_UNLOCK(vp);
871 }
872 KASSERT(vp->v_usecount > 0);
873 }
874
875 if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
876 /* Gained another reference while being reclaimed. */
877 mutex_exit(vp->v_interlock);
878 return;
879 }
880
881 if (VSTATE_GET(vp) == VS_RECLAIMED && vp->v_holdcnt == 0) {
882 /*
883 * It's clean so destroy it. It isn't referenced
884 * anywhere since it has been reclaimed.
885 */
886 vcache_free(VNODE_TO_VIMPL(vp));
887 } else {
888 /*
889 * Otherwise, put it back onto the freelist. It
890 * can't be destroyed while still associated with
891 * a file system.
892 */
893 lru_requeue(vp, lru_which(vp));
894 mutex_exit(vp->v_interlock);
895 }
896 }
897
898 void
899 vrele(vnode_t *vp)
900 {
901
902 if (vtryrele(vp)) {
903 return;
904 }
905 mutex_enter(vp->v_interlock);
906 vrelel(vp, 0, LK_NONE);
907 }
908
909 /*
910 * Asynchronous vnode release, vnode is released in different context.
911 */
912 void
913 vrele_async(vnode_t *vp)
914 {
915
916 if (vtryrele(vp)) {
917 return;
918 }
919 mutex_enter(vp->v_interlock);
920 vrelel(vp, VRELEL_ASYNC, LK_NONE);
921 }
922
923 /*
924 * Vnode reference, where a reference is already held by some other
925 * object (for example, a file structure).
926 *
927 * NB: we have lockless code sequences that rely on this not blocking.
928 */
929 void
930 vref(vnode_t *vp)
931 {
932
933 KASSERT(atomic_load_relaxed(&vp->v_usecount) != 0);
934
935 atomic_inc_uint(&vp->v_usecount);
936 }
937
938 /*
939 * Page or buffer structure gets a reference.
940 * Called with v_interlock held.
941 */
942 void
943 vholdl(vnode_t *vp)
944 {
945
946 KASSERT(mutex_owned(vp->v_interlock));
947
948 if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0)
949 lru_requeue(vp, lru_which(vp));
950 }
951
952 /*
953 * Page or buffer structure gets a reference.
954 */
955 void
956 vhold(vnode_t *vp)
957 {
958
959 mutex_enter(vp->v_interlock);
960 vholdl(vp);
961 mutex_exit(vp->v_interlock);
962 }
963
964 /*
965 * Page or buffer structure frees a reference.
966 * Called with v_interlock held.
967 */
968 void
969 holdrelel(vnode_t *vp)
970 {
971
972 KASSERT(mutex_owned(vp->v_interlock));
973
974 if (vp->v_holdcnt <= 0) {
975 vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
976 }
977
978 vp->v_holdcnt--;
979 if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
980 lru_requeue(vp, lru_which(vp));
981 }
982
983 /*
984 * Page or buffer structure frees a reference.
985 */
986 void
987 holdrele(vnode_t *vp)
988 {
989
990 mutex_enter(vp->v_interlock);
991 holdrelel(vp);
992 mutex_exit(vp->v_interlock);
993 }
994
995 /*
996 * Recycle an unused vnode if caller holds the last reference.
997 */
998 bool
999 vrecycle(vnode_t *vp)
1000 {
1001 int error __diagused;
1002
1003 mutex_enter(vp->v_interlock);
1004
1005 /* Make sure we hold the last reference. */
1006 VSTATE_WAIT_STABLE(vp);
1007 if (vp->v_usecount != 1) {
1008 mutex_exit(vp->v_interlock);
1009 return false;
1010 }
1011
1012 /* If the vnode is already clean we're done. */
1013 if (VSTATE_GET(vp) != VS_LOADED) {
1014 VSTATE_ASSERT(vp, VS_RECLAIMED);
1015 vrelel(vp, 0, LK_NONE);
1016 return true;
1017 }
1018
1019 /* Prevent further references until the vnode is locked. */
1020 VSTATE_CHANGE(vp, VS_LOADED, VS_BLOCKED);
1021 mutex_exit(vp->v_interlock);
1022
1023 /*
1024 * On a leaf file system this lock will always succeed as we hold
1025 * the last reference and prevent further references.
1026 * On layered file systems waiting for the lock would open a can of
1027 * deadlocks as the lower vnodes may have other active references.
1028 */
1029 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT);
1030
1031 mutex_enter(vp->v_interlock);
1032 VSTATE_CHANGE(vp, VS_BLOCKED, VS_LOADED);
1033
1034 if (error) {
1035 mutex_exit(vp->v_interlock);
1036 return false;
1037 }
1038
1039 KASSERT(vp->v_usecount == 1);
1040 vcache_reclaim(vp);
1041 vrelel(vp, 0, LK_NONE);
1042
1043 return true;
1044 }
1045
1046 /*
1047 * Helper for vrevoke() to propagate suspension from lastmp
1048 * to thismp. Both args may be NULL.
1049 * Returns the currently suspended file system or NULL.
1050 */
1051 static struct mount *
1052 vrevoke_suspend_next(struct mount *lastmp, struct mount *thismp)
1053 {
1054 int error;
1055
1056 if (lastmp == thismp)
1057 return thismp;
1058
1059 if (lastmp != NULL)
1060 vfs_resume(lastmp);
1061
1062 if (thismp == NULL)
1063 return NULL;
1064
1065 do {
1066 error = vfs_suspend(thismp, 0);
1067 } while (error == EINTR || error == ERESTART);
1068
1069 if (error == 0)
1070 return thismp;
1071
1072 KASSERT(error == EOPNOTSUPP);
1073 return NULL;
1074 }
1075
1076 /*
1077 * Eliminate all activity associated with the requested vnode
1078 * and with all vnodes aliased to the requested vnode.
1079 */
1080 void
1081 vrevoke(vnode_t *vp)
1082 {
1083 struct mount *mp;
1084 vnode_t *vq;
1085 enum vtype type;
1086 dev_t dev;
1087
1088 KASSERT(vp->v_usecount > 0);
1089
1090 mp = vrevoke_suspend_next(NULL, vp->v_mount);
1091
1092 mutex_enter(vp->v_interlock);
1093 VSTATE_WAIT_STABLE(vp);
1094 if (VSTATE_GET(vp) == VS_RECLAIMED) {
1095 mutex_exit(vp->v_interlock);
1096 } else if (vp->v_type != VBLK && vp->v_type != VCHR) {
1097 atomic_inc_uint(&vp->v_usecount);
1098 mutex_exit(vp->v_interlock);
1099 vgone(vp);
1100 } else {
1101 dev = vp->v_rdev;
1102 type = vp->v_type;
1103 mutex_exit(vp->v_interlock);
1104
1105 while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
1106 mp = vrevoke_suspend_next(mp, vq->v_mount);
1107 vgone(vq);
1108 }
1109 }
1110 vrevoke_suspend_next(mp, NULL);
1111 }
1112
1113 /*
1114 * Eliminate all activity associated with a vnode in preparation for
1115 * reuse. Drops a reference from the vnode.
1116 */
1117 void
1118 vgone(vnode_t *vp)
1119 {
1120 int lktype;
1121
1122 KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
1123
1124 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1125 lktype = LK_EXCLUSIVE;
1126 mutex_enter(vp->v_interlock);
1127 VSTATE_WAIT_STABLE(vp);
1128 if (VSTATE_GET(vp) == VS_LOADED) {
1129 vcache_reclaim(vp);
1130 lktype = LK_NONE;
1131 }
1132 VSTATE_ASSERT(vp, VS_RECLAIMED);
1133 vrelel(vp, 0, lktype);
1134 }
1135
1136 static inline uint32_t
1137 vcache_hash(const struct vcache_key *key)
1138 {
1139 uint32_t hash = HASH32_BUF_INIT;
1140
1141 KASSERT(key->vk_key_len > 0);
1142
1143 hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash);
1144 hash = hash32_buf(key->vk_key, key->vk_key_len, hash);
1145 return hash;
1146 }
1147
1148 static void
1149 vcache_init(void)
1150 {
1151
1152 vcache_pool = pool_cache_init(sizeof(vnode_impl_t), coherency_unit,
1153 0, 0, "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL);
1154 KASSERT(vcache_pool != NULL);
1155 mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE);
1156 cv_init(&vcache_cv, "vcache");
1157 vcache_hashsize = desiredvnodes;
1158 vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
1159 &vcache_hashmask);
1160 }
1161
1162 static void
1163 vcache_reinit(void)
1164 {
1165 int i;
1166 uint32_t hash;
1167 u_long oldmask, newmask;
1168 struct hashhead *oldtab, *newtab;
1169 vnode_impl_t *vip;
1170
1171 newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask);
1172 mutex_enter(&vcache_lock);
1173 oldtab = vcache_hashtab;
1174 oldmask = vcache_hashmask;
1175 vcache_hashsize = desiredvnodes;
1176 vcache_hashtab = newtab;
1177 vcache_hashmask = newmask;
1178 for (i = 0; i <= oldmask; i++) {
1179 while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) {
1180 SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash);
1181 hash = vcache_hash(&vip->vi_key);
1182 SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask],
1183 vip, vi_hash);
1184 }
1185 }
1186 mutex_exit(&vcache_lock);
1187 hashdone(oldtab, HASH_SLIST, oldmask);
1188 }
1189
1190 static inline vnode_impl_t *
1191 vcache_hash_lookup(const struct vcache_key *key, uint32_t hash)
1192 {
1193 struct hashhead *hashp;
1194 vnode_impl_t *vip;
1195
1196 KASSERT(mutex_owned(&vcache_lock));
1197
1198 hashp = &vcache_hashtab[hash & vcache_hashmask];
1199 SLIST_FOREACH(vip, hashp, vi_hash) {
1200 if (key->vk_mount != vip->vi_key.vk_mount)
1201 continue;
1202 if (key->vk_key_len != vip->vi_key.vk_key_len)
1203 continue;
1204 if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len))
1205 continue;
1206 return vip;
1207 }
1208 return NULL;
1209 }
1210
1211 /*
1212 * Allocate a new, uninitialized vcache node.
1213 */
1214 static vnode_impl_t *
1215 vcache_alloc(void)
1216 {
1217 vnode_impl_t *vip;
1218 vnode_t *vp;
1219
1220 vip = pool_cache_get(vcache_pool, PR_WAITOK);
1221 vp = VIMPL_TO_VNODE(vip);
1222 memset(vip, 0, sizeof(*vip));
1223
1224 rw_init(&vip->vi_lock);
1225 vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1226
1227 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 1);
1228 cv_init(&vp->v_cv, "vnode");
1229 cache_vnode_init(vp);
1230
1231 vp->v_usecount = 1;
1232 vp->v_type = VNON;
1233 vp->v_size = vp->v_writesize = VSIZENOTSET;
1234
1235 vip->vi_state = VS_LOADING;
1236
1237 lru_requeue(vp, &lru_list[LRU_FREE]);
1238
1239 return vip;
1240 }
1241
1242 /*
1243 * Deallocate a vcache node in state VS_LOADING.
1244 *
1245 * vcache_lock held on entry and released on return.
1246 */
1247 static void
1248 vcache_dealloc(vnode_impl_t *vip)
1249 {
1250 vnode_t *vp;
1251
1252 KASSERT(mutex_owned(&vcache_lock));
1253
1254 vp = VIMPL_TO_VNODE(vip);
1255 vfs_ref(dead_rootmount);
1256 vfs_insmntque(vp, dead_rootmount);
1257 mutex_enter(vp->v_interlock);
1258 vp->v_op = dead_vnodeop_p;
1259 VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED);
1260 mutex_exit(&vcache_lock);
1261 vrelel(vp, 0, LK_NONE);
1262 }
1263
1264 /*
1265 * Free an unused, unreferenced vcache node.
1266 * v_interlock locked on entry.
1267 */
1268 static void
1269 vcache_free(vnode_impl_t *vip)
1270 {
1271 vnode_t *vp;
1272
1273 vp = VIMPL_TO_VNODE(vip);
1274 KASSERT(mutex_owned(vp->v_interlock));
1275
1276 KASSERT(vp->v_usecount == 0);
1277 KASSERT(vp->v_holdcnt == 0);
1278 KASSERT(vp->v_writecount == 0);
1279 lru_requeue(vp, NULL);
1280 mutex_exit(vp->v_interlock);
1281
1282 vfs_insmntque(vp, NULL);
1283 if (vp->v_type == VBLK || vp->v_type == VCHR)
1284 spec_node_destroy(vp);
1285
1286 mutex_obj_free(vp->v_interlock);
1287 rw_destroy(&vip->vi_lock);
1288 uvm_obj_destroy(&vp->v_uobj, true);
1289 cv_destroy(&vp->v_cv);
1290 cache_vnode_fini(vp);
1291 pool_cache_put(vcache_pool, vip);
1292 }
1293
1294 /*
1295 * Try to get an initial reference on this cached vnode.
1296 * Returns zero on success, ENOENT if the vnode has been reclaimed and
1297 * EBUSY if the vnode state is unstable.
1298 *
1299 * v_interlock locked on entry and unlocked on exit.
1300 */
1301 int
1302 vcache_tryvget(vnode_t *vp)
1303 {
1304 int error = 0;
1305
1306 KASSERT(mutex_owned(vp->v_interlock));
1307
1308 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED))
1309 error = ENOENT;
1310 else if (__predict_false(VSTATE_GET(vp) != VS_LOADED))
1311 error = EBUSY;
1312 else if (vp->v_usecount == 0)
1313 vp->v_usecount = 1;
1314 else
1315 atomic_inc_uint(&vp->v_usecount);
1316
1317 mutex_exit(vp->v_interlock);
1318
1319 return error;
1320 }
1321
1322 /*
1323 * Try to get an initial reference on this cached vnode.
1324 * Returns zero on success and ENOENT if the vnode has been reclaimed.
1325 * Will wait for the vnode state to be stable.
1326 *
1327 * v_interlock locked on entry and unlocked on exit.
1328 */
1329 int
1330 vcache_vget(vnode_t *vp)
1331 {
1332
1333 KASSERT(mutex_owned(vp->v_interlock));
1334
1335 /* Increment hold count to prevent vnode from disappearing. */
1336 vp->v_holdcnt++;
1337 VSTATE_WAIT_STABLE(vp);
1338 vp->v_holdcnt--;
1339
1340 /* If this was the last reference to a reclaimed vnode free it now. */
1341 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) {
1342 if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
1343 vcache_free(VNODE_TO_VIMPL(vp));
1344 else
1345 mutex_exit(vp->v_interlock);
1346 return ENOENT;
1347 }
1348 VSTATE_ASSERT(vp, VS_LOADED);
1349 if (vp->v_usecount == 0)
1350 vp->v_usecount = 1;
1351 else
1352 atomic_inc_uint(&vp->v_usecount);
1353 mutex_exit(vp->v_interlock);
1354
1355 return 0;
1356 }
1357
1358 /*
1359 * Get a vnode / fs node pair by key and return it referenced through vpp.
1360 */
1361 int
1362 vcache_get(struct mount *mp, const void *key, size_t key_len,
1363 struct vnode **vpp)
1364 {
1365 int error;
1366 uint32_t hash;
1367 const void *new_key;
1368 struct vnode *vp;
1369 struct vcache_key vcache_key;
1370 vnode_impl_t *vip, *new_vip;
1371
1372 new_key = NULL;
1373 *vpp = NULL;
1374
1375 vcache_key.vk_mount = mp;
1376 vcache_key.vk_key = key;
1377 vcache_key.vk_key_len = key_len;
1378 hash = vcache_hash(&vcache_key);
1379
1380 again:
1381 mutex_enter(&vcache_lock);
1382 vip = vcache_hash_lookup(&vcache_key, hash);
1383
1384 /* If found, take a reference or retry. */
1385 if (__predict_true(vip != NULL)) {
1386 /*
1387 * If the vnode is loading we cannot take the v_interlock
1388 * here as it might change during load (see uvm_obj_setlock()).
1389 * As changing state from VS_LOADING requires both vcache_lock
1390 * and v_interlock it is safe to test with vcache_lock held.
1391 *
1392 * Wait for vnodes changing state from VS_LOADING and retry.
1393 */
1394 if (__predict_false(vip->vi_state == VS_LOADING)) {
1395 cv_wait(&vcache_cv, &vcache_lock);
1396 mutex_exit(&vcache_lock);
1397 goto again;
1398 }
1399 vp = VIMPL_TO_VNODE(vip);
1400 mutex_enter(vp->v_interlock);
1401 mutex_exit(&vcache_lock);
1402 error = vcache_vget(vp);
1403 if (error == ENOENT)
1404 goto again;
1405 if (error == 0)
1406 *vpp = vp;
1407 KASSERT((error != 0) == (*vpp == NULL));
1408 return error;
1409 }
1410 mutex_exit(&vcache_lock);
1411
1412 /* Allocate and initialize a new vcache / vnode pair. */
1413 error = vfs_busy(mp);
1414 if (error)
1415 return error;
1416 new_vip = vcache_alloc();
1417 new_vip->vi_key = vcache_key;
1418 vp = VIMPL_TO_VNODE(new_vip);
1419 mutex_enter(&vcache_lock);
1420 vip = vcache_hash_lookup(&vcache_key, hash);
1421 if (vip == NULL) {
1422 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1423 new_vip, vi_hash);
1424 vip = new_vip;
1425 }
1426
1427 /* If another thread beat us inserting this node, retry. */
1428 if (vip != new_vip) {
1429 vcache_dealloc(new_vip);
1430 vfs_unbusy(mp);
1431 goto again;
1432 }
1433 mutex_exit(&vcache_lock);
1434
1435 /* Load the fs node. Exclusive as new_node is VS_LOADING. */
1436 error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key);
1437 if (error) {
1438 mutex_enter(&vcache_lock);
1439 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1440 new_vip, vnode_impl, vi_hash);
1441 vcache_dealloc(new_vip);
1442 vfs_unbusy(mp);
1443 KASSERT(*vpp == NULL);
1444 return error;
1445 }
1446 KASSERT(new_key != NULL);
1447 KASSERT(memcmp(key, new_key, key_len) == 0);
1448 KASSERT(vp->v_op != NULL);
1449 vfs_insmntque(vp, mp);
1450 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1451 vp->v_vflag |= VV_MPSAFE;
1452 vfs_ref(mp);
1453 vfs_unbusy(mp);
1454
1455 /* Finished loading, finalize node. */
1456 mutex_enter(&vcache_lock);
1457 new_vip->vi_key.vk_key = new_key;
1458 mutex_enter(vp->v_interlock);
1459 VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
1460 mutex_exit(vp->v_interlock);
1461 mutex_exit(&vcache_lock);
1462 *vpp = vp;
1463 return 0;
1464 }
1465
1466 /*
1467 * Create a new vnode / fs node pair and return it referenced through vpp.
1468 */
1469 int
1470 vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap,
1471 kauth_cred_t cred, void *extra, struct vnode **vpp)
1472 {
1473 int error;
1474 uint32_t hash;
1475 struct vnode *vp, *ovp;
1476 vnode_impl_t *vip, *ovip;
1477
1478 *vpp = NULL;
1479
1480 /* Allocate and initialize a new vcache / vnode pair. */
1481 error = vfs_busy(mp);
1482 if (error)
1483 return error;
1484 vip = vcache_alloc();
1485 vip->vi_key.vk_mount = mp;
1486 vp = VIMPL_TO_VNODE(vip);
1487
1488 /* Create and load the fs node. */
1489 error = VFS_NEWVNODE(mp, dvp, vp, vap, cred, extra,
1490 &vip->vi_key.vk_key_len, &vip->vi_key.vk_key);
1491 if (error) {
1492 mutex_enter(&vcache_lock);
1493 vcache_dealloc(vip);
1494 vfs_unbusy(mp);
1495 KASSERT(*vpp == NULL);
1496 return error;
1497 }
1498 KASSERT(vp->v_op != NULL);
1499 KASSERT((vip->vi_key.vk_key_len == 0) == (mp == dead_rootmount));
1500 if (vip->vi_key.vk_key_len > 0) {
1501 KASSERT(vip->vi_key.vk_key != NULL);
1502 hash = vcache_hash(&vip->vi_key);
1503
1504 /*
1505 * Wait for previous instance to be reclaimed,
1506 * then insert new node.
1507 */
1508 mutex_enter(&vcache_lock);
1509 while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) {
1510 ovp = VIMPL_TO_VNODE(ovip);
1511 mutex_enter(ovp->v_interlock);
1512 mutex_exit(&vcache_lock);
1513 error = vcache_vget(ovp);
1514 KASSERT(error == ENOENT);
1515 mutex_enter(&vcache_lock);
1516 }
1517 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1518 vip, vi_hash);
1519 mutex_exit(&vcache_lock);
1520 }
1521 vfs_insmntque(vp, mp);
1522 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1523 vp->v_vflag |= VV_MPSAFE;
1524 vfs_ref(mp);
1525 vfs_unbusy(mp);
1526
1527 /* Finished loading, finalize node. */
1528 mutex_enter(&vcache_lock);
1529 mutex_enter(vp->v_interlock);
1530 VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
1531 mutex_exit(&vcache_lock);
1532 mutex_exit(vp->v_interlock);
1533 *vpp = vp;
1534 return 0;
1535 }
1536
1537 /*
1538 * Prepare key change: update old cache nodes key and lock new cache node.
1539 * Return an error if the new node already exists.
1540 */
1541 int
1542 vcache_rekey_enter(struct mount *mp, struct vnode *vp,
1543 const void *old_key, size_t old_key_len,
1544 const void *new_key, size_t new_key_len)
1545 {
1546 uint32_t old_hash, new_hash;
1547 struct vcache_key old_vcache_key, new_vcache_key;
1548 vnode_impl_t *vip, *new_vip;
1549
1550 old_vcache_key.vk_mount = mp;
1551 old_vcache_key.vk_key = old_key;
1552 old_vcache_key.vk_key_len = old_key_len;
1553 old_hash = vcache_hash(&old_vcache_key);
1554
1555 new_vcache_key.vk_mount = mp;
1556 new_vcache_key.vk_key = new_key;
1557 new_vcache_key.vk_key_len = new_key_len;
1558 new_hash = vcache_hash(&new_vcache_key);
1559
1560 new_vip = vcache_alloc();
1561 new_vip->vi_key = new_vcache_key;
1562
1563 /* Insert locked new node used as placeholder. */
1564 mutex_enter(&vcache_lock);
1565 vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1566 if (vip != NULL) {
1567 vcache_dealloc(new_vip);
1568 return EEXIST;
1569 }
1570 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1571 new_vip, vi_hash);
1572
1573 /* Replace old nodes key with the temporary copy. */
1574 vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1575 KASSERT(vip != NULL);
1576 KASSERT(VIMPL_TO_VNODE(vip) == vp);
1577 KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key);
1578 vip->vi_key = old_vcache_key;
1579 mutex_exit(&vcache_lock);
1580 return 0;
1581 }
1582
1583 /*
1584 * Key change complete: update old node and remove placeholder.
1585 */
1586 void
1587 vcache_rekey_exit(struct mount *mp, struct vnode *vp,
1588 const void *old_key, size_t old_key_len,
1589 const void *new_key, size_t new_key_len)
1590 {
1591 uint32_t old_hash, new_hash;
1592 struct vcache_key old_vcache_key, new_vcache_key;
1593 vnode_impl_t *vip, *new_vip;
1594 struct vnode *new_vp;
1595
1596 old_vcache_key.vk_mount = mp;
1597 old_vcache_key.vk_key = old_key;
1598 old_vcache_key.vk_key_len = old_key_len;
1599 old_hash = vcache_hash(&old_vcache_key);
1600
1601 new_vcache_key.vk_mount = mp;
1602 new_vcache_key.vk_key = new_key;
1603 new_vcache_key.vk_key_len = new_key_len;
1604 new_hash = vcache_hash(&new_vcache_key);
1605
1606 mutex_enter(&vcache_lock);
1607
1608 /* Lookup old and new node. */
1609 vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1610 KASSERT(vip != NULL);
1611 KASSERT(VIMPL_TO_VNODE(vip) == vp);
1612
1613 new_vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1614 KASSERT(new_vip != NULL);
1615 KASSERT(new_vip->vi_key.vk_key_len == new_key_len);
1616 new_vp = VIMPL_TO_VNODE(new_vip);
1617 mutex_enter(new_vp->v_interlock);
1618 VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING);
1619 mutex_exit(new_vp->v_interlock);
1620
1621 /* Rekey old node and put it onto its new hashlist. */
1622 vip->vi_key = new_vcache_key;
1623 if (old_hash != new_hash) {
1624 SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask],
1625 vip, vnode_impl, vi_hash);
1626 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1627 vip, vi_hash);
1628 }
1629
1630 /* Remove new node used as placeholder. */
1631 SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask],
1632 new_vip, vnode_impl, vi_hash);
1633 vcache_dealloc(new_vip);
1634 }
1635
1636 /*
1637 * Disassociate the underlying file system from a vnode.
1638 *
1639 * Must be called with vnode locked and will return unlocked.
1640 * Must be called with the interlock held, and will return with it held.
1641 */
1642 static void
1643 vcache_reclaim(vnode_t *vp)
1644 {
1645 lwp_t *l = curlwp;
1646 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
1647 struct mount *mp = vp->v_mount;
1648 uint32_t hash;
1649 uint8_t temp_buf[64], *temp_key;
1650 size_t temp_key_len;
1651 bool recycle, active;
1652 int error;
1653
1654 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
1655 VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1656 KASSERT(mutex_owned(vp->v_interlock));
1657 KASSERT(vp->v_usecount != 0);
1658
1659 active = (vp->v_usecount > 1);
1660 temp_key_len = vip->vi_key.vk_key_len;
1661 /*
1662 * Prevent the vnode from being recycled or brought into use
1663 * while we clean it out.
1664 */
1665 VSTATE_CHANGE(vp, VS_LOADED, VS_RECLAIMING);
1666 mutex_exit(vp->v_interlock);
1667
1668 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
1669 mutex_enter(vp->v_interlock);
1670 if ((vp->v_iflag & VI_EXECMAP) != 0 && vp->v_uobj.uo_npages != 0) {
1671 cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
1672 cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
1673 }
1674 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
1675 vp->v_iflag |= VI_DEADCHECK; /* for genfs_getpages() */
1676 mutex_exit(vp->v_interlock);
1677 rw_exit(vp->v_uobj.vmobjlock);
1678
1679 /*
1680 * With vnode state set to reclaiming, purge name cache immediately
1681 * to prevent new handles on vnode, and wait for existing threads
1682 * trying to get a handle to notice VS_RECLAIMED status and abort.
1683 */
1684 cache_purge(vp);
1685
1686 /* Replace the vnode key with a temporary copy. */
1687 if (vip->vi_key.vk_key_len > sizeof(temp_buf)) {
1688 temp_key = kmem_alloc(temp_key_len, KM_SLEEP);
1689 } else {
1690 temp_key = temp_buf;
1691 }
1692 if (vip->vi_key.vk_key_len > 0) {
1693 mutex_enter(&vcache_lock);
1694 memcpy(temp_key, vip->vi_key.vk_key, temp_key_len);
1695 vip->vi_key.vk_key = temp_key;
1696 mutex_exit(&vcache_lock);
1697 }
1698
1699 fstrans_start(mp);
1700
1701 /*
1702 * Clean out any cached data associated with the vnode.
1703 * If purging an active vnode, it must be closed and
1704 * deactivated before being reclaimed.
1705 */
1706 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
1707 if (error != 0) {
1708 if (wapbl_vphaswapbl(vp))
1709 WAPBL_DISCARD(wapbl_vptomp(vp));
1710 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
1711 }
1712 KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
1713 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1714 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
1715 spec_node_revoke(vp);
1716 }
1717
1718 /*
1719 * Disassociate the underlying file system from the vnode.
1720 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
1721 * the vnode, and may destroy the vnode so that VOP_UNLOCK
1722 * would no longer function.
1723 */
1724 VOP_INACTIVE(vp, &recycle);
1725 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
1726 VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1727 if (VOP_RECLAIM(vp)) {
1728 vnpanic(vp, "%s: cannot reclaim", __func__);
1729 }
1730
1731 KASSERT(vp->v_data == NULL);
1732 KASSERT((vp->v_iflag & VI_PAGES) == 0);
1733
1734 if (vp->v_type == VREG && vp->v_ractx != NULL) {
1735 uvm_ra_freectx(vp->v_ractx);
1736 vp->v_ractx = NULL;
1737 }
1738
1739 if (vip->vi_key.vk_key_len > 0) {
1740 /* Remove from vnode cache. */
1741 hash = vcache_hash(&vip->vi_key);
1742 mutex_enter(&vcache_lock);
1743 KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
1744 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1745 vip, vnode_impl, vi_hash);
1746 mutex_exit(&vcache_lock);
1747 }
1748 if (temp_key != temp_buf)
1749 kmem_free(temp_key, temp_key_len);
1750
1751 /* Done with purge, notify sleepers of the grim news. */
1752 mutex_enter(vp->v_interlock);
1753 vp->v_op = dead_vnodeop_p;
1754 vp->v_vflag |= VV_LOCKSWORK;
1755 VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED);
1756 vp->v_tag = VT_NON;
1757 KNOTE(&vp->v_klist, NOTE_REVOKE);
1758 mutex_exit(vp->v_interlock);
1759
1760 /*
1761 * Move to dead mount. Must be after changing the operations
1762 * vector as vnode operations enter the mount before using the
1763 * operations vector. See sys/kern/vnode_if.c.
1764 */
1765 vp->v_vflag &= ~VV_ROOT;
1766 vfs_ref(dead_rootmount);
1767 vfs_insmntque(vp, dead_rootmount);
1768
1769 #ifdef PAX_SEGVGUARD
1770 pax_segvguard_cleanup(vp);
1771 #endif /* PAX_SEGVGUARD */
1772
1773 mutex_enter(vp->v_interlock);
1774 fstrans_done(mp);
1775 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1776 }
1777
1778 /*
1779 * Disassociate the underlying file system from an open device vnode
1780 * and make it anonymous.
1781 *
1782 * Vnode unlocked on entry, drops a reference to the vnode.
1783 */
1784 void
1785 vcache_make_anon(vnode_t *vp)
1786 {
1787 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
1788 uint32_t hash;
1789 bool recycle;
1790
1791 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
1792 KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
1793 VSTATE_ASSERT_UNLOCKED(vp, VS_ACTIVE);
1794
1795 /* Remove from vnode cache. */
1796 hash = vcache_hash(&vip->vi_key);
1797 mutex_enter(&vcache_lock);
1798 KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
1799 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1800 vip, vnode_impl, vi_hash);
1801 vip->vi_key.vk_mount = dead_rootmount;
1802 vip->vi_key.vk_key_len = 0;
1803 vip->vi_key.vk_key = NULL;
1804 mutex_exit(&vcache_lock);
1805
1806 /*
1807 * Disassociate the underlying file system from the vnode.
1808 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
1809 * the vnode, and may destroy the vnode so that VOP_UNLOCK
1810 * would no longer function.
1811 */
1812 if (vn_lock(vp, LK_EXCLUSIVE)) {
1813 vnpanic(vp, "%s: cannot lock", __func__);
1814 }
1815 VOP_INACTIVE(vp, &recycle);
1816 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
1817 VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1818 if (VOP_RECLAIM(vp)) {
1819 vnpanic(vp, "%s: cannot reclaim", __func__);
1820 }
1821
1822 /* Purge name cache. */
1823 cache_purge(vp);
1824
1825 /* Done with purge, change operations vector. */
1826 mutex_enter(vp->v_interlock);
1827 vp->v_op = spec_vnodeop_p;
1828 vp->v_vflag |= VV_MPSAFE;
1829 vp->v_vflag &= ~VV_LOCKSWORK;
1830 mutex_exit(vp->v_interlock);
1831
1832 /*
1833 * Move to dead mount. Must be after changing the operations
1834 * vector as vnode operations enter the mount before using the
1835 * operations vector. See sys/kern/vnode_if.c.
1836 */
1837 vfs_ref(dead_rootmount);
1838 vfs_insmntque(vp, dead_rootmount);
1839
1840 vrele(vp);
1841 }
1842
1843 /*
1844 * Update outstanding I/O count and do wakeup if requested.
1845 */
1846 void
1847 vwakeup(struct buf *bp)
1848 {
1849 vnode_t *vp;
1850
1851 if ((vp = bp->b_vp) == NULL)
1852 return;
1853
1854 KASSERT(bp->b_objlock == vp->v_interlock);
1855 KASSERT(mutex_owned(bp->b_objlock));
1856
1857 if (--vp->v_numoutput < 0)
1858 vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
1859 if (vp->v_numoutput == 0)
1860 cv_broadcast(&vp->v_cv);
1861 }
1862
1863 /*
1864 * Test a vnode for being or becoming dead. Returns one of:
1865 * EBUSY: vnode is becoming dead, with "flags == VDEAD_NOWAIT" only.
1866 * ENOENT: vnode is dead.
1867 * 0: otherwise.
1868 *
1869 * Whenever this function returns a non-zero value all future
1870 * calls will also return a non-zero value.
1871 */
1872 int
1873 vdead_check(struct vnode *vp, int flags)
1874 {
1875
1876 KASSERT(mutex_owned(vp->v_interlock));
1877
1878 if (! ISSET(flags, VDEAD_NOWAIT))
1879 VSTATE_WAIT_STABLE(vp);
1880
1881 if (VSTATE_GET(vp) == VS_RECLAIMING) {
1882 KASSERT(ISSET(flags, VDEAD_NOWAIT));
1883 return EBUSY;
1884 } else if (VSTATE_GET(vp) == VS_RECLAIMED) {
1885 return ENOENT;
1886 }
1887
1888 return 0;
1889 }
1890
1891 int
1892 vfs_drainvnodes(void)
1893 {
1894 int i, gen;
1895
1896 mutex_enter(&vdrain_lock);
1897 for (i = 0; i < 2; i++) {
1898 gen = vdrain_gen;
1899 while (gen == vdrain_gen) {
1900 cv_broadcast(&vdrain_cv);
1901 cv_wait(&vdrain_gen_cv, &vdrain_lock);
1902 }
1903 }
1904 mutex_exit(&vdrain_lock);
1905
1906 if (numvnodes >= desiredvnodes)
1907 return EBUSY;
1908
1909 if (vcache_hashsize != desiredvnodes)
1910 vcache_reinit();
1911
1912 return 0;
1913 }
1914
1915 void
1916 vnpanic(vnode_t *vp, const char *fmt, ...)
1917 {
1918 va_list ap;
1919
1920 #ifdef DIAGNOSTIC
1921 vprint(NULL, vp);
1922 #endif
1923 va_start(ap, fmt);
1924 vpanic(fmt, ap);
1925 va_end(ap);
1926 }
1927
1928 void
1929 vshareilock(vnode_t *tvp, vnode_t *fvp)
1930 {
1931 kmutex_t *oldlock;
1932
1933 oldlock = tvp->v_interlock;
1934 mutex_obj_hold(fvp->v_interlock);
1935 tvp->v_interlock = fvp->v_interlock;
1936 mutex_obj_free(oldlock);
1937 }
1938