Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.8
      1 /*	$NetBSD: vfs_cache.c,v 1.8 1994/07/05 19:09:32 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)vfs_cache.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/time.h>
     41 #include <sys/mount.h>
     42 #include <sys/vnode.h>
     43 #include <sys/namei.h>
     44 #include <sys/errno.h>
     45 #include <sys/malloc.h>
     46 
     47 /*
     48  * Name caching works as follows:
     49  *
     50  * Names found by directory scans are retained in a cache
     51  * for future reference.  It is managed LRU, so frequently
     52  * used names will hang around.  Cache is indexed by hash value
     53  * obtained from (vp, name) where vp refers to the directory
     54  * containing name.
     55  *
     56  * For simplicity (and economy of storage), names longer than
     57  * a maximum length of NCHNAMLEN are not cached; they occur
     58  * infrequently in any case, and are almost never of interest.
     59  *
     60  * Upon reaching the last segment of a path, if the reference
     61  * is for DELETE, or NOCACHE is set (rewrite), and the
     62  * name is located in the cache, it will be dropped.
     63  */
     64 
     65 /*
     66  * Structures associated with name cacheing.
     67  */
     68 struct namecache **nchashtbl;
     69 u_long	nchash;				/* size of hash table - 1 */
     70 long	numcache;			/* number of cache entries allocated */
     71 struct	namecache *nchhead, **nchtail;	/* LRU chain pointers */
     72 struct	nchstats nchstats;		/* cache effectiveness statistics */
     73 
     74 int doingcache = 1;			/* 1 => enable the cache */
     75 
     76 /*
     77  * Look for a the name in the cache. We don't do this
     78  * if the segment name is long, simply so the cache can avoid
     79  * holding long names (which would either waste space, or
     80  * add greatly to the complexity).
     81  *
     82  * Lookup is called with ni_dvp pointing to the directory to search,
     83  * ni_ptr pointing to the name of the entry being sought, ni_namelen
     84  * tells the length of the name, and ni_hash contains a hash of
     85  * the name. If the lookup succeeds, the vnode is returned in ni_vp
     86  * and a status of -1 is returned. If the lookup determines that
     87  * the name does not exist (negative cacheing), a status of ENOENT
     88  * is returned. If the lookup fails, a status of zero is returned.
     89  */
     90 int
     91 cache_lookup(dvp, vpp, cnp)
     92 	struct vnode *dvp;
     93 	struct vnode **vpp;
     94 	struct componentname *cnp;
     95 {
     96 	register struct namecache *ncp, *ncq, **ncpp;
     97 
     98 	if (!doingcache) {
     99 		cnp->cn_flags &= ~MAKEENTRY;
    100 		return (0);
    101 	}
    102 	if (cnp->cn_namelen > NCHNAMLEN) {
    103 		nchstats.ncs_long++;
    104 		cnp->cn_flags &= ~MAKEENTRY;
    105 		return (0);
    106 	}
    107 	ncpp = &nchashtbl[cnp->cn_hash & nchash];
    108 	for (ncp = *ncpp; ncp; ncp = ncp->nc_forw) {
    109 		if (ncp->nc_dvp == dvp &&
    110 		    ncp->nc_dvpid == dvp->v_id &&
    111 		    ncp->nc_nlen == cnp->cn_namelen &&
    112 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    113 			break;
    114 	}
    115 	if (ncp == NULL) {
    116 		nchstats.ncs_miss++;
    117 		return (0);
    118 	}
    119 	if (!(cnp->cn_flags & MAKEENTRY)) {
    120 		nchstats.ncs_badhits++;
    121 	} else if (ncp->nc_vp == NULL) {
    122 		if (cnp->cn_nameiop != CREATE) {
    123 			nchstats.ncs_neghits++;
    124 			/*
    125 			 * Move this slot to end of LRU chain,
    126 			 * if not already there.
    127 			 */
    128 			if (ncp->nc_nxt) {
    129 				/* remove from LRU chain */
    130 				*ncp->nc_prev = ncp->nc_nxt;
    131 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
    132 				/* and replace at end of it */
    133 				ncp->nc_nxt = NULL;
    134 				ncp->nc_prev = nchtail;
    135 				*nchtail = ncp;
    136 				nchtail = &ncp->nc_nxt;
    137 			}
    138 			return (ENOENT);
    139 		}
    140 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
    141 		nchstats.ncs_falsehits++;
    142 	} else {
    143 		nchstats.ncs_goodhits++;
    144 		/*
    145 		 * move this slot to end of LRU chain, if not already there
    146 		 */
    147 		if (ncp->nc_nxt) {
    148 			/* remove from LRU chain */
    149 			*ncp->nc_prev = ncp->nc_nxt;
    150 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
    151 			/* and replace at end of it */
    152 			ncp->nc_nxt = NULL;
    153 			ncp->nc_prev = nchtail;
    154 			*nchtail = ncp;
    155 			nchtail = &ncp->nc_nxt;
    156 		}
    157 		*vpp = ncp->nc_vp;
    158 		return (-1);
    159 	}
    160 
    161 	/*
    162 	 * Last component and we are renaming or deleting,
    163 	 * the cache entry is invalid, or otherwise don't
    164 	 * want cache entry to exist.
    165 	 */
    166 	/* remove from LRU chain */
    167 	if (ncq = ncp->nc_nxt)
    168 		ncq->nc_prev = ncp->nc_prev;
    169 	else
    170 		nchtail = ncp->nc_prev;
    171 	*ncp->nc_prev = ncq;
    172 	/* remove from hash chain */
    173 	if (ncq = ncp->nc_forw)
    174 		ncq->nc_back = ncp->nc_back;
    175 	*ncp->nc_back = ncq;
    176 	/* and make a dummy hash chain */
    177 	ncp->nc_forw = NULL;
    178 	ncp->nc_back = NULL;
    179 	/* insert at head of LRU list (first to grab) */
    180 	if (ncq = nchhead)
    181 		ncq->nc_prev = &ncp->nc_nxt;
    182 	else
    183 		nchtail = &ncp->nc_nxt;
    184 	nchhead = ncp;
    185 	ncp->nc_nxt = ncq;
    186 	ncp->nc_prev = &nchhead;
    187 	return (0);
    188 }
    189 
    190 /*
    191  * Add an entry to the cache
    192  */
    193 cache_enter(dvp, vp, cnp)
    194 	struct vnode *dvp;
    195 	struct vnode *vp;
    196 	struct componentname *cnp;
    197 {
    198 	register struct namecache *ncp, *ncq, **ncpp;
    199 
    200 #ifdef DIAGNOSTIC
    201 	if (cnp->cn_namelen > NCHNAMLEN)
    202 		panic("cache_enter: name too long");
    203 #endif
    204 	if (!doingcache)
    205 		return;
    206 	/*
    207 	 * Free the cache slot at head of lru chain.
    208 	 */
    209 	if (numcache < desiredvnodes) {
    210 		ncp = (struct namecache *)
    211 			malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
    212 		bzero((char *)ncp, sizeof *ncp);
    213 		numcache++;
    214 	} else if (ncp = nchhead) {
    215 		/* remove from lru chain */
    216 		if (ncq = ncp->nc_nxt)
    217 			ncq->nc_prev = ncp->nc_prev;
    218 		else
    219 			nchtail = ncp->nc_prev;
    220 		*ncp->nc_prev = ncq;
    221 		/* remove from old hash chain, if on one */
    222 		if (ncp->nc_back) {
    223 			if (ncq = ncp->nc_forw)
    224 				ncq->nc_back = ncp->nc_back;
    225 			*ncp->nc_back = ncq;
    226 			ncp->nc_forw = NULL;
    227 			ncp->nc_back = NULL;
    228 		}
    229 	} else
    230 		return;
    231 	/* grab the vnode we just found */
    232 	ncp->nc_vp = vp;
    233 	if (vp)
    234 		ncp->nc_vpid = vp->v_id;
    235 	else
    236 		ncp->nc_vpid = 0;
    237 	/* fill in cache info */
    238 	ncp->nc_dvp = dvp;
    239 	ncp->nc_dvpid = dvp->v_id;
    240 	ncp->nc_nlen = cnp->cn_namelen;
    241 	bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
    242 	/* link at end of lru chain */
    243 	ncp->nc_nxt = NULL;
    244 	ncp->nc_prev = nchtail;
    245 	*nchtail = ncp;
    246 	nchtail = &ncp->nc_nxt;
    247 	/* and insert on hash chain */
    248 	ncpp = &nchashtbl[cnp->cn_hash & nchash];
    249 	if (ncq = *ncpp)
    250 		ncq->nc_back = &ncp->nc_forw;
    251 	ncp->nc_forw = ncq;
    252 	ncp->nc_back = ncpp;
    253 	*ncpp = ncp;
    254 }
    255 
    256 /*
    257  * Name cache initialization, from vfs_init() when we are booting
    258  */
    259 nchinit()
    260 {
    261 
    262 	nchtail = &nchhead;
    263 	nchashtbl = hashinit(desiredvnodes, M_CACHE, &nchash);
    264 }
    265 
    266 /*
    267  * Cache flush, a particular vnode; called when a vnode is renamed to
    268  * hide entries that would now be invalid
    269  */
    270 cache_purge(vp)
    271 	struct vnode *vp;
    272 {
    273 	struct namecache *ncp, **ncpp;
    274 
    275 	vp->v_id = ++nextvnodeid;
    276 	if (nextvnodeid != 0)
    277 		return;
    278 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
    279 		for (ncp = *ncpp; ncp; ncp = ncp->nc_forw) {
    280 			ncp->nc_vpid = 0;
    281 			ncp->nc_dvpid = 0;
    282 		}
    283 	}
    284 	vp->v_id = ++nextvnodeid;
    285 }
    286 
    287 /*
    288  * Cache flush, a whole filesystem; called when filesys is umounted to
    289  * remove entries that would now be invalid
    290  *
    291  * The line "nxtcp = nchhead" near the end is to avoid potential problems
    292  * if the cache lru chain is modified while we are dumping the
    293  * inode.  This makes the algorithm O(n^2), but do you think I care?
    294  */
    295 cache_purgevfs(mp)
    296 	struct mount *mp;
    297 {
    298 	register struct namecache *ncp, *nxtcp;
    299 
    300 	for (ncp = nchhead; ncp; ncp = nxtcp) {
    301 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    302 			nxtcp = ncp->nc_nxt;
    303 			continue;
    304 		}
    305 		/* free the resources we had */
    306 		ncp->nc_vp = NULL;
    307 		ncp->nc_dvp = NULL;
    308 		/* remove from old hash chain, if on one */
    309 		if (ncp->nc_back) {
    310 			if (nxtcp = ncp->nc_forw)
    311 				nxtcp->nc_back = ncp->nc_back;
    312 			*ncp->nc_back = nxtcp;
    313 			ncp->nc_forw = NULL;
    314 			ncp->nc_back = NULL;
    315 		}
    316 		/* delete this entry from LRU chain */
    317 		if (nxtcp = ncp->nc_nxt)
    318 			nxtcp->nc_prev = ncp->nc_prev;
    319 		else
    320 			nchtail = ncp->nc_prev;
    321 		*ncp->nc_prev = nxtcp;
    322 		/* cause rescan of list, it may have altered */
    323 		/* also put the now-free entry at head of LRU */
    324 		if (nxtcp = nchhead)
    325 			nxtcp->nc_prev = &ncp->nc_nxt;
    326 		else
    327 			nchtail = &ncp->nc_nxt;
    328 		nchhead = ncp;
    329 		ncp->nc_nxt = nxtcp;
    330 		ncp->nc_prev = &nchhead;
    331 	}
    332 }
    333