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