-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.c
562 lines (474 loc) · 13.3 KB
/
super.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* Chunkfs
*
* Chunks is a file system designed to be checked and repaired in
* small, mostly independent chunks. This allows quick recovery from
* file system corruption.
*
* (C) 2007-2008 Valerie Henson <[email protected]>
*
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/time.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/parser.h>
#include <linux/vfs.h>
#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/dcache.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>
#include "chunkfs.h"
#include "chunkfs_pool.h"
#include "chunkfs_dev.h"
#include "chunkfs_chunk.h"
#include "chunkfs_i.h"
static struct kmem_cache *chunkfs_inode_cachep;
static DEFINE_MUTEX(chunkfs_kernel_mutex);
static struct inode *chunkfs_alloc_inode(struct super_block *sb)
{
struct chunkfs_inode_info *ii;
struct inode *inode;
ii = kmem_cache_alloc(chunkfs_inode_cachep, GFP_KERNEL);
if (!ii)
return NULL;
/* XXX should be done in cache constructor */
mutex_init(&ii->ii_continuations_lock);
/* Don't load head continuation until file open */
inode = &ii->ii_vnode;
inode_init_once(inode);
inode->i_version = 1;
return inode;
}
static void chunkfs_destroy_inode(struct inode *inode)
{
struct chunkfs_inode_info *ii = CHUNKFS_I(inode);
chunkfs_debug("ino %0lx i_count %d\n",
inode->i_ino, atomic_read(&inode->i_count));
kmem_cache_free(chunkfs_inode_cachep, ii);
}
static void chunkfs_clear_inode(struct inode *inode)
{
struct chunkfs_inode_info *ii = CHUNKFS_I(inode);
chunkfs_debug("ino %0lx i_count %d\n",
inode->i_ino, atomic_read(&inode->i_count));
iput(ii->ii_client_inode);
clear_inode(inode);
}
static int
chunkfs_read_client_sb(struct chunkfs_chunk_info *ci)
{
/* XXX XXX XXX There aren't enough XXX's in the world XXX XXX */
const char *path_prefix = "/chunk";
char mount_path[sizeof(path_prefix) + 10];
struct nameidata nd;
int retval;
/*
* Userland has kindly mounted our client fs's in particular
* locations. Look up the path and grab the superblock for
* each chunk.
*
* XXX Yuckity yuckity yuck yuck
*/
sprintf(mount_path, "%s%llu", path_prefix, ci->ci_chunk_id);
retval = kern_path(mount_path, LOOKUP_FOLLOW, &nd.path);
if (retval) {
chunkfs_debug("path_lookup for %s failed: %d\n", mount_path, retval);
return retval;
}
/* XXX locking XXX prevent unmount XXX ref count XXX XXX */
ci->ci_mnt = mntget(nd.path.mnt);
ci->ci_sb = nd.path.mnt->mnt_sb;
path_put(&nd.path);
return 0;
}
struct chunkfs_chunk_info *
chunkfs_find_chunk(struct chunkfs_pool_info *pi, u64 chunk_id)
{
struct chunkfs_dev_info *di;
struct chunkfs_chunk_info *ci;
list_for_each_entry(di, &pi->pi_dlist_head, di_dlist) {
list_for_each_entry(ci, &di->di_clist_head, ci_clist) {
if(ci->ci_chunk_id == chunk_id)
return ci;
}
}
return NULL;
}
static void chunkfs_free_chunk(struct chunkfs_chunk_info *ci)
{
brelse(ci->ci_bh);
mntput(ci->ci_mnt);
kfree(ci);
}
static void chunkfs_free_dev(struct chunkfs_dev_info *di)
{
struct chunkfs_chunk_info *ci, *ci_next;
list_for_each_entry_safe(ci, ci_next, &di->di_clist_head, ci_clist) {
list_del(&ci->ci_clist);
chunkfs_free_chunk(ci);
}
brelse(di->di_bh);
kfree(di);
}
static void chunkfs_free_pool(struct chunkfs_pool_info *pi)
{
struct chunkfs_dev_info *di, *di_next;
list_for_each_entry_safe(di, di_next, &pi->pi_dlist_head, di_dlist) {
list_del(&di->di_dlist);
chunkfs_free_dev(di);
}
brelse(pi->pi_bh);
kfree(pi);
}
static int chunkfs_read_chunk(struct super_block *sb,
struct chunkfs_dev_info *dev,
struct chunkfs_chunk_info **chunk_info,
ci_byte_t chunk_offset,
ci_byte_t *next_chunk_offset)
{
struct chunkfs_chunk_info *ci;
struct chunkfs_chunk *chunk;
struct buffer_head *bh;
int retval = -EIO;
int err;
ci = kzalloc(sizeof(*ci), GFP_KERNEL);
if (!ci)
return -ENOMEM;
/* XXX assumes offset is multiple of underlying block size */
if (!(bh = sb_bread(sb, chunk_offset/CHUNKFS_BLK_SIZE))) {
printk(KERN_ERR "chunkfs: unable to read chunk summary at %llu",
chunk_offset);
goto out_nobh;
}
ci->ci_bh = bh;
chunk = CHUNKFS_CHUNK(ci);
if ((err = check_chunk(chunk)) != 0) {
printk(KERN_ERR "chunkfs: invalid chunk summary, err %d, chksum %0x\n",
err, le32_to_cpu(chunk->c_chksum));
goto out;
}
/* Fill in on-disk info */
ci->ci_flags = cpu_to_le64(chunk->c_flags);
*next_chunk_offset = cpu_to_le64(chunk->c_next_chunk);
ci->ci_chunk_id = cpu_to_le64(chunk->c_chunk_id);
memcpy(ci->ci_client_fs, chunk->c_client_fs, CHUNKFS_CLIENT_NAME_LEN);
/* Init non-disk stuff */
ci->ci_dev = dev;
/* Mount the client file system */
retval = chunkfs_read_client_sb(ci);
if (retval)
goto out;
*chunk_info = ci;
return 0;
out:
brelse(bh);
ci->ci_bh = NULL;
out_nobh:
kfree(ci);
BUG_ON(retval == 0);
return retval;
}
static int chunkfs_read_dev(struct super_block *sb,
struct chunkfs_pool_info *pool_info,
struct chunkfs_dev_info **dev_info)
{
struct chunkfs_dev_info *di;
struct chunkfs_dev *dev;
struct buffer_head * bh;
struct chunkfs_chunk_info *ci, *ci_next;
ci_byte_t chunk_offset, next_chunk_offset;
int retval = -EIO;
int err;
di = kzalloc(sizeof(*di), GFP_KERNEL);
if (!di)
return -ENOMEM;
/* XXX assumes sb offset is multiple of underlying block size */
if (!(bh = sb_bread(sb, CHUNKFS_DEV_BLK))) {
printk(KERN_ERR "chunkfs: unable to read dev summary\n");
goto out_nobh;
}
di->di_bh = bh;
dev = CHUNKFS_DEV(di);
if ((err = check_dev(dev)) != 0) {
printk(KERN_ERR "chunkfs: nnvalid dev summary err %d chksum %0x\n",
err, le32_to_cpu(dev->d_chksum));
goto out_bh;
}
/* Fill in on-disk info */
di->di_flags = cpu_to_le64(dev->d_flags);
chunk_offset = cpu_to_le64(dev->d_innards_begin);
/* Init non-disk stuff */
INIT_LIST_HEAD(&di->di_clist_head);
di->di_pool = pool_info;
/* XXX would like to sanity check dev size here */
while (chunk_offset != 0) {
retval = chunkfs_read_chunk(sb, di, &ci, chunk_offset,
&next_chunk_offset);
if (retval)
goto out_free_chunks;
list_add_tail(&ci->ci_clist, &di->di_clist_head);
if (CHUNKFS_IS_ROOT(ci)) {
BUG_ON(di->di_pool->pi_root_dev);
di->di_pool->pi_root_dev = di;
BUG_ON(di->di_root_chunk);
di->di_root_chunk = ci;
}
chunk_offset = next_chunk_offset;
}
/* Did we find root? */
if (!di->di_root_chunk) {
printk(KERN_ERR "chunkfs: did not find root\n");
goto out_free_chunks;
}
*dev_info = di;
return 0;
out_free_chunks:
list_for_each_entry_safe(ci, ci_next, &di->di_clist_head, ci_clist) {
list_del(&ci->ci_clist);
chunkfs_free_chunk(ci);
}
out_bh:
brelse(bh);
di->di_bh = NULL;
out_nobh:
kfree(di);
return retval;
}
static int chunkfs_read_pool(struct super_block *sb,
struct chunkfs_pool_info **pool_info)
{
struct chunkfs_pool_info *pi;
struct chunkfs_pool *pool;
struct buffer_head * bh;
struct chunkfs_dev_info *di;
int retval = -EIO;
int err;
pi = kzalloc(sizeof(*pi), GFP_KERNEL);
if (!pi)
return -ENOMEM;
/* XXX assumes sb offset is multiple of underlying block size */
if (!(bh = sb_bread(sb, CHUNKFS_POOL_BLK))) {
printk(KERN_ERR "chunkfs: unable to read pool summary\n");
goto out_nobh;
}
pi->pi_bh = bh;
pool = CHUNKFS_POOL(pi);
if ((err = check_pool(pool)) != 0) {
printk(KERN_ERR "chunkfs: invalid pool summary, err %d chksum %0x magic %0x\n",
err, le32_to_cpu(pool->p_chksum), le32_to_cpu(pool->p_magic));
goto out;
}
/* Fill in on-disk info */
pi->pi_flags = cpu_to_le64(pool->p_flags);
/* Init non-disk stuff */
INIT_LIST_HEAD(&pi->pi_dlist_head);
/* XXX read multiple devs */
/* For now, we just read at a particular offset on this dev */
retval = chunkfs_read_dev(sb, pi, &di);
if (retval)
goto out;
list_add_tail(&di->di_dlist, &pi->pi_dlist_head);
*pool_info = pi;
return 0;
out:
brelse(bh);
pi->pi_bh = NULL;
out_nobh:
kfree(pi);
return retval;
}
static void chunkfs_commit_super (struct super_block *sb, int sync)
{
struct buffer_head *sbh = CHUNKFS_PI(sb)->pi_bh;
if (!sbh)
return;
mark_buffer_dirty(sbh);
if (sync)
sync_dirty_buffer(sbh);
}
static void chunkfs_put_super (struct super_block *sb)
{
struct chunkfs_pool_info *pi = CHUNKFS_PI(sb);
if (!(sb->s_flags & MS_RDONLY)) {
/* XXX should mark super block as clean unmounted */
chunkfs_commit_super(sb, 1);
}
chunkfs_free_pool(pi);
sb->s_fs_info = NULL;
return;
}
static int
chunkfs_write_super (struct super_block *sb, int wait)
{
/* TODO: another mutex in private part of sb. */
mutex_lock(&chunkfs_kernel_mutex);
chunkfs_commit_super(sb, 1);
/* TODO: it is clear now */
mutex_unlock(&chunkfs_kernel_mutex);
return 0;
}
static struct super_operations chunkfs_sops = {
.alloc_inode = chunkfs_alloc_inode,
.destroy_inode = chunkfs_destroy_inode,
.write_inode = chunkfs_write_inode,
#if 0 /* XXX Totally unimplemented at present */
.dirty_inode = chunkfs_dirty_inode,
.delete_inode = chunkfs_delete_inode,
#endif
.put_super = chunkfs_put_super,
/* TODO: sync the whole fs, not just sb. */
.sync_fs = chunkfs_write_super,
#if 0
.sync_fs = chunkfs_sync_fs,
.write_super_lockfs = chunkfs_write_super_lockfs,
.unlockfs = chunkfs_unlockfs,
.statfs = chunkfs_statfs,
.remount_fs = chunkfs_remount,
#endif
.evict_inode = chunkfs_clear_inode,
#if 0
.show_options = chunkfs_show_options,
#endif
};
/*
* The file system in the root chunk has already been mounted, so the
* chunk root inode is already loaded and stored in the superblock.
* However, we really want to have the root directory in terms of the
* chunkfs namespace, which is presently named "/root" and inode 12.
*/
static int chunkfs_read_root(struct super_block *sb)
{
struct chunkfs_chunk_info *ci = CHUNKFS_PI(sb)->pi_root_dev->di_root_chunk;
ino_t ino = MAKE_UINO(ci->ci_chunk_id, 12); /* XXX */
struct inode *inode;
struct nameidata nd;
struct dentry *dentry;
int retval;
inode = new_inode(sb);
inode->i_ino = ino;
inode->i_sb = sb;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
inode_init_owner(inode, NULL, S_IFDIR);
retval = kern_path("/chunk1/root/", LOOKUP_FOLLOW, &nd.path);
if (retval)
goto out_dentry;
dentry = dget(nd.path.dentry);
/* Finish inode init */
chunkfs_init_cont_data(dentry);
chunkfs_start_inode(inode, dentry->d_inode, ci->ci_chunk_id);
/* Restore it, after chunkfs_start_inode() */
inode->i_ino = ino;
sb->s_root = d_make_root(inode);
if (!sb->s_root) {
retval = -ENOMEM;
goto out_iput;
}
retval = chunkfs_init_dentry(sb->s_root);
if (retval)
goto out_dput;
chunkfs_init_nd(inode, sb->s_root, dentry, ci->ci_chunk_id);
chunkfs_add_dentry(sb->s_root, dentry, nd.path.mnt);
path_put(&nd.path);
return 0;
out_dentry:
chunkfs_free_dentry(sb->s_root);
out_dput:
dput(sb->s_root);
out_iput:
iput(inode);
printk(KERN_ERR "chunkfs: allocation of root inode failed\n");
return retval;
}
/*
* chunkfs_setup_super does all things that are shared between mount
* and remount. At moment, I'm not sure what they are.
*/
static int chunkfs_setup_super(struct super_block *sb,
struct chunkfs_pool_info *pi,
int read_only)
{
return 0;
}
/*
* Get the superblock off the disk and check to see if it is sane.
*
* Note that VFS code has a generic routine to find alternate superblocks.
*
* XXX todo, put dev summary copies in chunk summaries.
*/
static int chunkfs_fill_super(struct super_block *sb, void *data, int silent)
{
struct chunkfs_pool_info *pi;
int retval = -EINVAL;
mutex_unlock(&chunkfs_kernel_mutex);
chunkfs_debug("enter\n");
/* We must set blocksize before we can read blocks. */
if (sb_set_blocksize(sb, CHUNKFS_BLK_SIZE) == 0)
goto out;
retval = chunkfs_read_pool(sb, &pi);
if (retval)
goto out;
sb->s_fs_info = pi;
sb->s_op = &chunkfs_sops;
retval = chunkfs_read_root(sb);
if (retval)
goto out;
/* If fail after this, dput sb->s_root */
chunkfs_setup_super (sb, pi, sb->s_flags & MS_RDONLY);
printk(KERN_ERR "chunkfs: mounted file system\n");
mutex_lock(&chunkfs_kernel_mutex);
return 0;
out:
mutex_lock(&chunkfs_kernel_mutex);
BUG_ON(retval == 0);
printk(KERN_ERR "chunkfs: mount failed (%d)\n", retval);
return retval;
}
static struct dentry *chunkfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, chunkfs_fill_super);
}
static void chunkfs_kill_super(struct super_block *sb)
{
struct block_device *bdev = sb->s_bdev;
fmode_t mode = sb->s_mode;
bdev->bd_super = NULL;
generic_shutdown_super(sb);
sync_blockdev(bdev);
WARN_ON_ONCE(!(mode & FMODE_EXCL));
blkdev_put(bdev, mode | FMODE_EXCL);
}
static struct file_system_type chunkfs_fs_type = {
.owner = THIS_MODULE,
.name = "chunkfs",
.mount = chunkfs_mount,
.kill_sb = chunkfs_kill_super,
.fs_flags = FS_REQUIRES_DEV,
};
static int __init init_chunkfs_fs(void)
{
int err = register_filesystem(&chunkfs_fs_type);
if (!err)
printk(KERN_INFO "chunkfs (C) 2007 Valerie Henson <[email protected]>\n");
chunkfs_inode_cachep = kmem_cache_create("chunkfs_inode_cachep",
sizeof(struct chunkfs_inode_info),
0, (SLAB_RECLAIM_ACCOUNT| SLAB_MEM_SPREAD), NULL);
if (!chunkfs_inode_cachep)
err = -ENOMEM;
return err;
}
static void __exit exit_chunkfs_fs(void)
{
unregister_filesystem(&chunkfs_fs_type);
kmem_cache_destroy(chunkfs_inode_cachep);
}
MODULE_AUTHOR("Val Henson");
MODULE_DESCRIPTION("Chunkfs");
MODULE_LICENSE("GPL");
module_init(init_chunkfs_fs)
module_exit(exit_chunkfs_fs)