]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_ihash.c
cc463f4919b335bea40c675a0a2fbeaf1a88fc38
[apple/xnu.git] / bsd / ufs / ufs / ufs_ihash.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
56 */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
61 #include <sys/malloc.h>
62 #include <sys/proc.h>
63
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/ufs_extern.h>
67
68 /*
69 * Structures associated with inode cacheing.
70 */
71 LIST_HEAD(ihashhead, inode) *ihashtbl;
72 u_long ihash; /* size of hash table - 1 */
73 #define INOHASH(device, inum) (&ihashtbl[((device) + (inum)) & ihash])
74 struct slock ufs_ihash_slock;
75
76 /*
77 * Initialize inode hash table.
78 */
79 void
80 ufs_ihashinit()
81 {
82
83 ihashtbl = hashinit(desiredvnodes, M_UFSMNT, &ihash);
84 simple_lock_init(&ufs_ihash_slock);
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 vnode *
92 ufs_ihashlookup(dev, inum)
93 dev_t dev;
94 ino_t inum;
95 {
96 struct inode *ip;
97
98 simple_lock(&ufs_ihash_slock);
99 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next)
100 if (inum == ip->i_number && dev == ip->i_dev)
101 break;
102 simple_unlock(&ufs_ihash_slock);
103
104 if (ip)
105 return (ITOV(ip));
106 return (NULLVP);
107 }
108
109 /*
110 * Use the device/inum pair to find the incore inode, and return a pointer
111 * to it. If it is in core, but locked, wait for it.
112 */
113 struct vnode *
114 ufs_ihashget(dev, inum)
115 dev_t dev;
116 ino_t inum;
117 {
118 struct proc *p = current_proc(); /* XXX */
119 struct inode *ip;
120 struct vnode *vp;
121
122 loop:
123 simple_lock(&ufs_ihash_slock);
124 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
125 if (inum == ip->i_number && dev == ip->i_dev) {
126 vp = ITOV(ip);
127 if (ip->i_flag & IN_TRANSIT) {
128 /* inode is getting reclaimed wait till
129 * the operation is complete and return
130 * error
131 */
132 ip->i_flag |= IN_WTRANSIT;
133 simple_unlock(&ufs_ihash_slock);
134 tsleep((caddr_t)ip, PINOD, "ufs_ihashget", 0);
135 goto loop;
136 }
137 simple_lock(&vp->v_interlock);
138 simple_unlock(&ufs_ihash_slock);
139 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
140 goto loop;
141 return (vp);
142 }
143 }
144 simple_unlock(&ufs_ihash_slock);
145 return (NULL);
146 }
147
148 /*
149 * Insert the inode into the hash table, and return it locked.
150 */
151 void
152 ufs_ihashins(ip)
153 struct inode *ip;
154 {
155 struct proc *p = current_proc(); /* XXX */
156 struct ihashhead *ipp;
157
158 /* lock the inode, then put it on the appropriate hash list */
159 lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct slock *)0, p);
160
161 simple_lock(&ufs_ihash_slock);
162 ipp = INOHASH(ip->i_dev, ip->i_number);
163 LIST_INSERT_HEAD(ipp, ip, i_hash);
164 simple_unlock(&ufs_ihash_slock);
165 }
166
167 /*
168 * Remove the inode from the hash table.
169 */
170 void
171 ufs_ihashrem(ip)
172 struct inode *ip;
173 {
174 struct inode *iq;
175
176 simple_lock(&ufs_ihash_slock);
177 LIST_REMOVE(ip, i_hash);
178 #if DIAGNOSTIC
179 ip->i_hash.le_next = NULL;
180 ip->i_hash.le_prev = NULL;
181 #endif
182 simple_unlock(&ufs_ihash_slock);
183 }