1 /* $OpenBSD: ufs_ihash.c,v 1.16 2010/07/19 21:13:43 deraadt Exp $ */ 2 /* $NetBSD: ufs_ihash.c,v 1.3 1996/02/09 22:36:04 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. 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 * @(#)ufs_ihash.c 8.4 (Berkeley) 12/30/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/vnode.h> 38 #include <sys/malloc.h> 39 #include <sys/proc.h> 40 41 #include <ufs/ufs/quota.h> 42 #include <ufs/ufs/inode.h> 43 #include <ufs/ufs/ufs_extern.h> 44 45 /* 46 * Structures associated with inode cacheing. 47 */ 48 LIST_HEAD(ihashhead, inode) *ihashtbl; 49 u_long ihash; /* size of hash table - 1 */ 50 #define INOHASH(device, inum) (&ihashtbl[((device) + (inum)) & ihash]) 51 52 /* 53 * Initialize inode hash table. 54 */ 55 void 56 ufs_ihashinit(void) 57 { 58 ihashtbl = hashinit(desiredvnodes, M_UFSMNT, M_WAITOK, &ihash); 59 } 60 61 /* 62 * Use the device/inum pair to find the incore inode, and return a pointer 63 * to it. If it is in core, return it, even if it is locked. 64 */ 65 struct vnode * 66 ufs_ihashlookup(dev_t dev, ino_t inum) 67 { 68 struct inode *ip; 69 70 /* XXXLOCKING lock hash list */ 71 LIST_FOREACH(ip, INOHASH(dev, inum), i_hash) 72 if (inum == ip->i_number && dev == ip->i_dev) 73 break; 74 /* XXXLOCKING unlock hash list? */ 75 76 if (ip) 77 return (ITOV(ip)); 78 79 return (NULLVP); 80 } 81 82 /* 83 * Use the device/inum pair to find the incore inode, and return a pointer 84 * to it. If it is in core, but locked, wait for it. 85 */ 86 struct vnode * 87 ufs_ihashget(dev_t dev, ino_t inum) 88 { 89 struct proc *p = curproc; 90 struct inode *ip; 91 struct vnode *vp; 92 loop: 93 /* XXXLOCKING lock hash list */ 94 LIST_FOREACH(ip, INOHASH(dev, inum), i_hash) { 95 if (inum == ip->i_number && dev == ip->i_dev) { 96 vp = ITOV(ip); 97 /* XXXLOCKING unlock hash list? */ 98 if (vget(vp, LK_EXCLUSIVE, p)) 99 goto loop; 100 return (vp); 101 } 102 } 103 /* XXXLOCKING unlock hash list? */ 104 return (NULL); 105 } 106 107 /* 108 * Insert the inode into the hash table, and return it locked. 109 */ 110 int 111 ufs_ihashins(struct inode *ip) 112 { 113 struct inode *curip; 114 struct ihashhead *ipp; 115 dev_t dev = ip->i_dev; 116 ino_t inum = ip->i_number; 117 118 /* lock the inode, then put it on the appropriate hash list */ 119 lockmgr(&ip->i_lock, LK_EXCLUSIVE, NULL); 120 121 /* XXXLOCKING lock hash list */ 122 123 LIST_FOREACH(curip, INOHASH(dev, inum), i_hash) { 124 if (inum == curip->i_number && dev == curip->i_dev) { 125 /* XXXLOCKING unlock hash list? */ 126 lockmgr(&ip->i_lock, LK_RELEASE, NULL); 127 return (EEXIST); 128 } 129 } 130 131 ipp = INOHASH(dev, inum); 132 LIST_INSERT_HEAD(ipp, ip, i_hash); 133 /* XXXLOCKING unlock hash list? */ 134 135 return (0); 136 } 137 138 /* 139 * Remove the inode from the hash table. 140 */ 141 void 142 ufs_ihashrem(struct inode *ip) 143 { 144 /* XXXLOCKING lock hash list */ 145 146 if (ip->i_hash.le_prev == NULL) 147 return; 148 149 LIST_REMOVE(ip, i_hash); 150 #ifdef DIAGNOSTIC 151 ip->i_hash.le_next = NULL; 152 ip->i_hash.le_prev = NULL; 153 #endif 154 /* XXXLOCKING unlock hash list? */ 155 } 156