Merge FreeBSD rev. 1.8:
[dragonfly.git] / sys / vfs / ufs / ufs_ihash.c
CommitLineData
984263bc
MD
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 $
1bfd75da 35 * $DragonFly: src/sys/vfs/ufs/ufs_ihash.c,v 1.10 2004/01/15 20:17:36 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/vnode.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45
1f2de5d4
MD
46#include "quota.h"
47#include "inode.h"
48#include "ufs_extern.h"
984263bc
MD
49
50static MALLOC_DEFINE(M_UFSIHASH, "UFS ihash", "UFS Inode hash tables");
51/*
52 * Structures associated with inode cacheing.
53 */
54static LIST_HEAD(ihashhead, inode) *ihashtbl;
55static u_long ihash; /* size of hash table - 1 */
56#define INOHASH(device, inum) (&ihashtbl[(minor(device) + (inum)) & ihash])
8a8d5d85 57static struct lwkt_token ufs_ihash_token;
984263bc
MD
58
59/*
60 * Initialize inode hash table.
61 */
62void
63ufs_ihashinit()
64{
984263bc 65 ihashtbl = hashinit(desiredvnodes, M_UFSIHASH, &ihash);
8a8d5d85 66 lwkt_inittoken(&ufs_ihash_token);
984263bc
MD
67}
68
69/*
70 * Use the device/inum pair to find the incore inode, and return a pointer
71 * to it. If it is in core, return it, even if it is locked.
72 */
73struct vnode *
74ufs_ihashlookup(dev, inum)
75 dev_t dev;
76 ino_t inum;
77{
78 struct inode *ip;
79
8a8d5d85 80 lwkt_gettoken(&ufs_ihash_token);
1bfd75da 81 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
984263bc
MD
82 if (inum == ip->i_number && dev == ip->i_dev)
83 break;
1bfd75da 84 }
8a8d5d85 85 lwkt_reltoken(&ufs_ihash_token);
984263bc
MD
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.
4954c633
MD
95 *
96 * Any time we potentially block we must regenerate the token using
97 * lwkt_gentoken(), which at the same time checks the generation number
98 * indicating whether another entity stole the token while we were blocked.
99 * In the best case lwkt_gentoken(). The code below is probably overkill,
100 * but it is particularly important to check the generation number after
101 * acquiring the vnode lock to ensure that the inode association is still
102 * valid.
984263bc
MD
103 */
104struct vnode *
dadab5e9 105ufs_ihashget(dev_t dev, ino_t inum)
984263bc 106{
dadab5e9 107 struct thread *td = curthread; /* XXX */
984263bc
MD
108 struct inode *ip;
109 struct vnode *vp;
8a8d5d85 110 int gen;
984263bc 111
8a8d5d85 112 gen = lwkt_gettoken(&ufs_ihash_token);
984263bc 113loop:
984263bc
MD
114 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
115 if (inum == ip->i_number && dev == ip->i_dev) {
116 vp = ITOV(ip);
c64f58b3 117 vhold(vp);
4954c633 118 lwkt_gettoken(&vp->v_interlock);
8a8d5d85
MD
119 if (lwkt_gentoken(&ufs_ihash_token, &gen) != 0) {
120 lwkt_reltoken(&vp->v_interlock);
c64f58b3 121 vdrop(vp);
8a8d5d85
MD
122 goto loop;
123 }
124 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
c64f58b3 125 vdrop(vp);
8a8d5d85 126 lwkt_gentoken(&ufs_ihash_token, &gen);
984263bc 127 goto loop;
8a8d5d85 128 }
4954c633 129 if (lwkt_reltoken(&ufs_ihash_token) != gen) {
c64f58b3 130 vdrop(vp);
4954c633
MD
131 vput(vp);
132 gen = lwkt_gettoken(&ufs_ihash_token);
133 goto loop;
134 }
17cc1eb2 135 vdrop(vp);
984263bc
MD
136 return (vp);
137 }
138 }
8a8d5d85 139 lwkt_reltoken(&ufs_ihash_token);
984263bc
MD
140 return (NULL);
141}
142
143/*
144 * Insert the inode into the hash table, and return it locked.
145 */
146void
dadab5e9 147ufs_ihashins(struct inode *ip)
984263bc 148{
dadab5e9 149 struct thread *td = curthread; /* XXX */
984263bc
MD
150 struct ihashhead *ipp;
151
152 /* lock the inode, then put it on the appropriate hash list */
8a8d5d85 153 lockmgr(&ip->i_lock, LK_EXCLUSIVE, NULL, td);
984263bc 154
8a8d5d85 155 lwkt_gettoken(&ufs_ihash_token);
984263bc
MD
156 ipp = INOHASH(ip->i_dev, ip->i_number);
157 LIST_INSERT_HEAD(ipp, ip, i_hash);
158 ip->i_flag |= IN_HASHED;
8a8d5d85 159 lwkt_reltoken(&ufs_ihash_token);
984263bc
MD
160}
161
162/*
163 * Remove the inode from the hash table.
164 */
165void
166ufs_ihashrem(ip)
167 struct inode *ip;
168{
8a8d5d85 169 lwkt_gettoken(&ufs_ihash_token);
984263bc
MD
170 if (ip->i_flag & IN_HASHED) {
171 ip->i_flag &= ~IN_HASHED;
172 LIST_REMOVE(ip, i_hash);
173#ifdef DIAGNOSTIC
174 ip->i_hash.le_next = NULL;
175 ip->i_hash.le_prev = NULL;
176#endif
177 }
8a8d5d85 178 lwkt_reltoken(&ufs_ihash_token);
984263bc 179}