137487Smckusick /* 237487Smckusick * Copyright (c) 1989 The Regents of the University of California. 337487Smckusick * All rights reserved. 437487Smckusick * 537487Smckusick * Redistribution and use in source and binary forms are permitted 637487Smckusick * provided that the above copyright notice and this paragraph are 737487Smckusick * duplicated in all such forms and that any documentation, 837487Smckusick * advertising materials, and other materials related to such 937487Smckusick * distribution and use acknowledge that the software was developed 1037487Smckusick * by the University of California, Berkeley. The name of the 1137487Smckusick * University may not be used to endorse or promote products derived 1237487Smckusick * from this software without specific prior written permission. 1337487Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437487Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537487Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637487Smckusick * 17*40881Smckusick * @(#)vfs_cache.c 7.5 (Berkeley) 04/10/90 1837487Smckusick */ 1937487Smckusick 2037487Smckusick #include "param.h" 2137487Smckusick #include "systm.h" 2237487Smckusick #include "time.h" 2337487Smckusick #include "vnode.h" 2437487Smckusick #include "namei.h" 2538768Smckusick #include "errno.h" 26*40881Smckusick #include "malloc.h" 2737487Smckusick 2837487Smckusick /* 2937487Smckusick * Name caching works as follows: 3037487Smckusick * 3137487Smckusick * Names found by directory scans are retained in a cache 3237487Smckusick * for future reference. It is managed LRU, so frequently 3337487Smckusick * used names will hang around. Cache is indexed by hash value 3438768Smckusick * obtained from (vp, name) where vp refers to the directory 3538768Smckusick * containing name. 3637487Smckusick * 3737487Smckusick * For simplicity (and economy of storage), names longer than 3837487Smckusick * a maximum length of NCHNAMLEN are not cached; they occur 3937487Smckusick * infrequently in any case, and are almost never of interest. 4037487Smckusick * 4137487Smckusick * Upon reaching the last segment of a path, if the reference 4237487Smckusick * is for DELETE, or NOCACHE is set (rewrite), and the 4337487Smckusick * name is located in the cache, it will be dropped. 4437487Smckusick */ 4537487Smckusick 4637487Smckusick /* 4737487Smckusick * Structures associated with name cacheing. 4837487Smckusick */ 4937487Smckusick union nchash { 5037487Smckusick union nchash *nch_head[2]; 5137487Smckusick struct namecache *nch_chain[2]; 52*40881Smckusick } *nchashtbl; 5337487Smckusick #define nch_forw nch_chain[0] 5437487Smckusick #define nch_back nch_chain[1] 5537487Smckusick 56*40881Smckusick u_long nchash; /* size of hash table - 1 */ 57*40881Smckusick long numcache; /* number of cache entries allocated */ 5837487Smckusick struct namecache *nchhead, **nchtail; /* LRU chain pointers */ 5937487Smckusick struct nchstats nchstats; /* cache effectiveness statistics */ 6037487Smckusick 6138768Smckusick int doingcache = 1; /* 1 => enable the cache */ 6238768Smckusick 6337487Smckusick /* 6437487Smckusick * Look for a the name in the cache. We don't do this 6537487Smckusick * if the segment name is long, simply so the cache can avoid 6637487Smckusick * holding long names (which would either waste space, or 6737487Smckusick * add greatly to the complexity). 6838768Smckusick * 6938768Smckusick * Lookup is called with ni_dvp pointing to the directory to search, 7038768Smckusick * ni_ptr pointing to the name of the entry being sought, ni_namelen 7138768Smckusick * tells the length of the name, and ni_hash contains a hash of 7238768Smckusick * the name. If the lookup succeeds, the vnode is returned in ni_vp 7338768Smckusick * and a status of -1 is returned. If the lookup determines that 7438768Smckusick * the name does not exist (negative cacheing), a status of ENOENT 7538768Smckusick * is returned. If the lookup fails, a status of zero is returned. 7637487Smckusick */ 7737487Smckusick cache_lookup(ndp) 7837487Smckusick register struct nameidata *ndp; 7937487Smckusick { 8038768Smckusick register struct vnode *dvp; 8137487Smckusick register struct namecache *ncp; 8237487Smckusick union nchash *nhp; 8337487Smckusick 8438768Smckusick if (!doingcache) 8538768Smckusick return (0); 8638768Smckusick if (ndp->ni_namelen > NCHNAMLEN) { 8737487Smckusick nchstats.ncs_long++; 8837487Smckusick ndp->ni_makeentry = 0; 8937487Smckusick return (0); 9037487Smckusick } 9138768Smckusick dvp = ndp->ni_dvp; 92*40881Smckusick nhp = &nchashtbl[ndp->ni_hash & nchash]; 9337487Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 9437487Smckusick ncp = ncp->nc_forw) { 9538768Smckusick if (ncp->nc_dvp == dvp && 9638768Smckusick ncp->nc_dvpid == dvp->v_id && 9738768Smckusick ncp->nc_nlen == ndp->ni_namelen && 9838768Smckusick !bcmp(ncp->nc_name, ndp->ni_ptr, (unsigned)ncp->nc_nlen)) 9937487Smckusick break; 10037487Smckusick } 10137487Smckusick if (ncp == (struct namecache *)nhp) { 10237487Smckusick nchstats.ncs_miss++; 10337487Smckusick return (0); 10437487Smckusick } 10538768Smckusick if (!ndp->ni_makeentry) { 10638768Smckusick nchstats.ncs_badhits++; 10738768Smckusick } else if (ncp->nc_vp == NULL) { 10838768Smckusick nchstats.ncs_neghits++; 10937487Smckusick /* 11038768Smckusick * move this slot to end of LRU chain, if not already there 11137487Smckusick */ 11237487Smckusick if (ncp->nc_nxt) { 11337487Smckusick /* remove from LRU chain */ 11437487Smckusick *ncp->nc_prev = ncp->nc_nxt; 11537487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 11637487Smckusick /* and replace at end of it */ 11737487Smckusick ncp->nc_nxt = NULL; 11837487Smckusick ncp->nc_prev = nchtail; 11937487Smckusick *nchtail = ncp; 12037487Smckusick nchtail = &ncp->nc_nxt; 12137487Smckusick } 12238768Smckusick return (ENOENT); 12338768Smckusick } else if (ncp->nc_vpid != ncp->nc_vp->v_id) { 12438768Smckusick nchstats.ncs_falsehits++; 12538768Smckusick } else { 12637487Smckusick nchstats.ncs_goodhits++; 12738768Smckusick /* 12838768Smckusick * move this slot to end of LRU chain, if not already there 12938768Smckusick */ 13038768Smckusick if (ncp->nc_nxt) { 13138768Smckusick /* remove from LRU chain */ 13238768Smckusick *ncp->nc_prev = ncp->nc_nxt; 13338768Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 13438768Smckusick /* and replace at end of it */ 13538768Smckusick ncp->nc_nxt = NULL; 13638768Smckusick ncp->nc_prev = nchtail; 13738768Smckusick *nchtail = ncp; 13838768Smckusick nchtail = &ncp->nc_nxt; 13938768Smckusick } 14038768Smckusick ndp->ni_vp = ncp->nc_vp; 14138768Smckusick return (-1); 14237487Smckusick } 14337487Smckusick 14437487Smckusick /* 14537487Smckusick * Last component and we are renaming or deleting, 14637487Smckusick * the cache entry is invalid, or otherwise don't 14737487Smckusick * want cache entry to exist. 14837487Smckusick */ 14937487Smckusick /* remove from LRU chain */ 15037487Smckusick *ncp->nc_prev = ncp->nc_nxt; 15137487Smckusick if (ncp->nc_nxt) 15237487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 15337487Smckusick else 15437487Smckusick nchtail = ncp->nc_prev; 15538768Smckusick /* remove from hash chain */ 15638768Smckusick remque(ncp); 15737487Smckusick /* insert at head of LRU list (first to grab) */ 15837487Smckusick ncp->nc_nxt = nchhead; 15937487Smckusick ncp->nc_prev = &nchhead; 16037487Smckusick nchhead->nc_prev = &ncp->nc_nxt; 16137487Smckusick nchhead = ncp; 16237487Smckusick /* and make a dummy hash chain */ 16337487Smckusick ncp->nc_forw = ncp; 16437487Smckusick ncp->nc_back = ncp; 16537487Smckusick return (0); 16637487Smckusick } 16737487Smckusick 16837487Smckusick /* 16937487Smckusick * Add an entry to the cache 17037487Smckusick */ 17137487Smckusick cache_enter(ndp) 17237487Smckusick register struct nameidata *ndp; 17337487Smckusick { 17437487Smckusick register struct namecache *ncp; 17537487Smckusick union nchash *nhp; 17637487Smckusick 17738768Smckusick if (!doingcache) 17838768Smckusick return; 17937487Smckusick /* 18037487Smckusick * Free the cache slot at head of lru chain. 18137487Smckusick */ 182*40881Smckusick if (numcache < desiredvnodes) { 183*40881Smckusick ncp = (struct namecache *) 184*40881Smckusick malloc(sizeof *ncp, M_CACHE, M_WAITOK); 185*40881Smckusick bzero((char *)ncp, sizeof *ncp); 186*40881Smckusick numcache++; 187*40881Smckusick } else if (ncp = nchhead) { 18837487Smckusick /* remove from lru chain */ 18937487Smckusick *ncp->nc_prev = ncp->nc_nxt; 19037487Smckusick if (ncp->nc_nxt) 19137487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 19237487Smckusick else 19337487Smckusick nchtail = ncp->nc_prev; 19438768Smckusick /* remove from old hash chain */ 19538768Smckusick remque(ncp); 196*40881Smckusick } else 197*40881Smckusick return; 198*40881Smckusick /* grab the vnode we just found */ 199*40881Smckusick ncp->nc_vp = ndp->ni_vp; 200*40881Smckusick if (ndp->ni_vp) 201*40881Smckusick ncp->nc_vpid = ndp->ni_vp->v_id; 202*40881Smckusick else 203*40881Smckusick ncp->nc_vpid = 0; 204*40881Smckusick /* fill in cache info */ 205*40881Smckusick ncp->nc_dvp = ndp->ni_dvp; 206*40881Smckusick ncp->nc_dvpid = ndp->ni_dvp->v_id; 207*40881Smckusick ncp->nc_nlen = ndp->ni_namelen; 208*40881Smckusick bcopy(ndp->ni_ptr, ncp->nc_name, (unsigned)ncp->nc_nlen); 209*40881Smckusick /* link at end of lru chain */ 210*40881Smckusick ncp->nc_nxt = NULL; 211*40881Smckusick ncp->nc_prev = nchtail; 212*40881Smckusick *nchtail = ncp; 213*40881Smckusick nchtail = &ncp->nc_nxt; 214*40881Smckusick /* and insert on hash chain */ 215*40881Smckusick nhp = &nchashtbl[ndp->ni_hash & nchash]; 216*40881Smckusick insque(ncp, nhp); 21737487Smckusick } 21837487Smckusick 21937487Smckusick /* 220*40881Smckusick * Name cache initialization, from vfs_init() when we are booting 22137487Smckusick */ 22237487Smckusick nchinit() 22337487Smckusick { 22437487Smckusick register union nchash *nchp; 225*40881Smckusick long nchashsize; 22637487Smckusick 22737487Smckusick nchhead = 0; 22837487Smckusick nchtail = &nchhead; 229*40881Smckusick nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2, 230*40881Smckusick NBPG * CLSIZE); 231*40881Smckusick nchashtbl = (union nchash *)malloc(nchashsize, M_CACHE, M_WAITOK); 232*40881Smckusick for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1) 233*40881Smckusick /* void */; 234*40881Smckusick nchash = (nchash >> 1) - 1; 235*40881Smckusick for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) { 23637487Smckusick nchp->nch_head[0] = nchp; 23737487Smckusick nchp->nch_head[1] = nchp; 23837487Smckusick } 23937487Smckusick } 24037487Smckusick 24137487Smckusick /* 24237487Smckusick * Cache flush, a particular vnode; called when a vnode is renamed to 24338768Smckusick * hide entries that would now be invalid 24437487Smckusick */ 24537487Smckusick cache_purge(vp) 24638768Smckusick struct vnode *vp; 24737487Smckusick { 248*40881Smckusick union nchash *nhp; 24938768Smckusick struct namecache *ncp; 25037487Smckusick 25138768Smckusick vp->v_id = ++nextvnodeid; 25238768Smckusick if (nextvnodeid != 0) 25338768Smckusick return; 254*40881Smckusick for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) { 255*40881Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 256*40881Smckusick ncp = ncp->nc_forw) { 257*40881Smckusick ncp->nc_vpid = 0; 258*40881Smckusick ncp->nc_dvpid = 0; 259*40881Smckusick } 26037487Smckusick } 26138768Smckusick vp->v_id = ++nextvnodeid; 26237487Smckusick } 26337487Smckusick 26437487Smckusick /* 26537487Smckusick * Cache flush, a whole filesystem; called when filesys is umounted to 26637487Smckusick * remove entries that would now be invalid 26737487Smckusick * 26837487Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 26937487Smckusick * if the cache lru chain is modified while we are dumping the 27037487Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 27137487Smckusick */ 27237487Smckusick cache_purgevfs(mp) 27337487Smckusick register struct mount *mp; 27437487Smckusick { 27537487Smckusick register struct namecache *ncp, *nxtcp; 27637487Smckusick 27737487Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 27837487Smckusick nxtcp = ncp->nc_nxt; 27938768Smckusick if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) 28037487Smckusick continue; 28137487Smckusick /* free the resources we had */ 28237487Smckusick ncp->nc_vp = NULL; 28338768Smckusick ncp->nc_dvp = NULL; 28437487Smckusick remque(ncp); /* remove entry from its hash chain */ 28537487Smckusick ncp->nc_forw = ncp; /* and make a dummy one */ 28637487Smckusick ncp->nc_back = ncp; 28737487Smckusick /* delete this entry from LRU chain */ 28837487Smckusick *ncp->nc_prev = nxtcp; 28937487Smckusick if (nxtcp) 29037487Smckusick nxtcp->nc_prev = ncp->nc_prev; 29137487Smckusick else 29237487Smckusick nchtail = ncp->nc_prev; 29337487Smckusick /* cause rescan of list, it may have altered */ 29437487Smckusick nxtcp = nchhead; 29537487Smckusick /* put the now-free entry at head of LRU */ 29637487Smckusick ncp->nc_nxt = nxtcp; 29737487Smckusick ncp->nc_prev = &nchhead; 29837487Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 29937487Smckusick nchhead = ncp; 30037487Smckusick } 30137487Smckusick } 302