vfs_vnode.c revision 1.53.2.4 1 1.53.2.4 pgoyette /* $NetBSD: vfs_vnode.c,v 1.53.2.4 2017/04/26 02:53:27 pgoyette Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.2 rmind * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1 rmind * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 rmind * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
10 1.1 rmind *
11 1.1 rmind * Redistribution and use in source and binary forms, with or without
12 1.1 rmind * modification, are permitted provided that the following conditions
13 1.1 rmind * are met:
14 1.1 rmind * 1. Redistributions of source code must retain the above copyright
15 1.1 rmind * notice, this list of conditions and the following disclaimer.
16 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 rmind * notice, this list of conditions and the following disclaimer in the
18 1.1 rmind * documentation and/or other materials provided with the distribution.
19 1.1 rmind *
20 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
31 1.1 rmind */
32 1.1 rmind
33 1.1 rmind /*
34 1.1 rmind * Copyright (c) 1989, 1993
35 1.1 rmind * The Regents of the University of California. All rights reserved.
36 1.1 rmind * (c) UNIX System Laboratories, Inc.
37 1.1 rmind * All or some portions of this file are derived from material licensed
38 1.1 rmind * to the University of California by American Telephone and Telegraph
39 1.1 rmind * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 1.1 rmind * the permission of UNIX System Laboratories, Inc.
41 1.1 rmind *
42 1.1 rmind * Redistribution and use in source and binary forms, with or without
43 1.1 rmind * modification, are permitted provided that the following conditions
44 1.1 rmind * are met:
45 1.1 rmind * 1. Redistributions of source code must retain the above copyright
46 1.1 rmind * notice, this list of conditions and the following disclaimer.
47 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 rmind * notice, this list of conditions and the following disclaimer in the
49 1.1 rmind * documentation and/or other materials provided with the distribution.
50 1.1 rmind * 3. Neither the name of the University nor the names of its contributors
51 1.1 rmind * may be used to endorse or promote products derived from this software
52 1.1 rmind * without specific prior written permission.
53 1.1 rmind *
54 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 rmind * SUCH DAMAGE.
65 1.1 rmind *
66 1.1 rmind * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
67 1.1 rmind */
68 1.1 rmind
69 1.1 rmind /*
70 1.8 rmind * The vnode cache subsystem.
71 1.1 rmind *
72 1.8 rmind * Life-cycle
73 1.1 rmind *
74 1.8 rmind * Normally, there are two points where new vnodes are created:
75 1.8 rmind * VOP_CREATE(9) and VOP_LOOKUP(9). The life-cycle of a vnode
76 1.8 rmind * starts in one of the following ways:
77 1.8 rmind *
78 1.45 hannken * - Allocation, via vcache_get(9) or vcache_new(9).
79 1.53.2.2 pgoyette * - Reclamation of inactive vnode, via vcache_vget(9).
80 1.8 rmind *
81 1.16 rmind * Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9)
82 1.16 rmind * was another, traditional way. Currently, only the draining thread
83 1.16 rmind * recycles the vnodes. This behaviour might be revisited.
84 1.16 rmind *
85 1.8 rmind * The life-cycle ends when the last reference is dropped, usually
86 1.8 rmind * in VOP_REMOVE(9). In such case, VOP_INACTIVE(9) is called to inform
87 1.8 rmind * the file system that vnode is inactive. Via this call, file system
88 1.16 rmind * indicates whether vnode can be recycled (usually, it checks its own
89 1.16 rmind * references, e.g. count of links, whether the file was removed).
90 1.8 rmind *
91 1.8 rmind * Depending on indication, vnode can be put into a free list (cache),
92 1.8 rmind * or cleaned via vclean(9), which calls VOP_RECLAIM(9) to disassociate
93 1.8 rmind * underlying file system from the vnode, and finally destroyed.
94 1.8 rmind *
95 1.52 hannken * Vnode state
96 1.52 hannken *
97 1.52 hannken * Vnode is always in one of six states:
98 1.52 hannken * - MARKER This is a marker vnode to help list traversal. It
99 1.52 hannken * will never change its state.
100 1.52 hannken * - LOADING Vnode is associating underlying file system and not
101 1.52 hannken * yet ready to use.
102 1.52 hannken * - ACTIVE Vnode has associated underlying file system and is
103 1.52 hannken * ready to use.
104 1.52 hannken * - BLOCKED Vnode is active but cannot get new references.
105 1.52 hannken * - RECLAIMING Vnode is disassociating from the underlying file
106 1.52 hannken * system.
107 1.52 hannken * - RECLAIMED Vnode has disassociated from underlying file system
108 1.52 hannken * and is dead.
109 1.52 hannken *
110 1.52 hannken * Valid state changes are:
111 1.52 hannken * LOADING -> ACTIVE
112 1.52 hannken * Vnode has been initialised in vcache_get() or
113 1.52 hannken * vcache_new() and is ready to use.
114 1.52 hannken * ACTIVE -> RECLAIMING
115 1.52 hannken * Vnode starts disassociation from underlying file
116 1.52 hannken * system in vclean().
117 1.52 hannken * RECLAIMING -> RECLAIMED
118 1.52 hannken * Vnode finished disassociation from underlying file
119 1.52 hannken * system in vclean().
120 1.52 hannken * ACTIVE -> BLOCKED
121 1.52 hannken * Either vcache_rekey*() is changing the vnode key or
122 1.52 hannken * vrelel() is about to call VOP_INACTIVE().
123 1.52 hannken * BLOCKED -> ACTIVE
124 1.52 hannken * The block condition is over.
125 1.52 hannken * LOADING -> RECLAIMED
126 1.52 hannken * Either vcache_get() or vcache_new() failed to
127 1.52 hannken * associate the underlying file system or vcache_rekey*()
128 1.52 hannken * drops a vnode used as placeholder.
129 1.52 hannken *
130 1.52 hannken * Of these states LOADING, BLOCKED and RECLAIMING are intermediate
131 1.52 hannken * and it is possible to wait for state change.
132 1.52 hannken *
133 1.52 hannken * State is protected with v_interlock with one exception:
134 1.53.2.2 pgoyette * to change from LOADING both v_interlock and vcache_lock must be held
135 1.52 hannken * so it is possible to check "state == LOADING" without holding
136 1.52 hannken * v_interlock. See vcache_get() for details.
137 1.52 hannken *
138 1.8 rmind * Reference counting
139 1.8 rmind *
140 1.8 rmind * Vnode is considered active, if reference count (vnode_t::v_usecount)
141 1.8 rmind * is non-zero. It is maintained using: vref(9) and vrele(9), as well
142 1.8 rmind * as vput(9), routines. Common points holding references are e.g.
143 1.8 rmind * file openings, current working directory, mount points, etc.
144 1.8 rmind *
145 1.8 rmind * Note on v_usecount and its locking
146 1.8 rmind *
147 1.8 rmind * At nearly all points it is known that v_usecount could be zero,
148 1.8 rmind * the vnode_t::v_interlock will be held. To change v_usecount away
149 1.8 rmind * from zero, the interlock must be held. To change from a non-zero
150 1.8 rmind * value to zero, again the interlock must be held.
151 1.8 rmind *
152 1.24 hannken * Changing the usecount from a non-zero value to a non-zero value can
153 1.24 hannken * safely be done using atomic operations, without the interlock held.
154 1.8 rmind *
155 1.1 rmind */
156 1.1 rmind
157 1.1 rmind #include <sys/cdefs.h>
158 1.53.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.53.2.4 2017/04/26 02:53:27 pgoyette Exp $");
159 1.1 rmind
160 1.1 rmind #include <sys/param.h>
161 1.1 rmind #include <sys/kernel.h>
162 1.1 rmind
163 1.1 rmind #include <sys/atomic.h>
164 1.1 rmind #include <sys/buf.h>
165 1.1 rmind #include <sys/conf.h>
166 1.1 rmind #include <sys/device.h>
167 1.36 hannken #include <sys/hash.h>
168 1.1 rmind #include <sys/kauth.h>
169 1.1 rmind #include <sys/kmem.h>
170 1.1 rmind #include <sys/kthread.h>
171 1.1 rmind #include <sys/module.h>
172 1.1 rmind #include <sys/mount.h>
173 1.1 rmind #include <sys/namei.h>
174 1.1 rmind #include <sys/syscallargs.h>
175 1.1 rmind #include <sys/sysctl.h>
176 1.1 rmind #include <sys/systm.h>
177 1.53.2.1 pgoyette #include <sys/vnode_impl.h>
178 1.1 rmind #include <sys/wapbl.h>
179 1.24 hannken #include <sys/fstrans.h>
180 1.1 rmind
181 1.1 rmind #include <uvm/uvm.h>
182 1.1 rmind #include <uvm/uvm_readahead.h>
183 1.1 rmind
184 1.23 hannken /* Flags to vrelel. */
185 1.23 hannken #define VRELEL_ASYNC_RELE 0x0001 /* Always defer to vrele thread. */
186 1.53.2.4 pgoyette #define VRELEL_FORCE_RELE 0x0002 /* Must always succeed. */
187 1.23 hannken
188 1.6 rmind u_int numvnodes __cacheline_aligned;
189 1.1 rmind
190 1.16 rmind /*
191 1.53.2.2 pgoyette * There are three lru lists: one holds vnodes waiting for async release,
192 1.53.2.2 pgoyette * one is for vnodes which have no buffer/page references and
193 1.53.2.2 pgoyette * one for those which do (i.e. v_holdcnt is non-zero).
194 1.53.2.2 pgoyette */
195 1.53.2.2 pgoyette static vnodelst_t lru_vrele_list __cacheline_aligned;
196 1.53.2.2 pgoyette static vnodelst_t lru_free_list __cacheline_aligned;
197 1.53.2.2 pgoyette static vnodelst_t lru_hold_list __cacheline_aligned;
198 1.53.2.2 pgoyette static kmutex_t vdrain_lock __cacheline_aligned;
199 1.16 rmind static kcondvar_t vdrain_cv __cacheline_aligned;
200 1.53.2.2 pgoyette static int vdrain_gen;
201 1.53.2.2 pgoyette static kcondvar_t vdrain_gen_cv;
202 1.53.2.2 pgoyette static bool vdrain_retry;
203 1.53.2.2 pgoyette static lwp_t * vdrain_lwp;
204 1.53.2.1 pgoyette SLIST_HEAD(hashhead, vnode_impl);
205 1.53.2.2 pgoyette static kmutex_t vcache_lock __cacheline_aligned;
206 1.53.2.2 pgoyette static kcondvar_t vcache_cv __cacheline_aligned;
207 1.53.2.2 pgoyette static u_int vcache_hashsize;
208 1.53.2.2 pgoyette static u_long vcache_hashmask;
209 1.53.2.2 pgoyette static struct hashhead *vcache_hashtab __cacheline_aligned;
210 1.53.2.2 pgoyette static pool_cache_t vcache_pool;
211 1.53.2.2 pgoyette static void lru_requeue(vnode_t *, vnodelst_t *);
212 1.53.2.2 pgoyette static vnodelst_t * lru_which(vnode_t *);
213 1.53.2.2 pgoyette static vnode_impl_t * vcache_alloc(void);
214 1.53.2.4 pgoyette static void vcache_dealloc(vnode_impl_t *);
215 1.53.2.1 pgoyette static void vcache_free(vnode_impl_t *);
216 1.36 hannken static void vcache_init(void);
217 1.36 hannken static void vcache_reinit(void);
218 1.25 hannken static void vclean(vnode_t *);
219 1.23 hannken static void vrelel(vnode_t *, int);
220 1.12 hannken static void vdrain_thread(void *);
221 1.11 christos static void vnpanic(vnode_t *, const char *, ...)
222 1.18 christos __printflike(2, 3);
223 1.1 rmind
224 1.1 rmind /* Routines having to do with the management of the vnode table. */
225 1.44 hannken extern struct mount *dead_rootmount;
226 1.1 rmind extern int (**dead_vnodeop_p)(void *);
227 1.31 hannken extern struct vfsops dead_vfsops;
228 1.1 rmind
229 1.51 hannken /* Vnode state operations and diagnostics. */
230 1.51 hannken
231 1.51 hannken #if defined(DIAGNOSTIC)
232 1.51 hannken
233 1.51 hannken #define VSTATE_GET(vp) \
234 1.51 hannken vstate_assert_get((vp), __func__, __LINE__)
235 1.51 hannken #define VSTATE_CHANGE(vp, from, to) \
236 1.51 hannken vstate_assert_change((vp), (from), (to), __func__, __LINE__)
237 1.51 hannken #define VSTATE_WAIT_STABLE(vp) \
238 1.51 hannken vstate_assert_wait_stable((vp), __func__, __LINE__)
239 1.51 hannken #define VSTATE_ASSERT(vp, state) \
240 1.51 hannken vstate_assert((vp), (state), __func__, __LINE__)
241 1.51 hannken
242 1.52 hannken static void
243 1.53.2.1 pgoyette vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line)
244 1.51 hannken {
245 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
246 1.51 hannken
247 1.51 hannken KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
248 1.51 hannken
249 1.53.2.2 pgoyette if (__predict_true(vip->vi_state == state))
250 1.51 hannken return;
251 1.51 hannken vnpanic(vp, "state is %s, expected %s at %s:%d",
252 1.53.2.2 pgoyette vstate_name(vip->vi_state), vstate_name(state), func, line);
253 1.51 hannken }
254 1.51 hannken
255 1.53.2.1 pgoyette static enum vnode_state
256 1.51 hannken vstate_assert_get(vnode_t *vp, const char *func, int line)
257 1.51 hannken {
258 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
259 1.51 hannken
260 1.51 hannken KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
261 1.53.2.2 pgoyette if (vip->vi_state == VS_MARKER)
262 1.51 hannken vnpanic(vp, "state is %s at %s:%d",
263 1.53.2.2 pgoyette vstate_name(vip->vi_state), func, line);
264 1.51 hannken
265 1.53.2.2 pgoyette return vip->vi_state;
266 1.51 hannken }
267 1.51 hannken
268 1.52 hannken static void
269 1.51 hannken vstate_assert_wait_stable(vnode_t *vp, const char *func, int line)
270 1.51 hannken {
271 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
272 1.51 hannken
273 1.51 hannken KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
274 1.53.2.2 pgoyette if (vip->vi_state == VS_MARKER)
275 1.51 hannken vnpanic(vp, "state is %s at %s:%d",
276 1.53.2.2 pgoyette vstate_name(vip->vi_state), func, line);
277 1.51 hannken
278 1.53.2.2 pgoyette while (vip->vi_state != VS_ACTIVE && vip->vi_state != VS_RECLAIMED)
279 1.51 hannken cv_wait(&vp->v_cv, vp->v_interlock);
280 1.51 hannken
281 1.53.2.2 pgoyette if (vip->vi_state == VS_MARKER)
282 1.51 hannken vnpanic(vp, "state is %s at %s:%d",
283 1.53.2.2 pgoyette vstate_name(vip->vi_state), func, line);
284 1.51 hannken }
285 1.51 hannken
286 1.52 hannken static void
287 1.53.2.1 pgoyette vstate_assert_change(vnode_t *vp, enum vnode_state from, enum vnode_state to,
288 1.51 hannken const char *func, int line)
289 1.51 hannken {
290 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
291 1.51 hannken
292 1.51 hannken KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
293 1.53.2.1 pgoyette if (from == VS_LOADING)
294 1.53.2.2 pgoyette KASSERTMSG(mutex_owned(&vcache_lock), "at %s:%d", func, line);
295 1.51 hannken
296 1.53.2.1 pgoyette if (from == VS_MARKER)
297 1.51 hannken vnpanic(vp, "from is %s at %s:%d",
298 1.51 hannken vstate_name(from), func, line);
299 1.53.2.1 pgoyette if (to == VS_MARKER)
300 1.51 hannken vnpanic(vp, "to is %s at %s:%d",
301 1.51 hannken vstate_name(to), func, line);
302 1.53.2.2 pgoyette if (vip->vi_state != from)
303 1.51 hannken vnpanic(vp, "from is %s, expected %s at %s:%d\n",
304 1.53.2.2 pgoyette vstate_name(vip->vi_state), vstate_name(from), func, line);
305 1.53.2.2 pgoyette if ((from == VS_BLOCKED || to == VS_BLOCKED) && vp->v_usecount != 1)
306 1.53.2.2 pgoyette vnpanic(vp, "%s to %s with usecount %d at %s:%d",
307 1.53.2.2 pgoyette vstate_name(from), vstate_name(to), vp->v_usecount,
308 1.53.2.2 pgoyette func, line);
309 1.51 hannken
310 1.53.2.2 pgoyette vip->vi_state = to;
311 1.53.2.1 pgoyette if (from == VS_LOADING)
312 1.53.2.2 pgoyette cv_broadcast(&vcache_cv);
313 1.53.2.1 pgoyette if (to == VS_ACTIVE || to == VS_RECLAIMED)
314 1.51 hannken cv_broadcast(&vp->v_cv);
315 1.51 hannken }
316 1.51 hannken
317 1.51 hannken #else /* defined(DIAGNOSTIC) */
318 1.51 hannken
319 1.51 hannken #define VSTATE_GET(vp) \
320 1.53.2.1 pgoyette (VNODE_TO_VIMPL((vp))->vi_state)
321 1.51 hannken #define VSTATE_CHANGE(vp, from, to) \
322 1.51 hannken vstate_change((vp), (from), (to))
323 1.51 hannken #define VSTATE_WAIT_STABLE(vp) \
324 1.51 hannken vstate_wait_stable((vp))
325 1.51 hannken #define VSTATE_ASSERT(vp, state)
326 1.51 hannken
327 1.52 hannken static void
328 1.51 hannken vstate_wait_stable(vnode_t *vp)
329 1.51 hannken {
330 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
331 1.51 hannken
332 1.53.2.2 pgoyette while (vip->vi_state != VS_ACTIVE && vip->vi_state != VS_RECLAIMED)
333 1.51 hannken cv_wait(&vp->v_cv, vp->v_interlock);
334 1.51 hannken }
335 1.51 hannken
336 1.52 hannken static void
337 1.53.2.1 pgoyette vstate_change(vnode_t *vp, enum vnode_state from, enum vnode_state to)
338 1.51 hannken {
339 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
340 1.51 hannken
341 1.53.2.2 pgoyette vip->vi_state = to;
342 1.53.2.1 pgoyette if (from == VS_LOADING)
343 1.53.2.2 pgoyette cv_broadcast(&vcache_cv);
344 1.53.2.1 pgoyette if (to == VS_ACTIVE || to == VS_RECLAIMED)
345 1.51 hannken cv_broadcast(&vp->v_cv);
346 1.51 hannken }
347 1.51 hannken
348 1.51 hannken #endif /* defined(DIAGNOSTIC) */
349 1.51 hannken
350 1.1 rmind void
351 1.1 rmind vfs_vnode_sysinit(void)
352 1.1 rmind {
353 1.22 martin int error __diagused;
354 1.1 rmind
355 1.44 hannken dead_rootmount = vfs_mountalloc(&dead_vfsops, NULL);
356 1.44 hannken KASSERT(dead_rootmount != NULL);
357 1.44 hannken dead_rootmount->mnt_iflag = IMNT_MPSAFE;
358 1.31 hannken
359 1.53.2.2 pgoyette mutex_init(&vdrain_lock, MUTEX_DEFAULT, IPL_NONE);
360 1.53.2.2 pgoyette TAILQ_INIT(&lru_free_list);
361 1.53.2.2 pgoyette TAILQ_INIT(&lru_hold_list);
362 1.53.2.2 pgoyette TAILQ_INIT(&lru_vrele_list);
363 1.1 rmind
364 1.36 hannken vcache_init();
365 1.36 hannken
366 1.12 hannken cv_init(&vdrain_cv, "vdrain");
367 1.53.2.2 pgoyette cv_init(&vdrain_gen_cv, "vdrainwt");
368 1.12 hannken error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread,
369 1.53.2.2 pgoyette NULL, &vdrain_lwp, "vdrain");
370 1.47 riastrad KASSERTMSG((error == 0), "kthread_create(vdrain) failed: %d", error);
371 1.1 rmind }
372 1.1 rmind
373 1.1 rmind /*
374 1.48 hannken * Allocate a new marker vnode.
375 1.48 hannken */
376 1.48 hannken vnode_t *
377 1.48 hannken vnalloc_marker(struct mount *mp)
378 1.48 hannken {
379 1.53.2.2 pgoyette vnode_impl_t *vip;
380 1.50 hannken vnode_t *vp;
381 1.50 hannken
382 1.53.2.2 pgoyette vip = pool_cache_get(vcache_pool, PR_WAITOK);
383 1.53.2.2 pgoyette memset(vip, 0, sizeof(*vip));
384 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(vip);
385 1.50 hannken uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
386 1.50 hannken vp->v_mount = mp;
387 1.50 hannken vp->v_type = VBAD;
388 1.53.2.2 pgoyette vip->vi_state = VS_MARKER;
389 1.48 hannken
390 1.50 hannken return vp;
391 1.48 hannken }
392 1.48 hannken
393 1.48 hannken /*
394 1.48 hannken * Free a marker vnode.
395 1.48 hannken */
396 1.48 hannken void
397 1.48 hannken vnfree_marker(vnode_t *vp)
398 1.48 hannken {
399 1.53.2.2 pgoyette vnode_impl_t *vip;
400 1.48 hannken
401 1.53.2.2 pgoyette vip = VNODE_TO_VIMPL(vp);
402 1.53.2.2 pgoyette KASSERT(vip->vi_state == VS_MARKER);
403 1.50 hannken uvm_obj_destroy(&vp->v_uobj, true);
404 1.53.2.2 pgoyette pool_cache_put(vcache_pool, vip);
405 1.48 hannken }
406 1.48 hannken
407 1.48 hannken /*
408 1.48 hannken * Test a vnode for being a marker vnode.
409 1.48 hannken */
410 1.48 hannken bool
411 1.48 hannken vnis_marker(vnode_t *vp)
412 1.48 hannken {
413 1.48 hannken
414 1.53.2.1 pgoyette return (VNODE_TO_VIMPL(vp)->vi_state == VS_MARKER);
415 1.48 hannken }
416 1.48 hannken
417 1.48 hannken /*
418 1.53.2.4 pgoyette * Set vnode to share another vnodes lock.
419 1.53.2.4 pgoyette */
420 1.53.2.4 pgoyette void
421 1.53.2.4 pgoyette vshare_lock(vnode_t *vp, vnode_t *src_vp)
422 1.53.2.4 pgoyette {
423 1.53.2.4 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
424 1.53.2.4 pgoyette vnode_impl_t *src_vip = VNODE_TO_VIMPL(src_vp);
425 1.53.2.4 pgoyette krwlock_t *oldlock = vip->vi_lock;
426 1.53.2.4 pgoyette
427 1.53.2.4 pgoyette rw_obj_hold(src_vip->vi_lock);
428 1.53.2.4 pgoyette vip->vi_lock = src_vip->vi_lock;
429 1.53.2.4 pgoyette rw_obj_free(oldlock);
430 1.53.2.4 pgoyette }
431 1.53.2.4 pgoyette
432 1.53.2.4 pgoyette /*
433 1.53.2.2 pgoyette * Return the lru list this node should be on.
434 1.1 rmind */
435 1.53.2.2 pgoyette static vnodelst_t *
436 1.53.2.2 pgoyette lru_which(vnode_t *vp)
437 1.1 rmind {
438 1.1 rmind
439 1.53.2.2 pgoyette KASSERT(mutex_owned(vp->v_interlock));
440 1.1 rmind
441 1.53.2.2 pgoyette if (vp->v_holdcnt > 0)
442 1.53.2.2 pgoyette return &lru_hold_list;
443 1.53.2.2 pgoyette else
444 1.53.2.2 pgoyette return &lru_free_list;
445 1.53.2.2 pgoyette }
446 1.1 rmind
447 1.53.2.2 pgoyette /*
448 1.53.2.2 pgoyette * Put vnode to end of given list.
449 1.53.2.2 pgoyette * Both the current and the new list may be NULL, used on vnode alloc/free.
450 1.53.2.2 pgoyette * Adjust numvnodes and signal vdrain thread if there is work.
451 1.53.2.2 pgoyette */
452 1.53.2.2 pgoyette static void
453 1.53.2.2 pgoyette lru_requeue(vnode_t *vp, vnodelst_t *listhd)
454 1.53.2.2 pgoyette {
455 1.53.2.2 pgoyette vnode_impl_t *vip;
456 1.12 hannken
457 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
458 1.53.2.2 pgoyette vip = VNODE_TO_VIMPL(vp);
459 1.53.2.2 pgoyette if (vip->vi_lrulisthd != NULL)
460 1.53.2.2 pgoyette TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
461 1.53.2.2 pgoyette else
462 1.53.2.2 pgoyette numvnodes++;
463 1.53.2.2 pgoyette vip->vi_lrulisthd = listhd;
464 1.53.2.2 pgoyette if (vip->vi_lrulisthd != NULL)
465 1.53.2.2 pgoyette TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
466 1.53.2.2 pgoyette else
467 1.53.2.2 pgoyette numvnodes--;
468 1.53.2.2 pgoyette if (numvnodes > desiredvnodes || listhd == &lru_vrele_list)
469 1.53.2.2 pgoyette cv_broadcast(&vdrain_cv);
470 1.53.2.2 pgoyette mutex_exit(&vdrain_lock);
471 1.1 rmind }
472 1.1 rmind
473 1.1 rmind /*
474 1.53.2.3 pgoyette * Release deferred vrele vnodes for this mount.
475 1.53.2.3 pgoyette * Called with file system suspended.
476 1.53.2.3 pgoyette */
477 1.53.2.3 pgoyette void
478 1.53.2.3 pgoyette vrele_flush(struct mount *mp)
479 1.53.2.3 pgoyette {
480 1.53.2.3 pgoyette vnode_impl_t *vip, *marker;
481 1.53.2.3 pgoyette
482 1.53.2.3 pgoyette KASSERT(fstrans_is_owner(mp));
483 1.53.2.3 pgoyette
484 1.53.2.3 pgoyette marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
485 1.53.2.3 pgoyette
486 1.53.2.3 pgoyette mutex_enter(&vdrain_lock);
487 1.53.2.3 pgoyette TAILQ_INSERT_HEAD(&lru_vrele_list, marker, vi_lrulist);
488 1.53.2.3 pgoyette
489 1.53.2.3 pgoyette while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
490 1.53.2.3 pgoyette TAILQ_REMOVE(&lru_vrele_list, marker, vi_lrulist);
491 1.53.2.3 pgoyette TAILQ_INSERT_AFTER(&lru_vrele_list, vip, marker, vi_lrulist);
492 1.53.2.3 pgoyette if (vnis_marker(VIMPL_TO_VNODE(vip)))
493 1.53.2.3 pgoyette continue;
494 1.53.2.3 pgoyette
495 1.53.2.3 pgoyette KASSERT(vip->vi_lrulisthd == &lru_vrele_list);
496 1.53.2.3 pgoyette TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
497 1.53.2.3 pgoyette vip->vi_lrulisthd = &lru_hold_list;
498 1.53.2.3 pgoyette TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
499 1.53.2.3 pgoyette mutex_exit(&vdrain_lock);
500 1.53.2.3 pgoyette
501 1.53.2.4 pgoyette mutex_enter(VIMPL_TO_VNODE(vip)->v_interlock);
502 1.53.2.4 pgoyette vrelel(VIMPL_TO_VNODE(vip), VRELEL_FORCE_RELE);
503 1.53.2.3 pgoyette
504 1.53.2.3 pgoyette mutex_enter(&vdrain_lock);
505 1.53.2.3 pgoyette }
506 1.53.2.3 pgoyette
507 1.53.2.3 pgoyette TAILQ_REMOVE(&lru_vrele_list, marker, vi_lrulist);
508 1.53.2.3 pgoyette mutex_exit(&vdrain_lock);
509 1.53.2.3 pgoyette
510 1.53.2.3 pgoyette vnfree_marker(VIMPL_TO_VNODE(marker));
511 1.53.2.3 pgoyette }
512 1.53.2.3 pgoyette
513 1.53.2.3 pgoyette /*
514 1.53.2.2 pgoyette * Reclaim a cached vnode. Used from vdrain_thread only.
515 1.12 hannken */
516 1.53.2.2 pgoyette static __inline void
517 1.53.2.2 pgoyette vdrain_remove(vnode_t *vp)
518 1.12 hannken {
519 1.53.2.2 pgoyette struct mount *mp;
520 1.12 hannken
521 1.53.2.2 pgoyette KASSERT(mutex_owned(&vdrain_lock));
522 1.12 hannken
523 1.53.2.2 pgoyette /* Probe usecount (unlocked). */
524 1.53.2.2 pgoyette if (vp->v_usecount > 0)
525 1.53.2.2 pgoyette return;
526 1.53.2.2 pgoyette /* Try v_interlock -- we lock the wrong direction! */
527 1.53.2.2 pgoyette if (!mutex_tryenter(vp->v_interlock))
528 1.53.2.2 pgoyette return;
529 1.53.2.2 pgoyette /* Probe usecount and state. */
530 1.53.2.2 pgoyette if (vp->v_usecount > 0 || VSTATE_GET(vp) != VS_ACTIVE) {
531 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
532 1.53.2.2 pgoyette return;
533 1.53.2.2 pgoyette }
534 1.53.2.2 pgoyette mp = vp->v_mount;
535 1.53.2.2 pgoyette if (fstrans_start_nowait(mp, FSTRANS_SHARED) != 0) {
536 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
537 1.53.2.2 pgoyette return;
538 1.53.2.2 pgoyette }
539 1.53.2.2 pgoyette vdrain_retry = true;
540 1.53.2.2 pgoyette mutex_exit(&vdrain_lock);
541 1.53.2.2 pgoyette
542 1.53.2.2 pgoyette if (vcache_vget(vp) == 0) {
543 1.53.2.4 pgoyette if (!vrecycle(vp)) {
544 1.53.2.4 pgoyette mutex_enter(vp->v_interlock);
545 1.53.2.4 pgoyette vrelel(vp, VRELEL_FORCE_RELE);
546 1.53.2.4 pgoyette }
547 1.12 hannken }
548 1.53.2.2 pgoyette fstrans_done(mp);
549 1.53.2.2 pgoyette
550 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
551 1.12 hannken }
552 1.12 hannken
553 1.12 hannken /*
554 1.53.2.2 pgoyette * Release a cached vnode. Used from vdrain_thread only.
555 1.1 rmind */
556 1.53.2.2 pgoyette static __inline void
557 1.53.2.2 pgoyette vdrain_vrele(vnode_t *vp)
558 1.1 rmind {
559 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
560 1.53.2.2 pgoyette struct mount *mp;
561 1.1 rmind
562 1.53.2.2 pgoyette KASSERT(mutex_owned(&vdrain_lock));
563 1.53.2.2 pgoyette
564 1.53.2.2 pgoyette mp = vp->v_mount;
565 1.53.2.4 pgoyette if (fstrans_start_nowait(mp, FSTRANS_SHARED) != 0)
566 1.53.2.2 pgoyette return;
567 1.1 rmind
568 1.1 rmind /*
569 1.53.2.2 pgoyette * First remove the vnode from the vrele list.
570 1.53.2.2 pgoyette * Put it on the last lru list, the last vrele()
571 1.53.2.2 pgoyette * will put it back onto the right list before
572 1.53.2.2 pgoyette * its v_usecount reaches zero.
573 1.1 rmind */
574 1.53.2.2 pgoyette KASSERT(vip->vi_lrulisthd == &lru_vrele_list);
575 1.53.2.2 pgoyette TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
576 1.53.2.2 pgoyette vip->vi_lrulisthd = &lru_hold_list;
577 1.53.2.2 pgoyette TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
578 1.53.2.2 pgoyette
579 1.53.2.2 pgoyette vdrain_retry = true;
580 1.53.2.2 pgoyette mutex_exit(&vdrain_lock);
581 1.53.2.2 pgoyette
582 1.53.2.2 pgoyette mutex_enter(vp->v_interlock);
583 1.53.2.4 pgoyette vrelel(vp, VRELEL_FORCE_RELE);
584 1.53.2.2 pgoyette fstrans_done(mp);
585 1.53.2.2 pgoyette
586 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
587 1.1 rmind }
588 1.1 rmind
589 1.1 rmind /*
590 1.53.2.2 pgoyette * Helper thread to keep the number of vnodes below desiredvnodes
591 1.53.2.2 pgoyette * and release vnodes from asynchronous vrele.
592 1.1 rmind */
593 1.53.2.2 pgoyette static void
594 1.53.2.2 pgoyette vdrain_thread(void *cookie)
595 1.1 rmind {
596 1.53.2.2 pgoyette vnodelst_t *listhd[] = {
597 1.53.2.2 pgoyette &lru_vrele_list, &lru_free_list, &lru_hold_list
598 1.53.2.2 pgoyette };
599 1.53.2.2 pgoyette int i;
600 1.53.2.2 pgoyette u_int target;
601 1.53.2.2 pgoyette vnode_impl_t *vip, *marker;
602 1.1 rmind
603 1.53.2.2 pgoyette marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
604 1.1 rmind
605 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
606 1.1 rmind
607 1.53.2.2 pgoyette for (;;) {
608 1.53.2.2 pgoyette vdrain_retry = false;
609 1.53.2.2 pgoyette target = desiredvnodes - desiredvnodes/10;
610 1.17 hannken
611 1.53.2.2 pgoyette for (i = 0; i < __arraycount(listhd); i++) {
612 1.53.2.2 pgoyette TAILQ_INSERT_HEAD(listhd[i], marker, vi_lrulist);
613 1.53.2.2 pgoyette while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
614 1.53.2.2 pgoyette TAILQ_REMOVE(listhd[i], marker, vi_lrulist);
615 1.53.2.2 pgoyette TAILQ_INSERT_AFTER(listhd[i], vip, marker,
616 1.53.2.2 pgoyette vi_lrulist);
617 1.53.2.3 pgoyette if (vnis_marker(VIMPL_TO_VNODE(vip)))
618 1.53.2.3 pgoyette continue;
619 1.53.2.2 pgoyette if (listhd[i] == &lru_vrele_list)
620 1.53.2.2 pgoyette vdrain_vrele(VIMPL_TO_VNODE(vip));
621 1.53.2.2 pgoyette else if (numvnodes < target)
622 1.53.2.2 pgoyette break;
623 1.53.2.2 pgoyette else
624 1.53.2.2 pgoyette vdrain_remove(VIMPL_TO_VNODE(vip));
625 1.53.2.2 pgoyette }
626 1.53.2.2 pgoyette TAILQ_REMOVE(listhd[i], marker, vi_lrulist);
627 1.53.2.2 pgoyette }
628 1.52 hannken
629 1.53.2.2 pgoyette if (vdrain_retry) {
630 1.53.2.2 pgoyette mutex_exit(&vdrain_lock);
631 1.53.2.2 pgoyette yield();
632 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
633 1.53.2.2 pgoyette } else {
634 1.53.2.2 pgoyette vdrain_gen++;
635 1.53.2.2 pgoyette cv_broadcast(&vdrain_gen_cv);
636 1.53.2.2 pgoyette cv_wait(&vdrain_cv, &vdrain_lock);
637 1.53.2.2 pgoyette }
638 1.53.2.2 pgoyette }
639 1.1 rmind }
640 1.1 rmind
641 1.1 rmind /*
642 1.4 rmind * vput: unlock and release the reference.
643 1.1 rmind */
644 1.1 rmind void
645 1.1 rmind vput(vnode_t *vp)
646 1.1 rmind {
647 1.1 rmind
648 1.1 rmind VOP_UNLOCK(vp);
649 1.1 rmind vrele(vp);
650 1.1 rmind }
651 1.1 rmind
652 1.1 rmind /*
653 1.1 rmind * Try to drop reference on a vnode. Abort if we are releasing the
654 1.1 rmind * last reference. Note: this _must_ succeed if not the last reference.
655 1.1 rmind */
656 1.1 rmind static inline bool
657 1.1 rmind vtryrele(vnode_t *vp)
658 1.1 rmind {
659 1.1 rmind u_int use, next;
660 1.1 rmind
661 1.1 rmind for (use = vp->v_usecount;; use = next) {
662 1.1 rmind if (use == 1) {
663 1.1 rmind return false;
664 1.1 rmind }
665 1.24 hannken KASSERT(use > 1);
666 1.1 rmind next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
667 1.1 rmind if (__predict_true(next == use)) {
668 1.1 rmind return true;
669 1.1 rmind }
670 1.1 rmind }
671 1.1 rmind }
672 1.1 rmind
673 1.1 rmind /*
674 1.1 rmind * Vnode release. If reference count drops to zero, call inactive
675 1.1 rmind * routine and either return to freelist or free to the pool.
676 1.1 rmind */
677 1.23 hannken static void
678 1.1 rmind vrelel(vnode_t *vp, int flags)
679 1.1 rmind {
680 1.53.2.4 pgoyette const bool async = ((flags & VRELEL_ASYNC_RELE) != 0);
681 1.53.2.4 pgoyette const bool force = ((flags & VRELEL_FORCE_RELE) != 0);
682 1.1 rmind bool recycle, defer;
683 1.1 rmind int error;
684 1.1 rmind
685 1.9 rmind KASSERT(mutex_owned(vp->v_interlock));
686 1.1 rmind
687 1.1 rmind if (__predict_false(vp->v_op == dead_vnodeop_p &&
688 1.53.2.1 pgoyette VSTATE_GET(vp) != VS_RECLAIMED)) {
689 1.11 christos vnpanic(vp, "dead but not clean");
690 1.1 rmind }
691 1.1 rmind
692 1.1 rmind /*
693 1.1 rmind * If not the last reference, just drop the reference count
694 1.1 rmind * and unlock.
695 1.1 rmind */
696 1.1 rmind if (vtryrele(vp)) {
697 1.9 rmind mutex_exit(vp->v_interlock);
698 1.1 rmind return;
699 1.1 rmind }
700 1.1 rmind if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
701 1.11 christos vnpanic(vp, "%s: bad ref count", __func__);
702 1.1 rmind }
703 1.1 rmind
704 1.15 hannken #ifdef DIAGNOSTIC
705 1.15 hannken if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
706 1.15 hannken vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
707 1.15 hannken vprint("vrelel: missing VOP_CLOSE()", vp);
708 1.15 hannken }
709 1.15 hannken #endif
710 1.15 hannken
711 1.1 rmind /*
712 1.53.2.4 pgoyette * First try to get the vnode locked for VOP_INACTIVE().
713 1.53.2.4 pgoyette * Defer vnode release to vdrain_thread if caller requests
714 1.53.2.4 pgoyette * it explicitly, is the pagedaemon or the lock failed.
715 1.1 rmind */
716 1.53.2.4 pgoyette if ((curlwp == uvm.pagedaemon_lwp) || async) {
717 1.53.2.4 pgoyette defer = true;
718 1.53.2.4 pgoyette } else {
719 1.53.2.4 pgoyette mutex_exit(vp->v_interlock);
720 1.53.2.4 pgoyette error = vn_lock(vp,
721 1.53.2.4 pgoyette LK_EXCLUSIVE | LK_RETRY | (force ? 0 : LK_NOWAIT));
722 1.53.2.4 pgoyette defer = (error != 0);
723 1.53.2.4 pgoyette mutex_enter(vp->v_interlock);
724 1.53.2.4 pgoyette }
725 1.53.2.4 pgoyette KASSERT(mutex_owned(vp->v_interlock));
726 1.53.2.4 pgoyette KASSERT(! (force && defer));
727 1.53.2.4 pgoyette if (defer) {
728 1.1 rmind /*
729 1.53.2.4 pgoyette * Defer reclaim to the kthread; it's not safe to
730 1.53.2.4 pgoyette * clean it here. We donate it our last reference.
731 1.1 rmind */
732 1.53.2.4 pgoyette lru_requeue(vp, &lru_vrele_list);
733 1.53.2.4 pgoyette mutex_exit(vp->v_interlock);
734 1.53.2.4 pgoyette return;
735 1.53.2.4 pgoyette }
736 1.30 hannken
737 1.53.2.4 pgoyette /*
738 1.53.2.4 pgoyette * If the node got another reference while we
739 1.53.2.4 pgoyette * released the interlock, don't try to inactivate it yet.
740 1.53.2.4 pgoyette */
741 1.53.2.4 pgoyette if (__predict_false(vtryrele(vp))) {
742 1.53.2.4 pgoyette VOP_UNLOCK(vp);
743 1.53.2.4 pgoyette mutex_exit(vp->v_interlock);
744 1.53.2.4 pgoyette return;
745 1.53.2.4 pgoyette }
746 1.1 rmind
747 1.53.2.4 pgoyette /*
748 1.53.2.4 pgoyette * If not clean, deactivate the vnode, but preserve
749 1.53.2.4 pgoyette * our reference across the call to VOP_INACTIVE().
750 1.53.2.4 pgoyette */
751 1.53.2.4 pgoyette if (VSTATE_GET(vp) == VS_RECLAIMED) {
752 1.53.2.4 pgoyette VOP_UNLOCK(vp);
753 1.53.2.4 pgoyette } else {
754 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_ACTIVE, VS_BLOCKED);
755 1.29 christos mutex_exit(vp->v_interlock);
756 1.29 christos
757 1.1 rmind /*
758 1.52 hannken * The vnode must not gain another reference while being
759 1.1 rmind * deactivated. If VOP_INACTIVE() indicates that
760 1.1 rmind * the described file has been deleted, then recycle
761 1.52 hannken * the vnode.
762 1.1 rmind *
763 1.53.2.4 pgoyette * Note that VOP_INACTIVE() will not drop the vnode lock.
764 1.1 rmind */
765 1.53.2.4 pgoyette recycle = false;
766 1.1 rmind VOP_INACTIVE(vp, &recycle);
767 1.53.2.4 pgoyette if (!recycle)
768 1.53.2.4 pgoyette VOP_UNLOCK(vp);
769 1.9 rmind mutex_enter(vp->v_interlock);
770 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_BLOCKED, VS_ACTIVE);
771 1.1 rmind if (!recycle) {
772 1.1 rmind if (vtryrele(vp)) {
773 1.9 rmind mutex_exit(vp->v_interlock);
774 1.1 rmind return;
775 1.1 rmind }
776 1.1 rmind }
777 1.1 rmind
778 1.1 rmind /* Take care of space accounting. */
779 1.1 rmind if (vp->v_iflag & VI_EXECMAP) {
780 1.1 rmind atomic_add_int(&uvmexp.execpages,
781 1.1 rmind -vp->v_uobj.uo_npages);
782 1.1 rmind atomic_add_int(&uvmexp.filepages,
783 1.1 rmind vp->v_uobj.uo_npages);
784 1.1 rmind }
785 1.1 rmind vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
786 1.1 rmind vp->v_vflag &= ~VV_MAPPED;
787 1.1 rmind
788 1.1 rmind /*
789 1.1 rmind * Recycle the vnode if the file is now unused (unlinked),
790 1.1 rmind * otherwise just free it.
791 1.1 rmind */
792 1.1 rmind if (recycle) {
793 1.53.2.1 pgoyette VSTATE_ASSERT(vp, VS_ACTIVE);
794 1.53.2.4 pgoyette /* vcache_reclaim drops the lock. */
795 1.53.2.1 pgoyette vcache_reclaim(vp);
796 1.1 rmind }
797 1.1 rmind KASSERT(vp->v_usecount > 0);
798 1.1 rmind }
799 1.1 rmind
800 1.1 rmind if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
801 1.1 rmind /* Gained another reference while being reclaimed. */
802 1.9 rmind mutex_exit(vp->v_interlock);
803 1.1 rmind return;
804 1.1 rmind }
805 1.1 rmind
806 1.53.2.2 pgoyette if (VSTATE_GET(vp) == VS_RECLAIMED && vp->v_holdcnt == 0) {
807 1.1 rmind /*
808 1.1 rmind * It's clean so destroy it. It isn't referenced
809 1.1 rmind * anywhere since it has been reclaimed.
810 1.1 rmind */
811 1.53.2.1 pgoyette vcache_free(VNODE_TO_VIMPL(vp));
812 1.1 rmind } else {
813 1.1 rmind /*
814 1.1 rmind * Otherwise, put it back onto the freelist. It
815 1.1 rmind * can't be destroyed while still associated with
816 1.1 rmind * a file system.
817 1.1 rmind */
818 1.53.2.2 pgoyette lru_requeue(vp, lru_which(vp));
819 1.9 rmind mutex_exit(vp->v_interlock);
820 1.1 rmind }
821 1.1 rmind }
822 1.1 rmind
823 1.1 rmind void
824 1.1 rmind vrele(vnode_t *vp)
825 1.1 rmind {
826 1.1 rmind
827 1.29 christos if (vtryrele(vp)) {
828 1.1 rmind return;
829 1.1 rmind }
830 1.9 rmind mutex_enter(vp->v_interlock);
831 1.1 rmind vrelel(vp, 0);
832 1.1 rmind }
833 1.1 rmind
834 1.1 rmind /*
835 1.1 rmind * Asynchronous vnode release, vnode is released in different context.
836 1.1 rmind */
837 1.1 rmind void
838 1.1 rmind vrele_async(vnode_t *vp)
839 1.1 rmind {
840 1.1 rmind
841 1.29 christos if (vtryrele(vp)) {
842 1.1 rmind return;
843 1.1 rmind }
844 1.9 rmind mutex_enter(vp->v_interlock);
845 1.1 rmind vrelel(vp, VRELEL_ASYNC_RELE);
846 1.1 rmind }
847 1.1 rmind
848 1.1 rmind /*
849 1.1 rmind * Vnode reference, where a reference is already held by some other
850 1.1 rmind * object (for example, a file structure).
851 1.1 rmind */
852 1.1 rmind void
853 1.1 rmind vref(vnode_t *vp)
854 1.1 rmind {
855 1.1 rmind
856 1.1 rmind KASSERT(vp->v_usecount != 0);
857 1.1 rmind
858 1.1 rmind atomic_inc_uint(&vp->v_usecount);
859 1.1 rmind }
860 1.1 rmind
861 1.1 rmind /*
862 1.1 rmind * Page or buffer structure gets a reference.
863 1.1 rmind * Called with v_interlock held.
864 1.1 rmind */
865 1.1 rmind void
866 1.1 rmind vholdl(vnode_t *vp)
867 1.1 rmind {
868 1.1 rmind
869 1.9 rmind KASSERT(mutex_owned(vp->v_interlock));
870 1.1 rmind
871 1.53.2.2 pgoyette if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0)
872 1.53.2.2 pgoyette lru_requeue(vp, lru_which(vp));
873 1.1 rmind }
874 1.1 rmind
875 1.1 rmind /*
876 1.1 rmind * Page or buffer structure frees a reference.
877 1.1 rmind * Called with v_interlock held.
878 1.1 rmind */
879 1.1 rmind void
880 1.1 rmind holdrelel(vnode_t *vp)
881 1.1 rmind {
882 1.1 rmind
883 1.9 rmind KASSERT(mutex_owned(vp->v_interlock));
884 1.1 rmind
885 1.1 rmind if (vp->v_holdcnt <= 0) {
886 1.11 christos vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
887 1.1 rmind }
888 1.1 rmind
889 1.1 rmind vp->v_holdcnt--;
890 1.53.2.2 pgoyette if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
891 1.53.2.2 pgoyette lru_requeue(vp, lru_which(vp));
892 1.1 rmind }
893 1.1 rmind
894 1.1 rmind /*
895 1.1 rmind * Disassociate the underlying file system from a vnode.
896 1.1 rmind *
897 1.46 hannken * Must be called with vnode locked and will return unlocked.
898 1.1 rmind * Must be called with the interlock held, and will return with it held.
899 1.1 rmind */
900 1.25 hannken static void
901 1.25 hannken vclean(vnode_t *vp)
902 1.1 rmind {
903 1.1 rmind lwp_t *l = curlwp;
904 1.43 hannken bool recycle, active;
905 1.1 rmind int error;
906 1.1 rmind
907 1.46 hannken KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
908 1.46 hannken VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
909 1.9 rmind KASSERT(mutex_owned(vp->v_interlock));
910 1.1 rmind KASSERT(vp->v_usecount != 0);
911 1.1 rmind
912 1.32 hannken active = (vp->v_usecount > 1);
913 1.1 rmind /*
914 1.1 rmind * Prevent the vnode from being recycled or brought into use
915 1.1 rmind * while we clean it out.
916 1.1 rmind */
917 1.52 hannken VSTATE_CHANGE(vp, VN_ACTIVE, VN_RECLAIMING);
918 1.1 rmind if (vp->v_iflag & VI_EXECMAP) {
919 1.1 rmind atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
920 1.1 rmind atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
921 1.1 rmind }
922 1.1 rmind vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
923 1.9 rmind mutex_exit(vp->v_interlock);
924 1.23 hannken
925 1.1 rmind /*
926 1.1 rmind * Clean out any cached data associated with the vnode.
927 1.1 rmind * If purging an active vnode, it must be closed and
928 1.1 rmind * deactivated before being reclaimed. Note that the
929 1.1 rmind * VOP_INACTIVE will unlock the vnode.
930 1.1 rmind */
931 1.43 hannken error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
932 1.43 hannken if (error != 0) {
933 1.43 hannken if (wapbl_vphaswapbl(vp))
934 1.43 hannken WAPBL_DISCARD(wapbl_vptomp(vp));
935 1.43 hannken error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
936 1.43 hannken }
937 1.47 riastrad KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
938 1.43 hannken KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
939 1.43 hannken if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
940 1.43 hannken spec_node_revoke(vp);
941 1.1 rmind }
942 1.1 rmind if (active) {
943 1.1 rmind VOP_INACTIVE(vp, &recycle);
944 1.1 rmind } else {
945 1.1 rmind /*
946 1.1 rmind * Any other processes trying to obtain this lock must first
947 1.52 hannken * wait for VN_RECLAIMED, then call the new lock operation.
948 1.1 rmind */
949 1.1 rmind VOP_UNLOCK(vp);
950 1.1 rmind }
951 1.1 rmind
952 1.1 rmind /* Disassociate the underlying file system from the vnode. */
953 1.1 rmind if (VOP_RECLAIM(vp)) {
954 1.11 christos vnpanic(vp, "%s: cannot reclaim", __func__);
955 1.1 rmind }
956 1.1 rmind
957 1.7 rmind KASSERT(vp->v_data == NULL);
958 1.1 rmind KASSERT(vp->v_uobj.uo_npages == 0);
959 1.7 rmind
960 1.1 rmind if (vp->v_type == VREG && vp->v_ractx != NULL) {
961 1.1 rmind uvm_ra_freectx(vp->v_ractx);
962 1.1 rmind vp->v_ractx = NULL;
963 1.1 rmind }
964 1.7 rmind
965 1.7 rmind /* Purge name cache. */
966 1.1 rmind cache_purge(vp);
967 1.1 rmind
968 1.31 hannken /* Move to dead mount. */
969 1.31 hannken vp->v_vflag &= ~VV_ROOT;
970 1.44 hannken atomic_inc_uint(&dead_rootmount->mnt_refcnt);
971 1.44 hannken vfs_insmntque(vp, dead_rootmount);
972 1.23 hannken
973 1.1 rmind /* Done with purge, notify sleepers of the grim news. */
974 1.9 rmind mutex_enter(vp->v_interlock);
975 1.43 hannken vp->v_op = dead_vnodeop_p;
976 1.43 hannken vp->v_vflag |= VV_LOCKSWORK;
977 1.52 hannken VSTATE_CHANGE(vp, VN_RECLAIMING, VN_RECLAIMED);
978 1.1 rmind vp->v_tag = VT_NON;
979 1.1 rmind KNOTE(&vp->v_klist, NOTE_REVOKE);
980 1.1 rmind
981 1.1 rmind KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
982 1.1 rmind }
983 1.1 rmind
984 1.1 rmind /*
985 1.33 hannken * Recycle an unused vnode if caller holds the last reference.
986 1.1 rmind */
987 1.33 hannken bool
988 1.33 hannken vrecycle(vnode_t *vp)
989 1.1 rmind {
990 1.53.2.2 pgoyette int error __diagused;
991 1.46 hannken
992 1.33 hannken mutex_enter(vp->v_interlock);
993 1.33 hannken
994 1.53.2.2 pgoyette /* Make sure we hold the last reference. */
995 1.53.2.2 pgoyette VSTATE_WAIT_STABLE(vp);
996 1.33 hannken if (vp->v_usecount != 1) {
997 1.33 hannken mutex_exit(vp->v_interlock);
998 1.33 hannken return false;
999 1.1 rmind }
1000 1.53.2.2 pgoyette
1001 1.53.2.2 pgoyette /* If the vnode is already clean we're done. */
1002 1.53.2.2 pgoyette if (VSTATE_GET(vp) != VS_ACTIVE) {
1003 1.53.2.2 pgoyette VSTATE_ASSERT(vp, VS_RECLAIMED);
1004 1.53.2.2 pgoyette vrelel(vp, 0);
1005 1.53.2.2 pgoyette return true;
1006 1.53.2.2 pgoyette }
1007 1.53.2.2 pgoyette
1008 1.53.2.2 pgoyette /* Prevent further references until the vnode is locked. */
1009 1.53.2.2 pgoyette VSTATE_CHANGE(vp, VS_ACTIVE, VS_BLOCKED);
1010 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
1011 1.53.2.2 pgoyette
1012 1.53.2.3 pgoyette /*
1013 1.53.2.3 pgoyette * On a leaf file system this lock will always succeed as we hold
1014 1.53.2.3 pgoyette * the last reference and prevent further references.
1015 1.53.2.3 pgoyette * On layered file systems waiting for the lock would open a can of
1016 1.53.2.3 pgoyette * deadlocks as the lower vnodes may have other active references.
1017 1.53.2.3 pgoyette */
1018 1.53.2.3 pgoyette error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT);
1019 1.53.2.2 pgoyette
1020 1.53.2.2 pgoyette mutex_enter(vp->v_interlock);
1021 1.53.2.2 pgoyette VSTATE_CHANGE(vp, VS_BLOCKED, VS_ACTIVE);
1022 1.53.2.2 pgoyette
1023 1.53.2.3 pgoyette if (error) {
1024 1.53.2.3 pgoyette mutex_exit(vp->v_interlock);
1025 1.53.2.3 pgoyette return false;
1026 1.53.2.3 pgoyette }
1027 1.53.2.3 pgoyette
1028 1.53.2.2 pgoyette KASSERT(vp->v_usecount == 1);
1029 1.53.2.2 pgoyette vcache_reclaim(vp);
1030 1.52 hannken vrelel(vp, 0);
1031 1.53.2.2 pgoyette
1032 1.33 hannken return true;
1033 1.1 rmind }
1034 1.1 rmind
1035 1.1 rmind /*
1036 1.1 rmind * Eliminate all activity associated with the requested vnode
1037 1.1 rmind * and with all vnodes aliased to the requested vnode.
1038 1.1 rmind */
1039 1.1 rmind void
1040 1.1 rmind vrevoke(vnode_t *vp)
1041 1.1 rmind {
1042 1.19 hannken vnode_t *vq;
1043 1.1 rmind enum vtype type;
1044 1.1 rmind dev_t dev;
1045 1.1 rmind
1046 1.1 rmind KASSERT(vp->v_usecount > 0);
1047 1.1 rmind
1048 1.9 rmind mutex_enter(vp->v_interlock);
1049 1.52 hannken VSTATE_WAIT_STABLE(vp);
1050 1.53.2.1 pgoyette if (VSTATE_GET(vp) == VS_RECLAIMED) {
1051 1.9 rmind mutex_exit(vp->v_interlock);
1052 1.1 rmind return;
1053 1.1 rmind } else if (vp->v_type != VBLK && vp->v_type != VCHR) {
1054 1.1 rmind atomic_inc_uint(&vp->v_usecount);
1055 1.29 christos mutex_exit(vp->v_interlock);
1056 1.29 christos vgone(vp);
1057 1.1 rmind return;
1058 1.1 rmind } else {
1059 1.1 rmind dev = vp->v_rdev;
1060 1.1 rmind type = vp->v_type;
1061 1.9 rmind mutex_exit(vp->v_interlock);
1062 1.1 rmind }
1063 1.1 rmind
1064 1.19 hannken while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
1065 1.29 christos vgone(vq);
1066 1.1 rmind }
1067 1.1 rmind }
1068 1.1 rmind
1069 1.1 rmind /*
1070 1.1 rmind * Eliminate all activity associated with a vnode in preparation for
1071 1.1 rmind * reuse. Drops a reference from the vnode.
1072 1.1 rmind */
1073 1.1 rmind void
1074 1.1 rmind vgone(vnode_t *vp)
1075 1.1 rmind {
1076 1.1 rmind
1077 1.53.2.3 pgoyette vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1078 1.9 rmind mutex_enter(vp->v_interlock);
1079 1.53.2.3 pgoyette VSTATE_WAIT_STABLE(vp);
1080 1.53.2.3 pgoyette if (VSTATE_GET(vp) == VS_ACTIVE)
1081 1.53.2.3 pgoyette vcache_reclaim(vp);
1082 1.53.2.3 pgoyette VSTATE_ASSERT(vp, VS_RECLAIMED);
1083 1.52 hannken vrelel(vp, 0);
1084 1.1 rmind }
1085 1.1 rmind
1086 1.36 hannken static inline uint32_t
1087 1.36 hannken vcache_hash(const struct vcache_key *key)
1088 1.36 hannken {
1089 1.36 hannken uint32_t hash = HASH32_BUF_INIT;
1090 1.36 hannken
1091 1.36 hannken hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash);
1092 1.36 hannken hash = hash32_buf(key->vk_key, key->vk_key_len, hash);
1093 1.36 hannken return hash;
1094 1.36 hannken }
1095 1.36 hannken
1096 1.36 hannken static void
1097 1.36 hannken vcache_init(void)
1098 1.36 hannken {
1099 1.36 hannken
1100 1.53.2.2 pgoyette vcache_pool = pool_cache_init(sizeof(vnode_impl_t), 0, 0, 0,
1101 1.36 hannken "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL);
1102 1.53.2.2 pgoyette KASSERT(vcache_pool != NULL);
1103 1.53.2.2 pgoyette mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE);
1104 1.53.2.2 pgoyette cv_init(&vcache_cv, "vcache");
1105 1.53.2.2 pgoyette vcache_hashsize = desiredvnodes;
1106 1.53.2.2 pgoyette vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
1107 1.53.2.2 pgoyette &vcache_hashmask);
1108 1.36 hannken }
1109 1.36 hannken
1110 1.36 hannken static void
1111 1.36 hannken vcache_reinit(void)
1112 1.36 hannken {
1113 1.36 hannken int i;
1114 1.36 hannken uint32_t hash;
1115 1.36 hannken u_long oldmask, newmask;
1116 1.36 hannken struct hashhead *oldtab, *newtab;
1117 1.53.2.2 pgoyette vnode_impl_t *vip;
1118 1.36 hannken
1119 1.36 hannken newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask);
1120 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1121 1.53.2.2 pgoyette oldtab = vcache_hashtab;
1122 1.53.2.2 pgoyette oldmask = vcache_hashmask;
1123 1.53.2.2 pgoyette vcache_hashsize = desiredvnodes;
1124 1.53.2.2 pgoyette vcache_hashtab = newtab;
1125 1.53.2.2 pgoyette vcache_hashmask = newmask;
1126 1.36 hannken for (i = 0; i <= oldmask; i++) {
1127 1.53.2.2 pgoyette while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) {
1128 1.53.2.2 pgoyette SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash);
1129 1.53.2.2 pgoyette hash = vcache_hash(&vip->vi_key);
1130 1.53.2.2 pgoyette SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask],
1131 1.53.2.2 pgoyette vip, vi_hash);
1132 1.36 hannken }
1133 1.36 hannken }
1134 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1135 1.36 hannken hashdone(oldtab, HASH_SLIST, oldmask);
1136 1.36 hannken }
1137 1.36 hannken
1138 1.53.2.1 pgoyette static inline vnode_impl_t *
1139 1.36 hannken vcache_hash_lookup(const struct vcache_key *key, uint32_t hash)
1140 1.36 hannken {
1141 1.36 hannken struct hashhead *hashp;
1142 1.53.2.2 pgoyette vnode_impl_t *vip;
1143 1.36 hannken
1144 1.53.2.2 pgoyette KASSERT(mutex_owned(&vcache_lock));
1145 1.36 hannken
1146 1.53.2.2 pgoyette hashp = &vcache_hashtab[hash & vcache_hashmask];
1147 1.53.2.2 pgoyette SLIST_FOREACH(vip, hashp, vi_hash) {
1148 1.53.2.2 pgoyette if (key->vk_mount != vip->vi_key.vk_mount)
1149 1.36 hannken continue;
1150 1.53.2.2 pgoyette if (key->vk_key_len != vip->vi_key.vk_key_len)
1151 1.36 hannken continue;
1152 1.53.2.2 pgoyette if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len))
1153 1.36 hannken continue;
1154 1.53.2.2 pgoyette return vip;
1155 1.36 hannken }
1156 1.36 hannken return NULL;
1157 1.36 hannken }
1158 1.36 hannken
1159 1.36 hannken /*
1160 1.50 hannken * Allocate a new, uninitialized vcache node.
1161 1.50 hannken */
1162 1.53.2.1 pgoyette static vnode_impl_t *
1163 1.50 hannken vcache_alloc(void)
1164 1.50 hannken {
1165 1.53.2.2 pgoyette vnode_impl_t *vip;
1166 1.50 hannken vnode_t *vp;
1167 1.50 hannken
1168 1.53.2.2 pgoyette vip = pool_cache_get(vcache_pool, PR_WAITOK);
1169 1.53.2.2 pgoyette memset(vip, 0, sizeof(*vip));
1170 1.50 hannken
1171 1.53.2.4 pgoyette vip->vi_lock = rw_obj_alloc();
1172 1.53.2.2 pgoyette /* SLIST_INIT(&vip->vi_hash); */
1173 1.53.2.3 pgoyette /* LIST_INIT(&vip->vi_nclist); */
1174 1.53.2.3 pgoyette /* LIST_INIT(&vip->vi_dnclist); */
1175 1.50 hannken
1176 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(vip);
1177 1.50 hannken uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
1178 1.50 hannken cv_init(&vp->v_cv, "vnode");
1179 1.50 hannken
1180 1.50 hannken vp->v_usecount = 1;
1181 1.50 hannken vp->v_type = VNON;
1182 1.50 hannken vp->v_size = vp->v_writesize = VSIZENOTSET;
1183 1.50 hannken
1184 1.53.2.2 pgoyette vip->vi_state = VS_LOADING;
1185 1.53.2.2 pgoyette
1186 1.53.2.2 pgoyette lru_requeue(vp, &lru_free_list);
1187 1.51 hannken
1188 1.53.2.2 pgoyette return vip;
1189 1.50 hannken }
1190 1.50 hannken
1191 1.50 hannken /*
1192 1.53.2.4 pgoyette * Deallocate a vcache node in state VS_LOADING.
1193 1.53.2.4 pgoyette *
1194 1.53.2.4 pgoyette * vcache_lock held on entry and released on return.
1195 1.53.2.4 pgoyette */
1196 1.53.2.4 pgoyette static void
1197 1.53.2.4 pgoyette vcache_dealloc(vnode_impl_t *vip)
1198 1.53.2.4 pgoyette {
1199 1.53.2.4 pgoyette vnode_t *vp;
1200 1.53.2.4 pgoyette
1201 1.53.2.4 pgoyette KASSERT(mutex_owned(&vcache_lock));
1202 1.53.2.4 pgoyette
1203 1.53.2.4 pgoyette vp = VIMPL_TO_VNODE(vip);
1204 1.53.2.4 pgoyette mutex_enter(vp->v_interlock);
1205 1.53.2.4 pgoyette vp->v_op = dead_vnodeop_p;
1206 1.53.2.4 pgoyette VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED);
1207 1.53.2.4 pgoyette mutex_exit(&vcache_lock);
1208 1.53.2.4 pgoyette vrelel(vp, 0);
1209 1.53.2.4 pgoyette }
1210 1.53.2.4 pgoyette
1211 1.53.2.4 pgoyette /*
1212 1.50 hannken * Free an unused, unreferenced vcache node.
1213 1.53.2.2 pgoyette * v_interlock locked on entry.
1214 1.50 hannken */
1215 1.50 hannken static void
1216 1.53.2.2 pgoyette vcache_free(vnode_impl_t *vip)
1217 1.50 hannken {
1218 1.50 hannken vnode_t *vp;
1219 1.50 hannken
1220 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(vip);
1221 1.53.2.2 pgoyette KASSERT(mutex_owned(vp->v_interlock));
1222 1.50 hannken
1223 1.50 hannken KASSERT(vp->v_usecount == 0);
1224 1.53.2.2 pgoyette KASSERT(vp->v_holdcnt == 0);
1225 1.53.2.2 pgoyette KASSERT(vp->v_writecount == 0);
1226 1.53.2.2 pgoyette lru_requeue(vp, NULL);
1227 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
1228 1.50 hannken
1229 1.53.2.2 pgoyette vfs_insmntque(vp, NULL);
1230 1.53.2.2 pgoyette if (vp->v_type == VBLK || vp->v_type == VCHR)
1231 1.53.2.2 pgoyette spec_node_destroy(vp);
1232 1.50 hannken
1233 1.53.2.4 pgoyette rw_obj_free(vip->vi_lock);
1234 1.50 hannken uvm_obj_destroy(&vp->v_uobj, true);
1235 1.50 hannken cv_destroy(&vp->v_cv);
1236 1.53.2.2 pgoyette pool_cache_put(vcache_pool, vip);
1237 1.53.2.2 pgoyette }
1238 1.53.2.2 pgoyette
1239 1.53.2.2 pgoyette /*
1240 1.53.2.2 pgoyette * Try to get an initial reference on this cached vnode.
1241 1.53.2.2 pgoyette * Returns zero on success, ENOENT if the vnode has been reclaimed and
1242 1.53.2.2 pgoyette * EBUSY if the vnode state is unstable.
1243 1.53.2.2 pgoyette *
1244 1.53.2.2 pgoyette * v_interlock locked on entry and unlocked on exit.
1245 1.53.2.2 pgoyette */
1246 1.53.2.2 pgoyette int
1247 1.53.2.2 pgoyette vcache_tryvget(vnode_t *vp)
1248 1.53.2.2 pgoyette {
1249 1.53.2.2 pgoyette int error = 0;
1250 1.53.2.2 pgoyette
1251 1.53.2.2 pgoyette KASSERT(mutex_owned(vp->v_interlock));
1252 1.53.2.2 pgoyette
1253 1.53.2.2 pgoyette if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED))
1254 1.53.2.2 pgoyette error = ENOENT;
1255 1.53.2.2 pgoyette else if (__predict_false(VSTATE_GET(vp) != VS_ACTIVE))
1256 1.53.2.2 pgoyette error = EBUSY;
1257 1.53.2.2 pgoyette else if (vp->v_usecount == 0)
1258 1.53.2.2 pgoyette vp->v_usecount = 1;
1259 1.53.2.2 pgoyette else
1260 1.53.2.2 pgoyette atomic_inc_uint(&vp->v_usecount);
1261 1.53.2.2 pgoyette
1262 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
1263 1.53.2.2 pgoyette
1264 1.53.2.2 pgoyette return error;
1265 1.53.2.2 pgoyette }
1266 1.53.2.2 pgoyette
1267 1.53.2.2 pgoyette /*
1268 1.53.2.2 pgoyette * Try to get an initial reference on this cached vnode.
1269 1.53.2.2 pgoyette * Returns zero on success and ENOENT if the vnode has been reclaimed.
1270 1.53.2.2 pgoyette * Will wait for the vnode state to be stable.
1271 1.53.2.2 pgoyette *
1272 1.53.2.2 pgoyette * v_interlock locked on entry and unlocked on exit.
1273 1.53.2.2 pgoyette */
1274 1.53.2.2 pgoyette int
1275 1.53.2.2 pgoyette vcache_vget(vnode_t *vp)
1276 1.53.2.2 pgoyette {
1277 1.53.2.2 pgoyette
1278 1.53.2.2 pgoyette KASSERT(mutex_owned(vp->v_interlock));
1279 1.53.2.2 pgoyette
1280 1.53.2.2 pgoyette /* Increment hold count to prevent vnode from disappearing. */
1281 1.53.2.2 pgoyette vp->v_holdcnt++;
1282 1.53.2.2 pgoyette VSTATE_WAIT_STABLE(vp);
1283 1.53.2.2 pgoyette vp->v_holdcnt--;
1284 1.53.2.2 pgoyette
1285 1.53.2.2 pgoyette /* If this was the last reference to a reclaimed vnode free it now. */
1286 1.53.2.2 pgoyette if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) {
1287 1.53.2.2 pgoyette if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
1288 1.53.2.2 pgoyette vcache_free(VNODE_TO_VIMPL(vp));
1289 1.53.2.2 pgoyette else
1290 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
1291 1.53.2.2 pgoyette return ENOENT;
1292 1.53.2.2 pgoyette }
1293 1.53.2.2 pgoyette VSTATE_ASSERT(vp, VS_ACTIVE);
1294 1.53.2.2 pgoyette if (vp->v_usecount == 0)
1295 1.53.2.2 pgoyette vp->v_usecount = 1;
1296 1.53.2.2 pgoyette else
1297 1.53.2.2 pgoyette atomic_inc_uint(&vp->v_usecount);
1298 1.53.2.2 pgoyette
1299 1.53.2.2 pgoyette mutex_exit(vp->v_interlock);
1300 1.53.2.2 pgoyette
1301 1.53.2.2 pgoyette return 0;
1302 1.50 hannken }
1303 1.50 hannken
1304 1.50 hannken /*
1305 1.36 hannken * Get a vnode / fs node pair by key and return it referenced through vpp.
1306 1.36 hannken */
1307 1.36 hannken int
1308 1.36 hannken vcache_get(struct mount *mp, const void *key, size_t key_len,
1309 1.36 hannken struct vnode **vpp)
1310 1.36 hannken {
1311 1.36 hannken int error;
1312 1.36 hannken uint32_t hash;
1313 1.36 hannken const void *new_key;
1314 1.36 hannken struct vnode *vp;
1315 1.36 hannken struct vcache_key vcache_key;
1316 1.53.2.2 pgoyette vnode_impl_t *vip, *new_vip;
1317 1.36 hannken
1318 1.36 hannken new_key = NULL;
1319 1.36 hannken *vpp = NULL;
1320 1.36 hannken
1321 1.36 hannken vcache_key.vk_mount = mp;
1322 1.36 hannken vcache_key.vk_key = key;
1323 1.36 hannken vcache_key.vk_key_len = key_len;
1324 1.36 hannken hash = vcache_hash(&vcache_key);
1325 1.36 hannken
1326 1.36 hannken again:
1327 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1328 1.53.2.2 pgoyette vip = vcache_hash_lookup(&vcache_key, hash);
1329 1.36 hannken
1330 1.36 hannken /* If found, take a reference or retry. */
1331 1.53.2.2 pgoyette if (__predict_true(vip != NULL)) {
1332 1.52 hannken /*
1333 1.52 hannken * If the vnode is loading we cannot take the v_interlock
1334 1.52 hannken * here as it might change during load (see uvm_obj_setlock()).
1335 1.53.2.2 pgoyette * As changing state from VS_LOADING requires both vcache_lock
1336 1.53.2.2 pgoyette * and v_interlock it is safe to test with vcache_lock held.
1337 1.52 hannken *
1338 1.53.2.1 pgoyette * Wait for vnodes changing state from VS_LOADING and retry.
1339 1.52 hannken */
1340 1.53.2.2 pgoyette if (__predict_false(vip->vi_state == VS_LOADING)) {
1341 1.53.2.2 pgoyette cv_wait(&vcache_cv, &vcache_lock);
1342 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1343 1.52 hannken goto again;
1344 1.52 hannken }
1345 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(vip);
1346 1.36 hannken mutex_enter(vp->v_interlock);
1347 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1348 1.53.2.2 pgoyette error = vcache_vget(vp);
1349 1.36 hannken if (error == ENOENT)
1350 1.36 hannken goto again;
1351 1.36 hannken if (error == 0)
1352 1.36 hannken *vpp = vp;
1353 1.36 hannken KASSERT((error != 0) == (*vpp == NULL));
1354 1.36 hannken return error;
1355 1.36 hannken }
1356 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1357 1.36 hannken
1358 1.36 hannken /* Allocate and initialize a new vcache / vnode pair. */
1359 1.53.2.4 pgoyette error = vfs_busy(mp);
1360 1.36 hannken if (error)
1361 1.36 hannken return error;
1362 1.53.2.2 pgoyette new_vip = vcache_alloc();
1363 1.53.2.2 pgoyette new_vip->vi_key = vcache_key;
1364 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(new_vip);
1365 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1366 1.53.2.2 pgoyette vip = vcache_hash_lookup(&vcache_key, hash);
1367 1.53.2.2 pgoyette if (vip == NULL) {
1368 1.53.2.2 pgoyette SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1369 1.53.2.2 pgoyette new_vip, vi_hash);
1370 1.53.2.2 pgoyette vip = new_vip;
1371 1.36 hannken }
1372 1.36 hannken
1373 1.36 hannken /* If another thread beat us inserting this node, retry. */
1374 1.53.2.2 pgoyette if (vip != new_vip) {
1375 1.53.2.4 pgoyette vcache_dealloc(new_vip);
1376 1.53.2.4 pgoyette vfs_unbusy(mp);
1377 1.36 hannken goto again;
1378 1.36 hannken }
1379 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1380 1.36 hannken
1381 1.53.2.1 pgoyette /* Load the fs node. Exclusive as new_node is VS_LOADING. */
1382 1.36 hannken error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key);
1383 1.36 hannken if (error) {
1384 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1385 1.53.2.2 pgoyette SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1386 1.53.2.2 pgoyette new_vip, vnode_impl, vi_hash);
1387 1.53.2.4 pgoyette vcache_dealloc(new_vip);
1388 1.53.2.4 pgoyette vfs_unbusy(mp);
1389 1.36 hannken KASSERT(*vpp == NULL);
1390 1.36 hannken return error;
1391 1.36 hannken }
1392 1.36 hannken KASSERT(new_key != NULL);
1393 1.36 hannken KASSERT(memcmp(key, new_key, key_len) == 0);
1394 1.36 hannken KASSERT(vp->v_op != NULL);
1395 1.36 hannken vfs_insmntque(vp, mp);
1396 1.36 hannken if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1397 1.36 hannken vp->v_vflag |= VV_MPSAFE;
1398 1.53.2.4 pgoyette vfs_ref(mp);
1399 1.53.2.4 pgoyette vfs_unbusy(mp);
1400 1.36 hannken
1401 1.36 hannken /* Finished loading, finalize node. */
1402 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1403 1.53.2.2 pgoyette new_vip->vi_key.vk_key = new_key;
1404 1.39 hannken mutex_enter(vp->v_interlock);
1405 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_LOADING, VS_ACTIVE);
1406 1.39 hannken mutex_exit(vp->v_interlock);
1407 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1408 1.36 hannken *vpp = vp;
1409 1.36 hannken return 0;
1410 1.36 hannken }
1411 1.36 hannken
1412 1.36 hannken /*
1413 1.40 hannken * Create a new vnode / fs node pair and return it referenced through vpp.
1414 1.40 hannken */
1415 1.40 hannken int
1416 1.40 hannken vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap,
1417 1.40 hannken kauth_cred_t cred, struct vnode **vpp)
1418 1.40 hannken {
1419 1.40 hannken int error;
1420 1.40 hannken uint32_t hash;
1421 1.53.2.2 pgoyette struct vnode *vp, *ovp;
1422 1.53.2.2 pgoyette vnode_impl_t *vip, *ovip;
1423 1.40 hannken
1424 1.40 hannken *vpp = NULL;
1425 1.40 hannken
1426 1.40 hannken /* Allocate and initialize a new vcache / vnode pair. */
1427 1.53.2.4 pgoyette error = vfs_busy(mp);
1428 1.40 hannken if (error)
1429 1.40 hannken return error;
1430 1.53.2.2 pgoyette vip = vcache_alloc();
1431 1.53.2.2 pgoyette vip->vi_key.vk_mount = mp;
1432 1.53.2.2 pgoyette vp = VIMPL_TO_VNODE(vip);
1433 1.40 hannken
1434 1.40 hannken /* Create and load the fs node. */
1435 1.40 hannken error = VFS_NEWVNODE(mp, dvp, vp, vap, cred,
1436 1.53.2.2 pgoyette &vip->vi_key.vk_key_len, &vip->vi_key.vk_key);
1437 1.40 hannken if (error) {
1438 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1439 1.53.2.4 pgoyette vcache_dealloc(vip);
1440 1.53.2.4 pgoyette vfs_unbusy(mp);
1441 1.40 hannken KASSERT(*vpp == NULL);
1442 1.40 hannken return error;
1443 1.40 hannken }
1444 1.53.2.2 pgoyette KASSERT(vip->vi_key.vk_key != NULL);
1445 1.40 hannken KASSERT(vp->v_op != NULL);
1446 1.53.2.2 pgoyette hash = vcache_hash(&vip->vi_key);
1447 1.40 hannken
1448 1.40 hannken /* Wait for previous instance to be reclaimed, then insert new node. */
1449 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1450 1.53.2.2 pgoyette while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) {
1451 1.53.2.2 pgoyette ovp = VIMPL_TO_VNODE(ovip);
1452 1.52 hannken mutex_enter(ovp->v_interlock);
1453 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1454 1.53.2.2 pgoyette error = vcache_vget(ovp);
1455 1.52 hannken KASSERT(error == ENOENT);
1456 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1457 1.40 hannken }
1458 1.53.2.2 pgoyette SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
1459 1.53.2.2 pgoyette vip, vi_hash);
1460 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1461 1.40 hannken vfs_insmntque(vp, mp);
1462 1.40 hannken if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
1463 1.40 hannken vp->v_vflag |= VV_MPSAFE;
1464 1.53.2.4 pgoyette vfs_ref(mp);
1465 1.53.2.4 pgoyette vfs_unbusy(mp);
1466 1.40 hannken
1467 1.40 hannken /* Finished loading, finalize node. */
1468 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1469 1.52 hannken mutex_enter(vp->v_interlock);
1470 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_LOADING, VS_ACTIVE);
1471 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1472 1.40 hannken mutex_exit(vp->v_interlock);
1473 1.40 hannken *vpp = vp;
1474 1.40 hannken return 0;
1475 1.40 hannken }
1476 1.40 hannken
1477 1.40 hannken /*
1478 1.53.2.2 pgoyette * Prepare key change: update old cache nodes key and lock new cache node.
1479 1.37 hannken * Return an error if the new node already exists.
1480 1.37 hannken */
1481 1.37 hannken int
1482 1.37 hannken vcache_rekey_enter(struct mount *mp, struct vnode *vp,
1483 1.37 hannken const void *old_key, size_t old_key_len,
1484 1.37 hannken const void *new_key, size_t new_key_len)
1485 1.37 hannken {
1486 1.37 hannken uint32_t old_hash, new_hash;
1487 1.37 hannken struct vcache_key old_vcache_key, new_vcache_key;
1488 1.53.2.2 pgoyette vnode_impl_t *vip, *new_vip;
1489 1.37 hannken
1490 1.37 hannken old_vcache_key.vk_mount = mp;
1491 1.37 hannken old_vcache_key.vk_key = old_key;
1492 1.37 hannken old_vcache_key.vk_key_len = old_key_len;
1493 1.37 hannken old_hash = vcache_hash(&old_vcache_key);
1494 1.37 hannken
1495 1.37 hannken new_vcache_key.vk_mount = mp;
1496 1.37 hannken new_vcache_key.vk_key = new_key;
1497 1.37 hannken new_vcache_key.vk_key_len = new_key_len;
1498 1.37 hannken new_hash = vcache_hash(&new_vcache_key);
1499 1.37 hannken
1500 1.53.2.2 pgoyette new_vip = vcache_alloc();
1501 1.53.2.2 pgoyette new_vip->vi_key = new_vcache_key;
1502 1.37 hannken
1503 1.52 hannken /* Insert locked new node used as placeholder. */
1504 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1505 1.53.2.2 pgoyette vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1506 1.53.2.2 pgoyette if (vip != NULL) {
1507 1.53.2.4 pgoyette vcache_dealloc(new_vip);
1508 1.37 hannken return EEXIST;
1509 1.37 hannken }
1510 1.53.2.2 pgoyette SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1511 1.53.2.2 pgoyette new_vip, vi_hash);
1512 1.49 hannken
1513 1.53.2.2 pgoyette /* Replace old nodes key with the temporary copy. */
1514 1.53.2.2 pgoyette vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1515 1.53.2.2 pgoyette KASSERT(vip != NULL);
1516 1.53.2.2 pgoyette KASSERT(VIMPL_TO_VNODE(vip) == vp);
1517 1.53.2.2 pgoyette KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key);
1518 1.53.2.2 pgoyette vip->vi_key = old_vcache_key;
1519 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1520 1.37 hannken return 0;
1521 1.37 hannken }
1522 1.37 hannken
1523 1.37 hannken /*
1524 1.53.2.2 pgoyette * Key change complete: update old node and remove placeholder.
1525 1.37 hannken */
1526 1.37 hannken void
1527 1.37 hannken vcache_rekey_exit(struct mount *mp, struct vnode *vp,
1528 1.37 hannken const void *old_key, size_t old_key_len,
1529 1.37 hannken const void *new_key, size_t new_key_len)
1530 1.37 hannken {
1531 1.37 hannken uint32_t old_hash, new_hash;
1532 1.37 hannken struct vcache_key old_vcache_key, new_vcache_key;
1533 1.53.2.2 pgoyette vnode_impl_t *vip, *new_vip;
1534 1.53.2.2 pgoyette struct vnode *new_vp;
1535 1.37 hannken
1536 1.37 hannken old_vcache_key.vk_mount = mp;
1537 1.37 hannken old_vcache_key.vk_key = old_key;
1538 1.37 hannken old_vcache_key.vk_key_len = old_key_len;
1539 1.37 hannken old_hash = vcache_hash(&old_vcache_key);
1540 1.37 hannken
1541 1.37 hannken new_vcache_key.vk_mount = mp;
1542 1.37 hannken new_vcache_key.vk_key = new_key;
1543 1.37 hannken new_vcache_key.vk_key_len = new_key_len;
1544 1.37 hannken new_hash = vcache_hash(&new_vcache_key);
1545 1.37 hannken
1546 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1547 1.49 hannken
1548 1.49 hannken /* Lookup old and new node. */
1549 1.53.2.2 pgoyette vip = vcache_hash_lookup(&old_vcache_key, old_hash);
1550 1.53.2.2 pgoyette KASSERT(vip != NULL);
1551 1.53.2.2 pgoyette KASSERT(VIMPL_TO_VNODE(vip) == vp);
1552 1.53.2.2 pgoyette
1553 1.53.2.2 pgoyette new_vip = vcache_hash_lookup(&new_vcache_key, new_hash);
1554 1.53.2.2 pgoyette KASSERT(new_vip != NULL);
1555 1.53.2.2 pgoyette KASSERT(new_vip->vi_key.vk_key_len == new_key_len);
1556 1.53.2.2 pgoyette new_vp = VIMPL_TO_VNODE(new_vip);
1557 1.53.2.2 pgoyette mutex_enter(new_vp->v_interlock);
1558 1.53.2.2 pgoyette VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING);
1559 1.53.2.4 pgoyette mutex_exit(new_vp->v_interlock);
1560 1.49 hannken
1561 1.49 hannken /* Rekey old node and put it onto its new hashlist. */
1562 1.53.2.2 pgoyette vip->vi_key = new_vcache_key;
1563 1.49 hannken if (old_hash != new_hash) {
1564 1.53.2.2 pgoyette SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask],
1565 1.53.2.2 pgoyette vip, vnode_impl, vi_hash);
1566 1.53.2.2 pgoyette SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
1567 1.53.2.2 pgoyette vip, vi_hash);
1568 1.49 hannken }
1569 1.49 hannken
1570 1.49 hannken /* Remove new node used as placeholder. */
1571 1.53.2.2 pgoyette SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask],
1572 1.53.2.2 pgoyette new_vip, vnode_impl, vi_hash);
1573 1.53.2.4 pgoyette vcache_dealloc(new_vip);
1574 1.37 hannken }
1575 1.37 hannken
1576 1.37 hannken /*
1577 1.36 hannken * Remove a vnode / fs node pair from the cache.
1578 1.36 hannken */
1579 1.36 hannken void
1580 1.36 hannken vcache_remove(struct mount *mp, const void *key, size_t key_len)
1581 1.36 hannken {
1582 1.53.2.1 pgoyette lwp_t *l = curlwp;
1583 1.53.2.2 pgoyette vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
1584 1.53.2.3 pgoyette struct mount *mp = vp->v_mount;
1585 1.36 hannken uint32_t hash;
1586 1.36 hannken struct vcache_key vcache_key;
1587 1.36 hannken struct vcache_node *node;
1588 1.36 hannken
1589 1.36 hannken vcache_key.vk_mount = mp;
1590 1.36 hannken vcache_key.vk_key = key;
1591 1.36 hannken vcache_key.vk_key_len = key_len;
1592 1.36 hannken hash = vcache_hash(&vcache_key);
1593 1.36 hannken
1594 1.53.2.1 pgoyette active = (vp->v_usecount > 1);
1595 1.53.2.2 pgoyette temp_key_len = vip->vi_key.vk_key_len;
1596 1.53.2.1 pgoyette /*
1597 1.53.2.1 pgoyette * Prevent the vnode from being recycled or brought into use
1598 1.53.2.1 pgoyette * while we clean it out.
1599 1.53.2.1 pgoyette */
1600 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_ACTIVE, VS_RECLAIMING);
1601 1.53.2.1 pgoyette if (vp->v_iflag & VI_EXECMAP) {
1602 1.53.2.1 pgoyette atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
1603 1.53.2.1 pgoyette atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
1604 1.53.2.1 pgoyette }
1605 1.53.2.1 pgoyette vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
1606 1.53.2.1 pgoyette mutex_exit(vp->v_interlock);
1607 1.53.2.1 pgoyette
1608 1.53.2.1 pgoyette /* Replace the vnode key with a temporary copy. */
1609 1.53.2.2 pgoyette if (vip->vi_key.vk_key_len > sizeof(temp_buf)) {
1610 1.53.2.1 pgoyette temp_key = kmem_alloc(temp_key_len, KM_SLEEP);
1611 1.53.2.1 pgoyette } else {
1612 1.53.2.1 pgoyette temp_key = temp_buf;
1613 1.53.2.1 pgoyette }
1614 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1615 1.53.2.2 pgoyette memcpy(temp_key, vip->vi_key.vk_key, temp_key_len);
1616 1.53.2.2 pgoyette vip->vi_key.vk_key = temp_key;
1617 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1618 1.53.2.1 pgoyette
1619 1.53.2.4 pgoyette fstrans_start(mp, FSTRANS_SHARED);
1620 1.53.2.3 pgoyette
1621 1.53.2.1 pgoyette /*
1622 1.53.2.1 pgoyette * Clean out any cached data associated with the vnode.
1623 1.53.2.1 pgoyette * If purging an active vnode, it must be closed and
1624 1.53.2.2 pgoyette * deactivated before being reclaimed.
1625 1.53.2.1 pgoyette */
1626 1.53.2.1 pgoyette error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
1627 1.53.2.1 pgoyette if (error != 0) {
1628 1.53.2.1 pgoyette if (wapbl_vphaswapbl(vp))
1629 1.53.2.1 pgoyette WAPBL_DISCARD(wapbl_vptomp(vp));
1630 1.53.2.1 pgoyette error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
1631 1.53.2.1 pgoyette }
1632 1.53.2.1 pgoyette KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
1633 1.53.2.1 pgoyette KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1634 1.53.2.1 pgoyette if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
1635 1.53.2.1 pgoyette spec_node_revoke(vp);
1636 1.53.2.1 pgoyette }
1637 1.53.2.1 pgoyette
1638 1.53.2.2 pgoyette /*
1639 1.53.2.2 pgoyette * Disassociate the underlying file system from the vnode.
1640 1.53.2.4 pgoyette * Note that the VOP_INACTIVE will not unlock the vnode.
1641 1.53.2.2 pgoyette */
1642 1.53.2.2 pgoyette VOP_INACTIVE(vp, &recycle);
1643 1.53.2.4 pgoyette VOP_UNLOCK(vp);
1644 1.53.2.1 pgoyette if (VOP_RECLAIM(vp)) {
1645 1.53.2.1 pgoyette vnpanic(vp, "%s: cannot reclaim", __func__);
1646 1.53.2.1 pgoyette }
1647 1.53.2.1 pgoyette
1648 1.53.2.1 pgoyette KASSERT(vp->v_data == NULL);
1649 1.53.2.1 pgoyette KASSERT(vp->v_uobj.uo_npages == 0);
1650 1.53.2.1 pgoyette
1651 1.53.2.1 pgoyette if (vp->v_type == VREG && vp->v_ractx != NULL) {
1652 1.53.2.1 pgoyette uvm_ra_freectx(vp->v_ractx);
1653 1.53.2.1 pgoyette vp->v_ractx = NULL;
1654 1.53.2.1 pgoyette }
1655 1.53.2.1 pgoyette
1656 1.53.2.1 pgoyette /* Purge name cache. */
1657 1.53.2.1 pgoyette cache_purge(vp);
1658 1.53.2.1 pgoyette
1659 1.53.2.1 pgoyette /* Remove from vnode cache. */
1660 1.53.2.2 pgoyette hash = vcache_hash(&vip->vi_key);
1661 1.53.2.2 pgoyette mutex_enter(&vcache_lock);
1662 1.53.2.2 pgoyette KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
1663 1.53.2.2 pgoyette SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
1664 1.53.2.2 pgoyette vip, vnode_impl, vi_hash);
1665 1.53.2.2 pgoyette mutex_exit(&vcache_lock);
1666 1.53.2.1 pgoyette if (temp_key != temp_buf)
1667 1.53.2.1 pgoyette kmem_free(temp_key, temp_key_len);
1668 1.50 hannken
1669 1.53.2.1 pgoyette /* Done with purge, notify sleepers of the grim news. */
1670 1.53.2.1 pgoyette mutex_enter(vp->v_interlock);
1671 1.53.2.1 pgoyette vp->v_op = dead_vnodeop_p;
1672 1.53.2.1 pgoyette vp->v_vflag |= VV_LOCKSWORK;
1673 1.53.2.1 pgoyette VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED);
1674 1.53.2.1 pgoyette vp->v_tag = VT_NON;
1675 1.53.2.1 pgoyette KNOTE(&vp->v_klist, NOTE_REVOKE);
1676 1.53.2.4 pgoyette mutex_exit(vp->v_interlock);
1677 1.50 hannken
1678 1.53.2.4 pgoyette /*
1679 1.53.2.4 pgoyette * Move to dead mount. Must be after changing the operations
1680 1.53.2.4 pgoyette * vector as vnode operations enter the mount before using the
1681 1.53.2.4 pgoyette * operations vector. See sys/kern/vnode_if.c.
1682 1.53.2.4 pgoyette */
1683 1.53.2.4 pgoyette vp->v_vflag &= ~VV_ROOT;
1684 1.53.2.4 pgoyette vfs_ref(dead_rootmount);
1685 1.53.2.4 pgoyette vfs_insmntque(vp, dead_rootmount);
1686 1.53.2.3 pgoyette
1687 1.53.2.4 pgoyette mutex_enter(vp->v_interlock);
1688 1.53.2.4 pgoyette fstrans_done(mp);
1689 1.53.2.1 pgoyette KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
1690 1.36 hannken }
1691 1.36 hannken
1692 1.1 rmind /*
1693 1.1 rmind * Update outstanding I/O count and do wakeup if requested.
1694 1.1 rmind */
1695 1.1 rmind void
1696 1.1 rmind vwakeup(struct buf *bp)
1697 1.1 rmind {
1698 1.1 rmind vnode_t *vp;
1699 1.1 rmind
1700 1.1 rmind if ((vp = bp->b_vp) == NULL)
1701 1.1 rmind return;
1702 1.1 rmind
1703 1.9 rmind KASSERT(bp->b_objlock == vp->v_interlock);
1704 1.1 rmind KASSERT(mutex_owned(bp->b_objlock));
1705 1.1 rmind
1706 1.1 rmind if (--vp->v_numoutput < 0)
1707 1.11 christos vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
1708 1.1 rmind if (vp->v_numoutput == 0)
1709 1.1 rmind cv_broadcast(&vp->v_cv);
1710 1.1 rmind }
1711 1.1 rmind
1712 1.1 rmind /*
1713 1.35 hannken * Test a vnode for being or becoming dead. Returns one of:
1714 1.35 hannken * EBUSY: vnode is becoming dead, with "flags == VDEAD_NOWAIT" only.
1715 1.35 hannken * ENOENT: vnode is dead.
1716 1.35 hannken * 0: otherwise.
1717 1.35 hannken *
1718 1.35 hannken * Whenever this function returns a non-zero value all future
1719 1.35 hannken * calls will also return a non-zero value.
1720 1.35 hannken */
1721 1.35 hannken int
1722 1.35 hannken vdead_check(struct vnode *vp, int flags)
1723 1.35 hannken {
1724 1.35 hannken
1725 1.35 hannken KASSERT(mutex_owned(vp->v_interlock));
1726 1.35 hannken
1727 1.52 hannken if (! ISSET(flags, VDEAD_NOWAIT))
1728 1.52 hannken VSTATE_WAIT_STABLE(vp);
1729 1.1 rmind
1730 1.53.2.1 pgoyette if (VSTATE_GET(vp) == VS_RECLAIMING) {
1731 1.52 hannken KASSERT(ISSET(flags, VDEAD_NOWAIT));
1732 1.52 hannken return EBUSY;
1733 1.53.2.1 pgoyette } else if (VSTATE_GET(vp) == VS_RECLAIMED) {
1734 1.52 hannken return ENOENT;
1735 1.52 hannken }
1736 1.1 rmind
1737 1.52 hannken return 0;
1738 1.1 rmind }
1739 1.1 rmind
1740 1.1 rmind int
1741 1.53.2.2 pgoyette vfs_drainvnodes(void)
1742 1.1 rmind {
1743 1.53.2.2 pgoyette int i, gen;
1744 1.12 hannken
1745 1.53.2.2 pgoyette mutex_enter(&vdrain_lock);
1746 1.53.2.2 pgoyette for (i = 0; i < 2; i++) {
1747 1.53.2.2 pgoyette gen = vdrain_gen;
1748 1.53.2.2 pgoyette while (gen == vdrain_gen) {
1749 1.53.2.2 pgoyette cv_broadcast(&vdrain_cv);
1750 1.53.2.2 pgoyette cv_wait(&vdrain_gen_cv, &vdrain_lock);
1751 1.53.2.2 pgoyette }
1752 1.1 rmind }
1753 1.53.2.2 pgoyette mutex_exit(&vdrain_lock);
1754 1.12 hannken
1755 1.53.2.2 pgoyette if (numvnodes >= desiredvnodes)
1756 1.53.2.2 pgoyette return EBUSY;
1757 1.12 hannken
1758 1.53.2.2 pgoyette if (vcache_hashsize != desiredvnodes)
1759 1.53.2.2 pgoyette vcache_reinit();
1760 1.36 hannken
1761 1.1 rmind return 0;
1762 1.1 rmind }
1763 1.1 rmind
1764 1.1 rmind void
1765 1.11 christos vnpanic(vnode_t *vp, const char *fmt, ...)
1766 1.1 rmind {
1767 1.11 christos va_list ap;
1768 1.11 christos
1769 1.1 rmind #ifdef DIAGNOSTIC
1770 1.1 rmind vprint(NULL, vp);
1771 1.1 rmind #endif
1772 1.11 christos va_start(ap, fmt);
1773 1.11 christos vpanic(fmt, ap);
1774 1.11 christos va_end(ap);
1775 1.1 rmind }
1776