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