vfs_vnode.c revision 1.30 1 /* $NetBSD: vfs_vnode.c,v 1.30 2013/12/07 10:03:28 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 getnewvnode(9) and/or vnalloc(9).
79 * - Reclamation of inactive vnode, via 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 vclean(9), which calls VOP_RECLAIM(9) to disassociate
93 * underlying file system from the vnode, and finally destroyed.
94 *
95 * Reference counting
96 *
97 * Vnode is considered active, if reference count (vnode_t::v_usecount)
98 * is non-zero. It is maintained using: vref(9) and vrele(9), as well
99 * as vput(9), routines. Common points holding references are e.g.
100 * file openings, current working directory, mount points, etc.
101 *
102 * Note on v_usecount and its locking
103 *
104 * At nearly all points it is known that v_usecount could be zero,
105 * the vnode_t::v_interlock will be held. To change v_usecount away
106 * from zero, the interlock must be held. To change from a non-zero
107 * value to zero, again the interlock must be held.
108 *
109 * Changing the usecount from a non-zero value to a non-zero value can
110 * safely be done using atomic operations, without the interlock held.
111 *
112 * Note: if VI_CLEAN is set, vnode_t::v_interlock will be released while
113 * mntvnode_lock is still held.
114 *
115 * See PR 41374.
116 */
117
118 #include <sys/cdefs.h>
119 __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.30 2013/12/07 10:03:28 hannken Exp $");
120
121 #define _VFS_VNODE_PRIVATE
122
123 #include <sys/param.h>
124 #include <sys/kernel.h>
125
126 #include <sys/atomic.h>
127 #include <sys/buf.h>
128 #include <sys/conf.h>
129 #include <sys/device.h>
130 #include <sys/kauth.h>
131 #include <sys/kmem.h>
132 #include <sys/kthread.h>
133 #include <sys/module.h>
134 #include <sys/mount.h>
135 #include <sys/namei.h>
136 #include <sys/syscallargs.h>
137 #include <sys/sysctl.h>
138 #include <sys/systm.h>
139 #include <sys/vnode.h>
140 #include <sys/wapbl.h>
141 #include <sys/fstrans.h>
142
143 #include <uvm/uvm.h>
144 #include <uvm/uvm_readahead.h>
145
146 /* Flags to vrelel. */
147 #define VRELEL_ASYNC_RELE 0x0001 /* Always defer to vrele thread. */
148 #define VRELEL_CHANGING_SET 0x0002 /* VI_CHANGING set by caller. */
149
150 u_int numvnodes __cacheline_aligned;
151
152 static pool_cache_t vnode_cache __read_mostly;
153
154 /*
155 * There are two free lists: one is for vnodes which have no buffer/page
156 * references and one for those which do (i.e. v_holdcnt is non-zero).
157 * Vnode recycling mechanism first attempts to look into the former list.
158 */
159 static kmutex_t vnode_free_list_lock __cacheline_aligned;
160 static vnodelst_t vnode_free_list __cacheline_aligned;
161 static vnodelst_t vnode_hold_list __cacheline_aligned;
162 static kcondvar_t vdrain_cv __cacheline_aligned;
163
164 static vnodelst_t vrele_list __cacheline_aligned;
165 static kmutex_t vrele_lock __cacheline_aligned;
166 static kcondvar_t vrele_cv __cacheline_aligned;
167 static lwp_t * vrele_lwp __cacheline_aligned;
168 static int vrele_pending __cacheline_aligned;
169 static int vrele_gen __cacheline_aligned;
170
171 static int cleanvnode(void);
172 static void vclean(vnode_t *);
173 static void vrelel(vnode_t *, int);
174 static void vdrain_thread(void *);
175 static void vrele_thread(void *);
176 static void vnpanic(vnode_t *, const char *, ...)
177 __printflike(2, 3);
178
179 /* Routines having to do with the management of the vnode table. */
180 extern int (**dead_vnodeop_p)(void *);
181
182 void
183 vfs_vnode_sysinit(void)
184 {
185 int error __diagused;
186
187 vnode_cache = pool_cache_init(sizeof(vnode_t), 0, 0, 0, "vnodepl",
188 NULL, IPL_NONE, NULL, NULL, NULL);
189 KASSERT(vnode_cache != NULL);
190
191 mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE);
192 TAILQ_INIT(&vnode_free_list);
193 TAILQ_INIT(&vnode_hold_list);
194 TAILQ_INIT(&vrele_list);
195
196 mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE);
197 cv_init(&vdrain_cv, "vdrain");
198 cv_init(&vrele_cv, "vrele");
199 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread,
200 NULL, NULL, "vdrain");
201 KASSERT(error == 0);
202 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread,
203 NULL, &vrele_lwp, "vrele");
204 KASSERT(error == 0);
205 }
206
207 /*
208 * Allocate a new, uninitialized vnode. If 'mp' is non-NULL, this is a
209 * marker vnode.
210 */
211 vnode_t *
212 vnalloc(struct mount *mp)
213 {
214 vnode_t *vp;
215
216 vp = pool_cache_get(vnode_cache, PR_WAITOK);
217 KASSERT(vp != NULL);
218
219 memset(vp, 0, sizeof(*vp));
220 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
221 cv_init(&vp->v_cv, "vnode");
222 /*
223 * Done by memset() above.
224 * LIST_INIT(&vp->v_nclist);
225 * LIST_INIT(&vp->v_dnclist);
226 */
227
228 if (mp != NULL) {
229 vp->v_mount = mp;
230 vp->v_type = VBAD;
231 vp->v_iflag = VI_MARKER;
232 } else {
233 rw_init(&vp->v_lock);
234 }
235
236 return vp;
237 }
238
239 /*
240 * Free an unused, unreferenced vnode.
241 */
242 void
243 vnfree(vnode_t *vp)
244 {
245
246 KASSERT(vp->v_usecount == 0);
247
248 if ((vp->v_iflag & VI_MARKER) == 0) {
249 rw_destroy(&vp->v_lock);
250 mutex_enter(&vnode_free_list_lock);
251 numvnodes--;
252 mutex_exit(&vnode_free_list_lock);
253 }
254
255 /*
256 * Note: the vnode interlock will either be freed, of reference
257 * dropped (if VI_LOCKSHARE was in use).
258 */
259 uvm_obj_destroy(&vp->v_uobj, true);
260 cv_destroy(&vp->v_cv);
261 pool_cache_put(vnode_cache, vp);
262 }
263
264 /*
265 * cleanvnode: grab a vnode from freelist, clean and free it.
266 *
267 * => Releases vnode_free_list_lock.
268 */
269 static int
270 cleanvnode(void)
271 {
272 vnode_t *vp;
273 vnodelst_t *listhd;
274 struct mount *mp;
275
276 KASSERT(mutex_owned(&vnode_free_list_lock));
277
278 listhd = &vnode_free_list;
279 try_nextlist:
280 TAILQ_FOREACH(vp, listhd, v_freelist) {
281 /*
282 * It's safe to test v_usecount and v_iflag
283 * without holding the interlock here, since
284 * these vnodes should never appear on the
285 * lists.
286 */
287 KASSERT(vp->v_usecount == 0);
288 KASSERT((vp->v_iflag & VI_CLEAN) == 0);
289 KASSERT(vp->v_freelisthd == listhd);
290
291 if (!mutex_tryenter(vp->v_interlock))
292 continue;
293 if ((vp->v_iflag & VI_XLOCK) != 0) {
294 mutex_exit(vp->v_interlock);
295 continue;
296 }
297 mp = vp->v_mount;
298 if (fstrans_start_nowait(mp, FSTRANS_SHARED) != 0) {
299 mutex_exit(vp->v_interlock);
300 continue;
301 }
302 break;
303 }
304
305 if (vp == NULL) {
306 if (listhd == &vnode_free_list) {
307 listhd = &vnode_hold_list;
308 goto try_nextlist;
309 }
310 mutex_exit(&vnode_free_list_lock);
311 return EBUSY;
312 }
313
314 /* Remove it from the freelist. */
315 TAILQ_REMOVE(listhd, vp, v_freelist);
316 vp->v_freelisthd = NULL;
317 mutex_exit(&vnode_free_list_lock);
318
319 KASSERT(vp->v_usecount == 0);
320
321 /*
322 * The vnode is still associated with a file system, so we must
323 * clean it out before freeing it. We need to add a reference
324 * before doing this.
325 */
326 vp->v_usecount = 1;
327 KASSERT((vp->v_iflag & VI_CHANGING) == 0);
328 vp->v_iflag |= VI_CHANGING;
329 vclean(vp);
330 vrelel(vp, VRELEL_CHANGING_SET);
331 fstrans_done(mp);
332
333 return 0;
334 }
335
336 /*
337 * getnewvnode: return a fresh vnode.
338 *
339 * => Returns referenced vnode, moved into the mount queue.
340 * => Shares the interlock specified by 'slock', if it is not NULL.
341 */
342 int
343 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
344 kmutex_t *slock, vnode_t **vpp)
345 {
346 struct uvm_object *uobj __diagused;
347 vnode_t *vp;
348 int error = 0;
349
350 if (mp != NULL) {
351 /*
352 * Mark filesystem busy while we are creating a vnode.
353 * If unmount is in progress, this will fail.
354 */
355 error = vfs_busy(mp, NULL);
356 if (error)
357 return error;
358 }
359
360 vp = NULL;
361
362 /* Allocate a new vnode. */
363 mutex_enter(&vnode_free_list_lock);
364 numvnodes++;
365 if (numvnodes > desiredvnodes + desiredvnodes / 10)
366 cv_signal(&vdrain_cv);
367 mutex_exit(&vnode_free_list_lock);
368 vp = vnalloc(NULL);
369
370 KASSERT(vp->v_freelisthd == NULL);
371 KASSERT(LIST_EMPTY(&vp->v_nclist));
372 KASSERT(LIST_EMPTY(&vp->v_dnclist));
373
374 /* Initialize vnode. */
375 vp->v_usecount = 1;
376 vp->v_type = VNON;
377 vp->v_tag = tag;
378 vp->v_op = vops;
379 vp->v_data = NULL;
380
381 uobj = &vp->v_uobj;
382 KASSERT(uobj->pgops == &uvm_vnodeops);
383 KASSERT(uobj->uo_npages == 0);
384 KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
385 vp->v_size = vp->v_writesize = VSIZENOTSET;
386
387 /* Share the vnode_t::v_interlock, if requested. */
388 if (slock) {
389 /* Set the interlock and mark that it is shared. */
390 KASSERT(vp->v_mount == NULL);
391 mutex_obj_hold(slock);
392 uvm_obj_setlock(&vp->v_uobj, slock);
393 KASSERT(vp->v_interlock == slock);
394 vp->v_iflag |= VI_LOCKSHARE;
395 }
396
397 /* Finally, move vnode into the mount queue. */
398 vfs_insmntque(vp, mp);
399
400 if (mp != NULL) {
401 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
402 vp->v_vflag |= VV_MPSAFE;
403 vfs_unbusy(mp, true, NULL);
404 }
405
406 *vpp = vp;
407 return 0;
408 }
409
410 /*
411 * This is really just the reverse of getnewvnode(). Needed for
412 * VFS_VGET functions who may need to push back a vnode in case
413 * of a locking race.
414 */
415 void
416 ungetnewvnode(vnode_t *vp)
417 {
418
419 KASSERT(vp->v_usecount == 1);
420 KASSERT(vp->v_data == NULL);
421 KASSERT(vp->v_freelisthd == NULL);
422
423 mutex_enter(vp->v_interlock);
424 vp->v_iflag |= VI_CLEAN;
425 vrelel(vp, 0);
426 }
427
428 /*
429 * Helper thread to keep the number of vnodes below desiredvnodes.
430 */
431 static void
432 vdrain_thread(void *cookie)
433 {
434 int error;
435
436 mutex_enter(&vnode_free_list_lock);
437
438 for (;;) {
439 cv_timedwait(&vdrain_cv, &vnode_free_list_lock, hz);
440 while (numvnodes > desiredvnodes) {
441 error = cleanvnode();
442 if (error)
443 kpause("vndsbusy", false, hz, NULL);
444 mutex_enter(&vnode_free_list_lock);
445 if (error)
446 break;
447 }
448 }
449 }
450
451 /*
452 * Remove a vnode from its freelist.
453 */
454 void
455 vremfree(vnode_t *vp)
456 {
457
458 KASSERT(mutex_owned(vp->v_interlock));
459 KASSERT(vp->v_usecount == 0);
460
461 /*
462 * Note that the reference count must not change until
463 * the vnode is removed.
464 */
465 mutex_enter(&vnode_free_list_lock);
466 if (vp->v_holdcnt > 0) {
467 KASSERT(vp->v_freelisthd == &vnode_hold_list);
468 } else {
469 KASSERT(vp->v_freelisthd == &vnode_free_list);
470 }
471 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
472 vp->v_freelisthd = NULL;
473 mutex_exit(&vnode_free_list_lock);
474 }
475
476 /*
477 * vget: get a particular vnode from the free list, increment its reference
478 * count and lock it.
479 *
480 * => Should be called with v_interlock held.
481 *
482 * If VI_CHANGING is set, the vnode may be eliminated in vgone()/vclean().
483 * In that case, we cannot grab the vnode, so the process is awakened when
484 * the transition is completed, and an error returned to indicate that the
485 * vnode is no longer usable.
486 */
487 int
488 vget(vnode_t *vp, int flags)
489 {
490 int error = 0;
491
492 KASSERT((vp->v_iflag & VI_MARKER) == 0);
493 KASSERT(mutex_owned(vp->v_interlock));
494 KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT)) == 0);
495
496 /*
497 * Before adding a reference, we must remove the vnode
498 * from its freelist.
499 */
500 if (vp->v_usecount == 0) {
501 vremfree(vp);
502 vp->v_usecount = 1;
503 } else {
504 atomic_inc_uint(&vp->v_usecount);
505 }
506
507 /*
508 * If the vnode is in the process of changing state we wait
509 * for the change to complete and take care not to return
510 * a clean vnode.
511 */
512 if ((vp->v_iflag & VI_CHANGING) != 0) {
513 if ((flags & LK_NOWAIT) != 0) {
514 vrelel(vp, 0);
515 return EBUSY;
516 }
517 vwait(vp, VI_CHANGING);
518 if ((vp->v_iflag & VI_CLEAN) != 0) {
519 vrelel(vp, 0);
520 return ENOENT;
521 }
522 }
523
524 /*
525 * Ok, we got it in good shape. Just locking left.
526 */
527 KASSERT((vp->v_iflag & VI_CLEAN) == 0);
528 mutex_exit(vp->v_interlock);
529 if (flags & (LK_EXCLUSIVE | LK_SHARED)) {
530 error = vn_lock(vp, flags);
531 if (error != 0) {
532 vrele(vp);
533 }
534 }
535 return error;
536 }
537
538 /*
539 * vput: unlock and release the reference.
540 */
541 void
542 vput(vnode_t *vp)
543 {
544
545 KASSERT((vp->v_iflag & VI_MARKER) == 0);
546
547 VOP_UNLOCK(vp);
548 vrele(vp);
549 }
550
551 /*
552 * Try to drop reference on a vnode. Abort if we are releasing the
553 * last reference. Note: this _must_ succeed if not the last reference.
554 */
555 static inline bool
556 vtryrele(vnode_t *vp)
557 {
558 u_int use, next;
559
560 for (use = vp->v_usecount;; use = next) {
561 if (use == 1) {
562 return false;
563 }
564 KASSERT(use > 1);
565 next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
566 if (__predict_true(next == use)) {
567 return true;
568 }
569 }
570 }
571
572 /*
573 * Vnode release. If reference count drops to zero, call inactive
574 * routine and either return to freelist or free to the pool.
575 */
576 static void
577 vrelel(vnode_t *vp, int flags)
578 {
579 bool recycle, defer;
580 int error;
581
582 KASSERT(mutex_owned(vp->v_interlock));
583 KASSERT((vp->v_iflag & VI_MARKER) == 0);
584 KASSERT(vp->v_freelisthd == NULL);
585
586 if (__predict_false(vp->v_op == dead_vnodeop_p &&
587 (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) {
588 vnpanic(vp, "dead but not clean");
589 }
590
591 /*
592 * If not the last reference, just drop the reference count
593 * and unlock.
594 */
595 if (vtryrele(vp)) {
596 if ((flags & VRELEL_CHANGING_SET) != 0) {
597 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
598 vp->v_iflag &= ~VI_CHANGING;
599 cv_broadcast(&vp->v_cv);
600 }
601 mutex_exit(vp->v_interlock);
602 return;
603 }
604 if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
605 vnpanic(vp, "%s: bad ref count", __func__);
606 }
607
608 KASSERT((vp->v_iflag & VI_XLOCK) == 0);
609
610 #ifdef DIAGNOSTIC
611 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
612 vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
613 vprint("vrelel: missing VOP_CLOSE()", vp);
614 }
615 #endif
616
617 /*
618 * If not clean, deactivate the vnode, but preserve
619 * our reference across the call to VOP_INACTIVE().
620 */
621 if ((vp->v_iflag & VI_CLEAN) == 0) {
622 recycle = false;
623
624 /*
625 * XXX This ugly block can be largely eliminated if
626 * locking is pushed down into the file systems.
627 *
628 * Defer vnode release to vrele_thread if caller
629 * requests it explicitly or is the pagedaemon.
630 */
631 if ((curlwp == uvm.pagedaemon_lwp) ||
632 (flags & VRELEL_ASYNC_RELE) != 0) {
633 defer = true;
634 } else if (curlwp == vrele_lwp) {
635 /*
636 * We have to try harder.
637 */
638 mutex_exit(vp->v_interlock);
639 error = VOP_LOCK(vp, LK_EXCLUSIVE);
640 KASSERT(error == 0);
641 mutex_enter(vp->v_interlock);
642 /*
643 * If the node got another reference while sleeping,
644 * don't try to inactivate it yet.
645 */
646 if (__predict_false(vtryrele(vp))) {
647 VOP_UNLOCK(vp);
648 if ((flags & VRELEL_CHANGING_SET) != 0) {
649 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
650 vp->v_iflag &= ~VI_CHANGING;
651 cv_broadcast(&vp->v_cv);
652 }
653 mutex_exit(vp->v_interlock);
654 return;
655 }
656 defer = false;
657 } else {
658 /* If we can't acquire the lock, then defer. */
659 error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT);
660 defer = (error != 0);
661 }
662
663 KASSERT(mutex_owned(vp->v_interlock));
664 KASSERT(! (curlwp == vrele_lwp && defer));
665
666 if (defer) {
667 /*
668 * Defer reclaim to the kthread; it's not safe to
669 * clean it here. We donate it our last reference.
670 */
671 if ((flags & VRELEL_CHANGING_SET) != 0) {
672 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
673 vp->v_iflag &= ~VI_CHANGING;
674 cv_broadcast(&vp->v_cv);
675 }
676 mutex_enter(&vrele_lock);
677 TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist);
678 if (++vrele_pending > (desiredvnodes >> 8))
679 cv_signal(&vrele_cv);
680 mutex_exit(&vrele_lock);
681 mutex_exit(vp->v_interlock);
682 return;
683 }
684
685 if ((flags & VRELEL_CHANGING_SET) == 0) {
686 KASSERT((vp->v_iflag & VI_CHANGING) == 0);
687 vp->v_iflag |= VI_CHANGING;
688 }
689 mutex_exit(vp->v_interlock);
690
691 /*
692 * The vnode can gain another reference while being
693 * deactivated. If VOP_INACTIVE() indicates that
694 * the described file has been deleted, then recycle
695 * the vnode irrespective of additional references.
696 * Another thread may be waiting to re-use the on-disk
697 * inode.
698 *
699 * Note that VOP_INACTIVE() will drop the vnode lock.
700 */
701 VOP_INACTIVE(vp, &recycle);
702 mutex_enter(vp->v_interlock);
703 if (!recycle) {
704 if (vtryrele(vp)) {
705 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
706 vp->v_iflag &= ~VI_CHANGING;
707 cv_broadcast(&vp->v_cv);
708 mutex_exit(vp->v_interlock);
709 return;
710 }
711 }
712
713 /* Take care of space accounting. */
714 if (vp->v_iflag & VI_EXECMAP) {
715 atomic_add_int(&uvmexp.execpages,
716 -vp->v_uobj.uo_npages);
717 atomic_add_int(&uvmexp.filepages,
718 vp->v_uobj.uo_npages);
719 }
720 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
721 vp->v_vflag &= ~VV_MAPPED;
722
723 /*
724 * Recycle the vnode if the file is now unused (unlinked),
725 * otherwise just free it.
726 */
727 if (recycle) {
728 vclean(vp);
729 }
730 KASSERT(vp->v_usecount > 0);
731 } else { /* vnode was already clean */
732 if ((flags & VRELEL_CHANGING_SET) == 0) {
733 KASSERT((vp->v_iflag & VI_CHANGING) == 0);
734 vp->v_iflag |= VI_CHANGING;
735 }
736 }
737
738 if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
739 /* Gained another reference while being reclaimed. */
740 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
741 vp->v_iflag &= ~VI_CHANGING;
742 cv_broadcast(&vp->v_cv);
743 mutex_exit(vp->v_interlock);
744 return;
745 }
746
747 if ((vp->v_iflag & VI_CLEAN) != 0) {
748 /*
749 * It's clean so destroy it. It isn't referenced
750 * anywhere since it has been reclaimed.
751 */
752 KASSERT(vp->v_holdcnt == 0);
753 KASSERT(vp->v_writecount == 0);
754 mutex_exit(vp->v_interlock);
755 vfs_insmntque(vp, NULL);
756 if (vp->v_type == VBLK || vp->v_type == VCHR) {
757 spec_node_destroy(vp);
758 }
759 vnfree(vp);
760 } else {
761 /*
762 * Otherwise, put it back onto the freelist. It
763 * can't be destroyed while still associated with
764 * a file system.
765 */
766 mutex_enter(&vnode_free_list_lock);
767 if (vp->v_holdcnt > 0) {
768 vp->v_freelisthd = &vnode_hold_list;
769 } else {
770 vp->v_freelisthd = &vnode_free_list;
771 }
772 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
773 mutex_exit(&vnode_free_list_lock);
774 KASSERT((vp->v_iflag & VI_CHANGING) != 0);
775 vp->v_iflag &= ~VI_CHANGING;
776 cv_broadcast(&vp->v_cv);
777 mutex_exit(vp->v_interlock);
778 }
779 }
780
781 void
782 vrele(vnode_t *vp)
783 {
784
785 KASSERT((vp->v_iflag & VI_MARKER) == 0);
786
787 if (vtryrele(vp)) {
788 return;
789 }
790 mutex_enter(vp->v_interlock);
791 vrelel(vp, 0);
792 }
793
794 /*
795 * Asynchronous vnode release, vnode is released in different context.
796 */
797 void
798 vrele_async(vnode_t *vp)
799 {
800
801 KASSERT((vp->v_iflag & VI_MARKER) == 0);
802
803 if (vtryrele(vp)) {
804 return;
805 }
806 mutex_enter(vp->v_interlock);
807 vrelel(vp, VRELEL_ASYNC_RELE);
808 }
809
810 static void
811 vrele_thread(void *cookie)
812 {
813 vnode_t *vp;
814
815 for (;;) {
816 mutex_enter(&vrele_lock);
817 while (TAILQ_EMPTY(&vrele_list)) {
818 vrele_gen++;
819 cv_broadcast(&vrele_cv);
820 cv_timedwait(&vrele_cv, &vrele_lock, hz);
821 }
822 vp = TAILQ_FIRST(&vrele_list);
823 TAILQ_REMOVE(&vrele_list, vp, v_freelist);
824 vrele_pending--;
825 mutex_exit(&vrele_lock);
826
827 /*
828 * If not the last reference, then ignore the vnode
829 * and look for more work.
830 */
831 mutex_enter(vp->v_interlock);
832 vrelel(vp, 0);
833 }
834 }
835
836 void
837 vrele_flush(void)
838 {
839 int gen;
840
841 mutex_enter(&vrele_lock);
842 gen = vrele_gen;
843 while (vrele_pending && gen == vrele_gen) {
844 cv_broadcast(&vrele_cv);
845 cv_wait(&vrele_cv, &vrele_lock);
846 }
847 mutex_exit(&vrele_lock);
848 }
849
850 /*
851 * Vnode reference, where a reference is already held by some other
852 * object (for example, a file structure).
853 */
854 void
855 vref(vnode_t *vp)
856 {
857
858 KASSERT((vp->v_iflag & VI_MARKER) == 0);
859 KASSERT(vp->v_usecount != 0);
860
861 atomic_inc_uint(&vp->v_usecount);
862 }
863
864 /*
865 * Page or buffer structure gets a reference.
866 * Called with v_interlock held.
867 */
868 void
869 vholdl(vnode_t *vp)
870 {
871
872 KASSERT(mutex_owned(vp->v_interlock));
873 KASSERT((vp->v_iflag & VI_MARKER) == 0);
874
875 if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) {
876 mutex_enter(&vnode_free_list_lock);
877 KASSERT(vp->v_freelisthd == &vnode_free_list);
878 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
879 vp->v_freelisthd = &vnode_hold_list;
880 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
881 mutex_exit(&vnode_free_list_lock);
882 }
883 }
884
885 /*
886 * Page or buffer structure frees a reference.
887 * Called with v_interlock held.
888 */
889 void
890 holdrelel(vnode_t *vp)
891 {
892
893 KASSERT(mutex_owned(vp->v_interlock));
894 KASSERT((vp->v_iflag & VI_MARKER) == 0);
895
896 if (vp->v_holdcnt <= 0) {
897 vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
898 }
899
900 vp->v_holdcnt--;
901 if (vp->v_holdcnt == 0 && vp->v_usecount == 0) {
902 mutex_enter(&vnode_free_list_lock);
903 KASSERT(vp->v_freelisthd == &vnode_hold_list);
904 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
905 vp->v_freelisthd = &vnode_free_list;
906 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
907 mutex_exit(&vnode_free_list_lock);
908 }
909 }
910
911 /*
912 * Disassociate the underlying file system from a vnode.
913 *
914 * Must be called with the interlock held, and will return with it held.
915 */
916 static void
917 vclean(vnode_t *vp)
918 {
919 lwp_t *l = curlwp;
920 bool recycle, active, doclose;
921 int error;
922
923 KASSERT(mutex_owned(vp->v_interlock));
924 KASSERT((vp->v_iflag & VI_MARKER) == 0);
925 KASSERT(vp->v_usecount != 0);
926
927 /* If cleaning is already in progress wait until done and return. */
928 if (vp->v_iflag & VI_XLOCK) {
929 vwait(vp, VI_XLOCK);
930 return;
931 }
932
933 /* If already clean, nothing to do. */
934 if ((vp->v_iflag & VI_CLEAN) != 0) {
935 return;
936 }
937
938 /*
939 * Prevent the vnode from being recycled or brought into use
940 * while we clean it out.
941 */
942 vp->v_iflag |= VI_XLOCK;
943 if (vp->v_iflag & VI_EXECMAP) {
944 atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
945 atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
946 }
947 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
948 active = (vp->v_usecount > 1);
949
950 /* XXXAD should not lock vnode under layer */
951 mutex_exit(vp->v_interlock);
952 VOP_LOCK(vp, LK_EXCLUSIVE);
953
954 doclose = ! (active && vp->v_type == VBLK &&
955 spec_node_getmountedfs(vp) != NULL);
956
957 /*
958 * Clean out any cached data associated with the vnode.
959 * If purging an active vnode, it must be closed and
960 * deactivated before being reclaimed. Note that the
961 * VOP_INACTIVE will unlock the vnode.
962 */
963 if (doclose) {
964 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
965 if (error != 0) {
966 if (wapbl_vphaswapbl(vp))
967 WAPBL_DISCARD(wapbl_vptomp(vp));
968 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
969 }
970 KASSERT(error == 0);
971 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
972 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
973 spec_node_revoke(vp);
974 }
975 }
976 if (active) {
977 VOP_INACTIVE(vp, &recycle);
978 } else {
979 /*
980 * Any other processes trying to obtain this lock must first
981 * wait for VI_XLOCK to clear, then call the new lock operation.
982 */
983 VOP_UNLOCK(vp);
984 }
985
986 /* Disassociate the underlying file system from the vnode. */
987 if (VOP_RECLAIM(vp)) {
988 vnpanic(vp, "%s: cannot reclaim", __func__);
989 }
990
991 KASSERT(vp->v_data == NULL);
992 KASSERT(vp->v_uobj.uo_npages == 0);
993
994 if (vp->v_type == VREG && vp->v_ractx != NULL) {
995 uvm_ra_freectx(vp->v_ractx);
996 vp->v_ractx = NULL;
997 }
998
999 /* Purge name cache. */
1000 cache_purge(vp);
1001
1002 /*
1003 * The vnode isn't clean, but still resides on the mount list. Remove
1004 * it. XXX This is a bit dodgy.
1005 */
1006 if (! doclose)
1007 vfs_insmntque(vp, NULL);
1008
1009 /* Done with purge, notify sleepers of the grim news. */
1010 mutex_enter(vp->v_interlock);
1011 if (doclose) {
1012 vp->v_op = dead_vnodeop_p;
1013 vp->v_vflag |= VV_LOCKSWORK;
1014 vp->v_iflag |= VI_CLEAN;
1015 } else {
1016 vp->v_op = spec_vnodeop_p;
1017 vp->v_vflag &= ~VV_LOCKSWORK;
1018 }
1019 vp->v_tag = VT_NON;
1020 KNOTE(&vp->v_klist, NOTE_REVOKE);
1021 vp->v_iflag &= ~VI_XLOCK;
1022 cv_broadcast(&vp->v_cv);
1023
1024 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1025 }
1026
1027 /*
1028 * Recycle an unused vnode to the front of the free list.
1029 * Release the passed interlock if the vnode will be recycled.
1030 */
1031 int
1032 vrecycle(vnode_t *vp, kmutex_t *inter_lkp)
1033 {
1034
1035 KASSERT((vp->v_iflag & VI_MARKER) == 0);
1036
1037 mutex_enter(vp->v_interlock);
1038 if (vp->v_usecount != 0 || (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) != 0) {
1039 mutex_exit(vp->v_interlock);
1040 return 0;
1041 }
1042 if (inter_lkp) {
1043 mutex_exit(inter_lkp);
1044 }
1045 vremfree(vp);
1046 vp->v_usecount = 1;
1047 KASSERT((vp->v_iflag & VI_CHANGING) == 0);
1048 vp->v_iflag |= VI_CHANGING;
1049 vclean(vp);
1050 vrelel(vp, VRELEL_CHANGING_SET);
1051 return 1;
1052 }
1053
1054 /*
1055 * Eliminate all activity associated with the requested vnode
1056 * and with all vnodes aliased to the requested vnode.
1057 */
1058 void
1059 vrevoke(vnode_t *vp)
1060 {
1061 vnode_t *vq;
1062 enum vtype type;
1063 dev_t dev;
1064
1065 KASSERT(vp->v_usecount > 0);
1066
1067 mutex_enter(vp->v_interlock);
1068 if ((vp->v_iflag & VI_CLEAN) != 0) {
1069 mutex_exit(vp->v_interlock);
1070 return;
1071 } else if (vp->v_type != VBLK && vp->v_type != VCHR) {
1072 atomic_inc_uint(&vp->v_usecount);
1073 mutex_exit(vp->v_interlock);
1074 vgone(vp);
1075 return;
1076 } else {
1077 dev = vp->v_rdev;
1078 type = vp->v_type;
1079 mutex_exit(vp->v_interlock);
1080 }
1081
1082 while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
1083 vgone(vq);
1084 }
1085 }
1086
1087 /*
1088 * Eliminate all activity associated with a vnode in preparation for
1089 * reuse. Drops a reference from the vnode.
1090 */
1091 void
1092 vgone(vnode_t *vp)
1093 {
1094
1095 mutex_enter(vp->v_interlock);
1096 if ((vp->v_iflag & VI_CHANGING) != 0)
1097 vwait(vp, VI_CHANGING);
1098 vp->v_iflag |= VI_CHANGING;
1099 vclean(vp);
1100 vrelel(vp, VRELEL_CHANGING_SET);
1101 }
1102
1103 /*
1104 * Update outstanding I/O count and do wakeup if requested.
1105 */
1106 void
1107 vwakeup(struct buf *bp)
1108 {
1109 vnode_t *vp;
1110
1111 if ((vp = bp->b_vp) == NULL)
1112 return;
1113
1114 KASSERT(bp->b_objlock == vp->v_interlock);
1115 KASSERT(mutex_owned(bp->b_objlock));
1116
1117 if (--vp->v_numoutput < 0)
1118 vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
1119 if (vp->v_numoutput == 0)
1120 cv_broadcast(&vp->v_cv);
1121 }
1122
1123 /*
1124 * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
1125 * recycled.
1126 */
1127 void
1128 vwait(vnode_t *vp, int flags)
1129 {
1130
1131 KASSERT(mutex_owned(vp->v_interlock));
1132 KASSERT(vp->v_usecount != 0);
1133
1134 while ((vp->v_iflag & flags) != 0)
1135 cv_wait(&vp->v_cv, vp->v_interlock);
1136 }
1137
1138 int
1139 vfs_drainvnodes(long target)
1140 {
1141 int error;
1142
1143 mutex_enter(&vnode_free_list_lock);
1144
1145 while (numvnodes > target) {
1146 error = cleanvnode();
1147 if (error != 0)
1148 return error;
1149 mutex_enter(&vnode_free_list_lock);
1150 }
1151
1152 mutex_exit(&vnode_free_list_lock);
1153
1154 return 0;
1155 }
1156
1157 void
1158 vnpanic(vnode_t *vp, const char *fmt, ...)
1159 {
1160 va_list ap;
1161
1162 #ifdef DIAGNOSTIC
1163 vprint(NULL, vp);
1164 #endif
1165 va_start(ap, fmt);
1166 vpanic(fmt, ap);
1167 va_end(ap);
1168 }
1169