Home | History | Annotate | Line # | Download | only in filecorefs
filecore_node.c revision 1.22.2.1
      1 /*	$NetBSD: filecore_node.c,v 1.22.2.1 2011/06/06 09:09:21 jruoho 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.22.2.1 2011/06/06 09:09:21 jruoho 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/pool.h>
     82 #include <sys/stat.h>
     83 #include <sys/mutex.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 static LIST_HEAD(ihashhead, filecore_node) *filecorehashtbl;
     94 static u_long		filecorehash;
     95 
     96 #define	INOHASH(device, inum)	(((device) + ((inum)>>12)) & filecorehash)
     97 
     98 static kmutex_t		filecore_ihash_lock;
     99 struct pool		filecore_node_pool;
    100 
    101 extern int prtactive;	/* 1 => print out reclaim of active vnodes */
    102 
    103 /*
    104  * Initialize hash links for inodes and dnodes.
    105  */
    106 void
    107 filecore_init(void)
    108 {
    109 
    110 	mutex_init(&filecore_ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    111 	pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
    112 	    "filecrnopl", &pool_allocator_nointr, IPL_NONE);
    113 	filecorehashtbl = hashinit(desiredvnodes, HASH_LIST, true,
    114 	    &filecorehash);
    115 }
    116 
    117 /*
    118  * Reinitialize inode hash table.
    119  */
    120 void
    121 filecore_reinit(void)
    122 {
    123 	struct filecore_node *ip;
    124 	struct ihashhead *oldhash, *hash;
    125 	u_long oldmask, mask, val;
    126 	int i;
    127 
    128 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    129 
    130 	mutex_enter(&filecore_ihash_lock);
    131 	oldhash = filecorehashtbl;
    132 	oldmask = filecorehash;
    133 	filecorehashtbl = hash;
    134 	filecorehash = mask;
    135 	for (i = 0; i <= oldmask; i++) {
    136 		while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
    137 			LIST_REMOVE(ip, i_hash);
    138 			val = INOHASH(ip->i_dev, ip->i_number);
    139 			LIST_INSERT_HEAD(&hash[val], ip, i_hash);
    140 		}
    141 	}
    142 	mutex_exit(&filecore_ihash_lock);
    143 	hashdone(oldhash, HASH_LIST, oldmask);
    144 }
    145 
    146 /*
    147  * Destroy node pool and hash table.
    148  */
    149 void
    150 filecore_done(void)
    151 {
    152 
    153 	hashdone(filecorehashtbl, HASH_LIST, filecorehash);
    154 	pool_destroy(&filecore_node_pool);
    155 	mutex_destroy(&filecore_ihash_lock);
    156 }
    157 
    158 /*
    159  * Use the device/inum pair to find the incore inode, and return a pointer
    160  * to it. If it is in core, but locked, wait for it.
    161  */
    162 struct vnode *
    163 filecore_ihashget(dev_t dev, ino_t inum)
    164 {
    165 	struct filecore_node *ip;
    166 	struct vnode *vp;
    167 
    168 loop:
    169 	mutex_enter(&filecore_ihash_lock);
    170 	LIST_FOREACH(ip, &filecorehashtbl[INOHASH(dev, inum)], i_hash) {
    171 		if (inum == ip->i_number && dev == ip->i_dev) {
    172 			vp = ITOV(ip);
    173 			mutex_enter(&vp->v_interlock);
    174 			mutex_exit(&filecore_ihash_lock);
    175 			if (vget(vp, LK_EXCLUSIVE))
    176 				goto loop;
    177 			return (vp);
    178 		}
    179 	}
    180 	mutex_exit(&filecore_ihash_lock);
    181 	return (NULL);
    182 }
    183 
    184 /*
    185  * Insert the inode into the hash table, and return it locked.
    186  */
    187 void
    188 filecore_ihashins(struct filecore_node *ip)
    189 {
    190 	struct ihashhead *ipp;
    191 
    192 	mutex_enter(&filecore_ihash_lock);
    193 	ipp = &filecorehashtbl[INOHASH(ip->i_dev, ip->i_number)];
    194 	LIST_INSERT_HEAD(ipp, ip, i_hash);
    195 	mutex_exit(&filecore_ihash_lock);
    196 
    197 	VOP_LOCK(ITOV(ip), LK_EXCLUSIVE);
    198 }
    199 
    200 /*
    201  * Remove the inode from the hash table.
    202  */
    203 void
    204 filecore_ihashrem(struct filecore_node *ip)
    205 {
    206 	mutex_enter(&filecore_ihash_lock);
    207 	LIST_REMOVE(ip, i_hash);
    208 	mutex_exit(&filecore_ihash_lock);
    209 }
    210 
    211 /*
    212  * Last reference to an inode, write the inode out and if necessary,
    213  * truncate and deallocate the file.
    214  */
    215 int
    216 filecore_inactive(void *v)
    217 {
    218 	struct vop_inactive_args /* {
    219 		struct vnode *a_vp;
    220 		bool *a_recycle;
    221 	} */ *ap = v;
    222 	struct vnode *vp = ap->a_vp;
    223 	struct filecore_node *ip = VTOI(vp);
    224 	int error = 0;
    225 
    226 	/*
    227 	 * If we are done with the inode, reclaim it
    228 	 * so that it can be reused immediately.
    229 	 */
    230 	ip->i_flag = 0;
    231 	*ap->a_recycle = (filecore_staleinode(ip) != 0);
    232 	VOP_UNLOCK(vp);
    233 	return error;
    234 }
    235 
    236 /*
    237  * Reclaim an inode so that it can be used for other purposes.
    238  */
    239 int
    240 filecore_reclaim(void *v)
    241 {
    242 	struct vop_reclaim_args /* {
    243 		struct vnode *a_vp;
    244 		struct lwp *a_l;
    245 	} */ *ap = v;
    246 	struct vnode *vp = ap->a_vp;
    247 	struct filecore_node *ip = VTOI(vp);
    248 
    249 	if (prtactive && vp->v_usecount > 1)
    250 		vprint("filecore_reclaim: pushing active", vp);
    251 	/*
    252 	 * Remove the inode from its hash chain.
    253 	 */
    254 	filecore_ihashrem(ip);
    255 	/*
    256 	 * Purge old data structures associated with the inode.
    257 	 */
    258 	if (ip->i_devvp) {
    259 		vrele(ip->i_devvp);
    260 		ip->i_devvp = 0;
    261 	}
    262 	genfs_node_destroy(vp);
    263 	pool_put(&filecore_node_pool, vp->v_data);
    264 	vp->v_data = NULL;
    265 	return (0);
    266 }
    267