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