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