1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)vfs_cache.c 7.9 (Berkeley) 01/22/92 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "time.h" 13 #include "mount.h" 14 #include "vnode.h" 15 #include "namei.h" 16 #include "errno.h" 17 #include "malloc.h" 18 19 /* 20 * Name caching works as follows: 21 * 22 * Names found by directory scans are retained in a cache 23 * for future reference. It is managed LRU, so frequently 24 * used names will hang around. Cache is indexed by hash value 25 * obtained from (vp, name) where vp refers to the directory 26 * containing name. 27 * 28 * For simplicity (and economy of storage), names longer than 29 * a maximum length of NCHNAMLEN are not cached; they occur 30 * infrequently in any case, and are almost never of interest. 31 * 32 * Upon reaching the last segment of a path, if the reference 33 * is for DELETE, or NOCACHE is set (rewrite), and the 34 * name is located in the cache, it will be dropped. 35 */ 36 37 /* 38 * Structures associated with name cacheing. 39 */ 40 union nchash { 41 union nchash *nch_head[2]; 42 struct namecache *nch_chain[2]; 43 } *nchashtbl; 44 #define nch_forw nch_chain[0] 45 #define nch_back nch_chain[1] 46 47 u_long nchash; /* size of hash table - 1 */ 48 long numcache; /* number of cache entries allocated */ 49 struct namecache *nchhead, **nchtail; /* LRU chain pointers */ 50 struct nchstats nchstats; /* cache effectiveness statistics */ 51 52 int doingcache = 1; /* 1 => enable the cache */ 53 54 /* 55 * Look for a the name in the cache. We don't do this 56 * if the segment name is long, simply so the cache can avoid 57 * holding long names (which would either waste space, or 58 * add greatly to the complexity). 59 * 60 * Lookup is called with ni_dvp pointing to the directory to search, 61 * ni_ptr pointing to the name of the entry being sought, ni_namelen 62 * tells the length of the name, and ni_hash contains a hash of 63 * the name. If the lookup succeeds, the vnode is returned in ni_vp 64 * and a status of -1 is returned. If the lookup determines that 65 * the name does not exist (negative cacheing), a status of ENOENT 66 * is returned. If the lookup fails, a status of zero is returned. 67 */ 68 int 69 cache_lookup(dvp, vpp, cnp) /* converted to CN. NEEDSWORK: do callers */ 70 /* old: cache_lookup(ndp) */ 71 struct vnode *dvp; 72 struct vnode **vpp; 73 struct componentname *cnp; 74 { 75 register struct namecache *ncp; 76 union nchash *nhp; 77 78 if (!doingcache) 79 return (0); 80 if (cnp->cn_namelen > NCHNAMLEN) { 81 nchstats.ncs_long++; 82 cnp->cn_flags &= ~MAKEENTRY; 83 return (0); 84 } 85 nhp = &nchashtbl[cnp->cn_hash & nchash]; 86 for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 87 ncp = ncp->nc_forw) { 88 if (ncp->nc_dvp == dvp && 89 ncp->nc_dvpid == dvp->v_id && 90 ncp->nc_nlen == cnp->cn_namelen && 91 !bcmp(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen)) 92 break; 93 } 94 if (ncp == (struct namecache *)nhp) { 95 nchstats.ncs_miss++; 96 return (0); 97 } 98 if (!(cnp->cn_flags & MAKEENTRY)) { 99 nchstats.ncs_badhits++; 100 } else if (ncp->nc_vp == NULL) { 101 if ((cnp->cn_nameiop & OPMASK) != CREATE) { 102 nchstats.ncs_neghits++; 103 /* 104 * Move this slot to end of LRU chain, 105 * if not already there. 106 */ 107 if (ncp->nc_nxt) { 108 /* remove from LRU chain */ 109 *ncp->nc_prev = ncp->nc_nxt; 110 ncp->nc_nxt->nc_prev = ncp->nc_prev; 111 /* and replace at end of it */ 112 ncp->nc_nxt = NULL; 113 ncp->nc_prev = nchtail; 114 *nchtail = ncp; 115 nchtail = &ncp->nc_nxt; 116 } 117 return (ENOENT); 118 } 119 } else if (ncp->nc_vpid != ncp->nc_vp->v_id) { 120 nchstats.ncs_falsehits++; 121 } else { 122 nchstats.ncs_goodhits++; 123 /* 124 * move this slot to end of LRU chain, if not already there 125 */ 126 if (ncp->nc_nxt) { 127 /* remove from LRU chain */ 128 *ncp->nc_prev = ncp->nc_nxt; 129 ncp->nc_nxt->nc_prev = ncp->nc_prev; 130 /* and replace at end of it */ 131 ncp->nc_nxt = NULL; 132 ncp->nc_prev = nchtail; 133 *nchtail = ncp; 134 nchtail = &ncp->nc_nxt; 135 } 136 *vpp = ncp->nc_vp; 137 return (-1); 138 } 139 140 /* 141 * Last component and we are renaming or deleting, 142 * the cache entry is invalid, or otherwise don't 143 * want cache entry to exist. 144 */ 145 /* remove from LRU chain */ 146 *ncp->nc_prev = ncp->nc_nxt; 147 if (ncp->nc_nxt) 148 ncp->nc_nxt->nc_prev = ncp->nc_prev; 149 else 150 nchtail = ncp->nc_prev; 151 /* remove from hash chain */ 152 remque(ncp); 153 /* insert at head of LRU list (first to grab) */ 154 ncp->nc_nxt = nchhead; 155 ncp->nc_prev = &nchhead; 156 nchhead->nc_prev = &ncp->nc_nxt; 157 nchhead = ncp; 158 /* and make a dummy hash chain */ 159 ncp->nc_forw = ncp; 160 ncp->nc_back = ncp; 161 return (0); 162 } 163 164 /* 165 * Add an entry to the cache 166 */ 167 cache_enter(dvp, vp, cnp) /* converted to CN. NEEDSWORK: do callers */ 168 /* old: cache_lookup(ndp) */ 169 struct vnode *dvp; 170 struct vnode *vp; 171 struct componentname *cnp; 172 { 173 register struct namecache *ncp; 174 union nchash *nhp; 175 176 if (!doingcache) 177 return; 178 /* 179 * Free the cache slot at head of lru chain. 180 */ 181 if (numcache < desiredvnodes) { 182 ncp = (struct namecache *) 183 malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK); 184 bzero((char *)ncp, sizeof *ncp); 185 numcache++; 186 } else if (ncp = nchhead) { 187 /* remove from lru chain */ 188 *ncp->nc_prev = ncp->nc_nxt; 189 if (ncp->nc_nxt) 190 ncp->nc_nxt->nc_prev = ncp->nc_prev; 191 else 192 nchtail = ncp->nc_prev; 193 /* remove from old hash chain */ 194 remque(ncp); 195 } else 196 return; 197 /* grab the vnode we just found */ 198 ncp->nc_vp = vp; 199 if (vp) 200 ncp->nc_vpid = vp->v_id; 201 else 202 ncp->nc_vpid = 0; 203 /* fill in cache info */ 204 ncp->nc_dvp = dvp; 205 ncp->nc_dvpid = dvp->v_id; 206 ncp->nc_nlen = cnp->cn_namelen; 207 bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen); 208 /* link at end of lru chain */ 209 ncp->nc_nxt = NULL; 210 ncp->nc_prev = nchtail; 211 *nchtail = ncp; 212 nchtail = &ncp->nc_nxt; 213 /* and insert on hash chain */ 214 nhp = &nchashtbl[cnp->cn_hash & nchash]; 215 insque(ncp, nhp); 216 } 217 218 /* 219 * Name cache initialization, from vfs_init() when we are booting 220 */ 221 nchinit() 222 { 223 register union nchash *nchp; 224 long nchashsize; 225 226 nchhead = 0; 227 nchtail = &nchhead; 228 nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2, 229 NBPG * CLSIZE); 230 nchashtbl = (union nchash *)malloc((u_long)nchashsize, 231 M_CACHE, M_WAITOK); 232 for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1) 233 /* void */; 234 nchash = (nchash >> 1) - 1; 235 for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) { 236 nchp->nch_head[0] = nchp; 237 nchp->nch_head[1] = nchp; 238 } 239 } 240 241 /* 242 * Cache flush, a particular vnode; called when a vnode is renamed to 243 * hide entries that would now be invalid 244 */ 245 cache_purge(vp) 246 struct vnode *vp; 247 { 248 union nchash *nhp; 249 struct namecache *ncp; 250 251 vp->v_id = ++nextvnodeid; 252 if (nextvnodeid != 0) 253 return; 254 for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) { 255 for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 256 ncp = ncp->nc_forw) { 257 ncp->nc_vpid = 0; 258 ncp->nc_dvpid = 0; 259 } 260 } 261 vp->v_id = ++nextvnodeid; 262 } 263 264 /* 265 * Cache flush, a whole filesystem; called when filesys is umounted to 266 * remove entries that would now be invalid 267 * 268 * The line "nxtcp = nchhead" near the end is to avoid potential problems 269 * if the cache lru chain is modified while we are dumping the 270 * inode. This makes the algorithm O(n^2), but do you think I care? 271 */ 272 cache_purgevfs(mp) 273 struct mount *mp; 274 { 275 register struct namecache *ncp, *nxtcp; 276 277 for (ncp = nchhead; ncp; ncp = nxtcp) { 278 nxtcp = ncp->nc_nxt; 279 if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) 280 continue; 281 /* free the resources we had */ 282 ncp->nc_vp = NULL; 283 ncp->nc_dvp = NULL; 284 remque(ncp); /* remove entry from its hash chain */ 285 ncp->nc_forw = ncp; /* and make a dummy one */ 286 ncp->nc_back = ncp; 287 /* delete this entry from LRU chain */ 288 *ncp->nc_prev = nxtcp; 289 if (nxtcp) 290 nxtcp->nc_prev = ncp->nc_prev; 291 else 292 nchtail = ncp->nc_prev; 293 /* cause rescan of list, it may have altered */ 294 nxtcp = nchhead; 295 /* put the now-free entry at head of LRU */ 296 ncp->nc_nxt = nxtcp; 297 ncp->nc_prev = &nchhead; 298 nxtcp->nc_prev = &ncp->nc_nxt; 299 nchhead = ncp; 300 } 301 } 302