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