nrelease - fix/improve livecd
[dragonfly.git] / sys / vfs / hpfs / hpfs_hash.c
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
30  * $FreeBSD: src/sys/fs/hpfs/hpfs_hash.c,v 1.1 1999/12/09 19:09:58 semenu Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41
42 #include "hpfs.h"
43
44 MALLOC_DEFINE(M_HPFSHASH, "HPFS hash", "HPFS node hash tables");
45
46 /*
47  * Structures associated with hpfsnode cacheing.
48  */
49 static LIST_HEAD(hphashhead, hpfsnode) *hpfs_hphashtbl;
50 static u_long   hpfs_hphash;            /* size of hash table - 1 */
51 #define HPNOHASH(dev, lsn)      (&hpfs_hphashtbl[(minor(dev) + (lsn)) & hpfs_hphash])
52 #ifndef NULL_SIMPLELOCKS
53 static struct lwkt_token hpfs_hphash_token;
54 #endif
55 struct lock hpfs_hphash_lock;
56
57 /*
58  * Initialize inode hash table.
59  */
60 void
61 hpfs_hphashinit(void)
62 {
63         int hsize = vfs_inodehashsize();
64
65         lockinit(&hpfs_hphash_lock, "hpfs_hphashlock", 0, 0);
66         hpfs_hphashtbl = hashinit(hsize, M_HPFSHASH, &hpfs_hphash);
67         lwkt_token_init(&hpfs_hphash_token, "hpfsihash");
68 }
69
70 /*
71  * Free the inode hash.
72  */
73 int
74 hpfs_hphash_uninit(struct vfsconf *vfc)
75 {
76         lwkt_gettoken(&hpfs_hphash_token);
77         if (hpfs_hphashtbl)
78                 hashdestroy(hpfs_hphashtbl, M_HPFSHASH, hpfs_hphash);
79         lwkt_reltoken(&hpfs_hphash_token);
80
81         return 0;
82 }
83
84 /*
85  * Use the device/inum pair to find the incore inode, and return a pointer
86  * to it. If it is in core, return it, even if it is locked.
87  */
88 struct hpfsnode *
89 hpfs_hphashlookup(cdev_t dev, lsn_t ino)
90 {
91         struct hpfsnode *hp;
92
93         lwkt_gettoken(&hpfs_hphash_token);
94         for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) {
95                 if (ino == hp->h_no && dev == hp->h_dev)
96                         break;
97         }
98         lwkt_reltoken(&hpfs_hphash_token);
99
100         return (hp);
101 }
102
103 struct vnode *
104 hpfs_hphashvget(cdev_t dev, lsn_t ino)
105 {
106         struct hpfsnode *hp;
107         struct vnode *vp;
108
109         lwkt_gettoken(&hpfs_hphash_token);
110 loop:
111         for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) {
112                 if (ino != hp->h_no || dev != hp->h_dev)
113                         continue;
114                 vp = HPTOV(hp);
115
116                 if (vget(vp, LK_EXCLUSIVE))
117                         goto loop;
118                 /*
119                  * We must check to see if the inode has been ripped
120                  * out from under us after blocking.
121                  */
122                 for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) {
123                         if (ino == hp->h_no && dev == hp->h_dev)
124                                 break;
125                 }
126                 if (hp == NULL || vp != HPTOV(hp)) {
127                         vput(vp);
128                         goto loop;
129                 }
130
131                 /*
132                  * Or if the vget fails (due to a race)
133                  */
134                 lwkt_reltoken(&hpfs_hphash_token);
135                 return (vp);
136         }
137         lwkt_reltoken(&hpfs_hphash_token);
138         return (NULLVP);
139 }
140
141 /*
142  * Insert the hpfsnode into the hash table.
143  */
144 void
145 hpfs_hphashins(struct hpfsnode *hp)
146 {
147         struct hphashhead *hpp;
148
149         lwkt_gettoken(&hpfs_hphash_token);
150         hpp = HPNOHASH(hp->h_dev, hp->h_no);
151         hp->h_flag |= H_HASHED;
152         LIST_INSERT_HEAD(hpp, hp, h_hash);
153         lwkt_reltoken(&hpfs_hphash_token);
154 }
155
156 /*
157  * Remove the inode from the hash table.
158  */
159 void
160 hpfs_hphashrem(struct hpfsnode *hp)
161 {
162         lwkt_gettoken(&hpfs_hphash_token);
163         if (hp->h_flag & H_HASHED) {
164                 hp->h_flag &= ~H_HASHED;
165                 LIST_REMOVE(hp, h_hash);
166 #ifdef DIAGNOSTIC
167                 hp->h_hash.le_next = NULL;
168                 hp->h_hash.le_prev = NULL;
169 #endif
170         }
171         lwkt_reltoken(&hpfs_hphash_token);
172 }