1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* $NetBSD: filecore_node.c,v 1.26 2014/02/27 16:51:38 hannken Exp $ */
/*-
* Copyright (c) 1982, 1986, 1989, 1994
* The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* filecore_node.c 1.0 1998/6/4
*/
/*-
* Copyright (c) 1998 Andrew McMurry
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* filecore_node.c 1.0 1998/6/4
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: filecore_node.c,v 1.26 2014/02/27 16:51:38 hannken Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/file.h>
#include <sys/buf.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/kernel.h>
#include <sys/pool.h>
#include <sys/stat.h>
#include <sys/mutex.h>
#include <fs/filecorefs/filecore.h>
#include <fs/filecorefs/filecore_extern.h>
#include <fs/filecorefs/filecore_node.h>
#include <fs/filecorefs/filecore_mount.h>
/*
* Structures associated with filecore_node caching.
*/
static LIST_HEAD(ihashhead, filecore_node) *filecorehashtbl;
static u_long filecorehash;
#define INOHASH(device, inum) (((device) + ((inum)>>12)) & filecorehash)
static kmutex_t filecore_ihash_lock;
struct pool filecore_node_pool;
extern int prtactive; /* 1 => print out reclaim of active vnodes */
/*
* Initialize hash links for inodes and dnodes.
*/
void
filecore_init(void)
{
mutex_init(&filecore_ihash_lock, MUTEX_DEFAULT, IPL_NONE);
pool_init(&filecore_node_pool, sizeof(struct filecore_node), 0, 0, 0,
"filecrnopl", &pool_allocator_nointr, IPL_NONE);
filecorehashtbl = hashinit(desiredvnodes, HASH_LIST, true,
&filecorehash);
}
/*
* Reinitialize inode hash table.
*/
void
filecore_reinit(void)
{
struct filecore_node *ip;
struct ihashhead *oldhash, *hash;
u_long oldmask, mask, val;
int i;
hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
mutex_enter(&filecore_ihash_lock);
oldhash = filecorehashtbl;
oldmask = filecorehash;
filecorehashtbl = hash;
filecorehash = mask;
for (i = 0; i <= oldmask; i++) {
while ((ip = LIST_FIRST(&oldhash[i])) != NULL) {
LIST_REMOVE(ip, i_hash);
val = INOHASH(ip->i_dev, ip->i_number);
LIST_INSERT_HEAD(&hash[val], ip, i_hash);
}
}
mutex_exit(&filecore_ihash_lock);
hashdone(oldhash, HASH_LIST, oldmask);
}
/*
* Destroy node pool and hash table.
*/
void
filecore_done(void)
{
hashdone(filecorehashtbl, HASH_LIST, filecorehash);
pool_destroy(&filecore_node_pool);
mutex_destroy(&filecore_ihash_lock);
}
/*
* Use the device/inum pair to find the incore inode, and return a pointer
* to it. If it is in core, but locked, wait for it.
*/
struct vnode *
filecore_ihashget(dev_t dev, ino_t inum)
{
struct filecore_node *ip;
struct vnode *vp;
loop:
mutex_enter(&filecore_ihash_lock);
LIST_FOREACH(ip, &filecorehashtbl[INOHASH(dev, inum)], i_hash) {
if (inum == ip->i_number && dev == ip->i_dev) {
vp = ITOV(ip);
mutex_enter(vp->v_interlock);
mutex_exit(&filecore_ihash_lock);
if (vget(vp, LK_EXCLUSIVE))
goto loop;
return (vp);
}
}
mutex_exit(&filecore_ihash_lock);
return (NULL);
}
/*
* Insert the inode into the hash table, and return it locked.
*/
void
filecore_ihashins(struct filecore_node *ip)
{
struct ihashhead *ipp;
int error __diagused;
mutex_enter(&filecore_ihash_lock);
ipp = &filecorehashtbl[INOHASH(ip->i_dev, ip->i_number)];
LIST_INSERT_HEAD(ipp, ip, i_hash);
mutex_exit(&filecore_ihash_lock);
error = VOP_LOCK(ITOV(ip), LK_EXCLUSIVE);
KASSERT(error == 0);
}
/*
* Remove the inode from the hash table.
*/
void
filecore_ihashrem(struct filecore_node *ip)
{
mutex_enter(&filecore_ihash_lock);
LIST_REMOVE(ip, i_hash);
mutex_exit(&filecore_ihash_lock);
}
/*
* Last reference to an inode, write the inode out and if necessary,
* truncate and deallocate the file.
*/
int
filecore_inactive(void *v)
{
struct vop_inactive_args /* {
struct vnode *a_vp;
bool *a_recycle;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct filecore_node *ip = VTOI(vp);
int error = 0;
/*
* If we are done with the inode, reclaim it
* so that it can be reused immediately.
*/
ip->i_flag = 0;
*ap->a_recycle = (filecore_staleinode(ip) != 0);
VOP_UNLOCK(vp);
return error;
}
/*
* Reclaim an inode so that it can be used for other purposes.
*/
int
filecore_reclaim(void *v)
{
struct vop_reclaim_args /* {
struct vnode *a_vp;
struct lwp *a_l;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct filecore_node *ip = VTOI(vp);
if (prtactive && vp->v_usecount > 1)
vprint("filecore_reclaim: pushing active", vp);
/*
* Remove the inode from its hash chain.
*/
filecore_ihashrem(ip);
/*
* Purge old data structures associated with the inode.
*/
if (ip->i_devvp) {
vrele(ip->i_devvp);
ip->i_devvp = 0;
}
genfs_node_destroy(vp);
pool_put(&filecore_node_pool, vp->v_data);
vp->v_data = NULL;
return (0);
}
|