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