137487Smckusick /* 237487Smckusick * Copyright (c) 1989 The Regents of the University of California. 337487Smckusick * All rights reserved. 437487Smckusick * 544455Sbostic * %sccs.include.redist.c% 637487Smckusick * 7*52306Sheideman * @(#)vfs_cache.c 7.10 (Berkeley) 02/03/92 837487Smckusick */ 937487Smckusick 1037487Smckusick #include "param.h" 1137487Smckusick #include "systm.h" 1237487Smckusick #include "time.h" 1345117Smckusick #include "mount.h" 1437487Smckusick #include "vnode.h" 1537487Smckusick #include "namei.h" 1638768Smckusick #include "errno.h" 1740881Smckusick #include "malloc.h" 1837487Smckusick 1937487Smckusick /* 2037487Smckusick * Name caching works as follows: 2137487Smckusick * 2237487Smckusick * Names found by directory scans are retained in a cache 2337487Smckusick * for future reference. It is managed LRU, so frequently 2437487Smckusick * used names will hang around. Cache is indexed by hash value 2538768Smckusick * obtained from (vp, name) where vp refers to the directory 2638768Smckusick * containing name. 2737487Smckusick * 2837487Smckusick * For simplicity (and economy of storage), names longer than 2937487Smckusick * a maximum length of NCHNAMLEN are not cached; they occur 3037487Smckusick * infrequently in any case, and are almost never of interest. 3137487Smckusick * 3237487Smckusick * Upon reaching the last segment of a path, if the reference 3337487Smckusick * is for DELETE, or NOCACHE is set (rewrite), and the 3437487Smckusick * name is located in the cache, it will be dropped. 3537487Smckusick */ 3637487Smckusick 3737487Smckusick /* 3837487Smckusick * Structures associated with name cacheing. 3937487Smckusick */ 4037487Smckusick union nchash { 4137487Smckusick union nchash *nch_head[2]; 4237487Smckusick struct namecache *nch_chain[2]; 4340881Smckusick } *nchashtbl; 4437487Smckusick #define nch_forw nch_chain[0] 4537487Smckusick #define nch_back nch_chain[1] 4637487Smckusick 4740881Smckusick u_long nchash; /* size of hash table - 1 */ 4840881Smckusick long numcache; /* number of cache entries allocated */ 4937487Smckusick struct namecache *nchhead, **nchtail; /* LRU chain pointers */ 5037487Smckusick struct nchstats nchstats; /* cache effectiveness statistics */ 5137487Smckusick 5238768Smckusick int doingcache = 1; /* 1 => enable the cache */ 5338768Smckusick 5437487Smckusick /* 5537487Smckusick * Look for a the name in the cache. We don't do this 5637487Smckusick * if the segment name is long, simply so the cache can avoid 5737487Smckusick * holding long names (which would either waste space, or 5837487Smckusick * add greatly to the complexity). 5938768Smckusick * 6038768Smckusick * Lookup is called with ni_dvp pointing to the directory to search, 6138768Smckusick * ni_ptr pointing to the name of the entry being sought, ni_namelen 6238768Smckusick * tells the length of the name, and ni_hash contains a hash of 6338768Smckusick * the name. If the lookup succeeds, the vnode is returned in ni_vp 6438768Smckusick * and a status of -1 is returned. If the lookup determines that 6538768Smckusick * the name does not exist (negative cacheing), a status of ENOENT 6638768Smckusick * is returned. If the lookup fails, a status of zero is returned. 6737487Smckusick */ 6852231Sheideman int 69*52306Sheideman cache_lookup(dvp, vpp, cnp) 7052231Sheideman struct vnode *dvp; 7152231Sheideman struct vnode **vpp; 7252231Sheideman struct componentname *cnp; 7337487Smckusick { 7437487Smckusick register struct namecache *ncp; 7537487Smckusick union nchash *nhp; 7637487Smckusick 7738768Smckusick if (!doingcache) 7838768Smckusick return (0); 7952231Sheideman if (cnp->cn_namelen > NCHNAMLEN) { 8037487Smckusick nchstats.ncs_long++; 8152231Sheideman cnp->cn_flags &= ~MAKEENTRY; 8237487Smckusick return (0); 8337487Smckusick } 8452231Sheideman nhp = &nchashtbl[cnp->cn_hash & nchash]; 8537487Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 8637487Smckusick ncp = ncp->nc_forw) { 8738768Smckusick if (ncp->nc_dvp == dvp && 8838768Smckusick ncp->nc_dvpid == dvp->v_id && 8952231Sheideman ncp->nc_nlen == cnp->cn_namelen && 9052231Sheideman !bcmp(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen)) 9137487Smckusick break; 9237487Smckusick } 9337487Smckusick if (ncp == (struct namecache *)nhp) { 9437487Smckusick nchstats.ncs_miss++; 9537487Smckusick return (0); 9637487Smckusick } 9752231Sheideman if (!(cnp->cn_flags & MAKEENTRY)) { 9838768Smckusick nchstats.ncs_badhits++; 9938768Smckusick } else if (ncp->nc_vp == NULL) { 100*52306Sheideman if (cnp->cn_nameiop != CREATE) { 10146747Smckusick nchstats.ncs_neghits++; 10246747Smckusick /* 10346747Smckusick * Move this slot to end of LRU chain, 10446747Smckusick * if not already there. 10546747Smckusick */ 10646747Smckusick if (ncp->nc_nxt) { 10746747Smckusick /* remove from LRU chain */ 10846747Smckusick *ncp->nc_prev = ncp->nc_nxt; 10946747Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 11046747Smckusick /* and replace at end of it */ 11146747Smckusick ncp->nc_nxt = NULL; 11246747Smckusick ncp->nc_prev = nchtail; 11346747Smckusick *nchtail = ncp; 11446747Smckusick nchtail = &ncp->nc_nxt; 11546747Smckusick } 11646747Smckusick return (ENOENT); 11737487Smckusick } 11838768Smckusick } else if (ncp->nc_vpid != ncp->nc_vp->v_id) { 11938768Smckusick nchstats.ncs_falsehits++; 12038768Smckusick } else { 12137487Smckusick nchstats.ncs_goodhits++; 12238768Smckusick /* 12338768Smckusick * move this slot to end of LRU chain, if not already there 12438768Smckusick */ 12538768Smckusick if (ncp->nc_nxt) { 12638768Smckusick /* remove from LRU chain */ 12738768Smckusick *ncp->nc_prev = ncp->nc_nxt; 12838768Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 12938768Smckusick /* and replace at end of it */ 13038768Smckusick ncp->nc_nxt = NULL; 13138768Smckusick ncp->nc_prev = nchtail; 13238768Smckusick *nchtail = ncp; 13338768Smckusick nchtail = &ncp->nc_nxt; 13438768Smckusick } 13552231Sheideman *vpp = ncp->nc_vp; 13638768Smckusick return (-1); 13737487Smckusick } 13837487Smckusick 13937487Smckusick /* 14037487Smckusick * Last component and we are renaming or deleting, 14137487Smckusick * the cache entry is invalid, or otherwise don't 14237487Smckusick * want cache entry to exist. 14337487Smckusick */ 14437487Smckusick /* remove from LRU chain */ 14537487Smckusick *ncp->nc_prev = ncp->nc_nxt; 14637487Smckusick if (ncp->nc_nxt) 14737487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 14837487Smckusick else 14937487Smckusick nchtail = ncp->nc_prev; 15038768Smckusick /* remove from hash chain */ 15138768Smckusick remque(ncp); 15237487Smckusick /* insert at head of LRU list (first to grab) */ 15337487Smckusick ncp->nc_nxt = nchhead; 15437487Smckusick ncp->nc_prev = &nchhead; 15537487Smckusick nchhead->nc_prev = &ncp->nc_nxt; 15637487Smckusick nchhead = ncp; 15737487Smckusick /* and make a dummy hash chain */ 15837487Smckusick ncp->nc_forw = ncp; 15937487Smckusick ncp->nc_back = ncp; 16037487Smckusick return (0); 16137487Smckusick } 16237487Smckusick 16337487Smckusick /* 16437487Smckusick * Add an entry to the cache 16537487Smckusick */ 166*52306Sheideman cache_enter(dvp, vp, cnp) 16752231Sheideman struct vnode *dvp; 16852231Sheideman struct vnode *vp; 16952231Sheideman struct componentname *cnp; 17037487Smckusick { 17137487Smckusick register struct namecache *ncp; 17237487Smckusick union nchash *nhp; 17337487Smckusick 17438768Smckusick if (!doingcache) 17538768Smckusick return; 17637487Smckusick /* 17737487Smckusick * Free the cache slot at head of lru chain. 17837487Smckusick */ 17940881Smckusick if (numcache < desiredvnodes) { 18040881Smckusick ncp = (struct namecache *) 18145117Smckusick malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK); 18240881Smckusick bzero((char *)ncp, sizeof *ncp); 18340881Smckusick numcache++; 18440881Smckusick } else if (ncp = nchhead) { 18537487Smckusick /* remove from lru chain */ 18637487Smckusick *ncp->nc_prev = ncp->nc_nxt; 18737487Smckusick if (ncp->nc_nxt) 18837487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 18937487Smckusick else 19037487Smckusick nchtail = ncp->nc_prev; 19138768Smckusick /* remove from old hash chain */ 19238768Smckusick remque(ncp); 19340881Smckusick } else 19440881Smckusick return; 19540881Smckusick /* grab the vnode we just found */ 19652231Sheideman ncp->nc_vp = vp; 19752231Sheideman if (vp) 19852231Sheideman ncp->nc_vpid = vp->v_id; 19940881Smckusick else 20040881Smckusick ncp->nc_vpid = 0; 20140881Smckusick /* fill in cache info */ 20252231Sheideman ncp->nc_dvp = dvp; 20352231Sheideman ncp->nc_dvpid = dvp->v_id; 20452231Sheideman ncp->nc_nlen = cnp->cn_namelen; 20552231Sheideman bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen); 20640881Smckusick /* link at end of lru chain */ 20740881Smckusick ncp->nc_nxt = NULL; 20840881Smckusick ncp->nc_prev = nchtail; 20940881Smckusick *nchtail = ncp; 21040881Smckusick nchtail = &ncp->nc_nxt; 21140881Smckusick /* and insert on hash chain */ 21252231Sheideman nhp = &nchashtbl[cnp->cn_hash & nchash]; 21340881Smckusick insque(ncp, nhp); 21437487Smckusick } 21537487Smckusick 21637487Smckusick /* 21740881Smckusick * Name cache initialization, from vfs_init() when we are booting 21837487Smckusick */ 21937487Smckusick nchinit() 22037487Smckusick { 22137487Smckusick register union nchash *nchp; 22240881Smckusick long nchashsize; 22337487Smckusick 22437487Smckusick nchhead = 0; 22537487Smckusick nchtail = &nchhead; 22640881Smckusick nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2, 22740881Smckusick NBPG * CLSIZE); 22845117Smckusick nchashtbl = (union nchash *)malloc((u_long)nchashsize, 22945117Smckusick M_CACHE, M_WAITOK); 23040881Smckusick for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1) 23140881Smckusick /* void */; 23240881Smckusick nchash = (nchash >> 1) - 1; 23340881Smckusick for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) { 23437487Smckusick nchp->nch_head[0] = nchp; 23537487Smckusick nchp->nch_head[1] = nchp; 23637487Smckusick } 23737487Smckusick } 23837487Smckusick 23937487Smckusick /* 24037487Smckusick * Cache flush, a particular vnode; called when a vnode is renamed to 24138768Smckusick * hide entries that would now be invalid 24237487Smckusick */ 24337487Smckusick cache_purge(vp) 24438768Smckusick struct vnode *vp; 24537487Smckusick { 24640881Smckusick union nchash *nhp; 24738768Smckusick struct namecache *ncp; 24837487Smckusick 24938768Smckusick vp->v_id = ++nextvnodeid; 25038768Smckusick if (nextvnodeid != 0) 25138768Smckusick return; 25240881Smckusick for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) { 25340881Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 25440881Smckusick ncp = ncp->nc_forw) { 25540881Smckusick ncp->nc_vpid = 0; 25640881Smckusick ncp->nc_dvpid = 0; 25740881Smckusick } 25837487Smckusick } 25938768Smckusick vp->v_id = ++nextvnodeid; 26037487Smckusick } 26137487Smckusick 26237487Smckusick /* 26337487Smckusick * Cache flush, a whole filesystem; called when filesys is umounted to 26437487Smckusick * remove entries that would now be invalid 26537487Smckusick * 26637487Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 26737487Smckusick * if the cache lru chain is modified while we are dumping the 26837487Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 26937487Smckusick */ 27037487Smckusick cache_purgevfs(mp) 27145117Smckusick struct mount *mp; 27237487Smckusick { 27337487Smckusick register struct namecache *ncp, *nxtcp; 27437487Smckusick 27537487Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 27637487Smckusick nxtcp = ncp->nc_nxt; 27738768Smckusick if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) 27837487Smckusick continue; 27937487Smckusick /* free the resources we had */ 28037487Smckusick ncp->nc_vp = NULL; 28138768Smckusick ncp->nc_dvp = NULL; 28237487Smckusick remque(ncp); /* remove entry from its hash chain */ 28337487Smckusick ncp->nc_forw = ncp; /* and make a dummy one */ 28437487Smckusick ncp->nc_back = ncp; 28537487Smckusick /* delete this entry from LRU chain */ 28637487Smckusick *ncp->nc_prev = nxtcp; 28737487Smckusick if (nxtcp) 28837487Smckusick nxtcp->nc_prev = ncp->nc_prev; 28937487Smckusick else 29037487Smckusick nchtail = ncp->nc_prev; 29137487Smckusick /* cause rescan of list, it may have altered */ 29237487Smckusick nxtcp = nchhead; 29337487Smckusick /* put the now-free entry at head of LRU */ 29437487Smckusick ncp->nc_nxt = nxtcp; 29537487Smckusick ncp->nc_prev = &nchhead; 29637487Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 29737487Smckusick nchhead = ncp; 29837487Smckusick } 29937487Smckusick } 300