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