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