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 <kern/kalloc.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 ZONE_DECLARE(namecache_zone
, "namecache", sizeof(struct namecache
), ZC_NONE
);
114 LIST_HEAD(nchashhead
, namecache
) * nchashtbl
; /* Hash Table */
116 u_long nchash
; /* size of hash table - 1 */
117 long numcache
; /* number of cache entries allocated */
122 TAILQ_HEAD(, namecache
) nchead
; /* chain of all name cache entries */
123 TAILQ_HEAD(, namecache
) neghead
; /* chain of only negative cache entries */
128 struct nchstats nchstats
; /* cache effectiveness statistics */
130 #define NCHSTAT(v) { \
133 #define NAME_CACHE_LOCK() name_cache_lock()
134 #define NAME_CACHE_UNLOCK() name_cache_unlock()
135 #define NAME_CACHE_LOCK_SHARED() name_cache_lock()
140 #define NAME_CACHE_LOCK() name_cache_lock()
141 #define NAME_CACHE_UNLOCK() name_cache_unlock()
142 #define NAME_CACHE_LOCK_SHARED() name_cache_lock_shared()
147 /* vars for name cache list lock */
148 static LCK_GRP_DECLARE(namecache_lck_grp
, "Name Cache");
149 static LCK_RW_DECLARE(namecache_rw_lock
, &namecache_lck_grp
);
151 static LCK_GRP_DECLARE(strcache_lck_grp
, "String Cache");
152 static LCK_ATTR_DECLARE(strcache_lck_attr
, 0, 0);
153 LCK_RW_DECLARE_ATTR(strtable_rw_lock
, &strcache_lck_grp
, &strcache_lck_attr
);
155 static LCK_GRP_DECLARE(rootvnode_lck_grp
, "rootvnode");
156 LCK_RW_DECLARE(rootvnode_rw_lock
, &rootvnode_lck_grp
);
158 #define NUM_STRCACHE_LOCKS 1024
160 lck_mtx_t strcache_mtx_locks
[NUM_STRCACHE_LOCKS
];
163 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
164 static const char *add_name_internal(const char *, uint32_t, u_int
, boolean_t
, u_int
);
165 static void init_string_table(void);
166 static void cache_delete(struct namecache
*, int);
167 static void cache_enter_locked(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, const char *strname
);
168 static void cache_purge_locked(vnode_t vp
, kauth_cred_t
*credp
);
170 #ifdef DUMP_STRING_TABLE
172 * Internal dump function used for debugging
174 void dump_string_table(void);
175 #endif /* DUMP_STRING_TABLE */
177 static void init_crc32(void);
178 static unsigned int crc32tab
[256];
181 #define NCHHASH(dvp, hash_val) \
182 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
185 * This function tries to check if a directory vp is a subdirectory of dvp
186 * only from valid v_parent pointers. It is called with the name cache lock
187 * held and does not drop the lock anytime inside the function.
189 * It returns a boolean that indicates whether or not it was able to
190 * successfully infer the parent/descendent relationship via the v_parent
191 * pointers, or if it could not infer such relationship and that the decision
192 * must be delegated to the owning filesystem.
194 * If it does not defer the decision, i.e. it was successfuly able to determine
195 * the parent/descendent relationship, *is_subdir tells the caller if vp is a
196 * subdirectory of dvp.
198 * If the decision is deferred, *next_vp is where it stopped i.e. *next_vp
199 * is the vnode whose parent is to be determined from the filesystem.
200 * *is_subdir, in this case, is not indicative of anything and should be
203 * The return value and output args should be used as follows :
205 * defer = cache_check_vnode_issubdir(vp, dvp, is_subdir, next_vp);
208 * vp is subdirectory;
210 * vp is not a subdirectory;
213 * check this vnode's parent from the filesystem
215 * error (likely because of forced unmount).
220 cache_check_vnode_issubdir(vnode_t vp
, vnode_t dvp
, boolean_t
*is_subdir
,
234 } else if (tvp
== rootvnode
) {
235 /* *is_subdir = FALSE */
240 while ((tvp
->v_flag
& VROOT
) && tmp
&& tmp
->mnt_vnodecovered
&&
241 tvp
!= dvp
&& tvp
!= rootvnode
) {
242 tvp
= tmp
->mnt_vnodecovered
;
247 * If dvp is not at the top of a mount "stack" then
248 * vp is not a subdirectory of dvp either.
250 if (tvp
== dvp
|| tvp
== rootvnode
) {
251 /* *is_subdir = FALSE */
261 if ((tvp
->v_flag
& VISHARDLINK
) || !(tvp
->v_parent
)) {
273 /* maximum times retry from potentially transient errors in vnode_issubdir */
274 #define MAX_ERROR_RETRY 3
277 * This function checks if a given directory (vp) is a subdirectory of dvp.
278 * It walks backwards from vp and if it hits dvp in its parent chain,
279 * it is a subdirectory. If it encounters the root directory, it is not
282 * This function returns an error if it is unsuccessful and 0 on success.
284 * On entry (and exit) vp has an iocount and if this function has to take
285 * any iocounts on other vnodes in the parent chain traversal, it releases them.
288 vnode_issubdir(vnode_t vp
, vnode_t dvp
, int *is_subdir
, vfs_context_t ctx
)
290 vnode_t start_vp
, tvp
;
291 vnode_t vp_with_iocount
;
293 char dotdotbuf
[] = "..";
294 int error_retry_count
= 0; /* retry count for potentially transient
300 * Anytime we acquire an iocount in this function, we save the vnode
301 * in this variable and release it before exiting.
303 vp_with_iocount
= NULLVP
;
309 struct componentname cn
;
310 boolean_t is_subdir_locked
= FALSE
;
315 } else if (tvp
== rootvnode
) {
316 /* *is_subdir = FALSE */
320 NAME_CACHE_LOCK_SHARED();
322 defer
= cache_check_vnode_issubdir(tvp
, dvp
, &is_subdir_locked
,
326 vid
= vnode_vid(tvp
);
332 *is_subdir
= is_subdir_locked
;
337 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
345 if (tvp
!= start_vp
) {
346 if (vp_with_iocount
) {
347 vnode_put(vp_with_iocount
);
348 vp_with_iocount
= NULLVP
;
351 error
= vnode_getwithvid(tvp
, vid
);
353 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
361 vp_with_iocount
= tvp
;
364 bzero(&cn
, sizeof(cn
));
365 cn
.cn_nameiop
= LOOKUP
;
366 cn
.cn_flags
= ISLASTCN
| ISDOTDOT
;
368 cn
.cn_pnbuf
= &dotdotbuf
[0];
369 cn
.cn_pnlen
= sizeof(dotdotbuf
);
370 cn
.cn_nameptr
= cn
.cn_pnbuf
;
374 if ((error
= VNOP_LOOKUP(tvp
, &pvp
, &cn
, ctx
))) {
378 if (!(tvp
->v_flag
& VISHARDLINK
) && tvp
->v_parent
!= pvp
) {
379 (void)vnode_update_identity(tvp
, pvp
, NULL
, 0, 0,
380 VNODE_UPDATE_PARENT
);
383 if (vp_with_iocount
) {
384 vnode_put(vp_with_iocount
);
387 vp_with_iocount
= tvp
= pvp
;
390 if (vp_with_iocount
) {
391 vnode_put(vp_with_iocount
);
398 * This function builds the path in "buff" from the supplied vnode.
399 * The length of the buffer *INCLUDING* the trailing zero byte is
400 * returned in outlen. NOTE: the length includes the trailing zero
401 * byte and thus the length is one greater than what strlen would
402 * return. This is important and lots of code elsewhere in the kernel
403 * assumes this behavior.
405 * This function can call vnop in file system if the parent vnode
406 * does not exist or when called for hardlinks via volfs path.
407 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
408 * in the name cache and does not enter the file system.
410 * If BUILDPATH_CHECK_MOVED is set in flags, we return EAGAIN when
411 * we encounter ENOENT during path reconstruction. ENOENT means that
412 * one of the parents moved while we were building the path. The
413 * caller can special handle this case by calling build_path again.
415 * If BUILDPATH_VOLUME_RELATIVE is set in flags, we return path
416 * that is relative to the nearest mount point, i.e. do not
417 * cross over mount points during building the path.
419 * passed in vp must have a valid io_count reference
421 * If parent vnode is non-NULL it also must have an io count. This
422 * allows build_path_with_parent to be safely called for operations
423 * unlink, rmdir and rename that already have io counts on the target
424 * and the directory. In this way build_path_with_parent does not have
425 * to try and obtain an additional io count on the parent. Taking an
426 * io count ont the parent can lead to dead lock if a forced unmount
427 * occures at the right moment. For a fuller explaination on how this
428 * can occur see the comment for vn_getpath_with_parent.
432 build_path_with_parent(vnode_t first_vp
, vnode_t parent_vp
, char *buff
, int buflen
,
433 int *outlen
, size_t *mntpt_outlen
, int flags
, vfs_context_t ctx
)
436 vnode_t vp_with_iocount
;
437 vnode_t proc_root_dir_vp
;
445 if (first_vp
== NULLVP
) {
454 * Grab the process fd so we can evaluate fd_rdir.
456 if (vfs_context_proc(ctx
)->p_fd
&& !(flags
& BUILDPATH_NO_PROCROOT
)) {
457 proc_root_dir_vp
= vfs_context_proc(ctx
)->p_fd
->fd_rdir
;
459 proc_root_dir_vp
= NULL
;
462 vp_with_iocount
= NULLVP
;
466 end
= &buff
[buflen
- 1];
471 * Catch a special corner case here: chroot to /full/path/to/dir, chdir to
472 * it, then open it. Without this check, the path to it will be
473 * /full/path/to/dir instead of "/".
475 if (proc_root_dir_vp
== first_vp
) {
481 * holding the NAME_CACHE_LOCK in shared mode is
482 * sufficient to stabilize both the vp->v_parent chain
483 * and the 'vp->v_mount->mnt_vnodecovered' chain
485 * if we need to drop this lock, we must first grab the v_id
486 * from the vnode we're currently working with... if that
487 * vnode doesn't already have an io_count reference (the vp
488 * passed in comes with one), we must grab a reference
489 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
490 * deadlocks may result if you call vnode_get while holding
491 * the NAME_CACHE_LOCK... we lazily release the reference
492 * we pick up the next time we encounter a need to drop
493 * the NAME_CACHE_LOCK or before we return from this routine
495 NAME_CACHE_LOCK_SHARED();
498 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
499 (vp
->v_flag
& VFMLINKTARGET
) && vp
->v_fmlink
) {
505 * Check if this is the root of a file system.
507 while (vp
&& vp
->v_flag
& VROOT
) {
508 if (vp
->v_mount
== NULL
) {
512 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
514 * It's the root of the root file system, so it's
522 * This the root of the volume and the caller does not
523 * want to cross mount points. Therefore just return
524 * '/' as the relative path.
527 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
528 (vp
->v_flag
& VFMLINKTARGET
) && vp
->v_fmlink
) {
532 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
536 vp
= vp
->v_mount
->mnt_vnodecovered
;
537 if (!mntpt_end
&& vp
) {
544 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
548 * For hardlinks the v_name may be stale, so if its OK
549 * to enter a file system, ask the file system for the
550 * name and parent (below).
552 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
553 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
554 !(flags
& BUILDPATH_NO_FS_ENTER
);
559 if (str
== NULL
|| *str
== '\0') {
560 if (vp
->v_parent
!= NULL
) {
567 len
= (unsigned int)strlen(str
);
569 * Check that there's enough space (including space for the '/')
571 if ((unsigned int)(end
- buff
) < (len
+ 1)) {
576 * Copy the name backwards.
580 for (; len
> 0; len
--) {
584 * Add a path separator.
590 * Walk up the parent chain.
592 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
593 (flags
& BUILDPATH_NO_FS_ENTER
)) {
595 * In this if () block we are not allowed to enter the filesystem
596 * to conclusively get the most accurate parent identifier.
597 * As a result, if 'vp' does not identify '/' and it
598 * does not have a valid v_parent, then error out
599 * and disallow further path construction
601 if ((vp
->v_parent
== NULLVP
) && (rootvnode
!= vp
)) {
603 * Only '/' is allowed to have a NULL parent
604 * pointer. Upper level callers should ideally
605 * re-drive name lookup on receiving a ENOENT.
609 /* The code below will exit early if 'tvp = vp' == NULL */
614 * if the vnode we have in hand isn't a directory and it
615 * has a v_parent, then we started with the resource fork
616 * so skip up to avoid getting a duplicate copy of the
617 * file name in the path.
619 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
624 * No parent, go get it if supported.
626 struct vnode_attr va
;
630 * Make sure file system supports obtaining a path from id.
632 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
640 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
641 if (vp_with_iocount
) {
642 vnode_put(vp_with_iocount
);
643 vp_with_iocount
= NULLVP
;
645 if (vnode_getwithvid(vp
, vid
)) {
648 vp_with_iocount
= vp
;
651 VATTR_WANTED(&va
, va_parentid
);
654 VATTR_WANTED(&va
, va_name
);
655 va
.va_name
= zalloc(ZV_NAMEI
);
660 * Ask the file system for its parent id and for its name (optional).
662 ret
= vnode_getattr(vp
, &va
, ctx
);
665 if ((ret
== 0) && (VATTR_IS_SUPPORTED(&va
, va_name
))) {
667 vnode_update_identity(vp
, NULL
, str
, (unsigned int)strlen(str
), 0, VNODE_UPDATE_NAME
);
668 } else if (vp
->v_name
) {
675 len
= (unsigned int)strlen(str
);
678 * Check that there's enough space.
680 if ((unsigned int)(end
- buff
) < (len
+ 1)) {
683 /* Copy the name backwards. */
686 for (; len
> 0; len
--) {
690 * Add a path separator.
695 zfree(ZV_NAMEI
, va
.va_name
);
697 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
702 * Ask the file system for the parent vnode.
704 if ((ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
))) {
708 if (!fixhardlink
&& (vp
->v_parent
!= dvp
)) {
709 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
712 if (vp_with_iocount
) {
713 vnode_put(vp_with_iocount
);
716 vp_with_iocount
= vp
;
718 NAME_CACHE_LOCK_SHARED();
721 * if the vnode we have in hand isn't a directory and it
722 * has a v_parent, then we started with the resource fork
723 * so skip up to avoid getting a duplicate copy of the
724 * file name in the path.
726 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
731 if (vp
&& (flags
& BUILDPATH_CHECKACCESS
)) {
736 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
737 if (vp_with_iocount
) {
738 vnode_put(vp_with_iocount
);
739 vp_with_iocount
= NULLVP
;
741 if (vnode_getwithvid(vp
, vid
)) {
744 vp_with_iocount
= vp
;
746 if ((ret
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
))) {
747 goto out
; /* no peeking */
749 NAME_CACHE_LOCK_SHARED();
753 * When a mount point is crossed switch the vp.
754 * Continue until we find the root or we find
755 * a vnode that's not the root of a mounted
761 if (tvp
== proc_root_dir_vp
) {
762 goto out_unlock
; /* encountered the root */
766 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
767 (tvp
->v_flag
& VFMLINKTARGET
) && tvp
->v_fmlink
) {
773 if (!(tvp
->v_flag
& VROOT
) || !tvp
->v_mount
) {
774 break; /* not the root of a mounted FS */
776 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
777 /* Do not cross over mount points */
780 tvp
= tvp
->v_mount
->mnt_vnodecovered
;
781 if (!mntpt_end
&& tvp
) {
794 if (vp_with_iocount
) {
795 vnode_put(vp_with_iocount
);
798 * Slide the name down to the beginning of the buffer.
800 memmove(buff
, end
, &buff
[buflen
] - end
);
803 * length includes the trailing zero byte
805 *outlen
= (int)(&buff
[buflen
] - end
);
806 if (mntpt_outlen
&& mntpt_end
) {
807 *mntpt_outlen
= (size_t)*outlen
- (size_t)(&buff
[buflen
] - mntpt_end
);
810 /* One of the parents was moved during path reconstruction.
811 * The caller is interested in knowing whether any of the
812 * parents moved via BUILDPATH_CHECK_MOVED, so return EAGAIN.
814 if ((ret
== ENOENT
) && (flags
& BUILDPATH_CHECK_MOVED
)) {
822 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
824 return build_path_with_parent(first_vp
, NULL
, buff
, buflen
, outlen
, NULL
, flags
, ctx
);
828 * return NULLVP if vp's parent doesn't
829 * exist, or we can't get a valid iocount
830 * else return the parent of vp
833 vnode_getparent(vnode_t vp
)
835 vnode_t pvp
= NULLVP
;
838 NAME_CACHE_LOCK_SHARED();
843 * v_parent is stable behind the name_cache lock
844 * however, the only thing we can really guarantee
845 * is that we've grabbed a valid iocount on the
846 * parent of 'vp' at the time we took the name_cache lock...
847 * once we drop the lock, vp could get re-parented
854 if (vnode_getwithvid(pvp
, pvid
) != 0) {
864 vnode_getname(vnode_t vp
)
866 const char *name
= NULL
;
868 NAME_CACHE_LOCK_SHARED();
871 name
= vfs_addname(vp
->v_name
, (unsigned int)strlen(vp
->v_name
), 0, 0);
879 vnode_putname(const char *name
)
881 vfs_removename(name
);
884 static const char unknown_vnodename
[] = "(unknown vnode name)";
887 vnode_getname_printable(vnode_t vp
)
889 const char *name
= vnode_getname(vp
);
894 switch (vp
->v_type
) {
899 * Create an artificial dev name from
900 * major and minor device number
903 (void) snprintf(dev_name
, sizeof(dev_name
),
904 "%c(%u, %u)", VCHR
== vp
->v_type
? 'c':'b',
905 major(vp
->v_rdev
), minor(vp
->v_rdev
));
907 * Add the newly created dev name to the name
908 * cache to allow easier cleanup. Also,
909 * vfs_addname allocates memory for the new name
912 NAME_CACHE_LOCK_SHARED();
913 name
= vfs_addname(dev_name
, (unsigned int)strlen(dev_name
), 0, 0);
918 return unknown_vnodename
;
923 vnode_putname_printable(const char *name
)
925 if (name
== unknown_vnodename
) {
933 * if VNODE_UPDATE_PARENT, and we can take
934 * a reference on dvp, then update vp with
935 * it's new parent... if vp already has a parent,
936 * then drop the reference vp held on it
938 * if VNODE_UPDATE_NAME,
939 * then drop string ref on v_name if it exists, and if name is non-NULL
940 * then pick up a string reference on name and record it in v_name...
941 * optionally pass in the length and hashval of name if known
943 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
946 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, uint32_t name_hashval
, int flags
)
948 struct namecache
*ncp
;
949 vnode_t old_parentvp
= NULLVP
;
950 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
951 int kusecountbumped
= 0;
952 kauth_cred_t tcred
= NULL
;
953 const char *vname
= NULL
;
954 const char *tname
= NULL
;
960 if (flags
& VNODE_UPDATE_PARENT
) {
961 if (dvp
&& vnode_ref(dvp
) != 0) {
964 /* Don't count a stream's parent ref during unmounts */
965 if (isstream
&& dvp
&& (dvp
!= vp
) && (dvp
!= vp
->v_parent
) && (dvp
->v_type
== VREG
)) {
966 vnode_lock_spin(dvp
);
974 if ((flags
& VNODE_UPDATE_NAME
)) {
975 if (name
!= vp
->v_name
) {
978 name_len
= (int)strlen(name
);
980 tname
= vfs_addname(name
, name_len
, name_hashval
, 0);
983 flags
&= ~VNODE_UPDATE_NAME
;
986 if ((flags
& (VNODE_UPDATE_PURGE
| VNODE_UPDATE_PARENT
| VNODE_UPDATE_CACHE
| VNODE_UPDATE_NAME
| VNODE_UPDATE_PURGEFIRMLINK
))) {
990 if (flags
& VNODE_UPDATE_PURGEFIRMLINK
) {
991 vnode_t old_fvp
= vp
->v_fmlink
;
994 vp
->v_flag
&= ~VFMLINKTARGET
;
995 vp
->v_fmlink
= NULLVP
;
1000 * vnode_rele can result in cascading series of
1001 * usecount releases. The combination of calling
1002 * vnode_recycle and dont_reenter (3rd arg to
1003 * vnode_rele_internal) ensures we don't have
1006 vnode_recycle(old_fvp
);
1007 vnode_rele_internal(old_fvp
, O_EVTONLY
, 1, 0);
1014 if ((flags
& VNODE_UPDATE_PURGE
)) {
1016 vp
->v_parent
->v_nc_generation
++;
1019 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
1020 cache_delete(ncp
, 1);
1023 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
1024 cache_delete(ncp
, 1);
1028 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1031 vp
->v_cred
= NOCRED
;
1032 vp
->v_authorized_actions
= 0;
1033 vp
->v_cred_timestamp
= 0;
1035 if ((flags
& VNODE_UPDATE_NAME
)) {
1039 if (flags
& VNODE_UPDATE_PARENT
) {
1040 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
1041 old_parentvp
= vp
->v_parent
;
1046 flags
|= VNODE_UPDATE_CACHE
;
1050 if (flags
& VNODE_UPDATE_CACHE
) {
1051 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
1052 cache_delete(ncp
, 1);
1055 NAME_CACHE_UNLOCK();
1057 if (vname
!= NULL
) {
1058 vfs_removename(vname
);
1061 if (IS_VALID_CRED(tcred
)) {
1062 kauth_cred_unref(&tcred
);
1065 if (dvp
!= NULLVP
) {
1066 /* Back-out the ref we took if we lost a race for vp->v_parent. */
1067 if (kusecountbumped
) {
1068 vnode_lock_spin(dvp
);
1069 if (dvp
->v_kusecount
> 0) {
1080 vnode_lock_spin(old_parentvp
);
1081 if ((old_parentvp
->v_type
!= VDIR
) && (old_parentvp
->v_kusecount
> 0)) {
1082 --old_parentvp
->v_kusecount
;
1084 vnode_unlock(old_parentvp
);
1086 ut
= get_bsdthread_info(current_thread());
1089 * indicated to vnode_rele that it shouldn't do a
1090 * vnode_reclaim at this time... instead it will
1091 * chain the vnode to the uu_vreclaims list...
1092 * we'll be responsible for calling vnode_reclaim
1093 * on each of the vnodes in this list...
1095 ut
->uu_defer_reclaims
= 1;
1096 ut
->uu_vreclaims
= NULLVP
;
1098 while ((vp
= old_parentvp
) != NULLVP
) {
1099 vnode_lock_spin(vp
);
1100 vnode_rele_internal(vp
, 0, 0, 1);
1103 * check to see if the vnode is now in the state
1104 * that would have triggered a vnode_reclaim in vnode_rele
1105 * if it is, we save it's parent pointer and then NULL
1106 * out the v_parent field... we'll drop the reference
1107 * that was held on the next iteration of this loop...
1108 * this short circuits a potential deep recursion if we
1109 * have a long chain of parents in this state...
1110 * we'll sit in this loop until we run into
1111 * a parent in this chain that is not in this state
1113 * make our check and the vnode_rele atomic
1114 * with respect to the current vnode we're working on
1115 * by holding the vnode lock
1116 * if vnode_rele deferred the vnode_reclaim and has put
1117 * this vnode on the list to be reaped by us, than
1118 * it has left this vnode with an iocount == 1
1120 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1121 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1123 * vnode_rele wanted to do a vnode_reclaim on this vnode
1124 * it should be sitting on the head of the uu_vreclaims chain
1125 * pull the parent pointer now so that when we do the
1126 * vnode_reclaim for each of the vnodes in the uu_vreclaims
1127 * list, we won't recurse back through here
1129 * need to do a convert here in case vnode_rele_internal
1130 * returns with the lock held in the spin mode... it
1131 * can drop and retake the lock under certain circumstances
1133 vnode_lock_convert(vp
);
1136 old_parentvp
= vp
->v_parent
;
1137 vp
->v_parent
= NULLVP
;
1138 NAME_CACHE_UNLOCK();
1141 * we're done... we ran into a vnode that isn't
1144 old_parentvp
= NULLVP
;
1148 ut
->uu_defer_reclaims
= 0;
1150 while ((vp
= ut
->uu_vreclaims
) != NULLVP
) {
1151 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
1154 * vnode_put will drive the vnode_reclaim if
1155 * we are still the only reference on this vnode
1162 #if CONFIG_FIRMLINKS
1164 vnode_setasfirmlink(vnode_t vp
, vnode_t target_vp
)
1167 vnode_t old_target_vp
= NULLVP
;
1168 vnode_t old_target_vp_v_fmlink
= NULLVP
;
1169 kauth_cred_t target_vp_cred
= NULL
;
1170 kauth_cred_t old_target_vp_cred
= NULL
;
1177 if (vp
->v_fmlink
== target_vp
) { /* Will be checked again under the name cache lock */
1182 * Firmlink source and target will take both a usecount
1183 * and kusecount on each other.
1185 if ((error
= vnode_ref_ext(target_vp
, O_EVTONLY
, VNODE_REF_FORCE
))) {
1189 if ((error
= vnode_ref_ext(vp
, O_EVTONLY
, VNODE_REF_FORCE
))) {
1190 vnode_rele_ext(target_vp
, O_EVTONLY
, 1);
1197 old_target_vp
= vp
->v_fmlink
;
1198 if (target_vp
&& (target_vp
== old_target_vp
)) {
1199 NAME_CACHE_UNLOCK();
1202 vp
->v_fmlink
= target_vp
;
1204 vnode_lock_spin(vp
);
1205 vp
->v_flag
&= ~VFMLINKTARGET
;
1209 target_vp
->v_fmlink
= vp
;
1210 vnode_lock_spin(target_vp
);
1211 target_vp
->v_flag
|= VFMLINKTARGET
;
1212 vnode_unlock(target_vp
);
1213 cache_purge_locked(vp
, &target_vp_cred
);
1216 if (old_target_vp
) {
1217 old_target_vp_v_fmlink
= old_target_vp
->v_fmlink
;
1218 old_target_vp
->v_fmlink
= NULLVP
;
1219 vnode_lock_spin(old_target_vp
);
1220 old_target_vp
->v_flag
&= ~VFMLINKTARGET
;
1221 vnode_unlock(old_target_vp
);
1222 cache_purge_locked(vp
, &old_target_vp_cred
);
1225 NAME_CACHE_UNLOCK();
1227 if (target_vp_cred
&& IS_VALID_CRED(target_vp_cred
)) {
1228 kauth_cred_unref(&target_vp_cred
);
1231 if (old_target_vp
) {
1232 if (old_target_vp_cred
&& IS_VALID_CRED(old_target_vp_cred
)) {
1233 kauth_cred_unref(&old_target_vp_cred
);
1236 vnode_rele_ext(old_target_vp
, O_EVTONLY
, 1);
1237 if (old_target_vp_v_fmlink
) {
1238 vnode_rele_ext(old_target_vp_v_fmlink
, O_EVTONLY
, 1);
1246 vnode_getfirmlink(vnode_t vp
, vnode_t
*target_vp
)
1250 if (!vp
->v_fmlink
) {
1254 NAME_CACHE_LOCK_SHARED();
1255 if (vp
->v_fmlink
&& !(vp
->v_flag
& VFMLINKTARGET
) &&
1256 (vnode_get(vp
->v_fmlink
) == 0)) {
1257 vnode_t tvp
= vp
->v_fmlink
;
1259 vnode_lock_spin(tvp
);
1260 if (tvp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) {
1262 NAME_CACHE_UNLOCK();
1266 if (!(tvp
->v_flag
& VFMLINKTARGET
)) {
1267 panic("firmlink target for vnode %p does not have flag set", vp
);
1273 *target_vp
= NULLVP
;
1276 NAME_CACHE_UNLOCK();
1280 #else /* CONFIG_FIRMLINKS */
1283 vnode_setasfirmlink(__unused vnode_t vp
, __unused vnode_t src_vp
)
1289 vnode_getfirmlink(__unused vnode_t vp
, __unused vnode_t
*target_vp
)
1297 * Mark a vnode as having multiple hard links. HFS makes use of this
1298 * because it keeps track of each link separately, and wants to know
1299 * which link was actually used.
1301 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
1302 * so that HFS can post-process the lookup. Also, volfs will call
1303 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
1306 vnode_setmultipath(vnode_t vp
)
1308 vnode_lock_spin(vp
);
1311 * In theory, we're changing the vnode's identity as far as the
1312 * name cache is concerned, so we ought to grab the name cache lock
1313 * here. However, there is already a race, and grabbing the name
1314 * cache lock only makes the race window slightly smaller.
1316 * The race happens because the vnode already exists in the name
1317 * cache, and could be found by one thread before another thread
1318 * can set the hard link flag.
1321 vp
->v_flag
|= VISHARDLINK
;
1329 * backwards compatibility
1332 vnode_uncache_credentials(vnode_t vp
)
1334 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
1339 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
1340 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
1341 * we use this lock so that we can look at the v_cred and v_authorized_actions
1342 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
1343 * which is the super-hot path... if we are updating the authorized actions for this
1344 * vnode, we are already in the super-slow and far less frequented path so its not
1345 * that bad that we take the lock exclusive for this case... of course we strive
1346 * to hold it for the minimum amount of time possible
1350 vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
1352 kauth_cred_t tcred
= NOCRED
;
1356 vp
->v_authorized_actions
&= ~action
;
1358 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
1359 IS_VALID_CRED(vp
->v_cred
)) {
1361 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1364 vp
->v_cred
= NOCRED
;
1366 NAME_CACHE_UNLOCK();
1368 if (tcred
!= NOCRED
) {
1369 kauth_cred_unref(&tcred
);
1374 extern int bootarg_vnode_cache_defeat
; /* default = 0, from bsd_init.c */
1377 vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1380 boolean_t retval
= FALSE
;
1382 /* Boot argument to defeat rights caching */
1383 if (bootarg_vnode_cache_defeat
) {
1387 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1389 * a TTL is enabled on the rights cache... handle it here
1390 * a TTL of 0 indicates that no rights should be cached
1392 if (vp
->v_mount
->mnt_authcache_ttl
) {
1393 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1395 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
1396 * we will only allow a SEARCH right on a directory to be cached...
1397 * that cached right always has a default TTL associated with it
1399 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
) {
1403 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
1404 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
1412 ucred
= vfs_context_ucred(ctx
);
1414 NAME_CACHE_LOCK_SHARED();
1416 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
) {
1420 NAME_CACHE_UNLOCK();
1427 vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1429 kauth_cred_t tcred
= NOCRED
;
1432 boolean_t ttl_active
= FALSE
;
1434 ucred
= vfs_context_ucred(ctx
);
1436 if (!IS_VALID_CRED(ucred
) || action
== 0) {
1440 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1442 * a TTL is enabled on the rights cache... handle it here
1443 * a TTL of 0 indicates that no rights should be cached
1445 if (vp
->v_mount
->mnt_authcache_ttl
== 0) {
1449 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1451 * only cache SEARCH action for filesystems marked
1452 * MNTK_AUTH_OPAQUE on VDIRs...
1453 * the lookup_path code will time these out
1455 if ((action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
) {
1465 if (vp
->v_cred
!= ucred
) {
1466 kauth_cred_ref(ucred
);
1468 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1472 vp
->v_authorized_actions
= 0;
1474 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
1476 * only reset the timestamnp on the
1477 * first authorization cached after the previous
1478 * timer has expired or we're switching creds...
1479 * 'vnode_cache_is_authorized' will clear the
1480 * authorized actions if the TTL is active and
1483 vp
->v_cred_timestamp
= (int)tv
.tv_sec
;
1485 vp
->v_authorized_actions
|= action
;
1487 NAME_CACHE_UNLOCK();
1489 if (IS_VALID_CRED(tcred
)) {
1490 kauth_cred_unref(&tcred
);
1496 vnode_cache_is_stale(vnode_t vp
)
1503 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
) {
1515 * Returns: 0 Success
1516 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
1517 * This errno value should not be seen by anyone outside of the kernel.
1520 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
1521 vfs_context_t ctx
, int *dp_authorized
, vnode_t last_dp
)
1523 char *cp
; /* pointer into pathname argument */
1525 int vvid
= 0; /* protected by vp != NULLVP */
1526 vnode_t vp
= NULLVP
;
1527 vnode_t tdp
= NULLVP
;
1529 boolean_t ttl_enabled
= FALSE
;
1534 boolean_t dotdotchecked
= FALSE
;
1538 #endif /* CONFIG_TRIGGERS */
1540 ucred
= vfs_context_ucred(ctx
);
1541 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
1543 NAME_CACHE_LOCK_SHARED();
1545 if (dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1551 * Search a directory.
1553 * The cn_hash value is for use by cache_lookup
1554 * The last component of the filename is left accessible via
1555 * cnp->cn_nameptr for callers that need the name.
1558 cp
= cnp
->cn_nameptr
;
1560 while (*cp
&& (*cp
!= '/')) {
1561 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1564 * the crc generator can legitimately generate
1565 * a 0... however, 0 for us means that we
1566 * haven't computed a hash, so use 1 instead
1571 cnp
->cn_hash
= hash
;
1572 cnp
->cn_namelen
= (int)(cp
- cnp
->cn_nameptr
);
1574 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
1578 * Replace multiple slashes by a single slash and trailing slashes
1579 * by a null. This must be done before VNOP_LOOKUP() because some
1580 * fs's don't know about trailing slashes. Remember if there were
1581 * trailing slashes to handle symlinks, existing non-directories
1582 * and non-existing files that won't be directories specially later.
1584 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
1589 ndp
->ni_flag
|= NAMEI_TRAILINGSLASH
;
1590 *ndp
->ni_next
= '\0';
1595 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
1598 cnp
->cn_flags
|= ISLASTCN
;
1601 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.') {
1602 cnp
->cn_flags
|= ISDOTDOT
;
1608 * Process a request for a file's resource fork.
1610 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1612 if ((ndp
->ni_pathlen
== sizeof(_PATH_RSRCFORKSPEC
)) &&
1613 (cp
[1] == '.' && cp
[2] == '.') &&
1614 bcmp(cp
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
1615 /* Skip volfs file systems that don't support native streams. */
1616 if ((dp
->v_mount
!= NULL
) &&
1617 (dp
->v_mount
->mnt_flag
& MNT_DOVOLFS
) &&
1618 (dp
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0) {
1621 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
1622 cnp
->cn_flags
|= ISLASTCN
;
1623 ndp
->ni_next
[0] = '\0';
1624 ndp
->ni_pathlen
= 1;
1632 * Name cache provides authorization caching (see below)
1633 * that will short circuit MAC checks in lookup().
1634 * We must perform MAC check here. On denial
1635 * dp_authorized will remain 0 and second check will
1636 * be perfomed in lookup().
1638 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
1639 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
1641 NAME_CACHE_UNLOCK();
1647 (dp
->v_mount
->mnt_authcache_ttl
== 0 ||
1648 ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))) {
1653 * NAME_CACHE_LOCK holds these fields stable
1655 * We can't cache KAUTH_VNODE_SEARCHBYANYONE for root correctly
1656 * so we make an ugly check for root here. root is always
1657 * allowed and breaking out of here only to find out that is
1658 * authorized by virtue of being root is very very expensive.
1659 * However, the check for not root is valid only for filesystems
1660 * which use local authorization.
1662 * XXX: Remove the check for root when we can reliably set
1663 * KAUTH_VNODE_SEARCHBYANYONE as root.
1665 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
1666 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
) &&
1667 (ttl_enabled
|| !vfs_context_issuser(ctx
))) {
1672 * indicate that we're allowed to traverse this directory...
1673 * even if we fail the cache lookup or decide to bail for
1674 * some other reason, this information is valid and is used
1675 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1679 if ((cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
))) {
1681 * Moving the firmlinks section to be first to catch a corner case:
1682 * When using DOTDOT to get a parent of a firmlink, we want the
1683 * firmlink source to be resolved even if cn_nameiop != LOOKUP.
1684 * This is because lookup() traverses DOTDOT by calling VNOP_LOOKUP
1685 * and has no notion about firmlinks
1687 #if CONFIG_FIRMLINKS
1688 if (cnp
->cn_flags
& ISDOTDOT
&& dp
->v_fmlink
&& (dp
->v_flag
& VFMLINKTARGET
)) {
1692 if (cnp
->cn_nameiop
!= LOOKUP
) {
1695 if (cnp
->cn_flags
& LOCKPARENT
) {
1698 if (cnp
->cn_flags
& NOCACHE
) {
1702 if (cnp
->cn_flags
& ISDOTDOT
) {
1704 * Force directory hardlinks to go to
1705 * file system for ".." requests.
1707 if ((dp
->v_flag
& VISHARDLINK
)) {
1711 * Quit here only if we can't use
1712 * the parent directory pointer or
1713 * don't have one. Otherwise, we'll
1716 if ((dp
->v_flag
& VROOT
) ||
1717 dp
== ndp
->ni_rootdir
||
1718 dp
->v_parent
== NULLVP
) {
1724 if ((cnp
->cn_flags
& CN_SKIPNAMECACHE
)) {
1726 * Force lookup to go to the filesystem with
1727 * all cnp fields set up.
1733 * "." and ".." aren't supposed to be cached, so check
1734 * for them before checking the cache.
1736 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') {
1738 } else if ((cnp
->cn_flags
& ISDOTDOT
)) {
1740 * If this is a chrooted process, we need to check if
1741 * the process is trying to break out of its chrooted
1742 * jail. We do that by trying to determine if dp is
1743 * a subdirectory of ndp->ni_rootdir. If we aren't
1744 * able to determine that by the v_parent pointers, we
1745 * will leave the fast path.
1747 * Since this function may see dotdot components
1748 * many times and it has the name cache lock held for
1749 * the entire duration, we optimise this by doing this
1750 * check only once per cache_lookup_path call.
1751 * If dotdotchecked is set, it means we've done this
1752 * check once already and don't need to do it again.
1754 if (!dotdotchecked
&& (ndp
->ni_rootdir
!= rootvnode
)) {
1756 boolean_t defer
= FALSE
;
1757 boolean_t is_subdir
= FALSE
;
1759 defer
= cache_check_vnode_issubdir(tvp
,
1760 ndp
->ni_rootdir
, &is_subdir
, &tvp
);
1763 /* defer to Filesystem */
1765 } else if (!is_subdir
) {
1767 * This process is trying to break out
1768 * of its chrooted jail, so all its
1769 * dotdot accesses will be translated to
1770 * its root directory.
1772 vp
= ndp
->ni_rootdir
;
1775 * All good, let this dotdot access
1780 dotdotchecked
= TRUE
;
1785 if ((vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
) {
1789 if ((vp
->v_flag
& VISHARDLINK
)) {
1791 * The file system wants a VNOP_LOOKUP on this vnode
1797 if ((cnp
->cn_flags
& ISLASTCN
)) {
1801 if (vp
->v_type
!= VDIR
) {
1802 if (vp
->v_type
!= VLNK
) {
1808 if ((mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1809 vnode_t tmp_vp
= mp
->mnt_realrootvp
;
1810 if (tmp_vp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1811 mp
->mnt_realrootvp_vid
!= tmp_vp
->v_id
) {
1819 * After traversing all mountpoints stacked here, if we have a
1820 * trigger in hand, resolve it. Note that we don't need to
1821 * leave the fast path if the mount has already happened.
1823 if (vp
->v_resolve
) {
1826 #endif /* CONFIG_TRIGGERS */
1832 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1834 while (*cnp
->cn_nameptr
== '/') {
1844 NAME_CACHE_UNLOCK();
1846 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1847 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
1849 * if we've got a child and it's the last component, and
1850 * the lookup doesn't need to return the parent then we
1851 * can skip grabbing an iocount on the parent, since all
1852 * we're going to do with it is a vnode_put just before
1853 * we return from 'lookup'. If it's a symbolic link,
1854 * we need the parent in case the link happens to be
1855 * a relative pathname.
1862 * return the last directory we looked at
1863 * with an io reference held. If it was the one passed
1864 * in as a result of the last iteration of VNOP_LOOKUP,
1865 * it should already hold an io ref. No need to increase ref.
1867 if (last_dp
!= dp
) {
1868 if (dp
== ndp
->ni_usedvp
) {
1870 * if this vnode matches the one passed in via USEDVP
1871 * than this context already holds an io_count... just
1872 * use vnode_get to get an extra ref for lookup to play
1873 * with... can't use the getwithvid variant here because
1874 * it will block behind a vnode_drain which would result
1875 * in a deadlock (since we already own an io_count that the
1876 * vnode_drain is waiting on)... vnode_get grabs the io_count
1877 * immediately w/o waiting... it always succeeds
1880 } else if ((error
= vnode_getwithvid_drainok(dp
, vid
))) {
1882 * failure indicates the vnode
1883 * changed identity or is being
1884 * TERMINATED... in either case
1887 * don't necessarily return ENOENT, though, because
1888 * we really want to go back to disk and make sure it's
1889 * there or not if someone else is changing this
1890 * vnode. That being said, the one case where we do want
1891 * to return ENOENT is when the vnode's mount point is
1892 * in the process of unmounting and we might cause a deadlock
1893 * in our attempt to take an iocount. An ENODEV error return
1894 * is from vnode_get* is an indication this but we change that
1895 * ENOENT for upper layers.
1897 if (error
== ENODEV
) {
1907 if ((vnode_getwithvid_drainok(vp
, vvid
))) {
1911 * can't get reference on the vp we'd like
1912 * to return... if we didn't grab a reference
1913 * on the directory (due to fast path bypass),
1914 * then we need to do it now... we can't return
1915 * with both ni_dvp and ni_vp NULL, and no
1929 trigger_vp
= vp
? vp
: dp
;
1930 if ((error
== 0) && (trigger_vp
!= NULLVP
) && vnode_isdir(trigger_vp
)) {
1931 error
= vnode_trigger_resolve(trigger_vp
, ndp
, ctx
);
1942 #endif /* CONFIG_TRIGGERS */
1946 * If we came into cache_lookup_path after an iteration of the lookup loop that
1947 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1948 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1949 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1950 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1951 * the case that we got to a subsequent path component and this previous vnode is
1952 * no longer needed. We can then drop the io ref on it.
1954 if ((last_dp
!= NULLVP
) && (last_dp
!= ndp
->ni_dvp
)) {
1958 //initialized to 0, should be the same if no error cases occurred.
1964 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1966 struct namecache
*ncp
;
1967 struct nchashhead
*ncpp
;
1968 long namelen
= cnp
->cn_namelen
;
1969 unsigned int hashval
= cnp
->cn_hash
;
1975 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1976 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1977 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1978 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
1985 * We failed to find an entry
1990 NCHSTAT(ncs_goodhits
);
1996 unsigned int hash_string(const char *cp
, int len
);
1998 // Have to take a len argument because we may only need to
1999 // hash part of a componentname.
2002 hash_string(const char *cp
, int len
)
2008 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
2011 while (*cp
!= '\0') {
2012 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
2016 * the crc generator can legitimately generate
2017 * a 0... however, 0 for us means that we
2018 * haven't computed a hash, so use 1 instead
2028 * Lookup an entry in the cache
2030 * We don't do this if the segment name is long, simply so the cache
2031 * can avoid holding long names (which would either waste space, or
2032 * add greatly to the complexity).
2034 * Lookup is called with dvp pointing to the directory to search,
2035 * cnp pointing to the name of the entry being sought. If the lookup
2036 * succeeds, the vnode is returned in *vpp, and a status of -1 is
2037 * returned. If the lookup determines that the name does not exist
2038 * (negative cacheing), a status of ENOENT is returned. If the lookup
2039 * fails, a status of zero is returned.
2043 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
2045 struct namecache
*ncp
;
2046 struct nchashhead
*ncpp
;
2047 long namelen
= cnp
->cn_namelen
;
2048 unsigned int hashval
;
2049 boolean_t have_exclusive
= FALSE
;
2053 if (cnp
->cn_hash
== 0) {
2054 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2056 hashval
= cnp
->cn_hash
;
2062 NAME_CACHE_LOCK_SHARED();
2065 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
2066 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
2067 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
2068 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
2073 /* We failed to find an entry */
2076 NAME_CACHE_UNLOCK();
2080 /* We don't want to have an entry, so dump it */
2081 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
2082 if (have_exclusive
== TRUE
) {
2083 NCHSTAT(ncs_badhits
);
2084 cache_delete(ncp
, 1);
2085 NAME_CACHE_UNLOCK();
2088 NAME_CACHE_UNLOCK();
2090 have_exclusive
= TRUE
;
2095 /* We found a "positive" match, return the vnode */
2097 NCHSTAT(ncs_goodhits
);
2100 NAME_CACHE_UNLOCK();
2102 if (vnode_getwithvid(vp
, vid
)) {
2105 NCHSTAT(ncs_badvid
);
2106 NAME_CACHE_UNLOCK();
2114 /* We found a negative match, and want to create it, so purge */
2115 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
2116 if (have_exclusive
== TRUE
) {
2117 NCHSTAT(ncs_badhits
);
2118 cache_delete(ncp
, 1);
2119 NAME_CACHE_UNLOCK();
2122 NAME_CACHE_UNLOCK();
2124 have_exclusive
= TRUE
;
2129 * We found a "negative" match, ENOENT notifies client of this match.
2131 NCHSTAT(ncs_neghits
);
2133 NAME_CACHE_UNLOCK();
2138 cache_enter_create(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
)
2140 const char *strname
;
2142 if (cnp
->cn_hash
== 0) {
2143 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2147 * grab 2 references on the string entered
2148 * one for the cache_enter_locked to consume
2149 * and the second to be consumed by v_name (vnode_create call point)
2151 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, TRUE
, 0);
2155 cache_enter_locked(dvp
, vp
, cnp
, strname
);
2157 NAME_CACHE_UNLOCK();
2164 * Add an entry to the cache...
2165 * but first check to see if the directory
2166 * that this entry is to be associated with has
2167 * had any cache_purges applied since we took
2168 * our identity snapshot... this check needs to
2169 * be done behind the name cache lock
2172 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
2174 if (cnp
->cn_hash
== 0) {
2175 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2180 if (dvp
->v_nc_generation
== gen
) {
2181 (void)cache_enter_locked(dvp
, vp
, cnp
, NULL
);
2184 NAME_CACHE_UNLOCK();
2189 * Add an entry to the cache.
2192 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
2194 const char *strname
;
2196 if (cnp
->cn_hash
== 0) {
2197 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2201 * grab 1 reference on the string entered
2202 * for the cache_enter_locked to consume
2204 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
2208 cache_enter_locked(dvp
, vp
, cnp
, strname
);
2210 NAME_CACHE_UNLOCK();
2215 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, const char *strname
)
2217 struct namecache
*ncp
, *negp
;
2218 struct nchashhead
*ncpp
;
2225 * if the entry is for -ve caching vp is null
2227 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
2229 * someone beat us to the punch..
2230 * this vnode is already in the cache
2232 if (strname
!= NULL
) {
2233 vfs_removename(strname
);
2238 * We allocate a new entry if we are less than the maximum
2239 * allowed and the one at the front of the list is in use.
2240 * Otherwise we use the one at the front of the list.
2242 if (numcache
< desiredNodes
&&
2243 ((ncp
= nchead
.tqh_first
) == NULL
||
2244 ncp
->nc_hash
.le_prev
!= 0)) {
2246 * Allocate one more entry
2248 ncp
= zalloc(namecache_zone
);
2252 * reuse an old entry
2254 ncp
= TAILQ_FIRST(&nchead
);
2255 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2257 if (ncp
->nc_hash
.le_prev
!= 0) {
2259 * still in use... we need to
2260 * delete it before re-using it
2262 NCHSTAT(ncs_stolen
);
2263 cache_delete(ncp
, 0);
2266 NCHSTAT(ncs_enters
);
2269 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
2273 ncp
->nc_hashval
= cnp
->cn_hash
;
2275 if (strname
== NULL
) {
2276 ncp
->nc_name
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
2278 ncp
->nc_name
= strname
;
2282 // If the bytes of the name associated with the vnode differ,
2283 // use the name associated with the vnode since the file system
2284 // may have set that explicitly in the case of a lookup on a
2285 // case-insensitive file system where the case of the looked up
2286 // name differs from what is on disk. For more details, see:
2287 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
2289 const char *vn_name
= vp
? vp
->v_name
: NULL
;
2290 unsigned int len
= vn_name
? (unsigned int)strlen(vn_name
) : 0;
2291 if (vn_name
&& ncp
&& ncp
->nc_name
&& strncmp(ncp
->nc_name
, vn_name
, len
) != 0) {
2292 unsigned int hash
= hash_string(vn_name
, len
);
2294 vfs_removename(ncp
->nc_name
);
2295 ncp
->nc_name
= add_name_internal(vn_name
, len
, hash
, FALSE
, 0);
2296 ncp
->nc_hashval
= hash
;
2300 * make us the newest entry in the cache
2301 * i.e. we'll be the last to be stolen
2303 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
2305 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
2308 struct namecache
*p
;
2310 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
) {
2312 panic("cache_enter: duplicate");
2318 * make us available to be found via lookup
2320 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
2324 * add to the list of name cache entries
2327 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
2330 * this is a negative cache entry (vp == NULL)
2331 * stick it on the negative cache list.
2333 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
2337 if (ncs_negtotal
> desiredNegNodes
) {
2339 * if we've reached our desired limit
2340 * of negative cache entries, delete
2343 negp
= TAILQ_FIRST(&neghead
);
2344 cache_delete(negp
, 1);
2348 * add us to the list of name cache entries that
2349 * are children of dvp
2352 TAILQ_INSERT_TAIL(&dvp
->v_ncchildren
, ncp
, nc_child
);
2354 TAILQ_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
2360 * Initialize CRC-32 remainder table.
2366 * the CRC-32 generator polynomial is:
2367 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
2368 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
2370 unsigned int crc32_polynomial
= 0x04c11db7;
2374 * pre-calculate the CRC-32 remainder for each possible octet encoding
2376 for (i
= 0; i
< 256; i
++) {
2377 unsigned int crc_rem
= i
<< 24;
2379 for (j
= 0; j
< 8; j
++) {
2380 if (crc_rem
& 0x80000000) {
2381 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
2383 crc_rem
= (crc_rem
<< 1);
2386 crc32tab
[i
] = crc_rem
;
2392 * Name cache initialization, from vfs_init() when we are booting
2397 desiredNegNodes
= (desiredvnodes
/ 10);
2398 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
2400 TAILQ_INIT(&nchead
);
2401 TAILQ_INIT(&neghead
);
2405 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 * desiredNodes
)), M_CACHE
, &nchash
);
2406 nchashmask
= nchash
;
2409 init_string_table();
2411 for (int i
= 0; i
< NUM_STRCACHE_LOCKS
; i
++) {
2412 lck_mtx_init(&strcache_mtx_locks
[i
], &strcache_lck_grp
, &strcache_lck_attr
);
2417 name_cache_lock_shared(void)
2419 lck_rw_lock_shared(&namecache_rw_lock
);
2423 name_cache_lock(void)
2425 lck_rw_lock_exclusive(&namecache_rw_lock
);
2429 name_cache_unlock(void)
2431 lck_rw_done(&namecache_rw_lock
);
2436 resize_namecache(int newsize
)
2438 struct nchashhead
*new_table
;
2439 struct nchashhead
*old_table
;
2440 struct nchashhead
*old_head
, *head
;
2441 struct namecache
*entry
, *next
;
2442 uint32_t i
, hashval
;
2443 int dNodes
, dNegNodes
, nelements
;
2444 u_long new_size
, old_size
;
2450 dNegNodes
= (newsize
/ 10);
2451 dNodes
= newsize
+ dNegNodes
;
2452 // we don't support shrinking yet
2453 if (dNodes
<= desiredNodes
) {
2457 if (os_mul_overflow(dNodes
, 2, &nelements
)) {
2461 new_table
= hashinit(nelements
, M_CACHE
, &nchashmask
);
2462 new_size
= nchashmask
+ 1;
2464 if (new_table
== NULL
) {
2470 old_table
= nchashtbl
;
2471 nchashtbl
= new_table
;
2475 // walk the old table and insert all the entries into
2478 for (i
= 0; i
< old_size
; i
++) {
2479 old_head
= &old_table
[i
];
2480 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2482 // XXXdbg - Beware: this assumes that hash_string() does
2483 // the same thing as what happens in
2484 // lookup() over in vfs_lookup.c
2485 hashval
= hash_string(entry
->nc_name
, 0);
2486 entry
->nc_hashval
= hashval
;
2487 head
= NCHHASH(entry
->nc_dvp
, hashval
);
2489 next
= entry
->nc_hash
.le_next
;
2490 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
2493 desiredNodes
= dNodes
;
2494 desiredNegNodes
= dNegNodes
;
2496 NAME_CACHE_UNLOCK();
2497 FREE(old_table
, M_CACHE
);
2503 cache_delete(struct namecache
*ncp
, int free_entry
)
2505 NCHSTAT(ncs_deletes
);
2508 LIST_REMOVE(ncp
, nc_un
.nc_link
);
2510 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
2513 TAILQ_REMOVE(&(ncp
->nc_dvp
->v_ncchildren
), ncp
, nc_child
);
2515 LIST_REMOVE(ncp
, nc_hash
);
2517 * this field is used to indicate
2518 * that the entry is in use and
2519 * must be deleted before it can
2522 ncp
->nc_hash
.le_prev
= NULL
;
2524 vfs_removename(ncp
->nc_name
);
2525 ncp
->nc_name
= NULL
;
2527 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2528 zfree(namecache_zone
, ncp
);
2535 * purge the entry associated with the
2536 * specified vnode from the name cache
2539 cache_purge_locked(vnode_t vp
, kauth_cred_t
*credp
)
2541 struct namecache
*ncp
;
2544 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
2545 (TAILQ_FIRST(&vp
->v_ncchildren
) == NULL
) &&
2546 (vp
->v_cred
== NOCRED
) &&
2547 (vp
->v_parent
== NULLVP
)) {
2552 vp
->v_parent
->v_nc_generation
++;
2555 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
2556 cache_delete(ncp
, 1);
2559 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
2560 cache_delete(ncp
, 1);
2564 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
2566 *credp
= vp
->v_cred
;
2567 vp
->v_cred
= NOCRED
;
2568 vp
->v_authorized_actions
= 0;
2572 cache_purge(vnode_t vp
)
2574 kauth_cred_t tcred
= NULL
;
2576 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
2577 (TAILQ_FIRST(&vp
->v_ncchildren
) == NULL
) &&
2578 (vp
->v_cred
== NOCRED
) &&
2579 (vp
->v_parent
== NULLVP
)) {
2585 cache_purge_locked(vp
, &tcred
);
2587 NAME_CACHE_UNLOCK();
2589 if (tcred
&& IS_VALID_CRED(tcred
)) {
2590 kauth_cred_unref(&tcred
);
2595 * Purge all negative cache entries that are children of the
2596 * given vnode. A case-insensitive file system (or any file
2597 * system that has multiple equivalent names for the same
2598 * directory entry) can use this when creating or renaming
2599 * to remove negative entries that may no longer apply.
2602 cache_purge_negatives(vnode_t vp
)
2604 struct namecache
*ncp
, *next_ncp
;
2608 TAILQ_FOREACH_SAFE(ncp
, &vp
->v_ncchildren
, nc_child
, next_ncp
) {
2613 cache_delete(ncp
, 1);
2616 NAME_CACHE_UNLOCK();
2620 * Flush all entries referencing a particular filesystem.
2622 * Since we need to check it anyway, we will flush all the invalid
2623 * entries at the same time.
2626 cache_purgevfs(struct mount
*mp
)
2628 struct nchashhead
*ncpp
;
2629 struct namecache
*ncp
;
2632 /* Scan hash tables for applicable entries */
2633 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
2635 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
2636 if (ncp
->nc_dvp
->v_mount
== mp
) {
2637 cache_delete(ncp
, 0);
2642 NAME_CACHE_UNLOCK();
2648 // String ref routines
2650 static LIST_HEAD(stringhead
, string_t
) * string_ref_table
;
2651 static u_long string_table_mask
;
2652 static uint32_t filled_buckets
= 0;
2655 typedef struct string_t
{
2656 LIST_ENTRY(string_t
) hash_chain
;
2663 resize_string_ref_table(void)
2665 struct stringhead
*new_table
;
2666 struct stringhead
*old_table
;
2667 struct stringhead
*old_head
, *head
;
2668 string_t
*entry
, *next
;
2669 uint32_t i
, hashval
;
2670 u_long new_mask
, old_mask
;
2673 * need to hold the table lock exclusively
2674 * in order to grow the table... need to recheck
2675 * the need to resize again after we've taken
2676 * the lock exclusively in case some other thread
2677 * beat us to the punch
2679 lck_rw_lock_exclusive(&strtable_rw_lock
);
2681 if (4 * filled_buckets
< ((string_table_mask
+ 1) * 3)) {
2682 lck_rw_done(&strtable_rw_lock
);
2685 assert(string_table_mask
< INT32_MAX
);
2686 new_table
= hashinit((int)(string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
2688 if (new_table
== NULL
) {
2689 printf("failed to resize the hash table.\n");
2690 lck_rw_done(&strtable_rw_lock
);
2695 old_table
= string_ref_table
;
2696 string_ref_table
= new_table
;
2697 old_mask
= string_table_mask
;
2698 string_table_mask
= new_mask
;
2701 // walk the old table and insert all the entries into
2704 for (i
= 0; i
<= old_mask
; i
++) {
2705 old_head
= &old_table
[i
];
2706 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2707 hashval
= hash_string((const char *)entry
->str
, 0);
2708 head
= &string_ref_table
[hashval
& string_table_mask
];
2709 if (head
->lh_first
== NULL
) {
2712 next
= entry
->hash_chain
.le_next
;
2713 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2716 lck_rw_done(&strtable_rw_lock
);
2718 FREE(old_table
, M_CACHE
);
2723 init_string_table(void)
2725 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
2730 vfs_addname(const char *name
, uint32_t len
, u_int hashval
, u_int flags
)
2732 return add_name_internal(name
, len
, hashval
, FALSE
, flags
);
2737 add_name_internal(const char *name
, uint32_t len
, u_int hashval
, boolean_t need_extra_ref
, __unused u_int flags
)
2739 struct stringhead
*head
;
2741 uint32_t chain_len
= 0;
2742 uint32_t hash_index
;
2743 uint32_t lock_index
;
2746 if (len
> MAXPATHLEN
) {
2751 * if the length already accounts for the null-byte, then
2752 * subtract one so later on we don't index past the end
2755 if (len
> 0 && name
[len
- 1] == '\0') {
2759 hashval
= hash_string(name
, len
);
2763 * take this lock 'shared' to keep the hash stable
2764 * if someone else decides to grow the pool they
2765 * will take this lock exclusively
2767 lck_rw_lock_shared(&strtable_rw_lock
);
2770 * If the table gets more than 3/4 full, resize it
2772 if (4 * filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
2773 lck_rw_done(&strtable_rw_lock
);
2775 resize_string_ref_table();
2777 lck_rw_lock_shared(&strtable_rw_lock
);
2779 hash_index
= hashval
& string_table_mask
;
2780 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2782 head
= &string_ref_table
[hash_index
];
2784 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2786 for (entry
= head
->lh_first
; entry
!= NULL
; chain_len
++, entry
= entry
->hash_chain
.le_next
) {
2787 if (strncmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == 0) {
2792 if (entry
== NULL
) {
2793 lck_mtx_convert_spin(&strcache_mtx_locks
[lock_index
]);
2795 * it wasn't already there so add it.
2797 entry
= kheap_alloc(KHEAP_DEFAULT
, sizeof(string_t
) + len
+ 1, Z_WAITOK
);
2799 if (head
->lh_first
== NULL
) {
2800 OSAddAtomic(1, &filled_buckets
);
2802 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
2803 strncpy(ptr
, name
, len
);
2806 entry
->refcount
= 1;
2807 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2809 if (need_extra_ref
== TRUE
) {
2813 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2814 lck_rw_done(&strtable_rw_lock
);
2816 return (const char *)entry
->str
;
2821 vfs_removename(const char *nameref
)
2823 struct stringhead
*head
;
2826 uint32_t hash_index
;
2827 uint32_t lock_index
;
2828 int retval
= ENOENT
;
2830 hashval
= hash_string(nameref
, 0);
2833 * take this lock 'shared' to keep the hash stable
2834 * if someone else decides to grow the pool they
2835 * will take this lock exclusively
2837 lck_rw_lock_shared(&strtable_rw_lock
);
2839 * must compute the head behind the table lock
2840 * since the size and location of the table
2841 * can change on the fly
2843 hash_index
= hashval
& string_table_mask
;
2844 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2846 head
= &string_ref_table
[hash_index
];
2848 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2850 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2851 if (entry
->str
== nameref
) {
2854 if (entry
->refcount
== 0) {
2855 LIST_REMOVE(entry
, hash_chain
);
2857 if (head
->lh_first
== NULL
) {
2858 OSAddAtomic(-1, &filled_buckets
);
2867 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2868 lck_rw_done(&strtable_rw_lock
);
2870 kheap_free_addr(KHEAP_DEFAULT
, entry
);
2876 #ifdef DUMP_STRING_TABLE
2878 dump_string_table(void)
2880 struct stringhead
*head
;
2884 lck_rw_lock_shared(&strtable_rw_lock
);
2886 for (i
= 0; i
<= string_table_mask
; i
++) {
2887 head
= &string_ref_table
[i
];
2888 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2889 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
2892 lck_rw_done(&strtable_rw_lock
);
2894 #endif /* DUMP_STRING_TABLE */