2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Poul-Henning Kamp of the FreeBSD Project.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
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,
73 #include <sys/param.h>
74 #include <sys/systm.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>
83 #include <sys/paths.h>
86 #include <security/mac_framework.h>
90 * Name caching works as follows:
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
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.
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.
107 * Structures associated with name cacheing.
110 LIST_HEAD(nchashhead
, namecache
) *nchashtbl
; /* Hash Table */
112 u_long nchash
; /* size of hash table - 1 */
113 long numcache
; /* number of cache entries allocated */
117 TAILQ_HEAD(, namecache
) nchead
; /* chain of all name cache entries */
118 TAILQ_HEAD(, namecache
) neghead
; /* chain of only negative cache entries */
123 struct nchstats nchstats
; /* cache effectiveness statistics */
125 #define NCHSTAT(v) { \
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()
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()
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 lck_rw_t
* namecache_rw_lock
;
148 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
149 static int remove_name_locked(const char *);
150 static const char *add_name_locked(const char *, size_t, u_int
, u_int
);
151 static void init_string_table(void) __attribute__((section("__TEXT, initcode")));
152 static void cache_delete(struct namecache
*, int);
153 static void cache_enter_locked(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
);
155 #ifdef DUMP_STRING_TABLE
157 * Internal dump function used for debugging
159 void dump_string_table(void);
160 #endif /* DUMP_STRING_TABLE */
162 static void init_crc32(void) __attribute__((section("__TEXT, initcode")));
163 static unsigned int crc32tab
[256];
166 #define NCHHASH(dvp, hash_val) \
167 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
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.
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.
185 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
188 vnode_t proc_root_dir_vp
;
195 if (first_vp
== NULLVP
) {
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
;
202 proc_root_dir_vp
= NULL
;
206 end
= &buff
[buflen
-1];
209 /* Check if this is the root of a file system. */
210 while (vp
&& vp
->v_flag
& VROOT
) {
211 if (vp
->v_mount
== NULL
) {
214 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
216 * It's the root of the root file system, so it's
222 vp
= vp
->v_mount
->mnt_vnodecovered
;
225 NAME_CACHE_LOCK_SHARED();
227 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
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).
233 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
234 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
235 !(flags
& BUILDPATH_NO_FS_ENTER
);
238 if (str
== NULL
|| *str
== '\0') {
239 if (vp
->v_parent
!= NULL
) {
248 * Check that there's enough space (including space for the '/')
250 if ((end
- buff
) < (len
+ 1)) {
254 /* Copy the name backwards. */
257 for (; len
> 0; len
--) {
260 /* Add a path separator. */
265 * Walk up the parent chain.
267 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
268 (flags
& BUILDPATH_NO_FS_ENTER
)) {
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
) {
278 } else /* No parent, go get it if supported. */ {
279 struct vnode_attr va
;
283 /* Make sure file system supports obtaining a path from id. */
284 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
291 if (vnode_getwithvid(vp
, vid
) != 0) {
292 /* vnode was recycled, so start over. */
297 VATTR_WANTED(&va
, va_parentid
);
299 VATTR_WANTED(&va
, va_name
);
300 MALLOC_ZONE(va
.va_name
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
304 /* Ask the file system for its parent id and for its name (optional). */
305 ret
= vnode_getattr(vp
, &va
, ctx
);
307 if (vp
->v_name
|| VATTR_IS_SUPPORTED(&va
, va_name
)) {
310 } else if (vp
->v_name
) {
320 /* Check that there's enough space. */
321 if ((end
- buff
) < (len
+ 1)) {
324 /* Copy the name backwards. */
327 for (; len
> 0; len
--) {
330 /* Add a path separator. */
335 FREE_ZONE(va
.va_name
, MAXPATHLEN
, M_NAMEI
);
337 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
342 /* Ask the file system for the parent vnode. */
343 ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
);
348 if (!fixhardlink
&& (vp
->v_parent
!= dvp
)) {
349 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
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.
362 if (vnode_ref(dvp
) != 0) {
366 NAME_CACHE_LOCK_SHARED();
369 vnode_rele_ext(dvp
, 0, 1);
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
) {
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
387 if (vp
== proc_root_dir_vp
) {
389 goto out
; /* encountered the root */
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
;
398 /* Slide the name down to the beginning of the buffer. */
399 memmove(buff
, end
, &buff
[buflen
] - end
);
401 *outlen
= &buff
[buflen
] - end
; /* length includes the trailing zero byte */
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
413 vnode_getparent(vnode_t vp
)
415 vnode_t pvp
= NULLVP
;
418 NAME_CACHE_LOCK_SHARED();
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
426 if ( (pvp
= vp
->v_parent
) != NULLVP
) {
431 if (vnode_getwithvid(pvp
, pvid
) != 0)
439 vnode_getname(vnode_t vp
)
441 const char *name
= NULL
;
446 name
= add_name_locked(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
453 vnode_putname(const char *name
)
457 remove_name_locked(name
);
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
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
474 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
477 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, int name_hashval
, int flags
)
479 struct namecache
*ncp
;
480 vnode_t old_parentvp
= NULLVP
;
482 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
483 int kusecountbumped
= 0;
486 if (flags
& VNODE_UPDATE_PARENT
) {
487 if (dvp
&& vnode_ref(dvp
) != 0) {
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
);
504 if ( (flags
& VNODE_UPDATE_NAME
) && (name
!= vp
->v_name
) ) {
505 if (vp
->v_name
!= NULL
) {
506 remove_name_locked(vp
->v_name
);
511 name_len
= strlen(name
);
512 vp
->v_name
= add_name_locked(name
, name_len
, name_hashval
, 0);
515 if (flags
& VNODE_UPDATE_PARENT
) {
516 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
517 old_parentvp
= vp
->v_parent
;
522 flags
|= VNODE_UPDATE_CACHE
;
525 if (flags
& VNODE_UPDATE_CACHE
) {
526 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
527 cache_delete(ncp
, 1);
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)
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
);
554 ut
= get_bsdthread_info(current_thread());
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...
563 ut
->uu_defer_reclaims
= 1;
564 ut
->uu_vreclaims
= NULLVP
;
566 while ( (vp
= old_parentvp
) != NULLVP
) {
569 vnode_rele_internal(vp
, 0, 0, 1);
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
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
589 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
590 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
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
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
602 vnode_lock_convert(vp
);
605 old_parentvp
= vp
->v_parent
;
606 vp
->v_parent
= NULLVP
;
610 * we're done... we ran into a vnode that isn't
613 old_parentvp
= NULLVP
;
617 ut
->uu_defer_reclaims
= 0;
619 while ( (vp
= ut
->uu_vreclaims
) != NULLVP
) {
620 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
623 * vnode_put will drive the vnode_reclaim if
624 * we are still the only reference on this vnode
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.
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.
641 void vnode_setmultipath(vnode_t vp
)
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.
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.
656 vp
->v_flag
|= VISHARDLINK
;
664 * backwards compatibility
666 void vnode_uncache_credentials(vnode_t vp
)
668 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
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
683 void vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
685 kauth_cred_t tcred
= NOCRED
;
689 vp
->v_authorized_actions
&= ~action
;
691 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
692 IS_VALID_CRED(vp
->v_cred
)) {
694 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
702 kauth_cred_unref(&tcred
);
706 boolean_t
vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
709 boolean_t retval
= FALSE
;
711 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
713 * a TTL is enabled on the rights cache... handle it here
714 * a TTL of 0 indicates that no rights should be cached
716 if (vp
->v_mount
->mnt_authcache_ttl
) {
717 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
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
723 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
)
726 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
727 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
734 ucred
= vfs_context_ucred(ctx
);
736 NAME_CACHE_LOCK_SHARED();
738 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
)
747 void vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
749 kauth_cred_t tcred
= NOCRED
;
752 boolean_t ttl_active
= FALSE
;
754 ucred
= vfs_context_ucred(ctx
);
756 if (!IS_VALID_CRED(ucred
) || action
== 0)
759 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
761 * a TTL is enabled on the rights cache... handle it here
762 * a TTL of 0 indicates that no rights should be cached
764 if (vp
->v_mount
->mnt_authcache_ttl
== 0)
767 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
769 * only cache SEARCH action for filesystems marked
770 * MNTK_AUTH_OPAQUE on VDIRs...
771 * the lookup_path code will time these out
773 if ( (action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
)
782 if (vp
->v_cred
!= ucred
) {
783 kauth_cred_ref(ucred
);
785 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
789 vp
->v_authorized_actions
= 0;
791 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
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
800 vp
->v_cred_timestamp
= tv
.tv_sec
;
802 vp
->v_authorized_actions
|= action
;
806 if (IS_VALID_CRED(tcred
))
807 kauth_cred_unref(&tcred
);
811 boolean_t
vnode_cache_is_stale(vnode_t vp
)
818 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
)
830 * ENOENT No such file or directory
833 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
, vfs_context_t ctx
, int *trailing_slash
, int *dp_authorized
)
835 char *cp
; /* pointer into pathname argument */
837 int vvid
= 0; /* protected by vp != NULLVP */
839 vnode_t tdp
= NULLVP
;
841 boolean_t ttl_enabled
= FALSE
;
849 ucred
= vfs_context_ucred(ctx
);
852 NAME_CACHE_LOCK_SHARED();
854 if ( dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
860 * Search a directory.
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.
867 cp
= cnp
->cn_nameptr
;
869 while (*cp
&& (*cp
!= '/')) {
870 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
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
880 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
882 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
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.
892 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
898 *ndp
->ni_next
= '\0';
903 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
906 cnp
->cn_flags
|= ISLASTCN
;
908 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.')
909 cnp
->cn_flags
|= ISDOTDOT
;
914 * Process a request for a file's resource fork.
916 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
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) {
927 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
928 cnp
->cn_flags
|= ISLASTCN
;
929 ndp
->ni_next
[0] = '\0';
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().
944 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
945 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
952 if (ttl_enabled
&& ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))
956 * NAME_CACHE_LOCK holds these fields stable
958 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
959 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
))
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
970 if ( (cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
)) ) {
971 if (cnp
->cn_nameiop
!= LOOKUP
)
973 if (cnp
->cn_flags
& (LOCKPARENT
| NOCACHE
))
975 if (cnp
->cn_flags
& ISDOTDOT
) {
977 * Force directory hardlinks to go to
978 * file system for ".." requests.
980 if (dp
&& (dp
->v_flag
& VISHARDLINK
)) {
984 * Quit here only if we can't use
985 * the parent directory pointer or
986 * don't have one. Otherwise, we'll
989 if ((dp
->v_flag
& VROOT
) ||
990 dp
== ndp
->ni_rootdir
||
991 dp
->v_parent
== NULLVP
)
997 * "." and ".." aren't supposed to be cached, so check
998 * for them before checking the cache.
1000 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
1002 else if ((cnp
->cn_flags
& ISDOTDOT
) && dp
->v_parent
)
1005 if ( (vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
)
1009 if ( (cnp
->cn_flags
& ISLASTCN
) )
1012 if (vp
->v_type
!= VDIR
) {
1013 if (vp
->v_type
!= VLNK
)
1017 if ( (mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1019 if (mp
->mnt_realrootvp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1020 mp
->mnt_realrootvp_vid
!= mp
->mnt_realrootvp
->v_id
)
1022 vp
= mp
->mnt_realrootvp
;
1027 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1029 while (*cnp
->cn_nameptr
== '/') {
1038 NAME_CACHE_UNLOCK();
1040 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1041 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
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.
1056 * return the last directory we looked at
1057 * with an io reference held
1059 if (dp
== ndp
->ni_usedvp
) {
1061 * if this vnode matches the one passed in via USEDVP
1062 * than this context already holds an io_count... just
1063 * use vnode_get to get an extra ref for lookup to play
1064 * with... can't use the getwithvid variant here because
1065 * it will block behind a vnode_drain which would result
1066 * in a deadlock (since we already own an io_count that the
1067 * vnode_drain is waiting on)... vnode_get grabs the io_count
1068 * immediately w/o waiting... it always succeeds
1071 } else if ( (vnode_getwithvid(dp
, vid
)) ) {
1073 * failure indicates the vnode
1074 * changed identity or is being
1075 * TERMINATED... in either case
1078 * don't necessarily return ENOENT, though, because
1079 * we really want to go back to disk and make sure it's
1080 * there or not if someone else is changing this
1087 if ( (vnode_getwithvid(vp
, vvid
)) ) {
1091 * can't get reference on the vp we'd like
1092 * to return... if we didn't grab a reference
1093 * on the directory (due to fast path bypass),
1094 * then we need to do it now... we can't return
1095 * with both ni_dvp and ni_vp NULL, and no
1112 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1114 struct namecache
*ncp
;
1115 struct nchashhead
*ncpp
;
1116 long namelen
= cnp
->cn_namelen
;
1117 char *nameptr
= cnp
->cn_nameptr
;
1118 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1121 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1122 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1123 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1124 if (memcmp(ncp
->nc_name
, nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1130 * We failed to find an entry
1135 NCHSTAT(ncs_goodhits
);
1138 if (vp
&& (vp
->v_flag
& VISHARDLINK
)) {
1140 * The file system wants a VNOP_LOOKUP on this vnode
1150 // Have to take a len argument because we may only need to
1151 // hash part of a componentname.
1154 hash_string(const char *cp
, int len
)
1160 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
1163 while (*cp
!= '\0') {
1164 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
1168 * the crc generator can legitimately generate
1169 * a 0... however, 0 for us means that we
1170 * haven't computed a hash, so use 1 instead
1179 * Lookup an entry in the cache
1181 * We don't do this if the segment name is long, simply so the cache
1182 * can avoid holding long names (which would either waste space, or
1183 * add greatly to the complexity).
1185 * Lookup is called with dvp pointing to the directory to search,
1186 * cnp pointing to the name of the entry being sought. If the lookup
1187 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1188 * returned. If the lookup determines that the name does not exist
1189 * (negative cacheing), a status of ENOENT is returned. If the lookup
1190 * fails, a status of zero is returned.
1194 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1196 struct namecache
*ncp
;
1197 struct nchashhead
*ncpp
;
1198 long namelen
= cnp
->cn_namelen
;
1199 char *nameptr
= cnp
->cn_nameptr
;
1200 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1201 boolean_t have_exclusive
= FALSE
;
1205 NAME_CACHE_LOCK_SHARED();
1207 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1209 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1210 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1211 if (memcmp(ncp
->nc_name
, nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1215 /* We failed to find an entry */
1218 NAME_CACHE_UNLOCK();
1222 /* We don't want to have an entry, so dump it */
1223 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
1224 if (have_exclusive
== TRUE
) {
1225 NCHSTAT(ncs_badhits
);
1226 cache_delete(ncp
, 1);
1227 NAME_CACHE_UNLOCK();
1230 NAME_CACHE_UNLOCK();
1232 have_exclusive
= TRUE
;
1237 /* We found a "positive" match, return the vnode */
1239 NCHSTAT(ncs_goodhits
);
1242 NAME_CACHE_UNLOCK();
1244 if (vnode_getwithvid(vp
, vid
)) {
1247 NCHSTAT(ncs_badvid
);
1248 NAME_CACHE_UNLOCK();
1256 /* We found a negative match, and want to create it, so purge */
1257 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
1258 if (have_exclusive
== TRUE
) {
1259 NCHSTAT(ncs_badhits
);
1260 cache_delete(ncp
, 1);
1261 NAME_CACHE_UNLOCK();
1264 NAME_CACHE_UNLOCK();
1266 have_exclusive
= TRUE
;
1271 * We found a "negative" match, ENOENT notifies client of this match.
1272 * The nc_whiteout field records whether this is a whiteout.
1274 NCHSTAT(ncs_neghits
);
1276 if (ncp
->nc_whiteout
)
1277 cnp
->cn_flags
|= ISWHITEOUT
;
1278 NAME_CACHE_UNLOCK();
1284 * Add an entry to the cache...
1285 * but first check to see if the directory
1286 * that this entry is to be associated with has
1287 * had any cache_purges applied since we took
1288 * our identity snapshot... this check needs to
1289 * be done behind the name cache lock
1292 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
1295 if (cnp
->cn_hash
== 0)
1296 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1300 if (dvp
->v_nc_generation
== gen
)
1301 cache_enter_locked(dvp
, vp
, cnp
);
1303 NAME_CACHE_UNLOCK();
1308 * Add an entry to the cache.
1311 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
1313 if (cnp
->cn_hash
== 0)
1314 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1318 cache_enter_locked(dvp
, vp
, cnp
);
1320 NAME_CACHE_UNLOCK();
1325 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
1327 struct namecache
*ncp
, *negp
;
1328 struct nchashhead
*ncpp
;
1331 * if the entry is for -ve caching vp is null
1333 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
1335 * someone beat us to the punch..
1336 * this vnode is already in the cache
1341 * We allocate a new entry if we are less than the maximum
1342 * allowed and the one at the front of the list is in use.
1343 * Otherwise we use the one at the front of the list.
1345 if (numcache
< desiredNodes
&&
1346 ((ncp
= nchead
.tqh_first
) == NULL
||
1347 ncp
->nc_hash
.le_prev
!= 0)) {
1349 * Allocate one more entry
1351 ncp
= (struct namecache
*)_MALLOC_ZONE((u_long
)sizeof *ncp
, M_CACHE
, M_WAITOK
);
1355 * reuse an old entry
1357 ncp
= TAILQ_FIRST(&nchead
);
1358 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1360 if (ncp
->nc_hash
.le_prev
!= 0) {
1362 * still in use... we need to
1363 * delete it before re-using it
1365 NCHSTAT(ncs_stolen
);
1366 cache_delete(ncp
, 0);
1369 NCHSTAT(ncs_enters
);
1372 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1376 ncp
->nc_hashval
= cnp
->cn_hash
;
1377 ncp
->nc_whiteout
= FALSE
;
1378 ncp
->nc_name
= add_name_locked(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
1381 * make us the newest entry in the cache
1382 * i.e. we'll be the last to be stolen
1384 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
1386 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1389 struct namecache
*p
;
1391 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
)
1393 panic("cache_enter: duplicate");
1397 * make us available to be found via lookup
1399 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
1403 * add to the list of name cache entries
1406 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
1409 * this is a negative cache entry (vp == NULL)
1410 * stick it on the negative cache list
1411 * and record the whiteout state
1413 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
1415 if (cnp
->cn_flags
& ISWHITEOUT
)
1416 ncp
->nc_whiteout
= TRUE
;
1419 if (ncs_negtotal
> desiredNegNodes
) {
1421 * if we've reached our desired limit
1422 * of negative cache entries, delete
1425 negp
= TAILQ_FIRST(&neghead
);
1426 TAILQ_REMOVE(&neghead
, negp
, nc_un
.nc_negentry
);
1428 cache_delete(negp
, 1);
1432 * add us to the list of name cache entries that
1433 * are children of dvp
1435 LIST_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
1440 * Initialize CRC-32 remainder table.
1442 static void init_crc32(void)
1445 * the CRC-32 generator polynomial is:
1446 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1447 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1449 unsigned int crc32_polynomial
= 0x04c11db7;
1453 * pre-calculate the CRC-32 remainder for each possible octet encoding
1455 for (i
= 0; i
< 256; i
++) {
1456 unsigned int crc_rem
= i
<< 24;
1458 for (j
= 0; j
< 8; j
++) {
1459 if (crc_rem
& 0x80000000)
1460 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
1462 crc_rem
= (crc_rem
<< 1);
1464 crc32tab
[i
] = crc_rem
;
1470 * Name cache initialization, from vfs_init() when we are booting
1475 desiredNegNodes
= (desiredvnodes
/ 10);
1476 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
1478 TAILQ_INIT(&nchead
);
1479 TAILQ_INIT(&neghead
);
1483 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 *desiredNodes
)), M_CACHE
, &nchash
);
1484 nchashmask
= nchash
;
1487 init_string_table();
1489 /* Allocate mount list lock group attribute and group */
1490 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
1492 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
1494 /* Allocate mount list lock attribute */
1495 namecache_lck_attr
= lck_attr_alloc_init();
1497 /* Allocate mount list lock */
1498 namecache_rw_lock
= lck_rw_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
1504 name_cache_lock_shared(void)
1506 lck_rw_lock_shared(namecache_rw_lock
);
1510 name_cache_lock(void)
1512 lck_rw_lock_exclusive(namecache_rw_lock
);
1516 name_cache_unlock(void)
1518 lck_rw_done(namecache_rw_lock
);
1523 resize_namecache(u_int newsize
)
1525 struct nchashhead
*new_table
;
1526 struct nchashhead
*old_table
;
1527 struct nchashhead
*old_head
, *head
;
1528 struct namecache
*entry
, *next
;
1529 uint32_t i
, hashval
;
1530 int dNodes
, dNegNodes
;
1531 u_long new_size
, old_size
;
1533 dNegNodes
= (newsize
/ 10);
1534 dNodes
= newsize
+ dNegNodes
;
1536 // we don't support shrinking yet
1537 if (dNodes
< desiredNodes
) {
1540 new_table
= hashinit(2 * dNodes
, M_CACHE
, &nchashmask
);
1541 new_size
= nchashmask
+ 1;
1543 if (new_table
== NULL
) {
1549 old_table
= nchashtbl
;
1550 nchashtbl
= new_table
;
1554 // walk the old table and insert all the entries into
1557 for(i
=0; i
< old_size
; i
++) {
1558 old_head
= &old_table
[i
];
1559 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1561 // XXXdbg - Beware: this assumes that hash_string() does
1562 // the same thing as what happens in
1563 // lookup() over in vfs_lookup.c
1564 hashval
= hash_string(entry
->nc_name
, 0);
1565 entry
->nc_hashval
= hashval
;
1566 head
= NCHHASH(entry
->nc_dvp
, hashval
);
1568 next
= entry
->nc_hash
.le_next
;
1569 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
1572 desiredNodes
= dNodes
;
1573 desiredNegNodes
= dNegNodes
;
1575 NAME_CACHE_UNLOCK();
1576 FREE(old_table
, M_CACHE
);
1582 cache_delete(struct namecache
*ncp
, int age_entry
)
1584 NCHSTAT(ncs_deletes
);
1587 LIST_REMOVE(ncp
, nc_un
.nc_link
);
1589 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
1592 LIST_REMOVE(ncp
, nc_child
);
1594 LIST_REMOVE(ncp
, nc_hash
);
1596 * this field is used to indicate
1597 * that the entry is in use and
1598 * must be deleted before it can
1601 ncp
->nc_hash
.le_prev
= NULL
;
1605 * make it the next one available
1606 * for cache_enter's use
1608 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1609 TAILQ_INSERT_HEAD(&nchead
, ncp
, nc_entry
);
1611 remove_name_locked(ncp
->nc_name
);
1612 ncp
->nc_name
= NULL
;
1617 * purge the entry associated with the
1618 * specified vnode from the name cache
1621 cache_purge(vnode_t vp
)
1623 struct namecache
*ncp
;
1624 kauth_cred_t tcred
= NULL
;
1626 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) && (LIST_FIRST(&vp
->v_ncchildren
) == NULL
))
1632 vp
->v_parent
->v_nc_generation
++;
1634 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
1635 cache_delete(ncp
, 1);
1637 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
1638 cache_delete(ncp
, 1);
1641 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1644 vp
->v_cred
= NOCRED
;
1645 vp
->v_authorized_actions
= 0;
1647 NAME_CACHE_UNLOCK();
1649 if (IS_VALID_CRED(tcred
))
1650 kauth_cred_unref(&tcred
);
1654 * Purge all negative cache entries that are children of the
1655 * given vnode. A case-insensitive file system (or any file
1656 * system that has multiple equivalent names for the same
1657 * directory entry) can use this when creating or renaming
1658 * to remove negative entries that may no longer apply.
1661 cache_purge_negatives(vnode_t vp
)
1663 struct namecache
*ncp
;
1667 LIST_FOREACH(ncp
, &vp
->v_ncchildren
, nc_child
)
1668 if (ncp
->nc_vp
== NULL
)
1669 cache_delete(ncp
, 1);
1671 NAME_CACHE_UNLOCK();
1675 * Flush all entries referencing a particular filesystem.
1677 * Since we need to check it anyway, we will flush all the invalid
1678 * entries at the same time.
1681 cache_purgevfs(struct mount
*mp
)
1683 struct nchashhead
*ncpp
;
1684 struct namecache
*ncp
;
1687 /* Scan hash tables for applicable entries */
1688 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
1690 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
1691 if (ncp
->nc_dvp
->v_mount
== mp
) {
1692 cache_delete(ncp
, 0);
1697 NAME_CACHE_UNLOCK();
1703 // String ref routines
1705 static LIST_HEAD(stringhead
, string_t
) *string_ref_table
;
1706 static u_long string_table_mask
;
1707 static uint32_t max_chain_len
=0;
1708 static struct stringhead
*long_chain_head
=NULL
;
1709 static uint32_t filled_buckets
=0;
1710 static uint32_t num_dups
=0;
1711 static uint32_t nstrings
=0;
1713 typedef struct string_t
{
1714 LIST_ENTRY(string_t
) hash_chain
;
1722 resize_string_ref_table(void)
1724 struct stringhead
*new_table
;
1725 struct stringhead
*old_table
;
1726 struct stringhead
*old_head
, *head
;
1727 string_t
*entry
, *next
;
1728 uint32_t i
, hashval
;
1729 u_long new_mask
, old_mask
;
1731 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
1732 if (new_table
== NULL
) {
1737 old_table
= string_ref_table
;
1738 string_ref_table
= new_table
;
1739 old_mask
= string_table_mask
;
1740 string_table_mask
= new_mask
;
1742 printf("resize: max chain len %d, new table size %lu\n",
1743 max_chain_len
, new_mask
+ 1);
1745 long_chain_head
= NULL
;
1748 // walk the old table and insert all the entries into
1751 for(i
=0; i
<= old_mask
; i
++) {
1752 old_head
= &old_table
[i
];
1753 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1754 hashval
= hash_string((const char *)entry
->str
, 0);
1755 head
= &string_ref_table
[hashval
& string_table_mask
];
1756 if (head
->lh_first
== NULL
) {
1760 next
= entry
->hash_chain
.le_next
;
1761 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
1765 FREE(old_table
, M_CACHE
);
1772 init_string_table(void)
1774 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
1779 vfs_addname(const char *name
, size_t len
, u_int hashval
, u_int flags
)
1784 ptr
= add_name_locked(name
, len
, hashval
, flags
);
1785 NAME_CACHE_UNLOCK();
1791 add_name_locked(const char *name
, size_t len
, u_int hashval
, __unused u_int flags
)
1793 struct stringhead
*head
;
1795 uint32_t chain_len
= 0;
1799 // If the table gets more than 3/4 full, resize it
1801 if (4*filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
1802 if (resize_string_ref_table() != 0) {
1803 printf("failed to resize the hash table.\n");
1807 hashval
= hash_string(name
, 0);
1811 // if the length already accounts for the null-byte, then
1812 // subtract one so later on we don't index past the end
1815 if (len
> 0 && name
[len
-1] == '\0') {
1819 head
= &string_ref_table
[hashval
& string_table_mask
];
1820 for (entry
=head
->lh_first
; entry
!= NULL
; chain_len
++, entry
=entry
->hash_chain
.le_next
) {
1821 if (memcmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == '\0') {
1828 if (entry
== NULL
) {
1829 // it wasn't already there so add it.
1830 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
1832 // have to get "head" again because we could have blocked
1833 // in malloc and thus head could have changed.
1835 head
= &string_ref_table
[hashval
& string_table_mask
];
1836 if (head
->lh_first
== NULL
) {
1840 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
1841 strncpy(ptr
, name
, len
);
1844 entry
->refcount
= 1;
1845 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
1847 if (chain_len
> max_chain_len
) {
1848 max_chain_len
= chain_len
;
1849 long_chain_head
= head
;
1855 return (const char *)entry
->str
;
1859 vfs_removename(const char *nameref
)
1864 i
= remove_name_locked(nameref
);
1865 NAME_CACHE_UNLOCK();
1873 remove_name_locked(const char *nameref
)
1875 struct stringhead
*head
;
1880 hashval
= hash_string(nameref
, 0);
1881 head
= &string_ref_table
[hashval
& string_table_mask
];
1882 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
1883 if (entry
->str
== nameref
) {
1885 if (entry
->refcount
== 0) {
1886 LIST_REMOVE(entry
, hash_chain
);
1887 if (head
->lh_first
== NULL
) {
1894 FREE(entry
, M_TEMP
);
1907 #ifdef DUMP_STRING_TABLE
1909 dump_string_table(void)
1911 struct stringhead
*head
;
1915 NAME_CACHE_LOCK_SHARED();
1917 for (i
= 0; i
<= string_table_mask
; i
++) {
1918 head
= &string_ref_table
[i
];
1919 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
1920 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
1923 NAME_CACHE_UNLOCK();
1925 #endif /* DUMP_STRING_TABLE */