1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95 30 * $FreeBSD: src/sys/fs/hpfs/hpfs_hash.c,v 1.1 1999/12/09 19:09:58 semenu Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/vnode.h> 38 #include <sys/mount.h> 39 #include <sys/malloc.h> 40 #include <sys/proc.h> 41 42 #include "hpfs.h" 43 44 MALLOC_DEFINE(M_HPFSHASH, "HPFS hash", "HPFS node hash tables"); 45 46 /* 47 * Structures associated with hpfsnode cacheing. 48 */ 49 static LIST_HEAD(hphashhead, hpfsnode) *hpfs_hphashtbl; 50 static u_long hpfs_hphash; /* size of hash table - 1 */ 51 #define HPNOHASH(dev, lsn) (&hpfs_hphashtbl[(minor(dev) + (lsn)) & hpfs_hphash]) 52 #ifndef NULL_SIMPLELOCKS 53 static struct lwkt_token hpfs_hphash_token; 54 #endif 55 struct lock hpfs_hphash_lock; 56 57 /* 58 * Initialize inode hash table. 59 */ 60 void 61 hpfs_hphashinit(void) 62 { 63 64 lockinit (&hpfs_hphash_lock, "hpfs_hphashlock", 0, 0); 65 hpfs_hphashtbl = hashinit(desiredvnodes, M_HPFSHASH, &hpfs_hphash); 66 lwkt_token_init(&hpfs_hphash_token, "hpfsihash"); 67 } 68 69 /* 70 * Free the inode hash. 71 */ 72 int 73 hpfs_hphash_uninit(struct vfsconf *vfc) 74 { 75 lwkt_gettoken(&hpfs_hphash_token); 76 if (hpfs_hphashtbl) 77 hashdestroy(hpfs_hphashtbl, M_HPFSHASH, hpfs_hphash); 78 lwkt_reltoken(&hpfs_hphash_token); 79 80 return 0; 81 } 82 83 /* 84 * Use the device/inum pair to find the incore inode, and return a pointer 85 * to it. If it is in core, return it, even if it is locked. 86 */ 87 struct hpfsnode * 88 hpfs_hphashlookup(cdev_t dev, lsn_t ino) 89 { 90 struct hpfsnode *hp; 91 92 lwkt_gettoken(&hpfs_hphash_token); 93 for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) { 94 if (ino == hp->h_no && dev == hp->h_dev) 95 break; 96 } 97 lwkt_reltoken(&hpfs_hphash_token); 98 99 return (hp); 100 } 101 102 struct vnode * 103 hpfs_hphashvget(cdev_t dev, lsn_t ino) 104 { 105 struct hpfsnode *hp; 106 struct vnode *vp; 107 108 lwkt_gettoken(&hpfs_hphash_token); 109 loop: 110 for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) { 111 if (ino != hp->h_no || dev != hp->h_dev) 112 continue; 113 vp = HPTOV(hp); 114 115 if (vget(vp, LK_EXCLUSIVE)) 116 goto loop; 117 /* 118 * We must check to see if the inode has been ripped 119 * out from under us after blocking. 120 */ 121 for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) { 122 if (ino == hp->h_no && dev == hp->h_dev) 123 break; 124 } 125 if (hp == NULL || vp != HPTOV(hp)) { 126 vput(vp); 127 goto loop; 128 } 129 130 /* 131 * Or if the vget fails (due to a race) 132 */ 133 lwkt_reltoken(&hpfs_hphash_token); 134 return (vp); 135 } 136 lwkt_reltoken(&hpfs_hphash_token); 137 return (NULLVP); 138 } 139 140 /* 141 * Insert the hpfsnode into the hash table. 142 */ 143 void 144 hpfs_hphashins(struct hpfsnode *hp) 145 { 146 struct hphashhead *hpp; 147 148 lwkt_gettoken(&hpfs_hphash_token); 149 hpp = HPNOHASH(hp->h_dev, hp->h_no); 150 hp->h_flag |= H_HASHED; 151 LIST_INSERT_HEAD(hpp, hp, h_hash); 152 lwkt_reltoken(&hpfs_hphash_token); 153 } 154 155 /* 156 * Remove the inode from the hash table. 157 */ 158 void 159 hpfs_hphashrem(struct hpfsnode *hp) 160 { 161 lwkt_gettoken(&hpfs_hphash_token); 162 if (hp->h_flag & H_HASHED) { 163 hp->h_flag &= ~H_HASHED; 164 LIST_REMOVE(hp, h_hash); 165 #ifdef DIAGNOSTIC 166 hp->h_hash.le_next = NULL; 167 hp->h_hash.le_prev = NULL; 168 #endif 169 } 170 lwkt_reltoken(&hpfs_hphash_token); 171 } 172