This source file includes following definitions.
- cache_lookup
- cache_revlookup
- cache_enter
- nchinit
- cache_purge
- cache_purgevfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/time.h>
38 #include <sys/mount.h>
39 #include <sys/vnode.h>
40 #include <sys/namei.h>
41 #include <sys/errno.h>
42 #include <sys/malloc.h>
43 #include <sys/pool.h>
44 #include <sys/hash.h>
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 LIST_HEAD(nchashhead, namecache) *nchashtbl;
72 u_long nchash;
73 long numcache;
74 TAILQ_HEAD(, namecache) nclruhead;
75 struct nchstats nchstats;
76
77 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
78 u_long ncvhash;
79
80 #define NCHASH(dvp, cnp) \
81 hash32_buf(&(dvp)->v_id, sizeof((dvp)->v_id), (cnp)->cn_hash) & nchash
82
83 #define NCVHASH(vp) (vp)->v_id & ncvhash
84
85 int doingcache = 1;
86
87 struct pool nch_pool;
88
89 u_long nextvnodeid;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 int
107 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
108 {
109 struct namecache *ncp;
110 struct nchashhead *ncpp;
111 struct vnode *vp;
112 struct proc *p = curproc;
113 u_long vpid;
114 int error;
115
116 *vpp = NULL;
117
118 if (!doingcache) {
119 cnp->cn_flags &= ~MAKEENTRY;
120 return (-1);
121 }
122 if (cnp->cn_namelen > NCHNAMLEN) {
123 nchstats.ncs_long++;
124 cnp->cn_flags &= ~MAKEENTRY;
125 return (-1);
126 }
127
128 ncpp = &nchashtbl[NCHASH(dvp, cnp)];
129 LIST_FOREACH(ncp, ncpp, nc_hash) {
130 if (ncp->nc_dvp == dvp &&
131 ncp->nc_dvpid == dvp->v_id &&
132 ncp->nc_nlen == cnp->cn_namelen &&
133 !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
134 break;
135 }
136 if (ncp == NULL) {
137 nchstats.ncs_miss++;
138 return (-1);
139 }
140 if ((cnp->cn_flags & MAKEENTRY) == 0) {
141 nchstats.ncs_badhits++;
142 goto remove;
143 } else if (ncp->nc_vp == NULL) {
144 if (cnp->cn_nameiop != CREATE ||
145 (cnp->cn_flags & ISLASTCN) == 0) {
146 nchstats.ncs_neghits++;
147
148
149
150
151 if (TAILQ_NEXT(ncp, nc_lru) != NULL) {
152 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
153 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
154 }
155 return (ENOENT);
156 } else {
157 nchstats.ncs_badhits++;
158 goto remove;
159 }
160 } else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
161 nchstats.ncs_falsehits++;
162 goto remove;
163 }
164
165 vp = ncp->nc_vp;
166 vpid = vp->v_id;
167 if (vp == dvp) {
168 VREF(dvp);
169 error = 0;
170 } else if (cnp->cn_flags & ISDOTDOT) {
171 VOP_UNLOCK(dvp, 0, p);
172 cnp->cn_flags |= PDIRUNLOCK;
173 error = vget(vp, LK_EXCLUSIVE, p);
174
175
176
177
178 if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
179 if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)) != 0) {
180 vput(vp);
181 return (error);
182 }
183 cnp->cn_flags &= ~PDIRUNLOCK;
184 }
185 } else {
186 error = vget(vp, LK_EXCLUSIVE, p);
187
188
189
190
191 if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
192 VOP_UNLOCK(dvp, 0, p);
193 cnp->cn_flags |= PDIRUNLOCK;
194 }
195 }
196
197
198
199
200
201 if (error || vpid != vp->v_id) {
202 if (!error) {
203 vput(vp);
204 nchstats.ncs_falsehits++;
205 } else
206 nchstats.ncs_badhits++;
207
208
209
210
211
212 if (vp == dvp || error ||
213 (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
214 if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)) != 0)
215 return (error);
216 cnp->cn_flags &= ~PDIRUNLOCK;
217 }
218 return (-1);
219 }
220
221 nchstats.ncs_goodhits++;
222
223
224
225 if (TAILQ_NEXT(ncp, nc_lru) != NULL) {
226 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
227 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
228 }
229 *vpp = vp;
230 return (0);
231
232 remove:
233
234
235
236
237
238 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
239 LIST_REMOVE(ncp, nc_hash);
240 ncp->nc_hash.le_prev = NULL;
241
242 if (ncp->nc_vhash.le_prev != NULL) {
243 LIST_REMOVE(ncp, nc_vhash);
244 ncp->nc_vhash.le_prev = NULL;
245 }
246
247 pool_put(&nch_pool, ncp);
248 numcache--;
249 return (-1);
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 int
268 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
269 {
270 struct namecache *ncp;
271 struct vnode *dvp;
272 struct ncvhashhead *nvcpp;
273 char *bp;
274
275 if (!doingcache)
276 goto out;
277
278 nvcpp = &ncvhashtbl[NCVHASH(vp)];
279
280 LIST_FOREACH(ncp, nvcpp, nc_vhash) {
281 if (ncp->nc_vp == vp &&
282 ncp->nc_vpid == vp->v_id &&
283 (dvp = ncp->nc_dvp) != NULL &&
284
285 dvp != vp && ncp->nc_dvpid == dvp->v_id) {
286
287 #ifdef DIAGNOSTIC
288 if (ncp->nc_nlen == 1 &&
289 ncp->nc_name[0] == '.')
290 panic("cache_revlookup: found entry for .");
291
292 if (ncp->nc_nlen == 2 &&
293 ncp->nc_name[0] == '.' &&
294 ncp->nc_name[1] == '.')
295 panic("cache_revlookup: found entry for ..");
296 #endif
297 nchstats.ncs_revhits++;
298
299 if (bufp != NULL) {
300 bp = *bpp;
301 bp -= ncp->nc_nlen;
302 if (bp <= bufp) {
303 *dvpp = NULL;
304 return (ERANGE);
305 }
306 memcpy(bp, ncp->nc_name, ncp->nc_nlen);
307 *bpp = bp;
308 }
309
310 *dvpp = dvp;
311
312
313
314
315
316
317
318
319
320
321 return (0);
322 }
323 }
324 nchstats.ncs_revmiss++;
325
326 out:
327 *dvpp = NULL;
328 return (-1);
329 }
330
331
332
333
334 void
335 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
336 {
337 struct namecache *ncp;
338 struct nchashhead *ncpp;
339 struct ncvhashhead *nvcpp;
340
341 if (!doingcache || cnp->cn_namelen > NCHNAMLEN)
342 return;
343
344
345
346
347 if (numcache < desiredvnodes) {
348 ncp = pool_get(&nch_pool, PR_WAITOK);
349 bzero((char *)ncp, sizeof *ncp);
350 numcache++;
351 } else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
352 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
353 if (ncp->nc_hash.le_prev != NULL) {
354 LIST_REMOVE(ncp, nc_hash);
355 ncp->nc_hash.le_prev = NULL;
356 }
357 if (ncp->nc_vhash.le_prev != NULL) {
358 LIST_REMOVE(ncp, nc_vhash);
359 ncp->nc_vhash.le_prev = NULL;
360 }
361 } else
362 return;
363
364 ncp->nc_vp = vp;
365 if (vp)
366 ncp->nc_vpid = vp->v_id;
367
368 ncp->nc_dvp = dvp;
369 ncp->nc_dvpid = dvp->v_id;
370 ncp->nc_nlen = cnp->cn_namelen;
371 bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
372 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
373 ncpp = &nchashtbl[NCHASH(dvp, cnp)];
374 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
375
376
377
378
379
380
381 ncp->nc_vhash.le_prev = NULL;
382 ncp->nc_vhash.le_next = NULL;
383
384 if (vp && vp != dvp && vp->v_type == VDIR &&
385 (ncp->nc_nlen > 2 ||
386 (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
387 (ncp->nc_nlen > 0 && ncp->nc_name[0] != '.'))) {
388 nvcpp = &ncvhashtbl[NCVHASH(vp)];
389 LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
390 }
391 }
392
393
394
395
396 void
397 nchinit(void)
398 {
399
400 TAILQ_INIT(&nclruhead);
401 nchashtbl = hashinit(desiredvnodes, M_CACHE, M_WAITOK, &nchash);
402 ncvhashtbl = hashinit(desiredvnodes/8, M_CACHE, M_WAITOK, &ncvhash);
403 pool_init(&nch_pool, sizeof(struct namecache), 0, 0, 0, "nchpl",
404 &pool_allocator_nointr);
405 }
406
407
408
409
410
411 void
412 cache_purge(struct vnode *vp)
413 {
414 struct namecache *ncp;
415 struct nchashhead *ncpp;
416
417 vp->v_id = ++nextvnodeid;
418 if (nextvnodeid != 0)
419 return;
420 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
421 LIST_FOREACH(ncp, ncpp, nc_hash) {
422 ncp->nc_vpid = 0;
423 ncp->nc_dvpid = 0;
424 }
425 }
426 vp->v_id = ++nextvnodeid;
427 }
428
429
430
431
432
433 void
434 cache_purgevfs(struct mount *mp)
435 {
436 struct namecache *ncp, *nxtcp;
437
438 for (ncp = TAILQ_FIRST(&nclruhead); ncp != TAILQ_END(&nclruhead);
439 ncp = nxtcp) {
440 nxtcp = TAILQ_NEXT(ncp, nc_lru);
441 if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
442 continue;
443
444 ncp->nc_vp = NULL;
445 ncp->nc_dvp = NULL;
446 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
447 if (ncp->nc_hash.le_prev != NULL) {
448 LIST_REMOVE(ncp, nc_hash);
449 ncp->nc_hash.le_prev = NULL;
450 }
451 if (ncp->nc_vhash.le_prev != NULL) {
452 LIST_REMOVE(ncp, nc_vhash);
453 ncp->nc_vhash.le_prev = NULL;
454 }
455 pool_put(&nch_pool, ncp);
456 numcache--;
457 }
458 }