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