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. 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/ufs/ufs/ufs_ihash.c,v 1.20 1999/08/28 00:52:29 peter 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/malloc.h> 39 #include <sys/proc.h> 40 #include <sys/mount.h> 41 42 #include "quota.h" 43 #include "inode.h" 44 #include "ufs_extern.h" 45 #include "ufsmount.h" 46 47 static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables"); 48 49 #define INOHASH(ump, inum) \ 50 (&ump->um_ihashtbl[inum & ump->um_ihash]) 51 52 /* 53 * Initialize inode hash table. 54 */ 55 void 56 ufs_ihashinit(struct ufsmount *ump) 57 { 58 ump->um_ihash = vfs_inodehashsize(); 59 ump->um_ihashtbl = kmalloc(sizeof(void *) * ump->um_ihash, 60 M_UFSIHASH, 61 M_WAITOK|M_ZERO); 62 --ump->um_ihash; 63 } 64 65 void 66 ufs_ihashuninit(struct ufsmount *ump) 67 { 68 if (ump->um_ihashtbl) { 69 kfree(ump->um_ihashtbl, M_UFSIHASH); 70 ump->um_ihashtbl = NULL; 71 } 72 } 73 74 /* 75 * Use the device/inum pair to find the incore inode, and return a pointer 76 * to it. If it is in core, return it, even if it is locked. 77 */ 78 struct vnode * 79 ufs_ihashlookup(struct ufsmount *ump, cdev_t dev, ino_t inum) 80 { 81 struct inode *ip = NULL; 82 83 for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) { 84 if (inum == ip->i_number && dev == ip->i_dev) 85 break; 86 } 87 if (ip) 88 return (ITOV(ip)); 89 return (NULLVP); 90 } 91 92 /* 93 * Use the device/inum pair to find the incore inode, and return a pointer 94 * to it. If it is in core, but locked, wait for it. 95 * 96 * This subroutine may block. 97 */ 98 struct vnode * 99 ufs_ihashget(struct ufsmount *ump, cdev_t dev, ino_t inum) 100 { 101 struct inode *ip; 102 struct vnode *vp; 103 104 loop: 105 for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) { 106 if (inum != ip->i_number || dev != ip->i_dev) 107 continue; 108 vp = ITOV(ip); 109 if (vget(vp, LK_EXCLUSIVE)) 110 goto loop; 111 /* 112 * We must check to see if the inode has been ripped 113 * out from under us after blocking. 114 */ 115 for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) { 116 if (inum == ip->i_number && dev == ip->i_dev) 117 break; 118 } 119 if (ip == NULL || ITOV(ip) != vp) { 120 vput(vp); 121 goto loop; 122 } 123 return (vp); 124 } 125 return (NULL); 126 } 127 128 /* 129 * Check to see if an inode is in the hash table. This is used to interlock 130 * file free operations to ensure that the vnode is not reused due to a 131 * reallocate of its inode number before we have had a chance to recycle it. 132 */ 133 int 134 ufs_ihashcheck(struct ufsmount *ump, cdev_t dev, ino_t inum) 135 { 136 struct inode *ip; 137 138 for (ip = *INOHASH(ump, inum); ip; ip = ip->i_next) { 139 if (inum == ip->i_number && dev == ip->i_dev) 140 break; 141 } 142 return(ip ? 1 : 0); 143 } 144 145 /* 146 * Insert the inode into the hash table, and return it locked. 147 */ 148 int 149 ufs_ihashins(struct ufsmount *ump, struct inode *ip) 150 { 151 struct inode **ipp; 152 struct inode *iq; 153 154 KKASSERT((ip->i_flag & IN_HASHED) == 0); 155 ipp = INOHASH(ump, ip->i_number); 156 while ((iq = *ipp) != NULL) { 157 if (ip->i_dev == iq->i_dev && ip->i_number == iq->i_number) { 158 return(EBUSY); 159 } 160 ipp = &iq->i_next; 161 } 162 ip->i_next = NULL; 163 *ipp = ip; 164 ip->i_flag |= IN_HASHED; 165 return(0); 166 } 167 168 /* 169 * Remove the inode from the hash table. 170 */ 171 void 172 ufs_ihashrem(struct ufsmount *ump, struct inode *ip) 173 { 174 struct inode **ipp; 175 struct inode *iq; 176 177 if (ip->i_flag & IN_HASHED) { 178 ipp = INOHASH(ump, ip->i_number); 179 while ((iq = *ipp) != NULL) { 180 if (ip == iq) 181 break; 182 ipp = &iq->i_next; 183 } 184 KKASSERT(ip == iq); 185 *ipp = ip->i_next; 186 ip->i_next = NULL; 187 ip->i_flag &= ~IN_HASHED; 188 } 189 } 190 191