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
);
167 static void cache_purge_locked(vnode_t vp
, kauth_cred_t
*credp
);
169 #ifdef DUMP_STRING_TABLE
171 * Internal dump function used for debugging
173 void dump_string_table(void);
174 #endif /* DUMP_STRING_TABLE */
176 static void init_crc32(void);
177 static unsigned int crc32tab
[256];
180 #define NCHHASH(dvp, hash_val) \
181 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
184 * This function tries to check if a directory vp is a subdirectory of dvp
185 * only from valid v_parent pointers. It is called with the name cache lock
186 * held and does not drop the lock anytime inside the function.
188 * It returns a boolean that indicates whether or not it was able to
189 * successfully infer the parent/descendent relationship via the v_parent
190 * pointers, or if it could not infer such relationship and that the decision
191 * must be delegated to the owning filesystem.
193 * If it does not defer the decision, i.e. it was successfuly able to determine
194 * the parent/descendent relationship, *is_subdir tells the caller if vp is a
195 * subdirectory of dvp.
197 * If the decision is deferred, *next_vp is where it stopped i.e. *next_vp
198 * is the vnode whose parent is to be determined from the filesystem.
199 * *is_subdir, in this case, is not indicative of anything and should be
202 * The return value and output args should be used as follows :
204 * defer = cache_check_vnode_issubdir(vp, dvp, is_subdir, next_vp);
207 * vp is subdirectory;
209 * vp is not a subdirectory;
212 * check this vnode's parent from the filesystem
214 * error (likely because of forced unmount).
219 cache_check_vnode_issubdir(vnode_t vp
, vnode_t dvp
, boolean_t
*is_subdir
,
233 } else if (tvp
== rootvnode
) {
234 /* *is_subdir = FALSE */
239 while ((tvp
->v_flag
& VROOT
) && tmp
&& tmp
->mnt_vnodecovered
&&
240 tvp
!= dvp
&& tvp
!= rootvnode
) {
241 tvp
= tmp
->mnt_vnodecovered
;
246 * If dvp is not at the top of a mount "stack" then
247 * vp is not a subdirectory of dvp either.
249 if (tvp
== dvp
|| tvp
== rootvnode
) {
250 /* *is_subdir = FALSE */
260 if ((tvp
->v_flag
& VISHARDLINK
) || !(tvp
->v_parent
)) {
272 /* maximum times retry from potentially transient errors in vnode_issubdir */
273 #define MAX_ERROR_RETRY 3
276 * This function checks if a given directory (vp) is a subdirectory of dvp.
277 * It walks backwards from vp and if it hits dvp in its parent chain,
278 * it is a subdirectory. If it encounters the root directory, it is not
281 * This function returns an error if it is unsuccessful and 0 on success.
283 * On entry (and exit) vp has an iocount and if this function has to take
284 * any iocounts on other vnodes in the parent chain traversal, it releases them.
287 vnode_issubdir(vnode_t vp
, vnode_t dvp
, int *is_subdir
, vfs_context_t ctx
)
289 vnode_t start_vp
, tvp
;
290 vnode_t vp_with_iocount
;
292 char dotdotbuf
[] = "..";
293 int error_retry_count
= 0; /* retry count for potentially transient
299 * Anytime we acquire an iocount in this function, we save the vnode
300 * in this variable and release it before exiting.
302 vp_with_iocount
= NULLVP
;
308 struct componentname cn
;
309 boolean_t is_subdir_locked
= FALSE
;
314 } else if (tvp
== rootvnode
) {
315 /* *is_subdir = FALSE */
319 NAME_CACHE_LOCK_SHARED();
321 defer
= cache_check_vnode_issubdir(tvp
, dvp
, &is_subdir_locked
,
325 vid
= vnode_vid(tvp
);
331 *is_subdir
= is_subdir_locked
;
336 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
344 if (tvp
!= start_vp
) {
345 if (vp_with_iocount
) {
346 vnode_put(vp_with_iocount
);
347 vp_with_iocount
= NULLVP
;
350 error
= vnode_getwithvid(tvp
, vid
);
352 if (error_retry_count
++ < MAX_ERROR_RETRY
) {
360 vp_with_iocount
= tvp
;
363 bzero(&cn
, sizeof(cn
));
364 cn
.cn_nameiop
= LOOKUP
;
365 cn
.cn_flags
= ISLASTCN
| ISDOTDOT
;
367 cn
.cn_pnbuf
= &dotdotbuf
[0];
368 cn
.cn_pnlen
= sizeof(dotdotbuf
);
369 cn
.cn_nameptr
= cn
.cn_pnbuf
;
373 if ((error
= VNOP_LOOKUP(tvp
, &pvp
, &cn
, ctx
))) {
377 if (!(tvp
->v_flag
& VISHARDLINK
) && tvp
->v_parent
!= pvp
) {
378 (void)vnode_update_identity(tvp
, pvp
, NULL
, 0, 0,
379 VNODE_UPDATE_PARENT
);
382 if (vp_with_iocount
) {
383 vnode_put(vp_with_iocount
);
386 vp_with_iocount
= tvp
= pvp
;
389 if (vp_with_iocount
) {
390 vnode_put(vp_with_iocount
);
397 * This function builds the path in "buff" from the supplied vnode.
398 * The length of the buffer *INCLUDING* the trailing zero byte is
399 * returned in outlen. NOTE: the length includes the trailing zero
400 * byte and thus the length is one greater than what strlen would
401 * return. This is important and lots of code elsewhere in the kernel
402 * assumes this behavior.
404 * This function can call vnop in file system if the parent vnode
405 * does not exist or when called for hardlinks via volfs path.
406 * If BUILDPATH_NO_FS_ENTER is set in flags, it only uses values present
407 * in the name cache and does not enter the file system.
409 * If BUILDPATH_CHECK_MOVED is set in flags, we return EAGAIN when
410 * we encounter ENOENT during path reconstruction. ENOENT means that
411 * one of the parents moved while we were building the path. The
412 * caller can special handle this case by calling build_path again.
414 * If BUILDPATH_VOLUME_RELATIVE is set in flags, we return path
415 * that is relative to the nearest mount point, i.e. do not
416 * cross over mount points during building the path.
418 * passed in vp must have a valid io_count reference
420 * If parent vnode is non-NULL it also must have an io count. This
421 * allows build_path_with_parent to be safely called for operations
422 * unlink, rmdir and rename that already have io counts on the target
423 * and the directory. In this way build_path_with_parent does not have
424 * to try and obtain an additional io count on the parent. Taking an
425 * io count ont the parent can lead to dead lock if a forced unmount
426 * occures at the right moment. For a fuller explaination on how this
427 * can occur see the comment for vn_getpath_with_parent.
431 build_path_with_parent(vnode_t first_vp
, vnode_t parent_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
434 vnode_t vp_with_iocount
;
435 vnode_t proc_root_dir_vp
;
442 if (first_vp
== NULLVP
) {
451 * Grab the process fd so we can evaluate fd_rdir.
453 if (vfs_context_proc(ctx
)->p_fd
) {
454 proc_root_dir_vp
= vfs_context_proc(ctx
)->p_fd
->fd_rdir
;
456 proc_root_dir_vp
= NULL
;
459 vp_with_iocount
= NULLVP
;
463 end
= &buff
[buflen
- 1];
467 * holding the NAME_CACHE_LOCK in shared mode is
468 * sufficient to stabilize both the vp->v_parent chain
469 * and the 'vp->v_mount->mnt_vnodecovered' chain
471 * if we need to drop this lock, we must first grab the v_id
472 * from the vnode we're currently working with... if that
473 * vnode doesn't already have an io_count reference (the vp
474 * passed in comes with one), we must grab a reference
475 * after we drop the NAME_CACHE_LOCK via vnode_getwithvid...
476 * deadlocks may result if you call vnode_get while holding
477 * the NAME_CACHE_LOCK... we lazily release the reference
478 * we pick up the next time we encounter a need to drop
479 * the NAME_CACHE_LOCK or before we return from this routine
481 NAME_CACHE_LOCK_SHARED();
484 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
485 (vp
->v_flag
& VFMLINKTARGET
) && vp
->v_fmlink
) {
491 * Check if this is the root of a file system.
493 while (vp
&& vp
->v_flag
& VROOT
) {
494 if (vp
->v_mount
== NULL
) {
498 if ((vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) || (vp
== proc_root_dir_vp
)) {
500 * It's the root of the root file system, so it's
508 * This the root of the volume and the caller does not
509 * want to cross mount points. Therefore just return
510 * '/' as the relative path.
513 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
514 (vp
->v_flag
& VFMLINKTARGET
) && vp
->v_fmlink
) {
518 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
522 vp
= vp
->v_mount
->mnt_vnodecovered
;
527 while ((vp
!= NULLVP
) && (vp
->v_parent
!= vp
)) {
531 * For hardlinks the v_name may be stale, so if its OK
532 * to enter a file system, ask the file system for the
533 * name and parent (below).
535 fixhardlink
= (vp
->v_flag
& VISHARDLINK
) &&
536 (vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
) &&
537 !(flags
& BUILDPATH_NO_FS_ENTER
);
542 if (str
== NULL
|| *str
== '\0') {
543 if (vp
->v_parent
!= NULL
) {
552 * Check that there's enough space (including space for the '/')
554 if ((end
- buff
) < (len
+ 1)) {
559 * Copy the name backwards.
563 for (; len
> 0; len
--) {
567 * Add a path separator.
573 * Walk up the parent chain.
575 if (((vp
->v_parent
!= NULLVP
) && !fixhardlink
) ||
576 (flags
& BUILDPATH_NO_FS_ENTER
)) {
578 * In this if () block we are not allowed to enter the filesystem
579 * to conclusively get the most accurate parent identifier.
580 * As a result, if 'vp' does not identify '/' and it
581 * does not have a valid v_parent, then error out
582 * and disallow further path construction
584 if ((vp
->v_parent
== NULLVP
) && (rootvnode
!= vp
)) {
586 * Only '/' is allowed to have a NULL parent
587 * pointer. Upper level callers should ideally
588 * re-drive name lookup on receiving a ENOENT.
592 /* The code below will exit early if 'tvp = vp' == NULL */
597 * if the vnode we have in hand isn't a directory and it
598 * has a v_parent, then we started with the resource fork
599 * so skip up to avoid getting a duplicate copy of the
600 * file name in the path.
602 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
607 * No parent, go get it if supported.
609 struct vnode_attr va
;
613 * Make sure file system supports obtaining a path from id.
615 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_PATH_FROM_ID
)) {
623 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
624 if (vp_with_iocount
) {
625 vnode_put(vp_with_iocount
);
626 vp_with_iocount
= NULLVP
;
628 if (vnode_getwithvid(vp
, vid
)) {
631 vp_with_iocount
= vp
;
634 VATTR_WANTED(&va
, va_parentid
);
637 VATTR_WANTED(&va
, va_name
);
638 MALLOC_ZONE(va
.va_name
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
643 * Ask the file system for its parent id and for its name (optional).
645 ret
= vnode_getattr(vp
, &va
, ctx
);
648 if ((ret
== 0) && (VATTR_IS_SUPPORTED(&va
, va_name
))) {
650 vnode_update_identity(vp
, NULL
, str
, strlen(str
), 0, VNODE_UPDATE_NAME
);
651 } else if (vp
->v_name
) {
661 * Check that there's enough space.
663 if ((end
- buff
) < (len
+ 1)) {
666 /* Copy the name backwards. */
669 for (; len
> 0; len
--) {
673 * Add a path separator.
678 FREE_ZONE(va
.va_name
, MAXPATHLEN
, M_NAMEI
);
680 if (ret
|| !VATTR_IS_SUPPORTED(&va
, va_parentid
)) {
685 * Ask the file system for the parent vnode.
687 if ((ret
= VFS_VGET(vp
->v_mount
, (ino64_t
)va
.va_parentid
, &dvp
, ctx
))) {
691 if (!fixhardlink
&& (vp
->v_parent
!= dvp
)) {
692 vnode_update_identity(vp
, dvp
, NULL
, 0, 0, VNODE_UPDATE_PARENT
);
695 if (vp_with_iocount
) {
696 vnode_put(vp_with_iocount
);
699 vp_with_iocount
= vp
;
701 NAME_CACHE_LOCK_SHARED();
704 * if the vnode we have in hand isn't a directory and it
705 * has a v_parent, then we started with the resource fork
706 * so skip up to avoid getting a duplicate copy of the
707 * file name in the path.
709 if (vp
&& !vnode_isdir(vp
) && vp
->v_parent
) {
714 if (vp
&& (flags
& BUILDPATH_CHECKACCESS
)) {
719 if (vp
!= first_vp
&& vp
!= parent_vp
&& vp
!= vp_with_iocount
) {
720 if (vp_with_iocount
) {
721 vnode_put(vp_with_iocount
);
722 vp_with_iocount
= NULLVP
;
724 if (vnode_getwithvid(vp
, vid
)) {
727 vp_with_iocount
= vp
;
729 if ((ret
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
))) {
730 goto out
; /* no peeking */
732 NAME_CACHE_LOCK_SHARED();
736 * When a mount point is crossed switch the vp.
737 * Continue until we find the root or we find
738 * a vnode that's not the root of a mounted
744 if (tvp
== proc_root_dir_vp
) {
745 goto out_unlock
; /* encountered the root */
749 if (!(flags
& BUILDPATH_NO_FIRMLINK
) &&
750 (tvp
->v_flag
& VFMLINKTARGET
) && tvp
->v_fmlink
) {
756 if (!(tvp
->v_flag
& VROOT
) || !tvp
->v_mount
) {
757 break; /* not the root of a mounted FS */
759 if (flags
& BUILDPATH_VOLUME_RELATIVE
) {
760 /* Do not cross over mount points */
763 tvp
= tvp
->v_mount
->mnt_vnodecovered
;
774 if (vp_with_iocount
) {
775 vnode_put(vp_with_iocount
);
778 * Slide the name down to the beginning of the buffer.
780 memmove(buff
, end
, &buff
[buflen
] - end
);
783 * length includes the trailing zero byte
785 *outlen
= &buff
[buflen
] - end
;
787 /* One of the parents was moved during path reconstruction.
788 * The caller is interested in knowing whether any of the
789 * parents moved via BUILDPATH_CHECK_MOVED, so return EAGAIN.
791 if ((ret
== ENOENT
) && (flags
& BUILDPATH_CHECK_MOVED
)) {
799 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
, int flags
, vfs_context_t ctx
)
801 return build_path_with_parent(first_vp
, NULL
, buff
, buflen
, outlen
, flags
, ctx
);
805 * return NULLVP if vp's parent doesn't
806 * exist, or we can't get a valid iocount
807 * else return the parent of vp
810 vnode_getparent(vnode_t vp
)
812 vnode_t pvp
= NULLVP
;
815 NAME_CACHE_LOCK_SHARED();
820 * v_parent is stable behind the name_cache lock
821 * however, the only thing we can really guarantee
822 * is that we've grabbed a valid iocount on the
823 * parent of 'vp' at the time we took the name_cache lock...
824 * once we drop the lock, vp could get re-parented
831 if (vnode_getwithvid(pvp
, pvid
) != 0) {
841 vnode_getname(vnode_t vp
)
843 const char *name
= NULL
;
845 NAME_CACHE_LOCK_SHARED();
848 name
= vfs_addname(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
856 vnode_putname(const char *name
)
858 vfs_removename(name
);
861 static const char unknown_vnodename
[] = "(unknown vnode name)";
864 vnode_getname_printable(vnode_t vp
)
866 const char *name
= vnode_getname(vp
);
871 switch (vp
->v_type
) {
876 * Create an artificial dev name from
877 * major and minor device number
880 (void) snprintf(dev_name
, sizeof(dev_name
),
881 "%c(%u, %u)", VCHR
== vp
->v_type
? 'c':'b',
882 major(vp
->v_rdev
), minor(vp
->v_rdev
));
884 * Add the newly created dev name to the name
885 * cache to allow easier cleanup. Also,
886 * vfs_addname allocates memory for the new name
889 NAME_CACHE_LOCK_SHARED();
890 name
= vfs_addname(dev_name
, strlen(dev_name
), 0, 0);
895 return unknown_vnodename
;
900 vnode_putname_printable(const char *name
)
902 if (name
== unknown_vnodename
) {
910 * if VNODE_UPDATE_PARENT, and we can take
911 * a reference on dvp, then update vp with
912 * it's new parent... if vp already has a parent,
913 * then drop the reference vp held on it
915 * if VNODE_UPDATE_NAME,
916 * then drop string ref on v_name if it exists, and if name is non-NULL
917 * then pick up a string reference on name and record it in v_name...
918 * optionally pass in the length and hashval of name if known
920 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
923 vnode_update_identity(vnode_t vp
, vnode_t dvp
, const char *name
, int name_len
, uint32_t name_hashval
, int flags
)
925 struct namecache
*ncp
;
926 vnode_t old_parentvp
= NULLVP
;
927 int isstream
= (vp
->v_flag
& VISNAMEDSTREAM
);
928 int kusecountbumped
= 0;
929 kauth_cred_t tcred
= NULL
;
930 const char *vname
= NULL
;
931 const char *tname
= NULL
;
933 if (flags
& VNODE_UPDATE_PARENT
) {
934 if (dvp
&& vnode_ref(dvp
) != 0) {
937 /* Don't count a stream's parent ref during unmounts */
938 if (isstream
&& dvp
&& (dvp
!= vp
) && (dvp
!= vp
->v_parent
) && (dvp
->v_type
== VREG
)) {
939 vnode_lock_spin(dvp
);
947 if ((flags
& VNODE_UPDATE_NAME
)) {
948 if (name
!= vp
->v_name
) {
951 name_len
= strlen(name
);
953 tname
= vfs_addname(name
, name_len
, name_hashval
, 0);
956 flags
&= ~VNODE_UPDATE_NAME
;
959 if ((flags
& (VNODE_UPDATE_PURGE
| VNODE_UPDATE_PARENT
| VNODE_UPDATE_CACHE
| VNODE_UPDATE_NAME
| VNODE_UPDATE_PURGEFIRMLINK
))) {
963 if (flags
& VNODE_UPDATE_PURGEFIRMLINK
) {
964 vnode_t old_fvp
= vp
->v_fmlink
;
967 vp
->v_flag
&= ~VFMLINKTARGET
;
968 vp
->v_fmlink
= NULLVP
;
973 * vnode_rele can result in cascading series of
974 * usecount releases. The combination of calling
975 * vnode_recycle and dont_reenter (3rd arg to
976 * vnode_rele_internal) ensures we don't have
979 vnode_recycle(old_fvp
);
980 vnode_rele_internal(old_fvp
, O_EVTONLY
, 1, 0);
987 if ((flags
& VNODE_UPDATE_PURGE
)) {
989 vp
->v_parent
->v_nc_generation
++;
992 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
993 cache_delete(ncp
, 1);
996 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
997 cache_delete(ncp
, 1);
1001 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1004 vp
->v_cred
= NOCRED
;
1005 vp
->v_authorized_actions
= 0;
1006 vp
->v_cred_timestamp
= 0;
1008 if ((flags
& VNODE_UPDATE_NAME
)) {
1012 if (flags
& VNODE_UPDATE_PARENT
) {
1013 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
1014 old_parentvp
= vp
->v_parent
;
1019 flags
|= VNODE_UPDATE_CACHE
;
1023 if (flags
& VNODE_UPDATE_CACHE
) {
1024 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
1025 cache_delete(ncp
, 1);
1028 NAME_CACHE_UNLOCK();
1030 if (vname
!= NULL
) {
1031 vfs_removename(vname
);
1034 if (IS_VALID_CRED(tcred
)) {
1035 kauth_cred_unref(&tcred
);
1038 if (dvp
!= NULLVP
) {
1039 /* Back-out the ref we took if we lost a race for vp->v_parent. */
1040 if (kusecountbumped
) {
1041 vnode_lock_spin(dvp
);
1042 if (dvp
->v_kusecount
> 0) {
1053 vnode_lock_spin(old_parentvp
);
1054 if ((old_parentvp
->v_type
!= VDIR
) && (old_parentvp
->v_kusecount
> 0)) {
1055 --old_parentvp
->v_kusecount
;
1057 vnode_unlock(old_parentvp
);
1059 ut
= get_bsdthread_info(current_thread());
1062 * indicated to vnode_rele that it shouldn't do a
1063 * vnode_reclaim at this time... instead it will
1064 * chain the vnode to the uu_vreclaims list...
1065 * we'll be responsible for calling vnode_reclaim
1066 * on each of the vnodes in this list...
1068 ut
->uu_defer_reclaims
= 1;
1069 ut
->uu_vreclaims
= NULLVP
;
1071 while ((vp
= old_parentvp
) != NULLVP
) {
1072 vnode_lock_spin(vp
);
1073 vnode_rele_internal(vp
, 0, 0, 1);
1076 * check to see if the vnode is now in the state
1077 * that would have triggered a vnode_reclaim in vnode_rele
1078 * if it is, we save it's parent pointer and then NULL
1079 * out the v_parent field... we'll drop the reference
1080 * that was held on the next iteration of this loop...
1081 * this short circuits a potential deep recursion if we
1082 * have a long chain of parents in this state...
1083 * we'll sit in this loop until we run into
1084 * a parent in this chain that is not in this state
1086 * make our check and the vnode_rele atomic
1087 * with respect to the current vnode we're working on
1088 * by holding the vnode lock
1089 * if vnode_rele deferred the vnode_reclaim and has put
1090 * this vnode on the list to be reaped by us, than
1091 * it has left this vnode with an iocount == 1
1093 if ((vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
1094 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
1096 * vnode_rele wanted to do a vnode_reclaim on this vnode
1097 * it should be sitting on the head of the uu_vreclaims chain
1098 * pull the parent pointer now so that when we do the
1099 * vnode_reclaim for each of the vnodes in the uu_vreclaims
1100 * list, we won't recurse back through here
1102 * need to do a convert here in case vnode_rele_internal
1103 * returns with the lock held in the spin mode... it
1104 * can drop and retake the lock under certain circumstances
1106 vnode_lock_convert(vp
);
1109 old_parentvp
= vp
->v_parent
;
1110 vp
->v_parent
= NULLVP
;
1111 NAME_CACHE_UNLOCK();
1114 * we're done... we ran into a vnode that isn't
1117 old_parentvp
= NULLVP
;
1121 ut
->uu_defer_reclaims
= 0;
1123 while ((vp
= ut
->uu_vreclaims
) != NULLVP
) {
1124 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
1127 * vnode_put will drive the vnode_reclaim if
1128 * we are still the only reference on this vnode
1135 #if CONFIG_FIRMLINKS
1137 vnode_setasfirmlink(vnode_t vp
, vnode_t target_vp
)
1140 vnode_t old_target_vp
= NULLVP
;
1141 vnode_t old_target_vp_v_fmlink
= NULLVP
;
1142 kauth_cred_t target_vp_cred
= NULL
;
1143 kauth_cred_t old_target_vp_cred
= NULL
;
1150 if (vp
->v_fmlink
== target_vp
) { /* Will be checked again under the name cache lock */
1155 * Firmlink source and target will take both a usecount
1156 * and kusecount on each other.
1158 if ((error
= vnode_ref_ext(target_vp
, O_EVTONLY
, VNODE_REF_FORCE
))) {
1162 if ((error
= vnode_ref_ext(vp
, O_EVTONLY
, VNODE_REF_FORCE
))) {
1163 vnode_rele_ext(target_vp
, O_EVTONLY
, 1);
1170 old_target_vp
= vp
->v_fmlink
;
1171 if (target_vp
&& (target_vp
== old_target_vp
)) {
1172 NAME_CACHE_UNLOCK();
1175 vp
->v_fmlink
= target_vp
;
1177 vnode_lock_spin(vp
);
1178 vp
->v_flag
&= ~VFMLINKTARGET
;
1182 target_vp
->v_fmlink
= vp
;
1183 vnode_lock_spin(target_vp
);
1184 target_vp
->v_flag
|= VFMLINKTARGET
;
1185 vnode_unlock(target_vp
);
1186 cache_purge_locked(vp
, &target_vp_cred
);
1189 if (old_target_vp
) {
1190 old_target_vp_v_fmlink
= old_target_vp
->v_fmlink
;
1191 old_target_vp
->v_fmlink
= NULLVP
;
1192 vnode_lock_spin(old_target_vp
);
1193 old_target_vp
->v_flag
&= ~VFMLINKTARGET
;
1194 vnode_unlock(old_target_vp
);
1195 cache_purge_locked(vp
, &old_target_vp_cred
);
1198 NAME_CACHE_UNLOCK();
1200 if (target_vp_cred
&& IS_VALID_CRED(target_vp_cred
)) {
1201 kauth_cred_unref(&target_vp_cred
);
1204 if (old_target_vp
) {
1205 if (old_target_vp_cred
&& IS_VALID_CRED(old_target_vp_cred
)) {
1206 kauth_cred_unref(&old_target_vp_cred
);
1209 vnode_rele_ext(old_target_vp
, O_EVTONLY
, 1);
1210 if (old_target_vp_v_fmlink
) {
1211 vnode_rele_ext(old_target_vp_v_fmlink
, O_EVTONLY
, 1);
1219 vnode_getfirmlink(vnode_t vp
, vnode_t
*target_vp
)
1223 if (!vp
->v_fmlink
) {
1227 NAME_CACHE_LOCK_SHARED();
1228 if (vp
->v_fmlink
&& !(vp
->v_flag
& VFMLINKTARGET
) &&
1229 (vnode_get(vp
->v_fmlink
) == 0)) {
1230 vnode_t tvp
= vp
->v_fmlink
;
1232 vnode_lock_spin(tvp
);
1233 if (tvp
->v_lflag
& (VL_TERMINATE
| VL_DEAD
)) {
1235 NAME_CACHE_UNLOCK();
1239 if (!(tvp
->v_flag
& VFMLINKTARGET
)) {
1240 panic("firmlink target for vnode %p does not have flag set", vp
);
1246 *target_vp
= NULLVP
;
1249 NAME_CACHE_UNLOCK();
1253 #else /* CONFIG_FIRMLINKS */
1256 vnode_setasfirmlink(__unused vnode_t vp
, __unused vnode_t src_vp
)
1262 vnode_getfirmlink(__unused vnode_t vp
, __unused vnode_t
*target_vp
)
1270 * Mark a vnode as having multiple hard links. HFS makes use of this
1271 * because it keeps track of each link separately, and wants to know
1272 * which link was actually used.
1274 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
1275 * so that HFS can post-process the lookup. Also, volfs will call
1276 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
1279 vnode_setmultipath(vnode_t vp
)
1281 vnode_lock_spin(vp
);
1284 * In theory, we're changing the vnode's identity as far as the
1285 * name cache is concerned, so we ought to grab the name cache lock
1286 * here. However, there is already a race, and grabbing the name
1287 * cache lock only makes the race window slightly smaller.
1289 * The race happens because the vnode already exists in the name
1290 * cache, and could be found by one thread before another thread
1291 * can set the hard link flag.
1294 vp
->v_flag
|= VISHARDLINK
;
1302 * backwards compatibility
1305 vnode_uncache_credentials(vnode_t vp
)
1307 vnode_uncache_authorized_action(vp
, KAUTH_INVALIDATE_CACHED_RIGHTS
);
1312 * use the exclusive form of NAME_CACHE_LOCK to protect the update of the
1313 * following fields in the vnode: v_cred_timestamp, v_cred, v_authorized_actions
1314 * we use this lock so that we can look at the v_cred and v_authorized_actions
1315 * atomically while behind the NAME_CACHE_LOCK in shared mode in 'cache_lookup_path',
1316 * which is the super-hot path... if we are updating the authorized actions for this
1317 * vnode, we are already in the super-slow and far less frequented path so its not
1318 * that bad that we take the lock exclusive for this case... of course we strive
1319 * to hold it for the minimum amount of time possible
1323 vnode_uncache_authorized_action(vnode_t vp
, kauth_action_t action
)
1325 kauth_cred_t tcred
= NOCRED
;
1329 vp
->v_authorized_actions
&= ~action
;
1331 if (action
== KAUTH_INVALIDATE_CACHED_RIGHTS
&&
1332 IS_VALID_CRED(vp
->v_cred
)) {
1334 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1337 vp
->v_cred
= NOCRED
;
1339 NAME_CACHE_UNLOCK();
1341 if (tcred
!= NOCRED
) {
1342 kauth_cred_unref(&tcred
);
1347 extern int bootarg_vnode_cache_defeat
; /* default = 0, from bsd_init.c */
1350 vnode_cache_is_authorized(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1353 boolean_t retval
= FALSE
;
1355 /* Boot argument to defeat rights caching */
1356 if (bootarg_vnode_cache_defeat
) {
1360 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1362 * a TTL is enabled on the rights cache... handle it here
1363 * a TTL of 0 indicates that no rights should be cached
1365 if (vp
->v_mount
->mnt_authcache_ttl
) {
1366 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1368 * For filesystems marked only MNTK_AUTH_OPAQUE (generally network ones),
1369 * we will only allow a SEARCH right on a directory to be cached...
1370 * that cached right always has a default TTL associated with it
1372 if (action
!= KAUTH_VNODE_SEARCH
|| vp
->v_type
!= VDIR
) {
1376 if (vp
!= NULLVP
&& vnode_cache_is_stale(vp
) == TRUE
) {
1377 vnode_uncache_authorized_action(vp
, vp
->v_authorized_actions
);
1385 ucred
= vfs_context_ucred(ctx
);
1387 NAME_CACHE_LOCK_SHARED();
1389 if (vp
->v_cred
== ucred
&& (vp
->v_authorized_actions
& action
) == action
) {
1393 NAME_CACHE_UNLOCK();
1400 vnode_cache_authorized_action(vnode_t vp
, vfs_context_t ctx
, kauth_action_t action
)
1402 kauth_cred_t tcred
= NOCRED
;
1405 boolean_t ttl_active
= FALSE
;
1407 ucred
= vfs_context_ucred(ctx
);
1409 if (!IS_VALID_CRED(ucred
) || action
== 0) {
1413 if ((vp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1415 * a TTL is enabled on the rights cache... handle it here
1416 * a TTL of 0 indicates that no rights should be cached
1418 if (vp
->v_mount
->mnt_authcache_ttl
== 0) {
1422 if (!(vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_CACHE_TTL
)) {
1424 * only cache SEARCH action for filesystems marked
1425 * MNTK_AUTH_OPAQUE on VDIRs...
1426 * the lookup_path code will time these out
1428 if ((action
& ~KAUTH_VNODE_SEARCH
) || vp
->v_type
!= VDIR
) {
1438 if (vp
->v_cred
!= ucred
) {
1439 kauth_cred_ref(ucred
);
1441 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
1445 vp
->v_authorized_actions
= 0;
1447 if (ttl_active
== TRUE
&& vp
->v_authorized_actions
== 0) {
1449 * only reset the timestamnp on the
1450 * first authorization cached after the previous
1451 * timer has expired or we're switching creds...
1452 * 'vnode_cache_is_authorized' will clear the
1453 * authorized actions if the TTL is active and
1456 vp
->v_cred_timestamp
= tv
.tv_sec
;
1458 vp
->v_authorized_actions
|= action
;
1460 NAME_CACHE_UNLOCK();
1462 if (IS_VALID_CRED(tcred
)) {
1463 kauth_cred_unref(&tcred
);
1469 vnode_cache_is_stale(vnode_t vp
)
1476 if ((tv
.tv_sec
- vp
->v_cred_timestamp
) > vp
->v_mount
->mnt_authcache_ttl
) {
1488 * Returns: 0 Success
1489 * ERECYCLE vnode was recycled from underneath us. Force lookup to be re-driven from namei.
1490 * This errno value should not be seen by anyone outside of the kernel.
1493 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
1494 vfs_context_t ctx
, int *dp_authorized
, vnode_t last_dp
)
1496 char *cp
; /* pointer into pathname argument */
1498 int vvid
= 0; /* protected by vp != NULLVP */
1499 vnode_t vp
= NULLVP
;
1500 vnode_t tdp
= NULLVP
;
1502 boolean_t ttl_enabled
= FALSE
;
1507 boolean_t dotdotchecked
= FALSE
;
1511 #endif /* CONFIG_TRIGGERS */
1513 ucred
= vfs_context_ucred(ctx
);
1514 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
1516 NAME_CACHE_LOCK_SHARED();
1518 if (dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& (MNTK_AUTH_OPAQUE
| MNTK_AUTH_CACHE_TTL
))) {
1524 * Search a directory.
1526 * The cn_hash value is for use by cache_lookup
1527 * The last component of the filename is left accessible via
1528 * cnp->cn_nameptr for callers that need the name.
1531 cp
= cnp
->cn_nameptr
;
1533 while (*cp
&& (*cp
!= '/')) {
1534 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1537 * the crc generator can legitimately generate
1538 * a 0... however, 0 for us means that we
1539 * haven't computed a hash, so use 1 instead
1544 cnp
->cn_hash
= hash
;
1545 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
1547 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
1551 * Replace multiple slashes by a single slash and trailing slashes
1552 * by a null. This must be done before VNOP_LOOKUP() because some
1553 * fs's don't know about trailing slashes. Remember if there were
1554 * trailing slashes to handle symlinks, existing non-directories
1555 * and non-existing files that won't be directories specially later.
1557 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
1562 ndp
->ni_flag
|= NAMEI_TRAILINGSLASH
;
1563 *ndp
->ni_next
= '\0';
1568 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
1571 cnp
->cn_flags
|= ISLASTCN
;
1574 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.') {
1575 cnp
->cn_flags
|= ISDOTDOT
;
1581 * Process a request for a file's resource fork.
1583 * Consume the _PATH_RSRCFORKSPEC suffix and tag the path.
1585 if ((ndp
->ni_pathlen
== sizeof(_PATH_RSRCFORKSPEC
)) &&
1586 (cp
[1] == '.' && cp
[2] == '.') &&
1587 bcmp(cp
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
1588 /* Skip volfs file systems that don't support native streams. */
1589 if ((dp
->v_mount
!= NULL
) &&
1590 (dp
->v_mount
->mnt_flag
& MNT_DOVOLFS
) &&
1591 (dp
->v_mount
->mnt_kern_flag
& MNTK_NAMED_STREAMS
) == 0) {
1594 cnp
->cn_flags
|= CN_WANTSRSRCFORK
;
1595 cnp
->cn_flags
|= ISLASTCN
;
1596 ndp
->ni_next
[0] = '\0';
1597 ndp
->ni_pathlen
= 1;
1605 * Name cache provides authorization caching (see below)
1606 * that will short circuit MAC checks in lookup().
1607 * We must perform MAC check here. On denial
1608 * dp_authorized will remain 0 and second check will
1609 * be perfomed in lookup().
1611 if (!(cnp
->cn_flags
& DONOTAUTH
)) {
1612 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
1614 NAME_CACHE_UNLOCK();
1620 (dp
->v_mount
->mnt_authcache_ttl
== 0 ||
1621 ((tv
.tv_sec
- dp
->v_cred_timestamp
) > dp
->v_mount
->mnt_authcache_ttl
))) {
1626 * NAME_CACHE_LOCK holds these fields stable
1628 * We can't cache KAUTH_VNODE_SEARCHBYANYONE for root correctly
1629 * so we make an ugly check for root here. root is always
1630 * allowed and breaking out of here only to find out that is
1631 * authorized by virtue of being root is very very expensive.
1632 * However, the check for not root is valid only for filesystems
1633 * which use local authorization.
1635 * XXX: Remove the check for root when we can reliably set
1636 * KAUTH_VNODE_SEARCHBYANYONE as root.
1638 if ((dp
->v_cred
!= ucred
|| !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCH
)) &&
1639 !(dp
->v_authorized_actions
& KAUTH_VNODE_SEARCHBYANYONE
) &&
1640 (ttl_enabled
|| !vfs_context_issuser(ctx
))) {
1645 * indicate that we're allowed to traverse this directory...
1646 * even if we fail the cache lookup or decide to bail for
1647 * some other reason, this information is valid and is used
1648 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
1652 if ((cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
))) {
1653 if (cnp
->cn_nameiop
!= LOOKUP
) {
1656 if (cnp
->cn_flags
& LOCKPARENT
) {
1659 if (cnp
->cn_flags
& NOCACHE
) {
1662 if (cnp
->cn_flags
& ISDOTDOT
) {
1663 #if CONFIG_FIRMLINKS
1664 if (dp
->v_fmlink
&& (dp
->v_flag
& VFMLINKTARGET
)) {
1670 * Force directory hardlinks to go to
1671 * file system for ".." requests.
1673 if ((dp
->v_flag
& VISHARDLINK
)) {
1677 * Quit here only if we can't use
1678 * the parent directory pointer or
1679 * don't have one. Otherwise, we'll
1682 if ((dp
->v_flag
& VROOT
) ||
1683 dp
== ndp
->ni_rootdir
||
1684 dp
->v_parent
== NULLVP
) {
1690 if ((cnp
->cn_flags
& CN_SKIPNAMECACHE
)) {
1692 * Force lookup to go to the filesystem with
1693 * all cnp fields set up.
1699 * "." and ".." aren't supposed to be cached, so check
1700 * for them before checking the cache.
1702 if (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') {
1704 } else if ((cnp
->cn_flags
& ISDOTDOT
)) {
1706 * If this is a chrooted process, we need to check if
1707 * the process is trying to break out of its chrooted
1708 * jail. We do that by trying to determine if dp is
1709 * a subdirectory of ndp->ni_rootdir. If we aren't
1710 * able to determine that by the v_parent pointers, we
1711 * will leave the fast path.
1713 * Since this function may see dotdot components
1714 * many times and it has the name cache lock held for
1715 * the entire duration, we optimise this by doing this
1716 * check only once per cache_lookup_path call.
1717 * If dotdotchecked is set, it means we've done this
1718 * check once already and don't need to do it again.
1720 if (!dotdotchecked
&& (ndp
->ni_rootdir
!= rootvnode
)) {
1722 boolean_t defer
= FALSE
;
1723 boolean_t is_subdir
= FALSE
;
1725 defer
= cache_check_vnode_issubdir(tvp
,
1726 ndp
->ni_rootdir
, &is_subdir
, &tvp
);
1729 /* defer to Filesystem */
1731 } else if (!is_subdir
) {
1733 * This process is trying to break out
1734 * of its chrooted jail, so all its
1735 * dotdot accesses will be translated to
1736 * its root directory.
1738 vp
= ndp
->ni_rootdir
;
1741 * All good, let this dotdot access
1746 dotdotchecked
= TRUE
;
1751 if ((vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
) {
1755 if ((vp
->v_flag
& VISHARDLINK
)) {
1757 * The file system wants a VNOP_LOOKUP on this vnode
1763 if ((cnp
->cn_flags
& ISLASTCN
)) {
1767 if (vp
->v_type
!= VDIR
) {
1768 if (vp
->v_type
!= VLNK
) {
1774 if ((mp
= vp
->v_mountedhere
) && ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1775 vnode_t tmp_vp
= mp
->mnt_realrootvp
;
1776 if (tmp_vp
== NULLVP
|| mp
->mnt_generation
!= mount_generation
||
1777 mp
->mnt_realrootvp_vid
!= tmp_vp
->v_id
) {
1785 * After traversing all mountpoints stacked here, if we have a
1786 * trigger in hand, resolve it. Note that we don't need to
1787 * leave the fast path if the mount has already happened.
1789 if (vp
->v_resolve
) {
1792 #endif /* CONFIG_TRIGGERS */
1798 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
1800 while (*cnp
->cn_nameptr
== '/') {
1810 NAME_CACHE_UNLOCK();
1812 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
1813 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
1815 * if we've got a child and it's the last component, and
1816 * the lookup doesn't need to return the parent then we
1817 * can skip grabbing an iocount on the parent, since all
1818 * we're going to do with it is a vnode_put just before
1819 * we return from 'lookup'. If it's a symbolic link,
1820 * we need the parent in case the link happens to be
1821 * a relative pathname.
1828 * return the last directory we looked at
1829 * with an io reference held. If it was the one passed
1830 * in as a result of the last iteration of VNOP_LOOKUP,
1831 * it should already hold an io ref. No need to increase ref.
1833 if (last_dp
!= dp
) {
1834 if (dp
== ndp
->ni_usedvp
) {
1836 * if this vnode matches the one passed in via USEDVP
1837 * than this context already holds an io_count... just
1838 * use vnode_get to get an extra ref for lookup to play
1839 * with... can't use the getwithvid variant here because
1840 * it will block behind a vnode_drain which would result
1841 * in a deadlock (since we already own an io_count that the
1842 * vnode_drain is waiting on)... vnode_get grabs the io_count
1843 * immediately w/o waiting... it always succeeds
1846 } else if ((error
= vnode_getwithvid_drainok(dp
, vid
))) {
1848 * failure indicates the vnode
1849 * changed identity or is being
1850 * TERMINATED... in either case
1853 * don't necessarily return ENOENT, though, because
1854 * we really want to go back to disk and make sure it's
1855 * there or not if someone else is changing this
1856 * vnode. That being said, the one case where we do want
1857 * to return ENOENT is when the vnode's mount point is
1858 * in the process of unmounting and we might cause a deadlock
1859 * in our attempt to take an iocount. An ENODEV error return
1860 * is from vnode_get* is an indication this but we change that
1861 * ENOENT for upper layers.
1863 if (error
== ENODEV
) {
1873 if ((vnode_getwithvid_drainok(vp
, vvid
))) {
1877 * can't get reference on the vp we'd like
1878 * to return... if we didn't grab a reference
1879 * on the directory (due to fast path bypass),
1880 * then we need to do it now... we can't return
1881 * with both ni_dvp and ni_vp NULL, and no
1895 trigger_vp
= vp
? vp
: dp
;
1896 if ((error
== 0) && (trigger_vp
!= NULLVP
) && vnode_isdir(trigger_vp
)) {
1897 error
= vnode_trigger_resolve(trigger_vp
, ndp
, ctx
);
1908 #endif /* CONFIG_TRIGGERS */
1912 * If we came into cache_lookup_path after an iteration of the lookup loop that
1913 * resulted in a call to VNOP_LOOKUP, then VNOP_LOOKUP returned a vnode with a io ref
1914 * on it. It is now the job of cache_lookup_path to drop the ref on this vnode
1915 * when it is no longer needed. If we get to this point, and last_dp is not NULL
1916 * and it is ALSO not the dvp we want to return to caller of this function, it MUST be
1917 * the case that we got to a subsequent path component and this previous vnode is
1918 * no longer needed. We can then drop the io ref on it.
1920 if ((last_dp
!= NULLVP
) && (last_dp
!= ndp
->ni_dvp
)) {
1924 //initialized to 0, should be the same if no error cases occurred.
1930 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
1932 struct namecache
*ncp
;
1933 struct nchashhead
*ncpp
;
1934 long namelen
= cnp
->cn_namelen
;
1935 unsigned int hashval
= cnp
->cn_hash
;
1941 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
1942 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
1943 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
1944 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
1951 * We failed to find an entry
1956 NCHSTAT(ncs_goodhits
);
1962 unsigned int hash_string(const char *cp
, int len
);
1964 // Have to take a len argument because we may only need to
1965 // hash part of a componentname.
1968 hash_string(const char *cp
, int len
)
1974 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1977 while (*cp
!= '\0') {
1978 hash
= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)] ^ hash
<< 8;
1982 * the crc generator can legitimately generate
1983 * a 0... however, 0 for us means that we
1984 * haven't computed a hash, so use 1 instead
1994 * Lookup an entry in the cache
1996 * We don't do this if the segment name is long, simply so the cache
1997 * can avoid holding long names (which would either waste space, or
1998 * add greatly to the complexity).
2000 * Lookup is called with dvp pointing to the directory to search,
2001 * cnp pointing to the name of the entry being sought. If the lookup
2002 * succeeds, the vnode is returned in *vpp, and a status of -1 is
2003 * returned. If the lookup determines that the name does not exist
2004 * (negative cacheing), a status of ENOENT is returned. If the lookup
2005 * fails, a status of zero is returned.
2009 cache_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
2011 struct namecache
*ncp
;
2012 struct nchashhead
*ncpp
;
2013 long namelen
= cnp
->cn_namelen
;
2014 unsigned int hashval
;
2015 boolean_t have_exclusive
= FALSE
;
2019 if (cnp
->cn_hash
== 0) {
2020 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2022 hashval
= cnp
->cn_hash
;
2028 NAME_CACHE_LOCK_SHARED();
2031 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
2032 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
2033 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
2034 if (strncmp(ncp
->nc_name
, cnp
->cn_nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0) {
2039 /* We failed to find an entry */
2042 NAME_CACHE_UNLOCK();
2046 /* We don't want to have an entry, so dump it */
2047 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
2048 if (have_exclusive
== TRUE
) {
2049 NCHSTAT(ncs_badhits
);
2050 cache_delete(ncp
, 1);
2051 NAME_CACHE_UNLOCK();
2054 NAME_CACHE_UNLOCK();
2056 have_exclusive
= TRUE
;
2061 /* We found a "positive" match, return the vnode */
2063 NCHSTAT(ncs_goodhits
);
2066 NAME_CACHE_UNLOCK();
2068 if (vnode_getwithvid(vp
, vid
)) {
2071 NCHSTAT(ncs_badvid
);
2072 NAME_CACHE_UNLOCK();
2080 /* We found a negative match, and want to create it, so purge */
2081 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
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 "negative" match, ENOENT notifies client of this match.
2097 NCHSTAT(ncs_neghits
);
2099 NAME_CACHE_UNLOCK();
2104 cache_enter_create(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
)
2106 const char *strname
;
2108 if (cnp
->cn_hash
== 0) {
2109 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2113 * grab 2 references on the string entered
2114 * one for the cache_enter_locked to consume
2115 * and the second to be consumed by v_name (vnode_create call point)
2117 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, TRUE
, 0);
2121 cache_enter_locked(dvp
, vp
, cnp
, strname
);
2123 NAME_CACHE_UNLOCK();
2130 * Add an entry to the cache...
2131 * but first check to see if the directory
2132 * that this entry is to be associated with has
2133 * had any cache_purges applied since we took
2134 * our identity snapshot... this check needs to
2135 * be done behind the name cache lock
2138 cache_enter_with_gen(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int gen
)
2140 if (cnp
->cn_hash
== 0) {
2141 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2146 if (dvp
->v_nc_generation
== gen
) {
2147 (void)cache_enter_locked(dvp
, vp
, cnp
, NULL
);
2150 NAME_CACHE_UNLOCK();
2155 * Add an entry to the cache.
2158 cache_enter(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
)
2160 const char *strname
;
2162 if (cnp
->cn_hash
== 0) {
2163 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
2167 * grab 1 reference on the string entered
2168 * for the cache_enter_locked to consume
2170 strname
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
2174 cache_enter_locked(dvp
, vp
, cnp
, strname
);
2176 NAME_CACHE_UNLOCK();
2181 cache_enter_locked(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, const char *strname
)
2183 struct namecache
*ncp
, *negp
;
2184 struct nchashhead
*ncpp
;
2191 * if the entry is for -ve caching vp is null
2193 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
2195 * someone beat us to the punch..
2196 * this vnode is already in the cache
2198 if (strname
!= NULL
) {
2199 vfs_removename(strname
);
2204 * We allocate a new entry if we are less than the maximum
2205 * allowed and the one at the front of the list is in use.
2206 * Otherwise we use the one at the front of the list.
2208 if (numcache
< desiredNodes
&&
2209 ((ncp
= nchead
.tqh_first
) == NULL
||
2210 ncp
->nc_hash
.le_prev
!= 0)) {
2212 * Allocate one more entry
2214 ncp
= (struct namecache
*)_MALLOC_ZONE(sizeof(*ncp
), M_CACHE
, M_WAITOK
);
2218 * reuse an old entry
2220 ncp
= TAILQ_FIRST(&nchead
);
2221 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2223 if (ncp
->nc_hash
.le_prev
!= 0) {
2225 * still in use... we need to
2226 * delete it before re-using it
2228 NCHSTAT(ncs_stolen
);
2229 cache_delete(ncp
, 0);
2232 NCHSTAT(ncs_enters
);
2235 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
2239 ncp
->nc_hashval
= cnp
->cn_hash
;
2241 if (strname
== NULL
) {
2242 ncp
->nc_name
= add_name_internal(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, FALSE
, 0);
2244 ncp
->nc_name
= strname
;
2248 // If the bytes of the name associated with the vnode differ,
2249 // use the name associated with the vnode since the file system
2250 // may have set that explicitly in the case of a lookup on a
2251 // case-insensitive file system where the case of the looked up
2252 // name differs from what is on disk. For more details, see:
2253 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
2255 const char *vn_name
= vp
? vp
->v_name
: NULL
;
2256 unsigned int len
= vn_name
? strlen(vn_name
) : 0;
2257 if (vn_name
&& ncp
&& ncp
->nc_name
&& strncmp(ncp
->nc_name
, vn_name
, len
) != 0) {
2258 unsigned int hash
= hash_string(vn_name
, len
);
2260 vfs_removename(ncp
->nc_name
);
2261 ncp
->nc_name
= add_name_internal(vn_name
, len
, hash
, FALSE
, 0);
2262 ncp
->nc_hashval
= hash
;
2266 * make us the newest entry in the cache
2267 * i.e. we'll be the last to be stolen
2269 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
2271 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
2274 struct namecache
*p
;
2276 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
) {
2278 panic("cache_enter: duplicate");
2284 * make us available to be found via lookup
2286 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
2290 * add to the list of name cache entries
2293 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
2296 * this is a negative cache entry (vp == NULL)
2297 * stick it on the negative cache list.
2299 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
2303 if (ncs_negtotal
> desiredNegNodes
) {
2305 * if we've reached our desired limit
2306 * of negative cache entries, delete
2309 negp
= TAILQ_FIRST(&neghead
);
2310 cache_delete(negp
, 1);
2314 * add us to the list of name cache entries that
2315 * are children of dvp
2318 TAILQ_INSERT_TAIL(&dvp
->v_ncchildren
, ncp
, nc_child
);
2320 TAILQ_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
2326 * Initialize CRC-32 remainder table.
2332 * the CRC-32 generator polynomial is:
2333 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
2334 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
2336 unsigned int crc32_polynomial
= 0x04c11db7;
2340 * pre-calculate the CRC-32 remainder for each possible octet encoding
2342 for (i
= 0; i
< 256; i
++) {
2343 unsigned int crc_rem
= i
<< 24;
2345 for (j
= 0; j
< 8; j
++) {
2346 if (crc_rem
& 0x80000000) {
2347 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
2349 crc_rem
= (crc_rem
<< 1);
2352 crc32tab
[i
] = crc_rem
;
2358 * Name cache initialization, from vfs_init() when we are booting
2365 desiredNegNodes
= (desiredvnodes
/ 10);
2366 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
2368 TAILQ_INIT(&nchead
);
2369 TAILQ_INIT(&neghead
);
2373 nchashtbl
= hashinit(MAX(CONFIG_NC_HASH
, (2 * desiredNodes
)), M_CACHE
, &nchash
);
2374 nchashmask
= nchash
;
2377 init_string_table();
2379 /* Allocate name cache lock group attribute and group */
2380 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
2382 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
2384 /* Allocate name cache lock attribute */
2385 namecache_lck_attr
= lck_attr_alloc_init();
2387 /* Allocate name cache lock */
2388 namecache_rw_lock
= lck_rw_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
2391 /* Allocate string cache lock group attribute and group */
2392 strcache_lck_grp_attr
= lck_grp_attr_alloc_init();
2394 strcache_lck_grp
= lck_grp_alloc_init("String Cache", strcache_lck_grp_attr
);
2396 /* Allocate string cache lock attribute */
2397 strcache_lck_attr
= lck_attr_alloc_init();
2399 /* Allocate string cache lock */
2400 strtable_rw_lock
= lck_rw_alloc_init(strcache_lck_grp
, strcache_lck_attr
);
2402 for (i
= 0; i
< NUM_STRCACHE_LOCKS
; i
++) {
2403 lck_mtx_init(&strcache_mtx_locks
[i
], strcache_lck_grp
, strcache_lck_attr
);
2408 name_cache_lock_shared(void)
2410 lck_rw_lock_shared(namecache_rw_lock
);
2414 name_cache_lock(void)
2416 lck_rw_lock_exclusive(namecache_rw_lock
);
2420 name_cache_unlock(void)
2422 lck_rw_done(namecache_rw_lock
);
2427 resize_namecache(int newsize
)
2429 struct nchashhead
*new_table
;
2430 struct nchashhead
*old_table
;
2431 struct nchashhead
*old_head
, *head
;
2432 struct namecache
*entry
, *next
;
2433 uint32_t i
, hashval
;
2434 int dNodes
, dNegNodes
, nelements
;
2435 u_long new_size
, old_size
;
2441 dNegNodes
= (newsize
/ 10);
2442 dNodes
= newsize
+ dNegNodes
;
2443 // we don't support shrinking yet
2444 if (dNodes
<= desiredNodes
) {
2448 if (os_mul_overflow(dNodes
, 2, &nelements
)) {
2452 new_table
= hashinit(nelements
, M_CACHE
, &nchashmask
);
2453 new_size
= nchashmask
+ 1;
2455 if (new_table
== NULL
) {
2461 old_table
= nchashtbl
;
2462 nchashtbl
= new_table
;
2466 // walk the old table and insert all the entries into
2469 for (i
= 0; i
< old_size
; i
++) {
2470 old_head
= &old_table
[i
];
2471 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2473 // XXXdbg - Beware: this assumes that hash_string() does
2474 // the same thing as what happens in
2475 // lookup() over in vfs_lookup.c
2476 hashval
= hash_string(entry
->nc_name
, 0);
2477 entry
->nc_hashval
= hashval
;
2478 head
= NCHHASH(entry
->nc_dvp
, hashval
);
2480 next
= entry
->nc_hash
.le_next
;
2481 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
2484 desiredNodes
= dNodes
;
2485 desiredNegNodes
= dNegNodes
;
2487 NAME_CACHE_UNLOCK();
2488 FREE(old_table
, M_CACHE
);
2494 cache_delete(struct namecache
*ncp
, int free_entry
)
2496 NCHSTAT(ncs_deletes
);
2499 LIST_REMOVE(ncp
, nc_un
.nc_link
);
2501 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
2504 TAILQ_REMOVE(&(ncp
->nc_dvp
->v_ncchildren
), ncp
, nc_child
);
2506 LIST_REMOVE(ncp
, nc_hash
);
2508 * this field is used to indicate
2509 * that the entry is in use and
2510 * must be deleted before it can
2513 ncp
->nc_hash
.le_prev
= NULL
;
2515 vfs_removename(ncp
->nc_name
);
2516 ncp
->nc_name
= NULL
;
2518 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
2519 FREE_ZONE(ncp
, sizeof(*ncp
), M_CACHE
);
2526 * purge the entry associated with the
2527 * specified vnode from the name cache
2530 cache_purge_locked(vnode_t vp
, kauth_cred_t
*credp
)
2532 struct namecache
*ncp
;
2535 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
2536 (TAILQ_FIRST(&vp
->v_ncchildren
) == NULL
) &&
2537 (vp
->v_cred
== NOCRED
) &&
2538 (vp
->v_parent
== NULLVP
)) {
2543 vp
->v_parent
->v_nc_generation
++;
2546 while ((ncp
= LIST_FIRST(&vp
->v_nclinks
))) {
2547 cache_delete(ncp
, 1);
2550 while ((ncp
= TAILQ_FIRST(&vp
->v_ncchildren
))) {
2551 cache_delete(ncp
, 1);
2555 * Use a temp variable to avoid kauth_cred_unref() while NAME_CACHE_LOCK is held
2557 *credp
= vp
->v_cred
;
2558 vp
->v_cred
= NOCRED
;
2559 vp
->v_authorized_actions
= 0;
2563 cache_purge(vnode_t vp
)
2565 kauth_cred_t tcred
= NULL
;
2567 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) &&
2568 (TAILQ_FIRST(&vp
->v_ncchildren
) == NULL
) &&
2569 (vp
->v_cred
== NOCRED
) &&
2570 (vp
->v_parent
== NULLVP
)) {
2576 cache_purge_locked(vp
, &tcred
);
2578 NAME_CACHE_UNLOCK();
2580 if (tcred
&& IS_VALID_CRED(tcred
)) {
2581 kauth_cred_unref(&tcred
);
2586 * Purge all negative cache entries that are children of the
2587 * given vnode. A case-insensitive file system (or any file
2588 * system that has multiple equivalent names for the same
2589 * directory entry) can use this when creating or renaming
2590 * to remove negative entries that may no longer apply.
2593 cache_purge_negatives(vnode_t vp
)
2595 struct namecache
*ncp
, *next_ncp
;
2599 TAILQ_FOREACH_SAFE(ncp
, &vp
->v_ncchildren
, nc_child
, next_ncp
) {
2604 cache_delete(ncp
, 1);
2607 NAME_CACHE_UNLOCK();
2611 * Flush all entries referencing a particular filesystem.
2613 * Since we need to check it anyway, we will flush all the invalid
2614 * entries at the same time.
2617 cache_purgevfs(struct mount
*mp
)
2619 struct nchashhead
*ncpp
;
2620 struct namecache
*ncp
;
2623 /* Scan hash tables for applicable entries */
2624 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
2626 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
2627 if (ncp
->nc_dvp
->v_mount
== mp
) {
2628 cache_delete(ncp
, 0);
2633 NAME_CACHE_UNLOCK();
2639 // String ref routines
2641 static LIST_HEAD(stringhead
, string_t
) * string_ref_table
;
2642 static u_long string_table_mask
;
2643 static uint32_t filled_buckets
= 0;
2646 typedef struct string_t
{
2647 LIST_ENTRY(string_t
) hash_chain
;
2654 resize_string_ref_table(void)
2656 struct stringhead
*new_table
;
2657 struct stringhead
*old_table
;
2658 struct stringhead
*old_head
, *head
;
2659 string_t
*entry
, *next
;
2660 uint32_t i
, hashval
;
2661 u_long new_mask
, old_mask
;
2664 * need to hold the table lock exclusively
2665 * in order to grow the table... need to recheck
2666 * the need to resize again after we've taken
2667 * the lock exclusively in case some other thread
2668 * beat us to the punch
2670 lck_rw_lock_exclusive(strtable_rw_lock
);
2672 if (4 * filled_buckets
< ((string_table_mask
+ 1) * 3)) {
2673 lck_rw_done(strtable_rw_lock
);
2676 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
2678 if (new_table
== NULL
) {
2679 printf("failed to resize the hash table.\n");
2680 lck_rw_done(strtable_rw_lock
);
2685 old_table
= string_ref_table
;
2686 string_ref_table
= new_table
;
2687 old_mask
= string_table_mask
;
2688 string_table_mask
= new_mask
;
2691 // walk the old table and insert all the entries into
2694 for (i
= 0; i
<= old_mask
; i
++) {
2695 old_head
= &old_table
[i
];
2696 for (entry
= old_head
->lh_first
; entry
!= NULL
; entry
= next
) {
2697 hashval
= hash_string((const char *)entry
->str
, 0);
2698 head
= &string_ref_table
[hashval
& string_table_mask
];
2699 if (head
->lh_first
== NULL
) {
2702 next
= entry
->hash_chain
.le_next
;
2703 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2706 lck_rw_done(strtable_rw_lock
);
2708 FREE(old_table
, M_CACHE
);
2713 init_string_table(void)
2715 string_ref_table
= hashinit(CONFIG_VFS_NAMES
, M_CACHE
, &string_table_mask
);
2720 vfs_addname(const char *name
, uint32_t len
, u_int hashval
, u_int flags
)
2722 return add_name_internal(name
, len
, hashval
, FALSE
, flags
);
2727 add_name_internal(const char *name
, uint32_t len
, u_int hashval
, boolean_t need_extra_ref
, __unused u_int flags
)
2729 struct stringhead
*head
;
2731 uint32_t chain_len
= 0;
2732 uint32_t hash_index
;
2733 uint32_t lock_index
;
2736 if (len
> MAXPATHLEN
) {
2741 * if the length already accounts for the null-byte, then
2742 * subtract one so later on we don't index past the end
2745 if (len
> 0 && name
[len
- 1] == '\0') {
2749 hashval
= hash_string(name
, len
);
2753 * take this lock 'shared' to keep the hash stable
2754 * if someone else decides to grow the pool they
2755 * will take this lock exclusively
2757 lck_rw_lock_shared(strtable_rw_lock
);
2760 * If the table gets more than 3/4 full, resize it
2762 if (4 * filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
2763 lck_rw_done(strtable_rw_lock
);
2765 resize_string_ref_table();
2767 lck_rw_lock_shared(strtable_rw_lock
);
2769 hash_index
= hashval
& string_table_mask
;
2770 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2772 head
= &string_ref_table
[hash_index
];
2774 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2776 for (entry
= head
->lh_first
; entry
!= NULL
; chain_len
++, entry
= entry
->hash_chain
.le_next
) {
2777 if (strncmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == 0) {
2782 if (entry
== NULL
) {
2783 lck_mtx_convert_spin(&strcache_mtx_locks
[lock_index
]);
2785 * it wasn't already there so add it.
2787 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
2789 if (head
->lh_first
== NULL
) {
2790 OSAddAtomic(1, &filled_buckets
);
2792 ptr
= (char *)((char *)entry
+ sizeof(string_t
));
2793 strncpy(ptr
, name
, len
);
2796 entry
->refcount
= 1;
2797 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
2799 if (need_extra_ref
== TRUE
) {
2803 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2804 lck_rw_done(strtable_rw_lock
);
2806 return (const char *)entry
->str
;
2811 vfs_removename(const char *nameref
)
2813 struct stringhead
*head
;
2816 uint32_t hash_index
;
2817 uint32_t lock_index
;
2818 int retval
= ENOENT
;
2820 hashval
= hash_string(nameref
, 0);
2823 * take this lock 'shared' to keep the hash stable
2824 * if someone else decides to grow the pool they
2825 * will take this lock exclusively
2827 lck_rw_lock_shared(strtable_rw_lock
);
2829 * must compute the head behind the table lock
2830 * since the size and location of the table
2831 * can change on the fly
2833 hash_index
= hashval
& string_table_mask
;
2834 lock_index
= hash_index
% NUM_STRCACHE_LOCKS
;
2836 head
= &string_ref_table
[hash_index
];
2838 lck_mtx_lock_spin(&strcache_mtx_locks
[lock_index
]);
2840 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2841 if (entry
->str
== nameref
) {
2844 if (entry
->refcount
== 0) {
2845 LIST_REMOVE(entry
, hash_chain
);
2847 if (head
->lh_first
== NULL
) {
2848 OSAddAtomic(-1, &filled_buckets
);
2857 lck_mtx_unlock(&strcache_mtx_locks
[lock_index
]);
2858 lck_rw_done(strtable_rw_lock
);
2860 if (entry
!= NULL
) {
2861 FREE(entry
, M_TEMP
);
2868 #ifdef DUMP_STRING_TABLE
2870 dump_string_table(void)
2872 struct stringhead
*head
;
2876 lck_rw_lock_shared(strtable_rw_lock
);
2878 for (i
= 0; i
<= string_table_mask
; i
++) {
2879 head
= &string_ref_table
[i
];
2880 for (entry
= head
->lh_first
; entry
!= NULL
; entry
= entry
->hash_chain
.le_next
) {
2881 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
2884 lck_rw_done(strtable_rw_lock
);
2886 #endif /* DUMP_STRING_TABLE */