Since ip_input() truncates the packet to ip->ip_len prior to entering the
[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 $
0973c589 35 * $DragonFly: src/sys/vfs/ufs/ufs_ihash.c,v 1.12 2004/05/18 00:16:46 cpressey 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
0973c589 63ufs_ihashinit(void)
984263bc 64{
984263bc 65 ihashtbl = hashinit(desiredvnodes, M_UFSIHASH, &ihash);
41a01a4d 66 lwkt_token_init(&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 *
0973c589 74ufs_ihashlookup(dev_t dev, ino_t inum)
984263bc
MD
75{
76 struct inode *ip;
41a01a4d 77 lwkt_tokref ilock;
984263bc 78
41a01a4d 79 lwkt_gettoken(&ilock, &ufs_ihash_token);
1bfd75da 80 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
984263bc
MD
81 if (inum == ip->i_number && dev == ip->i_dev)
82 break;
1bfd75da 83 }
41a01a4d 84 lwkt_reltoken(&ilock);
984263bc
MD
85
86 if (ip)
87 return (ITOV(ip));
88 return (NULLVP);
89}
90
91/*
92 * Use the device/inum pair to find the incore inode, and return a pointer
93 * to it. If it is in core, but locked, wait for it.
4954c633 94 *
41a01a4d
MD
95 * Note that the serializing tokens do not prevent other processes from
96 * playing with the data structure being protected while we are blocked.
97 * They do protect us from preemptive interrupts which might try to
98 * play with the protected data structure.
984263bc
MD
99 */
100struct vnode *
dadab5e9 101ufs_ihashget(dev_t dev, ino_t inum)
984263bc 102{
dadab5e9 103 struct thread *td = curthread; /* XXX */
41a01a4d
MD
104 lwkt_tokref ilock;
105 lwkt_tokref vlock;
984263bc
MD
106 struct inode *ip;
107 struct vnode *vp;
108
41a01a4d 109 lwkt_gettoken(&ilock, &ufs_ihash_token);
984263bc 110loop:
984263bc 111 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
41a01a4d
MD
112 if (inum != ip->i_number || dev != ip->i_dev)
113 continue;
114 vp = ITOV(ip);
115 lwkt_gettoken(&vlock, vp->v_interlock);
116 /*
117 * We must check to see if the inode has been ripped
118 * out from under us after blocking.
119 */
120 for (ip = INOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
121 if (inum == ip->i_number && dev == ip->i_dev)
122 break;
984263bc 123 }
41a01a4d
MD
124 if (ip == NULL || ITOV(ip) != vp) {
125 lwkt_reltoken(&vlock);
126 goto loop;
127 }
128 if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td))
129 goto loop;
130 lwkt_reltoken(&ilock);
131 return (vp);
984263bc 132 }
41a01a4d 133 lwkt_reltoken(&ilock);
984263bc
MD
134 return (NULL);
135}
136
137/*
138 * Insert the inode into the hash table, and return it locked.
139 */
140void
dadab5e9 141ufs_ihashins(struct inode *ip)
984263bc 142{
dadab5e9 143 struct thread *td = curthread; /* XXX */
984263bc 144 struct ihashhead *ipp;
41a01a4d 145 lwkt_tokref ilock;
984263bc
MD
146
147 /* lock the inode, then put it on the appropriate hash list */
8a8d5d85 148 lockmgr(&ip->i_lock, LK_EXCLUSIVE, NULL, td);
984263bc 149
41a01a4d 150 lwkt_gettoken(&ilock, &ufs_ihash_token);
984263bc
MD
151 ipp = INOHASH(ip->i_dev, ip->i_number);
152 LIST_INSERT_HEAD(ipp, ip, i_hash);
153 ip->i_flag |= IN_HASHED;
41a01a4d 154 lwkt_reltoken(&ilock);
984263bc
MD
155}
156
157/*
158 * Remove the inode from the hash table.
159 */
160void
0973c589 161ufs_ihashrem(struct inode *ip)
984263bc 162{
41a01a4d
MD
163 lwkt_tokref ilock;
164
165 lwkt_gettoken(&ilock, &ufs_ihash_token);
984263bc
MD
166 if (ip->i_flag & IN_HASHED) {
167 ip->i_flag &= ~IN_HASHED;
168 LIST_REMOVE(ip, i_hash);
169#ifdef DIAGNOSTIC
170 ip->i_hash.le_next = NULL;
171 ip->i_hash.le_prev = NULL;
172#endif
173 }
41a01a4d 174 lwkt_reltoken(&ilock);
984263bc 175}