Home | History | Annotate | Line # | Download | only in filecorefs
filecore_node.c revision 1.3
      1 /*	$NetBSD: filecore_node.c,v 1.3 2004/03/27 04:43:43 atatat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1982, 1986, 1989, 1994
      5  *           The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  *	filecore_node.c		1.0	1998/6/4
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1998 Andrew McMurry
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed by the University of
     49  *	California, Berkeley and its contributors.
     50  * 4. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	filecore_node.c		1.0	1998/6/4
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.3 2004/03/27 04:43:43 atatat Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/mount.h>
     75 #include <sys/proc.h>
     76 #include <sys/file.h>
     77 #include <sys/buf.h>
     78 #include <sys/vnode.h>
     79 #include <sys/namei.h>
     80 #include <sys/kernel.h>
     81 #include <sys/malloc.h>
     82 #include <sys/pool.h>
     83 #include <sys/stat.h>
     84 
     85 #include <fs/filecorefs/filecore.h>
     86 #include <fs/filecorefs/filecore_extern.h>
     87 #include <fs/filecorefs/filecore_node.h>
     88 #include <fs/filecorefs/filecore_mount.h>
     89 
     90 /*
     91  * Structures associated with filecore_node caching.
     92  */
     93 LIST_HEAD(ihashhead, filecore_node) *filecorehashtbl;
     94 u_long filecorehash;
     95 #define	INOHASH(device, inum)	(((device) + ((inum)>>12)) & filecorehash)
     96 struct simplelock filecore_ihash_slock;
     97 
     98 struct pool filecore_node_pool;
     99 
    100 extern int prtactive;	/* 1 => print out reclaim of active vnodes */
    101 
    102 /*
    103  * Initialize hash links for inodes and dnodes.
    104  */
    105 void
    106 filecore_init()
    107 {
    108 #ifdef _LKM
    109 	malloc_type_attach(M_FILECOREMNT);
    110 #endif
    111 	filecorehashtbl = hashinit(desiredvnodes, HASH_LIST, M_FILECOREMNT,
    112 	    M_WAITOK, &filecorehash);
    113 	simple_lock_init(&filecore_ihash_slock);
    114 	pool_init(&filecore_node_pool, sizeof(struct filecore_node),
    115 	    0, 0, 0, "filecrnopl", &pool_allocator_nointr);
    116 }
    117 
    118 /*
    119  * Reinitialize inode hash table.
    120  */
    121 void
    122 filecore_reinit()
    123 {
    124 	struct filecore_node *ip;
    125 	struct ihashhead *oldhash, *hash;
    126 	u_long oldmask, mask, val;
    127 	int i;
    128 
    129 	hash = hashinit(desiredvnodes, HASH_LIST, M_FILECOREMNT, M_WAITOK,
    130 	    &mask);
    131 
    132 	simple_lock(&filecore_ihash_slock);
    133 	oldhash = filecorehashtbl;
    134 	oldmask = filecorehash;
    135 	filecorehashtbl = hash;
    136 	filecorehash = mask;
    137 	for (i = 0; i <= oldmask; i++) {
    138 		while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
    139 			LIST_REMOVE(ip, i_hash);
    140 			val = INOHASH(ip->i_dev, ip->i_number);
    141 			LIST_INSERT_HEAD(&hash[val], ip, i_hash);
    142 		}
    143 	}
    144 	simple_unlock(&filecore_ihash_slock);
    145 	hashdone(oldhash, M_FILECOREMNT);
    146 }
    147 
    148 /*
    149  * Destroy node pool and hash table.
    150  */
    151 void
    152 filecore_done()
    153 {
    154 	pool_destroy(&filecore_node_pool);
    155 	hashdone(filecorehashtbl, M_FILECOREMNT);
    156 #ifdef _LKM
    157 	malloc_type_detach(M_FILECOREMNT);
    158 #endif
    159 }
    160 
    161 /*
    162  * Use the device/inum pair to find the incore inode, and return a pointer
    163  * to it. If it is in core, but locked, wait for it.
    164  */
    165 struct vnode *
    166 filecore_ihashget(dev, inum)
    167 	dev_t dev;
    168 	ino_t inum;
    169 {
    170 	struct filecore_node *ip;
    171 	struct vnode *vp;
    172 
    173 loop:
    174 	simple_lock(&filecore_ihash_slock);
    175 	LIST_FOREACH(ip, &filecorehashtbl[INOHASH(dev, inum)], i_hash) {
    176 		if (inum == ip->i_number && dev == ip->i_dev) {
    177 			vp = ITOV(ip);
    178 			simple_lock(&vp->v_interlock);
    179 			simple_unlock(&filecore_ihash_slock);
    180 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    181 				goto loop;
    182 			return (vp);
    183 		}
    184 	}
    185 	simple_unlock(&filecore_ihash_slock);
    186 	return (NULL);
    187 }
    188 
    189 /*
    190  * Insert the inode into the hash table, and return it locked.
    191  */
    192 void
    193 filecore_ihashins(ip)
    194 	struct filecore_node *ip;
    195 {
    196 	struct ihashhead *ipp;
    197 	struct vnode *vp;
    198 
    199 	simple_lock(&filecore_ihash_slock);
    200 	ipp = &filecorehashtbl[INOHASH(ip->i_dev, ip->i_number)];
    201 	LIST_INSERT_HEAD(ipp, ip, i_hash);
    202 	simple_unlock(&filecore_ihash_slock);
    203 
    204 	vp = ip->i_vnode;
    205 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, &vp->v_interlock);
    206 }
    207 
    208 /*
    209  * Remove the inode from the hash table.
    210  */
    211 void
    212 filecore_ihashrem(ip)
    213 	struct filecore_node *ip;
    214 {
    215 	simple_lock(&filecore_ihash_slock);
    216 	LIST_REMOVE(ip, i_hash);
    217 	simple_unlock(&filecore_ihash_slock);
    218 }
    219 
    220 /*
    221  * Last reference to an inode, write the inode out and if necessary,
    222  * truncate and deallocate the file.
    223  */
    224 int
    225 filecore_inactive(v)
    226 	void *v;
    227 {
    228 	struct vop_inactive_args /* {
    229 		struct vnode *a_vp;
    230 		struct proc *a_p;
    231 	} */ *ap = v;
    232 	struct vnode *vp = ap->a_vp;
    233 	struct proc *p = ap->a_p;
    234 	struct filecore_node *ip = VTOI(vp);
    235 	int error = 0;
    236 
    237 	if (prtactive && vp->v_usecount != 0)
    238 		vprint("filecore_inactive: pushing active", vp);
    239 
    240 	ip->i_flag = 0;
    241 	VOP_UNLOCK(vp, 0);
    242 	/*
    243 	 * If we are done with the inode, reclaim it
    244 	 * so that it can be reused immediately.
    245 	 */
    246 	if (filecore_staleinode(ip))
    247 		vrecycle(vp, (struct simplelock *)0, p);
    248 	return error;
    249 }
    250 
    251 /*
    252  * Reclaim an inode so that it can be used for other purposes.
    253  */
    254 int
    255 filecore_reclaim(v)
    256 	void *v;
    257 {
    258 	struct vop_reclaim_args /* {
    259 		struct vnode *a_vp;
    260 		struct proc *a_p;
    261 	} */ *ap = v;
    262 	struct vnode *vp = ap->a_vp;
    263 	struct filecore_node *ip = VTOI(vp);
    264 
    265 	if (prtactive && vp->v_usecount != 0)
    266 		vprint("filecore_reclaim: pushing active", vp);
    267 	/*
    268 	 * Remove the inode from its hash chain.
    269 	 */
    270 	filecore_ihashrem(ip);
    271 	/*
    272 	 * Purge old data structures associated with the inode.
    273 	 */
    274 	cache_purge(vp);
    275 	if (ip->i_devvp) {
    276 		vrele(ip->i_devvp);
    277 		ip->i_devvp = 0;
    278 	}
    279 	pool_put(&filecore_node_pool, vp->v_data);
    280 	vp->v_data = NULL;
    281 	return (0);
    282 }
    283