xref: /dragonfly/sys/vfs/ufs/ufs_ihash.c (revision 984263bc)
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/ufs/ufs/ufs_ihash.c,v 1.20 1999/08/28 00:52:29 peter 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/malloc.h>
43 #include <sys/proc.h>
44 
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/inode.h>
47 #include <ufs/ufs/ufs_extern.h>
48 
49 static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables");
50 /*
51  * Structures associated with inode cacheing.
52  */
53 static LIST_HEAD(ihashhead, inode) *ihashtbl;
54 static u_long	ihash;		/* size of hash table - 1 */
55 #define	INOHASH(device, inum)	(&ihashtbl[(minor(device) + (inum)) & ihash])
56 #ifndef NULL_SIMPLELOCKS
57 static struct simplelock ufs_ihash_slock;
58 #endif
59 
60 /*
61  * Initialize inode hash table.
62  */
63 void
64 ufs_ihashinit()
65 {
66 
67 	ihashtbl = hashinit(desiredvnodes, M_UFSIHASH, &ihash);
68 	simple_lock_init(&ufs_ihash_slock);
69 }
70 
71 /*
72  * Use the device/inum pair to find the incore inode, and return a pointer
73  * to it. If it is in core, return it, even if it is locked.
74  */
75 struct vnode *
76 ufs_ihashlookup(dev, inum)
77 	dev_t dev;
78 	ino_t inum;
79 {
80 	struct inode *ip;
81 
82 	simple_lock(&ufs_ihash_slock);
83 	for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next)
84 		if (inum == ip->i_number && dev == ip->i_dev)
85 			break;
86 	simple_unlock(&ufs_ihash_slock);
87 
88 	if (ip)
89 		return (ITOV(ip));
90 	return (NULLVP);
91 }
92 
93 /*
94  * Use the device/inum pair to find the incore inode, and return a pointer
95  * to it. If it is in core, but locked, wait for it.
96  */
97 struct vnode *
98 ufs_ihashget(dev, inum)
99 	dev_t dev;
100 	ino_t inum;
101 {
102 	struct proc *p = curproc;	/* XXX */
103 	struct inode *ip;
104 	struct vnode *vp;
105 
106 loop:
107 	simple_lock(&ufs_ihash_slock);
108 	for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
109 		if (inum == ip->i_number && dev == ip->i_dev) {
110 			vp = ITOV(ip);
111 			simple_lock(&vp->v_interlock);
112 			simple_unlock(&ufs_ihash_slock);
113 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
114 				goto loop;
115 			return (vp);
116 		}
117 	}
118 	simple_unlock(&ufs_ihash_slock);
119 	return (NULL);
120 }
121 
122 /*
123  * Insert the inode into the hash table, and return it locked.
124  */
125 void
126 ufs_ihashins(ip)
127 	struct inode *ip;
128 {
129 	struct proc *p = curproc;		/* XXX */
130 	struct ihashhead *ipp;
131 
132 	/* lock the inode, then put it on the appropriate hash list */
133 	lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct simplelock *)0, p);
134 
135 	simple_lock(&ufs_ihash_slock);
136 	ipp = INOHASH(ip->i_dev, ip->i_number);
137 	LIST_INSERT_HEAD(ipp, ip, i_hash);
138 	ip->i_flag |= IN_HASHED;
139 	simple_unlock(&ufs_ihash_slock);
140 }
141 
142 /*
143  * Remove the inode from the hash table.
144  */
145 void
146 ufs_ihashrem(ip)
147 	struct inode *ip;
148 {
149 	simple_lock(&ufs_ihash_slock);
150 	if (ip->i_flag & IN_HASHED) {
151 		ip->i_flag &= ~IN_HASHED;
152 		LIST_REMOVE(ip, i_hash);
153 #ifdef DIAGNOSTIC
154 		ip->i_hash.le_next = NULL;
155 		ip->i_hash.le_prev = NULL;
156 #endif
157 	}
158 	simple_unlock(&ufs_ihash_slock);
159 }
160