137487Smckusick /* 237487Smckusick * Copyright (c) 1989 The Regents of the University of California. 337487Smckusick * All rights reserved. 437487Smckusick * 544455Sbostic * %sccs.include.redist.c% 637487Smckusick * 7*52231Sheideman * @(#)vfs_cache.c 7.9 (Berkeley) 01/22/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 */ 68*52231Sheideman int 69*52231Sheideman cache_lookup(dvp, vpp, cnp) /* converted to CN. NEEDSWORK: do callers */ 70*52231Sheideman /* old: cache_lookup(ndp) */ 71*52231Sheideman struct vnode *dvp; 72*52231Sheideman struct vnode **vpp; 73*52231Sheideman struct componentname *cnp; 7437487Smckusick { 7537487Smckusick register struct namecache *ncp; 7637487Smckusick union nchash *nhp; 7737487Smckusick 7838768Smckusick if (!doingcache) 7938768Smckusick return (0); 80*52231Sheideman if (cnp->cn_namelen > NCHNAMLEN) { 8137487Smckusick nchstats.ncs_long++; 82*52231Sheideman cnp->cn_flags &= ~MAKEENTRY; 8337487Smckusick return (0); 8437487Smckusick } 85*52231Sheideman nhp = &nchashtbl[cnp->cn_hash & nchash]; 8637487Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 8737487Smckusick ncp = ncp->nc_forw) { 8838768Smckusick if (ncp->nc_dvp == dvp && 8938768Smckusick ncp->nc_dvpid == dvp->v_id && 90*52231Sheideman ncp->nc_nlen == cnp->cn_namelen && 91*52231Sheideman !bcmp(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen)) 9237487Smckusick break; 9337487Smckusick } 9437487Smckusick if (ncp == (struct namecache *)nhp) { 9537487Smckusick nchstats.ncs_miss++; 9637487Smckusick return (0); 9737487Smckusick } 98*52231Sheideman if (!(cnp->cn_flags & MAKEENTRY)) { 9938768Smckusick nchstats.ncs_badhits++; 10038768Smckusick } else if (ncp->nc_vp == NULL) { 101*52231Sheideman if ((cnp->cn_nameiop & OPMASK) != CREATE) { 10246747Smckusick nchstats.ncs_neghits++; 10346747Smckusick /* 10446747Smckusick * Move this slot to end of LRU chain, 10546747Smckusick * if not already there. 10646747Smckusick */ 10746747Smckusick if (ncp->nc_nxt) { 10846747Smckusick /* remove from LRU chain */ 10946747Smckusick *ncp->nc_prev = ncp->nc_nxt; 11046747Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 11146747Smckusick /* and replace at end of it */ 11246747Smckusick ncp->nc_nxt = NULL; 11346747Smckusick ncp->nc_prev = nchtail; 11446747Smckusick *nchtail = ncp; 11546747Smckusick nchtail = &ncp->nc_nxt; 11646747Smckusick } 11746747Smckusick return (ENOENT); 11837487Smckusick } 11938768Smckusick } else if (ncp->nc_vpid != ncp->nc_vp->v_id) { 12038768Smckusick nchstats.ncs_falsehits++; 12138768Smckusick } else { 12237487Smckusick nchstats.ncs_goodhits++; 12338768Smckusick /* 12438768Smckusick * move this slot to end of LRU chain, if not already there 12538768Smckusick */ 12638768Smckusick if (ncp->nc_nxt) { 12738768Smckusick /* remove from LRU chain */ 12838768Smckusick *ncp->nc_prev = ncp->nc_nxt; 12938768Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 13038768Smckusick /* and replace at end of it */ 13138768Smckusick ncp->nc_nxt = NULL; 13238768Smckusick ncp->nc_prev = nchtail; 13338768Smckusick *nchtail = ncp; 13438768Smckusick nchtail = &ncp->nc_nxt; 13538768Smckusick } 136*52231Sheideman *vpp = ncp->nc_vp; 13738768Smckusick return (-1); 13837487Smckusick } 13937487Smckusick 14037487Smckusick /* 14137487Smckusick * Last component and we are renaming or deleting, 14237487Smckusick * the cache entry is invalid, or otherwise don't 14337487Smckusick * want cache entry to exist. 14437487Smckusick */ 14537487Smckusick /* remove from LRU chain */ 14637487Smckusick *ncp->nc_prev = ncp->nc_nxt; 14737487Smckusick if (ncp->nc_nxt) 14837487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 14937487Smckusick else 15037487Smckusick nchtail = ncp->nc_prev; 15138768Smckusick /* remove from hash chain */ 15238768Smckusick remque(ncp); 15337487Smckusick /* insert at head of LRU list (first to grab) */ 15437487Smckusick ncp->nc_nxt = nchhead; 15537487Smckusick ncp->nc_prev = &nchhead; 15637487Smckusick nchhead->nc_prev = &ncp->nc_nxt; 15737487Smckusick nchhead = ncp; 15837487Smckusick /* and make a dummy hash chain */ 15937487Smckusick ncp->nc_forw = ncp; 16037487Smckusick ncp->nc_back = ncp; 16137487Smckusick return (0); 16237487Smckusick } 16337487Smckusick 16437487Smckusick /* 16537487Smckusick * Add an entry to the cache 16637487Smckusick */ 167*52231Sheideman cache_enter(dvp, vp, cnp) /* converted to CN. NEEDSWORK: do callers */ 168*52231Sheideman /* old: cache_lookup(ndp) */ 169*52231Sheideman struct vnode *dvp; 170*52231Sheideman struct vnode *vp; 171*52231Sheideman struct componentname *cnp; 17237487Smckusick { 17337487Smckusick register struct namecache *ncp; 17437487Smckusick union nchash *nhp; 17537487Smckusick 17638768Smckusick if (!doingcache) 17738768Smckusick return; 17837487Smckusick /* 17937487Smckusick * Free the cache slot at head of lru chain. 18037487Smckusick */ 18140881Smckusick if (numcache < desiredvnodes) { 18240881Smckusick ncp = (struct namecache *) 18345117Smckusick malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK); 18440881Smckusick bzero((char *)ncp, sizeof *ncp); 18540881Smckusick numcache++; 18640881Smckusick } else if (ncp = nchhead) { 18737487Smckusick /* remove from lru chain */ 18837487Smckusick *ncp->nc_prev = ncp->nc_nxt; 18937487Smckusick if (ncp->nc_nxt) 19037487Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 19137487Smckusick else 19237487Smckusick nchtail = ncp->nc_prev; 19338768Smckusick /* remove from old hash chain */ 19438768Smckusick remque(ncp); 19540881Smckusick } else 19640881Smckusick return; 19740881Smckusick /* grab the vnode we just found */ 198*52231Sheideman ncp->nc_vp = vp; 199*52231Sheideman if (vp) 200*52231Sheideman ncp->nc_vpid = vp->v_id; 20140881Smckusick else 20240881Smckusick ncp->nc_vpid = 0; 20340881Smckusick /* fill in cache info */ 204*52231Sheideman ncp->nc_dvp = dvp; 205*52231Sheideman ncp->nc_dvpid = dvp->v_id; 206*52231Sheideman ncp->nc_nlen = cnp->cn_namelen; 207*52231Sheideman bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen); 20840881Smckusick /* link at end of lru chain */ 20940881Smckusick ncp->nc_nxt = NULL; 21040881Smckusick ncp->nc_prev = nchtail; 21140881Smckusick *nchtail = ncp; 21240881Smckusick nchtail = &ncp->nc_nxt; 21340881Smckusick /* and insert on hash chain */ 214*52231Sheideman nhp = &nchashtbl[cnp->cn_hash & nchash]; 21540881Smckusick insque(ncp, nhp); 21637487Smckusick } 21737487Smckusick 21837487Smckusick /* 21940881Smckusick * Name cache initialization, from vfs_init() when we are booting 22037487Smckusick */ 22137487Smckusick nchinit() 22237487Smckusick { 22337487Smckusick register union nchash *nchp; 22440881Smckusick long nchashsize; 22537487Smckusick 22637487Smckusick nchhead = 0; 22737487Smckusick nchtail = &nchhead; 22840881Smckusick nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2, 22940881Smckusick NBPG * CLSIZE); 23045117Smckusick nchashtbl = (union nchash *)malloc((u_long)nchashsize, 23145117Smckusick M_CACHE, M_WAITOK); 23240881Smckusick for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1) 23340881Smckusick /* void */; 23440881Smckusick nchash = (nchash >> 1) - 1; 23540881Smckusick 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 { 24840881Smckusick union nchash *nhp; 24938768Smckusick struct namecache *ncp; 25037487Smckusick 25138768Smckusick vp->v_id = ++nextvnodeid; 25238768Smckusick if (nextvnodeid != 0) 25338768Smckusick return; 25440881Smckusick for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) { 25540881Smckusick for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 25640881Smckusick ncp = ncp->nc_forw) { 25740881Smckusick ncp->nc_vpid = 0; 25840881Smckusick ncp->nc_dvpid = 0; 25940881Smckusick } 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) 27345117Smckusick 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