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 */
118 TAILQ_HEAD(, namecache
) nchead
; /* chain of all name cache entries */
119 TAILQ_HEAD(, namecache
) neghead
; /* chain of only negative cache entries */
124 struct nchstats nchstats
; /* cache effectiveness statistics */
126 #define NCHSTAT(v) { \
129 #define NAME_CACHE_LOCK() name_cache_lock()
130 #define NAME_CACHE_UNLOCK() name_cache_unlock()
131 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
136 #define NAME_CACHE_LOCK() name_cache_lock()
137 #define NAME_CACHE_UNLOCK() name_cache_unlock()
138 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
143 /* vars for name cache list lock */
144 lck_grp_t
* namecache_lck_grp
;
145 lck_grp_attr_t
* namecache_lck_grp_attr
;
146 lck_attr_t
* namecache_lck_attr
;
148 lck_grp_t
* strcache_lck_grp
;
149 lck_grp_attr_t
* strcache_lck_grp_attr
;
150 lck_attr_t
* strcache_lck_attr
;
152 lck_rw_t
* namecache_rw_lock
;
153 lck_rw_t
* strtable_rw_lock
;
155 #define NUM_STRCACHE_LOCKS 1024
157 lck_mtx_t strcache_mtx_locks
[NUM_STRCACHE_LOCKS
];
160 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
161 static const char *add_name_internal(const char *, uint32_t, u_int
, boolean_t
, u_int
);
162 static void init_string_table(void) __attribute__((section("__TEXT, initcode")));
163 static void cache_delete(struct namecache
*, int);
164 static void cache_enter_locked(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, const char *strname
);
166 #ifdef DUMP_STRING_TABLE
168 * Internal dump function used for debugging
170 void dump_string_table(void);
171 #endif /* DUMP_STRING_TABLE */
173 static void init_crc32(void) __attribute__((section("__TEXT, initcode")));
174 static unsigned int crc32tab
[256];
177 #define NCHHASH(dvp, hash_val) \
178 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
183 * This function builds the path to a filename in "buff". The
184 * length of the buffer *INCLUDING* the trailing zero byte is
185 * returned in outlen. NOTE: the length includes the trailing
186 * zero byte and thus the length is one greater than what strlen
187 * would return. This is important and lots of code elsewhere
188 * in the kernel assumes this behavior.
190 * This function can call vnop in file system if the parent vnode
191 * does not exist or when called for hardlinks via volfs path.
192 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
193 * in the name cache and does not enter the file system.
195 * passed in vp must have a valid io_count reference
198 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
201 vnode_t vp_with_iocount
;
202 vnode_t proc_root_dir_vp
;
209 if (first_vp
== NULLVP
)
213 * Grab the process fd so we can evaluate fd_rdir.
215 if (vfs_context_proc(ctx
)->p_fd
)
216 proc_root_dir_vp
= vfs_context_proc(ctx
)->p_fd
->fd_rdir
;
218 proc_root_dir_vp
= NULL
;
220 vp_with_iocount
= NULLVP
;
224 end
= &buff
[buflen
-1];
228 * holding the NAME_CACHE_LOCK in shared mode is
229 * sufficient to stabilize both the vp->v_parent chain
230 * and the 'vp->v_mount->mnt_vnodecovered' chain
232 * if we need to drop this lock, we must first grab the v_id
233 * from the vnode we're currently working with... if that
234 * vnode doesn't already have an io_count reference (the vp
235 * passed in comes with one), we must grab a reference
236 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
237 * deadlocks may result if you call vnode_get while holding
238 * the NAME_CACHE_LOCK... we lazily release the reference
239 * we pick up the next time we encounter a need to drop
240 * the NAME_CACHE_LOCK or before we return from this routine
242 NAME_CACHE_LOCK_SHARED();
245 * Check if this is the root of a file system.
247 while (vp
&& vp
->v_flag
& VROOT
) {
248 if (vp
->v_mount
== NULL
) {
252 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
254 * It's the root of the root file system, so it's
261 vp
= vp
->v_mount
->mnt_vnodecovered
;
265 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
269 * For hardlinks the v_name may be stale, so if its OK
270 * to enter a file system, ask the file system for the
271 * name and parent (below).
273 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
274 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
275 !(flags
& BUILDPATH_NO_FS_ENTER
);
280 if (str
== NULL
|| *str
== '\0') {
281 if (vp
->v_parent
!= NULL
)
289 * Check that there's enough space (including space for the '/')
291 if ((end
- buff
) < (len
+ 1)) {
296 * Copy the name backwards.
300 for (; len
> 0; len
--)
303 * Add a path separator.
309 * Walk up the parent chain.
311 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
312 (flags
& BUILDPATH_NO_FS_ENTER
)) {
314 * In this if () block we are not allowed to enter the filesystem
315 * to conclusively get the most accurate parent identifier.
316 * As a result, if 'vp' does not identify '/' and it
317 * does not have a valid v_parent, then error out
318 * and disallow further path construction
320 if ((vp
->v_parent
== NULLVP
) && (rootvnode
!= vp
)) {
321 /* Only '/' is allowed to have a NULL parent pointer */
324 /* The code below will exit early if 'tvp = vp' == NULL */
330 * if the vnode we have in hand isn't a directory and it
331 * has a v_parent, then we started with the resource fork
332 * so skip up to avoid getting a duplicate copy of the
333 * file name in the path.
335 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
)
339 * No parent, go get it if supported.
341 struct vnode_attr va
;
345 * Make sure file system supports obtaining a path from id.
347 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
355 if (vp
!= first_vp
&& vp
!= vp_with_iocount
) {
356 if (vp_with_iocount
) {
357 vnode_put(vp_with_iocount
);
358 vp_with_iocount
= NULLVP
;
360 if (vnode_getwithvid(vp
, vid
))
362 vp_with_iocount
= vp
;
365 VATTR_WANTED(&va
, va_parentid
);
368 VATTR_WANTED(&va
, va_name
);
369 MALLOC_ZONE(va
.va_name
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
374 * Ask the file system for its parent id and for its name (optional).
376 ret
= vnode_getattr(vp
, &va
, ctx
);
379 if ((ret
== 0) && (VATTR_IS_SUPPORTED(&va
, va_name
))) {
381 vnode_update_identity(vp
, NULL
, str
, strlen(str
), 0, VNODE_UPDATE_NAME
);
382 } else if (vp
->v_name
) {
392 * Check that there's enough space.
394 if ((end
- buff
) < (len
+ 1)) {
397 /* Copy the name backwards. */
400 for (; len
> 0; len
--) {
404 * Add a path separator.
409 FREE_ZONE(va
.va_name
, MAXPATHLEN
, M_NAMEI
);
411 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
416 * Ask the file system for the parent vnode.
418 if ((ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
)))
421 if (!fixhardlink
&& (vp
->v_parent
!= dvp
))
422 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
425 vnode_put(vp_with_iocount
);
427 vp_with_iocount
= vp
;
429 NAME_CACHE_LOCK_SHARED();
432 * if the vnode we have in hand isn't a directory and it
433 * has a v_parent, then we started with the resource fork
434 * so skip up to avoid getting a duplicate copy of the
435 * file name in the path.
437 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
)
441 * When a mount point is crossed switch the vp.
442 * Continue until we find the root or we find
443 * a vnode that's not the root of a mounted
449 if (tvp
== proc_root_dir_vp
)
450 goto out_unlock
; /* encountered the root */
452 if (!(tvp
->v_flag
& VROOT
) || !tvp
->v_mount
)
453 break; /* not the root of a mounted FS */
454 tvp
= tvp
->v_mount
->mnt_vnodecovered
;
460 if (vp
&& (flags
& BUILDPATH_CHECKACCESS
)) {
465 if (vp
!= first_vp
&& vp
!= vp_with_iocount
) {
466 if (vp_with_iocount
) {
467 vnode_put(vp_with_iocount
);
468 vp_with_iocount
= NULLVP
;
470 if (vnode_getwithvid(vp
, vid
))
472 vp_with_iocount
= vp
;
474 if ((ret
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
)))
475 goto out
; /* no peeking */
477 NAME_CACHE_LOCK_SHARED();
484 vnode_put(vp_with_iocount
);
486 * Slide the name down to the beginning of the buffer.
488 memmove(buff
, end
, &buff
[buflen
] - end
);
491 * length includes the trailing zero byte
493 *outlen
= &buff
[buflen
] - end
;
500 * return NULLVP if vp's parent doesn't
501 * exist, or we can't get a valid iocount
502 * else return the parent of vp
505 vnode_getparent(vnode_t vp
)
507 vnode_t pvp
= NULLVP
;
510 NAME_CACHE_LOCK_SHARED();
512 * v_parent is stable behind the name_cache lock
513 * however, the only thing we can really guarantee
514 * is that we've grabbed a valid iocount on the
515 * parent of 'vp' at the time we took the name_cache lock...
516 * once we drop the lock, vp could get re-parented
518 if ( (pvp
= vp
->v_parent
) != NULLVP
) {
523 if (vnode_getwithvid(pvp
, pvid
) != 0)
531 vnode_getname(vnode_t vp
)
533 const char *name
= NULL
;
535 NAME_CACHE_LOCK_SHARED();
538 name
= vfs_addname(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
545 vnode_putname(const char *name
)
547 vfs_removename(name
);
552 * if VNODE_UPDATE_PARENT, and we can take
553 * a reference on dvp, then update vp with
554 * it's new parent... if vp already has a parent,
555 * then drop the reference vp held on it
557 * if VNODE_UPDATE_NAME,
558 * then drop string ref on v_name if it exists, and if name is non-NULL
559 * then pick up a string reference on name and record it in v_name...
560 * optionally pass in the length and hashval of name if known
562 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
565 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, uint32_t name_hashval
, int flags
)
567 struct namecache
*ncp
;
568 vnode_t old_parentvp
= NULLVP
;
570 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
571 int kusecountbumped
= 0;
573 kauth_cred_t tcred
= NULL
;
574 const char *vname
= NULL
;
575 const char *tname
= NULL
;
577 if (flags
& VNODE_UPDATE_PARENT
) {
578 if (dvp
&& vnode_ref(dvp
) != 0) {
582 /* Don't count a stream's parent ref during unmounts */
583 if (isstream
&& dvp
&& (dvp
!= vp
) && (dvp
!= vp
->v_parent
) && (dvp
->v_type
== VREG
)) {
584 vnode_lock_spin(dvp
);
593 if ( (flags
& VNODE_UPDATE_NAME
) ) {
594 if (name
!= vp
->v_name
) {
597 name_len
= strlen(name
);
598 tname
= vfs_addname(name
, name_len
, name_hashval
, 0);
601 flags
&= ~VNODE_UPDATE_NAME
;
603 if ( (flags
& (VNODE_UPDATE_PURGE
| VNODE_UPDATE_PARENT
| VNODE_UPDATE_CACHE
| VNODE_UPDATE_NAME
)) ) {
607 if ( (flags
& VNODE_UPDATE_PURGE
) ) {
610 vp
->v_parent
->v_nc_generation
++;
612 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
613 cache_delete(ncp
, 1);
615 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
616 cache_delete(ncp
, 1);
619 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
623 vp
->v_authorized_actions
= 0;
625 if ( (flags
& VNODE_UPDATE_NAME
) ) {
629 if (flags
& VNODE_UPDATE_PARENT
) {
630 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
631 old_parentvp
= vp
->v_parent
;
636 flags
|= VNODE_UPDATE_CACHE
;
639 if (flags
& VNODE_UPDATE_CACHE
) {
640 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
641 cache_delete(ncp
, 1);
646 vfs_removename(vname
);
648 if (IS_VALID_CRED(tcred
))
649 kauth_cred_unref(&tcred
);
653 /* Back-out the ref we took if we lost a race for vp->v_parent. */
654 if (kusecountbumped
) {
655 vnode_lock_spin(dvp
);
656 if (dvp
->v_kusecount
> 0)
668 vnode_lock_spin(old_parentvp
);
669 if ((old_parentvp
->v_type
!= VDIR
) && (old_parentvp
->v_kusecount
> 0))
670 --old_parentvp
->v_kusecount
;
671 vnode_unlock(old_parentvp
);
674 ut
= get_bsdthread_info(current_thread());
677 * indicated to vnode_rele that it shouldn't do a
678 * vnode_reclaim at this time... instead it will
679 * chain the vnode to the uu_vreclaims list...
680 * we'll be responsible for calling vnode_reclaim
681 * on each of the vnodes in this list...
683 ut
->uu_defer_reclaims
= 1;
684 ut
->uu_vreclaims
= NULLVP
;
686 while ( (vp
= old_parentvp
) != NULLVP
) {
689 vnode_rele_internal(vp
, 0, 0, 1);
692 * check to see if the vnode is now in the state
693 * that would have triggered a vnode_reclaim in vnode_rele
694 * if it is, we save it's parent pointer and then NULL
695 * out the v_parent field... we'll drop the reference
696 * that was held on the next iteration of this loop...
697 * this short circuits a potential deep recursion if we
698 * have a long chain of parents in this state...
699 * we'll sit in this loop until we run into
700 * a parent in this chain that is not in this state
702 * make our check and the vnode_rele atomic
703 * with respect to the current vnode we're working on
704 * by holding the vnode lock
705 * if vnode_rele deferred the vnode_reclaim and has put
706 * this vnode on the list to be reaped by us, than
707 * it has left this vnode with an iocount == 1
709 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
710 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
712 * vnode_rele wanted to do a vnode_reclaim on this vnode
713 * it should be sitting on the head of the uu_vreclaims chain
714 * pull the parent pointer now so that when we do the
715 * vnode_reclaim for each of the vnodes in the uu_vreclaims
716 * list, we won't recurse back through here
718 * need to do a convert here in case vnode_rele_internal
719 * returns with the lock held in the spin mode... it
720 * can drop and retake the lock under certain circumstances
722 vnode_lock_convert(vp
);
725 old_parentvp
= vp
->v_parent
;
726 vp
->v_parent
= NULLVP
;
730 * we're done... we ran into a vnode that isn't
733 old_parentvp
= NULLVP
;
737 ut
->uu_defer_reclaims
= 0;
739 while ( (vp
= ut
->uu_vreclaims
) != NULLVP
) {
740 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
743 * vnode_put will drive the vnode_reclaim if
744 * we are still the only reference on this vnode
753 * Mark a vnode as having multiple hard links. HFS makes use of this
754 * because it keeps track of each link separately, and wants to know
755 * which link was actually used.
757 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
758 * so that HFS can post-process the lookup. Also, volfs will call
759 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
761 void vnode_setmultipath(vnode_t vp
)
766 * In theory, we're changing the vnode's identity as far as the
767 * name cache is concerned, so we ought to grab the name cache lock
768 * here. However, there is already a race, and grabbing the name
769 * cache lock only makes the race window slightly smaller.
771 * The race happens because the vnode already exists in the name
772 * cache, and could be found by one thread before another thread
773 * can set the hard link flag.
776 vp
->v_flag
|= VISHARDLINK
;
784 * backwards compatibility
786 void vnode_uncache_credentials(vnode_t vp
)
788 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
793 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
794 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
795 * we use this lock so that we can look at the v_cred and v_authorized_actions
796 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
797 * which is the super-hot path... if we are updating the authorized actions for this
798 * vnode, we are already in the super-slow and far less frequented path so its not
799 * that bad that we take the lock exclusive for this case... of course we strive
800 * to hold it for the minimum amount of time possible
803 void vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
805 kauth_cred_t tcred
= NOCRED
;
809 vp
->v_authorized_actions
&= ~action
;
811 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
812 IS_VALID_CRED(vp
->v_cred
)) {
814 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
822 kauth_cred_unref(&tcred
);
826 extern int bootarg_vnode_cache_defeat
; /* default = 0, from bsd_init.c */
829 vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
832 boolean_t retval
= FALSE
;
834 /* Boot argument to defeat rights caching */
835 if (bootarg_vnode_cache_defeat
)
838 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
840 * a TTL is enabled on the rights cache... handle it here
841 * a TTL of 0 indicates that no rights should be cached
843 if (vp
->v_mount
->mnt_authcache_ttl
) {
844 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
846 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
847 * we will only allow a SEARCH right on a directory to be cached...
848 * that cached right always has a default TTL associated with it
850 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
)
853 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
854 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
861 ucred
= vfs_context_ucred(ctx
);
863 NAME_CACHE_LOCK_SHARED();
865 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
)
874 void vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
876 kauth_cred_t tcred
= NOCRED
;
879 boolean_t ttl_active
= FALSE
;
881 ucred
= vfs_context_ucred(ctx
);
883 if (!IS_VALID_CRED(ucred
) || action
== 0)
886 if ( (vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
888 * a TTL is enabled on the rights cache... handle it here
889 * a TTL of 0 indicates that no rights should be cached
891 if (vp
->v_mount
->mnt_authcache_ttl
== 0)
894 if ( !(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
) ) {
896 * only cache SEARCH action for filesystems marked
897 * MNTK_AUTH_OPAQUE on VDIRs...
898 * the lookup_path code will time these out
900 if ( (action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
)
909 if (vp
->v_cred
!= ucred
) {
910 kauth_cred_ref(ucred
);
912 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
916 vp
->v_authorized_actions
= 0;
918 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
920 * only reset the timestamnp on the
921 * first authorization cached after the previous
922 * timer has expired or we're switching creds...
923 * 'vnode_cache_is_authorized' will clear the
924 * authorized actions if the TTL is active and
927 vp
->v_cred_timestamp
= tv
.tv_sec
;
929 vp
->v_authorized_actions
|= action
;
933 if (IS_VALID_CRED(tcred
))
934 kauth_cred_unref(&tcred
);
938 boolean_t
vnode_cache_is_stale(vnode_t vp
)
945 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
)
957 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
958 * This errno value should not be seen by anyone outside of the kernel.
961 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
962 vfs_context_t ctx
, int *dp_authorized
, vnode_t last_dp
)
964 char *cp
; /* pointer into pathname argument */
966 int vvid
= 0; /* protected by vp != NULLVP */
968 vnode_t tdp
= NULLVP
;
970 boolean_t ttl_enabled
= FALSE
;
978 #endif /* CONFIG_TRIGGERS */
980 ucred
= vfs_context_ucred(ctx
);
981 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
983 NAME_CACHE_LOCK_SHARED();
985 if ( dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
)) ) {
991 * Search a directory.
993 * The cn_hash value is for use by cache_lookup
994 * The last component of the filename is left accessible via
995 * cnp->cn_nameptr for callers that need the name.
998 cp
= cnp
->cn_nameptr
;
1000 while (*cp
&& (*cp
!= '/')) {
1001 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1004 * the crc generator can legitimately generate
1005 * a 0... however, 0 for us means that we
1006 * haven't computed a hash, so use 1 instead
1010 cnp
->cn_hash
= hash
;
1011 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
1013 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
1017 * Replace multiple slashes by a single slash and trailing slashes
1018 * by a null. This must be done before VNOP_LOOKUP() because some
1019 * fs's don't know about trailing slashes. Remember if there were
1020 * trailing slashes to handle symlinks, existing non-directories
1021 * and non-existing files that won't be directories specially later.
1023 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
1028 ndp
->ni_flag
|= NAMEI_TRAILINGSLASH
;
1029 *ndp
->ni_next
= '\0';
1034 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
1037 cnp
->cn_flags
|= ISLASTCN
;
1039 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.')
1040 cnp
->cn_flags
|= ISDOTDOT
;
1045 * Process a request for a file's resource fork.
1047 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1049 if ((ndp
->ni_pathlen
== sizeof(_PATH_RSRCFORKSPEC
)) &&
1050 (cp
[1] == '.' && cp
[2] == '.') &&
1051 bcmp(cp
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
1052 /* Skip volfs file systems that don't support native streams. */
1053 if ((dp
->v_mount
!= NULL
) &&
1054 (dp
->v_mount
->mnt_flag
& MNT_DOVOLFS
) &&
1055 (dp
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0) {
1058 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
1059 cnp
->cn_flags
|= ISLASTCN
;
1060 ndp
->ni_next
[0] = '\0';
1061 ndp
->ni_pathlen
= 1;
1069 * Name cache provides authorization caching (see below)
1070 * that will short circuit MAC checks in lookup().
1071 * We must perform MAC check here. On denial
1072 * dp_authorized will remain 0 and second check will
1073 * be perfomed in lookup().
1075 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
1076 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
1078 NAME_CACHE_UNLOCK();
1083 if (ttl_enabled
&& ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))
1087 * NAME_CACHE_LOCK holds these fields stable
1089 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
1090 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
))
1094 * indicate that we're allowed to traverse this directory...
1095 * even if we fail the cache lookup or decide to bail for
1096 * some other reason, this information is valid and is used
1097 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1101 if ( (cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
)) ) {
1102 if (cnp
->cn_nameiop
!= LOOKUP
)
1104 if (cnp
->cn_flags
& LOCKPARENT
)
1106 if (cnp
->cn_flags
& NOCACHE
)
1108 if (cnp
->cn_flags
& ISDOTDOT
) {
1110 * Force directory hardlinks to go to
1111 * file system for ".." requests.
1113 if (dp
&& (dp
->v_flag
& VISHARDLINK
)) {
1117 * Quit here only if we can't use
1118 * the parent directory pointer or
1119 * don't have one. Otherwise, we'll
1122 if ((dp
->v_flag
& VROOT
) ||
1123 dp
== ndp
->ni_rootdir
||
1124 dp
->v_parent
== NULLVP
)
1130 * "." and ".." aren't supposed to be cached, so check
1131 * for them before checking the cache.
1133 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
1135 else if ( (cnp
->cn_flags
& ISDOTDOT
) )
1138 if ( (vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
)
1141 if ( (vp
->v_flag
& VISHARDLINK
) ) {
1143 * The file system wants a VNOP_LOOKUP on this vnode
1149 if ( (cnp
->cn_flags
& ISLASTCN
) )
1152 if (vp
->v_type
!= VDIR
) {
1153 if (vp
->v_type
!= VLNK
)
1158 if ( (mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1160 if (mp
->mnt_realrootvp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1161 mp
->mnt_realrootvp_vid
!= mp
->mnt_realrootvp
->v_id
)
1163 vp
= mp
->mnt_realrootvp
;
1168 * After traversing all mountpoints stacked here, if we have a
1169 * trigger in hand, resolve it. Note that we don't need to
1170 * leave the fast path if the mount has already happened.
1172 if ((vp
->v_resolve
!= NULL
) &&
1173 (vp
->v_resolve
->vr_resolve_func
!= NULL
)) {
1176 #endif /* CONFIG_TRIGGERS */
1182 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1184 while (*cnp
->cn_nameptr
== '/') {
1193 NAME_CACHE_UNLOCK();
1195 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1196 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
1198 * if we've got a child and it's the last component, and
1199 * the lookup doesn't need to return the parent then we
1200 * can skip grabbing an iocount on the parent, since all
1201 * we're going to do with it is a vnode_put just before
1202 * we return from 'lookup'. If it's a symbolic link,
1203 * we need the parent in case the link happens to be
1204 * a relative pathname.
1211 * return the last directory we looked at
1212 * with an io reference held. If it was the one passed
1213 * in as a result of the last iteration of VNOP_LOOKUP,
1214 * it should already hold an io ref. No need to increase ref.
1218 if (dp
== ndp
->ni_usedvp
) {
1220 * if this vnode matches the one passed in via USEDVP
1221 * than this context already holds an io_count... just
1222 * use vnode_get to get an extra ref for lookup to play
1223 * with... can't use the getwithvid variant here because
1224 * it will block behind a vnode_drain which would result
1225 * in a deadlock (since we already own an io_count that the
1226 * vnode_drain is waiting on)... vnode_get grabs the io_count
1227 * immediately w/o waiting... it always succeeds
1230 } else if ( (vnode_getwithvid_drainok(dp
, vid
)) ) {
1232 * failure indicates the vnode
1233 * changed identity or is being
1234 * TERMINATED... in either case
1237 * don't necessarily return ENOENT, though, because
1238 * we really want to go back to disk and make sure it's
1239 * there or not if someone else is changing this
1248 if ( (vnode_getwithvid_drainok(vp
, vvid
)) ) {
1252 * can't get reference on the vp we'd like
1253 * to return... if we didn't grab a reference
1254 * on the directory (due to fast path bypass),
1255 * then we need to do it now... we can't return
1256 * with both ni_dvp and ni_vp NULL, and no
1270 trigger_vp
= vp
? vp
: dp
;
1271 if ((error
== 0) && (trigger_vp
!= NULLVP
) && vnode_isdir(trigger_vp
)) {
1272 error
= vnode_trigger_resolve(trigger_vp
, ndp
, ctx
);
1281 #endif /* CONFIG_TRIGGERS */
1285 * If we came into cache_lookup_path after an iteration of the lookup loop that
1286 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1287 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1288 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1289 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1290 * the case that we got to a subsequent path component and this previous vnode is
1291 * no longer needed. We can then drop the io ref on it.
1293 if ((last_dp
!= NULLVP
) && (last_dp
!= ndp
->ni_dvp
)){
1297 //initialized to 0, should be the same if no error cases occurred.
1303 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1305 struct namecache
*ncp
;
1306 struct nchashhead
*ncpp
;
1307 long namelen
= cnp
->cn_namelen
;
1308 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1314 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1315 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1316 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1317 if (memcmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1323 * We failed to find an entry
1328 NCHSTAT(ncs_goodhits
);
1330 return (ncp
->nc_vp
);
1335 // Have to take a len argument because we may only need to
1336 // hash part of a componentname.
1339 hash_string(const char *cp
, int len
)
1345 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1348 while (*cp
!= '\0') {
1349 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1353 * the crc generator can legitimately generate
1354 * a 0... however, 0 for us means that we
1355 * haven't computed a hash, so use 1 instead
1364 * Lookup an entry in the cache
1366 * We don't do this if the segment name is long, simply so the cache
1367 * can avoid holding long names (which would either waste space, or
1368 * add greatly to the complexity).
1370 * Lookup is called with dvp pointing to the directory to search,
1371 * cnp pointing to the name of the entry being sought. If the lookup
1372 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1373 * returned. If the lookup determines that the name does not exist
1374 * (negative cacheing), a status of ENOENT is returned. If the lookup
1375 * fails, a status of zero is returned.
1379 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1381 struct namecache
*ncp
;
1382 struct nchashhead
*ncpp
;
1383 long namelen
= cnp
->cn_namelen
;
1384 unsigned int hashval
;
1385 boolean_t have_exclusive
= FALSE
;
1389 if (cnp
->cn_hash
== 0)
1390 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1391 hashval
= (cnp
->cn_hash
& NCHASHMASK
);
1397 NAME_CACHE_LOCK_SHARED();
1400 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1401 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1402 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1403 if (memcmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
1407 /* We failed to find an entry */
1410 NAME_CACHE_UNLOCK();
1414 /* We don't want to have an entry, so dump it */
1415 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
1416 if (have_exclusive
== TRUE
) {
1417 NCHSTAT(ncs_badhits
);
1418 cache_delete(ncp
, 1);
1419 NAME_CACHE_UNLOCK();
1422 NAME_CACHE_UNLOCK();
1424 have_exclusive
= TRUE
;
1429 /* We found a "positive" match, return the vnode */
1431 NCHSTAT(ncs_goodhits
);
1434 NAME_CACHE_UNLOCK();
1436 if (vnode_getwithvid(vp
, vid
)) {
1439 NCHSTAT(ncs_badvid
);
1440 NAME_CACHE_UNLOCK();
1448 /* We found a negative match, and want to create it, so purge */
1449 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
1450 if (have_exclusive
== TRUE
) {
1451 NCHSTAT(ncs_badhits
);
1452 cache_delete(ncp
, 1);
1453 NAME_CACHE_UNLOCK();
1456 NAME_CACHE_UNLOCK();
1458 have_exclusive
= TRUE
;
1463 * We found a "negative" match, ENOENT notifies client of this match.
1464 * The nc_whiteout field records whether this is a whiteout.
1466 NCHSTAT(ncs_neghits
);
1468 if (ncp
->nc_whiteout
)
1469 cnp
->cn_flags
|= ISWHITEOUT
;
1470 NAME_CACHE_UNLOCK();
1475 cache_enter_create(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
)
1477 const char *strname
;
1479 if (cnp
->cn_hash
== 0)
1480 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1483 * grab 2 references on the string entered
1484 * one for the cache_enter_locked to consume
1485 * and the second to be consumed by v_name (vnode_create call point)
1487 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, TRUE
, 0);
1491 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1493 NAME_CACHE_UNLOCK();
1500 * Add an entry to the cache...
1501 * but first check to see if the directory
1502 * that this entry is to be associated with has
1503 * had any cache_purges applied since we took
1504 * our identity snapshot... this check needs to
1505 * be done behind the name cache lock
1508 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
1511 if (cnp
->cn_hash
== 0)
1512 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1516 if (dvp
->v_nc_generation
== gen
)
1517 (void)cache_enter_locked(dvp
, vp
, cnp
, NULL
);
1519 NAME_CACHE_UNLOCK();
1524 * Add an entry to the cache.
1527 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
1529 const char *strname
;
1531 if (cnp
->cn_hash
== 0)
1532 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1535 * grab 1 reference on the string entered
1536 * for the cache_enter_locked to consume
1538 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
1542 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1544 NAME_CACHE_UNLOCK();
1549 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, const char *strname
)
1551 struct namecache
*ncp
, *negp
;
1552 struct nchashhead
*ncpp
;
1558 * if the entry is for -ve caching vp is null
1560 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
1562 * someone beat us to the punch..
1563 * this vnode is already in the cache
1565 if (strname
!= NULL
)
1566 vfs_removename(strname
);
1570 * We allocate a new entry if we are less than the maximum
1571 * allowed and the one at the front of the list is in use.
1572 * Otherwise we use the one at the front of the list.
1574 if (numcache
< desiredNodes
&&
1575 ((ncp
= nchead
.tqh_first
) == NULL
||
1576 ncp
->nc_hash
.le_prev
!= 0)) {
1578 * Allocate one more entry
1580 ncp
= (struct namecache
*)_MALLOC_ZONE(sizeof(*ncp
), M_CACHE
, M_WAITOK
);
1584 * reuse an old entry
1586 ncp
= TAILQ_FIRST(&nchead
);
1587 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1589 if (ncp
->nc_hash
.le_prev
!= 0) {
1591 * still in use... we need to
1592 * delete it before re-using it
1594 NCHSTAT(ncs_stolen
);
1595 cache_delete(ncp
, 0);
1598 NCHSTAT(ncs_enters
);
1601 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
1605 ncp
->nc_hashval
= cnp
->cn_hash
;
1606 ncp
->nc_whiteout
= FALSE
;
1608 if (strname
== NULL
)
1609 ncp
->nc_name
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
1611 ncp
->nc_name
= strname
;
1613 * make us the newest entry in the cache
1614 * i.e. we'll be the last to be stolen
1616 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
1618 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1621 struct namecache
*p
;
1623 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
)
1625 panic("cache_enter: duplicate");
1629 * make us available to be found via lookup
1631 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
1635 * add to the list of name cache entries
1638 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
1641 * this is a negative cache entry (vp == NULL)
1642 * stick it on the negative cache list
1643 * and record the whiteout state
1645 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
1647 if (cnp
->cn_flags
& ISWHITEOUT
)
1648 ncp
->nc_whiteout
= TRUE
;
1651 if (ncs_negtotal
> desiredNegNodes
) {
1653 * if we've reached our desired limit
1654 * of negative cache entries, delete
1657 negp
= TAILQ_FIRST(&neghead
);
1658 cache_delete(negp
, 1);
1662 * add us to the list of name cache entries that
1663 * are children of dvp
1665 LIST_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
1670 * Initialize CRC-32 remainder table.
1672 static void init_crc32(void)
1675 * the CRC-32 generator polynomial is:
1676 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1677 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1679 unsigned int crc32_polynomial
= 0x04c11db7;
1683 * pre-calculate the CRC-32 remainder for each possible octet encoding
1685 for (i
= 0; i
< 256; i
++) {
1686 unsigned int crc_rem
= i
<< 24;
1688 for (j
= 0; j
< 8; j
++) {
1689 if (crc_rem
& 0x80000000)
1690 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
1692 crc_rem
= (crc_rem
<< 1);
1694 crc32tab
[i
] = crc_rem
;
1700 * Name cache initialization, from vfs_init() when we are booting
1707 desiredNegNodes
= (desiredvnodes
/ 10);
1708 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
1710 TAILQ_INIT(&nchead
);
1711 TAILQ_INIT(&neghead
);
1715 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 *desiredNodes
)), M_CACHE
, &nchash
);
1716 nchashmask
= nchash
;
1719 init_string_table();
1721 /* Allocate name cache lock group attribute and group */
1722 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
1724 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
1726 /* Allocate name cache lock attribute */
1727 namecache_lck_attr
= lck_attr_alloc_init();
1729 /* Allocate name cache lock */
1730 namecache_rw_lock
= lck_rw_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
1733 /* Allocate string cache lock group attribute and group */
1734 strcache_lck_grp_attr
= lck_grp_attr_alloc_init();
1736 strcache_lck_grp
= lck_grp_alloc_init("String Cache", strcache_lck_grp_attr
);
1738 /* Allocate string cache lock attribute */
1739 strcache_lck_attr
= lck_attr_alloc_init();
1741 /* Allocate string cache lock */
1742 strtable_rw_lock
= lck_rw_alloc_init(strcache_lck_grp
, strcache_lck_attr
);
1744 for (i
= 0; i
< NUM_STRCACHE_LOCKS
; i
++)
1745 lck_mtx_init(&strcache_mtx_locks
[i
], strcache_lck_grp
, strcache_lck_attr
);
1749 name_cache_lock_shared(void)
1751 lck_rw_lock_shared(namecache_rw_lock
);
1755 name_cache_lock(void)
1757 lck_rw_lock_exclusive(namecache_rw_lock
);
1761 name_cache_unlock(void)
1763 lck_rw_done(namecache_rw_lock
);
1768 resize_namecache(u_int newsize
)
1770 struct nchashhead
*new_table
;
1771 struct nchashhead
*old_table
;
1772 struct nchashhead
*old_head
, *head
;
1773 struct namecache
*entry
, *next
;
1774 uint32_t i
, hashval
;
1775 int dNodes
, dNegNodes
;
1776 u_long new_size
, old_size
;
1778 dNegNodes
= (newsize
/ 10);
1779 dNodes
= newsize
+ dNegNodes
;
1781 // we don't support shrinking yet
1782 if (dNodes
<= desiredNodes
) {
1785 new_table
= hashinit(2 * dNodes
, M_CACHE
, &nchashmask
);
1786 new_size
= nchashmask
+ 1;
1788 if (new_table
== NULL
) {
1794 old_table
= nchashtbl
;
1795 nchashtbl
= new_table
;
1799 // walk the old table and insert all the entries into
1802 for(i
=0; i
< old_size
; i
++) {
1803 old_head
= &old_table
[i
];
1804 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1806 // XXXdbg - Beware: this assumes that hash_string() does
1807 // the same thing as what happens in
1808 // lookup() over in vfs_lookup.c
1809 hashval
= hash_string(entry
->nc_name
, 0);
1810 entry
->nc_hashval
= hashval
;
1811 head
= NCHHASH(entry
->nc_dvp
, hashval
);
1813 next
= entry
->nc_hash
.le_next
;
1814 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
1817 desiredNodes
= dNodes
;
1818 desiredNegNodes
= dNegNodes
;
1820 NAME_CACHE_UNLOCK();
1821 FREE(old_table
, M_CACHE
);
1827 cache_delete(struct namecache
*ncp
, int age_entry
)
1829 NCHSTAT(ncs_deletes
);
1832 LIST_REMOVE(ncp
, nc_un
.nc_link
);
1834 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
1837 LIST_REMOVE(ncp
, nc_child
);
1839 LIST_REMOVE(ncp
, nc_hash
);
1841 * this field is used to indicate
1842 * that the entry is in use and
1843 * must be deleted before it can
1846 ncp
->nc_hash
.le_prev
= NULL
;
1850 * make it the next one available
1851 * for cache_enter's use
1853 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1854 TAILQ_INSERT_HEAD(&nchead
, ncp
, nc_entry
);
1856 vfs_removename(ncp
->nc_name
);
1857 ncp
->nc_name
= NULL
;
1862 * purge the entry associated with the
1863 * specified vnode from the name cache
1866 cache_purge(vnode_t vp
)
1868 struct namecache
*ncp
;
1869 kauth_cred_t tcred
= NULL
;
1871 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
1872 (LIST_FIRST(&vp
->v_ncchildren
) == NULL
) &&
1873 (vp
->v_cred
== NOCRED
) &&
1874 (vp
->v_parent
== NULLVP
))
1880 vp
->v_parent
->v_nc_generation
++;
1882 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
1883 cache_delete(ncp
, 1);
1885 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
1886 cache_delete(ncp
, 1);
1889 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1892 vp
->v_cred
= NOCRED
;
1893 vp
->v_authorized_actions
= 0;
1895 NAME_CACHE_UNLOCK();
1897 if (IS_VALID_CRED(tcred
))
1898 kauth_cred_unref(&tcred
);
1902 * Purge all negative cache entries that are children of the
1903 * given vnode. A case-insensitive file system (or any file
1904 * system that has multiple equivalent names for the same
1905 * directory entry) can use this when creating or renaming
1906 * to remove negative entries that may no longer apply.
1909 cache_purge_negatives(vnode_t vp
)
1911 struct namecache
*ncp
, *next_ncp
;
1915 LIST_FOREACH_SAFE(ncp
, &vp
->v_ncchildren
, nc_child
, next_ncp
)
1916 if (ncp
->nc_vp
== NULL
)
1917 cache_delete(ncp
, 1);
1919 NAME_CACHE_UNLOCK();
1923 * Flush all entries referencing a particular filesystem.
1925 * Since we need to check it anyway, we will flush all the invalid
1926 * entries at the same time.
1929 cache_purgevfs(struct mount
*mp
)
1931 struct nchashhead
*ncpp
;
1932 struct namecache
*ncp
;
1935 /* Scan hash tables for applicable entries */
1936 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
1938 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
1939 if (ncp
->nc_dvp
->v_mount
== mp
) {
1940 cache_delete(ncp
, 0);
1945 NAME_CACHE_UNLOCK();
1951 // String ref routines
1953 static LIST_HEAD(stringhead
, string_t
) *string_ref_table
;
1954 static u_long string_table_mask
;
1955 static uint32_t filled_buckets
=0;
1958 typedef struct string_t
{
1959 LIST_ENTRY(string_t
) hash_chain
;
1966 resize_string_ref_table(void)
1968 struct stringhead
*new_table
;
1969 struct stringhead
*old_table
;
1970 struct stringhead
*old_head
, *head
;
1971 string_t
*entry
, *next
;
1972 uint32_t i
, hashval
;
1973 u_long new_mask
, old_mask
;
1976 * need to hold the table lock exclusively
1977 * in order to grow the table... need to recheck
1978 * the need to resize again after we've taken
1979 * the lock exclusively in case some other thread
1980 * beat us to the punch
1982 lck_rw_lock_exclusive(strtable_rw_lock
);
1984 if (4 * filled_buckets
< ((string_table_mask
+ 1) * 3)) {
1985 lck_rw_done(strtable_rw_lock
);
1988 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
1990 if (new_table
== NULL
) {
1991 printf("failed to resize the hash table.\n");
1992 lck_rw_done(strtable_rw_lock
);
1997 old_table
= string_ref_table
;
1998 string_ref_table
= new_table
;
1999 old_mask
= string_table_mask
;
2000 string_table_mask
= new_mask
;
2003 // walk the old table and insert all the entries into
2006 for (i
= 0; i
<= old_mask
; i
++) {
2007 old_head
= &old_table
[i
];
2008 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2009 hashval
= hash_string((const char *)entry
->str
, 0);
2010 head
= &string_ref_table
[hashval
& string_table_mask
];
2011 if (head
->lh_first
== NULL
) {
2014 next
= entry
->hash_chain
.le_next
;
2015 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2018 lck_rw_done(strtable_rw_lock
);
2020 FREE(old_table
, M_CACHE
);
2025 init_string_table(void)
2027 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
2032 vfs_addname(const char *name
, uint32_t len
, u_int hashval
, u_int flags
)
2034 return (add_name_internal(name
, len
, hashval
, FALSE
, flags
));
2039 add_name_internal(const char *name
, uint32_t len
, u_int hashval
, boolean_t need_extra_ref
, __unused u_int flags
)
2041 struct stringhead
*head
;
2043 uint32_t chain_len
= 0;
2044 uint32_t hash_index
;
2045 uint32_t lock_index
;
2049 * if the length already accounts for the null-byte, then
2050 * subtract one so later on we don't index past the end
2053 if (len
> 0 && name
[len
-1] == '\0') {
2057 hashval
= hash_string(name
, len
);
2061 * take this lock 'shared' to keep the hash stable
2062 * if someone else decides to grow the pool they
2063 * will take this lock exclusively
2065 lck_rw_lock_shared(strtable_rw_lock
);
2068 * If the table gets more than 3/4 full, resize it
2070 if (4 * filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
2071 lck_rw_done(strtable_rw_lock
);
2073 resize_string_ref_table();
2075 lck_rw_lock_shared(strtable_rw_lock
);
2077 hash_index
= hashval
& string_table_mask
;
2078 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2080 head
= &string_ref_table
[hash_index
];
2082 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2084 for (entry
= head
->lh_first
; entry
!= NULL
; chain_len
++, entry
= entry
->hash_chain
.le_next
) {
2085 if (memcmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == 0) {
2090 if (entry
== NULL
) {
2091 lck_mtx_convert_spin(&strcache_mtx_locks
[lock_index
]);
2093 * it wasn't already there so add it.
2095 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
2097 if (head
->lh_first
== NULL
) {
2098 OSAddAtomic(1, &filled_buckets
);
2100 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
2101 strncpy(ptr
, name
, len
);
2104 entry
->refcount
= 1;
2105 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2107 if (need_extra_ref
== TRUE
)
2110 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2111 lck_rw_done(strtable_rw_lock
);
2113 return (const char *)entry
->str
;
2118 vfs_removename(const char *nameref
)
2120 struct stringhead
*head
;
2123 uint32_t hash_index
;
2124 uint32_t lock_index
;
2125 int retval
= ENOENT
;
2127 hashval
= hash_string(nameref
, 0);
2130 * take this lock 'shared' to keep the hash stable
2131 * if someone else decides to grow the pool they
2132 * will take this lock exclusively
2134 lck_rw_lock_shared(strtable_rw_lock
);
2136 * must compute the head behind the table lock
2137 * since the size and location of the table
2138 * can change on the fly
2140 hash_index
= hashval
& string_table_mask
;
2141 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2143 head
= &string_ref_table
[hash_index
];
2145 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2147 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2148 if (entry
->str
== nameref
) {
2151 if (entry
->refcount
== 0) {
2152 LIST_REMOVE(entry
, hash_chain
);
2154 if (head
->lh_first
== NULL
) {
2155 OSAddAtomic(-1, &filled_buckets
);
2164 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2165 lck_rw_done(strtable_rw_lock
);
2168 FREE(entry
, M_TEMP
);
2174 #ifdef DUMP_STRING_TABLE
2176 dump_string_table(void)
2178 struct stringhead
*head
;
2182 lck_rw_lock_shared(strtable_rw_lock
);
2184 for (i
= 0; i
<= string_table_mask
; i
++) {
2185 head
= &string_ref_table
[i
];
2186 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
2187 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
2190 lck_rw_done(strtable_rw_lock
);
2192 #endif /* DUMP_STRING_TABLE */