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