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