forked from torvalds/linux
/
inode.c
435 lines (367 loc) · 8.99 KB
/
inode.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Tux3 versioning filesystem in user space
*
* Original copyright (c) 2008 Daniel Phillips <phillips@phunq.net>
* Licensed under the GPL version 3
*
* By contributing changes to this file you grant the original copyright holder
* the right to distribute those changes under any license.
*/
#include "tux3user.h"
#ifndef trace
#define trace trace_on
#endif
#define HASH_SHIFT 10
#define HASH_SIZE (1 << 10)
#define HASH_MASK (HASH_SIZE - 1)
static struct hlist_head inode_hashtable[HASH_SIZE] = {
[0 ... (HASH_SIZE - 1)] = HLIST_HEAD_INIT,
};
static unsigned int hash(inum_t inum)
{
return hash_64(inum, HASH_SHIFT);
}
void inode_leak_check(void)
{
int leaks = 0;
for (int i = 0; i < HASH_SIZE; i++) {
struct hlist_head *head = inode_hashtable + i;
struct inode *inode;
hlist_for_each_entry(inode, head, i_hash) {
trace_on("possible leak inode inum %Lu, i_count %d",
tux_inode(inode)->inum,
atomic_read(&inode->i_count));
leaks++;
}
}
assert(leaks == 0);
}
static inline int inode_unhashed(struct inode *inode)
{
return hlist_unhashed(&inode->i_hash);
}
static void insert_inode_hash(struct inode *inode)
{
struct hlist_head *b = inode_hashtable + hash(tux_inode(inode)->inum);
hlist_add_head(&inode->i_hash, b);
}
void remove_inode_hash(struct inode *inode)
{
if (!inode_unhashed(inode))
hlist_del_init(&inode->i_hash);
}
static void destroy_inode_nocheck(struct inode *inode)
{
free_map(mapping(inode));
__destroy_inode_nocheck(inode);
}
static void destroy_inode(struct inode *inode)
{
assert(hlist_unhashed(&inode->i_hash));
if (mapping(inode))
free_map(mapping(inode));
__destroy_inode(inode);
}
void inode_init_once(struct inode *inode)
{
memset(inode, 0, sizeof(*inode));
INIT_HLIST_NODE(&inode->i_hash);
}
static void inode_init_always(struct super_block *sb, struct inode *inode)
{
inode->i_sb = sb;
inode->i_nlink = 1;
atomic_set(&inode->i_count, 1);
spin_lock_init(&inode->i_lock);
init_rwsem(&inode->i_rwsem);
mapping(inode)->inode = inode;
mapping_set_gfp_mask(mapping(inode), GFP_HIGHUSER_MOVABLE);
}
static struct inode *new_inode(struct super_block *sb)
{
struct inode *inode = __alloc_inode(sb);
if (!inode)
goto error;
inode->map = new_map(sb_dev(tux_sb(sb)), NULL);
if (!inode->map)
goto error_map;
inode_init_always(sb, inode);
return inode;
error_map:
destroy_inode(inode);
error:
return NULL;
}
/* This is just to clean inode is partially initialized */
static void make_bad_inode(struct inode *inode)
{
remove_inode_hash(inode);
inode->i_state |= I_BAD;
}
static int is_bad_inode(struct inode *inode)
{
return inode->i_state & I_BAD;
}
void unlock_new_inode(struct inode *inode)
{
WARN_ON(!(inode->i_state & I_NEW));
inode->i_state &= ~I_NEW & ~I_CREATING;
}
void discard_new_inode(struct inode *inode)
{
WARN_ON(!(inode->i_state & I_NEW));
inode->i_state &= ~I_NEW;
iput(inode);
}
static void iget_failed(struct inode *inode)
{
make_bad_inode(inode);
unlock_new_inode(inode);
iput(inode);
}
void __iget(struct inode *inode)
{
assert(!(inode->i_state & I_FREEING));
if (atomic_read(&inode->i_count)) {
atomic_inc(&inode->i_count);
return;
}
/* i_count == 0 should happen only dirty inode */
assert(inode->i_state & I_DIRTY);
atomic_inc(&inode->i_count);
}
/* This is used by tux3_clear_dirty_inodes() to tell inode state was changed */
void iget_if_dirty(struct inode *inode)
{
__iget(inode);
}
/* get additional reference to inode; caller must already hold one. */
void ihold(struct inode *inode)
{
assert(!(inode->i_state & I_FREEING));
assert(atomic_read(&inode->i_count) >= 1);
atomic_inc(&inode->i_count);
}
static struct inode *find_inode(struct super_block *sb, struct hlist_head *head,
int (*test)(struct inode *, void *),
void *data)
{
struct inode *inode = NULL;
repeat:
hlist_for_each_entry(inode, head, i_hash) {
if (inode->i_sb != sb)
continue;
if (!test(inode, data))
continue;
spin_lock(&inode->i_lock);
if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
assert(0); /* On userland, shouldn't happen */
spin_unlock(&inode->i_lock);
goto repeat;
}
if (unlikely(inode->i_state & I_CREATING)) {
spin_unlock(&inode->i_lock);
return ERR_PTR(-ESTALE);
}
__iget(inode);
spin_unlock(&inode->i_lock);
return inode;
}
return NULL;
}
static struct inode *ilookup5_nowait(struct super_block *sb, inum_t inum,
int (*test)(struct inode *, void *), void *data)
{
struct hlist_head *head = inode_hashtable + hash(inum);
struct inode *inode;
inode = find_inode(sb, head, test, data);
return inode;
}
static struct inode *ilookup5(struct super_block *sb, inum_t inum,
int (*test)(struct inode *, void *), void *data)
{
struct inode *inode = ilookup5_nowait(sb, inum, test, data);
again:
if (inode) {
/* On userland, inode shouldn't have I_NEW */
assert(!(inode->i_state & I_NEW));
//wait_on_inode(inode);
if (unlikely(inode_unhashed(inode))) {
iput(inode);
goto again;
}
}
return inode;
}
static struct inode *inode_insert5(struct inode *inode, inum_t inum,
int (*test)(struct inode *, void *),
int (*set)(struct inode *, void *), void *data)
{
struct hlist_head *head = inode_hashtable + hash(inum);
struct inode *old;
again:
old = find_inode(inode->i_sb, head, test, data);
if (unlikely(old)) {
/*
* Uhhuh, somebody else created the same inode under us.
* Use the old inode instead of the preallocated one.
*/
assert(!(inode->i_state & I_NEW));
//wait_on_inode(old);
if (unlikely(inode_unhashed(old))) {
iput(old);
goto again;
}
return old;
}
if (set && unlikely(set(inode, data))) {
inode = NULL;
goto unlock;
}
/*
* Return the locked inode with I_NEW set, the
* caller is responsible for filling in the contents
*/
spin_lock(&inode->i_lock);
inode->i_state |= I_NEW;
hlist_add_head(&inode->i_hash, head);
spin_unlock(&inode->i_lock);
unlock:
return inode;
}
static struct inode *iget5_locked(struct super_block *sb, inum_t inum,
int (*test)(struct inode *, void *),
int (*set)(struct inode *, void *), void *data)
{
struct inode *inode = ilookup5(sb, inum, test, data);
if (!inode) {
struct inode *new = new_inode(sb);
if (new) {
inode = inode_insert5(new, inum, test, set, data);
if (unlikely(inode != new))
iput(new);
}
}
return inode;
}
static int insert_inode_locked4(struct inode *inode, inum_t inum,
int (*test)(struct inode *, void *), void *data)
{
struct inode *old;
inode->i_state |= I_CREATING;
old = inode_insert5(inode, inum, test, NULL, data);
if (old != inode) {
iput(old);
return -EBUSY;
}
return 0;
}
loff_t i_size_read(const struct inode *inode)
{
return inode->i_size;
}
void i_size_write(struct inode *inode, loff_t i_size)
{
inode->i_size = i_size;
}
/* For now, we doesn't cache inode */
static int generic_drop_inode(struct inode *inode)
{
return 1;
}
#include "../inode.c"
static void tux_setup_inode(struct inode *inode)
{
tux_setup_inode_common(inode);
switch (inode->i_mode & S_IFMT) {
case S_IFSOCK:
case S_IFIFO:
case S_IFBLK:
case S_IFCHR:
inode->map->io = dev_errio;
break;
case S_IFREG:
// inode->map->io = tux3_filemap_overwrite_io;
inode->map->io = tux3_filemap_redirect_io;
break;
case S_IFDIR:
case S_IFLNK:
inode->map->io = tux3_filemap_redirect_io;
break;
case 0: /* internal inode */
/* FIXME: bitmap, logmap, vtable, atable doesn't have S_IFMT */
switch (tux_inode(inode)->inum) {
case TUX_BITMAP_INO:
case TUX_COUNTMAP_INO:
case TUX_VTABLE_INO:
case TUX_ATABLE_INO:
inode->map->io = tux3_filemap_redirect_io;
break;
case TUX_VOLMAP_INO:
case TUX_LOGMAP_INO:
if (tux_inode(inode)->inum == TUX_VOLMAP_INO)
/* use default handler (dev_blockio) */;
else
inode->map->io = tux3_logmap_io;
break;
}
break;
}
}
/*
* NOTE: iput() must not be called inside of change_begin/end() if
* i_nlink == 0. Otherwise, it will become cause of deadlock.
*/
void iput(struct inode *inode)
{
if (inode == NULL)
return;
if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
int drop;
assert(!(inode->i_state & I_NEW));
drop = tux3_drop_inode(inode);
if (!drop && (inode->i_sb->s_flags & SB_ACTIVE)) {
/* Keep the inode on dirty list */
spin_unlock(&inode->i_lock);
return;
}
spin_unlock(&inode->i_lock);
tux3_evict_inode(inode);
remove_inode_hash(inode);
destroy_inode(inode);
}
}
/* For unit test */
int __tuxtruncate(struct inode *inode, loff_t size)
{
return tux3_truncate(inode, size);
}
int tuxtruncate(struct inode *inode, loff_t size)
{
struct dentry dentry = {
.d_inode = inode,
};
struct iattr iattrs = {
.ia_valid = ATTR_SIZE,
.ia_size = size,
};
if (size < 0)
return -EINVAL;
return tux3_setattr(&dentry, &iattrs);
}
/* Easy way to make a dummy inode. */
struct inode *rapid_new_inode(struct sb *sb, blockio_t *io, umode_t mode)
{
struct tux_iattr iattr = {
.mode = mode,
};
struct inode *inode;
inode = tux_new_inode(sb, NULL, &iattr);
assert(inode);
inode->map->io = io;
return inode;
}
void rapid_free_inode(struct inode *inode)
{
destroy_inode_nocheck(inode);
}