Home | History | Annotate | Line # | Download | only in ntfs
ntfs_ihash.c revision 1.2
      1  1.2       agc /*	$NetBSD: ntfs_ihash.c,v 1.2 2003/08/07 16:31:39 agc Exp $	*/
      2  1.1  jdolecek 
      3  1.1  jdolecek /*
      4  1.1  jdolecek  * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
      5  1.1  jdolecek  *	The Regents of the University of California.  All rights reserved.
      6  1.1  jdolecek  *
      7  1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
      8  1.1  jdolecek  * modification, are permitted provided that the following conditions
      9  1.1  jdolecek  * are met:
     10  1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     15  1.2       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     17  1.1  jdolecek  *    without specific prior written permission.
     18  1.1  jdolecek  *
     19  1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  jdolecek  * SUCH DAMAGE.
     30  1.1  jdolecek  *
     31  1.1  jdolecek  *	@(#)ufs_ihash.c	8.7 (Berkeley) 5/17/95
     32  1.1  jdolecek  * Id: ntfs_ihash.c,v 1.5 1999/05/12 09:42:58 semenu Exp
     33  1.1  jdolecek  */
     34  1.1  jdolecek 
     35  1.1  jdolecek #include <sys/cdefs.h>
     36  1.2       agc __KERNEL_RCSID(0, "$NetBSD: ntfs_ihash.c,v 1.2 2003/08/07 16:31:39 agc Exp $");
     37  1.1  jdolecek 
     38  1.1  jdolecek #include <sys/param.h>
     39  1.1  jdolecek #include <sys/systm.h>
     40  1.1  jdolecek #include <sys/kernel.h>
     41  1.1  jdolecek #include <sys/lock.h>
     42  1.1  jdolecek #include <sys/vnode.h>
     43  1.1  jdolecek #include <sys/malloc.h>
     44  1.1  jdolecek #include <sys/proc.h>
     45  1.1  jdolecek #include <sys/mount.h>
     46  1.1  jdolecek 
     47  1.1  jdolecek #include <fs/ntfs/ntfs.h>
     48  1.1  jdolecek #include <fs/ntfs/ntfs_inode.h>
     49  1.1  jdolecek #include <fs/ntfs/ntfs_ihash.h>
     50  1.1  jdolecek 
     51  1.1  jdolecek MALLOC_DEFINE(M_NTFSNTHASH, "NTFS nthash", "NTFS ntnode hash tables");
     52  1.1  jdolecek 
     53  1.1  jdolecek /*
     54  1.1  jdolecek  * Structures associated with inode cacheing.
     55  1.1  jdolecek  */
     56  1.1  jdolecek static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
     57  1.1  jdolecek static u_long	ntfs_nthash;		/* size of hash table - 1 */
     58  1.1  jdolecek #define	NTNOHASH(device, inum)	((minor(device) + (inum)) & ntfs_nthash)
     59  1.1  jdolecek #ifndef NULL_SIMPLELOCKS
     60  1.1  jdolecek static struct simplelock ntfs_nthash_slock;
     61  1.1  jdolecek #endif
     62  1.1  jdolecek struct lock ntfs_hashlock;
     63  1.1  jdolecek 
     64  1.1  jdolecek /*
     65  1.1  jdolecek  * Initialize inode hash table.
     66  1.1  jdolecek  */
     67  1.1  jdolecek void
     68  1.1  jdolecek ntfs_nthashinit()
     69  1.1  jdolecek {
     70  1.1  jdolecek 	lockinit(&ntfs_hashlock, PINOD, "ntfs_nthashlock", 0, 0);
     71  1.1  jdolecek 	ntfs_nthashtbl = HASHINIT(desiredvnodes, M_NTFSNTHASH, M_WAITOK,
     72  1.1  jdolecek 	    &ntfs_nthash);
     73  1.1  jdolecek 	simple_lock_init(&ntfs_nthash_slock);
     74  1.1  jdolecek }
     75  1.1  jdolecek 
     76  1.1  jdolecek #ifdef __NetBSD__
     77  1.1  jdolecek /*
     78  1.1  jdolecek  * Reinitialize inode hash table.
     79  1.1  jdolecek  */
     80  1.1  jdolecek 
     81  1.1  jdolecek void
     82  1.1  jdolecek ntfs_nthashreinit()
     83  1.1  jdolecek {
     84  1.1  jdolecek 	struct ntnode *ip;
     85  1.1  jdolecek 	struct nthashhead *oldhash, *hash;
     86  1.1  jdolecek 	u_long oldmask, mask, val;
     87  1.1  jdolecek 	int i;
     88  1.1  jdolecek 
     89  1.1  jdolecek 	hash = HASHINIT(desiredvnodes, M_NTFSNTHASH, M_WAITOK, &mask);
     90  1.1  jdolecek 
     91  1.1  jdolecek 	simple_lock(&ntfs_nthash_slock);
     92  1.1  jdolecek 	oldhash = ntfs_nthashtbl;
     93  1.1  jdolecek 	oldmask = ntfs_nthash;
     94  1.1  jdolecek 	ntfs_nthashtbl = hash;
     95  1.1  jdolecek 	ntfs_nthash = mask;
     96  1.1  jdolecek 	for (i = 0; i <= oldmask; i++) {
     97  1.1  jdolecek 		while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
     98  1.1  jdolecek 			LIST_REMOVE(ip, i_hash);
     99  1.1  jdolecek 			val = NTNOHASH(ip->i_dev, ip->i_number);
    100  1.1  jdolecek 			LIST_INSERT_HEAD(&hash[val], ip, i_hash);
    101  1.1  jdolecek 		}
    102  1.1  jdolecek 	}
    103  1.1  jdolecek 	simple_unlock(&ntfs_nthash_slock);
    104  1.1  jdolecek 	hashdone(oldhash, M_NTFSNTHASH);
    105  1.1  jdolecek }
    106  1.1  jdolecek 
    107  1.1  jdolecek /*
    108  1.1  jdolecek  * Free the inode hash table. Called from ntfs_done(), only needed
    109  1.1  jdolecek  * on NetBSD.
    110  1.1  jdolecek  */
    111  1.1  jdolecek void
    112  1.1  jdolecek ntfs_nthashdone()
    113  1.1  jdolecek {
    114  1.1  jdolecek 	hashdone(ntfs_nthashtbl, M_NTFSNTHASH);
    115  1.1  jdolecek }
    116  1.1  jdolecek #endif
    117  1.1  jdolecek 
    118  1.1  jdolecek /*
    119  1.1  jdolecek  * Use the device/inum pair to find the incore inode, and return a pointer
    120  1.1  jdolecek  * to it. If it is in core, return it, even if it is locked.
    121  1.1  jdolecek  */
    122  1.1  jdolecek struct ntnode *
    123  1.1  jdolecek ntfs_nthashlookup(dev, inum)
    124  1.1  jdolecek 	dev_t dev;
    125  1.1  jdolecek 	ino_t inum;
    126  1.1  jdolecek {
    127  1.1  jdolecek 	struct ntnode *ip;
    128  1.1  jdolecek 	struct nthashhead *ipp;
    129  1.1  jdolecek 
    130  1.1  jdolecek 	simple_lock(&ntfs_nthash_slock);
    131  1.1  jdolecek 	ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)];
    132  1.1  jdolecek 	LIST_FOREACH(ip, ipp, i_hash) {
    133  1.1  jdolecek 		if (inum == ip->i_number && dev == ip->i_dev)
    134  1.1  jdolecek 			break;
    135  1.1  jdolecek 	}
    136  1.1  jdolecek 	simple_unlock(&ntfs_nthash_slock);
    137  1.1  jdolecek 
    138  1.1  jdolecek 	return (ip);
    139  1.1  jdolecek }
    140  1.1  jdolecek 
    141  1.1  jdolecek /*
    142  1.1  jdolecek  * Insert the ntnode into the hash table.
    143  1.1  jdolecek  */
    144  1.1  jdolecek void
    145  1.1  jdolecek ntfs_nthashins(ip)
    146  1.1  jdolecek 	struct ntnode *ip;
    147  1.1  jdolecek {
    148  1.1  jdolecek 	struct nthashhead *ipp;
    149  1.1  jdolecek 
    150  1.1  jdolecek 	simple_lock(&ntfs_nthash_slock);
    151  1.1  jdolecek 	ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)];
    152  1.1  jdolecek 	LIST_INSERT_HEAD(ipp, ip, i_hash);
    153  1.1  jdolecek 	ip->i_flag |= IN_HASHED;
    154  1.1  jdolecek 	simple_unlock(&ntfs_nthash_slock);
    155  1.1  jdolecek }
    156  1.1  jdolecek 
    157  1.1  jdolecek /*
    158  1.1  jdolecek  * Remove the inode from the hash table.
    159  1.1  jdolecek  */
    160  1.1  jdolecek void
    161  1.1  jdolecek ntfs_nthashrem(ip)
    162  1.1  jdolecek 	struct ntnode *ip;
    163  1.1  jdolecek {
    164  1.1  jdolecek 	simple_lock(&ntfs_nthash_slock);
    165  1.1  jdolecek 	if (ip->i_flag & IN_HASHED) {
    166  1.1  jdolecek 		ip->i_flag &= ~IN_HASHED;
    167  1.1  jdolecek 		LIST_REMOVE(ip, i_hash);
    168  1.1  jdolecek 	}
    169  1.1  jdolecek 	simple_unlock(&ntfs_nthash_slock);
    170  1.1  jdolecek }
    171