]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cache.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_cache.c
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Poul-Henning Kamp of the FreeBSD Project.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 *
65 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/time.h>
76 #include <sys/mount_internal.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/namei.h>
79 #include <sys/errno.h>
80 #include <sys/malloc.h>
81 #include <sys/kauth.h>
82 #include <sys/user.h>
83 #include <sys/paths.h>
84
85 #if CONFIG_MACF
86 #include <security/mac_framework.h>
87 #endif
88
89 /*
90 * Name caching works as follows:
91 *
92 * Names found by directory scans are retained in a cache
93 * for future reference. It is managed LRU, so frequently
94 * used names will hang around. Cache is indexed by hash value
95 * obtained from (vp, name) where vp refers to the directory
96 * containing name.
97 *
98 * If it is a "negative" entry, (i.e. for a name that is known NOT to
99 * exist) the vnode pointer will be NULL.
100 *
101 * Upon reaching the last segment of a path, if the reference
102 * is for DELETE, or NOCACHE is set (rewrite), and the
103 * name is located in the cache, it will be dropped.
104 */
105
106 /*
107 * Structures associated with name cacheing.
108 */
109
110 LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
111 u_long nchashmask;
112 u_long nchash; /* size of hash table - 1 */
113 long numcache; /* number of cache entries allocated */
114 int desiredNodes;
115 int desiredNegNodes;
116 int ncs_negtotal;
117 TAILQ_HEAD(, namecache) nchead; /* chain of all name cache entries */
118 TAILQ_HEAD(, namecache) neghead; /* chain of only negative cache entries */
119
120
121 #if COLLECT_STATS
122
123 struct nchstats nchstats; /* cache effectiveness statistics */
124
125 #define NCHSTAT(v) { \
126 nchstats.v++; \
127 }
128 #define NAME_CACHE_LOCK() name_cache_lock()
129 #define NAME_CACHE_UNLOCK() name_cache_unlock()
130 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
131
132 #else
133
134 #define NCHSTAT(v)
135 #define NAME_CACHE_LOCK() name_cache_lock()
136 #define NAME_CACHE_UNLOCK() name_cache_unlock()
137 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
138
139 #endif
140
141
142 /* vars for name cache list lock */
143 lck_grp_t * namecache_lck_grp;
144 lck_grp_attr_t * namecache_lck_grp_attr;
145 lck_attr_t * namecache_lck_attr;
146
147 lck_grp_t * strcache_lck_grp;
148 lck_grp_attr_t * strcache_lck_grp_attr;
149 lck_attr_t * strcache_lck_attr;
150
151 lck_rw_t * namecache_rw_lock;
152 lck_rw_t * strtable_rw_lock;
153
154 #define NUM_STRCACHE_LOCKS 1024
155
156 lck_mtx_t strcache_mtx_locks[NUM_STRCACHE_LOCKS];
157
158
159 static vnode_t cache_lookup_locked(vnode_t dvp, struct componentname *cnp);
160 static const char *add_name_internal(const char *, uint32_t, u_int, boolean_t, u_int);
161 static void init_string_table(void) __attribute__((section("__TEXT, initcode")));
162 static void cache_delete(struct namecache *, int);
163 static void cache_enter_locked(vnode_t dvp, vnode_t vp, struct componentname *cnp, const char *strname);
164
165 #ifdef DUMP_STRING_TABLE
166 /*
167 * Internal dump function used for debugging
168 */
169 void dump_string_table(void);
170 #endif /* DUMP_STRING_TABLE */
171
172 static void init_crc32(void) __attribute__((section("__TEXT, initcode")));
173 static unsigned int crc32tab[256];
174
175
176 #define NCHHASH(dvp, hash_val) \
177 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
178
179
180
181 /*
182 * This function builds the path to a filename in "buff". The
183 * length of the buffer *INCLUDING* the trailing zero byte is
184 * returned in outlen. NOTE: the length includes the trailing
185 * zero byte and thus the length is one greater than what strlen
186 * would return. This is important and lots of code elsewhere
187 * in the kernel assumes this behavior.
188 *
189 * This function can call vnop in file system if the parent vnode
190 * does not exist or when called for hardlinks via volfs path.
191 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
192 * in the name cache and does not enter the file system.
193 *
194 * passed in vp must have a valid io_count reference
195 */
196 int
197 build_path(vnode_t first_vp, char *buff, int buflen, int *outlen, int flags, vfs_context_t ctx)
198 {
199 vnode_t vp, tvp;
200 vnode_t vp_with_iocount;
201 vnode_t proc_root_dir_vp;
202 char *end;
203 const char *str;
204 int len;
205 int ret = 0;
206 int fixhardlink;
207
208 if (first_vp == NULLVP)
209 return (EINVAL);
210
211 /*
212 * Grab the process fd so we can evaluate fd_rdir.
213 */
214 if (vfs_context_proc(ctx)->p_fd)
215 proc_root_dir_vp = vfs_context_proc(ctx)->p_fd->fd_rdir;
216 else
217 proc_root_dir_vp = NULL;
218
219 vp_with_iocount = NULLVP;
220 again:
221 vp = first_vp;
222
223 end = &buff[buflen-1];
224 *end = '\0';
225
226 /*
227 * holding the NAME_CACHE_LOCK in shared mode is
228 * sufficient to stabilize both the vp->v_parent chain
229 * and the 'vp->v_mount->mnt_vnodecovered' chain
230 *
231 * if we need to drop this lock, we must first grab the v_id
232 * from the vnode we're currently working with... if that
233 * vnode doesn't already have an io_count reference (the vp
234 * passed in comes with one), we must grab a reference
235 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
236 * deadlocks may result if you call vnode_get while holding
237 * the NAME_CACHE_LOCK... we lazily release the reference
238 * we pick up the next time we encounter a need to drop
239 * the NAME_CACHE_LOCK or before we return from this routine
240 */
241 NAME_CACHE_LOCK_SHARED();
242
243 /*
244 * Check if this is the root of a file system.
245 */
246 while (vp && vp->v_flag & VROOT) {
247 if (vp->v_mount == NULL) {
248 ret = EINVAL;
249 goto out_unlock;
250 }
251 if ((vp->v_mount->mnt_flag & MNT_ROOTFS) || (vp == proc_root_dir_vp)) {
252 /*
253 * It's the root of the root file system, so it's
254 * just "/".
255 */
256 *--end = '/';
257
258 goto out_unlock;
259 } else {
260 vp = vp->v_mount->mnt_vnodecovered;
261 }
262 }
263
264 while ((vp != NULLVP) && (vp->v_parent != vp)) {
265 int vid;
266
267 /*
268 * For hardlinks the v_name may be stale, so if its OK
269 * to enter a file system, ask the file system for the
270 * name and parent (below).
271 */
272 fixhardlink = (vp->v_flag & VISHARDLINK) &&
273 (vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID) &&
274 !(flags & BUILDPATH_NO_FS_ENTER);
275
276 if (!fixhardlink) {
277 str = vp->v_name;
278
279 if (str == NULL || *str == '\0') {
280 if (vp->v_parent != NULL)
281 ret = EINVAL;
282 else
283 ret = ENOENT;
284 goto out_unlock;
285 }
286 len = strlen(str);
287 /*
288 * Check that there's enough space (including space for the '/')
289 */
290 if ((end - buff) < (len + 1)) {
291 ret = ENOSPC;
292 goto out_unlock;
293 }
294 /*
295 * Copy the name backwards.
296 */
297 str += len;
298
299 for (; len > 0; len--)
300 *--end = *--str;
301 /*
302 * Add a path separator.
303 */
304 *--end = '/';
305 }
306
307 /*
308 * Walk up the parent chain.
309 */
310 if (((vp->v_parent != NULLVP) && !fixhardlink) ||
311 (flags & BUILDPATH_NO_FS_ENTER)) {
312 vp = vp->v_parent;
313
314 /*
315 * if the vnode we have in hand isn't a directory and it
316 * has a v_parent, then we started with the resource fork
317 * so skip up to avoid getting a duplicate copy of the
318 * file name in the path.
319 */
320 if (vp && !vnode_isdir(vp) && vp->v_parent)
321 vp = vp->v_parent;
322 } else {
323 /*
324 * No parent, go get it if supported.
325 */
326 struct vnode_attr va;
327 vnode_t dvp;
328
329 /*
330 * Make sure file system supports obtaining a path from id.
331 */
332 if (!(vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID)) {
333 ret = ENOENT;
334 goto out_unlock;
335 }
336 vid = vp->v_id;
337
338 NAME_CACHE_UNLOCK();
339
340 if (vp != first_vp && vp != vp_with_iocount) {
341 if (vp_with_iocount) {
342 vnode_put(vp_with_iocount);
343 vp_with_iocount = NULLVP;
344 }
345 if (vnode_getwithvid(vp, vid))
346 goto again;
347 vp_with_iocount = vp;
348 }
349 VATTR_INIT(&va);
350 VATTR_WANTED(&va, va_parentid);
351
352 if (fixhardlink) {
353 VATTR_WANTED(&va, va_name);
354 MALLOC_ZONE(va.va_name, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
355 } else {
356 va.va_name = NULL;
357 }
358 /*
359 * Ask the file system for its parent id and for its name (optional).
360 */
361 ret = vnode_getattr(vp, &va, ctx);
362
363 if (fixhardlink) {
364 if ((ret == 0) && (VATTR_IS_SUPPORTED(&va, va_name))) {
365 str = va.va_name;
366 vnode_update_identity(vp, NULL, str, strlen(str), 0, VNODE_UPDATE_NAME);
367 } else if (vp->v_name) {
368 str = vp->v_name;
369 ret = 0;
370 } else {
371 ret = ENOENT;
372 goto bad_news;
373 }
374 len = strlen(str);
375
376 /*
377 * Check that there's enough space.
378 */
379 if ((end - buff) < (len + 1)) {
380 ret = ENOSPC;
381 } else {
382 /* Copy the name backwards. */
383 str += len;
384
385 for (; len > 0; len--) {
386 *--end = *--str;
387 }
388 /*
389 * Add a path separator.
390 */
391 *--end = '/';
392 }
393 bad_news:
394 FREE_ZONE(va.va_name, MAXPATHLEN, M_NAMEI);
395 }
396 if (ret || !VATTR_IS_SUPPORTED(&va, va_parentid)) {
397 ret = ENOENT;
398 goto out;
399 }
400 /*
401 * Ask the file system for the parent vnode.
402 */
403 if ((ret = VFS_VGET(vp->v_mount, (ino64_t)va.va_parentid, &dvp, ctx)))
404 goto out;
405
406 if (!fixhardlink && (vp->v_parent != dvp))
407 vnode_update_identity(vp, dvp, NULL, 0, 0, VNODE_UPDATE_PARENT);
408
409 if (vp_with_iocount)
410 vnode_put(vp_with_iocount);
411 vp = dvp;
412 vp_with_iocount = vp;
413
414 NAME_CACHE_LOCK_SHARED();
415
416 /*
417 * if the vnode we have in hand isn't a directory and it
418 * has a v_parent, then we started with the resource fork
419 * so skip up to avoid getting a duplicate copy of the
420 * file name in the path.
421 */
422 if (vp && !vnode_isdir(vp) && vp->v_parent)
423 vp = vp->v_parent;
424 }
425 /*
426 * When a mount point is crossed switch the vp.
427 * Continue until we find the root or we find
428 * a vnode that's not the root of a mounted
429 * file system.
430 */
431 tvp = vp;
432
433 while (tvp) {
434 if (tvp == proc_root_dir_vp)
435 goto out_unlock; /* encountered the root */
436
437 if (!(tvp->v_flag & VROOT) || !tvp->v_mount)
438 break; /* not the root of a mounted FS */
439 tvp = tvp->v_mount->mnt_vnodecovered;
440 }
441 if (tvp == NULLVP)
442 goto out_unlock;
443 vp = tvp;
444
445 if (vp && (flags & BUILDPATH_CHECKACCESS)) {
446 vid = vp->v_id;
447
448 NAME_CACHE_UNLOCK();
449
450 if (vp != first_vp && vp != vp_with_iocount) {
451 if (vp_with_iocount) {
452 vnode_put(vp_with_iocount);
453 vp_with_iocount = NULLVP;
454 }
455 if (vnode_getwithvid(vp, vid))
456 goto again;
457 vp_with_iocount = vp;
458 }
459 if ((ret = vnode_authorize(vp, NULL, KAUTH_VNODE_SEARCH, ctx)))
460 goto out; /* no peeking */
461
462 NAME_CACHE_LOCK_SHARED();
463 }
464 }
465 out_unlock:
466 NAME_CACHE_UNLOCK();
467 out:
468 if (vp_with_iocount)
469 vnode_put(vp_with_iocount);
470 /*
471 * Slide the name down to the beginning of the buffer.
472 */
473 memmove(buff, end, &buff[buflen] - end);
474
475 /*
476 * length includes the trailing zero byte
477 */
478 *outlen = &buff[buflen] - end;
479
480 return (ret);
481 }
482
483
484 /*
485 * return NULLVP if vp's parent doesn't
486 * exist, or we can't get a valid iocount
487 * else return the parent of vp
488 */
489 vnode_t
490 vnode_getparent(vnode_t vp)
491 {
492 vnode_t pvp = NULLVP;
493 int pvid;
494
495 NAME_CACHE_LOCK_SHARED();
496 /*
497 * v_parent is stable behind the name_cache lock
498 * however, the only thing we can really guarantee
499 * is that we've grabbed a valid iocount on the
500 * parent of 'vp' at the time we took the name_cache lock...
501 * once we drop the lock, vp could get re-parented
502 */
503 if ( (pvp = vp->v_parent) != NULLVP ) {
504 pvid = pvp->v_id;
505
506 NAME_CACHE_UNLOCK();
507
508 if (vnode_getwithvid(pvp, pvid) != 0)
509 pvp = NULL;
510 } else
511 NAME_CACHE_UNLOCK();
512 return (pvp);
513 }
514
515 const char *
516 vnode_getname(vnode_t vp)
517 {
518 const char *name = NULL;
519
520 NAME_CACHE_LOCK_SHARED();
521
522 if (vp->v_name)
523 name = vfs_addname(vp->v_name, strlen(vp->v_name), 0, 0);
524 NAME_CACHE_UNLOCK();
525
526 return (name);
527 }
528
529 void
530 vnode_putname(const char *name)
531 {
532 vfs_removename(name);
533 }
534
535
536 /*
537 * if VNODE_UPDATE_PARENT, and we can take
538 * a reference on dvp, then update vp with
539 * it's new parent... if vp already has a parent,
540 * then drop the reference vp held on it
541 *
542 * if VNODE_UPDATE_NAME,
543 * then drop string ref on v_name if it exists, and if name is non-NULL
544 * then pick up a string reference on name and record it in v_name...
545 * optionally pass in the length and hashval of name if known
546 *
547 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
548 */
549 void
550 vnode_update_identity(vnode_t vp, vnode_t dvp, const char *name, int name_len, uint32_t name_hashval, int flags)
551 {
552 struct namecache *ncp;
553 vnode_t old_parentvp = NULLVP;
554 #if NAMEDSTREAMS
555 int isstream = (vp->v_flag & VISNAMEDSTREAM);
556 int kusecountbumped = 0;
557 #endif
558 kauth_cred_t tcred = NULL;
559 const char *vname = NULL;
560 const char *tname = NULL;
561
562 if (flags & VNODE_UPDATE_PARENT) {
563 if (dvp && vnode_ref(dvp) != 0) {
564 dvp = NULLVP;
565 }
566 #if NAMEDSTREAMS
567 /* Don't count a stream's parent ref during unmounts */
568 if (isstream && dvp && (dvp != vp) && (dvp != vp->v_parent) && (dvp->v_type == VREG)) {
569 vnode_lock_spin(dvp);
570 ++dvp->v_kusecount;
571 kusecountbumped = 1;
572 vnode_unlock(dvp);
573 }
574 #endif
575 } else {
576 dvp = NULLVP;
577 }
578 if ( (flags & VNODE_UPDATE_NAME) ) {
579 if (name != vp->v_name) {
580 if (name && *name) {
581 if (name_len == 0)
582 name_len = strlen(name);
583 tname = vfs_addname(name, name_len, name_hashval, 0);
584 }
585 } else
586 flags &= ~VNODE_UPDATE_NAME;
587 }
588 if ( (flags & (VNODE_UPDATE_PURGE | VNODE_UPDATE_PARENT | VNODE_UPDATE_CACHE | VNODE_UPDATE_NAME)) ) {
589
590 NAME_CACHE_LOCK();
591
592 if ( (flags & VNODE_UPDATE_PURGE) ) {
593
594 if (vp->v_parent)
595 vp->v_parent->v_nc_generation++;
596
597 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
598 cache_delete(ncp, 1);
599
600 while ( (ncp = LIST_FIRST(&vp->v_ncchildren)) )
601 cache_delete(ncp, 1);
602
603 /*
604 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
605 */
606 tcred = vp->v_cred;
607 vp->v_cred = NOCRED;
608 vp->v_authorized_actions = 0;
609 }
610 if ( (flags & VNODE_UPDATE_NAME) ) {
611 vname = vp->v_name;
612 vp->v_name = tname;
613 }
614 if (flags & VNODE_UPDATE_PARENT) {
615 if (dvp != vp && dvp != vp->v_parent) {
616 old_parentvp = vp->v_parent;
617 vp->v_parent = dvp;
618 dvp = NULLVP;
619
620 if (old_parentvp)
621 flags |= VNODE_UPDATE_CACHE;
622 }
623 }
624 if (flags & VNODE_UPDATE_CACHE) {
625 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
626 cache_delete(ncp, 1);
627 }
628 NAME_CACHE_UNLOCK();
629
630 if (vname != NULL)
631 vfs_removename(vname);
632
633 if (IS_VALID_CRED(tcred))
634 kauth_cred_unref(&tcred);
635 }
636 if (dvp != NULLVP) {
637 #if NAMEDSTREAMS
638 /* Back-out the ref we took if we lost a race for vp->v_parent. */
639 if (kusecountbumped) {
640 vnode_lock_spin(dvp);
641 if (dvp->v_kusecount > 0)
642 --dvp->v_kusecount;
643 vnode_unlock(dvp);
644 }
645 #endif
646 vnode_rele(dvp);
647 }
648 if (old_parentvp) {
649 struct uthread *ut;
650
651 #if NAMEDSTREAMS
652 if (isstream) {
653 vnode_lock_spin(old_parentvp);
654 if ((old_parentvp->v_type != VDIR) && (old_parentvp->v_kusecount > 0))
655 --old_parentvp->v_kusecount;
656 vnode_unlock(old_parentvp);
657 }
658 #endif
659 ut = get_bsdthread_info(current_thread());
660
661 /*
662 * indicated to vnode_rele that it shouldn't do a
663 * vnode_reclaim at this time... instead it will
664 * chain the vnode to the uu_vreclaims list...
665 * we'll be responsible for calling vnode_reclaim
666 * on each of the vnodes in this list...
667 */
668 ut->uu_defer_reclaims = 1;
669 ut->uu_vreclaims = NULLVP;
670
671 while ( (vp = old_parentvp) != NULLVP ) {
672
673 vnode_lock_spin(vp);
674 vnode_rele_internal(vp, 0, 0, 1);
675
676 /*
677 * check to see if the vnode is now in the state
678 * that would have triggered a vnode_reclaim in vnode_rele
679 * if it is, we save it's parent pointer and then NULL
680 * out the v_parent field... we'll drop the reference
681 * that was held on the next iteration of this loop...
682 * this short circuits a potential deep recursion if we
683 * have a long chain of parents in this state...
684 * we'll sit in this loop until we run into
685 * a parent in this chain that is not in this state
686 *
687 * make our check and the vnode_rele atomic
688 * with respect to the current vnode we're working on
689 * by holding the vnode lock
690 * if vnode_rele deferred the vnode_reclaim and has put
691 * this vnode on the list to be reaped by us, than
692 * it has left this vnode with an iocount == 1
693 */
694 if ( (vp->v_iocount == 1) && (vp->v_usecount == 0) &&
695 ((vp->v_lflag & (VL_MARKTERM | VL_TERMINATE | VL_DEAD)) == VL_MARKTERM)) {
696 /*
697 * vnode_rele wanted to do a vnode_reclaim on this vnode
698 * it should be sitting on the head of the uu_vreclaims chain
699 * pull the parent pointer now so that when we do the
700 * vnode_reclaim for each of the vnodes in the uu_vreclaims
701 * list, we won't recurse back through here
702 *
703 * need to do a convert here in case vnode_rele_internal
704 * returns with the lock held in the spin mode... it
705 * can drop and retake the lock under certain circumstances
706 */
707 vnode_lock_convert(vp);
708
709 NAME_CACHE_LOCK();
710 old_parentvp = vp->v_parent;
711 vp->v_parent = NULLVP;
712 NAME_CACHE_UNLOCK();
713 } else {
714 /*
715 * we're done... we ran into a vnode that isn't
716 * being terminated
717 */
718 old_parentvp = NULLVP;
719 }
720 vnode_unlock(vp);
721 }
722 ut->uu_defer_reclaims = 0;
723
724 while ( (vp = ut->uu_vreclaims) != NULLVP) {
725 ut->uu_vreclaims = vp->v_defer_reclaimlist;
726
727 /*
728 * vnode_put will drive the vnode_reclaim if
729 * we are still the only reference on this vnode
730 */
731 vnode_put(vp);
732 }
733 }
734 }
735
736
737 /*
738 * Mark a vnode as having multiple hard links. HFS makes use of this
739 * because it keeps track of each link separately, and wants to know
740 * which link was actually used.
741 *
742 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
743 * so that HFS can post-process the lookup. Also, volfs will call
744 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
745 */
746 void vnode_setmultipath(vnode_t vp)
747 {
748 vnode_lock_spin(vp);
749
750 /*
751 * In theory, we're changing the vnode's identity as far as the
752 * name cache is concerned, so we ought to grab the name cache lock
753 * here. However, there is already a race, and grabbing the name
754 * cache lock only makes the race window slightly smaller.
755 *
756 * The race happens because the vnode already exists in the name
757 * cache, and could be found by one thread before another thread
758 * can set the hard link flag.
759 */
760
761 vp->v_flag |= VISHARDLINK;
762
763 vnode_unlock(vp);
764 }
765
766
767
768 /*
769 * backwards compatibility
770 */
771 void vnode_uncache_credentials(vnode_t vp)
772 {
773 vnode_uncache_authorized_action(vp, KAUTH_INVALIDATE_CACHED_RIGHTS);
774 }
775
776
777 /*
778 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
779 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
780 * we use this lock so that we can look at the v_cred and v_authorized_actions
781 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
782 * which is the super-hot path... if we are updating the authorized actions for this
783 * vnode, we are already in the super-slow and far less frequented path so its not
784 * that bad that we take the lock exclusive for this case... of course we strive
785 * to hold it for the minimum amount of time possible
786 */
787
788 void vnode_uncache_authorized_action(vnode_t vp, kauth_action_t action)
789 {
790 kauth_cred_t tcred = NOCRED;
791
792 NAME_CACHE_LOCK();
793
794 vp->v_authorized_actions &= ~action;
795
796 if (action == KAUTH_INVALIDATE_CACHED_RIGHTS &&
797 IS_VALID_CRED(vp->v_cred)) {
798 /*
799 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
800 */
801 tcred = vp->v_cred;
802 vp->v_cred = NOCRED;
803 }
804 NAME_CACHE_UNLOCK();
805
806 if (tcred != NOCRED)
807 kauth_cred_unref(&tcred);
808 }
809
810
811 boolean_t vnode_cache_is_authorized(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
812 {
813 kauth_cred_t ucred;
814 boolean_t retval = FALSE;
815
816 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
817 /*
818 * a TTL is enabled on the rights cache... handle it here
819 * a TTL of 0 indicates that no rights should be cached
820 */
821 if (vp->v_mount->mnt_authcache_ttl) {
822 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
823 /*
824 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
825 * we will only allow a SEARCH right on a directory to be cached...
826 * that cached right always has a default TTL associated with it
827 */
828 if (action != KAUTH_VNODE_SEARCH || vp->v_type != VDIR)
829 vp = NULLVP;
830 }
831 if (vp != NULLVP && vnode_cache_is_stale(vp) == TRUE) {
832 vnode_uncache_authorized_action(vp, vp->v_authorized_actions);
833 vp = NULLVP;
834 }
835 } else
836 vp = NULLVP;
837 }
838 if (vp != NULLVP) {
839 ucred = vfs_context_ucred(ctx);
840
841 NAME_CACHE_LOCK_SHARED();
842
843 if (vp->v_cred == ucred && (vp->v_authorized_actions & action) == action)
844 retval = TRUE;
845
846 NAME_CACHE_UNLOCK();
847 }
848 return retval;
849 }
850
851
852 void vnode_cache_authorized_action(vnode_t vp, vfs_context_t ctx, kauth_action_t action)
853 {
854 kauth_cred_t tcred = NOCRED;
855 kauth_cred_t ucred;
856 struct timeval tv;
857 boolean_t ttl_active = FALSE;
858
859 ucred = vfs_context_ucred(ctx);
860
861 if (!IS_VALID_CRED(ucred) || action == 0)
862 return;
863
864 if ( (vp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
865 /*
866 * a TTL is enabled on the rights cache... handle it here
867 * a TTL of 0 indicates that no rights should be cached
868 */
869 if (vp->v_mount->mnt_authcache_ttl == 0)
870 return;
871
872 if ( !(vp->v_mount->mnt_kern_flag & MNTK_AUTH_CACHE_TTL) ) {
873 /*
874 * only cache SEARCH action for filesystems marked
875 * MNTK_AUTH_OPAQUE on VDIRs...
876 * the lookup_path code will time these out
877 */
878 if ( (action & ~KAUTH_VNODE_SEARCH) || vp->v_type != VDIR )
879 return;
880 }
881 ttl_active = TRUE;
882
883 microuptime(&tv);
884 }
885 NAME_CACHE_LOCK();
886
887 if (vp->v_cred != ucred) {
888 kauth_cred_ref(ucred);
889 /*
890 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
891 */
892 tcred = vp->v_cred;
893 vp->v_cred = ucred;
894 vp->v_authorized_actions = 0;
895 }
896 if (ttl_active == TRUE && vp->v_authorized_actions == 0) {
897 /*
898 * only reset the timestamnp on the
899 * first authorization cached after the previous
900 * timer has expired or we're switching creds...
901 * 'vnode_cache_is_authorized' will clear the
902 * authorized actions if the TTL is active and
903 * it has expired
904 */
905 vp->v_cred_timestamp = tv.tv_sec;
906 }
907 vp->v_authorized_actions |= action;
908
909 NAME_CACHE_UNLOCK();
910
911 if (IS_VALID_CRED(tcred))
912 kauth_cred_unref(&tcred);
913 }
914
915
916 boolean_t vnode_cache_is_stale(vnode_t vp)
917 {
918 struct timeval tv;
919 boolean_t retval;
920
921 microuptime(&tv);
922
923 if ((tv.tv_sec - vp->v_cred_timestamp) > vp->v_mount->mnt_authcache_ttl)
924 retval = TRUE;
925 else
926 retval = FALSE;
927
928 return retval;
929 }
930
931
932
933 /*
934 * Returns: 0 Success
935 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
936 * This errno value should not be seen by anyone outside of the kernel.
937 */
938 int
939 cache_lookup_path(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
940 vfs_context_t ctx, int *trailing_slash, int *dp_authorized, vnode_t last_dp)
941 {
942 char *cp; /* pointer into pathname argument */
943 int vid;
944 int vvid = 0; /* protected by vp != NULLVP */
945 vnode_t vp = NULLVP;
946 vnode_t tdp = NULLVP;
947 kauth_cred_t ucred;
948 boolean_t ttl_enabled = FALSE;
949 struct timeval tv;
950 mount_t mp;
951 unsigned int hash;
952 int error = 0;
953
954 ucred = vfs_context_ucred(ctx);
955 *trailing_slash = 0;
956
957 NAME_CACHE_LOCK_SHARED();
958
959 if ( dp->v_mount && (dp->v_mount->mnt_kern_flag & (MNTK_AUTH_OPAQUE | MNTK_AUTH_CACHE_TTL)) ) {
960 ttl_enabled = TRUE;
961 microuptime(&tv);
962 }
963 for (;;) {
964 /*
965 * Search a directory.
966 *
967 * The cn_hash value is for use by cache_lookup
968 * The last component of the filename is left accessible via
969 * cnp->cn_nameptr for callers that need the name.
970 */
971 hash = 0;
972 cp = cnp->cn_nameptr;
973
974 while (*cp && (*cp != '/')) {
975 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
976 }
977 /*
978 * the crc generator can legitimately generate
979 * a 0... however, 0 for us means that we
980 * haven't computed a hash, so use 1 instead
981 */
982 if (hash == 0)
983 hash = 1;
984 cnp->cn_hash = hash;
985 cnp->cn_namelen = cp - cnp->cn_nameptr;
986
987 ndp->ni_pathlen -= cnp->cn_namelen;
988 ndp->ni_next = cp;
989
990 /*
991 * Replace multiple slashes by a single slash and trailing slashes
992 * by a null. This must be done before VNOP_LOOKUP() because some
993 * fs's don't know about trailing slashes. Remember if there were
994 * trailing slashes to handle symlinks, existing non-directories
995 * and non-existing files that won't be directories specially later.
996 */
997 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
998 cp++;
999 ndp->ni_pathlen--;
1000
1001 if (*cp == '\0') {
1002 *trailing_slash = 1;
1003 *ndp->ni_next = '\0';
1004 }
1005 }
1006 ndp->ni_next = cp;
1007
1008 cnp->cn_flags &= ~(MAKEENTRY | ISLASTCN | ISDOTDOT);
1009
1010 if (*cp == '\0')
1011 cnp->cn_flags |= ISLASTCN;
1012
1013 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1014 cnp->cn_flags |= ISDOTDOT;
1015
1016 *dp_authorized = 0;
1017 #if NAMEDRSRCFORK
1018 /*
1019 * Process a request for a file's resource fork.
1020 *
1021 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1022 */
1023 if ((ndp->ni_pathlen == sizeof(_PATH_RSRCFORKSPEC)) &&
1024 (cp[1] == '.' && cp[2] == '.') &&
1025 bcmp(cp, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
1026 /* Skip volfs file systems that don't support native streams. */
1027 if ((dp->v_mount != NULL) &&
1028 (dp->v_mount->mnt_flag & MNT_DOVOLFS) &&
1029 (dp->v_mount->mnt_kern_flag & MNTK_NAMED_STREAMS) == 0) {
1030 goto skiprsrcfork;
1031 }
1032 cnp->cn_flags |= CN_WANTSRSRCFORK;
1033 cnp->cn_flags |= ISLASTCN;
1034 ndp->ni_next[0] = '\0';
1035 ndp->ni_pathlen = 1;
1036 }
1037 skiprsrcfork:
1038 #endif
1039
1040 #if CONFIG_MACF
1041
1042 /*
1043 * Name cache provides authorization caching (see below)
1044 * that will short circuit MAC checks in lookup().
1045 * We must perform MAC check here. On denial
1046 * dp_authorized will remain 0 and second check will
1047 * be perfomed in lookup().
1048 */
1049 if (!(cnp->cn_flags & DONOTAUTH)) {
1050 error = mac_vnode_check_lookup(ctx, dp, cnp);
1051 if (error) {
1052 NAME_CACHE_UNLOCK();
1053 goto errorout;
1054 }
1055 }
1056 #endif /* MAC */
1057 if (ttl_enabled && ((tv.tv_sec - dp->v_cred_timestamp) > dp->v_mount->mnt_authcache_ttl))
1058 break;
1059
1060 /*
1061 * NAME_CACHE_LOCK holds these fields stable
1062 */
1063 if ((dp->v_cred != ucred || !(dp->v_authorized_actions & KAUTH_VNODE_SEARCH)) &&
1064 !(dp->v_authorized_actions & KAUTH_VNODE_SEARCHBYANYONE))
1065 break;
1066
1067 /*
1068 * indicate that we're allowed to traverse this directory...
1069 * even if we fail the cache lookup or decide to bail for
1070 * some other reason, this information is valid and is used
1071 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1072 */
1073 *dp_authorized = 1;
1074
1075 if ( (cnp->cn_flags & (ISLASTCN | ISDOTDOT)) ) {
1076 if (cnp->cn_nameiop != LOOKUP)
1077 break;
1078 if (cnp->cn_flags & (LOCKPARENT | NOCACHE))
1079 break;
1080 if (cnp->cn_flags & ISDOTDOT) {
1081 /*
1082 * Force directory hardlinks to go to
1083 * file system for ".." requests.
1084 */
1085 if (dp && (dp->v_flag & VISHARDLINK)) {
1086 break;
1087 }
1088 /*
1089 * Quit here only if we can't use
1090 * the parent directory pointer or
1091 * don't have one. Otherwise, we'll
1092 * use it below.
1093 */
1094 if ((dp->v_flag & VROOT) ||
1095 dp == ndp->ni_rootdir ||
1096 dp->v_parent == NULLVP)
1097 break;
1098 }
1099 }
1100
1101 /*
1102 * "." and ".." aren't supposed to be cached, so check
1103 * for them before checking the cache.
1104 */
1105 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
1106 vp = dp;
1107 else if ( (cnp->cn_flags & ISDOTDOT) )
1108 vp = dp->v_parent;
1109 else {
1110 if ( (vp = cache_lookup_locked(dp, cnp)) == NULLVP)
1111 break;
1112
1113 if ( (vp->v_flag & VISHARDLINK) ) {
1114 /*
1115 * The file system wants a VNOP_LOOKUP on this vnode
1116 */
1117 vp = NULL;
1118 break;
1119 }
1120 }
1121 if ( (cnp->cn_flags & ISLASTCN) )
1122 break;
1123
1124 if (vp->v_type != VDIR) {
1125 if (vp->v_type != VLNK)
1126 vp = NULL;
1127 break;
1128 }
1129 if ( (mp = vp->v_mountedhere) && ((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
1130
1131 if (mp->mnt_realrootvp == NULLVP || mp->mnt_generation != mount_generation ||
1132 mp->mnt_realrootvp_vid != mp->mnt_realrootvp->v_id)
1133 break;
1134 vp = mp->mnt_realrootvp;
1135 }
1136 dp = vp;
1137 vp = NULLVP;
1138
1139 cnp->cn_nameptr = ndp->ni_next + 1;
1140 ndp->ni_pathlen--;
1141 while (*cnp->cn_nameptr == '/') {
1142 cnp->cn_nameptr++;
1143 ndp->ni_pathlen--;
1144 }
1145 }
1146 if (vp != NULLVP)
1147 vvid = vp->v_id;
1148 vid = dp->v_id;
1149
1150 NAME_CACHE_UNLOCK();
1151
1152 if ((vp != NULLVP) && (vp->v_type != VLNK) &&
1153 ((cnp->cn_flags & (ISLASTCN | LOCKPARENT | WANTPARENT | SAVESTART)) == ISLASTCN)) {
1154 /*
1155 * if we've got a child and it's the last component, and
1156 * the lookup doesn't need to return the parent then we
1157 * can skip grabbing an iocount on the parent, since all
1158 * we're going to do with it is a vnode_put just before
1159 * we return from 'lookup'. If it's a symbolic link,
1160 * we need the parent in case the link happens to be
1161 * a relative pathname.
1162 */
1163 tdp = dp;
1164 dp = NULLVP;
1165 } else {
1166 need_dp:
1167 /*
1168 * return the last directory we looked at
1169 * with an io reference held. If it was the one passed
1170 * in as a result of the last iteration of VNOP_LOOKUP,
1171 * it should already hold an io ref. No need to increase ref.
1172 */
1173 if (last_dp != dp){
1174
1175 if (dp == ndp->ni_usedvp) {
1176 /*
1177 * if this vnode matches the one passed in via USEDVP
1178 * than this context already holds an io_count... just
1179 * use vnode_get to get an extra ref for lookup to play
1180 * with... can't use the getwithvid variant here because
1181 * it will block behind a vnode_drain which would result
1182 * in a deadlock (since we already own an io_count that the
1183 * vnode_drain is waiting on)... vnode_get grabs the io_count
1184 * immediately w/o waiting... it always succeeds
1185 */
1186 vnode_get(dp);
1187 } else if ( (vnode_getwithvid(dp, vid)) ) {
1188 /*
1189 * failure indicates the vnode
1190 * changed identity or is being
1191 * TERMINATED... in either case
1192 * punt this lookup.
1193 *
1194 * don't necessarily return ENOENT, though, because
1195 * we really want to go back to disk and make sure it's
1196 * there or not if someone else is changing this
1197 * vnode.
1198 */
1199 error = ERECYCLE;
1200 goto errorout;
1201 }
1202 }
1203 }
1204 if (vp != NULLVP) {
1205 if ( (vnode_getwithvid(vp, vvid)) ) {
1206 vp = NULLVP;
1207
1208 /*
1209 * can't get reference on the vp we'd like
1210 * to return... if we didn't grab a reference
1211 * on the directory (due to fast path bypass),
1212 * then we need to do it now... we can't return
1213 * with both ni_dvp and ni_vp NULL, and no
1214 * error condition
1215 */
1216 if (dp == NULLVP) {
1217 dp = tdp;
1218 goto need_dp;
1219 }
1220 }
1221 }
1222 ndp->ni_dvp = dp;
1223 ndp->ni_vp = vp;
1224
1225 errorout:
1226 /*
1227 * If we came into cache_lookup_path after an iteration of the lookup loop that
1228 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1229 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1230 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1231 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1232 * the case that we got to a subsequent path component and this previous vnode is
1233 * no longer needed. We can then drop the io ref on it.
1234 */
1235 if ((last_dp != NULLVP) && (last_dp != ndp->ni_dvp)){
1236 vnode_put(last_dp);
1237 }
1238
1239 //initialized to 0, should be the same if no error cases occurred.
1240 return error;
1241 }
1242
1243
1244 static vnode_t
1245 cache_lookup_locked(vnode_t dvp, struct componentname *cnp)
1246 {
1247 struct namecache *ncp;
1248 struct nchashhead *ncpp;
1249 long namelen = cnp->cn_namelen;
1250 unsigned int hashval = (cnp->cn_hash & NCHASHMASK);
1251
1252 ncpp = NCHHASH(dvp, cnp->cn_hash);
1253 LIST_FOREACH(ncp, ncpp, nc_hash) {
1254 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1255 if (memcmp(ncp->nc_name, cnp->cn_nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1256 break;
1257 }
1258 }
1259 if (ncp == 0) {
1260 /*
1261 * We failed to find an entry
1262 */
1263 NCHSTAT(ncs_miss);
1264 return (NULL);
1265 }
1266 NCHSTAT(ncs_goodhits);
1267
1268 return (ncp->nc_vp);
1269 }
1270
1271
1272 //
1273 // Have to take a len argument because we may only need to
1274 // hash part of a componentname.
1275 //
1276 static unsigned int
1277 hash_string(const char *cp, int len)
1278 {
1279 unsigned hash = 0;
1280
1281 if (len) {
1282 while (len--) {
1283 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
1284 }
1285 } else {
1286 while (*cp != '\0') {
1287 hash = crc32tab[((hash >> 24) ^ (unsigned char)*cp++)] ^ hash << 8;
1288 }
1289 }
1290 /*
1291 * the crc generator can legitimately generate
1292 * a 0... however, 0 for us means that we
1293 * haven't computed a hash, so use 1 instead
1294 */
1295 if (hash == 0)
1296 hash = 1;
1297 return hash;
1298 }
1299
1300
1301 /*
1302 * Lookup an entry in the cache
1303 *
1304 * We don't do this if the segment name is long, simply so the cache
1305 * can avoid holding long names (which would either waste space, or
1306 * add greatly to the complexity).
1307 *
1308 * Lookup is called with dvp pointing to the directory to search,
1309 * cnp pointing to the name of the entry being sought. If the lookup
1310 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1311 * returned. If the lookup determines that the name does not exist
1312 * (negative cacheing), a status of ENOENT is returned. If the lookup
1313 * fails, a status of zero is returned.
1314 */
1315
1316 int
1317 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1318 {
1319 struct namecache *ncp;
1320 struct nchashhead *ncpp;
1321 long namelen = cnp->cn_namelen;
1322 unsigned int hashval;
1323 boolean_t have_exclusive = FALSE;
1324 uint32_t vid;
1325 vnode_t vp;
1326
1327 if (cnp->cn_hash == 0)
1328 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1329 hashval = (cnp->cn_hash & NCHASHMASK);
1330
1331 NAME_CACHE_LOCK_SHARED();
1332
1333 relook:
1334 ncpp = NCHHASH(dvp, cnp->cn_hash);
1335 LIST_FOREACH(ncp, ncpp, nc_hash) {
1336 if ((ncp->nc_dvp == dvp) && (ncp->nc_hashval == hashval)) {
1337 if (memcmp(ncp->nc_name, cnp->cn_nameptr, namelen) == 0 && ncp->nc_name[namelen] == 0)
1338 break;
1339 }
1340 }
1341 /* We failed to find an entry */
1342 if (ncp == 0) {
1343 NCHSTAT(ncs_miss);
1344 NAME_CACHE_UNLOCK();
1345 return (0);
1346 }
1347
1348 /* We don't want to have an entry, so dump it */
1349 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1350 if (have_exclusive == TRUE) {
1351 NCHSTAT(ncs_badhits);
1352 cache_delete(ncp, 1);
1353 NAME_CACHE_UNLOCK();
1354 return (0);
1355 }
1356 NAME_CACHE_UNLOCK();
1357 NAME_CACHE_LOCK();
1358 have_exclusive = TRUE;
1359 goto relook;
1360 }
1361 vp = ncp->nc_vp;
1362
1363 /* We found a "positive" match, return the vnode */
1364 if (vp) {
1365 NCHSTAT(ncs_goodhits);
1366
1367 vid = vp->v_id;
1368 NAME_CACHE_UNLOCK();
1369
1370 if (vnode_getwithvid(vp, vid)) {
1371 #if COLLECT_STATS
1372 NAME_CACHE_LOCK();
1373 NCHSTAT(ncs_badvid);
1374 NAME_CACHE_UNLOCK();
1375 #endif
1376 return (0);
1377 }
1378 *vpp = vp;
1379 return (-1);
1380 }
1381
1382 /* We found a negative match, and want to create it, so purge */
1383 if (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) {
1384 if (have_exclusive == TRUE) {
1385 NCHSTAT(ncs_badhits);
1386 cache_delete(ncp, 1);
1387 NAME_CACHE_UNLOCK();
1388 return (0);
1389 }
1390 NAME_CACHE_UNLOCK();
1391 NAME_CACHE_LOCK();
1392 have_exclusive = TRUE;
1393 goto relook;
1394 }
1395
1396 /*
1397 * We found a "negative" match, ENOENT notifies client of this match.
1398 * The nc_whiteout field records whether this is a whiteout.
1399 */
1400 NCHSTAT(ncs_neghits);
1401
1402 if (ncp->nc_whiteout)
1403 cnp->cn_flags |= ISWHITEOUT;
1404 NAME_CACHE_UNLOCK();
1405 return (ENOENT);
1406 }
1407
1408 const char *
1409 cache_enter_create(vnode_t dvp, vnode_t vp, struct componentname *cnp)
1410 {
1411 const char *strname;
1412
1413 if (cnp->cn_hash == 0)
1414 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1415
1416 /*
1417 * grab 2 references on the string entered
1418 * one for the cache_enter_locked to consume
1419 * and the second to be consumed by v_name (vnode_create call point)
1420 */
1421 strname = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, TRUE, 0);
1422
1423 NAME_CACHE_LOCK();
1424
1425 cache_enter_locked(dvp, vp, cnp, strname);
1426
1427 NAME_CACHE_UNLOCK();
1428
1429 return (strname);
1430 }
1431
1432
1433 /*
1434 * Add an entry to the cache...
1435 * but first check to see if the directory
1436 * that this entry is to be associated with has
1437 * had any cache_purges applied since we took
1438 * our identity snapshot... this check needs to
1439 * be done behind the name cache lock
1440 */
1441 void
1442 cache_enter_with_gen(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, int gen)
1443 {
1444
1445 if (cnp->cn_hash == 0)
1446 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1447
1448 NAME_CACHE_LOCK();
1449
1450 if (dvp->v_nc_generation == gen)
1451 (void)cache_enter_locked(dvp, vp, cnp, NULL);
1452
1453 NAME_CACHE_UNLOCK();
1454 }
1455
1456
1457 /*
1458 * Add an entry to the cache.
1459 */
1460 void
1461 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1462 {
1463 const char *strname;
1464
1465 if (cnp->cn_hash == 0)
1466 cnp->cn_hash = hash_string(cnp->cn_nameptr, cnp->cn_namelen);
1467
1468 /*
1469 * grab 1 reference on the string entered
1470 * for the cache_enter_locked to consume
1471 */
1472 strname = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, FALSE, 0);
1473
1474 NAME_CACHE_LOCK();
1475
1476 cache_enter_locked(dvp, vp, cnp, strname);
1477
1478 NAME_CACHE_UNLOCK();
1479 }
1480
1481
1482 static void
1483 cache_enter_locked(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, const char *strname)
1484 {
1485 struct namecache *ncp, *negp;
1486 struct nchashhead *ncpp;
1487
1488 /*
1489 * if the entry is for -ve caching vp is null
1490 */
1491 if ((vp != NULLVP) && (LIST_FIRST(&vp->v_nclinks))) {
1492 /*
1493 * someone beat us to the punch..
1494 * this vnode is already in the cache
1495 */
1496 if (strname != NULL)
1497 vfs_removename(strname);
1498 return;
1499 }
1500 /*
1501 * We allocate a new entry if we are less than the maximum
1502 * allowed and the one at the front of the list is in use.
1503 * Otherwise we use the one at the front of the list.
1504 */
1505 if (numcache < desiredNodes &&
1506 ((ncp = nchead.tqh_first) == NULL ||
1507 ncp->nc_hash.le_prev != 0)) {
1508 /*
1509 * Allocate one more entry
1510 */
1511 ncp = (struct namecache *)_MALLOC_ZONE(sizeof(*ncp), M_CACHE, M_WAITOK);
1512 numcache++;
1513 } else {
1514 /*
1515 * reuse an old entry
1516 */
1517 ncp = TAILQ_FIRST(&nchead);
1518 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1519
1520 if (ncp->nc_hash.le_prev != 0) {
1521 /*
1522 * still in use... we need to
1523 * delete it before re-using it
1524 */
1525 NCHSTAT(ncs_stolen);
1526 cache_delete(ncp, 0);
1527 }
1528 }
1529 NCHSTAT(ncs_enters);
1530
1531 /*
1532 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1533 */
1534 ncp->nc_vp = vp;
1535 ncp->nc_dvp = dvp;
1536 ncp->nc_hashval = cnp->cn_hash;
1537 ncp->nc_whiteout = FALSE;
1538
1539 if (strname == NULL)
1540 ncp->nc_name = add_name_internal(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, FALSE, 0);
1541 else
1542 ncp->nc_name = strname;
1543 /*
1544 * make us the newest entry in the cache
1545 * i.e. we'll be the last to be stolen
1546 */
1547 TAILQ_INSERT_TAIL(&nchead, ncp, nc_entry);
1548
1549 ncpp = NCHHASH(dvp, cnp->cn_hash);
1550 #if DIAGNOSTIC
1551 {
1552 struct namecache *p;
1553
1554 for (p = ncpp->lh_first; p != 0; p = p->nc_hash.le_next)
1555 if (p == ncp)
1556 panic("cache_enter: duplicate");
1557 }
1558 #endif
1559 /*
1560 * make us available to be found via lookup
1561 */
1562 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1563
1564 if (vp) {
1565 /*
1566 * add to the list of name cache entries
1567 * that point at vp
1568 */
1569 LIST_INSERT_HEAD(&vp->v_nclinks, ncp, nc_un.nc_link);
1570 } else {
1571 /*
1572 * this is a negative cache entry (vp == NULL)
1573 * stick it on the negative cache list
1574 * and record the whiteout state
1575 */
1576 TAILQ_INSERT_TAIL(&neghead, ncp, nc_un.nc_negentry);
1577
1578 if (cnp->cn_flags & ISWHITEOUT)
1579 ncp->nc_whiteout = TRUE;
1580 ncs_negtotal++;
1581
1582 if (ncs_negtotal > desiredNegNodes) {
1583 /*
1584 * if we've reached our desired limit
1585 * of negative cache entries, delete
1586 * the oldest
1587 */
1588 negp = TAILQ_FIRST(&neghead);
1589 cache_delete(negp, 1);
1590 }
1591 }
1592 /*
1593 * add us to the list of name cache entries that
1594 * are children of dvp
1595 */
1596 LIST_INSERT_HEAD(&dvp->v_ncchildren, ncp, nc_child);
1597 }
1598
1599
1600 /*
1601 * Initialize CRC-32 remainder table.
1602 */
1603 static void init_crc32(void)
1604 {
1605 /*
1606 * the CRC-32 generator polynomial is:
1607 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1608 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1609 */
1610 unsigned int crc32_polynomial = 0x04c11db7;
1611 unsigned int i,j;
1612
1613 /*
1614 * pre-calculate the CRC-32 remainder for each possible octet encoding
1615 */
1616 for (i = 0; i < 256; i++) {
1617 unsigned int crc_rem = i << 24;
1618
1619 for (j = 0; j < 8; j++) {
1620 if (crc_rem & 0x80000000)
1621 crc_rem = (crc_rem << 1) ^ crc32_polynomial;
1622 else
1623 crc_rem = (crc_rem << 1);
1624 }
1625 crc32tab[i] = crc_rem;
1626 }
1627 }
1628
1629
1630 /*
1631 * Name cache initialization, from vfs_init() when we are booting
1632 */
1633 void
1634 nchinit(void)
1635 {
1636 int i;
1637
1638 desiredNegNodes = (desiredvnodes / 10);
1639 desiredNodes = desiredvnodes + desiredNegNodes;
1640
1641 TAILQ_INIT(&nchead);
1642 TAILQ_INIT(&neghead);
1643
1644 init_crc32();
1645
1646 nchashtbl = hashinit(MAX(CONFIG_NC_HASH, (2 *desiredNodes)), M_CACHE, &nchash);
1647 nchashmask = nchash;
1648 nchash++;
1649
1650 init_string_table();
1651
1652 /* Allocate name cache lock group attribute and group */
1653 namecache_lck_grp_attr= lck_grp_attr_alloc_init();
1654
1655 namecache_lck_grp = lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr);
1656
1657 /* Allocate name cache lock attribute */
1658 namecache_lck_attr = lck_attr_alloc_init();
1659
1660 /* Allocate name cache lock */
1661 namecache_rw_lock = lck_rw_alloc_init(namecache_lck_grp, namecache_lck_attr);
1662
1663
1664 /* Allocate string cache lock group attribute and group */
1665 strcache_lck_grp_attr= lck_grp_attr_alloc_init();
1666
1667 strcache_lck_grp = lck_grp_alloc_init("String Cache", strcache_lck_grp_attr);
1668
1669 /* Allocate string cache lock attribute */
1670 strcache_lck_attr = lck_attr_alloc_init();
1671
1672 /* Allocate string cache lock */
1673 strtable_rw_lock = lck_rw_alloc_init(strcache_lck_grp, strcache_lck_attr);
1674
1675 for (i = 0; i < NUM_STRCACHE_LOCKS; i++)
1676 lck_mtx_init(&strcache_mtx_locks[i], strcache_lck_grp, strcache_lck_attr);
1677 }
1678
1679 void
1680 name_cache_lock_shared(void)
1681 {
1682 lck_rw_lock_shared(namecache_rw_lock);
1683 }
1684
1685 void
1686 name_cache_lock(void)
1687 {
1688 lck_rw_lock_exclusive(namecache_rw_lock);
1689 }
1690
1691 void
1692 name_cache_unlock(void)
1693 {
1694 lck_rw_done(namecache_rw_lock);
1695 }
1696
1697
1698 int
1699 resize_namecache(u_int newsize)
1700 {
1701 struct nchashhead *new_table;
1702 struct nchashhead *old_table;
1703 struct nchashhead *old_head, *head;
1704 struct namecache *entry, *next;
1705 uint32_t i, hashval;
1706 int dNodes, dNegNodes;
1707 u_long new_size, old_size;
1708
1709 dNegNodes = (newsize / 10);
1710 dNodes = newsize + dNegNodes;
1711
1712 // we don't support shrinking yet
1713 if (dNodes <= desiredNodes) {
1714 return 0;
1715 }
1716 new_table = hashinit(2 * dNodes, M_CACHE, &nchashmask);
1717 new_size = nchashmask + 1;
1718
1719 if (new_table == NULL) {
1720 return ENOMEM;
1721 }
1722
1723 NAME_CACHE_LOCK();
1724 // do the switch!
1725 old_table = nchashtbl;
1726 nchashtbl = new_table;
1727 old_size = nchash;
1728 nchash = new_size;
1729
1730 // walk the old table and insert all the entries into
1731 // the new table
1732 //
1733 for(i=0; i < old_size; i++) {
1734 old_head = &old_table[i];
1735 for (entry=old_head->lh_first; entry != NULL; entry=next) {
1736 //
1737 // XXXdbg - Beware: this assumes that hash_string() does
1738 // the same thing as what happens in
1739 // lookup() over in vfs_lookup.c
1740 hashval = hash_string(entry->nc_name, 0);
1741 entry->nc_hashval = hashval;
1742 head = NCHHASH(entry->nc_dvp, hashval);
1743
1744 next = entry->nc_hash.le_next;
1745 LIST_INSERT_HEAD(head, entry, nc_hash);
1746 }
1747 }
1748 desiredNodes = dNodes;
1749 desiredNegNodes = dNegNodes;
1750
1751 NAME_CACHE_UNLOCK();
1752 FREE(old_table, M_CACHE);
1753
1754 return 0;
1755 }
1756
1757 static void
1758 cache_delete(struct namecache *ncp, int age_entry)
1759 {
1760 NCHSTAT(ncs_deletes);
1761
1762 if (ncp->nc_vp) {
1763 LIST_REMOVE(ncp, nc_un.nc_link);
1764 } else {
1765 TAILQ_REMOVE(&neghead, ncp, nc_un.nc_negentry);
1766 ncs_negtotal--;
1767 }
1768 LIST_REMOVE(ncp, nc_child);
1769
1770 LIST_REMOVE(ncp, nc_hash);
1771 /*
1772 * this field is used to indicate
1773 * that the entry is in use and
1774 * must be deleted before it can
1775 * be reused...
1776 */
1777 ncp->nc_hash.le_prev = NULL;
1778
1779 if (age_entry) {
1780 /*
1781 * make it the next one available
1782 * for cache_enter's use
1783 */
1784 TAILQ_REMOVE(&nchead, ncp, nc_entry);
1785 TAILQ_INSERT_HEAD(&nchead, ncp, nc_entry);
1786 }
1787 vfs_removename(ncp->nc_name);
1788 ncp->nc_name = NULL;
1789 }
1790
1791
1792 /*
1793 * purge the entry associated with the
1794 * specified vnode from the name cache
1795 */
1796 void
1797 cache_purge(vnode_t vp)
1798 {
1799 struct namecache *ncp;
1800 kauth_cred_t tcred = NULL;
1801
1802 if ((LIST_FIRST(&vp->v_nclinks) == NULL) && (LIST_FIRST(&vp->v_ncchildren) == NULL) && (vp->v_cred == NOCRED))
1803 return;
1804
1805 NAME_CACHE_LOCK();
1806
1807 if (vp->v_parent)
1808 vp->v_parent->v_nc_generation++;
1809
1810 while ( (ncp = LIST_FIRST(&vp->v_nclinks)) )
1811 cache_delete(ncp, 1);
1812
1813 while ( (ncp = LIST_FIRST(&vp->v_ncchildren)) )
1814 cache_delete(ncp, 1);
1815
1816 /*
1817 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1818 */
1819 tcred = vp->v_cred;
1820 vp->v_cred = NOCRED;
1821 vp->v_authorized_actions = 0;
1822
1823 NAME_CACHE_UNLOCK();
1824
1825 if (IS_VALID_CRED(tcred))
1826 kauth_cred_unref(&tcred);
1827 }
1828
1829 /*
1830 * Purge all negative cache entries that are children of the
1831 * given vnode. A case-insensitive file system (or any file
1832 * system that has multiple equivalent names for the same
1833 * directory entry) can use this when creating or renaming
1834 * to remove negative entries that may no longer apply.
1835 */
1836 void
1837 cache_purge_negatives(vnode_t vp)
1838 {
1839 struct namecache *ncp, *next_ncp;
1840
1841 NAME_CACHE_LOCK();
1842
1843 LIST_FOREACH_SAFE(ncp, &vp->v_ncchildren, nc_child, next_ncp)
1844 if (ncp->nc_vp == NULL)
1845 cache_delete(ncp , 1);
1846
1847 NAME_CACHE_UNLOCK();
1848 }
1849
1850 /*
1851 * Flush all entries referencing a particular filesystem.
1852 *
1853 * Since we need to check it anyway, we will flush all the invalid
1854 * entries at the same time.
1855 */
1856 void
1857 cache_purgevfs(struct mount *mp)
1858 {
1859 struct nchashhead *ncpp;
1860 struct namecache *ncp;
1861
1862 NAME_CACHE_LOCK();
1863 /* Scan hash tables for applicable entries */
1864 for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
1865 restart:
1866 for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
1867 if (ncp->nc_dvp->v_mount == mp) {
1868 cache_delete(ncp, 0);
1869 goto restart;
1870 }
1871 }
1872 }
1873 NAME_CACHE_UNLOCK();
1874 }
1875
1876
1877
1878 //
1879 // String ref routines
1880 //
1881 static LIST_HEAD(stringhead, string_t) *string_ref_table;
1882 static u_long string_table_mask;
1883 static uint32_t filled_buckets=0;
1884
1885
1886 typedef struct string_t {
1887 LIST_ENTRY(string_t) hash_chain;
1888 const char *str;
1889 uint32_t refcount;
1890 } string_t;
1891
1892
1893 static void
1894 resize_string_ref_table(void)
1895 {
1896 struct stringhead *new_table;
1897 struct stringhead *old_table;
1898 struct stringhead *old_head, *head;
1899 string_t *entry, *next;
1900 uint32_t i, hashval;
1901 u_long new_mask, old_mask;
1902
1903 /*
1904 * need to hold the table lock exclusively
1905 * in order to grow the table... need to recheck
1906 * the need to resize again after we've taken
1907 * the lock exclusively in case some other thread
1908 * beat us to the punch
1909 */
1910 lck_rw_lock_exclusive(strtable_rw_lock);
1911
1912 if (4 * filled_buckets < ((string_table_mask + 1) * 3)) {
1913 lck_rw_done(strtable_rw_lock);
1914 return;
1915 }
1916 new_table = hashinit((string_table_mask + 1) * 2, M_CACHE, &new_mask);
1917
1918 if (new_table == NULL) {
1919 printf("failed to resize the hash table.\n");
1920 lck_rw_done(strtable_rw_lock);
1921 return;
1922 }
1923
1924 // do the switch!
1925 old_table = string_ref_table;
1926 string_ref_table = new_table;
1927 old_mask = string_table_mask;
1928 string_table_mask = new_mask;
1929 filled_buckets = 0;
1930
1931 // walk the old table and insert all the entries into
1932 // the new table
1933 //
1934 for (i = 0; i <= old_mask; i++) {
1935 old_head = &old_table[i];
1936 for (entry = old_head->lh_first; entry != NULL; entry = next) {
1937 hashval = hash_string((const char *)entry->str, 0);
1938 head = &string_ref_table[hashval & string_table_mask];
1939 if (head->lh_first == NULL) {
1940 filled_buckets++;
1941 }
1942 next = entry->hash_chain.le_next;
1943 LIST_INSERT_HEAD(head, entry, hash_chain);
1944 }
1945 }
1946 lck_rw_done(strtable_rw_lock);
1947
1948 FREE(old_table, M_CACHE);
1949 }
1950
1951
1952 static void
1953 init_string_table(void)
1954 {
1955 string_ref_table = hashinit(CONFIG_VFS_NAMES, M_CACHE, &string_table_mask);
1956 }
1957
1958
1959 const char *
1960 vfs_addname(const char *name, uint32_t len, u_int hashval, u_int flags)
1961 {
1962 return (add_name_internal(name, len, hashval, FALSE, flags));
1963 }
1964
1965
1966 static const char *
1967 add_name_internal(const char *name, uint32_t len, u_int hashval, boolean_t need_extra_ref, __unused u_int flags)
1968 {
1969 struct stringhead *head;
1970 string_t *entry;
1971 uint32_t chain_len = 0;
1972 uint32_t hash_index;
1973 uint32_t lock_index;
1974 char *ptr;
1975
1976 if (hashval == 0) {
1977 hashval = hash_string(name, 0);
1978 }
1979 /*
1980 * if the length already accounts for the null-byte, then
1981 * subtract one so later on we don't index past the end
1982 * of the string.
1983 */
1984 if (len > 0 && name[len-1] == '\0') {
1985 len--;
1986 }
1987 /*
1988 * take this lock 'shared' to keep the hash stable
1989 * if someone else decides to grow the pool they
1990 * will take this lock exclusively
1991 */
1992 lck_rw_lock_shared(strtable_rw_lock);
1993
1994 /*
1995 * If the table gets more than 3/4 full, resize it
1996 */
1997 if (4 * filled_buckets >= ((string_table_mask + 1) * 3)) {
1998 lck_rw_done(strtable_rw_lock);
1999
2000 resize_string_ref_table();
2001
2002 lck_rw_lock_shared(strtable_rw_lock);
2003 }
2004 hash_index = hashval & string_table_mask;
2005 lock_index = hash_index % NUM_STRCACHE_LOCKS;
2006
2007 head = &string_ref_table[hash_index];
2008
2009 lck_mtx_lock_spin(&strcache_mtx_locks[lock_index]);
2010
2011 for (entry = head->lh_first; entry != NULL; chain_len++, entry = entry->hash_chain.le_next) {
2012 if (memcmp(entry->str, name, len) == 0 && entry->str[len] == 0) {
2013 entry->refcount++;
2014 break;
2015 }
2016 }
2017 if (entry == NULL) {
2018 lck_mtx_convert_spin(&strcache_mtx_locks[lock_index]);
2019 /*
2020 * it wasn't already there so add it.
2021 */
2022 MALLOC(entry, string_t *, sizeof(string_t) + len + 1, M_TEMP, M_WAITOK);
2023
2024 if (head->lh_first == NULL) {
2025 OSAddAtomic(1, &filled_buckets);
2026 }
2027 ptr = (char *)((char *)entry + sizeof(string_t));
2028 strncpy(ptr, name, len);
2029 ptr[len] = '\0';
2030 entry->str = ptr;
2031 entry->refcount = 1;
2032 LIST_INSERT_HEAD(head, entry, hash_chain);
2033 }
2034 if (need_extra_ref == TRUE)
2035 entry->refcount++;
2036
2037 lck_mtx_unlock(&strcache_mtx_locks[lock_index]);
2038 lck_rw_done(strtable_rw_lock);
2039
2040 return (const char *)entry->str;
2041 }
2042
2043
2044 int
2045 vfs_removename(const char *nameref)
2046 {
2047 struct stringhead *head;
2048 string_t *entry;
2049 uint32_t hashval;
2050 uint32_t hash_index;
2051 uint32_t lock_index;
2052 int retval = ENOENT;
2053
2054 hashval = hash_string(nameref, 0);
2055
2056 /*
2057 * take this lock 'shared' to keep the hash stable
2058 * if someone else decides to grow the pool they
2059 * will take this lock exclusively
2060 */
2061 lck_rw_lock_shared(strtable_rw_lock);
2062 /*
2063 * must compute the head behind the table lock
2064 * since the size and location of the table
2065 * can change on the fly
2066 */
2067 hash_index = hashval & string_table_mask;
2068 lock_index = hash_index % NUM_STRCACHE_LOCKS;
2069
2070 head = &string_ref_table[hash_index];
2071
2072 lck_mtx_lock_spin(&strcache_mtx_locks[lock_index]);
2073
2074 for (entry = head->lh_first; entry != NULL; entry = entry->hash_chain.le_next) {
2075 if (entry->str == nameref) {
2076 entry->refcount--;
2077
2078 if (entry->refcount == 0) {
2079 LIST_REMOVE(entry, hash_chain);
2080
2081 if (head->lh_first == NULL) {
2082 OSAddAtomic(-1, &filled_buckets);
2083 }
2084 } else {
2085 entry = NULL;
2086 }
2087 retval = 0;
2088 break;
2089 }
2090 }
2091 lck_mtx_unlock(&strcache_mtx_locks[lock_index]);
2092 lck_rw_done(strtable_rw_lock);
2093
2094 if (entry != NULL)
2095 FREE(entry, M_TEMP);
2096
2097 return retval;
2098 }
2099
2100
2101 #ifdef DUMP_STRING_TABLE
2102 void
2103 dump_string_table(void)
2104 {
2105 struct stringhead *head;
2106 string_t *entry;
2107 u_long i;
2108
2109 lck_rw_lock_shared(strtable_rw_lock);
2110
2111 for (i = 0; i <= string_table_mask; i++) {
2112 head = &string_ref_table[i];
2113 for (entry=head->lh_first; entry != NULL; entry=entry->hash_chain.le_next) {
2114 printf("%6d - %s\n", entry->refcount, entry->str);
2115 }
2116 }
2117 lck_rw_done(strtable_rw_lock);
2118 }
2119 #endif /* DUMP_STRING_TABLE */