2 * Copyright (c) 2000-2008 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
;
147 lck_grp_t
* strcache_lck_grp
;
148 lck_grp_attr_t
* strcache_lck_grp_attr
;
149 lck_attr_t
* strcache_lck_attr
;
151 lck_rw_t
* namecache_rw_lock
;
152 lck_rw_t
* strtable_rw_lock
;
154 #define NUM_STRCACHE_LOCKS 1024
156 lck_mtx_t strcache_mtx_locks
[NUM_STRCACHE_LOCKS
];
159 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
160 static const char *add_name_internal(const char *, uint32_t, u_int
, boolean_t
, u_int
);
161 static void init_string_table(void) __attribute__((section("__TEXT, initcode")));
162 static void cache_delete(struct namecache
*, int);
163 static void cache_enter_locked(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, const char *strname
);
165 #ifdef DUMP_STRING_TABLE
167 * Internal dump function used for debugging
169 void dump_string_table(void);
170 #endif /* DUMP_STRING_TABLE */
172 static void init_crc32(void) __attribute__((section("__TEXT, initcode")));
173 static unsigned int crc32tab
[256];
176 #define NCHHASH(dvp, hash_val) \
177 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
182 * This function builds the path to a filename in "buff". The
183 * length of the buffer *INCLUDING* the trailing zero byte is
184 * returned in outlen. NOTE: the length includes the trailing
185 * zero byte and thus the length is one greater than what strlen
186 * would return. This is important and lots of code elsewhere
187 * in the kernel assumes this behavior.
189 * This function can call vnop in file system if the parent vnode
190 * does not exist or when called for hardlinks via volfs path.
191 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
192 * in the name cache and does not enter the file system.
194 * passed in vp must have a valid io_count reference
197 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
200 vnode_t vp_with_iocount
;
201 vnode_t proc_root_dir_vp
;
208 if (first_vp
== NULLVP
)
212 * Grab the process fd so we can evaluate fd_rdir.
214 if (vfs_context_proc(ctx
)->p_fd
)
215 proc_root_dir_vp
= vfs_context_proc(ctx
)->p_fd
->fd_rdir
;
217 proc_root_dir_vp
= NULL
;
219 vp_with_iocount
= NULLVP
;
223 end
= &buff
[buflen
-1];
227 * holding the NAME_CACHE_LOCK in shared mode is
228 * sufficient to stabilize both the vp->v_parent chain
229 * and the 'vp->v_mount->mnt_vnodecovered' chain
231 * if we need to drop this lock, we must first grab the v_id
232 * from the vnode we're currently working with... if that
233 * vnode doesn't already have an io_count reference (the vp
234 * passed in comes with one), we must grab a reference
235 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
236 * deadlocks may result if you call vnode_get while holding
237 * the NAME_CACHE_LOCK... we lazily release the reference
238 * we pick up the next time we encounter a need to drop
239 * the NAME_CACHE_LOCK or before we return from this routine
241 NAME_CACHE_LOCK_SHARED();
244 * Check if this is the root of a file system.
246 while (vp
&& vp
->v_flag
& VROOT
) {
247 if (vp
->v_mount
== NULL
) {
251 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
253 * It's the root of the root file system, so it's
260 vp
= vp
->v_mount
->mnt_vnodecovered
;
264 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
268 * For hardlinks the v_name may be stale, so if its OK
269 * to enter a file system, ask the file system for the
270 * name and parent (below).
272 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
273 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
274 !(flags
& BUILDPATH_NO_FS_ENTER
);
279 if (str
== NULL
|| *str
== '\0') {
280 if (vp
->v_parent
!= NULL
)
288 * Check that there's enough space (including space for the '/')
290 if ((end
- buff
) < (len
+ 1)) {
295 * Copy the name backwards.
299 for (; len
> 0; len
--)
302 * Add a path separator.
308 * Walk up the parent chain.
310 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
311 (flags
& BUILDPATH_NO_FS_ENTER
)) {
315 * if the vnode we have in hand isn't a directory and it
316 * has a v_parent, then we started with the resource fork
317 * so skip up to avoid getting a duplicate copy of the
318 * file name in the path.
320 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
)
324 * No parent, go get it if supported.
326 struct vnode_attr va
;
330 * Make sure file system supports obtaining a path from id.
332 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
340 if (vp
!= first_vp
&& vp
!= vp_with_iocount
) {
341 if (vp_with_iocount
) {
342 vnode_put(vp_with_iocount
);
343 vp_with_iocount
= NULLVP
;
345 if (vnode_getwithvid(vp
, vid
))
347 vp_with_iocount
= vp
;
350 VATTR_WANTED(&va
, va_parentid
);
353 VATTR_WANTED(&va
, va_name
);
354 MALLOC_ZONE(va
.va_name
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
359 * Ask the file system for its parent id and for its name (optional).
361 ret
= vnode_getattr(vp
, &va
, ctx
);
364 if ((ret
== 0) && (VATTR_IS_SUPPORTED(&va
, va_name
))) {
366 vnode_update_identity(vp
, NULL
, str
, strlen(str
), 0, VNODE_UPDATE_NAME
);
367 } else if (vp
->v_name
) {
377 * Check that there's enough space.
379 if ((end
- buff
) < (len
+ 1)) {
382 /* Copy the name backwards. */
385 for (; len
> 0; len
--) {
389 * Add a path separator.
394 FREE_ZONE(va
.va_name
, MAXPATHLEN
, M_NAMEI
);
396 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
401 * Ask the file system for the parent vnode.
403 if ((ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
)))
406 if (!fixhardlink
&& (vp
->v_parent
!= dvp
))
407 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
410 vnode_put(vp_with_iocount
);
412 vp_with_iocount
= vp
;
414 NAME_CACHE_LOCK_SHARED();
417 * if the vnode we have in hand isn't a directory and it
418 * has a v_parent, then we started with the resource fork
419 * so skip up to avoid getting a duplicate copy of the
420 * file name in the path.
422 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
)
426 * When a mount point is crossed switch the vp.
427 * Continue until we find the root or we find
428 * a vnode that's not the root of a mounted
434 if (tvp
== proc_root_dir_vp
)
435 goto out_unlock
; /* encountered the root */
437 if (!(tvp
->v_flag
& VROOT
) || !tvp
->v_mount
)
438 break; /* not the root of a mounted FS */
439 tvp
= tvp
->v_mount
->mnt_vnodecovered
;
445 if (vp
&& (flags
& BUILDPATH_CHECKACCESS
)) {
450 if (vp
!= first_vp
&& vp
!= vp_with_iocount
) {
451 if (vp_with_iocount
) {
452 vnode_put(vp_with_iocount
);
453 vp_with_iocount
= NULLVP
;
455 if (vnode_getwithvid(vp
, vid
))
457 vp_with_iocount
= vp
;
459 if ((ret
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
)))
460 goto out
; /* no peeking */
462 NAME_CACHE_LOCK_SHARED();
469 vnode_put(vp_with_iocount
);
471 * Slide the name down to the beginning of the buffer.
473 memmove(buff
, end
, &buff
[buflen
] - end
);
476 * length includes the trailing zero byte
478 *outlen
= &buff
[buflen
] - end
;
485 * return NULLVP if vp's parent doesn't
486 * exist, or we can't get a valid iocount
487 * else return the parent of vp
490 vnode_getparent(vnode_t vp
)
492 vnode_t pvp
= NULLVP
;
495 NAME_CACHE_LOCK_SHARED();
497 * v_parent is stable behind the name_cache lock
498 * however, the only thing we can really guarantee
499 * is that we've grabbed a valid iocount on the
500 * parent of 'vp' at the time we took the name_cache lock...
501 * once we drop the lock, vp could get re-parented
503 if ( (pvp
= vp
->v_parent
) != NULLVP
) {
508 if (vnode_getwithvid(pvp
, pvid
) != 0)
516 vnode_getname(vnode_t vp
)
518 const char *name
= NULL
;
520 NAME_CACHE_LOCK_SHARED();
523 name
= vfs_addname(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
530 vnode_putname(const char *name
)
532 vfs_removename(name
);
537 * if VNODE_UPDATE_PARENT, and we can take
538 * a reference on dvp, then update vp with
539 * it's new parent... if vp already has a parent,
540 * then drop the reference vp held on it
542 * if VNODE_UPDATE_NAME,
543 * then drop string ref on v_name if it exists, and if name is non-NULL
544 * then pick up a string reference on name and record it in v_name...
545 * optionally pass in the length and hashval of name if known
547 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
550 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, uint32_t name_hashval
, int flags
)
552 struct namecache
*ncp
;
553 vnode_t old_parentvp
= NULLVP
;
555 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
556 int kusecountbumped
= 0;
558 kauth_cred_t tcred
= NULL
;
559 const char *vname
= NULL
;
560 const char *tname
= NULL
;
562 if (flags
& VNODE_UPDATE_PARENT
) {
563 if (dvp
&& vnode_ref(dvp
) != 0) {
567 /* Don't count a stream's parent ref during unmounts */
568 if (isstream
&& dvp
&& (dvp
!= vp
) && (dvp
!= vp
->v_parent
) && (dvp
->v_type
== VREG
)) {
569 vnode_lock_spin(dvp
);
578 if ( (flags
& VNODE_UPDATE_NAME
) ) {
579 if (name
!= vp
->v_name
) {
582 name_len
= strlen(name
);
583 tname
= vfs_addname(name
, name_len
, name_hashval
, 0);
586 flags
&= ~VNODE_UPDATE_NAME
;
588 if ( (flags
& (VNODE_UPDATE_PURGE
| VNODE_UPDATE_PARENT
| VNODE_UPDATE_CACHE
| VNODE_UPDATE_NAME
)) ) {
592 if ( (flags
& VNODE_UPDATE_PURGE
) ) {
595 vp
->v_parent
->v_nc_generation
++;
597 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
598 cache_delete(ncp
, 1);
600 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
601 cache_delete(ncp
, 1);
604 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
608 vp
->v_authorized_actions
= 0;
610 if ( (flags
& VNODE_UPDATE_NAME
) ) {
614 if (flags
& VNODE_UPDATE_PARENT
) {
615 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
616 old_parentvp
= vp
->v_parent
;
621 flags
|= VNODE_UPDATE_CACHE
;
624 if (flags
& VNODE_UPDATE_CACHE
) {
625 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
626 cache_delete(ncp
, 1);
631 vfs_removename(vname
);
633 if (IS_VALID_CRED(tcred
))
634 kauth_cred_unref(&tcred
);
638 /* Back-out the ref we took if we lost a race for vp->v_parent. */
639 if (kusecountbumped
) {
640 vnode_lock_spin(dvp
);
641 if (dvp
->v_kusecount
> 0)
653 vnode_lock_spin(old_parentvp
);
654 if ((old_parentvp
->v_type
!= VDIR
) && (old_parentvp
->v_kusecount
> 0))
655 --old_parentvp
->v_kusecount
;
656 vnode_unlock(old_parentvp
);
659 ut
= get_bsdthread_info(current_thread());
662 * indicated to vnode_rele that it shouldn't do a
663 * vnode_reclaim at this time... instead it will
664 * chain the vnode to the uu_vreclaims list...
665 * we'll be responsible for calling vnode_reclaim
666 * on each of the vnodes in this list...
668 ut
->uu_defer_reclaims
= 1;
669 ut
->uu_vreclaims
= NULLVP
;
671 while ( (vp
= old_parentvp
) != NULLVP
) {
674 vnode_rele_internal(vp
, 0, 0, 1);
677 * check to see if the vnode is now in the state
678 * that would have triggered a vnode_reclaim in vnode_rele
679 * if it is, we save it's parent pointer and then NULL
680 * out the v_parent field... we'll drop the reference
681 * that was held on the next iteration of this loop...
682 * this short circuits a potential deep recursion if we
683 * have a long chain of parents in this state...
684 * we'll sit in this loop until we run into
685 * a parent in this chain that is not in this state
687 * make our check and the vnode_rele atomic
688 * with respect to the current vnode we're working on
689 * by holding the vnode lock
690 * if vnode_rele deferred the vnode_reclaim and has put
691 * this vnode on the list to be reaped by us, than
692 * it has left this vnode with an iocount == 1
694 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
695 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
697 * vnode_rele wanted to do a vnode_reclaim on this vnode
698 * it should be sitting on the head of the uu_vreclaims chain
699 * pull the parent pointer now so that when we do the
700 * vnode_reclaim for each of the vnodes in the uu_vreclaims
701 * list, we won't recurse back through here
703 * need to do a convert here in case vnode_rele_internal
704 * returns with the lock held in the spin mode... it
705 * can drop and retake the lock under certain circumstances
707 vnode_lock_convert(vp
);
710 old_parentvp
= vp
->v_parent
;
711 vp
->v_parent
= NULLVP
;
715 * we're done... we ran into a vnode that isn't
718 old_parentvp
= NULLVP
;
722 ut
->uu_defer_reclaims
= 0;
724 while ( (vp
= ut
->uu_vreclaims
) != NULLVP
) {
725 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
728 * vnode_put will drive the vnode_reclaim if
729 * we are still the only reference on this vnode
738 * Mark a vnode as having multiple hard links. HFS makes use of this
739 * because it keeps track of each link separately, and wants to know
740 * which link was actually used.
742 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
743 * so that HFS can post-process the lookup. Also, volfs will call
744 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
746 void vnode_setmultipath(vnode_t vp
)
751 * In theory, we're changing the vnode's identity as far as the
752 * name cache is concerned, so we ought to grab the name cache lock
753 * here. However, there is already a race, and grabbing the name
754 * cache lock only makes the race window slightly smaller.
756 * The race happens because the vnode already exists in the name
757 * cache, and could be found by one thread before another thread
758 * can set the hard link flag.
761 vp
->v_flag
|= VISHARDLINK
;
769 * backwards compatibility
771 void vnode_uncache_credentials(vnode_t vp
)
773 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
778 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
779 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
780 * we use this lock so that we can look at the v_cred and v_authorized_actions
781 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
782 * which is the super-hot path... if we are updating the authorized actions for this
783 * vnode, we are already in the super-slow and far less frequented path so its not
784 * that bad that we take the lock exclusive for this case... of course we strive
785 * to hold it for the minimum amount of time possible
788 void vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
790 kauth_cred_t tcred
= NOCRED
;
794 vp
->v_authorized_actions
&= ~action
;
796 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
797 IS_VALID_CRED(vp
->v_cred
)) {
799 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
807 kauth_cred_unref(&tcred
);
811 boolean_t
vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
814 boolean_t retval
= FALSE
;
816 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
818 * a TTL is enabled on the rights cache... handle it here
819 * a TTL of 0 indicates that no rights should be cached
821 if (vp
->v_mount
->mnt_authcache_ttl
) {
822 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
824 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
825 * we will only allow a SEARCH right on a directory to be cached...
826 * that cached right always has a default TTL associated with it
828 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
)
831 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
832 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
839 ucred
= vfs_context_ucred(ctx
);
841 NAME_CACHE_LOCK_SHARED();
843 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
)
852 void vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
854 kauth_cred_t tcred
= NOCRED
;
857 boolean_t ttl_active
= FALSE
;
859 ucred
= vfs_context_ucred(ctx
);
861 if (!IS_VALID_CRED(ucred
) || action
== 0)
864 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
866 * a TTL is enabled on the rights cache... handle it here
867 * a TTL of 0 indicates that no rights should be cached
869 if (vp
->v_mount
->mnt_authcache_ttl
== 0)
872 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
874 * only cache SEARCH action for filesystems marked
875 * MNTK_AUTH_OPAQUE on VDIRs...
876 * the lookup_path code will time these out
878 if ( (action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
)
887 if (vp
->v_cred
!= ucred
) {
888 kauth_cred_ref(ucred
);
890 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
894 vp
->v_authorized_actions
= 0;
896 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
898 * only reset the timestamnp on the
899 * first authorization cached after the previous
900 * timer has expired or we're switching creds...
901 * 'vnode_cache_is_authorized' will clear the
902 * authorized actions if the TTL is active and
905 vp
->v_cred_timestamp
= tv
.tv_sec
;
907 vp
->v_authorized_actions
|= action
;
911 if (IS_VALID_CRED(tcred
))
912 kauth_cred_unref(&tcred
);
916 boolean_t
vnode_cache_is_stale(vnode_t vp
)
923 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
)
935 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
936 * This errno value should not be seen by anyone outside of the kernel.
939 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
940 vfs_context_t ctx
, int *trailing_slash
, int *dp_authorized
, vnode_t last_dp
)
942 char *cp
; /* pointer into pathname argument */
944 int vvid
= 0; /* protected by vp != NULLVP */
946 vnode_t tdp
= NULLVP
;
948 boolean_t ttl_enabled
= FALSE
;
954 ucred
= vfs_context_ucred(ctx
);
957 NAME_CACHE_LOCK_SHARED();
959 if ( dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
965 * Search a directory.
967 * The cn_hash value is for use by cache_lookup
968 * The last component of the filename is left accessible via
969 * cnp->cn_nameptr for callers that need the name.
972 cp
= cnp
->cn_nameptr
;
974 while (*cp
&& (*cp
!= '/')) {
975 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
978 * the crc generator can legitimately generate
979 * a 0... however, 0 for us means that we
980 * haven't computed a hash, so use 1 instead
985 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
987 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
991 * Replace multiple slashes by a single slash and trailing slashes
992 * by a null. This must be done before VNOP_LOOKUP() because some
993 * fs's don't know about trailing slashes. Remember if there were
994 * trailing slashes to handle symlinks, existing non-directories
995 * and non-existing files that won't be directories specially later.
997 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
1002 *trailing_slash
= 1;
1003 *ndp
->ni_next
= '\0';
1008 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
1011 cnp
->cn_flags
|= ISLASTCN
;
1013 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.')
1014 cnp
->cn_flags
|= ISDOTDOT
;
1019 * Process a request for a file's resource fork.
1021 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1023 if ((ndp
->ni_pathlen
== sizeof(_PATH_RSRCFORKSPEC
)) &&
1024 (cp
[1] == '.' && cp
[2] == '.') &&
1025 bcmp(cp
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
1026 /* Skip volfs file systems that don't support native streams. */
1027 if ((dp
->v_mount
!= NULL
) &&
1028 (dp
->v_mount
->mnt_flag
& MNT_DOVOLFS
) &&
1029 (dp
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0) {
1032 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
1033 cnp
->cn_flags
|= ISLASTCN
;
1034 ndp
->ni_next
[0] = '\0';
1035 ndp
->ni_pathlen
= 1;
1043 * Name cache provides authorization caching (see below)
1044 * that will short circuit MAC checks in lookup().
1045 * We must perform MAC check here. On denial
1046 * dp_authorized will remain 0 and second check will
1047 * be perfomed in lookup().
1049 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
1050 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
1052 NAME_CACHE_UNLOCK();
1057 if (ttl_enabled
&& ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))
1061 * NAME_CACHE_LOCK holds these fields stable
1063 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
1064 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
))
1068 * indicate that we're allowed to traverse this directory...
1069 * even if we fail the cache lookup or decide to bail for
1070 * some other reason, this information is valid and is used
1071 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1075 if ( (cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
)) ) {
1076 if (cnp
->cn_nameiop
!= LOOKUP
)
1078 if (cnp
->cn_flags
& (LOCKPARENT
| NOCACHE
))
1080 if (cnp
->cn_flags
& ISDOTDOT
) {
1082 * Force directory hardlinks to go to
1083 * file system for ".." requests.
1085 if (dp
&& (dp
->v_flag
& VISHARDLINK
)) {
1089 * Quit here only if we can't use
1090 * the parent directory pointer or
1091 * don't have one. Otherwise, we'll
1094 if ((dp
->v_flag
& VROOT
) ||
1095 dp
== ndp
->ni_rootdir
||
1096 dp
->v_parent
== NULLVP
)
1102 * "." and ".." aren't supposed to be cached, so check
1103 * for them before checking the cache.
1105 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
1107 else if ( (cnp
->cn_flags
& ISDOTDOT
) )
1110 if ( (vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
)
1113 if ( (vp
->v_flag
& VISHARDLINK
) ) {
1115 * The file system wants a VNOP_LOOKUP on this vnode
1121 if ( (cnp
->cn_flags
& ISLASTCN
) )
1124 if (vp
->v_type
!= VDIR
) {
1125 if (vp
->v_type
!= VLNK
)
1129 if ( (mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1131 if (mp
->mnt_realrootvp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1132 mp
->mnt_realrootvp_vid
!= mp
->mnt_realrootvp
->v_id
)
1134 vp
= mp
->mnt_realrootvp
;
1139 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1141 while (*cnp
->cn_nameptr
== '/') {
1150 NAME_CACHE_UNLOCK();
1152 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1153 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
1155 * if we've got a child and it's the last component, and
1156 * the lookup doesn't need to return the parent then we
1157 * can skip grabbing an iocount on the parent, since all
1158 * we're going to do with it is a vnode_put just before
1159 * we return from 'lookup'. If it's a symbolic link,
1160 * we need the parent in case the link happens to be
1161 * a relative pathname.
1168 * return the last directory we looked at
1169 * with an io reference held. If it was the one passed
1170 * in as a result of the last iteration of VNOP_LOOKUP,
1171 * it should already hold an io ref. No need to increase ref.
1175 if (dp
== ndp
->ni_usedvp
) {
1177 * if this vnode matches the one passed in via USEDVP
1178 * than this context already holds an io_count... just
1179 * use vnode_get to get an extra ref for lookup to play
1180 * with... can't use the getwithvid variant here because
1181 * it will block behind a vnode_drain which would result
1182 * in a deadlock (since we already own an io_count that the
1183 * vnode_drain is waiting on)... vnode_get grabs the io_count
1184 * immediately w/o waiting... it always succeeds
1187 } else if ( (vnode_getwithvid(dp
, vid
)) ) {
1189 * failure indicates the vnode
1190 * changed identity or is being
1191 * TERMINATED... in either case
1194 * don't necessarily return ENOENT, though, because
1195 * we really want to go back to disk and make sure it's
1196 * there or not if someone else is changing this
1205 if ( (vnode_getwithvid(vp
, vvid
)) ) {
1209 * can't get reference on the vp we'd like
1210 * to return... if we didn't grab a reference
1211 * on the directory (due to fast path bypass),
1212 * then we need to do it now... we can't return
1213 * with both ni_dvp and ni_vp NULL, and no
1227 * If we came into cache_lookup_path after an iteration of the lookup loop that
1228 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1229 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1230 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1231 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1232 * the case that we got to a subsequent path component and this previous vnode is
1233 * no longer needed. We can then drop the io ref on it.
1235 if ((last_dp
!= NULLVP
) && (last_dp
!= ndp
->ni_dvp
)){
1239 //initialized to 0, should be the same if no error cases occurred.
1245 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1247 struct namecache
*ncp
;
1248 struct nchashhead
*ncpp
;
1249 long namelen
= cnp
->cn_namelen
;
1250 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1252 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1253 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1254 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1255 if (memcmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1261 * We failed to find an entry
1266 NCHSTAT(ncs_goodhits
);
1268 return (ncp
->nc_vp
);
1273 // Have to take a len argument because we may only need to
1274 // hash part of a componentname.
1277 hash_string(const char *cp
, int len
)
1283 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1286 while (*cp
!= '\0') {
1287 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1291 * the crc generator can legitimately generate
1292 * a 0... however, 0 for us means that we
1293 * haven't computed a hash, so use 1 instead
1302 * Lookup an entry in the cache
1304 * We don't do this if the segment name is long, simply so the cache
1305 * can avoid holding long names (which would either waste space, or
1306 * add greatly to the complexity).
1308 * Lookup is called with dvp pointing to the directory to search,
1309 * cnp pointing to the name of the entry being sought. If the lookup
1310 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1311 * returned. If the lookup determines that the name does not exist
1312 * (negative cacheing), a status of ENOENT is returned. If the lookup
1313 * fails, a status of zero is returned.
1317 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1319 struct namecache
*ncp
;
1320 struct nchashhead
*ncpp
;
1321 long namelen
= cnp
->cn_namelen
;
1322 unsigned int hashval
;
1323 boolean_t have_exclusive
= FALSE
;
1327 if (cnp
->cn_hash
== 0)
1328 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1329 hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1331 NAME_CACHE_LOCK_SHARED();
1334 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1335 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1336 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1337 if (memcmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1341 /* We failed to find an entry */
1344 NAME_CACHE_UNLOCK();
1348 /* We don't want to have an entry, so dump it */
1349 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
1350 if (have_exclusive
== TRUE
) {
1351 NCHSTAT(ncs_badhits
);
1352 cache_delete(ncp
, 1);
1353 NAME_CACHE_UNLOCK();
1356 NAME_CACHE_UNLOCK();
1358 have_exclusive
= TRUE
;
1363 /* We found a "positive" match, return the vnode */
1365 NCHSTAT(ncs_goodhits
);
1368 NAME_CACHE_UNLOCK();
1370 if (vnode_getwithvid(vp
, vid
)) {
1373 NCHSTAT(ncs_badvid
);
1374 NAME_CACHE_UNLOCK();
1382 /* We found a negative match, and want to create it, so purge */
1383 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
1384 if (have_exclusive
== TRUE
) {
1385 NCHSTAT(ncs_badhits
);
1386 cache_delete(ncp
, 1);
1387 NAME_CACHE_UNLOCK();
1390 NAME_CACHE_UNLOCK();
1392 have_exclusive
= TRUE
;
1397 * We found a "negative" match, ENOENT notifies client of this match.
1398 * The nc_whiteout field records whether this is a whiteout.
1400 NCHSTAT(ncs_neghits
);
1402 if (ncp
->nc_whiteout
)
1403 cnp
->cn_flags
|= ISWHITEOUT
;
1404 NAME_CACHE_UNLOCK();
1409 cache_enter_create(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
)
1411 const char *strname
;
1413 if (cnp
->cn_hash
== 0)
1414 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1417 * grab 2 references on the string entered
1418 * one for the cache_enter_locked to consume
1419 * and the second to be consumed by v_name (vnode_create call point)
1421 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, TRUE
, 0);
1425 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1427 NAME_CACHE_UNLOCK();
1434 * Add an entry to the cache...
1435 * but first check to see if the directory
1436 * that this entry is to be associated with has
1437 * had any cache_purges applied since we took
1438 * our identity snapshot... this check needs to
1439 * be done behind the name cache lock
1442 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
1445 if (cnp
->cn_hash
== 0)
1446 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1450 if (dvp
->v_nc_generation
== gen
)
1451 (void)cache_enter_locked(dvp
, vp
, cnp
, NULL
);
1453 NAME_CACHE_UNLOCK();
1458 * Add an entry to the cache.
1461 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
1463 const char *strname
;
1465 if (cnp
->cn_hash
== 0)
1466 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1469 * grab 1 reference on the string entered
1470 * for the cache_enter_locked to consume
1472 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
1476 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1478 NAME_CACHE_UNLOCK();
1483 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, const char *strname
)
1485 struct namecache
*ncp
, *negp
;
1486 struct nchashhead
*ncpp
;
1489 * if the entry is for -ve caching vp is null
1491 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
1493 * someone beat us to the punch..
1494 * this vnode is already in the cache
1496 if (strname
!= NULL
)
1497 vfs_removename(strname
);
1501 * We allocate a new entry if we are less than the maximum
1502 * allowed and the one at the front of the list is in use.
1503 * Otherwise we use the one at the front of the list.
1505 if (numcache
< desiredNodes
&&
1506 ((ncp
= nchead
.tqh_first
) == NULL
||
1507 ncp
->nc_hash
.le_prev
!= 0)) {
1509 * Allocate one more entry
1511 ncp
= (struct namecache
*)_MALLOC_ZONE(sizeof(*ncp
), M_CACHE
, M_WAITOK
);
1515 * reuse an old entry
1517 ncp
= TAILQ_FIRST(&nchead
);
1518 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1520 if (ncp
->nc_hash
.le_prev
!= 0) {
1522 * still in use... we need to
1523 * delete it before re-using it
1525 NCHSTAT(ncs_stolen
);
1526 cache_delete(ncp
, 0);
1529 NCHSTAT(ncs_enters
);
1532 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1536 ncp
->nc_hashval
= cnp
->cn_hash
;
1537 ncp
->nc_whiteout
= FALSE
;
1539 if (strname
== NULL
)
1540 ncp
->nc_name
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
1542 ncp
->nc_name
= strname
;
1544 * make us the newest entry in the cache
1545 * i.e. we'll be the last to be stolen
1547 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
1549 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1552 struct namecache
*p
;
1554 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
)
1556 panic("cache_enter: duplicate");
1560 * make us available to be found via lookup
1562 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
1566 * add to the list of name cache entries
1569 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
1572 * this is a negative cache entry (vp == NULL)
1573 * stick it on the negative cache list
1574 * and record the whiteout state
1576 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
1578 if (cnp
->cn_flags
& ISWHITEOUT
)
1579 ncp
->nc_whiteout
= TRUE
;
1582 if (ncs_negtotal
> desiredNegNodes
) {
1584 * if we've reached our desired limit
1585 * of negative cache entries, delete
1588 negp
= TAILQ_FIRST(&neghead
);
1589 cache_delete(negp
, 1);
1593 * add us to the list of name cache entries that
1594 * are children of dvp
1596 LIST_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
1601 * Initialize CRC-32 remainder table.
1603 static void init_crc32(void)
1606 * the CRC-32 generator polynomial is:
1607 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1608 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1610 unsigned int crc32_polynomial
= 0x04c11db7;
1614 * pre-calculate the CRC-32 remainder for each possible octet encoding
1616 for (i
= 0; i
< 256; i
++) {
1617 unsigned int crc_rem
= i
<< 24;
1619 for (j
= 0; j
< 8; j
++) {
1620 if (crc_rem
& 0x80000000)
1621 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
1623 crc_rem
= (crc_rem
<< 1);
1625 crc32tab
[i
] = crc_rem
;
1631 * Name cache initialization, from vfs_init() when we are booting
1638 desiredNegNodes
= (desiredvnodes
/ 10);
1639 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
1641 TAILQ_INIT(&nchead
);
1642 TAILQ_INIT(&neghead
);
1646 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 *desiredNodes
)), M_CACHE
, &nchash
);
1647 nchashmask
= nchash
;
1650 init_string_table();
1652 /* Allocate name cache lock group attribute and group */
1653 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
1655 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
1657 /* Allocate name cache lock attribute */
1658 namecache_lck_attr
= lck_attr_alloc_init();
1660 /* Allocate name cache lock */
1661 namecache_rw_lock
= lck_rw_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
1664 /* Allocate string cache lock group attribute and group */
1665 strcache_lck_grp_attr
= lck_grp_attr_alloc_init();
1667 strcache_lck_grp
= lck_grp_alloc_init("String Cache", strcache_lck_grp_attr
);
1669 /* Allocate string cache lock attribute */
1670 strcache_lck_attr
= lck_attr_alloc_init();
1672 /* Allocate string cache lock */
1673 strtable_rw_lock
= lck_rw_alloc_init(strcache_lck_grp
, strcache_lck_attr
);
1675 for (i
= 0; i
< NUM_STRCACHE_LOCKS
; i
++)
1676 lck_mtx_init(&strcache_mtx_locks
[i
], strcache_lck_grp
, strcache_lck_attr
);
1680 name_cache_lock_shared(void)
1682 lck_rw_lock_shared(namecache_rw_lock
);
1686 name_cache_lock(void)
1688 lck_rw_lock_exclusive(namecache_rw_lock
);
1692 name_cache_unlock(void)
1694 lck_rw_done(namecache_rw_lock
);
1699 resize_namecache(u_int newsize
)
1701 struct nchashhead
*new_table
;
1702 struct nchashhead
*old_table
;
1703 struct nchashhead
*old_head
, *head
;
1704 struct namecache
*entry
, *next
;
1705 uint32_t i
, hashval
;
1706 int dNodes
, dNegNodes
;
1707 u_long new_size
, old_size
;
1709 dNegNodes
= (newsize
/ 10);
1710 dNodes
= newsize
+ dNegNodes
;
1712 // we don't support shrinking yet
1713 if (dNodes
<= desiredNodes
) {
1716 new_table
= hashinit(2 * dNodes
, M_CACHE
, &nchashmask
);
1717 new_size
= nchashmask
+ 1;
1719 if (new_table
== NULL
) {
1725 old_table
= nchashtbl
;
1726 nchashtbl
= new_table
;
1730 // walk the old table and insert all the entries into
1733 for(i
=0; i
< old_size
; i
++) {
1734 old_head
= &old_table
[i
];
1735 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1737 // XXXdbg - Beware: this assumes that hash_string() does
1738 // the same thing as what happens in
1739 // lookup() over in vfs_lookup.c
1740 hashval
= hash_string(entry
->nc_name
, 0);
1741 entry
->nc_hashval
= hashval
;
1742 head
= NCHHASH(entry
->nc_dvp
, hashval
);
1744 next
= entry
->nc_hash
.le_next
;
1745 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
1748 desiredNodes
= dNodes
;
1749 desiredNegNodes
= dNegNodes
;
1751 NAME_CACHE_UNLOCK();
1752 FREE(old_table
, M_CACHE
);
1758 cache_delete(struct namecache
*ncp
, int age_entry
)
1760 NCHSTAT(ncs_deletes
);
1763 LIST_REMOVE(ncp
, nc_un
.nc_link
);
1765 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
1768 LIST_REMOVE(ncp
, nc_child
);
1770 LIST_REMOVE(ncp
, nc_hash
);
1772 * this field is used to indicate
1773 * that the entry is in use and
1774 * must be deleted before it can
1777 ncp
->nc_hash
.le_prev
= NULL
;
1781 * make it the next one available
1782 * for cache_enter's use
1784 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1785 TAILQ_INSERT_HEAD(&nchead
, ncp
, nc_entry
);
1787 vfs_removename(ncp
->nc_name
);
1788 ncp
->nc_name
= NULL
;
1793 * purge the entry associated with the
1794 * specified vnode from the name cache
1797 cache_purge(vnode_t vp
)
1799 struct namecache
*ncp
;
1800 kauth_cred_t tcred
= NULL
;
1802 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) && (LIST_FIRST(&vp
->v_ncchildren
) == NULL
) && (vp
->v_cred
== NOCRED
))
1808 vp
->v_parent
->v_nc_generation
++;
1810 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
1811 cache_delete(ncp
, 1);
1813 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
1814 cache_delete(ncp
, 1);
1817 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1820 vp
->v_cred
= NOCRED
;
1821 vp
->v_authorized_actions
= 0;
1823 NAME_CACHE_UNLOCK();
1825 if (IS_VALID_CRED(tcred
))
1826 kauth_cred_unref(&tcred
);
1830 * Purge all negative cache entries that are children of the
1831 * given vnode. A case-insensitive file system (or any file
1832 * system that has multiple equivalent names for the same
1833 * directory entry) can use this when creating or renaming
1834 * to remove negative entries that may no longer apply.
1837 cache_purge_negatives(vnode_t vp
)
1839 struct namecache
*ncp
, *next_ncp
;
1843 LIST_FOREACH_SAFE(ncp
, &vp
->v_ncchildren
, nc_child
, next_ncp
)
1844 if (ncp
->nc_vp
== NULL
)
1845 cache_delete(ncp
, 1);
1847 NAME_CACHE_UNLOCK();
1851 * Flush all entries referencing a particular filesystem.
1853 * Since we need to check it anyway, we will flush all the invalid
1854 * entries at the same time.
1857 cache_purgevfs(struct mount
*mp
)
1859 struct nchashhead
*ncpp
;
1860 struct namecache
*ncp
;
1863 /* Scan hash tables for applicable entries */
1864 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
1866 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
1867 if (ncp
->nc_dvp
->v_mount
== mp
) {
1868 cache_delete(ncp
, 0);
1873 NAME_CACHE_UNLOCK();
1879 // String ref routines
1881 static LIST_HEAD(stringhead
, string_t
) *string_ref_table
;
1882 static u_long string_table_mask
;
1883 static uint32_t filled_buckets
=0;
1886 typedef struct string_t
{
1887 LIST_ENTRY(string_t
) hash_chain
;
1894 resize_string_ref_table(void)
1896 struct stringhead
*new_table
;
1897 struct stringhead
*old_table
;
1898 struct stringhead
*old_head
, *head
;
1899 string_t
*entry
, *next
;
1900 uint32_t i
, hashval
;
1901 u_long new_mask
, old_mask
;
1904 * need to hold the table lock exclusively
1905 * in order to grow the table... need to recheck
1906 * the need to resize again after we've taken
1907 * the lock exclusively in case some other thread
1908 * beat us to the punch
1910 lck_rw_lock_exclusive(strtable_rw_lock
);
1912 if (4 * filled_buckets
< ((string_table_mask
+ 1) * 3)) {
1913 lck_rw_done(strtable_rw_lock
);
1916 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
1918 if (new_table
== NULL
) {
1919 printf("failed to resize the hash table.\n");
1920 lck_rw_done(strtable_rw_lock
);
1925 old_table
= string_ref_table
;
1926 string_ref_table
= new_table
;
1927 old_mask
= string_table_mask
;
1928 string_table_mask
= new_mask
;
1931 // walk the old table and insert all the entries into
1934 for (i
= 0; i
<= old_mask
; i
++) {
1935 old_head
= &old_table
[i
];
1936 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
1937 hashval
= hash_string((const char *)entry
->str
, 0);
1938 head
= &string_ref_table
[hashval
& string_table_mask
];
1939 if (head
->lh_first
== NULL
) {
1942 next
= entry
->hash_chain
.le_next
;
1943 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
1946 lck_rw_done(strtable_rw_lock
);
1948 FREE(old_table
, M_CACHE
);
1953 init_string_table(void)
1955 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
1960 vfs_addname(const char *name
, uint32_t len
, u_int hashval
, u_int flags
)
1962 return (add_name_internal(name
, len
, hashval
, FALSE
, flags
));
1967 add_name_internal(const char *name
, uint32_t len
, u_int hashval
, boolean_t need_extra_ref
, __unused u_int flags
)
1969 struct stringhead
*head
;
1971 uint32_t chain_len
= 0;
1972 uint32_t hash_index
;
1973 uint32_t lock_index
;
1977 hashval
= hash_string(name
, 0);
1980 * if the length already accounts for the null-byte, then
1981 * subtract one so later on we don't index past the end
1984 if (len
> 0 && name
[len
-1] == '\0') {
1988 * take this lock 'shared' to keep the hash stable
1989 * if someone else decides to grow the pool they
1990 * will take this lock exclusively
1992 lck_rw_lock_shared(strtable_rw_lock
);
1995 * If the table gets more than 3/4 full, resize it
1997 if (4 * filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
1998 lck_rw_done(strtable_rw_lock
);
2000 resize_string_ref_table();
2002 lck_rw_lock_shared(strtable_rw_lock
);
2004 hash_index
= hashval
& string_table_mask
;
2005 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2007 head
= &string_ref_table
[hash_index
];
2009 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2011 for (entry
= head
->lh_first
; entry
!= NULL
; chain_len
++, entry
= entry
->hash_chain
.le_next
) {
2012 if (memcmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == 0) {
2017 if (entry
== NULL
) {
2018 lck_mtx_convert_spin(&strcache_mtx_locks
[lock_index
]);
2020 * it wasn't already there so add it.
2022 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
2024 if (head
->lh_first
== NULL
) {
2025 OSAddAtomic(1, &filled_buckets
);
2027 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
2028 strncpy(ptr
, name
, len
);
2031 entry
->refcount
= 1;
2032 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2034 if (need_extra_ref
== TRUE
)
2037 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2038 lck_rw_done(strtable_rw_lock
);
2040 return (const char *)entry
->str
;
2045 vfs_removename(const char *nameref
)
2047 struct stringhead
*head
;
2050 uint32_t hash_index
;
2051 uint32_t lock_index
;
2052 int retval
= ENOENT
;
2054 hashval
= hash_string(nameref
, 0);
2057 * take this lock 'shared' to keep the hash stable
2058 * if someone else decides to grow the pool they
2059 * will take this lock exclusively
2061 lck_rw_lock_shared(strtable_rw_lock
);
2063 * must compute the head behind the table lock
2064 * since the size and location of the table
2065 * can change on the fly
2067 hash_index
= hashval
& string_table_mask
;
2068 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2070 head
= &string_ref_table
[hash_index
];
2072 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2074 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2075 if (entry
->str
== nameref
) {
2078 if (entry
->refcount
== 0) {
2079 LIST_REMOVE(entry
, hash_chain
);
2081 if (head
->lh_first
== NULL
) {
2082 OSAddAtomic(-1, &filled_buckets
);
2091 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2092 lck_rw_done(strtable_rw_lock
);
2095 FREE(entry
, M_TEMP
);
2101 #ifdef DUMP_STRING_TABLE
2103 dump_string_table(void)
2105 struct stringhead
*head
;
2109 lck_rw_lock_shared(strtable_rw_lock
);
2111 for (i
= 0; i
<= string_table_mask
; i
++) {
2112 head
= &string_ref_table
[i
];
2113 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
2114 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
2117 lck_rw_done(strtable_rw_lock
);
2119 #endif /* DUMP_STRING_TABLE */