2 * Copyright (c) 2000-2015 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 <miscfs/specfs/specdev.h>
79 #include <sys/namei.h>
80 #include <sys/errno.h>
81 #include <sys/malloc.h>
82 #include <sys/kauth.h>
84 #include <sys/paths.h>
85 #include <os/overflow.h>
88 #include <security/mac_framework.h>
92 * Name caching works as follows:
94 * Names found by directory scans are retained in a cache
95 * for future reference. It is managed LRU, so frequently
96 * used names will hang around. Cache is indexed by hash value
97 * obtained from (vp, name) where vp refers to the directory
100 * If it is a "negative" entry, (i.e. for a name that is known NOT to
101 * exist) the vnode pointer will be NULL.
103 * Upon reaching the last segment of a path, if the reference
104 * is for DELETE, or NOCACHE is set (rewrite), and the
105 * name is located in the cache, it will be dropped.
109 * Structures associated with name cacheing.
112 LIST_HEAD(nchashhead
, namecache
) * nchashtbl
; /* Hash Table */
114 u_long nchash
; /* size of hash table - 1 */
115 long numcache
; /* number of cache entries allocated */
120 TAILQ_HEAD(, namecache
) nchead
; /* chain of all name cache entries */
121 TAILQ_HEAD(, namecache
) neghead
; /* chain of only negative cache entries */
126 struct nchstats nchstats
; /* cache effectiveness statistics */
128 #define NCHSTAT(v) { \
131 #define NAME_CACHE_LOCK() name_cache_lock()
132 #define NAME_CACHE_UNLOCK() name_cache_unlock()
133 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
138 #define NAME_CACHE_LOCK() name_cache_lock()
139 #define NAME_CACHE_UNLOCK() name_cache_unlock()
140 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
145 /* vars for name cache list lock */
146 lck_grp_t
* namecache_lck_grp
;
147 lck_grp_attr_t
* namecache_lck_grp_attr
;
148 lck_attr_t
* namecache_lck_attr
;
150 lck_grp_t
* strcache_lck_grp
;
151 lck_grp_attr_t
* strcache_lck_grp_attr
;
152 lck_attr_t
* strcache_lck_attr
;
154 lck_rw_t
* namecache_rw_lock
;
155 lck_rw_t
* strtable_rw_lock
;
157 #define NUM_STRCACHE_LOCKS 1024
159 lck_mtx_t strcache_mtx_locks
[NUM_STRCACHE_LOCKS
];
162 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
163 static const char *add_name_internal(const char *, uint32_t, u_int
, boolean_t
, u_int
);
164 static void init_string_table(void);
165 static void cache_delete(struct namecache
*, int);
166 static void cache_enter_locked(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, const char *strname
);
168 #ifdef DUMP_STRING_TABLE
170 * Internal dump function used for debugging
172 void dump_string_table(void);
173 #endif /* DUMP_STRING_TABLE */
175 static void init_crc32(void);
176 static unsigned int crc32tab
[256];
179 #define NCHHASH(dvp, hash_val) \
180 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
183 * This function tries to check if a directory vp is a subdirectory of dvp
184 * only from valid v_parent pointers. It is called with the name cache lock
185 * held and does not drop the lock anytime inside the function.
187 * It returns a boolean that indicates whether or not it was able to
188 * successfully infer the parent/descendent relationship via the v_parent
189 * pointers, or if it could not infer such relationship and that the decision
190 * must be delegated to the owning filesystem.
192 * If it does not defer the decision, i.e. it was successfuly able to determine
193 * the parent/descendent relationship, *is_subdir tells the caller if vp is a
194 * subdirectory of dvp.
196 * If the decision is deferred, *next_vp is where it stopped i.e. *next_vp
197 * is the vnode whose parent is to be determined from the filesystem.
198 * *is_subdir, in this case, is not indicative of anything and should be
201 * The return value and output args should be used as follows :
203 * defer = cache_check_vnode_issubdir(vp, dvp, is_subdir, next_vp);
206 * vp is subdirectory;
208 * vp is not a subdirectory;
211 * check this vnode's parent from the filesystem
213 * error (likely because of forced unmount).
218 cache_check_vnode_issubdir(vnode_t vp
, vnode_t dvp
, boolean_t
*is_subdir
,
232 } else if (tvp
== rootvnode
) {
233 /* *is_subdir = FALSE */
238 while ((tvp
->v_flag
& VROOT
) && tmp
&& tmp
->mnt_vnodecovered
&&
239 tvp
!= dvp
&& tvp
!= rootvnode
) {
240 tvp
= tmp
->mnt_vnodecovered
;
245 * If dvp is not at the top of a mount "stack" then
246 * vp is not a subdirectory of dvp either.
248 if (tvp
== dvp
|| tvp
== rootvnode
) {
249 /* *is_subdir = FALSE */
259 if ((tvp
->v_flag
& VISHARDLINK
) || !(tvp
->v_parent
)) {
271 /* maximum times retry from potentially transient errors in vnode_issubdir */
272 #define MAX_ERROR_RETRY 3
275 * This function checks if a given directory (vp) is a subdirectory of dvp.
276 * It walks backwards from vp and if it hits dvp in its parent chain,
277 * it is a subdirectory. If it encounters the root directory, it is not
280 * This function returns an error if it is unsuccessful and 0 on success.
282 * On entry (and exit) vp has an iocount and if this function has to take
283 * any iocounts on other vnodes in the parent chain traversal, it releases them.
286 vnode_issubdir(vnode_t vp
, vnode_t dvp
, int *is_subdir
, vfs_context_t ctx
)
288 vnode_t start_vp
, tvp
;
289 vnode_t vp_with_iocount
;
291 char dotdotbuf
[] = "..";
292 int error_retry_count
= 0; /* retry count for potentially transient
298 * Anytime we acquire an iocount in this function, we save the vnode
299 * in this variable and release it before exiting.
301 vp_with_iocount
= NULLVP
;
307 struct componentname cn
;
308 boolean_t is_subdir_locked
= FALSE
;
313 } else if (tvp
== rootvnode
) {
314 /* *is_subdir = FALSE */
318 NAME_CACHE_LOCK_SHARED();
320 defer
= cache_check_vnode_issubdir(tvp
, dvp
, &is_subdir_locked
,
324 vid
= vnode_vid(tvp
);
330 *is_subdir
= is_subdir_locked
;
335 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
343 if (tvp
!= start_vp
) {
344 if (vp_with_iocount
) {
345 vnode_put(vp_with_iocount
);
346 vp_with_iocount
= NULLVP
;
349 error
= vnode_getwithvid(tvp
, vid
);
351 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
359 vp_with_iocount
= tvp
;
362 bzero(&cn
, sizeof(cn
));
363 cn
.cn_nameiop
= LOOKUP
;
364 cn
.cn_flags
= ISLASTCN
| ISDOTDOT
;
366 cn
.cn_pnbuf
= &dotdotbuf
[0];
367 cn
.cn_pnlen
= sizeof(dotdotbuf
);
368 cn
.cn_nameptr
= cn
.cn_pnbuf
;
372 if ((error
= VNOP_LOOKUP(tvp
, &pvp
, &cn
, ctx
))) {
376 if (!(tvp
->v_flag
& VISHARDLINK
) && tvp
->v_parent
!= pvp
) {
377 (void)vnode_update_identity(tvp
, pvp
, NULL
, 0, 0,
378 VNODE_UPDATE_PARENT
);
381 if (vp_with_iocount
) {
382 vnode_put(vp_with_iocount
);
385 vp_with_iocount
= tvp
= pvp
;
388 if (vp_with_iocount
) {
389 vnode_put(vp_with_iocount
);
396 * This function builds the path in "buff" from the supplied vnode.
397 * The length of the buffer *INCLUDING* the trailing zero byte is
398 * returned in outlen. NOTE: the length includes the trailing zero
399 * byte and thus the length is one greater than what strlen would
400 * return. This is important and lots of code elsewhere in the kernel
401 * assumes this behavior.
403 * This function can call vnop in file system if the parent vnode
404 * does not exist or when called for hardlinks via volfs path.
405 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
406 * in the name cache and does not enter the file system.
408 * If BUILDPATH_CHECK_MOVED is set in flags, we return EAGAIN when
409 * we encounter ENOENT during path reconstruction. ENOENT means that
410 * one of the parents moved while we were building the path. The
411 * caller can special handle this case by calling build_path again.
413 * If BUILDPATH_VOLUME_RELATIVE is set in flags, we return path
414 * that is relative to the nearest mount point, i.e. do not
415 * cross over mount points during building the path.
417 * passed in vp must have a valid io_count reference
419 * If parent vnode is non-NULL it also must have an io count. This
420 * allows build_path_with_parent to be safely called for operations
421 * unlink, rmdir and rename that already have io counts on the target
422 * and the directory. In this way build_path_with_parent does not have
423 * to try and obtain an additional io count on the parent. Taking an
424 * io count ont the parent can lead to dead lock if a forced unmount
425 * occures at the right moment. For a fuller explaination on how this
426 * can occur see the comment for vn_getpath_with_parent.
430 build_path_with_parent(vnode_t first_vp
, vnode_t parent_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
433 vnode_t vp_with_iocount
;
434 vnode_t proc_root_dir_vp
;
441 if (first_vp
== NULLVP
) {
450 * Grab the process fd so we can evaluate fd_rdir.
452 if (vfs_context_proc(ctx
)->p_fd
) {
453 proc_root_dir_vp
= vfs_context_proc(ctx
)->p_fd
->fd_rdir
;
455 proc_root_dir_vp
= NULL
;
458 vp_with_iocount
= NULLVP
;
462 end
= &buff
[buflen
- 1];
466 * holding the NAME_CACHE_LOCK in shared mode is
467 * sufficient to stabilize both the vp->v_parent chain
468 * and the 'vp->v_mount->mnt_vnodecovered' chain
470 * if we need to drop this lock, we must first grab the v_id
471 * from the vnode we're currently working with... if that
472 * vnode doesn't already have an io_count reference (the vp
473 * passed in comes with one), we must grab a reference
474 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
475 * deadlocks may result if you call vnode_get while holding
476 * the NAME_CACHE_LOCK... we lazily release the reference
477 * we pick up the next time we encounter a need to drop
478 * the NAME_CACHE_LOCK or before we return from this routine
480 NAME_CACHE_LOCK_SHARED();
483 * Check if this is the root of a file system.
485 while (vp
&& vp
->v_flag
& VROOT
) {
486 if (vp
->v_mount
== NULL
) {
490 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
492 * It's the root of the root file system, so it's
500 * This the root of the volume and the caller does not
501 * want to cross mount points. Therefore just return
502 * '/' as the relative path.
504 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
508 vp
= vp
->v_mount
->mnt_vnodecovered
;
513 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
517 * For hardlinks the v_name may be stale, so if its OK
518 * to enter a file system, ask the file system for the
519 * name and parent (below).
521 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
522 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
523 !(flags
& BUILDPATH_NO_FS_ENTER
);
528 if (str
== NULL
|| *str
== '\0') {
529 if (vp
->v_parent
!= NULL
) {
538 * Check that there's enough space (including space for the '/')
540 if ((end
- buff
) < (len
+ 1)) {
545 * Copy the name backwards.
549 for (; len
> 0; len
--) {
553 * Add a path separator.
559 * Walk up the parent chain.
561 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
562 (flags
& BUILDPATH_NO_FS_ENTER
)) {
564 * In this if () block we are not allowed to enter the filesystem
565 * to conclusively get the most accurate parent identifier.
566 * As a result, if 'vp' does not identify '/' and it
567 * does not have a valid v_parent, then error out
568 * and disallow further path construction
570 if ((vp
->v_parent
== NULLVP
) && (rootvnode
!= vp
)) {
572 * Only '/' is allowed to have a NULL parent
573 * pointer. Upper level callers should ideally
574 * re-drive name lookup on receiving a ENOENT.
578 /* The code below will exit early if 'tvp = vp' == NULL */
583 * if the vnode we have in hand isn't a directory and it
584 * has a v_parent, then we started with the resource fork
585 * so skip up to avoid getting a duplicate copy of the
586 * file name in the path.
588 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
593 * No parent, go get it if supported.
595 struct vnode_attr va
;
599 * Make sure file system supports obtaining a path from id.
601 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
609 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
610 if (vp_with_iocount
) {
611 vnode_put(vp_with_iocount
);
612 vp_with_iocount
= NULLVP
;
614 if (vnode_getwithvid(vp
, vid
)) {
617 vp_with_iocount
= vp
;
620 VATTR_WANTED(&va
, va_parentid
);
623 VATTR_WANTED(&va
, va_name
);
624 MALLOC_ZONE(va
.va_name
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
629 * Ask the file system for its parent id and for its name (optional).
631 ret
= vnode_getattr(vp
, &va
, ctx
);
634 if ((ret
== 0) && (VATTR_IS_SUPPORTED(&va
, va_name
))) {
636 vnode_update_identity(vp
, NULL
, str
, strlen(str
), 0, VNODE_UPDATE_NAME
);
637 } else if (vp
->v_name
) {
647 * Check that there's enough space.
649 if ((end
- buff
) < (len
+ 1)) {
652 /* Copy the name backwards. */
655 for (; len
> 0; len
--) {
659 * Add a path separator.
664 FREE_ZONE(va
.va_name
, MAXPATHLEN
, M_NAMEI
);
666 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
671 * Ask the file system for the parent vnode.
673 if ((ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
))) {
677 if (!fixhardlink
&& (vp
->v_parent
!= dvp
)) {
678 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
681 if (vp_with_iocount
) {
682 vnode_put(vp_with_iocount
);
685 vp_with_iocount
= vp
;
687 NAME_CACHE_LOCK_SHARED();
690 * if the vnode we have in hand isn't a directory and it
691 * has a v_parent, then we started with the resource fork
692 * so skip up to avoid getting a duplicate copy of the
693 * file name in the path.
695 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
700 if (vp
&& (flags
& BUILDPATH_CHECKACCESS
)) {
705 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
706 if (vp_with_iocount
) {
707 vnode_put(vp_with_iocount
);
708 vp_with_iocount
= NULLVP
;
710 if (vnode_getwithvid(vp
, vid
)) {
713 vp_with_iocount
= vp
;
715 if ((ret
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
))) {
716 goto out
; /* no peeking */
718 NAME_CACHE_LOCK_SHARED();
722 * When a mount point is crossed switch the vp.
723 * Continue until we find the root or we find
724 * a vnode that's not the root of a mounted
730 if (tvp
== proc_root_dir_vp
) {
731 goto out_unlock
; /* encountered the root */
733 if (!(tvp
->v_flag
& VROOT
) || !tvp
->v_mount
) {
734 break; /* not the root of a mounted FS */
736 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
737 /* Do not cross over mount points */
740 tvp
= tvp
->v_mount
->mnt_vnodecovered
;
751 if (vp_with_iocount
) {
752 vnode_put(vp_with_iocount
);
755 * Slide the name down to the beginning of the buffer.
757 memmove(buff
, end
, &buff
[buflen
] - end
);
760 * length includes the trailing zero byte
762 *outlen
= &buff
[buflen
] - end
;
764 /* One of the parents was moved during path reconstruction.
765 * The caller is interested in knowing whether any of the
766 * parents moved via BUILDPATH_CHECK_MOVED, so return EAGAIN.
768 if ((ret
== ENOENT
) && (flags
& BUILDPATH_CHECK_MOVED
)) {
776 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
778 return build_path_with_parent(first_vp
, NULL
, buff
, buflen
, outlen
, flags
, ctx
);
782 * return NULLVP if vp's parent doesn't
783 * exist, or we can't get a valid iocount
784 * else return the parent of vp
787 vnode_getparent(vnode_t vp
)
789 vnode_t pvp
= NULLVP
;
792 NAME_CACHE_LOCK_SHARED();
794 * v_parent is stable behind the name_cache lock
795 * however, the only thing we can really guarantee
796 * is that we've grabbed a valid iocount on the
797 * parent of 'vp' at the time we took the name_cache lock...
798 * once we drop the lock, vp could get re-parented
800 if ((pvp
= vp
->v_parent
) != NULLVP
) {
805 if (vnode_getwithvid(pvp
, pvid
) != 0) {
815 vnode_getname(vnode_t vp
)
817 const char *name
= NULL
;
819 NAME_CACHE_LOCK_SHARED();
822 name
= vfs_addname(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
830 vnode_putname(const char *name
)
832 vfs_removename(name
);
835 static const char unknown_vnodename
[] = "(unknown vnode name)";
838 vnode_getname_printable(vnode_t vp
)
840 const char *name
= vnode_getname(vp
);
845 switch (vp
->v_type
) {
850 * Create an artificial dev name from
851 * major and minor device number
854 (void) snprintf(dev_name
, sizeof(dev_name
),
855 "%c(%u, %u)", VCHR
== vp
->v_type
? 'c':'b',
856 major(vp
->v_rdev
), minor(vp
->v_rdev
));
858 * Add the newly created dev name to the name
859 * cache to allow easier cleanup. Also,
860 * vfs_addname allocates memory for the new name
863 NAME_CACHE_LOCK_SHARED();
864 name
= vfs_addname(dev_name
, strlen(dev_name
), 0, 0);
869 return unknown_vnodename
;
874 vnode_putname_printable(const char *name
)
876 if (name
== unknown_vnodename
) {
884 * if VNODE_UPDATE_PARENT, and we can take
885 * a reference on dvp, then update vp with
886 * it's new parent... if vp already has a parent,
887 * then drop the reference vp held on it
889 * if VNODE_UPDATE_NAME,
890 * then drop string ref on v_name if it exists, and if name is non-NULL
891 * then pick up a string reference on name and record it in v_name...
892 * optionally pass in the length and hashval of name if known
894 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
897 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, uint32_t name_hashval
, int flags
)
899 struct namecache
*ncp
;
900 vnode_t old_parentvp
= NULLVP
;
901 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
902 int kusecountbumped
= 0;
903 kauth_cred_t tcred
= NULL
;
904 const char *vname
= NULL
;
905 const char *tname
= NULL
;
907 if (flags
& VNODE_UPDATE_PARENT
) {
908 if (dvp
&& vnode_ref(dvp
) != 0) {
911 /* Don't count a stream's parent ref during unmounts */
912 if (isstream
&& dvp
&& (dvp
!= vp
) && (dvp
!= vp
->v_parent
) && (dvp
->v_type
== VREG
)) {
913 vnode_lock_spin(dvp
);
921 if ((flags
& VNODE_UPDATE_NAME
)) {
922 if (name
!= vp
->v_name
) {
925 name_len
= strlen(name
);
927 tname
= vfs_addname(name
, name_len
, name_hashval
, 0);
930 flags
&= ~VNODE_UPDATE_NAME
;
933 if ((flags
& (VNODE_UPDATE_PURGE
| VNODE_UPDATE_PARENT
| VNODE_UPDATE_CACHE
| VNODE_UPDATE_NAME
))) {
936 if ((flags
& VNODE_UPDATE_PURGE
)) {
938 vp
->v_parent
->v_nc_generation
++;
941 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
942 cache_delete(ncp
, 1);
945 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
946 cache_delete(ncp
, 1);
950 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
954 vp
->v_authorized_actions
= 0;
955 vp
->v_cred_timestamp
= 0;
957 if ((flags
& VNODE_UPDATE_NAME
)) {
961 if (flags
& VNODE_UPDATE_PARENT
) {
962 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
963 old_parentvp
= vp
->v_parent
;
968 flags
|= VNODE_UPDATE_CACHE
;
972 if (flags
& VNODE_UPDATE_CACHE
) {
973 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
974 cache_delete(ncp
, 1);
980 vfs_removename(vname
);
983 if (IS_VALID_CRED(tcred
)) {
984 kauth_cred_unref(&tcred
);
988 /* Back-out the ref we took if we lost a race for vp->v_parent. */
989 if (kusecountbumped
) {
990 vnode_lock_spin(dvp
);
991 if (dvp
->v_kusecount
> 0) {
1002 vnode_lock_spin(old_parentvp
);
1003 if ((old_parentvp
->v_type
!= VDIR
) && (old_parentvp
->v_kusecount
> 0)) {
1004 --old_parentvp
->v_kusecount
;
1006 vnode_unlock(old_parentvp
);
1008 ut
= get_bsdthread_info(current_thread());
1011 * indicated to vnode_rele that it shouldn't do a
1012 * vnode_reclaim at this time... instead it will
1013 * chain the vnode to the uu_vreclaims list...
1014 * we'll be responsible for calling vnode_reclaim
1015 * on each of the vnodes in this list...
1017 ut
->uu_defer_reclaims
= 1;
1018 ut
->uu_vreclaims
= NULLVP
;
1020 while ((vp
= old_parentvp
) != NULLVP
) {
1021 vnode_lock_spin(vp
);
1022 vnode_rele_internal(vp
, 0, 0, 1);
1025 * check to see if the vnode is now in the state
1026 * that would have triggered a vnode_reclaim in vnode_rele
1027 * if it is, we save it's parent pointer and then NULL
1028 * out the v_parent field... we'll drop the reference
1029 * that was held on the next iteration of this loop...
1030 * this short circuits a potential deep recursion if we
1031 * have a long chain of parents in this state...
1032 * we'll sit in this loop until we run into
1033 * a parent in this chain that is not in this state
1035 * make our check and the vnode_rele atomic
1036 * with respect to the current vnode we're working on
1037 * by holding the vnode lock
1038 * if vnode_rele deferred the vnode_reclaim and has put
1039 * this vnode on the list to be reaped by us, than
1040 * it has left this vnode with an iocount == 1
1042 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1043 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1045 * vnode_rele wanted to do a vnode_reclaim on this vnode
1046 * it should be sitting on the head of the uu_vreclaims chain
1047 * pull the parent pointer now so that when we do the
1048 * vnode_reclaim for each of the vnodes in the uu_vreclaims
1049 * list, we won't recurse back through here
1051 * need to do a convert here in case vnode_rele_internal
1052 * returns with the lock held in the spin mode... it
1053 * can drop and retake the lock under certain circumstances
1055 vnode_lock_convert(vp
);
1058 old_parentvp
= vp
->v_parent
;
1059 vp
->v_parent
= NULLVP
;
1060 NAME_CACHE_UNLOCK();
1063 * we're done... we ran into a vnode that isn't
1066 old_parentvp
= NULLVP
;
1070 ut
->uu_defer_reclaims
= 0;
1072 while ((vp
= ut
->uu_vreclaims
) != NULLVP
) {
1073 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
1076 * vnode_put will drive the vnode_reclaim if
1077 * we are still the only reference on this vnode
1086 * Mark a vnode as having multiple hard links. HFS makes use of this
1087 * because it keeps track of each link separately, and wants to know
1088 * which link was actually used.
1090 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
1091 * so that HFS can post-process the lookup. Also, volfs will call
1092 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
1095 vnode_setmultipath(vnode_t vp
)
1097 vnode_lock_spin(vp
);
1100 * In theory, we're changing the vnode's identity as far as the
1101 * name cache is concerned, so we ought to grab the name cache lock
1102 * here. However, there is already a race, and grabbing the name
1103 * cache lock only makes the race window slightly smaller.
1105 * The race happens because the vnode already exists in the name
1106 * cache, and could be found by one thread before another thread
1107 * can set the hard link flag.
1110 vp
->v_flag
|= VISHARDLINK
;
1118 * backwards compatibility
1121 vnode_uncache_credentials(vnode_t vp
)
1123 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
1128 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
1129 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
1130 * we use this lock so that we can look at the v_cred and v_authorized_actions
1131 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
1132 * which is the super-hot path... if we are updating the authorized actions for this
1133 * vnode, we are already in the super-slow and far less frequented path so its not
1134 * that bad that we take the lock exclusive for this case... of course we strive
1135 * to hold it for the minimum amount of time possible
1139 vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
1141 kauth_cred_t tcred
= NOCRED
;
1145 vp
->v_authorized_actions
&= ~action
;
1147 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
1148 IS_VALID_CRED(vp
->v_cred
)) {
1150 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1153 vp
->v_cred
= NOCRED
;
1155 NAME_CACHE_UNLOCK();
1157 if (tcred
!= NOCRED
) {
1158 kauth_cred_unref(&tcred
);
1163 extern int bootarg_vnode_cache_defeat
; /* default = 0, from bsd_init.c */
1166 vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1169 boolean_t retval
= FALSE
;
1171 /* Boot argument to defeat rights caching */
1172 if (bootarg_vnode_cache_defeat
) {
1176 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1178 * a TTL is enabled on the rights cache... handle it here
1179 * a TTL of 0 indicates that no rights should be cached
1181 if (vp
->v_mount
->mnt_authcache_ttl
) {
1182 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1184 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
1185 * we will only allow a SEARCH right on a directory to be cached...
1186 * that cached right always has a default TTL associated with it
1188 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
) {
1192 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
1193 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
1201 ucred
= vfs_context_ucred(ctx
);
1203 NAME_CACHE_LOCK_SHARED();
1205 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
) {
1209 NAME_CACHE_UNLOCK();
1216 vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1218 kauth_cred_t tcred
= NOCRED
;
1221 boolean_t ttl_active
= FALSE
;
1223 ucred
= vfs_context_ucred(ctx
);
1225 if (!IS_VALID_CRED(ucred
) || action
== 0) {
1229 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1231 * a TTL is enabled on the rights cache... handle it here
1232 * a TTL of 0 indicates that no rights should be cached
1234 if (vp
->v_mount
->mnt_authcache_ttl
== 0) {
1238 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1240 * only cache SEARCH action for filesystems marked
1241 * MNTK_AUTH_OPAQUE on VDIRs...
1242 * the lookup_path code will time these out
1244 if ((action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
) {
1254 if (vp
->v_cred
!= ucred
) {
1255 kauth_cred_ref(ucred
);
1257 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1261 vp
->v_authorized_actions
= 0;
1263 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
1265 * only reset the timestamnp on the
1266 * first authorization cached after the previous
1267 * timer has expired or we're switching creds...
1268 * 'vnode_cache_is_authorized' will clear the
1269 * authorized actions if the TTL is active and
1272 vp
->v_cred_timestamp
= tv
.tv_sec
;
1274 vp
->v_authorized_actions
|= action
;
1276 NAME_CACHE_UNLOCK();
1278 if (IS_VALID_CRED(tcred
)) {
1279 kauth_cred_unref(&tcred
);
1285 vnode_cache_is_stale(vnode_t vp
)
1292 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
) {
1304 * Returns: 0 Success
1305 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
1306 * This errno value should not be seen by anyone outside of the kernel.
1309 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
1310 vfs_context_t ctx
, int *dp_authorized
, vnode_t last_dp
)
1312 char *cp
; /* pointer into pathname argument */
1314 int vvid
= 0; /* protected by vp != NULLVP */
1315 vnode_t vp
= NULLVP
;
1316 vnode_t tdp
= NULLVP
;
1318 boolean_t ttl_enabled
= FALSE
;
1323 boolean_t dotdotchecked
= FALSE
;
1327 #endif /* CONFIG_TRIGGERS */
1329 ucred
= vfs_context_ucred(ctx
);
1330 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
1332 NAME_CACHE_LOCK_SHARED();
1334 if (dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1340 * Search a directory.
1342 * The cn_hash value is for use by cache_lookup
1343 * The last component of the filename is left accessible via
1344 * cnp->cn_nameptr for callers that need the name.
1347 cp
= cnp
->cn_nameptr
;
1349 while (*cp
&& (*cp
!= '/')) {
1350 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
1360 cnp
->cn_hash
= hash
;
1361 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
1363 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
1367 * Replace multiple slashes by a single slash and trailing slashes
1368 * by a null. This must be done before VNOP_LOOKUP() because some
1369 * fs's don't know about trailing slashes. Remember if there were
1370 * trailing slashes to handle symlinks, existing non-directories
1371 * and non-existing files that won't be directories specially later.
1373 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
1378 ndp
->ni_flag
|= NAMEI_TRAILINGSLASH
;
1379 *ndp
->ni_next
= '\0';
1384 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
1387 cnp
->cn_flags
|= ISLASTCN
;
1390 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.') {
1391 cnp
->cn_flags
|= ISDOTDOT
;
1397 * Process a request for a file's resource fork.
1399 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1401 if ((ndp
->ni_pathlen
== sizeof(_PATH_RSRCFORKSPEC
)) &&
1402 (cp
[1] == '.' && cp
[2] == '.') &&
1403 bcmp(cp
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
1404 /* Skip volfs file systems that don't support native streams. */
1405 if ((dp
->v_mount
!= NULL
) &&
1406 (dp
->v_mount
->mnt_flag
& MNT_DOVOLFS
) &&
1407 (dp
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0) {
1410 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
1411 cnp
->cn_flags
|= ISLASTCN
;
1412 ndp
->ni_next
[0] = '\0';
1413 ndp
->ni_pathlen
= 1;
1421 * Name cache provides authorization caching (see below)
1422 * that will short circuit MAC checks in lookup().
1423 * We must perform MAC check here. On denial
1424 * dp_authorized will remain 0 and second check will
1425 * be perfomed in lookup().
1427 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
1428 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
1430 NAME_CACHE_UNLOCK();
1436 (dp
->v_mount
->mnt_authcache_ttl
== 0 ||
1437 ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))) {
1442 * NAME_CACHE_LOCK holds these fields stable
1444 * We can't cache KAUTH_VNODE_SEARCHBYANYONE for root correctly
1445 * so we make an ugly check for root here. root is always
1446 * allowed and breaking out of here only to find out that is
1447 * authorized by virtue of being root is very very expensive.
1448 * However, the check for not root is valid only for filesystems
1449 * which use local authorization.
1451 * XXX: Remove the check for root when we can reliably set
1452 * KAUTH_VNODE_SEARCHBYANYONE as root.
1454 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
1455 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
) &&
1456 (ttl_enabled
|| !vfs_context_issuser(ctx
))) {
1461 * indicate that we're allowed to traverse this directory...
1462 * even if we fail the cache lookup or decide to bail for
1463 * some other reason, this information is valid and is used
1464 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1468 if ((cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
))) {
1469 if (cnp
->cn_nameiop
!= LOOKUP
) {
1472 if (cnp
->cn_flags
& LOCKPARENT
) {
1475 if (cnp
->cn_flags
& NOCACHE
) {
1478 if (cnp
->cn_flags
& ISDOTDOT
) {
1480 * Force directory hardlinks to go to
1481 * file system for ".." requests.
1483 if ((dp
->v_flag
& VISHARDLINK
)) {
1487 * Quit here only if we can't use
1488 * the parent directory pointer or
1489 * don't have one. Otherwise, we'll
1492 if ((dp
->v_flag
& VROOT
) ||
1493 dp
== ndp
->ni_rootdir
||
1494 dp
->v_parent
== NULLVP
) {
1500 if ((cnp
->cn_flags
& CN_SKIPNAMECACHE
)) {
1502 * Force lookup to go to the filesystem with
1503 * all cnp fields set up.
1509 * "." and ".." aren't supposed to be cached, so check
1510 * for them before checking the cache.
1512 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') {
1514 } else if ((cnp
->cn_flags
& ISDOTDOT
)) {
1516 * If this is a chrooted process, we need to check if
1517 * the process is trying to break out of its chrooted
1518 * jail. We do that by trying to determine if dp is
1519 * a subdirectory of ndp->ni_rootdir. If we aren't
1520 * able to determine that by the v_parent pointers, we
1521 * will leave the fast path.
1523 * Since this function may see dotdot components
1524 * many times and it has the name cache lock held for
1525 * the entire duration, we optimise this by doing this
1526 * check only once per cache_lookup_path call.
1527 * If dotdotchecked is set, it means we've done this
1528 * check once already and don't need to do it again.
1530 if (!dotdotchecked
&& (ndp
->ni_rootdir
!= rootvnode
)) {
1532 boolean_t defer
= FALSE
;
1533 boolean_t is_subdir
= FALSE
;
1535 defer
= cache_check_vnode_issubdir(tvp
,
1536 ndp
->ni_rootdir
, &is_subdir
, &tvp
);
1539 /* defer to Filesystem */
1541 } else if (!is_subdir
) {
1543 * This process is trying to break out
1544 * of its chrooted jail, so all its
1545 * dotdot accesses will be translated to
1546 * its root directory.
1548 vp
= ndp
->ni_rootdir
;
1551 * All good, let this dotdot access
1556 dotdotchecked
= TRUE
;
1561 if ((vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
) {
1565 if ((vp
->v_flag
& VISHARDLINK
)) {
1567 * The file system wants a VNOP_LOOKUP on this vnode
1573 if ((cnp
->cn_flags
& ISLASTCN
)) {
1577 if (vp
->v_type
!= VDIR
) {
1578 if (vp
->v_type
!= VLNK
) {
1584 if ((mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1585 vnode_t tmp_vp
= mp
->mnt_realrootvp
;
1586 if (tmp_vp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1587 mp
->mnt_realrootvp_vid
!= tmp_vp
->v_id
) {
1595 * After traversing all mountpoints stacked here, if we have a
1596 * trigger in hand, resolve it. Note that we don't need to
1597 * leave the fast path if the mount has already happened.
1599 if (vp
->v_resolve
) {
1602 #endif /* CONFIG_TRIGGERS */
1608 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1610 while (*cnp
->cn_nameptr
== '/') {
1620 NAME_CACHE_UNLOCK();
1622 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1623 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
1625 * if we've got a child and it's the last component, and
1626 * the lookup doesn't need to return the parent then we
1627 * can skip grabbing an iocount on the parent, since all
1628 * we're going to do with it is a vnode_put just before
1629 * we return from 'lookup'. If it's a symbolic link,
1630 * we need the parent in case the link happens to be
1631 * a relative pathname.
1638 * return the last directory we looked at
1639 * with an io reference held. If it was the one passed
1640 * in as a result of the last iteration of VNOP_LOOKUP,
1641 * it should already hold an io ref. No need to increase ref.
1643 if (last_dp
!= dp
) {
1644 if (dp
== ndp
->ni_usedvp
) {
1646 * if this vnode matches the one passed in via USEDVP
1647 * than this context already holds an io_count... just
1648 * use vnode_get to get an extra ref for lookup to play
1649 * with... can't use the getwithvid variant here because
1650 * it will block behind a vnode_drain which would result
1651 * in a deadlock (since we already own an io_count that the
1652 * vnode_drain is waiting on)... vnode_get grabs the io_count
1653 * immediately w/o waiting... it always succeeds
1656 } else if ((error
= vnode_getwithvid_drainok(dp
, vid
))) {
1658 * failure indicates the vnode
1659 * changed identity or is being
1660 * TERMINATED... in either case
1663 * don't necessarily return ENOENT, though, because
1664 * we really want to go back to disk and make sure it's
1665 * there or not if someone else is changing this
1666 * vnode. That being said, the one case where we do want
1667 * to return ENOENT is when the vnode's mount point is
1668 * in the process of unmounting and we might cause a deadlock
1669 * in our attempt to take an iocount. An ENODEV error return
1670 * is from vnode_get* is an indication this but we change that
1671 * ENOENT for upper layers.
1673 if (error
== ENODEV
) {
1683 if ((vnode_getwithvid_drainok(vp
, vvid
))) {
1687 * can't get reference on the vp we'd like
1688 * to return... if we didn't grab a reference
1689 * on the directory (due to fast path bypass),
1690 * then we need to do it now... we can't return
1691 * with both ni_dvp and ni_vp NULL, and no
1705 trigger_vp
= vp
? vp
: dp
;
1706 if ((error
== 0) && (trigger_vp
!= NULLVP
) && vnode_isdir(trigger_vp
)) {
1707 error
= vnode_trigger_resolve(trigger_vp
, ndp
, ctx
);
1718 #endif /* CONFIG_TRIGGERS */
1722 * If we came into cache_lookup_path after an iteration of the lookup loop that
1723 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1724 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1725 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1726 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1727 * the case that we got to a subsequent path component and this previous vnode is
1728 * no longer needed. We can then drop the io ref on it.
1730 if ((last_dp
!= NULLVP
) && (last_dp
!= ndp
->ni_dvp
)) {
1734 //initialized to 0, should be the same if no error cases occurred.
1740 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1742 struct namecache
*ncp
;
1743 struct nchashhead
*ncpp
;
1744 long namelen
= cnp
->cn_namelen
;
1745 unsigned int hashval
= cnp
->cn_hash
;
1751 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1752 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1753 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1754 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
1761 * We failed to find an entry
1766 NCHSTAT(ncs_goodhits
);
1772 unsigned int hash_string(const char *cp
, int len
);
1774 // Have to take a len argument because we may only need to
1775 // hash part of a componentname.
1778 hash_string(const char *cp
, int len
)
1784 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1787 while (*cp
!= '\0') {
1788 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1792 * the crc generator can legitimately generate
1793 * a 0... however, 0 for us means that we
1794 * haven't computed a hash, so use 1 instead
1804 * Lookup an entry in the cache
1806 * We don't do this if the segment name is long, simply so the cache
1807 * can avoid holding long names (which would either waste space, or
1808 * add greatly to the complexity).
1810 * Lookup is called with dvp pointing to the directory to search,
1811 * cnp pointing to the name of the entry being sought. If the lookup
1812 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1813 * returned. If the lookup determines that the name does not exist
1814 * (negative cacheing), a status of ENOENT is returned. If the lookup
1815 * fails, a status of zero is returned.
1819 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1821 struct namecache
*ncp
;
1822 struct nchashhead
*ncpp
;
1823 long namelen
= cnp
->cn_namelen
;
1824 unsigned int hashval
;
1825 boolean_t have_exclusive
= FALSE
;
1829 if (cnp
->cn_hash
== 0) {
1830 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1832 hashval
= cnp
->cn_hash
;
1838 NAME_CACHE_LOCK_SHARED();
1841 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1842 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1843 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1844 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
1849 /* We failed to find an entry */
1852 NAME_CACHE_UNLOCK();
1856 /* We don't want to have an entry, so dump it */
1857 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
1858 if (have_exclusive
== TRUE
) {
1859 NCHSTAT(ncs_badhits
);
1860 cache_delete(ncp
, 1);
1861 NAME_CACHE_UNLOCK();
1864 NAME_CACHE_UNLOCK();
1866 have_exclusive
= TRUE
;
1871 /* We found a "positive" match, return the vnode */
1873 NCHSTAT(ncs_goodhits
);
1876 NAME_CACHE_UNLOCK();
1878 if (vnode_getwithvid(vp
, vid
)) {
1881 NCHSTAT(ncs_badvid
);
1882 NAME_CACHE_UNLOCK();
1890 /* We found a negative match, and want to create it, so purge */
1891 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
1892 if (have_exclusive
== TRUE
) {
1893 NCHSTAT(ncs_badhits
);
1894 cache_delete(ncp
, 1);
1895 NAME_CACHE_UNLOCK();
1898 NAME_CACHE_UNLOCK();
1900 have_exclusive
= TRUE
;
1905 * We found a "negative" match, ENOENT notifies client of this match.
1907 NCHSTAT(ncs_neghits
);
1909 NAME_CACHE_UNLOCK();
1914 cache_enter_create(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
)
1916 const char *strname
;
1918 if (cnp
->cn_hash
== 0) {
1919 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1923 * grab 2 references on the string entered
1924 * one for the cache_enter_locked to consume
1925 * and the second to be consumed by v_name (vnode_create call point)
1927 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, TRUE
, 0);
1931 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1933 NAME_CACHE_UNLOCK();
1940 * Add an entry to the cache...
1941 * but first check to see if the directory
1942 * that this entry is to be associated with has
1943 * had any cache_purges applied since we took
1944 * our identity snapshot... this check needs to
1945 * be done behind the name cache lock
1948 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
1950 if (cnp
->cn_hash
== 0) {
1951 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1956 if (dvp
->v_nc_generation
== gen
) {
1957 (void)cache_enter_locked(dvp
, vp
, cnp
, NULL
);
1960 NAME_CACHE_UNLOCK();
1965 * Add an entry to the cache.
1968 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
1970 const char *strname
;
1972 if (cnp
->cn_hash
== 0) {
1973 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
1977 * grab 1 reference on the string entered
1978 * for the cache_enter_locked to consume
1980 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
1984 cache_enter_locked(dvp
, vp
, cnp
, strname
);
1986 NAME_CACHE_UNLOCK();
1991 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, const char *strname
)
1993 struct namecache
*ncp
, *negp
;
1994 struct nchashhead
*ncpp
;
2001 * if the entry is for -ve caching vp is null
2003 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
2005 * someone beat us to the punch..
2006 * this vnode is already in the cache
2008 if (strname
!= NULL
) {
2009 vfs_removename(strname
);
2014 * We allocate a new entry if we are less than the maximum
2015 * allowed and the one at the front of the list is in use.
2016 * Otherwise we use the one at the front of the list.
2018 if (numcache
< desiredNodes
&&
2019 ((ncp
= nchead
.tqh_first
) == NULL
||
2020 ncp
->nc_hash
.le_prev
!= 0)) {
2022 * Allocate one more entry
2024 ncp
= (struct namecache
*)_MALLOC_ZONE(sizeof(*ncp
), M_CACHE
, M_WAITOK
);
2028 * reuse an old entry
2030 ncp
= TAILQ_FIRST(&nchead
);
2031 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2033 if (ncp
->nc_hash
.le_prev
!= 0) {
2035 * still in use... we need to
2036 * delete it before re-using it
2038 NCHSTAT(ncs_stolen
);
2039 cache_delete(ncp
, 0);
2042 NCHSTAT(ncs_enters
);
2045 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
2049 ncp
->nc_hashval
= cnp
->cn_hash
;
2051 if (strname
== NULL
) {
2052 ncp
->nc_name
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
2054 ncp
->nc_name
= strname
;
2058 // If the bytes of the name associated with the vnode differ,
2059 // use the name associated with the vnode since the file system
2060 // may have set that explicitly in the case of a lookup on a
2061 // case-insensitive file system where the case of the looked up
2062 // name differs from what is on disk. For more details, see:
2063 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
2065 const char *vn_name
= vp
? vp
->v_name
: NULL
;
2066 unsigned int len
= vn_name
? strlen(vn_name
) : 0;
2067 if (vn_name
&& ncp
&& ncp
->nc_name
&& strncmp(ncp
->nc_name
, vn_name
, len
) != 0) {
2068 unsigned int hash
= hash_string(vn_name
, len
);
2070 vfs_removename(ncp
->nc_name
);
2071 ncp
->nc_name
= add_name_internal(vn_name
, len
, hash
, FALSE
, 0);
2072 ncp
->nc_hashval
= hash
;
2076 * make us the newest entry in the cache
2077 * i.e. we'll be the last to be stolen
2079 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
2081 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
2084 struct namecache
*p
;
2086 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
) {
2088 panic("cache_enter: duplicate");
2094 * make us available to be found via lookup
2096 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
2100 * add to the list of name cache entries
2103 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
2106 * this is a negative cache entry (vp == NULL)
2107 * stick it on the negative cache list.
2109 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
2113 if (ncs_negtotal
> desiredNegNodes
) {
2115 * if we've reached our desired limit
2116 * of negative cache entries, delete
2119 negp
= TAILQ_FIRST(&neghead
);
2120 cache_delete(negp
, 1);
2124 * add us to the list of name cache entries that
2125 * are children of dvp
2128 TAILQ_INSERT_TAIL(&dvp
->v_ncchildren
, ncp
, nc_child
);
2130 TAILQ_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
2136 * Initialize CRC-32 remainder table.
2142 * the CRC-32 generator polynomial is:
2143 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
2144 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
2146 unsigned int crc32_polynomial
= 0x04c11db7;
2150 * pre-calculate the CRC-32 remainder for each possible octet encoding
2152 for (i
= 0; i
< 256; i
++) {
2153 unsigned int crc_rem
= i
<< 24;
2155 for (j
= 0; j
< 8; j
++) {
2156 if (crc_rem
& 0x80000000) {
2157 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
2159 crc_rem
= (crc_rem
<< 1);
2162 crc32tab
[i
] = crc_rem
;
2168 * Name cache initialization, from vfs_init() when we are booting
2175 desiredNegNodes
= (desiredvnodes
/ 10);
2176 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
2178 TAILQ_INIT(&nchead
);
2179 TAILQ_INIT(&neghead
);
2183 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 * desiredNodes
)), M_CACHE
, &nchash
);
2184 nchashmask
= nchash
;
2187 init_string_table();
2189 /* Allocate name cache lock group attribute and group */
2190 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
2192 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
2194 /* Allocate name cache lock attribute */
2195 namecache_lck_attr
= lck_attr_alloc_init();
2197 /* Allocate name cache lock */
2198 namecache_rw_lock
= lck_rw_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
2201 /* Allocate string cache lock group attribute and group */
2202 strcache_lck_grp_attr
= lck_grp_attr_alloc_init();
2204 strcache_lck_grp
= lck_grp_alloc_init("String Cache", strcache_lck_grp_attr
);
2206 /* Allocate string cache lock attribute */
2207 strcache_lck_attr
= lck_attr_alloc_init();
2209 /* Allocate string cache lock */
2210 strtable_rw_lock
= lck_rw_alloc_init(strcache_lck_grp
, strcache_lck_attr
);
2212 for (i
= 0; i
< NUM_STRCACHE_LOCKS
; i
++) {
2213 lck_mtx_init(&strcache_mtx_locks
[i
], strcache_lck_grp
, strcache_lck_attr
);
2218 name_cache_lock_shared(void)
2220 lck_rw_lock_shared(namecache_rw_lock
);
2224 name_cache_lock(void)
2226 lck_rw_lock_exclusive(namecache_rw_lock
);
2230 name_cache_unlock(void)
2232 lck_rw_done(namecache_rw_lock
);
2237 resize_namecache(int newsize
)
2239 struct nchashhead
*new_table
;
2240 struct nchashhead
*old_table
;
2241 struct nchashhead
*old_head
, *head
;
2242 struct namecache
*entry
, *next
;
2243 uint32_t i
, hashval
;
2244 int dNodes
, dNegNodes
, nelements
;
2245 u_long new_size
, old_size
;
2251 dNegNodes
= (newsize
/ 10);
2252 dNodes
= newsize
+ dNegNodes
;
2253 // we don't support shrinking yet
2254 if (dNodes
<= desiredNodes
) {
2258 if (os_mul_overflow(dNodes
, 2, &nelements
)) {
2262 new_table
= hashinit(nelements
, M_CACHE
, &nchashmask
);
2263 new_size
= nchashmask
+ 1;
2265 if (new_table
== NULL
) {
2271 old_table
= nchashtbl
;
2272 nchashtbl
= new_table
;
2276 // walk the old table and insert all the entries into
2279 for (i
= 0; i
< old_size
; i
++) {
2280 old_head
= &old_table
[i
];
2281 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2283 // XXXdbg - Beware: this assumes that hash_string() does
2284 // the same thing as what happens in
2285 // lookup() over in vfs_lookup.c
2286 hashval
= hash_string(entry
->nc_name
, 0);
2287 entry
->nc_hashval
= hashval
;
2288 head
= NCHHASH(entry
->nc_dvp
, hashval
);
2290 next
= entry
->nc_hash
.le_next
;
2291 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
2294 desiredNodes
= dNodes
;
2295 desiredNegNodes
= dNegNodes
;
2297 NAME_CACHE_UNLOCK();
2298 FREE(old_table
, M_CACHE
);
2304 cache_delete(struct namecache
*ncp
, int free_entry
)
2306 NCHSTAT(ncs_deletes
);
2309 LIST_REMOVE(ncp
, nc_un
.nc_link
);
2311 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
2314 TAILQ_REMOVE(&(ncp
->nc_dvp
->v_ncchildren
), ncp
, nc_child
);
2316 LIST_REMOVE(ncp
, nc_hash
);
2318 * this field is used to indicate
2319 * that the entry is in use and
2320 * must be deleted before it can
2323 ncp
->nc_hash
.le_prev
= NULL
;
2325 vfs_removename(ncp
->nc_name
);
2326 ncp
->nc_name
= NULL
;
2328 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2329 FREE_ZONE(ncp
, sizeof(*ncp
), M_CACHE
);
2336 * purge the entry associated with the
2337 * specified vnode from the name cache
2340 cache_purge(vnode_t vp
)
2342 struct namecache
*ncp
;
2343 kauth_cred_t tcred
= NULL
;
2345 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
2346 (TAILQ_FIRST(&vp
->v_ncchildren
) == NULL
) &&
2347 (vp
->v_cred
== NOCRED
) &&
2348 (vp
->v_parent
== NULLVP
)) {
2355 vp
->v_parent
->v_nc_generation
++;
2358 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
2359 cache_delete(ncp
, 1);
2362 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
2363 cache_delete(ncp
, 1);
2367 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
2370 vp
->v_cred
= NOCRED
;
2371 vp
->v_authorized_actions
= 0;
2373 NAME_CACHE_UNLOCK();
2375 if (IS_VALID_CRED(tcred
)) {
2376 kauth_cred_unref(&tcred
);
2381 * Purge all negative cache entries that are children of the
2382 * given vnode. A case-insensitive file system (or any file
2383 * system that has multiple equivalent names for the same
2384 * directory entry) can use this when creating or renaming
2385 * to remove negative entries that may no longer apply.
2388 cache_purge_negatives(vnode_t vp
)
2390 struct namecache
*ncp
, *next_ncp
;
2394 TAILQ_FOREACH_SAFE(ncp
, &vp
->v_ncchildren
, nc_child
, next_ncp
) {
2399 cache_delete(ncp
, 1);
2402 NAME_CACHE_UNLOCK();
2406 * Flush all entries referencing a particular filesystem.
2408 * Since we need to check it anyway, we will flush all the invalid
2409 * entries at the same time.
2412 cache_purgevfs(struct mount
*mp
)
2414 struct nchashhead
*ncpp
;
2415 struct namecache
*ncp
;
2418 /* Scan hash tables for applicable entries */
2419 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
2421 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
2422 if (ncp
->nc_dvp
->v_mount
== mp
) {
2423 cache_delete(ncp
, 0);
2428 NAME_CACHE_UNLOCK();
2434 // String ref routines
2436 static LIST_HEAD(stringhead
, string_t
) * string_ref_table
;
2437 static u_long string_table_mask
;
2438 static uint32_t filled_buckets
= 0;
2441 typedef struct string_t
{
2442 LIST_ENTRY(string_t
) hash_chain
;
2449 resize_string_ref_table(void)
2451 struct stringhead
*new_table
;
2452 struct stringhead
*old_table
;
2453 struct stringhead
*old_head
, *head
;
2454 string_t
*entry
, *next
;
2455 uint32_t i
, hashval
;
2456 u_long new_mask
, old_mask
;
2459 * need to hold the table lock exclusively
2460 * in order to grow the table... need to recheck
2461 * the need to resize again after we've taken
2462 * the lock exclusively in case some other thread
2463 * beat us to the punch
2465 lck_rw_lock_exclusive(strtable_rw_lock
);
2467 if (4 * filled_buckets
< ((string_table_mask
+ 1) * 3)) {
2468 lck_rw_done(strtable_rw_lock
);
2471 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
2473 if (new_table
== NULL
) {
2474 printf("failed to resize the hash table.\n");
2475 lck_rw_done(strtable_rw_lock
);
2480 old_table
= string_ref_table
;
2481 string_ref_table
= new_table
;
2482 old_mask
= string_table_mask
;
2483 string_table_mask
= new_mask
;
2486 // walk the old table and insert all the entries into
2489 for (i
= 0; i
<= old_mask
; i
++) {
2490 old_head
= &old_table
[i
];
2491 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2492 hashval
= hash_string((const char *)entry
->str
, 0);
2493 head
= &string_ref_table
[hashval
& string_table_mask
];
2494 if (head
->lh_first
== NULL
) {
2497 next
= entry
->hash_chain
.le_next
;
2498 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2501 lck_rw_done(strtable_rw_lock
);
2503 FREE(old_table
, M_CACHE
);
2508 init_string_table(void)
2510 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
2515 vfs_addname(const char *name
, uint32_t len
, u_int hashval
, u_int flags
)
2517 return add_name_internal(name
, len
, hashval
, FALSE
, flags
);
2522 add_name_internal(const char *name
, uint32_t len
, u_int hashval
, boolean_t need_extra_ref
, __unused u_int flags
)
2524 struct stringhead
*head
;
2526 uint32_t chain_len
= 0;
2527 uint32_t hash_index
;
2528 uint32_t lock_index
;
2531 if (len
> MAXPATHLEN
) {
2536 * if the length already accounts for the null-byte, then
2537 * subtract one so later on we don't index past the end
2540 if (len
> 0 && name
[len
- 1] == '\0') {
2544 hashval
= hash_string(name
, len
);
2548 * take this lock 'shared' to keep the hash stable
2549 * if someone else decides to grow the pool they
2550 * will take this lock exclusively
2552 lck_rw_lock_shared(strtable_rw_lock
);
2555 * If the table gets more than 3/4 full, resize it
2557 if (4 * filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
2558 lck_rw_done(strtable_rw_lock
);
2560 resize_string_ref_table();
2562 lck_rw_lock_shared(strtable_rw_lock
);
2564 hash_index
= hashval
& string_table_mask
;
2565 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2567 head
= &string_ref_table
[hash_index
];
2569 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2571 for (entry
= head
->lh_first
; entry
!= NULL
; chain_len
++, entry
= entry
->hash_chain
.le_next
) {
2572 if (strncmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == 0) {
2577 if (entry
== NULL
) {
2578 lck_mtx_convert_spin(&strcache_mtx_locks
[lock_index
]);
2580 * it wasn't already there so add it.
2582 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
2584 if (head
->lh_first
== NULL
) {
2585 OSAddAtomic(1, &filled_buckets
);
2587 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
2588 strncpy(ptr
, name
, len
);
2591 entry
->refcount
= 1;
2592 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2594 if (need_extra_ref
== TRUE
) {
2598 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2599 lck_rw_done(strtable_rw_lock
);
2601 return (const char *)entry
->str
;
2606 vfs_removename(const char *nameref
)
2608 struct stringhead
*head
;
2611 uint32_t hash_index
;
2612 uint32_t lock_index
;
2613 int retval
= ENOENT
;
2615 hashval
= hash_string(nameref
, 0);
2618 * take this lock 'shared' to keep the hash stable
2619 * if someone else decides to grow the pool they
2620 * will take this lock exclusively
2622 lck_rw_lock_shared(strtable_rw_lock
);
2624 * must compute the head behind the table lock
2625 * since the size and location of the table
2626 * can change on the fly
2628 hash_index
= hashval
& string_table_mask
;
2629 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2631 head
= &string_ref_table
[hash_index
];
2633 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2635 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2636 if (entry
->str
== nameref
) {
2639 if (entry
->refcount
== 0) {
2640 LIST_REMOVE(entry
, hash_chain
);
2642 if (head
->lh_first
== NULL
) {
2643 OSAddAtomic(-1, &filled_buckets
);
2652 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2653 lck_rw_done(strtable_rw_lock
);
2655 if (entry
!= NULL
) {
2656 FREE(entry
, M_TEMP
);
2663 #ifdef DUMP_STRING_TABLE
2665 dump_string_table(void)
2667 struct stringhead
*head
;
2671 lck_rw_lock_shared(strtable_rw_lock
);
2673 for (i
= 0; i
<= string_table_mask
; i
++) {
2674 head
= &string_ref_table
[i
];
2675 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2676 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
2679 lck_rw_done(strtable_rw_lock
);
2681 #endif /* DUMP_STRING_TABLE */