vfs_vnode.c revision 1.88 1 /* $NetBSD: vfs_vnode.c,v 1.88 2017/05/17 12:46:14 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.88 2017/05/17 12:46:14 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 * Eliminate all activity associated with the requested vnode
948 * and with all vnodes aliased to the requested vnode.
949 */
950 void
951 vrevoke(vnode_t *vp)
952 {
953 int error;
954 struct mount *mp;
955 vnode_t *vq;
956 enum vtype type;
957 dev_t dev;
958
959 KASSERT(vp->v_usecount > 0);
960
961 mp = vp->v_mount;
962 error = vfs_suspend(mp, 0);
963 KASSERT(error == 0 || error == EOPNOTSUPP);
964 if (error)
965 mp = NULL;
966
967 mutex_enter(vp->v_interlock);
968 VSTATE_WAIT_STABLE(vp);
969 if (VSTATE_GET(vp) == VS_RECLAIMED) {
970 mutex_exit(vp->v_interlock);
971 } else if (vp->v_type != VBLK && vp->v_type != VCHR) {
972 atomic_inc_uint(&vp->v_usecount);
973 mutex_exit(vp->v_interlock);
974 vgone(vp);
975 } else {
976 dev = vp->v_rdev;
977 type = vp->v_type;
978 mutex_exit(vp->v_interlock);
979
980 while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
981 if (mp != vq->v_mount) {
982 if (mp)
983 vfs_resume(mp);
984 mp = vp->v_mount;
985 error = vfs_suspend(mp, 0);
986 KASSERT(error == 0 || error == EOPNOTSUPP);
987 if (error)
988 mp = NULL;
989 }
990 vgone(vq);
991 }
992 }
993 if (mp)
994 vfs_resume(mp);
995 }
996
997 /*
998 * Eliminate all activity associated with a vnode in preparation for
999 * reuse. Drops a reference from the vnode.
1000 */
1001 void
1002 vgone(vnode_t *vp)
1003 {
1004
1005 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1006 mutex_enter(vp->v_interlock);
1007 VSTATE_WAIT_STABLE(vp);
1008 if (VSTATE_GET(vp) == VS_ACTIVE)
1009 vcache_reclaim(vp);
1010 VSTATE_ASSERT(vp, VS_RECLAIMED);
1011 vrelel(vp, 0);
1012 }
1013
1014 static inline uint32_t
1015 vcache_hash(const struct vcache_key *key)
1016 {
1017 uint32_t hash = HASH32_BUF_INIT;
1018
1019 hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash);
1020 hash = hash32_buf(key->vk_key, key->vk_key_len, hash);
1021 return hash;
1022 }
1023
1024 static void
1025 vcache_init(void)
1026 {
1027
1028 vcache_pool = pool_cache_init(sizeof(vnode_impl_t), 0, 0, 0,
1029 "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL);
1030 KASSERT(vcache_pool != NULL);
1031 mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE);
1032 cv_init(&vcache_cv, "vcache");
1033 vcache_hashsize = desiredvnodes;
1034 vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
1035 &vcache_hashmask);
1036 }
1037
1038 static void
1039 vcache_reinit(void)
1040 {
1041 int i;
1042 uint32_t hash;
1043 u_long oldmask, newmask;
1044 struct hashhead *oldtab, *newtab;
1045 vnode_impl_t *vip;
1046
1047 newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask);
1048 mutex_enter(&vcache_lock);
1049 oldtab = vcache_hashtab;
1050 oldmask = vcache_hashmask;
1051 vcache_hashsize = desiredvnodes;
1052 vcache_hashtab = newtab;
1053 vcache_hashmask = newmask;
1054 for (i = 0; i <= oldmask; i++) {
1055 while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) {
1056 SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash);
1057 hash = vcache_hash(&vip->vi_key);
1058 SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask],
1059 vip, vi_hash);
1060 }
1061 }
1062 mutex_exit(&vcache_lock);
1063 hashdone(oldtab, HASH_SLIST, oldmask);
1064 }
1065
1066 static inline vnode_impl_t *
1067 vcache_hash_lookup(const struct vcache_key *key, uint32_t hash)
1068 {
1069 struct hashhead *hashp;
1070 vnode_impl_t *vip;
1071
1072 KASSERT(mutex_owned(&vcache_lock));
1073
1074 hashp = &vcache_hashtab[hash & vcache_hashmask];
1075 SLIST_FOREACH(vip, hashp, vi_hash) {
1076 if (key->vk_mount != vip->vi_key.vk_mount)
1077 continue;
1078 if (key->vk_key_len != vip->vi_key.vk_key_len)
1079 continue;
1080 if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len))
1081 continue;
1082 return vip;
1083 }
1084 return NULL;
1085 }
1086
1087 /*
1088 * Allocate a new, uninitialized vcache node.
1089 */
1090 static vnode_impl_t *
1091 vcache_alloc(void)
1092 {
1093 vnode_impl_t *vip;
1094 vnode_t *vp;
1095
1096 vip = pool_cache_get(vcache_pool, PR_WAITOK);
1097 memset(vip, 0, sizeof(*vip));
1098
1099 vip->vi_lock = rw_obj_alloc();
1100 /* SLIST_INIT(&vip->vi_hash); */
1101 /* LIST_INIT(&vip->vi_nclist); */
1102 /* LIST_INIT(&vip->vi_dnclist); */
1103
1104 vp = VIMPL_TO_VNODE(vip);
1105 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
1106 cv_init(&vp->v_cv, "vnode");
1107
1108 vp->v_usecount = 1;
1109 vp->v_type = VNON;
1110 vp->v_size = vp->v_writesize = VSIZENOTSET;
1111
1112 vip->vi_state = VS_LOADING;
1113
1114 lru_requeue(vp, &lru_free_list);
1115
1116 return vip;
1117 }
1118
1119 /*
1120 * Deallocate a vcache node in state VS_LOADING.
1121 *
1122 * vcache_lock held on entry and released on return.
1123 */
1124 static void
1125 vcache_dealloc(vnode_impl_t *vip)
1126 {
1127 vnode_t *vp;
1128
1129 KASSERT(mutex_owned(&vcache_lock));
1130
1131 vp = VIMPL_TO_VNODE(vip);
1132 mutex_enter(vp->v_interlock);
1133 vp->v_op = dead_vnodeop_p;
1134 VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED);
1135 mutex_exit(&vcache_lock);
1136 vrelel(vp, 0);
1137 }
1138
1139 /*
1140 * Free an unused, unreferenced vcache node.
1141 * v_interlock locked on entry.
1142 */
1143 static void
1144 vcache_free(vnode_impl_t *vip)
1145 {
1146 vnode_t *vp;
1147
1148 vp = VIMPL_TO_VNODE(vip);
1149 KASSERT(mutex_owned(vp->v_interlock));
1150
1151 KASSERT(vp->v_usecount == 0);
1152 KASSERT(vp->v_holdcnt == 0);
1153 KASSERT(vp->v_writecount == 0);
1154 lru_requeue(vp, NULL);
1155 mutex_exit(vp->v_interlock);
1156
1157 vfs_insmntque(vp, NULL);
1158 if (vp->v_type == VBLK || vp->v_type == VCHR)
1159 spec_node_destroy(vp);
1160
1161 rw_obj_free(vip->vi_lock);
1162 uvm_obj_destroy(&vp->v_uobj, true);
1163 cv_destroy(&vp->v_cv);
1164 pool_cache_put(vcache_pool, vip);
1165 }
1166
1167 /*
1168 * Try to get an initial reference on this cached vnode.
1169 * Returns zero on success, ENOENT if the vnode has been reclaimed and
1170 * EBUSY if the vnode state is unstable.
1171 *
1172 * v_interlock locked on entry and unlocked on exit.
1173 */
1174 int
1175 vcache_tryvget(vnode_t *vp)
1176 {
1177 int error = 0;
1178
1179 KASSERT(mutex_owned(vp->v_interlock));
1180
1181 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED))
1182 error = ENOENT;
1183 else if (__predict_false(VSTATE_GET(vp) != VS_ACTIVE))
1184 error = EBUSY;
1185 else if (vp->v_usecount == 0)
1186 vp->v_usecount = 1;
1187 else
1188 atomic_inc_uint(&vp->v_usecount);
1189
1190 mutex_exit(vp->v_interlock);
1191
1192 return error;
1193 }
1194
1195 /*
1196 * Try to get an initial reference on this cached vnode.
1197 * Returns zero on success and ENOENT if the vnode has been reclaimed.
1198 * Will wait for the vnode state to be stable.
1199 *
1200 * v_interlock locked on entry and unlocked on exit.
1201 */
1202 int
1203 vcache_vget(vnode_t *vp)
1204 {
1205
1206 KASSERT(mutex_owned(vp->v_interlock));
1207
1208 /* Increment hold count to prevent vnode from disappearing. */
1209 vp->v_holdcnt++;
1210 VSTATE_WAIT_STABLE(vp);
1211 vp->v_holdcnt--;
1212
1213 /* If this was the last reference to a reclaimed vnode free it now. */
1214 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) {
1215 if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
1216 vcache_free(VNODE_TO_VIMPL(vp));
1217 else
1218 mutex_exit(vp->v_interlock);
1219 return ENOENT;
1220 }
1221 VSTATE_ASSERT(vp, VS_ACTIVE);
1222 if (vp->v_usecount == 0)
1223 vp->v_usecount = 1;
1224 else
1225 atomic_inc_uint(&vp->v_usecount);
1226
1227 mutex_exit(vp->v_interlock);
1228
1229 return 0;
1230 }
1231
1232 /*
1233 * Get a vnode / fs node pair by key and return it referenced through vpp.
1234 */
1235 int
1236 vcache_get(struct mount *mp, const void *key, size_t key_len,
1237 struct vnode **vpp)
1238 {
1239 int error;
1240 uint32_t hash;
1241 const void *new_key;
1242 struct vnode *vp;
1243 struct vcache_key vcache_key;
1244 vnode_impl_t *vip, *new_vip;
1245
1246 new_key = NULL;
1247 *vpp = NULL;
1248
1249 vcache_key.vk_mount = mp;
1250 vcache_key.vk_key = key;
1251 vcache_key.vk_key_len = key_len;
1252 hash = vcache_hash(&vcache_key);
1253
1254 again:
1255 mutex_enter(&vcache_lock);
1256 vip = vcache_hash_lookup(&vcache_key, hash);
1257
1258 /* If found, take a reference or retry. */
1259 if (__predict_true(vip != NULL)) {
1260 /*
1261 * If the vnode is loading we cannot take the v_interlock
1262 * here as it might change during load (see uvm_obj_setlock()).
1263 * As changing state from VS_LOADING requires both vcache_lock
1264 * and v_interlock it is safe to test with vcache_lock held.
1265 *
1266 * Wait for vnodes changing state from VS_LOADING and retry.
1267 */
1268 if (__predict_false(vip->vi_state == VS_LOADING)) {
1269 cv_wait(&vcache_cv, &vcache_lock);
1270 mutex_exit(&vcache_lock);
1271 goto again;
1272 }
1273 vp = VIMPL_TO_VNODE(vip);
1274 mutex_enter(vp->v_interlock);
1275 mutex_exit(&vcache_lock);
1276 error = vcache_vget(vp);
1277 if (error == ENOENT)
1278 goto again;
1279 if (error == 0)
1280 *vpp = vp;
1281 KASSERT((error != 0) == (*vpp == NULL));
1282 return error;
1283 }
1284 mutex_exit(&vcache_lock);
1285
1286 /* Allocate and initialize a new vcache / vnode pair. */
1287 error = vfs_busy(mp);
1288 if (error)
1289 return error;
1290 new_vip = vcache_alloc();
1291 new_vip->vi_key = vcache_key;
1292 vp = VIMPL_TO_VNODE(new_vip);
1293 mutex_enter(&vcache_lock);
1294 vip = vcache_hash_lookup(&vcache_key, hash);
1295 if (vip == NULL) {
1296 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1297 new_vip, vi_hash);
1298 vip = new_vip;
1299 }
1300
1301 /* If another thread beat us inserting this node, retry. */
1302 if (vip != new_vip) {
1303 vcache_dealloc(new_vip);
1304 vfs_unbusy(mp);
1305 goto again;
1306 }
1307 mutex_exit(&vcache_lock);
1308
1309 /* Load the fs node. Exclusive as new_node is VS_LOADING. */
1310 error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key);
1311 if (error) {
1312 mutex_enter(&vcache_lock);
1313 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1314 new_vip, vnode_impl, vi_hash);
1315 vcache_dealloc(new_vip);
1316 vfs_unbusy(mp);
1317 KASSERT(*vpp == NULL);
1318 return error;
1319 }
1320 KASSERT(new_key != NULL);
1321 KASSERT(memcmp(key, new_key, key_len) == 0);
1322 KASSERT(vp->v_op != NULL);
1323 vfs_insmntque(vp, mp);
1324 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1325 vp->v_vflag |= VV_MPSAFE;
1326 vfs_ref(mp);
1327 vfs_unbusy(mp);
1328
1329 /* Finished loading, finalize node. */
1330 mutex_enter(&vcache_lock);
1331 new_vip->vi_key.vk_key = new_key;
1332 mutex_enter(vp->v_interlock);
1333 VSTATE_CHANGE(vp, VS_LOADING, VS_ACTIVE);
1334 mutex_exit(vp->v_interlock);
1335 mutex_exit(&vcache_lock);
1336 *vpp = vp;
1337 return 0;
1338 }
1339
1340 /*
1341 * Create a new vnode / fs node pair and return it referenced through vpp.
1342 */
1343 int
1344 vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap,
1345 kauth_cred_t cred, struct vnode **vpp)
1346 {
1347 int error;
1348 uint32_t hash;
1349 struct vnode *vp, *ovp;
1350 vnode_impl_t *vip, *ovip;
1351
1352 *vpp = NULL;
1353
1354 /* Allocate and initialize a new vcache / vnode pair. */
1355 error = vfs_busy(mp);
1356 if (error)
1357 return error;
1358 vip = vcache_alloc();
1359 vip->vi_key.vk_mount = mp;
1360 vp = VIMPL_TO_VNODE(vip);
1361
1362 /* Create and load the fs node. */
1363 error = VFS_NEWVNODE(mp, dvp, vp, vap, cred,
1364 &vip->vi_key.vk_key_len, &vip->vi_key.vk_key);
1365 if (error) {
1366 mutex_enter(&vcache_lock);
1367 vcache_dealloc(vip);
1368 vfs_unbusy(mp);
1369 KASSERT(*vpp == NULL);
1370 return error;
1371 }
1372 KASSERT(vip->vi_key.vk_key != NULL);
1373 KASSERT(vp->v_op != NULL);
1374 hash = vcache_hash(&vip->vi_key);
1375
1376 /* Wait for previous instance to be reclaimed, then insert new node. */
1377 mutex_enter(&vcache_lock);
1378 while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) {
1379 ovp = VIMPL_TO_VNODE(ovip);
1380 mutex_enter(ovp->v_interlock);
1381 mutex_exit(&vcache_lock);
1382 error = vcache_vget(ovp);
1383 KASSERT(error == ENOENT);
1384 mutex_enter(&vcache_lock);
1385 }
1386 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1387 vip, vi_hash);
1388 mutex_exit(&vcache_lock);
1389 vfs_insmntque(vp, mp);
1390 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1391 vp->v_vflag |= VV_MPSAFE;
1392 vfs_ref(mp);
1393 vfs_unbusy(mp);
1394
1395 /* Finished loading, finalize node. */
1396 mutex_enter(&vcache_lock);
1397 mutex_enter(vp->v_interlock);
1398 VSTATE_CHANGE(vp, VS_LOADING, VS_ACTIVE);
1399 mutex_exit(&vcache_lock);
1400 mutex_exit(vp->v_interlock);
1401 *vpp = vp;
1402 return 0;
1403 }
1404
1405 /*
1406 * Prepare key change: update old cache nodes key and lock new cache node.
1407 * Return an error if the new node already exists.
1408 */
1409 int
1410 vcache_rekey_enter(struct mount *mp, struct vnode *vp,
1411 const void *old_key, size_t old_key_len,
1412 const void *new_key, size_t new_key_len)
1413 {
1414 uint32_t old_hash, new_hash;
1415 struct vcache_key old_vcache_key, new_vcache_key;
1416 vnode_impl_t *vip, *new_vip;
1417
1418 old_vcache_key.vk_mount = mp;
1419 old_vcache_key.vk_key = old_key;
1420 old_vcache_key.vk_key_len = old_key_len;
1421 old_hash = vcache_hash(&old_vcache_key);
1422
1423 new_vcache_key.vk_mount = mp;
1424 new_vcache_key.vk_key = new_key;
1425 new_vcache_key.vk_key_len = new_key_len;
1426 new_hash = vcache_hash(&new_vcache_key);
1427
1428 new_vip = vcache_alloc();
1429 new_vip->vi_key = new_vcache_key;
1430
1431 /* Insert locked new node used as placeholder. */
1432 mutex_enter(&vcache_lock);
1433 vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1434 if (vip != NULL) {
1435 vcache_dealloc(new_vip);
1436 return EEXIST;
1437 }
1438 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1439 new_vip, vi_hash);
1440
1441 /* Replace old nodes key with the temporary copy. */
1442 vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1443 KASSERT(vip != NULL);
1444 KASSERT(VIMPL_TO_VNODE(vip) == vp);
1445 KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key);
1446 vip->vi_key = old_vcache_key;
1447 mutex_exit(&vcache_lock);
1448 return 0;
1449 }
1450
1451 /*
1452 * Key change complete: update old node and remove placeholder.
1453 */
1454 void
1455 vcache_rekey_exit(struct mount *mp, struct vnode *vp,
1456 const void *old_key, size_t old_key_len,
1457 const void *new_key, size_t new_key_len)
1458 {
1459 uint32_t old_hash, new_hash;
1460 struct vcache_key old_vcache_key, new_vcache_key;
1461 vnode_impl_t *vip, *new_vip;
1462 struct vnode *new_vp;
1463
1464 old_vcache_key.vk_mount = mp;
1465 old_vcache_key.vk_key = old_key;
1466 old_vcache_key.vk_key_len = old_key_len;
1467 old_hash = vcache_hash(&old_vcache_key);
1468
1469 new_vcache_key.vk_mount = mp;
1470 new_vcache_key.vk_key = new_key;
1471 new_vcache_key.vk_key_len = new_key_len;
1472 new_hash = vcache_hash(&new_vcache_key);
1473
1474 mutex_enter(&vcache_lock);
1475
1476 /* Lookup old and new node. */
1477 vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1478 KASSERT(vip != NULL);
1479 KASSERT(VIMPL_TO_VNODE(vip) == vp);
1480
1481 new_vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1482 KASSERT(new_vip != NULL);
1483 KASSERT(new_vip->vi_key.vk_key_len == new_key_len);
1484 new_vp = VIMPL_TO_VNODE(new_vip);
1485 mutex_enter(new_vp->v_interlock);
1486 VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING);
1487 mutex_exit(new_vp->v_interlock);
1488
1489 /* Rekey old node and put it onto its new hashlist. */
1490 vip->vi_key = new_vcache_key;
1491 if (old_hash != new_hash) {
1492 SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask],
1493 vip, vnode_impl, vi_hash);
1494 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1495 vip, vi_hash);
1496 }
1497
1498 /* Remove new node used as placeholder. */
1499 SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask],
1500 new_vip, vnode_impl, vi_hash);
1501 vcache_dealloc(new_vip);
1502 }
1503
1504 /*
1505 * Disassociate the underlying file system from a vnode.
1506 *
1507 * Must be called with vnode locked and will return unlocked.
1508 * Must be called with the interlock held, and will return with it held.
1509 */
1510 static void
1511 vcache_reclaim(vnode_t *vp)
1512 {
1513 lwp_t *l = curlwp;
1514 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
1515 struct mount *mp = vp->v_mount;
1516 uint32_t hash;
1517 uint8_t temp_buf[64], *temp_key;
1518 size_t temp_key_len;
1519 bool recycle, active;
1520 int error;
1521
1522 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
1523 VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1524 KASSERT(mutex_owned(vp->v_interlock));
1525 KASSERT(vp->v_usecount != 0);
1526
1527 active = (vp->v_usecount > 1);
1528 temp_key_len = vip->vi_key.vk_key_len;
1529 /*
1530 * Prevent the vnode from being recycled or brought into use
1531 * while we clean it out.
1532 */
1533 VSTATE_CHANGE(vp, VS_ACTIVE, VS_RECLAIMING);
1534 if (vp->v_iflag & VI_EXECMAP) {
1535 atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
1536 atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
1537 }
1538 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
1539 mutex_exit(vp->v_interlock);
1540
1541 /* Replace the vnode key with a temporary copy. */
1542 if (vip->vi_key.vk_key_len > sizeof(temp_buf)) {
1543 temp_key = kmem_alloc(temp_key_len, KM_SLEEP);
1544 } else {
1545 temp_key = temp_buf;
1546 }
1547 mutex_enter(&vcache_lock);
1548 memcpy(temp_key, vip->vi_key.vk_key, temp_key_len);
1549 vip->vi_key.vk_key = temp_key;
1550 mutex_exit(&vcache_lock);
1551
1552 fstrans_start(mp, FSTRANS_SHARED);
1553
1554 /*
1555 * Clean out any cached data associated with the vnode.
1556 * If purging an active vnode, it must be closed and
1557 * deactivated before being reclaimed.
1558 */
1559 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
1560 if (error != 0) {
1561 if (wapbl_vphaswapbl(vp))
1562 WAPBL_DISCARD(wapbl_vptomp(vp));
1563 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
1564 }
1565 KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
1566 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1567 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
1568 spec_node_revoke(vp);
1569 }
1570
1571 /*
1572 * Disassociate the underlying file system from the vnode.
1573 * Note that the VOP_INACTIVE will not unlock the vnode.
1574 */
1575 VOP_INACTIVE(vp, &recycle);
1576 VOP_UNLOCK(vp);
1577 if (VOP_RECLAIM(vp)) {
1578 vnpanic(vp, "%s: cannot reclaim", __func__);
1579 }
1580
1581 KASSERT(vp->v_data == NULL);
1582 KASSERT(vp->v_uobj.uo_npages == 0);
1583
1584 if (vp->v_type == VREG && vp->v_ractx != NULL) {
1585 uvm_ra_freectx(vp->v_ractx);
1586 vp->v_ractx = NULL;
1587 }
1588
1589 /* Purge name cache. */
1590 cache_purge(vp);
1591
1592 /* Remove from vnode cache. */
1593 hash = vcache_hash(&vip->vi_key);
1594 mutex_enter(&vcache_lock);
1595 KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
1596 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1597 vip, vnode_impl, vi_hash);
1598 mutex_exit(&vcache_lock);
1599 if (temp_key != temp_buf)
1600 kmem_free(temp_key, temp_key_len);
1601
1602 /* Done with purge, notify sleepers of the grim news. */
1603 mutex_enter(vp->v_interlock);
1604 vp->v_op = dead_vnodeop_p;
1605 vp->v_vflag |= VV_LOCKSWORK;
1606 VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED);
1607 vp->v_tag = VT_NON;
1608 KNOTE(&vp->v_klist, NOTE_REVOKE);
1609 mutex_exit(vp->v_interlock);
1610
1611 /*
1612 * Move to dead mount. Must be after changing the operations
1613 * vector as vnode operations enter the mount before using the
1614 * operations vector. See sys/kern/vnode_if.c.
1615 */
1616 vp->v_vflag &= ~VV_ROOT;
1617 vfs_ref(dead_rootmount);
1618 vfs_insmntque(vp, dead_rootmount);
1619
1620 mutex_enter(vp->v_interlock);
1621 fstrans_done(mp);
1622 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1623 }
1624
1625 /*
1626 * Update outstanding I/O count and do wakeup if requested.
1627 */
1628 void
1629 vwakeup(struct buf *bp)
1630 {
1631 vnode_t *vp;
1632
1633 if ((vp = bp->b_vp) == NULL)
1634 return;
1635
1636 KASSERT(bp->b_objlock == vp->v_interlock);
1637 KASSERT(mutex_owned(bp->b_objlock));
1638
1639 if (--vp->v_numoutput < 0)
1640 vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
1641 if (vp->v_numoutput == 0)
1642 cv_broadcast(&vp->v_cv);
1643 }
1644
1645 /*
1646 * Test a vnode for being or becoming dead. Returns one of:
1647 * EBUSY: vnode is becoming dead, with "flags == VDEAD_NOWAIT" only.
1648 * ENOENT: vnode is dead.
1649 * 0: otherwise.
1650 *
1651 * Whenever this function returns a non-zero value all future
1652 * calls will also return a non-zero value.
1653 */
1654 int
1655 vdead_check(struct vnode *vp, int flags)
1656 {
1657
1658 KASSERT(mutex_owned(vp->v_interlock));
1659
1660 if (! ISSET(flags, VDEAD_NOWAIT))
1661 VSTATE_WAIT_STABLE(vp);
1662
1663 if (VSTATE_GET(vp) == VS_RECLAIMING) {
1664 KASSERT(ISSET(flags, VDEAD_NOWAIT));
1665 return EBUSY;
1666 } else if (VSTATE_GET(vp) == VS_RECLAIMED) {
1667 return ENOENT;
1668 }
1669
1670 return 0;
1671 }
1672
1673 int
1674 vfs_drainvnodes(void)
1675 {
1676 int i, gen;
1677
1678 mutex_enter(&vdrain_lock);
1679 for (i = 0; i < 2; i++) {
1680 gen = vdrain_gen;
1681 while (gen == vdrain_gen) {
1682 cv_broadcast(&vdrain_cv);
1683 cv_wait(&vdrain_gen_cv, &vdrain_lock);
1684 }
1685 }
1686 mutex_exit(&vdrain_lock);
1687
1688 if (numvnodes >= desiredvnodes)
1689 return EBUSY;
1690
1691 if (vcache_hashsize != desiredvnodes)
1692 vcache_reinit();
1693
1694 return 0;
1695 }
1696
1697 void
1698 vnpanic(vnode_t *vp, const char *fmt, ...)
1699 {
1700 va_list ap;
1701
1702 #ifdef DIAGNOSTIC
1703 vprint(NULL, vp);
1704 #endif
1705 va_start(ap, fmt);
1706 vpanic(fmt, ap);
1707 va_end(ap);
1708 }
1709