]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cache.c
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Poul-Henning Kamp of the FreeBSD Project.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
61 #include <sys/param.h>
62 #include <sys/systm.h>
64 #include <sys/mount_internal.h>
65 #include <sys/vnode_internal.h>
66 #include <sys/namei.h>
67 #include <sys/errno.h>
68 #include <sys/malloc.h>
69 #include <sys/kauth.h>
73 * Name caching works as follows:
75 * Names found by directory scans are retained in a cache
76 * for future reference. It is managed LRU, so frequently
77 * used names will hang around. Cache is indexed by hash value
78 * obtained from (vp, name) where vp refers to the directory
81 * If it is a "negative" entry, (i.e. for a name that is known NOT to
82 * exist) the vnode pointer will be NULL.
84 * Upon reaching the last segment of a path, if the reference
85 * is for DELETE, or NOCACHE is set (rewrite), and the
86 * name is located in the cache, it will be dropped.
90 * Structures associated with name cacheing.
93 LIST_HEAD(nchashhead
, namecache
) *nchashtbl
; /* Hash Table */
95 u_long nchash
; /* size of hash table - 1 */
96 long numcache
; /* number of cache entries allocated */
99 TAILQ_HEAD(, namecache
) nchead
; /* chain of all name cache entries */
100 TAILQ_HEAD(, namecache
) neghead
; /* chain of only negative cache entries */
101 struct nchstats nchstats
; /* cache effectiveness statistics */
103 /* vars for name cache list lock */
104 lck_grp_t
* namecache_lck_grp
;
105 lck_grp_attr_t
* namecache_lck_grp_attr
;
106 lck_attr_t
* namecache_lck_attr
;
107 lck_mtx_t
* namecache_mtx_lock
;
109 static vnode_t
cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
);
110 static int remove_name_locked(const char *);
111 static char *add_name_locked(const char *, size_t, u_int
, u_int
);
112 static void init_string_table(void);
113 static void cache_delete(struct namecache
*, int);
114 static void dump_string_table(void);
116 static void init_crc32(void);
117 static unsigned int crc32tab
[256];
120 #define NCHHASH(dvp, hash_val) \
121 (&nchashtbl[(dvp->v_id ^ (hash_val)) & nchashmask])
126 // This function builds the path to a filename in "buff". The
127 // length of the buffer *INCLUDING* the trailing zero byte is
128 // returned in outlen. NOTE: the length includes the trailing
129 // zero byte and thus the length is one greater than what strlen
130 // would return. This is important and lots of code elsewhere
131 // in the kernel assumes this behavior.
134 build_path(vnode_t first_vp
, char *buff
, int buflen
, int *outlen
)
136 vnode_t vp
= first_vp
;
138 int len
, ret
=0, counter
=0;
140 end
= &buff
[buflen
-1];
144 * if this is the root dir of a file system...
146 if (vp
&& (vp
->v_flag
& VROOT
) && vp
->v_mount
) {
148 * then if it's the root fs, just put in a '/' and get out of here
150 if (vp
->v_mount
->mnt_flag
& MNT_ROOTFS
) {
155 * else just use the covered vnode to get the mount path
157 vp
= vp
->v_mount
->mnt_vnodecovered
;
162 while (vp
&& vp
->v_parent
!= vp
) {
164 * the maximum depth of a file system hierarchy is MAXPATHLEN/2
165 * (with single-char names separated by slashes). we panic if
166 * we've ever looped more than that.
168 if (counter
++ > MAXPATHLEN
/2) {
169 panic("build_path: vnode parent chain is too long! vp 0x%x\n", vp
);
174 if (vp
->v_parent
!= NULL
) {
182 * check that there's enough space (make sure to include space for the '/')
184 if ((end
- buff
) < (len
+ 1)) {
193 for (; len
> 0; len
--) {
197 * put in the path separator
202 * walk up the chain (as long as we're not the root)
204 if (vp
== first_vp
&& (vp
->v_flag
& VROOT
)) {
205 if (vp
->v_mount
&& vp
->v_mount
->mnt_vnodecovered
) {
206 vp
= vp
->v_mount
->mnt_vnodecovered
->v_parent
;
214 * check if we're crossing a mount point and
215 * switch the vp if we are.
217 if (vp
&& (vp
->v_flag
& VROOT
) && vp
->v_mount
) {
218 vp
= vp
->v_mount
->mnt_vnodecovered
;
224 * slide it down to the beginning of the buffer
226 memmove(buff
, end
, &buff
[buflen
] - end
);
228 *outlen
= &buff
[buflen
] - end
; // length includes the trailing zero byte
235 * return NULLVP if vp's parent doesn't
236 * exist, or we can't get a valid iocount
237 * else return the parent of vp
240 vnode_getparent(vnode_t vp
)
242 vnode_t pvp
= NULLVP
;
247 * v_parent is stable behind the name_cache lock
248 * however, the only thing we can really guarantee
249 * is that we've grabbed a valid iocount on the
250 * parent of 'vp' at the time we took the name_cache lock...
251 * once we drop the lock, vp could get re-parented
253 if ( (pvp
= vp
->v_parent
) != NULLVP
) {
258 if (vnode_getwithvid(pvp
, pvid
) != 0)
267 vnode_getname(vnode_t vp
)
274 name
= add_name_locked(vp
->v_name
, strlen(vp
->v_name
), 0, 0);
281 vnode_putname(char *name
)
285 remove_name_locked(name
);
292 * if VNODE_UPDATE_PARENT, and we can take
293 * a reference on dvp, then update vp with
294 * it's new parent... if vp already has a parent,
295 * then drop the reference vp held on it
297 * if VNODE_UPDATE_NAME,
298 * then drop string ref on v_name if it exists, and if name is non-NULL
299 * then pick up a string reference on name and record it in v_name...
300 * optionally pass in the length and hashval of name if known
302 * if VNODE_UPDATE_CACHE, flush the name cache entries associated with vp
305 vnode_update_identity(vnode_t vp
, vnode_t dvp
, char *name
, int name_len
, int name_hashval
, int flags
)
307 struct namecache
*ncp
;
308 vnode_t old_parentvp
= NULLVP
;
311 if (flags
& VNODE_UPDATE_PARENT
) {
312 if (dvp
&& vnode_ref(dvp
) != 0)
318 if ( (flags
& VNODE_UPDATE_NAME
) && (name
!= vp
->v_name
) ) {
319 if (vp
->v_name
!= NULL
) {
320 remove_name_locked(vp
->v_name
);
325 name_len
= strlen(name
);
326 vp
->v_name
= add_name_locked(name
, name_len
, name_hashval
, 0);
329 if (flags
& VNODE_UPDATE_PARENT
) {
330 if (dvp
!= vp
&& dvp
!= vp
->v_parent
) {
331 old_parentvp
= vp
->v_parent
;
336 flags
|= VNODE_UPDATE_CACHE
;
339 if (flags
& VNODE_UPDATE_CACHE
) {
340 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
341 cache_delete(ncp
, 1);
351 ut
= get_bsdthread_info(current_thread());
354 * indicated to vnode_rele that it shouldn't do a
355 * vnode_reclaim at this time... instead it will
356 * chain the vnode to the uu_vreclaims list...
357 * we'll be responsible for calling vnode_reclaim
358 * on each of the vnodes in this list...
360 ut
->uu_defer_reclaims
= 1;
361 ut
->uu_vreclaims
= NULLVP
;
363 while ( (vp
= old_parentvp
) != NULLVP
) {
367 vnode_rele_internal(vp
, 0, 0, 1);
370 * check to see if the vnode is now in the state
371 * that would have triggered a vnode_reclaim in vnode_rele
372 * if it is, we save it's parent pointer and then NULL
373 * out the v_parent field... we'll drop the reference
374 * that was held on the next iteration of this loop...
375 * this short circuits a potential deep recursion if we
376 * have a long chain of parents in this state...
377 * we'll sit in this loop until we run into
378 * a parent in this chain that is not in this state
380 * make our check and the node_rele atomic
381 * with respect to the current vnode we're working on
382 * by holding the vnode lock
383 * if vnode_rele deferred the vnode_reclaim and has put
384 * this vnode on the list to be reaped by us, than
385 * it has left this vnode with an iocount == 1
387 if ( (vp
->v_iocount
== 1) && (vp
->v_usecount
== 0) &&
388 ((vp
->v_lflag
& (VL_MARKTERM
| VL_TERMINATE
| VL_DEAD
)) == VL_MARKTERM
)) {
390 * vnode_rele wanted to do a vnode_reclaim on this vnode
391 * it should be sitting on the head of the uu_vreclaims chain
392 * pull the parent pointer now so that when we do the
393 * vnode_reclaim for each of the vnodes in the uu_vreclaims
394 * list, we won't recurse back through here
397 old_parentvp
= vp
->v_parent
;
398 vp
->v_parent
= NULLVP
;
402 * we're done... we ran into a vnode that isn't
405 old_parentvp
= NULLVP
;
409 ut
->uu_defer_reclaims
= 0;
411 while ( (vp
= ut
->uu_vreclaims
) != NULLVP
) {
412 ut
->uu_vreclaims
= vp
->v_defer_reclaimlist
;
415 * vnode_put will drive the vnode_reclaim if
416 * we are still the only reference on this vnode
425 * Mark a vnode as having multiple hard links. HFS makes use of this
426 * because it keeps track of each link separately, and wants to know
427 * which link was actually used.
429 * This will cause the name cache to force a VNOP_LOOKUP on the vnode
430 * so that HFS can post-process the lookup. Also, volfs will call
431 * VNOP_GETATTR2 to determine the parent, instead of using v_parent.
433 void vnode_set_hard_link(vnode_t vp
)
438 * In theory, we're changing the vnode's identity as far as the
439 * name cache is concerned, so we ought to grab the name cache lock
440 * here. However, there is already a race, and grabbing the name
441 * cache lock only makes the race window slightly smaller.
443 * The race happens because the vnode already exists in the name
444 * cache, and could be found by one thread before another thread
445 * can set the hard link flag.
448 vp
->v_flag
|= VISHARDLINK
;
454 void vnode_uncache_credentials(vnode_t vp
)
456 kauth_cred_t ucred
= NULL
;
467 kauth_cred_rele(ucred
);
472 void vnode_cache_credentials(vnode_t vp
, vfs_context_t context
)
475 kauth_cred_t tcred
= NOCRED
;
478 ucred
= vfs_context_ucred(context
);
480 if (vp
->v_cred
!= ucred
|| (vp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
)) {
484 vp
->v_cred_timestamp
= tv
.tv_sec
;
486 if (vp
->v_cred
!= ucred
) {
487 kauth_cred_ref(ucred
);
495 kauth_cred_rele(tcred
);
499 /* reverse_lookup - lookup by walking back up the parent chain while leveraging
500 * use of the name cache lock in order to protect our starting vnode.
501 * NOTE - assumes you already have search access to starting point.
502 * returns 0 when we have reached the root, current working dir, or chroot root
506 reverse_lookup(vnode_t start_vp
, vnode_t
*lookup_vpp
, struct filedesc
*fdp
, vfs_context_t context
, int *dp_authorized
)
510 vnode_t dp
= start_vp
;
515 ucred
= vfs_context_ucred(context
);
516 *lookup_vpp
= start_vp
;
520 if ( dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
) ) {
527 if (auth_opaque
&& ((tv
.tv_sec
- dp
->v_cred_timestamp
) > VCRED_EXPIRED
))
529 if (dp
->v_cred
!= ucred
)
532 * indicate that we're allowed to traverse this directory...
533 * even if we bail for some reason, this information is valid and is used
534 * to avoid doing a vnode_authorize
538 if ((dp
->v_flag
& VROOT
) != 0 || /* Hit "/" */
539 (dp
== fdp
->fd_cdir
) || /* Hit process's working directory */
540 (dp
== fdp
->fd_rdir
)) { /* Hit process chroot()-ed root */
545 if ( (vp
= dp
->v_parent
) == NULLVP
)
556 if (done
== 0 && dp
!= start_vp
) {
557 if (vnode_getwithvid(dp
, vid
) != 0) {
558 *lookup_vpp
= start_vp
;
562 return((done
== 1) ? 0 : -1);
566 cache_lookup_path(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
, vfs_context_t context
, int *trailing_slash
, int *dp_authorized
)
568 char *cp
; /* pointer into pathname argument */
572 vnode_t tdp
= NULLVP
;
577 ucred
= vfs_context_ucred(context
);
583 if ( dp
->v_mount
&& (dp
->v_mount
->mnt_kern_flag
& MNTK_AUTH_OPAQUE
) ) {
589 * Search a directory.
591 * The cn_hash value is for use by cache_lookup
592 * The last component of the filename is left accessible via
593 * cnp->cn_nameptr for callers that need the name.
596 cp
= cnp
->cn_nameptr
;
598 while (*cp
&& (*cp
!= '/')) {
599 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
602 * the crc generator can legitimately generate
603 * a 0... however, 0 for us means that we
604 * haven't computed a hash, so use 1 instead
609 cnp
->cn_namelen
= cp
- cnp
->cn_nameptr
;
611 ndp
->ni_pathlen
-= cnp
->cn_namelen
;
615 * Replace multiple slashes by a single slash and trailing slashes
616 * by a null. This must be done before VNOP_LOOKUP() because some
617 * fs's don't know about trailing slashes. Remember if there were
618 * trailing slashes to handle symlinks, existing non-directories
619 * and non-existing files that won't be directories specially later.
621 while (*cp
== '/' && (cp
[1] == '/' || cp
[1] == '\0')) {
627 *ndp
->ni_next
= '\0';
632 cnp
->cn_flags
&= ~(MAKEENTRY
| ISLASTCN
| ISDOTDOT
);
635 cnp
->cn_flags
|= ISLASTCN
;
637 if (cnp
->cn_namelen
== 2 && cnp
->cn_nameptr
[1] == '.' && cnp
->cn_nameptr
[0] == '.')
638 cnp
->cn_flags
|= ISDOTDOT
;
642 if (auth_opaque
&& ((tv
.tv_sec
- dp
->v_cred_timestamp
) > VCRED_EXPIRED
))
645 if (dp
->v_cred
!= ucred
)
648 * indicate that we're allowed to traverse this directory...
649 * even if we fail the cache lookup or decide to bail for
650 * some other reason, this information is valid and is used
651 * to avoid doing a vnode_authorize before the call to VNOP_LOOKUP
655 if ( (cnp
->cn_flags
& (ISLASTCN
| ISDOTDOT
)) ) {
656 if (cnp
->cn_nameiop
!= LOOKUP
)
658 if (cnp
->cn_flags
& (LOCKPARENT
| NOCACHE
| ISDOTDOT
))
661 if ( (vp
= cache_lookup_locked(dp
, cnp
)) == NULLVP
)
664 if ( (cnp
->cn_flags
& ISLASTCN
) )
667 if (vp
->v_type
!= VDIR
) {
668 if (vp
->v_type
!= VLNK
)
672 if (vp
->v_mountedhere
&& ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0))
678 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
680 while (*cnp
->cn_nameptr
== '/') {
692 if ((vp
!= NULLVP
) && (vp
->v_type
!= VLNK
) &&
693 ((cnp
->cn_flags
& (ISLASTCN
| LOCKPARENT
| WANTPARENT
| SAVESTART
)) == ISLASTCN
)) {
695 * if we've got a child and it's the last component, and
696 * the lookup doesn't need to return the parent then we
697 * can skip grabbing an iocount on the parent, since all
698 * we're going to do with it is a vnode_put just before
699 * we return from 'lookup'. If it's a symbolic link,
700 * we need the parent in case the link happens to be
701 * a relative pathname.
708 * return the last directory we looked at
709 * with an io reference held
711 if (dp
== ndp
->ni_usedvp
) {
713 * if this vnode matches the one passed in via USEDVP
714 * than this context already holds an io_count... just
715 * use vnode_get to get an extra ref for lookup to play
716 * with... can't use the getwithvid variant here because
717 * it will block behind a vnode_drain which would result
718 * in a deadlock (since we already own an io_count that the
719 * vnode_drain is waiting on)... vnode_get grabs the io_count
720 * immediately w/o waiting... it always succeeds
723 } else if ( (vnode_getwithvid(dp
, vid
)) ) {
725 * failure indicates the vnode
726 * changed identity or is being
727 * TERMINATED... in either case
734 if ( (vnode_getwithvid(vp
, vvid
)) ) {
738 * can't get reference on the vp we'd like
739 * to return... if we didn't grab a reference
740 * on the directory (due to fast path bypass),
741 * then we need to do it now... we can't return
742 * with both ni_dvp and ni_vp NULL, and no
759 cache_lookup_locked(vnode_t dvp
, struct componentname
*cnp
)
761 register struct namecache
*ncp
;
762 register struct nchashhead
*ncpp
;
763 register long namelen
= cnp
->cn_namelen
;
764 char *nameptr
= cnp
->cn_nameptr
;
765 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
768 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
769 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
770 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
771 if (memcmp(ncp
->nc_name
, nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
777 * We failed to find an entry
782 if (vp
&& (vp
->v_flag
& VISHARDLINK
)) {
784 * The file system wants a VNOP_LOOKUP on this vnode
794 // Have to take a len argument because we may only need to
795 // hash part of a componentname.
798 hash_string(const char *cp
, int len
)
804 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
807 while (*cp
!= '\0') {
808 hash
^= crc32tab
[((hash
>> 24) ^ (unsigned char)*cp
++)];
812 * the crc generator can legitimately generate
813 * a 0... however, 0 for us means that we
814 * haven't computed a hash, so use 1 instead
823 * Lookup an entry in the cache
825 * We don't do this if the segment name is long, simply so the cache
826 * can avoid holding long names (which would either waste space, or
827 * add greatly to the complexity).
829 * Lookup is called with dvp pointing to the directory to search,
830 * cnp pointing to the name of the entry being sought. If the lookup
831 * succeeds, the vnode is returned in *vpp, and a status of -1 is
832 * returned. If the lookup determines that the name does not exist
833 * (negative cacheing), a status of ENOENT is returned. If the lookup
834 * fails, a status of zero is returned.
838 cache_lookup(dvp
, vpp
, cnp
)
841 struct componentname
*cnp
;
843 register struct namecache
*ncp
;
844 register struct nchashhead
*ncpp
;
845 register long namelen
= cnp
->cn_namelen
;
846 char *nameptr
= cnp
->cn_nameptr
;
847 unsigned int hashval
= (cnp
->cn_hash
& NCHASHMASK
);
853 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
854 LIST_FOREACH(ncp
, ncpp
, nc_hash
) {
855 if ((ncp
->nc_dvp
== dvp
) && (ncp
->nc_hashval
== hashval
)) {
856 if (memcmp(ncp
->nc_name
, nameptr
, namelen
) == 0 && ncp
->nc_name
[namelen
] == 0)
860 /* We failed to find an entry */
867 /* We don't want to have an entry, so dump it */
868 if ((cnp
->cn_flags
& MAKEENTRY
) == 0) {
869 nchstats
.ncs_badhits
++;
870 cache_delete(ncp
, 1);
876 /* We found a "positive" match, return the vnode */
878 nchstats
.ncs_goodhits
++;
883 if (vnode_getwithvid(vp
, vid
)) {
885 nchstats
.ncs_badvid
++;
893 /* We found a negative match, and want to create it, so purge */
894 if (cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) {
895 nchstats
.ncs_badhits
++;
896 cache_delete(ncp
, 1);
902 * We found a "negative" match, ENOENT notifies client of this match.
903 * The nc_whiteout field records whether this is a whiteout.
905 nchstats
.ncs_neghits
++;
907 if (ncp
->nc_whiteout
)
908 cnp
->cn_flags
|= ISWHITEOUT
;
914 * Add an entry to the cache.
917 cache_enter(dvp
, vp
, cnp
)
920 struct componentname
*cnp
;
922 register struct namecache
*ncp
, *negp
;
923 register struct nchashhead
*ncpp
;
925 if (cnp
->cn_hash
== 0)
926 cnp
->cn_hash
= hash_string(cnp
->cn_nameptr
, cnp
->cn_namelen
);
930 /* if the entry is for -ve caching vp is null */
931 if ((vp
!= NULLVP
) && (LIST_FIRST(&vp
->v_nclinks
))) {
933 * someone beat us to the punch..
934 * this vnode is already in the cache
940 * We allocate a new entry if we are less than the maximum
941 * allowed and the one at the front of the list is in use.
942 * Otherwise we use the one at the front of the list.
944 if (numcache
< desiredNodes
&&
945 ((ncp
= nchead
.tqh_first
) == NULL
||
946 ncp
->nc_hash
.le_prev
!= 0)) {
948 * Allocate one more entry
950 ncp
= (struct namecache
*)_MALLOC_ZONE((u_long
)sizeof *ncp
, M_CACHE
, M_WAITOK
);
956 ncp
= TAILQ_FIRST(&nchead
);
957 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
959 if (ncp
->nc_hash
.le_prev
!= 0) {
961 * still in use... we need to
962 * delete it before re-using it
964 nchstats
.ncs_stolen
++;
965 cache_delete(ncp
, 0);
968 nchstats
.ncs_enters
++;
971 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
975 ncp
->nc_hashval
= cnp
->cn_hash
;
976 ncp
->nc_whiteout
= FALSE
;
977 ncp
->nc_name
= add_name_locked(cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, 0);
980 * make us the newest entry in the cache
981 * i.e. we'll be the last to be stolen
983 TAILQ_INSERT_TAIL(&nchead
, ncp
, nc_entry
);
985 ncpp
= NCHHASH(dvp
, cnp
->cn_hash
);
988 register struct namecache
*p
;
990 for (p
= ncpp
->lh_first
; p
!= 0; p
= p
->nc_hash
.le_next
)
992 panic("cache_enter: duplicate");
996 * make us available to be found via lookup
998 LIST_INSERT_HEAD(ncpp
, ncp
, nc_hash
);
1002 * add to the list of name cache entries
1005 LIST_INSERT_HEAD(&vp
->v_nclinks
, ncp
, nc_un
.nc_link
);
1008 * this is a negative cache entry (vp == NULL)
1009 * stick it on the negative cache list
1010 * and record the whiteout state
1012 TAILQ_INSERT_TAIL(&neghead
, ncp
, nc_un
.nc_negentry
);
1014 if (cnp
->cn_flags
& ISWHITEOUT
)
1015 ncp
->nc_whiteout
= TRUE
;
1016 nchstats
.ncs_negtotal
++;
1018 if (nchstats
.ncs_negtotal
> desiredNegNodes
) {
1020 * if we've reached our desired limit
1021 * of negative cache entries, delete
1024 negp
= TAILQ_FIRST(&neghead
);
1025 TAILQ_REMOVE(&neghead
, negp
, nc_un
.nc_negentry
);
1027 cache_delete(negp
, 1);
1031 * add us to the list of name cache entries that
1032 * are children of dvp
1034 LIST_INSERT_HEAD(&dvp
->v_ncchildren
, ncp
, nc_child
);
1036 name_cache_unlock();
1041 * Initialize CRC-32 remainder table.
1043 static void init_crc32(void)
1046 * the CRC-32 generator polynomial is:
1047 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^10
1048 * + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1050 unsigned int crc32_polynomial
= 0x04c11db7;
1054 * pre-calculate the CRC-32 remainder for each possible octet encoding
1056 for (i
= 0; i
< 256; i
++) {
1057 unsigned int crc_rem
= i
<< 24;
1059 for (j
= 0; j
< 8; j
++) {
1060 if (crc_rem
& 0x80000000)
1061 crc_rem
= (crc_rem
<< 1) ^ crc32_polynomial
;
1063 crc_rem
= (crc_rem
<< 1);
1065 crc32tab
[i
] = crc_rem
;
1071 * Name cache initialization, from vfs_init() when we are booting
1076 desiredNegNodes
= (desiredvnodes
/ 10);
1077 desiredNodes
= desiredvnodes
+ desiredNegNodes
;
1079 TAILQ_INIT(&nchead
);
1080 TAILQ_INIT(&neghead
);
1084 nchashtbl
= hashinit(MAX(4096, (2 *desiredNodes
)), M_CACHE
, &nchash
);
1085 nchashmask
= nchash
;
1088 init_string_table();
1090 /* Allocate mount list lock group attribute and group */
1091 namecache_lck_grp_attr
= lck_grp_attr_alloc_init();
1092 lck_grp_attr_setstat(namecache_lck_grp_attr
);
1094 namecache_lck_grp
= lck_grp_alloc_init("Name Cache", namecache_lck_grp_attr
);
1096 /* Allocate mount list lock attribute */
1097 namecache_lck_attr
= lck_attr_alloc_init();
1098 //lck_attr_setdebug(namecache_lck_attr);
1100 /* Allocate mount list lock */
1101 namecache_mtx_lock
= lck_mtx_alloc_init(namecache_lck_grp
, namecache_lck_attr
);
1107 name_cache_lock(void)
1109 lck_mtx_lock(namecache_mtx_lock
);
1113 name_cache_unlock(void)
1115 lck_mtx_unlock(namecache_mtx_lock
);
1121 resize_namecache(u_int newsize
)
1123 struct nchashhead
*new_table
;
1124 struct nchashhead
*old_table
;
1125 struct nchashhead
*old_head
, *head
;
1126 struct namecache
*entry
, *next
;
1127 uint32_t i
, hashval
;
1128 int dNodes
, dNegNodes
;
1129 u_long new_size
, old_size
;
1131 dNegNodes
= (newsize
/ 10);
1132 dNodes
= newsize
+ dNegNodes
;
1134 // we don't support shrinking yet
1135 if (dNodes
< desiredNodes
) {
1138 new_table
= hashinit(2 * dNodes
, M_CACHE
, &nchashmask
);
1139 new_size
= nchashmask
+ 1;
1141 if (new_table
== NULL
) {
1147 old_table
= nchashtbl
;
1148 nchashtbl
= new_table
;
1152 // walk the old table and insert all the entries into
1155 for(i
=0; i
< old_size
; i
++) {
1156 old_head
= &old_table
[i
];
1157 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1159 // XXXdbg - Beware: this assumes that hash_string() does
1160 // the same thing as what happens in
1161 // lookup() over in vfs_lookup.c
1162 hashval
= hash_string(entry
->nc_name
, 0);
1163 entry
->nc_hashval
= hashval
;
1164 head
= NCHHASH(entry
->nc_dvp
, hashval
);
1166 next
= entry
->nc_hash
.le_next
;
1167 LIST_INSERT_HEAD(head
, entry
, nc_hash
);
1170 desiredNodes
= dNodes
;
1171 desiredNegNodes
= dNegNodes
;
1173 name_cache_unlock();
1174 FREE(old_table
, M_CACHE
);
1180 cache_delete(struct namecache
*ncp
, int age_entry
)
1182 nchstats
.ncs_deletes
++;
1185 LIST_REMOVE(ncp
, nc_un
.nc_link
);
1187 TAILQ_REMOVE(&neghead
, ncp
, nc_un
.nc_negentry
);
1188 nchstats
.ncs_negtotal
--;
1190 LIST_REMOVE(ncp
, nc_child
);
1192 LIST_REMOVE(ncp
, nc_hash
);
1194 * this field is used to indicate
1195 * that the entry is in use and
1196 * must be deleted before it can
1199 ncp
->nc_hash
.le_prev
= NULL
;
1203 * make it the next one available
1204 * for cache_enter's use
1206 TAILQ_REMOVE(&nchead
, ncp
, nc_entry
);
1207 TAILQ_INSERT_HEAD(&nchead
, ncp
, nc_entry
);
1209 remove_name_locked(ncp
->nc_name
);
1210 ncp
->nc_name
= NULL
;
1215 * purge the entry associated with the
1216 * specified vnode from the name cache
1219 cache_purge(vnode_t vp
)
1221 struct namecache
*ncp
;
1223 if ((LIST_FIRST(&vp
->v_nclinks
) == NULL
) && (LIST_FIRST(&vp
->v_ncchildren
) == NULL
))
1228 while ( (ncp
= LIST_FIRST(&vp
->v_nclinks
)) )
1229 cache_delete(ncp
, 1);
1231 while ( (ncp
= LIST_FIRST(&vp
->v_ncchildren
)) )
1232 cache_delete(ncp
, 1);
1234 name_cache_unlock();
1238 * Purge all negative cache entries that are children of the
1239 * given vnode. A case-insensitive file system (or any file
1240 * system that has multiple equivalent names for the same
1241 * directory entry) can use this when creating or renaming
1242 * to remove negative entries that may no longer apply.
1245 cache_purge_negatives(vnode_t vp
)
1247 struct namecache
*ncp
;
1251 LIST_FOREACH(ncp
, &vp
->v_ncchildren
, nc_child
)
1252 if (ncp
->nc_vp
== NULL
)
1253 cache_delete(ncp
, 1);
1255 name_cache_unlock();
1259 * Flush all entries referencing a particular filesystem.
1261 * Since we need to check it anyway, we will flush all the invalid
1262 * entries at the same time.
1268 struct nchashhead
*ncpp
;
1269 struct namecache
*ncp
;
1272 /* Scan hash tables for applicable entries */
1273 for (ncpp
= &nchashtbl
[nchash
- 1]; ncpp
>= nchashtbl
; ncpp
--) {
1275 for (ncp
= ncpp
->lh_first
; ncp
!= 0; ncp
= ncp
->nc_hash
.le_next
) {
1276 if (ncp
->nc_dvp
->v_mount
== mp
) {
1277 cache_delete(ncp
, 0);
1282 name_cache_unlock();
1288 // String ref routines
1290 static LIST_HEAD(stringhead
, string_t
) *string_ref_table
;
1291 static u_long string_table_mask
;
1292 static uint32_t max_chain_len
=0;
1293 static struct stringhead
*long_chain_head
=NULL
;
1294 static uint32_t filled_buckets
=0;
1295 static uint32_t num_dups
=0;
1296 static uint32_t nstrings
=0;
1298 typedef struct string_t
{
1299 LIST_ENTRY(string_t
) hash_chain
;
1307 resize_string_ref_table(void)
1309 struct stringhead
*new_table
;
1310 struct stringhead
*old_table
;
1311 struct stringhead
*old_head
, *head
;
1312 string_t
*entry
, *next
;
1313 uint32_t i
, hashval
;
1314 u_long new_mask
, old_mask
;
1316 new_table
= hashinit((string_table_mask
+ 1) * 2, M_CACHE
, &new_mask
);
1317 if (new_table
== NULL
) {
1322 old_table
= string_ref_table
;
1323 string_ref_table
= new_table
;
1324 old_mask
= string_table_mask
;
1325 string_table_mask
= new_mask
;
1327 printf("resize: max chain len %d, new table size %d\n",
1328 max_chain_len
, new_mask
+ 1);
1330 long_chain_head
= NULL
;
1333 // walk the old table and insert all the entries into
1336 for(i
=0; i
<= old_mask
; i
++) {
1337 old_head
= &old_table
[i
];
1338 for (entry
=old_head
->lh_first
; entry
!= NULL
; entry
=next
) {
1339 hashval
= hash_string(entry
->str
, 0);
1340 head
= &string_ref_table
[hashval
& string_table_mask
];
1341 if (head
->lh_first
== NULL
) {
1345 next
= entry
->hash_chain
.le_next
;
1346 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
1350 FREE(old_table
, M_CACHE
);
1357 init_string_table(void)
1359 string_ref_table
= hashinit(4096, M_CACHE
, &string_table_mask
);
1364 vfs_addname(const char *name
, size_t len
, u_int hashval
, u_int flags
)
1369 ptr
= add_name_locked(name
, len
, hashval
, flags
);
1370 name_cache_unlock();
1376 add_name_locked(const char *name
, size_t len
, u_int hashval
, __unused u_int flags
)
1378 struct stringhead
*head
;
1380 uint32_t chain_len
= 0;
1383 // If the table gets more than 3/4 full, resize it
1385 if (4*filled_buckets
>= ((string_table_mask
+ 1) * 3)) {
1386 if (resize_string_ref_table() != 0) {
1387 printf("failed to resize the hash table.\n");
1391 hashval
= hash_string(name
, 0);
1394 head
= &string_ref_table
[hashval
& string_table_mask
];
1395 for (entry
=head
->lh_first
; entry
!= NULL
; chain_len
++, entry
=entry
->hash_chain
.le_next
) {
1396 if (memcmp(entry
->str
, name
, len
) == 0 && entry
->str
[len
] == '\0') {
1403 if (entry
== NULL
) {
1404 // it wasn't already there so add it.
1405 MALLOC(entry
, string_t
*, sizeof(string_t
) + len
+ 1, M_TEMP
, M_WAITOK
);
1407 // have to get "head" again because we could have blocked
1408 // in malloc and thus head could have changed.
1410 head
= &string_ref_table
[hashval
& string_table_mask
];
1411 if (head
->lh_first
== NULL
) {
1415 entry
->str
= (char *)((char *)entry
+ sizeof(string_t
));
1416 strncpy(entry
->str
, name
, len
);
1417 entry
->str
[len
] = '\0';
1418 entry
->refcount
= 1;
1419 LIST_INSERT_HEAD(head
, entry
, hash_chain
);
1421 if (chain_len
> max_chain_len
) {
1422 max_chain_len
= chain_len
;
1423 long_chain_head
= head
;
1433 vfs_removename(const char *nameref
)
1438 i
= remove_name_locked(nameref
);
1439 name_cache_unlock();
1447 remove_name_locked(const char *nameref
)
1449 struct stringhead
*head
;
1454 hashval
= hash_string(nameref
, 0);
1455 head
= &string_ref_table
[hashval
& string_table_mask
];
1456 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
1457 if (entry
->str
== (unsigned char *)nameref
) {
1459 if (entry
->refcount
== 0) {
1460 LIST_REMOVE(entry
, hash_chain
);
1461 if (head
->lh_first
== NULL
) {
1468 FREE(entry
, M_TEMP
);
1482 dump_string_table(void)
1484 struct stringhead
*head
;
1489 for (i
= 0; i
<= string_table_mask
; i
++) {
1490 head
= &string_ref_table
[i
];
1491 for (entry
=head
->lh_first
; entry
!= NULL
; entry
=entry
->hash_chain
.le_next
) {
1492 printf("%6d - %s\n", entry
->refcount
, entry
->str
);
1495 name_cache_unlock();