vfs_cache.c revision 1.100 1 1.100 dennis /* $NetBSD: vfs_cache.c,v 1.100 2014/11/30 04:11:03 dennis Exp $ */
2 1.73 ad
3 1.73 ad /*-
4 1.73 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.73 ad * All rights reserved.
6 1.73 ad *
7 1.73 ad * Redistribution and use in source and binary forms, with or without
8 1.73 ad * modification, are permitted provided that the following conditions
9 1.73 ad * are met:
10 1.73 ad * 1. Redistributions of source code must retain the above copyright
11 1.73 ad * notice, this list of conditions and the following disclaimer.
12 1.73 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.73 ad * notice, this list of conditions and the following disclaimer in the
14 1.73 ad * documentation and/or other materials provided with the distribution.
15 1.73 ad *
16 1.73 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.73 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.73 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.73 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.73 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.73 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.73 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.73 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.73 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.73 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.73 ad * POSSIBILITY OF SUCH DAMAGE.
27 1.73 ad */
28 1.6 cgd
29 1.1 cgd /*
30 1.5 mycroft * Copyright (c) 1989, 1993
31 1.5 mycroft * The Regents of the University of California. All rights reserved.
32 1.1 cgd *
33 1.1 cgd * Redistribution and use in source and binary forms, with or without
34 1.1 cgd * modification, are permitted provided that the following conditions
35 1.1 cgd * are met:
36 1.1 cgd * 1. Redistributions of source code must retain the above copyright
37 1.1 cgd * notice, this list of conditions and the following disclaimer.
38 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 cgd * notice, this list of conditions and the following disclaimer in the
40 1.1 cgd * documentation and/or other materials provided with the distribution.
41 1.51 agc * 3. Neither the name of the University nor the names of its contributors
42 1.1 cgd * may be used to endorse or promote products derived from this software
43 1.1 cgd * without specific prior written permission.
44 1.1 cgd *
45 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 cgd * SUCH DAMAGE.
56 1.1 cgd *
57 1.10 mycroft * @(#)vfs_cache.c 8.3 (Berkeley) 8/22/94
58 1.1 cgd */
59 1.32 lukem
60 1.32 lukem #include <sys/cdefs.h>
61 1.100 dennis __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.100 2014/11/30 04:11:03 dennis Exp $");
62 1.1 cgd
63 1.28 chs #include "opt_ddb.h"
64 1.29 fvdl #include "opt_revcache.h"
65 1.28 chs
66 1.4 mycroft #include <sys/param.h>
67 1.4 mycroft #include <sys/systm.h>
68 1.97 joerg #include <sys/sysctl.h>
69 1.4 mycroft #include <sys/time.h>
70 1.4 mycroft #include <sys/mount.h>
71 1.4 mycroft #include <sys/vnode.h>
72 1.4 mycroft #include <sys/namei.h>
73 1.4 mycroft #include <sys/errno.h>
74 1.18 thorpej #include <sys/pool.h>
75 1.68 ad #include <sys/mutex.h>
76 1.73 ad #include <sys/atomic.h>
77 1.73 ad #include <sys/kthread.h>
78 1.73 ad #include <sys/kernel.h>
79 1.73 ad #include <sys/cpu.h>
80 1.73 ad #include <sys/evcnt.h>
81 1.1 cgd
82 1.66 christos #define NAMECACHE_ENTER_REVERSE
83 1.1 cgd /*
84 1.1 cgd * Name caching works as follows:
85 1.1 cgd *
86 1.1 cgd * Names found by directory scans are retained in a cache
87 1.1 cgd * for future reference. It is managed LRU, so frequently
88 1.1 cgd * used names will hang around. Cache is indexed by hash value
89 1.20 jdolecek * obtained from (dvp, name) where dvp refers to the directory
90 1.1 cgd * containing name.
91 1.1 cgd *
92 1.1 cgd * For simplicity (and economy of storage), names longer than
93 1.1 cgd * a maximum length of NCHNAMLEN are not cached; they occur
94 1.1 cgd * infrequently in any case, and are almost never of interest.
95 1.1 cgd *
96 1.1 cgd * Upon reaching the last segment of a path, if the reference
97 1.1 cgd * is for DELETE, or NOCACHE is set (rewrite), and the
98 1.1 cgd * name is located in the cache, it will be dropped.
99 1.20 jdolecek * The entry is dropped also when it was not possible to lock
100 1.20 jdolecek * the cached vnode, either because vget() failed or the generation
101 1.20 jdolecek * number has changed while waiting for the lock.
102 1.1 cgd */
103 1.1 cgd
104 1.1 cgd /*
105 1.100 dennis * The locking in this subsystem works as follows:
106 1.100 dennis *
107 1.100 dennis * When an entry is added to the cache, via cache_enter(),
108 1.100 dennis * namecache_lock is taken to exclude other writers. The new
109 1.100 dennis * entry is added to the hash list in a way which permits
110 1.100 dennis * concurrent lookups and invalidations in the cache done on
111 1.100 dennis * other CPUs to continue in parallel.
112 1.100 dennis *
113 1.100 dennis * When a lookup is done in the cache, via cache_lookup() or
114 1.100 dennis * cache_lookup_raw(), the per-cpu lock below is taken. This
115 1.100 dennis * protects calls to cache_lookup_entry() and cache_invalidate()
116 1.100 dennis * against cache_reclaim(), and protects the per-cpu stats against
117 1.100 dennis * modification in both cache_reclaim() and cache_stat_sysctl(),
118 1.100 dennis * but allows lookups to continue in parallel with cache_enter().
119 1.100 dennis *
120 1.100 dennis * cache_revlookup() takes namecache_lock to exclude cache_enter()
121 1.100 dennis * and cache_reclaim() since the list it operates on is not
122 1.100 dennis * maintained to allow concurrent reads.
123 1.100 dennis *
124 1.100 dennis * When cache_reclaim() is called namecache_lock is held to hold
125 1.100 dennis * off calls to cache_enter() and each of the per-cpu locks is
126 1.100 dennis * taken to hold off lookups. Holding all these locks essentially
127 1.100 dennis * idles the subsystem, ensuring there are no concurrent references
128 1.100 dennis * to the cache entries being freed. As a side effect, once the
129 1.100 dennis * per-cpu locks are held, the per-cpu stats are added to the
130 1.100 dennis * subsystem totals and then zeroed. cache_stat_sysctl() similarly
131 1.100 dennis * takes all locks to collect the per-cpu stats (though it perhaps
132 1.100 dennis * could avoid this by living with stats that were a second out
133 1.100 dennis * of date?).
134 1.100 dennis *
135 1.100 dennis * The per-cpu namecache data is defined below. cpu_lock is used
136 1.100 dennis * to protect cpu_stats updates and to exclude cache_reclaim()
137 1.100 dennis * during lookups.
138 1.77 ad */
139 1.77 ad struct nchcpu {
140 1.77 ad kmutex_t cpu_lock;
141 1.77 ad struct nchstats cpu_stats;
142 1.77 ad };
143 1.77 ad
144 1.77 ad /*
145 1.90 dholland * The type for the hash code. While the hash function generates a
146 1.90 dholland * u32, the hash code has historically been passed around as a u_long,
147 1.90 dholland * and the value is modified by xor'ing a uintptr_t, so it's not
148 1.90 dholland * entirely clear what the best type is. For now I'll leave it
149 1.90 dholland * unchanged as u_long.
150 1.90 dholland */
151 1.90 dholland
152 1.90 dholland typedef u_long nchash_t;
153 1.90 dholland
154 1.90 dholland /*
155 1.1 cgd * Structures associated with name cacheing.
156 1.1 cgd */
157 1.89 rmind
158 1.89 rmind static kmutex_t *namecache_lock __read_mostly;
159 1.89 rmind static pool_cache_t namecache_cache __read_mostly;
160 1.89 rmind static TAILQ_HEAD(, namecache) nclruhead __cacheline_aligned;
161 1.89 rmind
162 1.89 rmind static LIST_HEAD(nchashhead, namecache) *nchashtbl __read_mostly;
163 1.89 rmind static u_long nchash __read_mostly;
164 1.89 rmind
165 1.90 dholland #define NCHASH2(hash, dvp) \
166 1.90 dholland (((hash) ^ ((uintptr_t)(dvp) >> 3)) & nchash)
167 1.19 sommerfe
168 1.89 rmind static LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl __read_mostly;
169 1.89 rmind static u_long ncvhash __read_mostly;
170 1.89 rmind
171 1.48 yamt #define NCVHASH(vp) (((uintptr_t)(vp) >> 3) & ncvhash)
172 1.19 sommerfe
173 1.89 rmind /* Number of cache entries allocated. */
174 1.89 rmind static long numcache __cacheline_aligned;
175 1.73 ad
176 1.89 rmind /* Garbage collection queue and number of entries pending in it. */
177 1.89 rmind static void *cache_gcqueue;
178 1.89 rmind static u_int cache_gcpend;
179 1.89 rmind
180 1.89 rmind /* Cache effectiveness statistics. */
181 1.89 rmind struct nchstats nchstats __cacheline_aligned;
182 1.77 ad #define COUNT(c,x) (c.x++)
183 1.38 thorpej
184 1.89 rmind static const int cache_lowat = 95;
185 1.89 rmind static const int cache_hiwat = 98;
186 1.89 rmind static const int cache_hottime = 5; /* number of seconds */
187 1.89 rmind static int doingcache = 1; /* 1 => enable the cache */
188 1.1 cgd
189 1.73 ad static struct evcnt cache_ev_scan;
190 1.73 ad static struct evcnt cache_ev_gc;
191 1.73 ad static struct evcnt cache_ev_over;
192 1.73 ad static struct evcnt cache_ev_under;
193 1.73 ad static struct evcnt cache_ev_forced;
194 1.73 ad
195 1.73 ad static void cache_invalidate(struct namecache *);
196 1.89 rmind static struct namecache *cache_lookup_entry(
197 1.91 dholland const struct vnode *, const char *, size_t);
198 1.73 ad static void cache_thread(void *);
199 1.73 ad static void cache_invalidate(struct namecache *);
200 1.73 ad static void cache_disassociate(struct namecache *);
201 1.73 ad static void cache_reclaim(void);
202 1.73 ad static int cache_ctor(void *, void *, int);
203 1.73 ad static void cache_dtor(void *, void *);
204 1.46 yamt
205 1.73 ad /*
206 1.90 dholland * Compute the hash for an entry.
207 1.90 dholland *
208 1.90 dholland * (This is for now a wrapper around namei_hash, whose interface is
209 1.90 dholland * for the time being slightly inconvenient.)
210 1.90 dholland */
211 1.90 dholland static nchash_t
212 1.91 dholland cache_hash(const char *name, size_t namelen)
213 1.90 dholland {
214 1.90 dholland const char *endptr;
215 1.90 dholland
216 1.91 dholland endptr = name + namelen;
217 1.91 dholland return namei_hash(name, &endptr);
218 1.90 dholland }
219 1.90 dholland
220 1.90 dholland /*
221 1.73 ad * Invalidate a cache entry and enqueue it for garbage collection.
222 1.100 dennis * The caller needs to hold namecache_lock or a per-cpu lock to hold
223 1.100 dennis * off cache_reclaim().
224 1.73 ad */
225 1.46 yamt static void
226 1.73 ad cache_invalidate(struct namecache *ncp)
227 1.46 yamt {
228 1.73 ad void *head;
229 1.46 yamt
230 1.73 ad KASSERT(mutex_owned(&ncp->nc_lock));
231 1.46 yamt
232 1.73 ad if (ncp->nc_dvp != NULL) {
233 1.73 ad ncp->nc_vp = NULL;
234 1.73 ad ncp->nc_dvp = NULL;
235 1.73 ad do {
236 1.73 ad head = cache_gcqueue;
237 1.73 ad ncp->nc_gcqueue = head;
238 1.73 ad } while (atomic_cas_ptr(&cache_gcqueue, head, ncp) != head);
239 1.73 ad atomic_inc_uint(&cache_gcpend);
240 1.73 ad }
241 1.73 ad }
242 1.46 yamt
243 1.73 ad /*
244 1.73 ad * Disassociate a namecache entry from any vnodes it is attached to,
245 1.73 ad * and remove from the global LRU list.
246 1.73 ad */
247 1.73 ad static void
248 1.73 ad cache_disassociate(struct namecache *ncp)
249 1.73 ad {
250 1.73 ad
251 1.73 ad KASSERT(mutex_owned(namecache_lock));
252 1.73 ad KASSERT(ncp->nc_dvp == NULL);
253 1.73 ad
254 1.73 ad if (ncp->nc_lru.tqe_prev != NULL) {
255 1.73 ad TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
256 1.73 ad ncp->nc_lru.tqe_prev = NULL;
257 1.46 yamt }
258 1.46 yamt if (ncp->nc_vhash.le_prev != NULL) {
259 1.46 yamt LIST_REMOVE(ncp, nc_vhash);
260 1.46 yamt ncp->nc_vhash.le_prev = NULL;
261 1.46 yamt }
262 1.46 yamt if (ncp->nc_vlist.le_prev != NULL) {
263 1.46 yamt LIST_REMOVE(ncp, nc_vlist);
264 1.46 yamt ncp->nc_vlist.le_prev = NULL;
265 1.46 yamt }
266 1.46 yamt if (ncp->nc_dvlist.le_prev != NULL) {
267 1.46 yamt LIST_REMOVE(ncp, nc_dvlist);
268 1.46 yamt ncp->nc_dvlist.le_prev = NULL;
269 1.46 yamt }
270 1.46 yamt }
271 1.46 yamt
272 1.73 ad /*
273 1.73 ad * Lock all CPUs to prevent any cache lookup activity. Conceptually,
274 1.73 ad * this locks out all "readers".
275 1.73 ad */
276 1.96 joerg #define UPDATE(f) do { \
277 1.96 joerg nchstats.f += cpup->cpu_stats.f; \
278 1.96 joerg cpup->cpu_stats.f = 0; \
279 1.96 joerg } while (/* CONSTCOND */ 0)
280 1.96 joerg
281 1.46 yamt static void
282 1.73 ad cache_lock_cpus(void)
283 1.46 yamt {
284 1.73 ad CPU_INFO_ITERATOR cii;
285 1.73 ad struct cpu_info *ci;
286 1.77 ad struct nchcpu *cpup;
287 1.46 yamt
288 1.73 ad for (CPU_INFO_FOREACH(cii, ci)) {
289 1.77 ad cpup = ci->ci_data.cpu_nch;
290 1.77 ad mutex_enter(&cpup->cpu_lock);
291 1.96 joerg UPDATE(ncs_goodhits);
292 1.96 joerg UPDATE(ncs_neghits);
293 1.96 joerg UPDATE(ncs_badhits);
294 1.96 joerg UPDATE(ncs_falsehits);
295 1.96 joerg UPDATE(ncs_miss);
296 1.96 joerg UPDATE(ncs_long);
297 1.96 joerg UPDATE(ncs_pass2);
298 1.96 joerg UPDATE(ncs_2passes);
299 1.96 joerg UPDATE(ncs_revhits);
300 1.96 joerg UPDATE(ncs_revmiss);
301 1.73 ad }
302 1.46 yamt }
303 1.46 yamt
304 1.96 joerg #undef UPDATE
305 1.96 joerg
306 1.73 ad /*
307 1.73 ad * Release all CPU locks.
308 1.73 ad */
309 1.73 ad static void
310 1.73 ad cache_unlock_cpus(void)
311 1.73 ad {
312 1.73 ad CPU_INFO_ITERATOR cii;
313 1.73 ad struct cpu_info *ci;
314 1.77 ad struct nchcpu *cpup;
315 1.73 ad
316 1.73 ad for (CPU_INFO_FOREACH(cii, ci)) {
317 1.77 ad cpup = ci->ci_data.cpu_nch;
318 1.77 ad mutex_exit(&cpup->cpu_lock);
319 1.73 ad }
320 1.73 ad }
321 1.73 ad
322 1.73 ad /*
323 1.99 joerg * Find a single cache entry and return it locked.
324 1.100 dennis * The caller needs to hold namecache_lock or a per-cpu lock to hold
325 1.100 dennis * off cache_reclaim().
326 1.73 ad */
327 1.73 ad static struct namecache *
328 1.91 dholland cache_lookup_entry(const struct vnode *dvp, const char *name, size_t namelen)
329 1.55 yamt {
330 1.55 yamt struct nchashhead *ncpp;
331 1.55 yamt struct namecache *ncp;
332 1.90 dholland nchash_t hash;
333 1.55 yamt
334 1.84 yamt KASSERT(dvp != NULL);
335 1.91 dholland hash = cache_hash(name, namelen);
336 1.90 dholland ncpp = &nchashtbl[NCHASH2(hash, dvp)];
337 1.55 yamt
338 1.55 yamt LIST_FOREACH(ncp, ncpp, nc_hash) {
339 1.100 dennis /* XXX Needs barrier for Alpha here */
340 1.73 ad if (ncp->nc_dvp != dvp ||
341 1.91 dholland ncp->nc_nlen != namelen ||
342 1.91 dholland memcmp(ncp->nc_name, name, (u_int)ncp->nc_nlen))
343 1.73 ad continue;
344 1.73 ad mutex_enter(&ncp->nc_lock);
345 1.77 ad if (__predict_true(ncp->nc_dvp == dvp)) {
346 1.73 ad ncp->nc_hittime = hardclock_ticks;
347 1.73 ad return ncp;
348 1.73 ad }
349 1.73 ad /* Raced: entry has been nullified. */
350 1.73 ad mutex_exit(&ncp->nc_lock);
351 1.55 yamt }
352 1.55 yamt
353 1.73 ad return NULL;
354 1.55 yamt }
355 1.55 yamt
356 1.1 cgd /*
357 1.1 cgd * Look for a the name in the cache. We don't do this
358 1.1 cgd * if the segment name is long, simply so the cache can avoid
359 1.1 cgd * holding long names (which would either waste space, or
360 1.1 cgd * add greatly to the complexity).
361 1.1 cgd *
362 1.90 dholland * Lookup is called with DVP pointing to the directory to search,
363 1.90 dholland * and CNP providing the name of the entry being sought: cn_nameptr
364 1.90 dholland * is the name, cn_namelen is its length, and cn_flags is the flags
365 1.90 dholland * word from the namei operation.
366 1.90 dholland *
367 1.90 dholland * DVP must be locked.
368 1.90 dholland *
369 1.90 dholland * There are three possible non-error return states:
370 1.90 dholland * 1. Nothing was found in the cache. Nothing is known about
371 1.90 dholland * the requested name.
372 1.90 dholland * 2. A negative entry was found in the cache, meaning that the
373 1.90 dholland * requested name definitely does not exist.
374 1.90 dholland * 3. A positive entry was found in the cache, meaning that the
375 1.90 dholland * requested name does exist and that we are providing the
376 1.90 dholland * vnode.
377 1.90 dholland * In these cases the results are:
378 1.90 dholland * 1. 0 returned; VN is set to NULL.
379 1.90 dholland * 2. 1 returned; VN is set to NULL.
380 1.90 dholland * 3. 1 returned; VN is set to the vnode found.
381 1.90 dholland *
382 1.90 dholland * The additional result argument ISWHT is set to zero, unless a
383 1.90 dholland * negative entry is found that was entered as a whiteout, in which
384 1.90 dholland * case ISWHT is set to one.
385 1.90 dholland *
386 1.90 dholland * The ISWHT_RET argument pointer may be null. In this case an
387 1.90 dholland * assertion is made that the whiteout flag is not set. File systems
388 1.90 dholland * that do not support whiteouts can/should do this.
389 1.90 dholland *
390 1.90 dholland * Filesystems that do support whiteouts should add ISWHITEOUT to
391 1.90 dholland * cnp->cn_flags if ISWHT comes back nonzero.
392 1.90 dholland *
393 1.90 dholland * When a vnode is returned, it is locked, as per the vnode lookup
394 1.90 dholland * locking protocol.
395 1.90 dholland *
396 1.90 dholland * There is no way for this function to fail, in the sense of
397 1.90 dholland * generating an error that requires aborting the namei operation.
398 1.90 dholland *
399 1.90 dholland * (Prior to October 2012, this function returned an integer status,
400 1.90 dholland * and a vnode, and mucked with the flags word in CNP for whiteouts.
401 1.90 dholland * The integer status was -1 for "nothing found", ENOENT for "a
402 1.90 dholland * negative entry found", 0 for "a positive entry found", and possibly
403 1.90 dholland * other errors, and the value of VN might or might not have been set
404 1.90 dholland * depending on what error occurred.)
405 1.1 cgd */
406 1.5 mycroft int
407 1.91 dholland cache_lookup(struct vnode *dvp, const char *name, size_t namelen,
408 1.91 dholland uint32_t nameiop, uint32_t cnflags,
409 1.90 dholland int *iswht_ret, struct vnode **vn_ret)
410 1.1 cgd {
411 1.23 augustss struct namecache *ncp;
412 1.20 jdolecek struct vnode *vp;
413 1.77 ad struct nchcpu *cpup;
414 1.100 dennis int error, ret_value;
415 1.100 dennis
416 1.1 cgd
417 1.90 dholland /* Establish default result values */
418 1.90 dholland if (iswht_ret != NULL) {
419 1.90 dholland *iswht_ret = 0;
420 1.90 dholland }
421 1.90 dholland *vn_ret = NULL;
422 1.90 dholland
423 1.77 ad if (__predict_false(!doingcache)) {
424 1.90 dholland return 0;
425 1.8 cgd }
426 1.39 pk
427 1.77 ad cpup = curcpu()->ci_data.cpu_nch;
428 1.100 dennis mutex_enter(&cpup->cpu_lock);
429 1.91 dholland if (__predict_false(namelen > NCHNAMLEN)) {
430 1.77 ad COUNT(cpup->cpu_stats, ncs_long);
431 1.77 ad mutex_exit(&cpup->cpu_lock);
432 1.90 dholland /* found nothing */
433 1.90 dholland return 0;
434 1.1 cgd }
435 1.100 dennis
436 1.91 dholland ncp = cache_lookup_entry(dvp, name, namelen);
437 1.77 ad if (__predict_false(ncp == NULL)) {
438 1.77 ad COUNT(cpup->cpu_stats, ncs_miss);
439 1.77 ad mutex_exit(&cpup->cpu_lock);
440 1.90 dholland /* found nothing */
441 1.90 dholland return 0;
442 1.1 cgd }
443 1.91 dholland if ((cnflags & MAKEENTRY) == 0) {
444 1.77 ad COUNT(cpup->cpu_stats, ncs_badhits);
445 1.77 ad /*
446 1.77 ad * Last component and we are renaming or deleting,
447 1.77 ad * the cache entry is invalid, or otherwise don't
448 1.77 ad * want cache entry to exist.
449 1.77 ad */
450 1.77 ad cache_invalidate(ncp);
451 1.77 ad mutex_exit(&ncp->nc_lock);
452 1.100 dennis mutex_exit(&cpup->cpu_lock);
453 1.90 dholland /* found nothing */
454 1.90 dholland return 0;
455 1.90 dholland }
456 1.90 dholland if (ncp->nc_vp == NULL) {
457 1.90 dholland if (iswht_ret != NULL) {
458 1.90 dholland /*
459 1.90 dholland * Restore the ISWHITEOUT flag saved earlier.
460 1.90 dholland */
461 1.90 dholland KASSERT((ncp->nc_flags & ~ISWHITEOUT) == 0);
462 1.90 dholland *iswht_ret = (ncp->nc_flags & ISWHITEOUT) != 0;
463 1.90 dholland } else {
464 1.90 dholland KASSERT(ncp->nc_flags == 0);
465 1.90 dholland }
466 1.90 dholland
467 1.91 dholland if (__predict_true(nameiop != CREATE ||
468 1.91 dholland (cnflags & ISLASTCN) == 0)) {
469 1.77 ad COUNT(cpup->cpu_stats, ncs_neghits);
470 1.90 dholland /* found neg entry; vn is already null from above */
471 1.100 dennis ret_value = 1;
472 1.20 jdolecek } else {
473 1.77 ad COUNT(cpup->cpu_stats, ncs_badhits);
474 1.77 ad /*
475 1.77 ad * Last component and we are renaming or
476 1.77 ad * deleting, the cache entry is invalid,
477 1.77 ad * or otherwise don't want cache entry to
478 1.77 ad * exist.
479 1.77 ad */
480 1.77 ad cache_invalidate(ncp);
481 1.90 dholland /* found nothing */
482 1.100 dennis ret_value = 0;
483 1.20 jdolecek }
484 1.100 dennis mutex_exit(&ncp->nc_lock);
485 1.100 dennis mutex_exit(&cpup->cpu_lock);
486 1.100 dennis return ret_value;
487 1.20 jdolecek }
488 1.20 jdolecek
489 1.20 jdolecek vp = ncp->nc_vp;
490 1.92 hannken mutex_enter(vp->v_interlock);
491 1.92 hannken mutex_exit(&ncp->nc_lock);
492 1.100 dennis
493 1.100 dennis /*
494 1.100 dennis * Drop per-cpu lock across the call to vget(), take it
495 1.100 dennis * again for the sake of the stats update.
496 1.100 dennis */
497 1.100 dennis mutex_exit(&cpup->cpu_lock);
498 1.92 hannken error = vget(vp, LK_NOWAIT);
499 1.100 dennis mutex_enter(&cpup->cpu_lock);
500 1.92 hannken if (error) {
501 1.92 hannken KASSERT(error == EBUSY);
502 1.92 hannken /*
503 1.92 hannken * This vnode is being cleaned out.
504 1.92 hannken * XXX badhits?
505 1.92 hannken */
506 1.92 hannken COUNT(cpup->cpu_stats, ncs_falsehits);
507 1.92 hannken /* found nothing */
508 1.100 dennis ret_value = 0;
509 1.100 dennis } else {
510 1.100 dennis COUNT(cpup->cpu_stats, ncs_goodhits);
511 1.100 dennis /* found it */
512 1.100 dennis *vn_ret = vp;
513 1.100 dennis ret_value = 1;
514 1.77 ad }
515 1.98 joerg mutex_exit(&cpup->cpu_lock);
516 1.90 dholland
517 1.100 dennis return ret_value;
518 1.1 cgd }
519 1.1 cgd
520 1.100 dennis
521 1.100 dennis /*
522 1.100 dennis * Cut-'n-pasted version of the above without the nameiop argument.
523 1.100 dennis */
524 1.61 yamt int
525 1.91 dholland cache_lookup_raw(struct vnode *dvp, const char *name, size_t namelen,
526 1.91 dholland uint32_t cnflags,
527 1.90 dholland int *iswht_ret, struct vnode **vn_ret)
528 1.61 yamt {
529 1.61 yamt struct namecache *ncp;
530 1.61 yamt struct vnode *vp;
531 1.77 ad struct nchcpu *cpup;
532 1.100 dennis int error, ret_value;
533 1.61 yamt
534 1.90 dholland /* Establish default results. */
535 1.90 dholland if (iswht_ret != NULL) {
536 1.90 dholland *iswht_ret = 0;
537 1.90 dholland }
538 1.90 dholland *vn_ret = NULL;
539 1.90 dholland
540 1.77 ad if (__predict_false(!doingcache)) {
541 1.90 dholland /* found nothing */
542 1.90 dholland return 0;
543 1.61 yamt }
544 1.61 yamt
545 1.77 ad cpup = curcpu()->ci_data.cpu_nch;
546 1.100 dennis mutex_enter(&cpup->cpu_lock);
547 1.91 dholland if (__predict_false(namelen > NCHNAMLEN)) {
548 1.77 ad COUNT(cpup->cpu_stats, ncs_long);
549 1.77 ad mutex_exit(&cpup->cpu_lock);
550 1.90 dholland /* found nothing */
551 1.90 dholland return 0;
552 1.61 yamt }
553 1.91 dholland ncp = cache_lookup_entry(dvp, name, namelen);
554 1.77 ad if (__predict_false(ncp == NULL)) {
555 1.77 ad COUNT(cpup->cpu_stats, ncs_miss);
556 1.77 ad mutex_exit(&cpup->cpu_lock);
557 1.90 dholland /* found nothing */
558 1.90 dholland return 0;
559 1.61 yamt }
560 1.61 yamt vp = ncp->nc_vp;
561 1.61 yamt if (vp == NULL) {
562 1.61 yamt /*
563 1.61 yamt * Restore the ISWHITEOUT flag saved earlier.
564 1.61 yamt */
565 1.90 dholland if (iswht_ret != NULL) {
566 1.90 dholland KASSERT((ncp->nc_flags & ~ISWHITEOUT) == 0);
567 1.90 dholland /*cnp->cn_flags |= ncp->nc_flags;*/
568 1.90 dholland *iswht_ret = (ncp->nc_flags & ISWHITEOUT) != 0;
569 1.90 dholland }
570 1.77 ad COUNT(cpup->cpu_stats, ncs_neghits);
571 1.100 dennis mutex_exit(&ncp->nc_lock);
572 1.98 joerg mutex_exit(&cpup->cpu_lock);
573 1.90 dholland /* found negative entry; vn is already null from above */
574 1.90 dholland return 1;
575 1.61 yamt }
576 1.92 hannken mutex_enter(vp->v_interlock);
577 1.92 hannken mutex_exit(&ncp->nc_lock);
578 1.100 dennis
579 1.100 dennis /*
580 1.100 dennis * Drop per-cpu lock across the call to vget(), take it
581 1.100 dennis * again for the sake of the stats update.
582 1.100 dennis */
583 1.100 dennis mutex_exit(&cpup->cpu_lock);
584 1.92 hannken error = vget(vp, LK_NOWAIT);
585 1.100 dennis mutex_enter(&cpup->cpu_lock);
586 1.92 hannken if (error) {
587 1.92 hannken KASSERT(error == EBUSY);
588 1.92 hannken /*
589 1.92 hannken * This vnode is being cleaned out.
590 1.92 hannken * XXX badhits?
591 1.92 hannken */
592 1.92 hannken COUNT(cpup->cpu_stats, ncs_falsehits);
593 1.92 hannken /* found nothing */
594 1.100 dennis ret_value = 0;
595 1.100 dennis } else {
596 1.100 dennis COUNT(cpup->cpu_stats, ncs_goodhits); /* XXX can be "badhits" */
597 1.100 dennis /* found it */
598 1.100 dennis *vn_ret = vp;
599 1.100 dennis ret_value = 1;
600 1.61 yamt }
601 1.99 joerg mutex_exit(&cpup->cpu_lock);
602 1.90 dholland
603 1.100 dennis return ret_value;
604 1.61 yamt }
605 1.61 yamt
606 1.1 cgd /*
607 1.19 sommerfe * Scan cache looking for name of directory entry pointing at vp.
608 1.19 sommerfe *
609 1.86 hannken * If the lookup succeeds the vnode is referenced and stored in dvpp.
610 1.19 sommerfe *
611 1.19 sommerfe * If bufp is non-NULL, also place the name in the buffer which starts
612 1.19 sommerfe * at bufp, immediately before *bpp, and move bpp backwards to point
613 1.19 sommerfe * at the start of it. (Yes, this is a little baroque, but it's done
614 1.19 sommerfe * this way to cater to the whims of getcwd).
615 1.19 sommerfe *
616 1.19 sommerfe * Returns 0 on success, -1 on cache miss, positive errno on failure.
617 1.19 sommerfe */
618 1.19 sommerfe int
619 1.34 enami cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
620 1.19 sommerfe {
621 1.19 sommerfe struct namecache *ncp;
622 1.19 sommerfe struct vnode *dvp;
623 1.95 joerg struct nchcpu *cpup;
624 1.19 sommerfe struct ncvhashhead *nvcpp;
625 1.34 enami char *bp;
626 1.86 hannken int error, nlen;
627 1.34 enami
628 1.19 sommerfe if (!doingcache)
629 1.19 sommerfe goto out;
630 1.19 sommerfe
631 1.30 chs nvcpp = &ncvhashtbl[NCVHASH(vp)];
632 1.95 joerg cpup = curcpu()->ci_data.cpu_nch;
633 1.19 sommerfe
634 1.73 ad mutex_enter(namecache_lock);
635 1.27 chs LIST_FOREACH(ncp, nvcpp, nc_vhash) {
636 1.73 ad mutex_enter(&ncp->nc_lock);
637 1.34 enami if (ncp->nc_vp == vp &&
638 1.34 enami (dvp = ncp->nc_dvp) != NULL &&
639 1.47 yamt dvp != vp) { /* avoid pesky . entries.. */
640 1.34 enami
641 1.19 sommerfe #ifdef DIAGNOSTIC
642 1.34 enami if (ncp->nc_nlen == 1 &&
643 1.34 enami ncp->nc_name[0] == '.')
644 1.19 sommerfe panic("cache_revlookup: found entry for .");
645 1.19 sommerfe
646 1.34 enami if (ncp->nc_nlen == 2 &&
647 1.34 enami ncp->nc_name[0] == '.' &&
648 1.34 enami ncp->nc_name[1] == '.')
649 1.19 sommerfe panic("cache_revlookup: found entry for ..");
650 1.19 sommerfe #endif
651 1.95 joerg mutex_enter(&cpup->cpu_lock);
652 1.95 joerg COUNT(cpup->cpu_stats, ncs_revhits);
653 1.95 joerg mutex_exit(&cpup->cpu_lock);
654 1.86 hannken nlen = ncp->nc_nlen;
655 1.19 sommerfe
656 1.19 sommerfe if (bufp) {
657 1.19 sommerfe bp = *bpp;
658 1.86 hannken bp -= nlen;
659 1.19 sommerfe if (bp <= bufp) {
660 1.34 enami *dvpp = NULL;
661 1.73 ad mutex_exit(&ncp->nc_lock);
662 1.73 ad mutex_exit(namecache_lock);
663 1.34 enami return (ERANGE);
664 1.19 sommerfe }
665 1.86 hannken memcpy(bp, ncp->nc_name, nlen);
666 1.19 sommerfe *bpp = bp;
667 1.19 sommerfe }
668 1.34 enami
669 1.92 hannken mutex_enter(dvp->v_interlock);
670 1.92 hannken mutex_exit(&ncp->nc_lock);
671 1.92 hannken mutex_exit(namecache_lock);
672 1.92 hannken error = vget(dvp, LK_NOWAIT);
673 1.92 hannken if (error) {
674 1.92 hannken KASSERT(error == EBUSY);
675 1.92 hannken if (bufp)
676 1.92 hannken (*bpp) += nlen;
677 1.92 hannken *dvpp = NULL;
678 1.92 hannken return -1;
679 1.86 hannken }
680 1.19 sommerfe *dvpp = dvp;
681 1.34 enami return (0);
682 1.19 sommerfe }
683 1.73 ad mutex_exit(&ncp->nc_lock);
684 1.19 sommerfe }
685 1.95 joerg mutex_enter(&cpup->cpu_lock);
686 1.95 joerg COUNT(cpup->cpu_stats, ncs_revmiss);
687 1.95 joerg mutex_exit(&cpup->cpu_lock);
688 1.73 ad mutex_exit(namecache_lock);
689 1.19 sommerfe out:
690 1.34 enami *dvpp = NULL;
691 1.34 enami return (-1);
692 1.19 sommerfe }
693 1.19 sommerfe
694 1.19 sommerfe /*
695 1.1 cgd * Add an entry to the cache
696 1.1 cgd */
697 1.13 christos void
698 1.91 dholland cache_enter(struct vnode *dvp, struct vnode *vp,
699 1.91 dholland const char *name, size_t namelen, uint32_t cnflags)
700 1.1 cgd {
701 1.23 augustss struct namecache *ncp;
702 1.59 yamt struct namecache *oncp;
703 1.23 augustss struct nchashhead *ncpp;
704 1.23 augustss struct ncvhashhead *nvcpp;
705 1.90 dholland nchash_t hash;
706 1.1 cgd
707 1.89 rmind /* First, check whether we can/should add a cache entry. */
708 1.91 dholland if ((cnflags & MAKEENTRY) == 0 ||
709 1.91 dholland __predict_false(namelen > NCHNAMLEN || !doingcache)) {
710 1.1 cgd return;
711 1.89 rmind }
712 1.58 yamt
713 1.73 ad if (numcache > desiredvnodes) {
714 1.73 ad mutex_enter(namecache_lock);
715 1.73 ad cache_ev_forced.ev_count++;
716 1.73 ad cache_reclaim();
717 1.73 ad mutex_exit(namecache_lock);
718 1.39 pk }
719 1.57 pk
720 1.73 ad ncp = pool_cache_get(namecache_cache, PR_WAITOK);
721 1.73 ad mutex_enter(namecache_lock);
722 1.73 ad numcache++;
723 1.73 ad
724 1.59 yamt /*
725 1.59 yamt * Concurrent lookups in the same directory may race for a
726 1.59 yamt * cache entry. if there's a duplicated entry, free it.
727 1.59 yamt */
728 1.91 dholland oncp = cache_lookup_entry(dvp, name, namelen);
729 1.59 yamt if (oncp) {
730 1.73 ad cache_invalidate(oncp);
731 1.73 ad mutex_exit(&oncp->nc_lock);
732 1.59 yamt }
733 1.59 yamt
734 1.34 enami /* Grab the vnode we just found. */
735 1.73 ad mutex_enter(&ncp->nc_lock);
736 1.5 mycroft ncp->nc_vp = vp;
737 1.73 ad ncp->nc_flags = 0;
738 1.73 ad ncp->nc_hittime = 0;
739 1.73 ad ncp->nc_gcqueue = NULL;
740 1.47 yamt if (vp == NULL) {
741 1.11 mycroft /*
742 1.11 mycroft * For negative hits, save the ISWHITEOUT flag so we can
743 1.11 mycroft * restore it later when the cache entry is used again.
744 1.11 mycroft */
745 1.91 dholland ncp->nc_flags = cnflags & ISWHITEOUT;
746 1.11 mycroft }
747 1.89 rmind
748 1.34 enami /* Fill in cache info. */
749 1.5 mycroft ncp->nc_dvp = dvp;
750 1.46 yamt LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
751 1.46 yamt if (vp)
752 1.46 yamt LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
753 1.73 ad else {
754 1.73 ad ncp->nc_vlist.le_prev = NULL;
755 1.73 ad ncp->nc_vlist.le_next = NULL;
756 1.73 ad }
757 1.91 dholland KASSERT(namelen <= NCHNAMLEN);
758 1.91 dholland ncp->nc_nlen = namelen;
759 1.91 dholland memcpy(ncp->nc_name, name, (unsigned)ncp->nc_nlen);
760 1.73 ad TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
761 1.91 dholland hash = cache_hash(name, namelen);
762 1.90 dholland ncpp = &nchashtbl[NCHASH2(hash, dvp)];
763 1.73 ad
764 1.73 ad /*
765 1.73 ad * Flush updates before making visible in table. No need for a
766 1.73 ad * memory barrier on the other side: to see modifications the
767 1.73 ad * list must be followed, meaning a dependent pointer load.
768 1.74 ad * The below is LIST_INSERT_HEAD() inlined, with the memory
769 1.74 ad * barrier included in the correct place.
770 1.73 ad */
771 1.74 ad if ((ncp->nc_hash.le_next = ncpp->lh_first) != NULL)
772 1.74 ad ncpp->lh_first->nc_hash.le_prev = &ncp->nc_hash.le_next;
773 1.74 ad ncp->nc_hash.le_prev = &ncpp->lh_first;
774 1.73 ad membar_producer();
775 1.74 ad ncpp->lh_first = ncp;
776 1.19 sommerfe
777 1.34 enami ncp->nc_vhash.le_prev = NULL;
778 1.34 enami ncp->nc_vhash.le_next = NULL;
779 1.34 enami
780 1.19 sommerfe /*
781 1.19 sommerfe * Create reverse-cache entries (used in getcwd) for directories.
782 1.66 christos * (and in linux procfs exe node)
783 1.19 sommerfe */
784 1.33 enami if (vp != NULL &&
785 1.33 enami vp != dvp &&
786 1.29 fvdl #ifndef NAMECACHE_ENTER_REVERSE
787 1.33 enami vp->v_type == VDIR &&
788 1.29 fvdl #endif
789 1.33 enami (ncp->nc_nlen > 2 ||
790 1.33 enami (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
791 1.33 enami (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
792 1.30 chs nvcpp = &ncvhashtbl[NCVHASH(vp)];
793 1.19 sommerfe LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
794 1.19 sommerfe }
795 1.73 ad mutex_exit(&ncp->nc_lock);
796 1.73 ad mutex_exit(namecache_lock);
797 1.1 cgd }
798 1.1 cgd
799 1.1 cgd /*
800 1.1 cgd * Name cache initialization, from vfs_init() when we are booting
801 1.1 cgd */
802 1.13 christos void
803 1.34 enami nchinit(void)
804 1.1 cgd {
805 1.73 ad int error;
806 1.1 cgd
807 1.89 rmind TAILQ_INIT(&nclruhead);
808 1.73 ad namecache_cache = pool_cache_init(sizeof(struct namecache),
809 1.73 ad coherency_unit, 0, 0, "ncache", NULL, IPL_NONE, cache_ctor,
810 1.73 ad cache_dtor, NULL);
811 1.71 ad KASSERT(namecache_cache != NULL);
812 1.71 ad
813 1.73 ad namecache_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
814 1.73 ad
815 1.76 ad nchashtbl = hashinit(desiredvnodes, HASH_LIST, true, &nchash);
816 1.26 ad ncvhashtbl =
817 1.29 fvdl #ifdef NAMECACHE_ENTER_REVERSE
818 1.76 ad hashinit(desiredvnodes, HASH_LIST, true, &ncvhash);
819 1.29 fvdl #else
820 1.76 ad hashinit(desiredvnodes/8, HASH_LIST, true, &ncvhash);
821 1.29 fvdl #endif
822 1.73 ad
823 1.73 ad error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, cache_thread,
824 1.73 ad NULL, NULL, "cachegc");
825 1.73 ad if (error != 0)
826 1.73 ad panic("nchinit %d", error);
827 1.73 ad
828 1.73 ad evcnt_attach_dynamic(&cache_ev_scan, EVCNT_TYPE_MISC, NULL,
829 1.73 ad "namecache", "entries scanned");
830 1.73 ad evcnt_attach_dynamic(&cache_ev_gc, EVCNT_TYPE_MISC, NULL,
831 1.73 ad "namecache", "entries collected");
832 1.73 ad evcnt_attach_dynamic(&cache_ev_over, EVCNT_TYPE_MISC, NULL,
833 1.73 ad "namecache", "over scan target");
834 1.73 ad evcnt_attach_dynamic(&cache_ev_under, EVCNT_TYPE_MISC, NULL,
835 1.73 ad "namecache", "under scan target");
836 1.73 ad evcnt_attach_dynamic(&cache_ev_forced, EVCNT_TYPE_MISC, NULL,
837 1.73 ad "namecache", "forced reclaims");
838 1.73 ad }
839 1.73 ad
840 1.73 ad static int
841 1.73 ad cache_ctor(void *arg, void *obj, int flag)
842 1.73 ad {
843 1.73 ad struct namecache *ncp;
844 1.73 ad
845 1.73 ad ncp = obj;
846 1.73 ad mutex_init(&ncp->nc_lock, MUTEX_DEFAULT, IPL_NONE);
847 1.73 ad
848 1.73 ad return 0;
849 1.73 ad }
850 1.73 ad
851 1.73 ad static void
852 1.73 ad cache_dtor(void *arg, void *obj)
853 1.73 ad {
854 1.73 ad struct namecache *ncp;
855 1.73 ad
856 1.73 ad ncp = obj;
857 1.73 ad mutex_destroy(&ncp->nc_lock);
858 1.73 ad }
859 1.73 ad
860 1.73 ad /*
861 1.73 ad * Called once for each CPU in the system as attached.
862 1.73 ad */
863 1.73 ad void
864 1.73 ad cache_cpu_init(struct cpu_info *ci)
865 1.73 ad {
866 1.77 ad struct nchcpu *cpup;
867 1.77 ad size_t sz;
868 1.73 ad
869 1.77 ad sz = roundup2(sizeof(*cpup), coherency_unit) + coherency_unit;
870 1.77 ad cpup = kmem_zalloc(sz, KM_SLEEP);
871 1.77 ad cpup = (void *)roundup2((uintptr_t)cpup, coherency_unit);
872 1.77 ad mutex_init(&cpup->cpu_lock, MUTEX_DEFAULT, IPL_NONE);
873 1.77 ad ci->ci_data.cpu_nch = cpup;
874 1.30 chs }
875 1.30 chs
876 1.30 chs /*
877 1.30 chs * Name cache reinitialization, for when the maximum number of vnodes increases.
878 1.30 chs */
879 1.30 chs void
880 1.34 enami nchreinit(void)
881 1.30 chs {
882 1.30 chs struct namecache *ncp;
883 1.30 chs struct nchashhead *oldhash1, *hash1;
884 1.30 chs struct ncvhashhead *oldhash2, *hash2;
885 1.36 thorpej u_long i, oldmask1, oldmask2, mask1, mask2;
886 1.30 chs
887 1.76 ad hash1 = hashinit(desiredvnodes, HASH_LIST, true, &mask1);
888 1.30 chs hash2 =
889 1.30 chs #ifdef NAMECACHE_ENTER_REVERSE
890 1.76 ad hashinit(desiredvnodes, HASH_LIST, true, &mask2);
891 1.30 chs #else
892 1.76 ad hashinit(desiredvnodes/8, HASH_LIST, true, &mask2);
893 1.30 chs #endif
894 1.73 ad mutex_enter(namecache_lock);
895 1.73 ad cache_lock_cpus();
896 1.30 chs oldhash1 = nchashtbl;
897 1.30 chs oldmask1 = nchash;
898 1.30 chs nchashtbl = hash1;
899 1.30 chs nchash = mask1;
900 1.30 chs oldhash2 = ncvhashtbl;
901 1.30 chs oldmask2 = ncvhash;
902 1.30 chs ncvhashtbl = hash2;
903 1.30 chs ncvhash = mask2;
904 1.30 chs for (i = 0; i <= oldmask1; i++) {
905 1.30 chs while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
906 1.30 chs LIST_REMOVE(ncp, nc_hash);
907 1.30 chs ncp->nc_hash.le_prev = NULL;
908 1.30 chs }
909 1.30 chs }
910 1.30 chs for (i = 0; i <= oldmask2; i++) {
911 1.30 chs while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
912 1.30 chs LIST_REMOVE(ncp, nc_vhash);
913 1.30 chs ncp->nc_vhash.le_prev = NULL;
914 1.30 chs }
915 1.30 chs }
916 1.73 ad cache_unlock_cpus();
917 1.73 ad mutex_exit(namecache_lock);
918 1.76 ad hashdone(oldhash1, HASH_LIST, oldmask1);
919 1.76 ad hashdone(oldhash2, HASH_LIST, oldmask2);
920 1.1 cgd }
921 1.1 cgd
922 1.1 cgd /*
923 1.1 cgd * Cache flush, a particular vnode; called when a vnode is renamed to
924 1.1 cgd * hide entries that would now be invalid
925 1.1 cgd */
926 1.13 christos void
927 1.91 dholland cache_purge1(struct vnode *vp, const char *name, size_t namelen, int flags)
928 1.1 cgd {
929 1.46 yamt struct namecache *ncp, *ncnext;
930 1.1 cgd
931 1.73 ad mutex_enter(namecache_lock);
932 1.55 yamt if (flags & PURGE_PARENTS) {
933 1.55 yamt for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
934 1.55 yamt ncp = ncnext) {
935 1.55 yamt ncnext = LIST_NEXT(ncp, nc_vlist);
936 1.73 ad mutex_enter(&ncp->nc_lock);
937 1.73 ad cache_invalidate(ncp);
938 1.73 ad mutex_exit(&ncp->nc_lock);
939 1.73 ad cache_disassociate(ncp);
940 1.55 yamt }
941 1.55 yamt }
942 1.55 yamt if (flags & PURGE_CHILDREN) {
943 1.55 yamt for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
944 1.55 yamt ncp = ncnext) {
945 1.55 yamt ncnext = LIST_NEXT(ncp, nc_dvlist);
946 1.73 ad mutex_enter(&ncp->nc_lock);
947 1.73 ad cache_invalidate(ncp);
948 1.73 ad mutex_exit(&ncp->nc_lock);
949 1.73 ad cache_disassociate(ncp);
950 1.55 yamt }
951 1.46 yamt }
952 1.91 dholland if (name != NULL) {
953 1.91 dholland ncp = cache_lookup_entry(vp, name, namelen);
954 1.55 yamt if (ncp) {
955 1.73 ad cache_invalidate(ncp);
956 1.83 yamt mutex_exit(&ncp->nc_lock);
957 1.73 ad cache_disassociate(ncp);
958 1.55 yamt }
959 1.46 yamt }
960 1.73 ad mutex_exit(namecache_lock);
961 1.1 cgd }
962 1.1 cgd
963 1.1 cgd /*
964 1.1 cgd * Cache flush, a whole filesystem; called when filesys is umounted to
965 1.27 chs * remove entries that would now be invalid.
966 1.1 cgd */
967 1.13 christos void
968 1.34 enami cache_purgevfs(struct mount *mp)
969 1.1 cgd {
970 1.23 augustss struct namecache *ncp, *nxtcp;
971 1.1 cgd
972 1.73 ad mutex_enter(namecache_lock);
973 1.73 ad for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
974 1.73 ad nxtcp = TAILQ_NEXT(ncp, nc_lru);
975 1.73 ad mutex_enter(&ncp->nc_lock);
976 1.73 ad if (ncp->nc_dvp != NULL && ncp->nc_dvp->v_mount == mp) {
977 1.73 ad /* Free the resources we had. */
978 1.73 ad cache_invalidate(ncp);
979 1.73 ad cache_disassociate(ncp);
980 1.73 ad }
981 1.73 ad mutex_exit(&ncp->nc_lock);
982 1.73 ad }
983 1.73 ad cache_reclaim();
984 1.73 ad mutex_exit(namecache_lock);
985 1.73 ad }
986 1.73 ad
987 1.73 ad /*
988 1.73 ad * Scan global list invalidating entries until we meet a preset target.
989 1.73 ad * Prefer to invalidate entries that have not scored a hit within
990 1.73 ad * cache_hottime seconds. We sort the LRU list only for this routine's
991 1.73 ad * benefit.
992 1.73 ad */
993 1.73 ad static void
994 1.73 ad cache_prune(int incache, int target)
995 1.73 ad {
996 1.73 ad struct namecache *ncp, *nxtcp, *sentinel;
997 1.73 ad int items, recent, tryharder;
998 1.73 ad
999 1.73 ad KASSERT(mutex_owned(namecache_lock));
1000 1.73 ad
1001 1.73 ad items = 0;
1002 1.73 ad tryharder = 0;
1003 1.73 ad recent = hardclock_ticks - hz * cache_hottime;
1004 1.73 ad sentinel = NULL;
1005 1.27 chs for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
1006 1.73 ad if (incache <= target)
1007 1.73 ad break;
1008 1.73 ad items++;
1009 1.27 chs nxtcp = TAILQ_NEXT(ncp, nc_lru);
1010 1.73 ad if (ncp == sentinel) {
1011 1.73 ad /*
1012 1.73 ad * If we looped back on ourself, then ignore
1013 1.73 ad * recent entries and purge whatever we find.
1014 1.73 ad */
1015 1.73 ad tryharder = 1;
1016 1.5 mycroft }
1017 1.93 hannken if (ncp->nc_dvp == NULL)
1018 1.93 hannken continue;
1019 1.81 yamt if (!tryharder && (ncp->nc_hittime - recent) > 0) {
1020 1.73 ad if (sentinel == NULL)
1021 1.73 ad sentinel = ncp;
1022 1.73 ad TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
1023 1.73 ad TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
1024 1.73 ad continue;
1025 1.73 ad }
1026 1.73 ad mutex_enter(&ncp->nc_lock);
1027 1.73 ad if (ncp->nc_dvp != NULL) {
1028 1.73 ad cache_invalidate(ncp);
1029 1.73 ad cache_disassociate(ncp);
1030 1.73 ad incache--;
1031 1.73 ad }
1032 1.73 ad mutex_exit(&ncp->nc_lock);
1033 1.73 ad }
1034 1.73 ad cache_ev_scan.ev_count += items;
1035 1.73 ad }
1036 1.73 ad
1037 1.73 ad /*
1038 1.73 ad * Collect dead cache entries from all CPUs and garbage collect.
1039 1.73 ad */
1040 1.73 ad static void
1041 1.73 ad cache_reclaim(void)
1042 1.73 ad {
1043 1.73 ad struct namecache *ncp, *next;
1044 1.73 ad int items;
1045 1.73 ad
1046 1.73 ad KASSERT(mutex_owned(namecache_lock));
1047 1.73 ad
1048 1.73 ad /*
1049 1.73 ad * If the number of extant entries not awaiting garbage collection
1050 1.73 ad * exceeds the high water mark, then reclaim stale entries until we
1051 1.73 ad * reach our low water mark.
1052 1.73 ad */
1053 1.73 ad items = numcache - cache_gcpend;
1054 1.73 ad if (items > (uint64_t)desiredvnodes * cache_hiwat / 100) {
1055 1.73 ad cache_prune(items, (int)((uint64_t)desiredvnodes *
1056 1.73 ad cache_lowat / 100));
1057 1.73 ad cache_ev_over.ev_count++;
1058 1.73 ad } else
1059 1.73 ad cache_ev_under.ev_count++;
1060 1.73 ad
1061 1.73 ad /*
1062 1.73 ad * Stop forward lookup activity on all CPUs and garbage collect dead
1063 1.73 ad * entries.
1064 1.73 ad */
1065 1.73 ad cache_lock_cpus();
1066 1.73 ad ncp = cache_gcqueue;
1067 1.73 ad cache_gcqueue = NULL;
1068 1.73 ad items = cache_gcpend;
1069 1.73 ad cache_gcpend = 0;
1070 1.73 ad while (ncp != NULL) {
1071 1.73 ad next = ncp->nc_gcqueue;
1072 1.73 ad cache_disassociate(ncp);
1073 1.73 ad KASSERT(ncp->nc_dvp == NULL);
1074 1.73 ad if (ncp->nc_hash.le_prev != NULL) {
1075 1.73 ad LIST_REMOVE(ncp, nc_hash);
1076 1.73 ad ncp->nc_hash.le_prev = NULL;
1077 1.73 ad }
1078 1.73 ad pool_cache_put(namecache_cache, ncp);
1079 1.73 ad ncp = next;
1080 1.73 ad }
1081 1.73 ad cache_unlock_cpus();
1082 1.73 ad numcache -= items;
1083 1.73 ad cache_ev_gc.ev_count += items;
1084 1.73 ad }
1085 1.73 ad
1086 1.73 ad /*
1087 1.73 ad * Cache maintainence thread, awakening once per second to:
1088 1.73 ad *
1089 1.73 ad * => keep number of entries below the high water mark
1090 1.73 ad * => sort pseudo-LRU list
1091 1.73 ad * => garbage collect dead entries
1092 1.73 ad */
1093 1.73 ad static void
1094 1.73 ad cache_thread(void *arg)
1095 1.73 ad {
1096 1.73 ad
1097 1.73 ad mutex_enter(namecache_lock);
1098 1.73 ad for (;;) {
1099 1.73 ad cache_reclaim();
1100 1.73 ad kpause("cachegc", false, hz, namecache_lock);
1101 1.1 cgd }
1102 1.1 cgd }
1103 1.19 sommerfe
1104 1.28 chs #ifdef DDB
1105 1.28 chs void
1106 1.28 chs namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
1107 1.28 chs {
1108 1.28 chs struct vnode *dvp = NULL;
1109 1.28 chs struct namecache *ncp;
1110 1.28 chs
1111 1.28 chs TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
1112 1.73 ad if (ncp->nc_vp == vp && ncp->nc_dvp != NULL) {
1113 1.28 chs (*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
1114 1.28 chs dvp = ncp->nc_dvp;
1115 1.28 chs }
1116 1.28 chs }
1117 1.28 chs if (dvp == NULL) {
1118 1.28 chs (*pr)("name not found\n");
1119 1.28 chs return;
1120 1.28 chs }
1121 1.28 chs vp = dvp;
1122 1.28 chs TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
1123 1.47 yamt if (ncp->nc_vp == vp) {
1124 1.28 chs (*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
1125 1.28 chs }
1126 1.28 chs }
1127 1.28 chs }
1128 1.28 chs #endif
1129 1.95 joerg
1130 1.95 joerg void
1131 1.95 joerg namecache_count_pass2(void)
1132 1.95 joerg {
1133 1.95 joerg struct nchcpu *cpup = curcpu()->ci_data.cpu_nch;
1134 1.95 joerg
1135 1.95 joerg mutex_enter(&cpup->cpu_lock);
1136 1.95 joerg COUNT(cpup->cpu_stats, ncs_pass2);
1137 1.95 joerg mutex_exit(&cpup->cpu_lock);
1138 1.95 joerg }
1139 1.95 joerg
1140 1.95 joerg void
1141 1.95 joerg namecache_count_2passes(void)
1142 1.95 joerg {
1143 1.95 joerg struct nchcpu *cpup = curcpu()->ci_data.cpu_nch;
1144 1.95 joerg
1145 1.95 joerg mutex_enter(&cpup->cpu_lock);
1146 1.95 joerg COUNT(cpup->cpu_stats, ncs_2passes);
1147 1.95 joerg mutex_exit(&cpup->cpu_lock);
1148 1.95 joerg }
1149 1.97 joerg
1150 1.97 joerg static int
1151 1.97 joerg cache_stat_sysctl(SYSCTLFN_ARGS)
1152 1.97 joerg {
1153 1.97 joerg struct nchstats_sysctl stats;
1154 1.97 joerg
1155 1.97 joerg if (oldp == NULL) {
1156 1.97 joerg *oldlenp = sizeof(stats);
1157 1.97 joerg return 0;
1158 1.97 joerg }
1159 1.97 joerg
1160 1.97 joerg if (*oldlenp < sizeof(stats)) {
1161 1.97 joerg *oldlenp = 0;
1162 1.97 joerg return 0;
1163 1.97 joerg }
1164 1.97 joerg
1165 1.97 joerg memset(&stats, 0, sizeof(stats));
1166 1.97 joerg
1167 1.97 joerg sysctl_unlock();
1168 1.97 joerg cache_lock_cpus();
1169 1.97 joerg stats.ncs_goodhits = nchstats.ncs_goodhits;
1170 1.97 joerg stats.ncs_neghits = nchstats.ncs_neghits;
1171 1.97 joerg stats.ncs_badhits = nchstats.ncs_badhits;
1172 1.97 joerg stats.ncs_falsehits = nchstats.ncs_falsehits;
1173 1.97 joerg stats.ncs_miss = nchstats.ncs_miss;
1174 1.97 joerg stats.ncs_long = nchstats.ncs_long;
1175 1.97 joerg stats.ncs_pass2 = nchstats.ncs_pass2;
1176 1.97 joerg stats.ncs_2passes = nchstats.ncs_2passes;
1177 1.97 joerg stats.ncs_revhits = nchstats.ncs_revhits;
1178 1.97 joerg stats.ncs_revmiss = nchstats.ncs_revmiss;
1179 1.97 joerg cache_unlock_cpus();
1180 1.97 joerg sysctl_relock();
1181 1.97 joerg
1182 1.97 joerg *oldlenp = sizeof(stats);
1183 1.97 joerg return sysctl_copyout(l, &stats, oldp, sizeof(stats));
1184 1.97 joerg }
1185 1.97 joerg
1186 1.97 joerg SYSCTL_SETUP(sysctl_cache_stat_setup, "vfs.namecache_stats subtree setup")
1187 1.97 joerg {
1188 1.97 joerg sysctl_createv(clog, 0, NULL, NULL,
1189 1.97 joerg CTLFLAG_PERMANENT,
1190 1.97 joerg CTLTYPE_STRUCT, "namecache_stats",
1191 1.97 joerg SYSCTL_DESCR("namecache statistics"),
1192 1.97 joerg cache_stat_sysctl, 0, NULL, 0,
1193 1.97 joerg CTL_VFS, CTL_CREATE, CTL_EOL);
1194 1.97 joerg }
1195