1 .\" $NetBSD: namecache.9,v 1.12 2008/04/30 13:10:58 martin Exp $
3 .\" Copyright (c) 2001 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Gregory McGarry.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
48 .Fn cache_lookup "struct vnode *dvp" "struct vnode **vpp" \
49 "struct componentname *cnp"
51 .Fn cache_revlookup "struct vnode *vp" "struct vnode *dvp" \
52 "char **bpp" "char *bufp"
54 .Fn cache_enter "struct vnode *dvp" "struct vnode *vp" \
55 "struct componentname *cnp"
57 .Fn cache_purge "struct vnode *vp"
59 .Fn cache_purgevfs "struct mount *mp"
61 .Fn namecache_print "struct vnode *vp" "void (*func)(const char *, ...)"
63 The name lookup cache is the mechanism to allow the file system type
64 dependent algorithms to quickly resolve a file's vnode from its
66 The name lookup cache is generally accessed through the higher-level
70 The name of the file is used to lookup an entry associated with the
71 file in the name lookup cache.
72 If no entry is found, one is created for it.
73 If an entry is found, the information obtained from the cache lookup
74 contains information about the file which is useful to the file system
75 type dependent functions.
77 The name lookup cache is managed by a least recently used (LRU)
78 algorithm so frequently used names will hang around.
79 The cache itself is a hash table called
83 entries that are allocated dynamically from a kernel memory pool.
84 Each entry has the following structure:
86 #define NCHNAMLEN 31 /* maximum name segment length */
88 LIST_ENTRY(namecache) nc_hash; /* hash chain */
89 TAILQ_ENTRY(namecache) nc_lru; /* LRU chain */
90 LIST_ENTRY(namecache) nc_vhash; /* directory hash chain */
91 LIST_ENTRY(namecache) nc_dvlist;
92 struct vnode *nc_dvp; /* vnode of parent of name */
93 LIST_ENTRY(namecache) nc_vlist;
94 struct vnode *nc_vp; /* vnode the name refers to */
95 int nc_flags; /* copy of componentname's ISWHITEOUT */
96 char nc_nlen; /* length of name */
97 char nc_name[NCHNAMLEN]; /* segment name */
101 For simplicity (and economy of storage), names longer than a maximum
102 length of NCHNAMLEN are not cached; they occur infrequently in any
103 case, and are almost never of interest.
107 entry can appear on two hash chains in addition to
110 (the name cache directory hash chain), and
112 (the name cache LRU chain).
113 The hash chains are indexed by a hash value obtained from the file's
114 name and the address of its parent directory vnode.
116 Functions which access to the name cache pass arguments in the
117 partially initialised
122 for details on this structure.
124 .Bl -tag -width compact
125 .It Fn cache_lookup "dvp" "vpp" "cnp"
126 Look for a name in the cache.
130 pointing to the vnode of the directory to search and
132 pointing to the partially initialised component structure.
133 .Fa cnp-\*[Gt]cn_nameptr
134 points to the name of the entry being sought,
135 .Fa cnp-\*[Gt]cn_namelen
136 tells the length of the name, and
137 .Fa cnp-\*[Gt]cn_hash
138 contains a hash of the name.
139 If the lookup succeeds, the vnode is locked, stored in
141 and a status of zero is returned.
142 If the locking fails for whatever reason, the vnode is unlocked and the
144 If the lookup determines that the name does not exist any longer, a
145 status of ENOENT is returned.
146 If the lookup fails, a status of -1 is returned.
147 .It Fn cache_revlookup "vp" "dvp" "bpp" "bufp"
148 Scan cache looking for name of directory entry pointing at
156 also place the name in the buffer which starts at
160 and move bpp backwards to point at the start of it.
161 Returns 0 on success, -1 on cache miss, positive errno on failure.
162 .It Fn cache_enter "dvp" "vp" "cnp"
163 Add an entry to the cache.
167 pointing to the vnode of the directory to enter and
169 pointing to the partially initialised component structure.
174 a negative cache entry is created, specifying that the entry
175 does not exist in the file system.
176 .Fa cnp-\*[Gt]cn_nameptr
177 points to the name of the entry being entered,
178 .Fa cnp-\*[Gt]cn_namelen
179 tells the length of the name, and
180 .Fa cnp-\*[Gt]cn_hash
181 contains a hash of the name.
182 .It Fn cache_purge "vp"
183 Flush the cache of a particular vnode
186 is called when a vnode is renamed to hide entries that would now be
188 .It Fn cache_purgevfs "mp"
189 Flush cache of a whole file system
192 is called when file system is unmounted to remove entries that would
194 .It Fn namecache_print "vp" "func"
195 Print all entries of the name cache.
201 is only defined if the kernel option DDB is compiled into the kernel.
204 This section describes places within the
206 source tree where actual code implementing or using the name
207 lookup cache can be found.
208 All pathnames are relative to
211 The name lookup cache is implemented within the file
212 .Pa sys/kern/vfs_cache.c .
219 The name lookup cache first appeared in
222 The original name lookup cache was written by Robert Elz.