vfs_vnode.c revision 1.24 1 /* $NetBSD: vfs_vnode.c,v 1.24 2013/11/03 08:33:00 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.24 2013/11/03 08:33:00 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 #define DOCLOSE 0x0008 /* vclean: close active files */
147
148 /* Flags to vrelel. */
149 #define VRELEL_ASYNC_RELE 0x0001 /* Always defer to vrele thread. */
150
151 u_int numvnodes __cacheline_aligned;
152
153 static pool_cache_t vnode_cache __read_mostly;
154
155 /*
156 * There are two free lists: one is for vnodes which have no buffer/page
157 * references and one for those which do (i.e. v_holdcnt is non-zero).
158 * Vnode recycling mechanism first attempts to look into the former list.
159 */
160 static kmutex_t vnode_free_list_lock __cacheline_aligned;
161 static vnodelst_t vnode_free_list __cacheline_aligned;
162 static vnodelst_t vnode_hold_list __cacheline_aligned;
163 static kcondvar_t vdrain_cv __cacheline_aligned;
164
165 static vnodelst_t vrele_list __cacheline_aligned;
166 static kmutex_t vrele_lock __cacheline_aligned;
167 static kcondvar_t vrele_cv __cacheline_aligned;
168 static lwp_t * vrele_lwp __cacheline_aligned;
169 static int vrele_pending __cacheline_aligned;
170 static int vrele_gen __cacheline_aligned;
171
172 static int cleanvnode(void);
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 vclean(vp, DOCLOSE);
328 vrelel(vp, 0);
329 fstrans_done(mp);
330
331 return 0;
332 }
333
334 /*
335 * getnewvnode: return a fresh vnode.
336 *
337 * => Returns referenced vnode, moved into the mount queue.
338 * => Shares the interlock specified by 'slock', if it is not NULL.
339 */
340 int
341 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
342 kmutex_t *slock, vnode_t **vpp)
343 {
344 struct uvm_object *uobj __diagused;
345 vnode_t *vp;
346 int error = 0;
347
348 if (mp != NULL) {
349 /*
350 * Mark filesystem busy while we are creating a vnode.
351 * If unmount is in progress, this will fail.
352 */
353 error = vfs_busy(mp, NULL);
354 if (error)
355 return error;
356 }
357
358 vp = NULL;
359
360 /* Allocate a new vnode. */
361 mutex_enter(&vnode_free_list_lock);
362 numvnodes++;
363 if (numvnodes > desiredvnodes + desiredvnodes / 10)
364 cv_signal(&vdrain_cv);
365 mutex_exit(&vnode_free_list_lock);
366 vp = vnalloc(NULL);
367
368 KASSERT(vp->v_freelisthd == NULL);
369 KASSERT(LIST_EMPTY(&vp->v_nclist));
370 KASSERT(LIST_EMPTY(&vp->v_dnclist));
371
372 /* Initialize vnode. */
373 vp->v_usecount = 1;
374 vp->v_type = VNON;
375 vp->v_tag = tag;
376 vp->v_op = vops;
377 vp->v_data = NULL;
378
379 uobj = &vp->v_uobj;
380 KASSERT(uobj->pgops == &uvm_vnodeops);
381 KASSERT(uobj->uo_npages == 0);
382 KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
383 vp->v_size = vp->v_writesize = VSIZENOTSET;
384
385 /* Share the vnode_t::v_interlock, if requested. */
386 if (slock) {
387 /* Set the interlock and mark that it is shared. */
388 KASSERT(vp->v_mount == NULL);
389 mutex_obj_hold(slock);
390 uvm_obj_setlock(&vp->v_uobj, slock);
391 KASSERT(vp->v_interlock == slock);
392 vp->v_iflag |= VI_LOCKSHARE;
393 }
394
395 /* Finally, move vnode into the mount queue. */
396 vfs_insmntque(vp, mp);
397
398 if (mp != NULL) {
399 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
400 vp->v_vflag |= VV_MPSAFE;
401 vfs_unbusy(mp, true, NULL);
402 }
403
404 *vpp = vp;
405 return 0;
406 }
407
408 /*
409 * This is really just the reverse of getnewvnode(). Needed for
410 * VFS_VGET functions who may need to push back a vnode in case
411 * of a locking race.
412 */
413 void
414 ungetnewvnode(vnode_t *vp)
415 {
416
417 KASSERT(vp->v_usecount == 1);
418 KASSERT(vp->v_data == NULL);
419 KASSERT(vp->v_freelisthd == NULL);
420
421 mutex_enter(vp->v_interlock);
422 vp->v_iflag |= VI_CLEAN;
423 vrelel(vp, 0);
424 }
425
426 /*
427 * Helper thread to keep the number of vnodes below desiredvnodes.
428 */
429 static void
430 vdrain_thread(void *cookie)
431 {
432 int error;
433
434 mutex_enter(&vnode_free_list_lock);
435
436 for (;;) {
437 cv_timedwait(&vdrain_cv, &vnode_free_list_lock, hz);
438 while (numvnodes > desiredvnodes) {
439 error = cleanvnode();
440 if (error)
441 kpause("vndsbusy", false, hz, NULL);
442 mutex_enter(&vnode_free_list_lock);
443 if (error)
444 break;
445 }
446 }
447 }
448
449 /*
450 * Remove a vnode from its freelist.
451 */
452 void
453 vremfree(vnode_t *vp)
454 {
455
456 KASSERT(mutex_owned(vp->v_interlock));
457 KASSERT(vp->v_usecount == 0);
458
459 /*
460 * Note that the reference count must not change until
461 * the vnode is removed.
462 */
463 mutex_enter(&vnode_free_list_lock);
464 if (vp->v_holdcnt > 0) {
465 KASSERT(vp->v_freelisthd == &vnode_hold_list);
466 } else {
467 KASSERT(vp->v_freelisthd == &vnode_free_list);
468 }
469 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
470 vp->v_freelisthd = NULL;
471 mutex_exit(&vnode_free_list_lock);
472 }
473
474 /*
475 * vget: get a particular vnode from the free list, increment its reference
476 * count and lock it.
477 *
478 * => Should be called with v_interlock held.
479 *
480 * If VI_XLOCK is set, the vnode is being eliminated in vgone()/vclean().
481 * In that case, we cannot grab the vnode, so the process is awakened when
482 * the transition is completed, and an error returned to indicate that the
483 * vnode is no longer usable (e.g. changed to a new file system type).
484 */
485 int
486 vget(vnode_t *vp, int flags)
487 {
488 int error = 0;
489
490 KASSERT((vp->v_iflag & VI_MARKER) == 0);
491 KASSERT(mutex_owned(vp->v_interlock));
492 KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT)) == 0);
493
494 /*
495 * Before adding a reference, we must remove the vnode
496 * from its freelist.
497 */
498 if (vp->v_usecount == 0) {
499 vremfree(vp);
500 vp->v_usecount = 1;
501 } else {
502 atomic_inc_uint(&vp->v_usecount);
503 }
504
505 /*
506 * If the vnode is in the process of being cleaned out for
507 * another use, we wait for the cleaning to finish and then
508 * return failure. Cleaning is determined by checking if
509 * the VI_XLOCK flag is set.
510 */
511 if ((vp->v_iflag & VI_XLOCK) != 0) {
512 if ((flags & LK_NOWAIT) != 0) {
513 vrelel(vp, 0);
514 return EBUSY;
515 }
516 vwait(vp, VI_XLOCK);
517 vrelel(vp, 0);
518 return ENOENT;
519 }
520
521 if ((vp->v_iflag & VI_INACTNOW) != 0) {
522 /*
523 * if it's being desactived, wait for it to complete.
524 * Make sure to not return a clean vnode.
525 */
526 if ((flags & LK_NOWAIT) != 0) {
527 vrelel(vp, 0);
528 return EBUSY;
529 }
530 vwait(vp, VI_INACTNOW);
531 if ((vp->v_iflag & VI_CLEAN) != 0) {
532 vrelel(vp, 0);
533 return ENOENT;
534 }
535 }
536
537 /*
538 * Ok, we got it in good shape. Just locking left.
539 */
540 KASSERT((vp->v_iflag & VI_CLEAN) == 0);
541 mutex_exit(vp->v_interlock);
542 if (flags & (LK_EXCLUSIVE | LK_SHARED)) {
543 error = vn_lock(vp, flags);
544 if (error != 0) {
545 vrele(vp);
546 }
547 }
548 return error;
549 }
550
551 /*
552 * vput: unlock and release the reference.
553 */
554 void
555 vput(vnode_t *vp)
556 {
557
558 KASSERT((vp->v_iflag & VI_MARKER) == 0);
559
560 VOP_UNLOCK(vp);
561 vrele(vp);
562 }
563
564 /*
565 * Try to drop reference on a vnode. Abort if we are releasing the
566 * last reference. Note: this _must_ succeed if not the last reference.
567 */
568 static inline bool
569 vtryrele(vnode_t *vp)
570 {
571 u_int use, next;
572
573 for (use = vp->v_usecount;; use = next) {
574 if (use == 1) {
575 return false;
576 }
577 KASSERT(use > 1);
578 next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
579 if (__predict_true(next == use)) {
580 return true;
581 }
582 }
583 }
584
585 /*
586 * Vnode release. If reference count drops to zero, call inactive
587 * routine and either return to freelist or free to the pool.
588 */
589 static void
590 vrelel(vnode_t *vp, int flags)
591 {
592 bool recycle, defer;
593 int error;
594
595 KASSERT(mutex_owned(vp->v_interlock));
596 KASSERT((vp->v_iflag & VI_MARKER) == 0);
597 KASSERT(vp->v_freelisthd == NULL);
598
599 if (__predict_false(vp->v_op == dead_vnodeop_p &&
600 (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) {
601 vnpanic(vp, "dead but not clean");
602 }
603
604 /*
605 * If not the last reference, just drop the reference count
606 * and unlock.
607 */
608 if (vtryrele(vp)) {
609 vp->v_iflag |= VI_INACTREDO;
610 mutex_exit(vp->v_interlock);
611 return;
612 }
613 if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
614 vnpanic(vp, "%s: bad ref count", __func__);
615 }
616
617 KASSERT((vp->v_iflag & VI_XLOCK) == 0);
618
619 #ifdef DIAGNOSTIC
620 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
621 vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
622 vprint("vrelel: missing VOP_CLOSE()", vp);
623 }
624 #endif
625
626 /*
627 * If not clean, deactivate the vnode, but preserve
628 * our reference across the call to VOP_INACTIVE().
629 */
630 retry:
631 if ((vp->v_iflag & VI_CLEAN) == 0) {
632 recycle = false;
633 vp->v_iflag |= VI_INACTNOW;
634
635 /*
636 * XXX This ugly block can be largely eliminated if
637 * locking is pushed down into the file systems.
638 *
639 * Defer vnode release to vrele_thread if caller
640 * requests it explicitly.
641 */
642 if ((curlwp == uvm.pagedaemon_lwp) ||
643 (flags & VRELEL_ASYNC_RELE) != 0) {
644 /* The pagedaemon can't wait around; defer. */
645 defer = true;
646 } else if (curlwp == vrele_lwp) {
647 /*
648 * We have to try harder. But we can't sleep
649 * with VI_INACTNOW as vget() may be waiting on it.
650 */
651 vp->v_iflag &= ~(VI_INACTREDO|VI_INACTNOW);
652 cv_broadcast(&vp->v_cv);
653 mutex_exit(vp->v_interlock);
654 error = vn_lock(vp, LK_EXCLUSIVE);
655 if (error != 0) {
656 /* XXX */
657 vnpanic(vp, "%s: unable to lock %p",
658 __func__, vp);
659 }
660 mutex_enter(vp->v_interlock);
661 /*
662 * if we did get another reference while
663 * sleeping, don't try to inactivate it yet.
664 */
665 if (__predict_false(vtryrele(vp))) {
666 VOP_UNLOCK(vp);
667 mutex_exit(vp->v_interlock);
668 return;
669 }
670 vp->v_iflag |= VI_INACTNOW;
671 mutex_exit(vp->v_interlock);
672 defer = false;
673 } else if ((vp->v_iflag & VI_LAYER) != 0) {
674 /*
675 * Acquiring the stack's lock in vclean() even
676 * for an honest vput/vrele is dangerous because
677 * our caller may hold other vnode locks; defer.
678 */
679 defer = true;
680 } else {
681 /* If we can't acquire the lock, then defer. */
682 vp->v_iflag &= ~VI_INACTREDO;
683 mutex_exit(vp->v_interlock);
684 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
685 if (error != 0) {
686 defer = true;
687 mutex_enter(vp->v_interlock);
688 } else {
689 defer = false;
690 }
691 }
692
693 if (defer) {
694 /*
695 * Defer reclaim to the kthread; it's not safe to
696 * clean it here. We donate it our last reference.
697 */
698 KASSERT(mutex_owned(vp->v_interlock));
699 vp->v_iflag &= ~VI_INACTNOW;
700 mutex_enter(&vrele_lock);
701 TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist);
702 if (++vrele_pending > (desiredvnodes >> 8))
703 cv_signal(&vrele_cv);
704 mutex_exit(&vrele_lock);
705 cv_broadcast(&vp->v_cv);
706 mutex_exit(vp->v_interlock);
707 return;
708 }
709
710 /*
711 * The vnode can gain another reference while being
712 * deactivated. If VOP_INACTIVE() indicates that
713 * the described file has been deleted, then recycle
714 * the vnode irrespective of additional references.
715 * Another thread may be waiting to re-use the on-disk
716 * inode.
717 *
718 * Note that VOP_INACTIVE() will drop the vnode lock.
719 */
720 VOP_INACTIVE(vp, &recycle);
721 mutex_enter(vp->v_interlock);
722 vp->v_iflag &= ~VI_INACTNOW;
723 cv_broadcast(&vp->v_cv);
724 if (!recycle) {
725 if (vtryrele(vp)) {
726 mutex_exit(vp->v_interlock);
727 return;
728 }
729
730 /*
731 * If we grew another reference while
732 * VOP_INACTIVE() was underway, retry.
733 */
734 if ((vp->v_iflag & VI_INACTREDO) != 0) {
735 goto retry;
736 }
737 }
738
739 /* Take care of space accounting. */
740 if (vp->v_iflag & VI_EXECMAP) {
741 atomic_add_int(&uvmexp.execpages,
742 -vp->v_uobj.uo_npages);
743 atomic_add_int(&uvmexp.filepages,
744 vp->v_uobj.uo_npages);
745 }
746 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
747 vp->v_vflag &= ~VV_MAPPED;
748
749 /*
750 * Recycle the vnode if the file is now unused (unlinked),
751 * otherwise just free it.
752 */
753 if (recycle) {
754 vclean(vp, DOCLOSE);
755 }
756 KASSERT(vp->v_usecount > 0);
757 }
758
759 if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
760 /* Gained another reference while being reclaimed. */
761 mutex_exit(vp->v_interlock);
762 return;
763 }
764
765 if ((vp->v_iflag & VI_CLEAN) != 0) {
766 /*
767 * It's clean so destroy it. It isn't referenced
768 * anywhere since it has been reclaimed.
769 */
770 KASSERT(vp->v_holdcnt == 0);
771 KASSERT(vp->v_writecount == 0);
772 mutex_exit(vp->v_interlock);
773 vfs_insmntque(vp, NULL);
774 if (vp->v_type == VBLK || vp->v_type == VCHR) {
775 spec_node_destroy(vp);
776 }
777 vnfree(vp);
778 } else {
779 /*
780 * Otherwise, put it back onto the freelist. It
781 * can't be destroyed while still associated with
782 * a file system.
783 */
784 mutex_enter(&vnode_free_list_lock);
785 if (vp->v_holdcnt > 0) {
786 vp->v_freelisthd = &vnode_hold_list;
787 } else {
788 vp->v_freelisthd = &vnode_free_list;
789 }
790 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
791 mutex_exit(&vnode_free_list_lock);
792 mutex_exit(vp->v_interlock);
793 }
794 }
795
796 void
797 vrele(vnode_t *vp)
798 {
799
800 KASSERT((vp->v_iflag & VI_MARKER) == 0);
801
802 if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
803 return;
804 }
805 mutex_enter(vp->v_interlock);
806 vrelel(vp, 0);
807 }
808
809 /*
810 * Asynchronous vnode release, vnode is released in different context.
811 */
812 void
813 vrele_async(vnode_t *vp)
814 {
815
816 KASSERT((vp->v_iflag & VI_MARKER) == 0);
817
818 if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
819 return;
820 }
821 mutex_enter(vp->v_interlock);
822 vrelel(vp, VRELEL_ASYNC_RELE);
823 }
824
825 static void
826 vrele_thread(void *cookie)
827 {
828 vnode_t *vp;
829
830 for (;;) {
831 mutex_enter(&vrele_lock);
832 while (TAILQ_EMPTY(&vrele_list)) {
833 vrele_gen++;
834 cv_broadcast(&vrele_cv);
835 cv_timedwait(&vrele_cv, &vrele_lock, hz);
836 }
837 vp = TAILQ_FIRST(&vrele_list);
838 TAILQ_REMOVE(&vrele_list, vp, v_freelist);
839 vrele_pending--;
840 mutex_exit(&vrele_lock);
841
842 /*
843 * If not the last reference, then ignore the vnode
844 * and look for more work.
845 */
846 mutex_enter(vp->v_interlock);
847 vrelel(vp, 0);
848 }
849 }
850
851 void
852 vrele_flush(void)
853 {
854 int gen;
855
856 mutex_enter(&vrele_lock);
857 gen = vrele_gen;
858 while (vrele_pending && gen == vrele_gen) {
859 cv_broadcast(&vrele_cv);
860 cv_wait(&vrele_cv, &vrele_lock);
861 }
862 mutex_exit(&vrele_lock);
863 }
864
865 /*
866 * Vnode reference, where a reference is already held by some other
867 * object (for example, a file structure).
868 */
869 void
870 vref(vnode_t *vp)
871 {
872
873 KASSERT((vp->v_iflag & VI_MARKER) == 0);
874 KASSERT(vp->v_usecount != 0);
875
876 atomic_inc_uint(&vp->v_usecount);
877 }
878
879 /*
880 * Page or buffer structure gets a reference.
881 * Called with v_interlock held.
882 */
883 void
884 vholdl(vnode_t *vp)
885 {
886
887 KASSERT(mutex_owned(vp->v_interlock));
888 KASSERT((vp->v_iflag & VI_MARKER) == 0);
889
890 if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) {
891 mutex_enter(&vnode_free_list_lock);
892 KASSERT(vp->v_freelisthd == &vnode_free_list);
893 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
894 vp->v_freelisthd = &vnode_hold_list;
895 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
896 mutex_exit(&vnode_free_list_lock);
897 }
898 }
899
900 /*
901 * Page or buffer structure frees a reference.
902 * Called with v_interlock held.
903 */
904 void
905 holdrelel(vnode_t *vp)
906 {
907
908 KASSERT(mutex_owned(vp->v_interlock));
909 KASSERT((vp->v_iflag & VI_MARKER) == 0);
910
911 if (vp->v_holdcnt <= 0) {
912 vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
913 }
914
915 vp->v_holdcnt--;
916 if (vp->v_holdcnt == 0 && vp->v_usecount == 0) {
917 mutex_enter(&vnode_free_list_lock);
918 KASSERT(vp->v_freelisthd == &vnode_hold_list);
919 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
920 vp->v_freelisthd = &vnode_free_list;
921 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
922 mutex_exit(&vnode_free_list_lock);
923 }
924 }
925
926 /*
927 * Disassociate the underlying file system from a vnode.
928 *
929 * Must be called with the interlock held, and will return with it held.
930 */
931 void
932 vclean(vnode_t *vp, int flags)
933 {
934 lwp_t *l = curlwp;
935 bool recycle, active, make_anon;
936 int error;
937
938 KASSERT(mutex_owned(vp->v_interlock));
939 KASSERT((vp->v_iflag & VI_MARKER) == 0);
940 KASSERT(vp->v_usecount != 0);
941
942 /* If cleaning is already in progress wait until done and return. */
943 if (vp->v_iflag & VI_XLOCK) {
944 vwait(vp, VI_XLOCK);
945 return;
946 }
947
948 /* If already clean, nothing to do. */
949 if ((vp->v_iflag & VI_CLEAN) != 0) {
950 return;
951 }
952
953 /*
954 * Prevent the vnode from being recycled or brought into use
955 * while we clean it out.
956 */
957 vp->v_iflag |= VI_XLOCK;
958 if (vp->v_iflag & VI_EXECMAP) {
959 atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
960 atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
961 }
962 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
963 active = (vp->v_usecount > 1);
964
965 /* XXXAD should not lock vnode under layer */
966 mutex_exit(vp->v_interlock);
967 VOP_LOCK(vp, LK_EXCLUSIVE);
968
969 make_anon = (active && vp->v_type == VBLK &&
970 spec_node_getmountedfs(vp) != NULL);
971 if (make_anon)
972 flags &= ~DOCLOSE;
973
974 /*
975 * Clean out any cached data associated with the vnode.
976 * If purging an active vnode, it must be closed and
977 * deactivated before being reclaimed. Note that the
978 * VOP_INACTIVE will unlock the vnode.
979 */
980 if (flags & DOCLOSE) {
981 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
982 if (error != 0) {
983 /* XXX, fix vn_start_write's grab of mp and use that. */
984
985 if (wapbl_vphaswapbl(vp))
986 WAPBL_DISCARD(wapbl_vptomp(vp));
987 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
988 }
989 KASSERT(error == 0);
990 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
991 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
992 spec_node_revoke(vp);
993 }
994 }
995 if (active) {
996 VOP_INACTIVE(vp, &recycle);
997 } else {
998 /*
999 * Any other processes trying to obtain this lock must first
1000 * wait for VI_XLOCK to clear, then call the new lock operation.
1001 */
1002 VOP_UNLOCK(vp);
1003 }
1004
1005 /* Disassociate the underlying file system from the vnode. */
1006 if (VOP_RECLAIM(vp)) {
1007 vnpanic(vp, "%s: cannot reclaim", __func__);
1008 }
1009
1010 KASSERT(vp->v_data == NULL);
1011 KASSERT(vp->v_uobj.uo_npages == 0);
1012
1013 if (vp->v_type == VREG && vp->v_ractx != NULL) {
1014 uvm_ra_freectx(vp->v_ractx);
1015 vp->v_ractx = NULL;
1016 }
1017
1018 /* Purge name cache. */
1019 cache_purge(vp);
1020
1021 /*
1022 * The vnode isn't clean, but still resides on the mount list. Remove
1023 * it. XXX This is a bit dodgy.
1024 */
1025 if (make_anon)
1026 vfs_insmntque(vp, NULL);
1027
1028 /* Done with purge, notify sleepers of the grim news. */
1029 mutex_enter(vp->v_interlock);
1030 if (make_anon) {
1031 vp->v_op = spec_vnodeop_p;
1032 } else {
1033 vp->v_op = dead_vnodeop_p;
1034 }
1035 vp->v_tag = VT_NON;
1036 KNOTE(&vp->v_klist, NOTE_REVOKE);
1037 vp->v_iflag &= ~VI_XLOCK;
1038 vp->v_vflag &= ~VV_LOCKSWORK;
1039 if ((flags & DOCLOSE) != 0) {
1040 vp->v_iflag |= VI_CLEAN;
1041 }
1042 cv_broadcast(&vp->v_cv);
1043
1044 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1045 }
1046
1047 /*
1048 * Recycle an unused vnode to the front of the free list.
1049 * Release the passed interlock if the vnode will be recycled.
1050 */
1051 int
1052 vrecycle(vnode_t *vp, kmutex_t *inter_lkp)
1053 {
1054
1055 KASSERT((vp->v_iflag & VI_MARKER) == 0);
1056
1057 mutex_enter(vp->v_interlock);
1058 if (vp->v_usecount != 0 || (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) != 0) {
1059 mutex_exit(vp->v_interlock);
1060 return 0;
1061 }
1062 if (inter_lkp) {
1063 mutex_exit(inter_lkp);
1064 }
1065 vremfree(vp);
1066 vp->v_usecount = 1;
1067 vclean(vp, DOCLOSE);
1068 vrelel(vp, 0);
1069 return 1;
1070 }
1071
1072 /*
1073 * Eliminate all activity associated with the requested vnode
1074 * and with all vnodes aliased to the requested vnode.
1075 */
1076 void
1077 vrevoke(vnode_t *vp)
1078 {
1079 vnode_t *vq;
1080 enum vtype type;
1081 dev_t dev;
1082
1083 KASSERT(vp->v_usecount > 0);
1084
1085 mutex_enter(vp->v_interlock);
1086 if ((vp->v_iflag & VI_CLEAN) != 0) {
1087 mutex_exit(vp->v_interlock);
1088 return;
1089 } else if (vp->v_type != VBLK && vp->v_type != VCHR) {
1090 atomic_inc_uint(&vp->v_usecount);
1091 vclean(vp, DOCLOSE);
1092 vrelel(vp, 0);
1093 return;
1094 } else {
1095 dev = vp->v_rdev;
1096 type = vp->v_type;
1097 mutex_exit(vp->v_interlock);
1098 }
1099
1100 while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
1101 mutex_enter(vq->v_interlock);
1102 vclean(vq, DOCLOSE);
1103 vrelel(vq, 0);
1104 }
1105 }
1106
1107 /*
1108 * Eliminate all activity associated with a vnode in preparation for
1109 * reuse. Drops a reference from the vnode.
1110 */
1111 void
1112 vgone(vnode_t *vp)
1113 {
1114
1115 mutex_enter(vp->v_interlock);
1116 vclean(vp, DOCLOSE);
1117 vrelel(vp, 0);
1118 }
1119
1120 /*
1121 * Update outstanding I/O count and do wakeup if requested.
1122 */
1123 void
1124 vwakeup(struct buf *bp)
1125 {
1126 vnode_t *vp;
1127
1128 if ((vp = bp->b_vp) == NULL)
1129 return;
1130
1131 KASSERT(bp->b_objlock == vp->v_interlock);
1132 KASSERT(mutex_owned(bp->b_objlock));
1133
1134 if (--vp->v_numoutput < 0)
1135 vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
1136 if (vp->v_numoutput == 0)
1137 cv_broadcast(&vp->v_cv);
1138 }
1139
1140 /*
1141 * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
1142 * recycled.
1143 */
1144 void
1145 vwait(vnode_t *vp, int flags)
1146 {
1147
1148 KASSERT(mutex_owned(vp->v_interlock));
1149 KASSERT(vp->v_usecount != 0);
1150
1151 while ((vp->v_iflag & flags) != 0)
1152 cv_wait(&vp->v_cv, vp->v_interlock);
1153 }
1154
1155 int
1156 vfs_drainvnodes(long target)
1157 {
1158 int error;
1159
1160 mutex_enter(&vnode_free_list_lock);
1161
1162 while (numvnodes > target) {
1163 error = cleanvnode();
1164 if (error != 0)
1165 return error;
1166 mutex_enter(&vnode_free_list_lock);
1167 }
1168
1169 mutex_exit(&vnode_free_list_lock);
1170
1171 return 0;
1172 }
1173
1174 void
1175 vnpanic(vnode_t *vp, const char *fmt, ...)
1176 {
1177 va_list ap;
1178
1179 #ifdef DIAGNOSTIC
1180 vprint(NULL, vp);
1181 #endif
1182 va_start(ap, fmt);
1183 vpanic(fmt, ap);
1184 va_end(ap);
1185 }
1186