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