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