]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cache.c
174b0ecb784f490a79ea9100f814c78d5b5b1bc2
[apple/xnu.git] / bsd / vfs / vfs_cache.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1989, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
34 *
35 * This code is derived from software contributed to Berkeley by
36 * Poul-Henning Kamp of the FreeBSD Project.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 *
67 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
68 */
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/time.h>
72 #include <sys/mount_internal.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/namei.h>
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <sys/kauth.h>
78 #include <sys/user.h>
79
80 /*
81 * Name caching works as follows:
82 *
83 * Names found by directory scans are retained in a cache
84 * for future reference. It is managed LRU, so frequently
85 * used names will hang around. Cache is indexed by hash value
86 * obtained from (vp, name) where vp refers to the directory
87 * containing name.
88 *
89 * If it is a "negative" entry, (i.e. for a name that is known NOT to
90 * exist) the vnode pointer will be NULL.
91 *
92 * Upon reaching the last segment of a path, if the reference
93 * is for DELETE, or NOCACHE is set (rewrite), and the
94 * name is located in the cache, it will be dropped.
95 */
96
97 /*
98 * Structures associated with name cacheing.
99 */
100
101 LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
102 u_long nchashmask;
103 u_long nchash; /* size of hash table - 1 */
104 long numcache; /* number of cache entries allocated */
105 int desiredNodes;
106 int desiredNegNodes;
107 TAILQ_HEAD(, namecache) nchead; /* chain of all name cache entries */
108 TAILQ_HEAD(, namecache) neghead; /* chain of only negative cache entries */
109 struct nchstats nchstats; /* cache effectiveness statistics */
110
111 /* vars for name cache list lock */
112 lck_grp_t * namecache_lck_grp;
113 lck_grp_attr_t * namecache_lck_grp_attr;
114 lck_attr_t * namecache_lck_attr;
115 lck_mtx_t * namecache_mtx_lock;
116
117 static vnode_t cache_lookup_locked(vnode_t dvp, struct componentname *cnp);
118 static int remove_name_locked(const char *);
119 static char *add_name_locked(const char *, size_t, u_int, u_int);
120 static void init_string_table(void);
121 static void cache_delete(struct namecache *, int);
122 static void dump_string_table(void);
123
124 static void init_crc32(void);
125 static unsigned int crc32tab[256];
126
127
128 #define NCHHASH(dvp, hash_val) \
129 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
130
131
132
133 //
134 // This function builds the path to a filename in "buff". The
135 // length of the buffer *INCLUDING* the trailing zero byte is
136 // returned in outlen. NOTE: the length includes the trailing
137 // zero byte and thus the length is one greater than what strlen
138 // would return. This is important and lots of code elsewhere
139 // in the kernel assumes this behavior.
140 //
141 int
142 build_path(vnode_t first_vp, char *buff, int buflen, int *outlen)
143 {
144 vnode_t vp = first_vp;
145 char *end, *str;
146 int len, ret=0, counter=0;
147
148 end = &buff[buflen-1];
149 *end = '\0';
150
151 /*
152 * if this is the root dir of a file system...
153 */
154 if (vp && (vp->v_flag & VROOT) && vp->v_mount) {
155 /*
156 * then if it's the root fs, just put in a '/' and get out of here
157 */
158 if (vp->v_mount->mnt_flag & MNT_ROOTFS) {
159 *--end = '/';
160 goto out;
161 } else {
162 /*
163 * else just use the covered vnode to get the mount path
164 */
165 vp = vp->v_mount->mnt_vnodecovered;
166 }
167 }
168 name_cache_lock();
169
170 while (vp && vp->v_parent != vp) {
171 /*
172 * the maximum depth of a file system hierarchy is MAXPATHLEN/2
173 * (with single-char names separated by slashes). we panic if
174 * we've ever looped more than that.
175 */
176 if (counter++ > MAXPATHLEN/2) {
177 panic("build_path: vnode parent chain is too long! vp 0x%x\n", vp);
178 }
179 str = vp->v_name;
180
181 if (str == NULL) {
182 if (vp->v_parent != NULL) {
183 ret = EINVAL;
184 }
185 break;
186 }
187 len = strlen(str);
188
189 /*
190 * check that there's enough space (make sure to include space for the '/')
191 */
192 if ((end - buff) < (len + 1)) {
193 ret = ENOSPC;
194 break;
195 }
196 /*
197 * copy it backwards
198 */
199 str += len;
200
201 for (; len > 0; len--) {
202 *--end = *--str;
203 }
204 /*
205 * put in the path separator
206 */
207 *--end = '/';
208
209 /*
210 * walk up the chain (as long as we're not the root)
211 */
212 if (vp == first_vp && (vp->v_flag & VROOT)) {
213 if (vp->v_mount && vp->v_mount->mnt_vnodecovered) {
214 vp = vp->v_mount->mnt_vnodecovered->v_parent;
215 } else {
216 vp = NULLVP;
217 }
218 } else {
219 vp = vp->v_parent;
220 }
221 /*
222 * check if we're crossing a mount point and
223 * switch the vp if we are.
224 */
225 if (vp && (vp->v_flag & VROOT) && vp->v_mount) {
226 vp = vp->v_mount->mnt_vnodecovered;
227 }
228 }
229 name_cache_unlock();
230 out:
231 /*
232 * slide it down to the beginning of the buffer
233 */
234 memmove(buff, end, &buff[buflen] - end);
235
236 *outlen = &buff[buflen] - end; // length includes the trailing zero byte
237
238 return ret;
239 }
240
241
242 /*
243 * return NULLVP if vp's parent doesn't
244 * exist, or we can't get a valid iocount
245 * else return the parent of vp
246 */
247 vnode_t
248 vnode_getparent(vnode_t vp)
249 {
250 vnode_t pvp = NULLVP;
251 int pvid;
252
253 name_cache_lock();
254 /*
255 * v_parent is stable behind the name_cache lock
256 * however, the only thing we can really guarantee
257 * is that we've grabbed a valid iocount on the
258 * parent of 'vp' at the time we took the name_cache lock...
259 * once we drop the lock, vp could get re-parented
260 */
261 if ( (pvp = vp->v_parent) != NULLVP ) {
262 pvid = pvp->v_id;
263
264 name_cache_unlock();
265
266 if (vnode_getwithvid(pvp, pvid) != 0)
267 pvp = NULL;
268 } else
269 name_cache_unlock();
270
271 return (pvp);
272 }
273
274 char *
275 vnode_getname(vnode_t vp)
276 {
277 char *name = NULL;
278
279 name_cache_lock();
280
281 if (vp->v_name)
282 name = add_name_locked(vp->v_name, strlen(vp->v_name), 0, 0);
283 name_cache_unlock();
284
285 return (name);
286 }
287
288 void
289 vnode_putname(char *name)
290 {
291 name_cache_lock();
292
293 remove_name_locked(name);
294
295 name_cache_unlock();
296 }
297
298
299 /*
300 * if VNODE_UPDATE_PARENT, and we can take
301 * a reference on dvp, then update vp with
302 * it's new parent... if vp already has a parent,
303 * then drop the reference vp held on it
304 *
305 * if VNODE_UPDATE_NAME,
306 * then drop string ref on v_name if it exists, and if name is non-NULL
307 * then pick up a string reference on name and record it in v_name...
308 * optionally pass in the length and hashval of name if known
309 *
310 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
311 */
312 void
313 vnode_update_identity(vnode_t vp, vnode_t dvp, char *name, int name_len, int name_hashval, int flags)
314 {
315 struct namecache *ncp;
316 vnode_t old_parentvp = NULLVP;
317
318
319 if (flags & VNODE_UPDATE_PARENT) {
320 if (dvp && vnode_ref(dvp) != 0)
321 dvp = NULLVP;
322 } else
323 dvp = NULLVP;
324 name_cache_lock();
325
326 if ( (flags & VNODE_UPDATE_NAME) && (name != vp->v_name) ) {
327 if (vp->v_name != NULL) {
328 remove_name_locked(vp->v_name);
329 vp->v_name = NULL;
330 }
331 if (name && *name) {
332 if (name_len == 0)
333 name_len = strlen(name);
334 vp->v_name = add_name_locked(name, name_len, name_hashval, 0);
335 }
336 }
337 if (flags & VNODE_UPDATE_PARENT) {
338 if (dvp != vp && dvp != vp->v_parent) {
339 old_parentvp = vp->v_parent;
340 vp->v_parent = dvp;
341 dvp = NULLVP;
342
343 if (old_parentvp)
344 flags |= VNODE_UPDATE_CACHE;
345 }
346 }
347 if (flags & VNODE_UPDATE_CACHE) {
348 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
349 cache_delete(ncp, 1);
350 }
351 name_cache_unlock();
352
353 if (dvp != NULLVP)
354 vnode_rele(dvp);
355
356 if (old_parentvp) {
357 struct uthread *ut;
358
359 ut = get_bsdthread_info(current_thread());
360
361 /*
362 * indicated to vnode_rele that it shouldn't do a
363 * vnode_reclaim at this time... instead it will
364 * chain the vnode to the uu_vreclaims list...
365 * we'll be responsible for calling vnode_reclaim
366 * on each of the vnodes in this list...
367 */
368 ut->uu_defer_reclaims = 1;
369 ut->uu_vreclaims = NULLVP;
370
371 while ( (vp = old_parentvp) != NULLVP ) {
372
373 vnode_lock(vp);
374
375 vnode_rele_internal(vp, 0, 0, 1);
376
377 /*
378 * check to see if the vnode is now in the state
379 * that would have triggered a vnode_reclaim in vnode_rele
380 * if it is, we save it's parent pointer and then NULL
381 * out the v_parent field... we'll drop the reference
382 * that was held on the next iteration of this loop...
383 * this short circuits a potential deep recursion if we
384 * have a long chain of parents in this state...
385 * we'll sit in this loop until we run into
386 * a parent in this chain that is not in this state
387 *
388 * make our check and the node_rele atomic
389 * with respect to the current vnode we're working on
390 * by holding the vnode lock
391 * if vnode_rele deferred the vnode_reclaim and has put
392 * this vnode on the list to be reaped by us, than
393 * it has left this vnode with an iocount == 1
394 */
395 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
396 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
397 /*
398 * vnode_rele wanted to do a vnode_reclaim on this vnode
399 * it should be sitting on the head of the uu_vreclaims chain
400 * pull the parent pointer now so that when we do the
401 * vnode_reclaim for each of the vnodes in the uu_vreclaims
402 * list, we won't recurse back through here
403 */
404 name_cache_lock();
405 old_parentvp = vp->v_parent;
406 vp->v_parent = NULLVP;
407 name_cache_unlock();
408 } else {
409 /*
410 * we're done... we ran into a vnode that isn't
411 * being terminated
412 */
413 old_parentvp = NULLVP;
414 }
415 vnode_unlock(vp);
416 }
417 ut->uu_defer_reclaims = 0;
418
419 while ( (vp = ut->uu_vreclaims) != NULLVP) {
420 ut->uu_vreclaims = vp->v_defer_reclaimlist;
421
422 /*
423 * vnode_put will drive the vnode_reclaim if
424 * we are still the only reference on this vnode
425 */
426 vnode_put(vp);
427 }
428 }
429 }
430
431
432 /*
433 * Mark a vnode as having multiple hard links. HFS makes use of this
434 * because it keeps track of each link separately, and wants to know
435 * which link was actually used.
436 *
437 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
438 * so that HFS can post-process the lookup. Also, volfs will call
439 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
440 */
441 void vnode_set_hard_link(vnode_t vp)
442 {
443 vnode_lock(vp);
444
445 /*
446 * In theory, we're changing the vnode's identity as far as the
447 * name cache is concerned, so we ought to grab the name cache lock
448 * here. However, there is already a race, and grabbing the name
449 * cache lock only makes the race window slightly smaller.
450 *
451 * The race happens because the vnode already exists in the name
452 * cache, and could be found by one thread before another thread
453 * can set the hard link flag.
454 */
455
456 vp->v_flag |= VISHARDLINK;
457
458 vnode_unlock(vp);
459 }
460
461
462 void vnode_uncache_credentials(vnode_t vp)
463 {
464 kauth_cred_t ucred = NULL;
465
466 if (vp->v_cred) {
467 vnode_lock(vp);
468
469 ucred = vp->v_cred;
470 vp->v_cred = NULL;
471
472 vnode_unlock(vp);
473
474 if (ucred)
475 kauth_cred_rele(ucred);
476 }
477 }
478
479
480 void vnode_cache_credentials(vnode_t vp, vfs_context_t context)
481 {
482 kauth_cred_t ucred;
483 kauth_cred_t tcred = NOCRED;
484 struct timeval tv;
485
486 ucred = vfs_context_ucred(context);
487
488 if (vp->v_cred != ucred || (vp->v_mount->mnt_kern_flag & MNTK_AUTH_OPAQUE)) {
489 vnode_lock(vp);
490
491 microuptime(&tv);
492 vp->v_cred_timestamp = tv.tv_sec;
493
494 if (vp->v_cred != ucred) {
495 kauth_cred_ref(ucred);
496
497 tcred = vp->v_cred;
498 vp->v_cred = ucred;
499 }
500 vnode_unlock(vp);
501
502 if (tcred)
503 kauth_cred_rele(tcred);
504 }
505 }
506
507 /* reverse_lookup - lookup by walking back up the parent chain while leveraging
508 * use of the name cache lock in order to protect our starting vnode.
509 * NOTE - assumes you already have search access to starting point.
510 * returns 0 when we have reached the root, current working dir, or chroot root
511 *
512 */
513 int
514 reverse_lookup(vnode_t start_vp, vnode_t *lookup_vpp, struct filedesc *fdp, vfs_context_t context, int *dp_authorized)
515 {
516 int vid, done = 0;
517 int auth_opaque = 0;
518 vnode_t dp = start_vp;
519 vnode_t vp = NULLVP;
520 kauth_cred_t ucred;
521 struct timeval tv;
522
523 ucred = vfs_context_ucred(context);
524 *lookup_vpp = start_vp;
525
526 name_cache_lock();
527
528 if ( dp->v_mount && (dp->v_mount->mnt_kern_flag & MNTK_AUTH_OPAQUE) ) {
529 auth_opaque = 1;
530 microuptime(&tv);
531 }
532 for (;;) {
533 *dp_authorized = 0;
534
535 if (auth_opaque && ((tv.tv_sec - dp->v_cred_timestamp) > VCRED_EXPIRED))
536 break;
537 if (dp->v_cred != ucred)
538 break;
539 /*
540 * indicate that we're allowed to traverse this directory...
541 * even if we bail for some reason, this information is valid and is used
542 * to avoid doing a vnode_authorize
543 */
544 *dp_authorized = 1;
545
546 if ((dp->v_flag & VROOT) != 0 || /* Hit "/" */
547 (dp == fdp->fd_cdir) || /* Hit process's working directory */
548 (dp == fdp->fd_rdir)) { /* Hit process chroot()-ed root */
549 done = 1;
550 break;
551 }
552
553 if ( (vp = dp->v_parent) == NULLVP)
554 break;
555
556 dp = vp;
557 *lookup_vpp = dp;
558 } /* for (;;) */
559
560 vid = dp->v_id;
561
562 name_cache_unlock();
563
564 if (done == 0 && dp != start_vp) {
565 if (vnode_getwithvid(dp, vid) != 0) {
566 *lookup_vpp = start_vp;
567 }
568 }
569
570 return((done == 1) ? 0 : -1);
571 }
572
573 int
574 cache_lookup_path(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, vfs_context_t context, int *trailing_slash, int *dp_authorized)
575 {
576 char *cp; /* pointer into pathname argument */
577 int vid, vvid;
578 int auth_opaque = 0;
579 vnode_t vp = NULLVP;
580 vnode_t tdp = NULLVP;
581 kauth_cred_t ucred;
582 struct timeval tv;
583 unsigned int hash;
584
585 ucred = vfs_context_ucred(context);
586 *trailing_slash = 0;
587
588 name_cache_lock();
589
590
591 if ( dp->v_mount && (dp->v_mount->mnt_kern_flag & MNTK_AUTH_OPAQUE) ) {
592 auth_opaque = 1;
593 microuptime(&tv);
594 }
595 for (;;) {
596 /*
597 * Search a directory.
598 *
599 * The cn_hash value is for use by cache_lookup
600 * The last component of the filename is left accessible via
601 * cnp->cn_nameptr for callers that need the name.
602 */
603 hash = 0;
604 cp = cnp->cn_nameptr;
605
606 while (*cp && (*cp != '/')) {
607 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
608 }
609 /*
610 * the crc generator can legitimately generate
611 * a 0... however, 0 for us means that we
612 * haven't computed a hash, so use 1 instead
613 */
614 if (hash == 0)
615 hash = 1;
616 cnp->cn_hash = hash;
617 cnp->cn_namelen = cp - cnp->cn_nameptr;
618
619 ndp->ni_pathlen -= cnp->cn_namelen;
620 ndp->ni_next = cp;
621
622 /*
623 * Replace multiple slashes by a single slash and trailing slashes
624 * by a null. This must be done before VNOP_LOOKUP() because some
625 * fs's don't know about trailing slashes. Remember if there were
626 * trailing slashes to handle symlinks, existing non-directories
627 * and non-existing files that won't be directories specially later.
628 */
629 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
630 cp++;
631 ndp->ni_pathlen--;
632
633 if (*cp == '\0') {
634 *trailing_slash = 1;
635 *ndp->ni_next = '\0';
636 }
637 }
638 ndp->ni_next = cp;
639
640 cnp->cn_flags &= ~(MAKEENTRY | ISLASTCN | ISDOTDOT);
641
642 if (*cp == '\0')
643 cnp->cn_flags |= ISLASTCN;
644
645 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
646 cnp->cn_flags |= ISDOTDOT;
647
648 *dp_authorized = 0;
649
650 if (auth_opaque && ((tv.tv_sec - dp->v_cred_timestamp) > VCRED_EXPIRED))
651 break;
652
653 if (dp->v_cred != ucred)
654 break;
655 /*
656 * indicate that we're allowed to traverse this directory...
657 * even if we fail the cache lookup or decide to bail for
658 * some other reason, this information is valid and is used
659 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
660 */
661 *dp_authorized = 1;
662
663 if ( (cnp->cn_flags & (ISLASTCN | ISDOTDOT)) ) {
664 if (cnp->cn_nameiop != LOOKUP)
665 break;
666 if (cnp->cn_flags & (LOCKPARENT | NOCACHE))
667 break;
668 if (cnp->cn_flags & ISDOTDOT) {
669 /*
670 * Quit here only if we can't use
671 * the parent directory pointer or
672 * don't have one. Otherwise, we'll
673 * use it below.
674 */
675 if ((dp->v_flag & VROOT) ||
676 dp->v_parent == NULLVP)
677 break;
678 }
679 }
680
681 /*
682 * "." and ".." aren't supposed to be cached, so check
683 * for them before checking the cache.
684 */
685 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
686 vp = dp;
687 else if (cnp->cn_flags & ISDOTDOT)
688 vp = dp->v_parent;
689 else {
690 if ( (vp = cache_lookup_locked(dp, cnp)) == NULLVP)
691 break;
692 }
693
694 if ( (cnp->cn_flags & ISLASTCN) )
695 break;
696
697 if (vp->v_type != VDIR) {
698 if (vp->v_type != VLNK)
699 vp = NULL;
700 break;
701 }
702 if (vp->v_mountedhere && ((cnp->cn_flags & NOCROSSMOUNT) == 0))
703 break;
704
705 dp = vp;
706 vp = NULLVP;
707
708 cnp->cn_nameptr = ndp->ni_next + 1;
709 ndp->ni_pathlen--;
710 while (*cnp->cn_nameptr == '/') {
711 cnp->cn_nameptr++;
712 ndp->ni_pathlen--;
713 }
714 }
715 if (vp != NULLVP)
716 vvid = vp->v_id;
717 vid = dp->v_id;
718
719 name_cache_unlock();
720
721
722 if ((vp != NULLVP) && (vp->v_type != VLNK) &&
723 ((cnp->cn_flags & (ISLASTCN | LOCKPARENT | WANTPARENT | SAVESTART)) == ISLASTCN)) {
724 /*
725 * if we've got a child and it's the last component, and
726 * the lookup doesn't need to return the parent then we
727 * can skip grabbing an iocount on the parent, since all
728 * we're going to do with it is a vnode_put just before
729 * we return from 'lookup'. If it's a symbolic link,
730 * we need the parent in case the link happens to be
731 * a relative pathname.
732 */
733 tdp = dp;
734 dp = NULLVP;
735 } else {
736 need_dp:
737 /*
738 * return the last directory we looked at
739 * with an io reference held
740 */
741 if (dp == ndp->ni_usedvp) {
742 /*
743 * if this vnode matches the one passed in via USEDVP
744 * than this context already holds an io_count... just
745 * use vnode_get to get an extra ref for lookup to play
746 * with... can't use the getwithvid variant here because
747 * it will block behind a vnode_drain which would result
748 * in a deadlock (since we already own an io_count that the
749 * vnode_drain is waiting on)... vnode_get grabs the io_count
750 * immediately w/o waiting... it always succeeds
751 */
752 vnode_get(dp);
753 } else if ( (vnode_getwithvid(dp, vid)) ) {
754 /*
755 * failure indicates the vnode
756 * changed identity or is being
757 * TERMINATED... in either case
758 * punt this lookup
759 */
760 return (ENOENT);
761 }
762 }
763 if (vp != NULLVP) {
764 if ( (vnode_getwithvid(vp, vvid)) ) {
765 vp = NULLVP;
766
767 /*
768 * can't get reference on the vp we'd like
769 * to return... if we didn't grab a reference
770 * on the directory (due to fast path bypass),
771 * then we need to do it now... we can't return
772 * with both ni_dvp and ni_vp NULL, and no
773 * error condition
774 */
775 if (dp == NULLVP) {
776 dp = tdp;
777 goto need_dp;
778 }
779 }
780 }
781 ndp->ni_dvp = dp;
782 ndp->ni_vp = vp;
783
784 return (0);
785 }
786
787
788 static vnode_t
789 cache_lookup_locked(vnode_t dvp, struct componentname *cnp)
790 {
791 register struct namecache *ncp;
792 register struct nchashhead *ncpp;
793 register long namelen = cnp->cn_namelen;
794 char *nameptr = cnp->cn_nameptr;
795 unsigned int hashval = (cnp->cn_hash & NCHASHMASK);
796 vnode_t vp;
797
798 ncpp = NCHHASH(dvp, cnp->cn_hash);
799 LIST_FOREACH(ncp, ncpp, nc_hash) {
800 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
801 if (memcmp(ncp->nc_name, nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
802 break;
803 }
804 }
805 if (ncp == 0)
806 /*
807 * We failed to find an entry
808 */
809 return (NULL);
810
811 vp = ncp->nc_vp;
812 if (vp && (vp->v_flag & VISHARDLINK)) {
813 /*
814 * The file system wants a VNOP_LOOKUP on this vnode
815 */
816 vp = NULL;
817 }
818
819 return (vp);
820 }
821
822
823 //
824 // Have to take a len argument because we may only need to
825 // hash part of a componentname.
826 //
827 static unsigned int
828 hash_string(const char *cp, int len)
829 {
830 unsigned hash = 0;
831
832 if (len) {
833 while (len--) {
834 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
835 }
836 } else {
837 while (*cp != '\0') {
838 hash ^= crc32tab[((hash >> 24) ^ (unsigned char)*cp++)];
839 }
840 }
841 /*
842 * the crc generator can legitimately generate
843 * a 0... however, 0 for us means that we
844 * haven't computed a hash, so use 1 instead
845 */
846 if (hash == 0)
847 hash = 1;
848 return hash;
849 }
850
851
852 /*
853 * Lookup an entry in the cache
854 *
855 * We don't do this if the segment name is long, simply so the cache
856 * can avoid holding long names (which would either waste space, or
857 * add greatly to the complexity).
858 *
859 * Lookup is called with dvp pointing to the directory to search,
860 * cnp pointing to the name of the entry being sought. If the lookup
861 * succeeds, the vnode is returned in *vpp, and a status of -1 is
862 * returned. If the lookup determines that the name does not exist
863 * (negative cacheing), a status of ENOENT is returned. If the lookup
864 * fails, a status of zero is returned.
865 */
866
867 int
868 cache_lookup(dvp, vpp, cnp)
869 struct vnode *dvp;
870 struct vnode **vpp;
871 struct componentname *cnp;
872 {
873 register struct namecache *ncp;
874 register struct nchashhead *ncpp;
875 register long namelen = cnp->cn_namelen;
876 char *nameptr = cnp->cn_nameptr;
877 unsigned int hashval = (cnp->cn_hash & NCHASHMASK);
878 uint32_t vid;
879 vnode_t vp;
880
881 name_cache_lock();
882
883 ncpp = NCHHASH(dvp, cnp->cn_hash);
884 LIST_FOREACH(ncp, ncpp, nc_hash) {
885 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
886 if (memcmp(ncp->nc_name, nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
887 break;
888 }
889 }
890 /* We failed to find an entry */
891 if (ncp == 0) {
892 nchstats.ncs_miss++;
893 name_cache_unlock();
894 return (0);
895 }
896
897 /* We don't want to have an entry, so dump it */
898 if ((cnp->cn_flags & MAKEENTRY) == 0) {
899 nchstats.ncs_badhits++;
900 cache_delete(ncp, 1);
901 name_cache_unlock();
902 return (0);
903 }
904 vp = ncp->nc_vp;
905
906 /* We found a "positive" match, return the vnode */
907 if (vp) {
908 nchstats.ncs_goodhits++;
909
910 vid = vp->v_id;
911 name_cache_unlock();
912
913 if (vnode_getwithvid(vp, vid)) {
914 name_cache_lock();
915 nchstats.ncs_badvid++;
916 name_cache_unlock();
917 return (0);
918 }
919 *vpp = vp;
920 return (-1);
921 }
922
923 /* We found a negative match, and want to create it, so purge */
924 if (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) {
925 nchstats.ncs_badhits++;
926 cache_delete(ncp, 1);
927 name_cache_unlock();
928 return (0);
929 }
930
931 /*
932 * We found a "negative" match, ENOENT notifies client of this match.
933 * The nc_whiteout field records whether this is a whiteout.
934 */
935 nchstats.ncs_neghits++;
936
937 if (ncp->nc_whiteout)
938 cnp->cn_flags |= ISWHITEOUT;
939 name_cache_unlock();
940 return (ENOENT);
941 }
942
943 /*
944 * Add an entry to the cache.
945 */
946 void
947 cache_enter(dvp, vp, cnp)
948 struct vnode *dvp;
949 struct vnode *vp;
950 struct componentname *cnp;
951 {
952 register struct namecache *ncp, *negp;
953 register struct nchashhead *ncpp;
954
955 if (cnp->cn_hash == 0)
956 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
957
958 name_cache_lock();
959
960 /* if the entry is for -ve caching vp is null */
961 if ((vp != NULLVP) && (LIST_FIRST(&vp->v_nclinks))) {
962 /*
963 * someone beat us to the punch..
964 * this vnode is already in the cache
965 */
966 name_cache_unlock();
967 return;
968 }
969 /*
970 * We allocate a new entry if we are less than the maximum
971 * allowed and the one at the front of the list is in use.
972 * Otherwise we use the one at the front of the list.
973 */
974 if (numcache < desiredNodes &&
975 ((ncp = nchead.tqh_first) == NULL ||
976 ncp->nc_hash.le_prev != 0)) {
977 /*
978 * Allocate one more entry
979 */
980 ncp = (struct namecache *)_MALLOC_ZONE((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
981 numcache++;
982 } else {
983 /*
984 * reuse an old entry
985 */
986 ncp = TAILQ_FIRST(&nchead);
987 TAILQ_REMOVE(&nchead, ncp, nc_entry);
988
989 if (ncp->nc_hash.le_prev != 0) {
990 /*
991 * still in use... we need to
992 * delete it before re-using it
993 */
994 nchstats.ncs_stolen++;
995 cache_delete(ncp, 0);
996 }
997 }
998 nchstats.ncs_enters++;
999
1000 /*
1001 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1002 */
1003 ncp->nc_vp = vp;
1004 ncp->nc_dvp = dvp;
1005 ncp->nc_hashval = cnp->cn_hash;
1006 ncp->nc_whiteout = FALSE;
1007 ncp->nc_name = add_name_locked(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, 0);
1008
1009 /*
1010 * make us the newest entry in the cache
1011 * i.e. we'll be the last to be stolen
1012 */
1013 TAILQ_INSERT_TAIL(&nchead, ncp, nc_entry);
1014
1015 ncpp = NCHHASH(dvp, cnp->cn_hash);
1016 #if DIAGNOSTIC
1017 {
1018 register struct namecache *p;
1019
1020 for (p = ncpp->lh_first; p != 0; p = p->nc_hash.le_next)
1021 if (p == ncp)
1022 panic("cache_enter: duplicate");
1023 }
1024 #endif
1025 /*
1026 * make us available to be found via lookup
1027 */
1028 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1029
1030 if (vp) {
1031 /*
1032 * add to the list of name cache entries
1033 * that point at vp
1034 */
1035 LIST_INSERT_HEAD(&vp->v_nclinks, ncp, nc_un.nc_link);
1036 } else {
1037 /*
1038 * this is a negative cache entry (vp == NULL)
1039 * stick it on the negative cache list
1040 * and record the whiteout state
1041 */
1042 TAILQ_INSERT_TAIL(&neghead, ncp, nc_un.nc_negentry);
1043
1044 if (cnp->cn_flags & ISWHITEOUT)
1045 ncp->nc_whiteout = TRUE;
1046 nchstats.ncs_negtotal++;
1047
1048 if (nchstats.ncs_negtotal > desiredNegNodes) {
1049 /*
1050 * if we've reached our desired limit
1051 * of negative cache entries, delete
1052 * the oldest
1053 */
1054 negp = TAILQ_FIRST(&neghead);
1055 TAILQ_REMOVE(&neghead, negp, nc_un.nc_negentry);
1056
1057 cache_delete(negp, 1);
1058 }
1059 }
1060 /*
1061 * add us to the list of name cache entries that
1062 * are children of dvp
1063 */
1064 LIST_INSERT_HEAD(&dvp->v_ncchildren, ncp, nc_child);
1065
1066 name_cache_unlock();
1067 }
1068
1069
1070 /*
1071 * Initialize CRC-32 remainder table.
1072 */
1073 static void init_crc32(void)
1074 {
1075 /*
1076 * the CRC-32 generator polynomial is:
1077 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1078 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1079 */
1080 unsigned int crc32_polynomial = 0x04c11db7;
1081 unsigned int i,j;
1082
1083 /*
1084 * pre-calculate the CRC-32 remainder for each possible octet encoding
1085 */
1086 for (i = 0; i < 256; i++) {
1087 unsigned int crc_rem = i << 24;
1088
1089 for (j = 0; j < 8; j++) {
1090 if (crc_rem & 0x80000000)
1091 crc_rem = (crc_rem << 1) ^ crc32_polynomial;
1092 else
1093 crc_rem = (crc_rem << 1);
1094 }
1095 crc32tab[i] = crc_rem;
1096 }
1097 }
1098
1099
1100 /*
1101 * Name cache initialization, from vfs_init() when we are booting
1102 */
1103 void
1104 nchinit(void)
1105 {
1106 desiredNegNodes = (desiredvnodes / 10);
1107 desiredNodes = desiredvnodes + desiredNegNodes;
1108
1109 TAILQ_INIT(&nchead);
1110 TAILQ_INIT(&neghead);
1111
1112 init_crc32();
1113
1114 nchashtbl = hashinit(MAX(4096, (2 *desiredNodes)), M_CACHE, &nchash);
1115 nchashmask = nchash;
1116 nchash++;
1117
1118 init_string_table();
1119
1120 /* Allocate mount list lock group attribute and group */
1121 namecache_lck_grp_attr= lck_grp_attr_alloc_init();
1122 lck_grp_attr_setstat(namecache_lck_grp_attr);
1123
1124 namecache_lck_grp = lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr);
1125
1126 /* Allocate mount list lock attribute */
1127 namecache_lck_attr = lck_attr_alloc_init();
1128 //lck_attr_setdebug(namecache_lck_attr);
1129
1130 /* Allocate mount list lock */
1131 namecache_mtx_lock = lck_mtx_alloc_init(namecache_lck_grp, namecache_lck_attr);
1132
1133
1134 }
1135
1136 void
1137 name_cache_lock(void)
1138 {
1139 lck_mtx_lock(namecache_mtx_lock);
1140 }
1141
1142 void
1143 name_cache_unlock(void)
1144 {
1145 lck_mtx_unlock(namecache_mtx_lock);
1146
1147 }
1148
1149
1150 int
1151 resize_namecache(u_int newsize)
1152 {
1153 struct nchashhead *new_table;
1154 struct nchashhead *old_table;
1155 struct nchashhead *old_head, *head;
1156 struct namecache *entry, *next;
1157 uint32_t i, hashval;
1158 int dNodes, dNegNodes;
1159 u_long new_size, old_size;
1160
1161 dNegNodes = (newsize / 10);
1162 dNodes = newsize + dNegNodes;
1163
1164 // we don't support shrinking yet
1165 if (dNodes < desiredNodes) {
1166 return 0;
1167 }
1168 new_table = hashinit(2 * dNodes, M_CACHE, &nchashmask);
1169 new_size = nchashmask + 1;
1170
1171 if (new_table == NULL) {
1172 return ENOMEM;
1173 }
1174
1175 name_cache_lock();
1176 // do the switch!
1177 old_table = nchashtbl;
1178 nchashtbl = new_table;
1179 old_size = nchash;
1180 nchash = new_size;
1181
1182 // walk the old table and insert all the entries into
1183 // the new table
1184 //
1185 for(i=0; i < old_size; i++) {
1186 old_head = &old_table[i];
1187 for (entry=old_head->lh_first; entry != NULL; entry=next) {
1188 //
1189 // XXXdbg - Beware: this assumes that hash_string() does
1190 // the same thing as what happens in
1191 // lookup() over in vfs_lookup.c
1192 hashval = hash_string(entry->nc_name, 0);
1193 entry->nc_hashval = hashval;
1194 head = NCHHASH(entry->nc_dvp, hashval);
1195
1196 next = entry->nc_hash.le_next;
1197 LIST_INSERT_HEAD(head, entry, nc_hash);
1198 }
1199 }
1200 desiredNodes = dNodes;
1201 desiredNegNodes = dNegNodes;
1202
1203 name_cache_unlock();
1204 FREE(old_table, M_CACHE);
1205
1206 return 0;
1207 }
1208
1209 static void
1210 cache_delete(struct namecache *ncp, int age_entry)
1211 {
1212 nchstats.ncs_deletes++;
1213
1214 if (ncp->nc_vp) {
1215 LIST_REMOVE(ncp, nc_un.nc_link);
1216 } else {
1217 TAILQ_REMOVE(&neghead, ncp, nc_un.nc_negentry);
1218 nchstats.ncs_negtotal--;
1219 }
1220 LIST_REMOVE(ncp, nc_child);
1221
1222 LIST_REMOVE(ncp, nc_hash);
1223 /*
1224 * this field is used to indicate
1225 * that the entry is in use and
1226 * must be deleted before it can
1227 * be reused...
1228 */
1229 ncp->nc_hash.le_prev = NULL;
1230
1231 if (age_entry) {
1232 /*
1233 * make it the next one available
1234 * for cache_enter's use
1235 */
1236 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1237 TAILQ_INSERT_HEAD(&nchead, ncp, nc_entry);
1238 }
1239 remove_name_locked(ncp->nc_name);
1240 ncp->nc_name = NULL;
1241 }
1242
1243
1244 /*
1245 * purge the entry associated with the
1246 * specified vnode from the name cache
1247 */
1248 void
1249 cache_purge(vnode_t vp)
1250 {
1251 struct namecache *ncp;
1252
1253 if ((LIST_FIRST(&vp->v_nclinks) == NULL) && (LIST_FIRST(&vp->v_ncchildren) == NULL))
1254 return;
1255
1256 name_cache_lock();
1257
1258 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
1259 cache_delete(ncp, 1);
1260
1261 while ( (ncp = LIST_FIRST(&vp->v_ncchildren)) )
1262 cache_delete(ncp, 1);
1263
1264 name_cache_unlock();
1265 }
1266
1267 /*
1268 * Purge all negative cache entries that are children of the
1269 * given vnode. A case-insensitive file system (or any file
1270 * system that has multiple equivalent names for the same
1271 * directory entry) can use this when creating or renaming
1272 * to remove negative entries that may no longer apply.
1273 */
1274 void
1275 cache_purge_negatives(vnode_t vp)
1276 {
1277 struct namecache *ncp;
1278
1279 name_cache_lock();
1280
1281 LIST_FOREACH(ncp, &vp->v_ncchildren, nc_child)
1282 if (ncp->nc_vp == NULL)
1283 cache_delete(ncp , 1);
1284
1285 name_cache_unlock();
1286 }
1287
1288 /*
1289 * Flush all entries referencing a particular filesystem.
1290 *
1291 * Since we need to check it anyway, we will flush all the invalid
1292 * entries at the same time.
1293 */
1294 void
1295 cache_purgevfs(mp)
1296 struct mount *mp;
1297 {
1298 struct nchashhead *ncpp;
1299 struct namecache *ncp;
1300
1301 name_cache_lock();
1302 /* Scan hash tables for applicable entries */
1303 for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
1304 restart:
1305 for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
1306 if (ncp->nc_dvp->v_mount == mp) {
1307 cache_delete(ncp, 0);
1308 goto restart;
1309 }
1310 }
1311 }
1312 name_cache_unlock();
1313 }
1314
1315
1316
1317 //
1318 // String ref routines
1319 //
1320 static LIST_HEAD(stringhead, string_t) *string_ref_table;
1321 static u_long string_table_mask;
1322 static uint32_t max_chain_len=0;
1323 static struct stringhead *long_chain_head=NULL;
1324 static uint32_t filled_buckets=0;
1325 static uint32_t num_dups=0;
1326 static uint32_t nstrings=0;
1327
1328 typedef struct string_t {
1329 LIST_ENTRY(string_t) hash_chain;
1330 unsigned char *str;
1331 uint32_t refcount;
1332 } string_t;
1333
1334
1335
1336 static int
1337 resize_string_ref_table(void)
1338 {
1339 struct stringhead *new_table;
1340 struct stringhead *old_table;
1341 struct stringhead *old_head, *head;
1342 string_t *entry, *next;
1343 uint32_t i, hashval;
1344 u_long new_mask, old_mask;
1345
1346 new_table = hashinit((string_table_mask + 1) * 2, M_CACHE, &new_mask);
1347 if (new_table == NULL) {
1348 return ENOMEM;
1349 }
1350
1351 // do the switch!
1352 old_table = string_ref_table;
1353 string_ref_table = new_table;
1354 old_mask = string_table_mask;
1355 string_table_mask = new_mask;
1356
1357 printf("resize: max chain len %d, new table size %d\n",
1358 max_chain_len, new_mask + 1);
1359 max_chain_len = 0;
1360 long_chain_head = NULL;
1361 filled_buckets = 0;
1362
1363 // walk the old table and insert all the entries into
1364 // the new table
1365 //
1366 for(i=0; i <= old_mask; i++) {
1367 old_head = &old_table[i];
1368 for (entry=old_head->lh_first; entry != NULL; entry=next) {
1369 hashval = hash_string(entry->str, 0);
1370 head = &string_ref_table[hashval & string_table_mask];
1371 if (head->lh_first == NULL) {
1372 filled_buckets++;
1373 }
1374
1375 next = entry->hash_chain.le_next;
1376 LIST_INSERT_HEAD(head, entry, hash_chain);
1377 }
1378 }
1379
1380 FREE(old_table, M_CACHE);
1381
1382 return 0;
1383 }
1384
1385
1386 static void
1387 init_string_table(void)
1388 {
1389 string_ref_table = hashinit(4096, M_CACHE, &string_table_mask);
1390 }
1391
1392
1393 char *
1394 vfs_addname(const char *name, size_t len, u_int hashval, u_int flags)
1395 {
1396 char * ptr;
1397
1398 name_cache_lock();
1399 ptr = add_name_locked(name, len, hashval, flags);
1400 name_cache_unlock();
1401
1402 return(ptr);
1403 }
1404
1405 static char *
1406 add_name_locked(const char *name, size_t len, u_int hashval, __unused u_int flags)
1407 {
1408 struct stringhead *head;
1409 string_t *entry;
1410 uint32_t chain_len = 0;
1411
1412 //
1413 // If the table gets more than 3/4 full, resize it
1414 //
1415 if (4*filled_buckets >= ((string_table_mask + 1) * 3)) {
1416 if (resize_string_ref_table() != 0) {
1417 printf("failed to resize the hash table.\n");
1418 }
1419 }
1420 if (hashval == 0) {
1421 hashval = hash_string(name, 0);
1422 }
1423
1424 head = &string_ref_table[hashval & string_table_mask];
1425 for (entry=head->lh_first; entry != NULL; chain_len++, entry=entry->hash_chain.le_next) {
1426 if (memcmp(entry->str, name, len) == 0 && entry->str[len] == '\0') {
1427 entry->refcount++;
1428 num_dups++;
1429 break;
1430 }
1431 }
1432
1433 if (entry == NULL) {
1434 // it wasn't already there so add it.
1435 MALLOC(entry, string_t *, sizeof(string_t) + len + 1, M_TEMP, M_WAITOK);
1436
1437 // have to get "head" again because we could have blocked
1438 // in malloc and thus head could have changed.
1439 //
1440 head = &string_ref_table[hashval & string_table_mask];
1441 if (head->lh_first == NULL) {
1442 filled_buckets++;
1443 }
1444
1445 entry->str = (char *)((char *)entry + sizeof(string_t));
1446 strncpy(entry->str, name, len);
1447 entry->str[len] = '\0';
1448 entry->refcount = 1;
1449 LIST_INSERT_HEAD(head, entry, hash_chain);
1450
1451 if (chain_len > max_chain_len) {
1452 max_chain_len = chain_len;
1453 long_chain_head = head;
1454 }
1455
1456 nstrings++;
1457 }
1458
1459 return entry->str;
1460 }
1461
1462 int
1463 vfs_removename(const char *nameref)
1464 {
1465 int i;
1466
1467 name_cache_lock();
1468 i = remove_name_locked(nameref);
1469 name_cache_unlock();
1470
1471 return(i);
1472
1473 }
1474
1475
1476 static int
1477 remove_name_locked(const char *nameref)
1478 {
1479 struct stringhead *head;
1480 string_t *entry;
1481 uint32_t hashval;
1482 char * ptr;
1483
1484 hashval = hash_string(nameref, 0);
1485 head = &string_ref_table[hashval & string_table_mask];
1486 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
1487 if (entry->str == (unsigned char *)nameref) {
1488 entry->refcount--;
1489 if (entry->refcount == 0) {
1490 LIST_REMOVE(entry, hash_chain);
1491 if (head->lh_first == NULL) {
1492 filled_buckets--;
1493 }
1494 ptr = entry->str;
1495 entry->str = NULL;
1496 nstrings--;
1497
1498 FREE(entry, M_TEMP);
1499 } else {
1500 num_dups--;
1501 }
1502
1503 return 0;
1504 }
1505 }
1506
1507 return ENOENT;
1508 }
1509
1510
1511 void
1512 dump_string_table(void)
1513 {
1514 struct stringhead *head;
1515 string_t *entry;
1516 u_long i;
1517
1518 name_cache_lock();
1519 for (i = 0; i <= string_table_mask; i++) {
1520 head = &string_ref_table[i];
1521 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
1522 printf("%6d - %s\n", entry->refcount, entry->str);
1523 }
1524 }
1525 name_cache_unlock();
1526 }