-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminfs.c
766 lines (614 loc) · 17.6 KB
/
minfs.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#include <linux/bitops.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uio.h>
#include <linux/sched.h>
#include <linux/statfs.h>
#include <linux/pagemap.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/timekeeping.h>
#include <linux/cred.h>
#include <linux/uidgid.h>
#include "minfs.h"
MODULE_DESCRIPTION("Simple filesystem");
MODULE_AUTHOR("yuezato");
MODULE_LICENSE("GPL");
#define LOG_LEVEL KERN_ALERT
#define DEBUG 0
#if DEBUG == 1
#define dprintk(fmt, ...) \
printk(LOG_LEVEL fmt, ##__VA_ARGS__)
#else
#define dprintk(fmt, ...) \
do {} while (0)
#endif
struct minfs_sb_info {
__u8 version;
unsigned long imap;
struct buffer_head *sbh;
};
struct minfs_inode_info {
__u16 data_block;
struct inode vfs_inode;
};
kuid_t uint_to_kuid(unsigned int v) {
kuid_t k = { v };
return k;
}
unsigned int kuid_to_uint(kuid_t k) {
return k.val;
}
kgid_t uint_to_kgid(unsigned int v) {
kgid_t k = { v };
return k;
}
unsigned int kgid_to_uint(kgid_t k) {
return k.val;
}
struct timespec64 get_current_timespec64(void) {
struct timespec64 t;
ktime_get_coarse_real_ts64(&t);
return t;
}
/*
* forward declarations
*/
static const struct address_space_operations minfs_aops;
static const struct file_operations minfs_file_operations;
static struct inode_operations minfs_file_inode_operations;
static const struct file_operations minfs_dir_operations;
static struct inode_operations minfs_dir_inode_operations;
static struct inode *minfs_iget(struct super_block *s, unsigned long ino)
{
struct minfs_inode *mi;
struct buffer_head *bh;
struct inode *inode;
struct minfs_inode_info *mii;
/* allocate VFS inode */
inode = iget_locked(s, ino);
if (inode == NULL) {
printk(LOG_LEVEL "error aquiring inode\n");
return ERR_PTR(-ENOMEM);
}
if (!(inode->i_state & I_NEW))
return inode;
/* read disk inode block */
bh = sb_bread(s, MINFS_INODE_BLOCK);
if (bh == NULL) {
printk(LOG_LEVEL "could not read block\n");
goto out_bad_sb;
}
/* extract disk inode */
mi = ((struct minfs_inode *) bh->b_data) + ino;
/* fill VFS inode */
inode->i_mode = mi->mode;
// uid :: kuid_t where kuid_t = struct { uid_t val };
// uid_t = __kernel_uid32_t
// __kernel_uid32_t = unsigned int
inode->i_uid = uint_to_kuid(mi->uid);
inode->i_gid = uint_to_kgid(mi->gid);
inode->i_size = mi->size;
inode->i_blocks = 0;
inode->i_mtime = inode->i_atime = inode->i_ctime = get_current_timespec64(); // CURRENT_TIME;
inode->i_mapping->a_ops = &minfs_aops;
if (S_ISDIR(inode->i_mode)) {
inode->i_op = &minfs_dir_inode_operations;
inode->i_fop = &minfs_dir_operations;
/* directory inodes start off with i_nlink == 2 */
inc_nlink(inode);
}
if (S_ISREG(inode->i_mode)) {
inode->i_op = &minfs_file_inode_operations;
inode->i_fop = &minfs_file_operations;
}
/* fill data for mii */
mii = container_of(inode, struct minfs_inode_info, vfs_inode);
mii->data_block = mi->data_block;
/* free resources */
brelse(bh);
unlock_new_inode(inode);
dprintk("got inode %lu\n", ino);
return inode;
out_bad_sb:
iget_failed(inode);
return NULL;
}
static int minfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) {
struct super_block *sb = inode->i_sb;
// max 5block / per
sector_t phys = 10 + (inode->i_ino * 5) + iblock;
map_bh(bh_result, sb, phys);
dprintk("[start] I am minfs_get_block");
// asm volatile ("int $3");
return 0;
}
static int minfs_readpage(struct file *file, struct page *page) {
return block_read_full_page(page, minfs_get_block);
}
static int minfs_writepage(struct page *page, struct writeback_control *wbc) {
return block_write_full_page(page, minfs_get_block, wbc);
}
static ssize_t minfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) {
dprintk("I am minfs_direct_IO");
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
struct inode *inode = mapping->host;
size_t count = iov_iter_count(iter);
loff_t offset = iocb->ki_pos;
ssize_t ret = blockdev_direct_IO(iocb, inode, iter, minfs_get_block);
/*
ext2, ext4 や reiserfsを真似るべき
*/
if(ret < 0 && iov_iter_rw(iter) == WRITE) {
dprintk("[fail] minfs_direct_IO");
}
return ret;
}
static const struct address_space_operations minfs_aops =
{
// .readpage = simple_readpage, // diskに書き込めてることがstringsでわかっても、simple_readpageだと読み込み失敗するぽいので
.readpage = minfs_readpage,
.writepage = minfs_writepage,
.write_begin = simple_write_begin,
.write_end = simple_write_end,
.direct_IO = minfs_direct_IO,
};
static ssize_t
minfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) {
ssize_t r;
dprintk("[start] I am minfs_file_read_iter");
dprintk("filp = %p", iocb->ki_filp);
dprintk("mapping = %p", iocb->ki_filp->f_mapping);
dprintk("inode = %p", iocb->ki_filp->f_mapping->host);
dprintk("inode.i_ino = %lu", ((struct inode *)iocb->ki_filp->f_mapping->host)->i_ino);
r = generic_file_read_iter(iocb, iter);
dprintk("[end] I am minfs_file_read_iter");
return r;
}
static ssize_t
minfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) {
ssize_t r;
// asm volatile ("int $3");
dprintk("[start] I am minfs_file_write_iter");
r = generic_file_write_iter(iocb, from);
dprintk("[end] I am minfs_file_write_iter");
return r;
}
static int
minfs_file_fsync(struct file *file, loff_t start, loff_t end,
int datasync) {
dprintk("I am minfs_file_fsync");
return generic_file_fsync(file, start, end, datasync);
}
static loff_t
minfs_file_llseek(struct file *file, loff_t offset, int whence) {
dprintk("I am minfs_file_llseek");
return generic_file_llseek(file, offset, whence);
}
static const struct file_operations minfs_file_operations =
{
.llseek = minfs_file_llseek,
.read_iter = minfs_file_read_iter,
.write_iter = minfs_file_write_iter,
.fsync = minfs_file_fsync,
};
static struct inode_operations minfs_file_inode_operations =
{
.getattr = simple_getattr,
};
static int minfs_readdir(struct file *filp, struct dir_context *ctx)
{
struct buffer_head *bh;
struct minfs_dir_entry *de;
struct inode *inode = file_inode(filp);
struct minfs_inode_info *mii = container_of(inode,
struct minfs_inode_info, vfs_inode);
struct super_block *sb = inode->i_sb;
int err = 0;
int over;
/* read data block for directory inode */
bh = sb_bread(sb, mii->data_block);
if (bh == NULL) {
printk(LOG_LEVEL "could not read block\n");
err = -ENOMEM;
goto out_bad_sb;
}
dprintk("Read data block %d for folder %s\n", mii->data_block,
filp->f_path.dentry->d_name.name);
for (; ctx->pos < MINFS_NUM_ENTRIES; ctx->pos++) {
de = (struct minfs_dir_entry *) bh->b_data + ctx->pos;
if (de->ino != 0) {
over = dir_emit(ctx, de->name, MINFS_NAME_LEN,
de->ino, DT_UNKNOWN);
if (over) {
dprintk("Read %s from folder %s, ctx->pos: %lld\n",
de->name,
filp->f_path.dentry->d_name.name,
ctx->pos);
ctx->pos += 1;
goto done;
}
}
}
done:
brelse(bh);
out_bad_sb:
return err;
}
static const struct file_operations minfs_dir_operations = {
.read = generic_read_dir,
.iterate = minfs_readdir,
};
/*
* find dentry in parent folder; return parent folder's data buffer_head
*/
static struct minfs_dir_entry *minfs_find_entry(struct dentry *dentry,
struct buffer_head **bhp)
{
struct buffer_head *bh;
struct inode *dir = dentry->d_parent->d_inode;
struct minfs_inode_info *mii = container_of(dir, struct minfs_inode_info, vfs_inode);
struct super_block *sb = dir->i_sb;
const char *name = dentry->d_name.name;
struct minfs_dir_entry *final_de = NULL;
struct minfs_dir_entry *de;
size_t i;
/* read parent folder data block (contains dentries) */
bh = sb_bread(sb, mii->data_block);
if (bh == NULL) {
printk(LOG_LEVEL "could not read block\n");
return NULL;
}
*bhp = bh;
dprintk("Looking for dentry name %s in parent folder %s\n",
name, dentry->d_parent->d_name.name);
/* traverse all entries */
for (i = 0; i < MINFS_NUM_ENTRIES; i++) {
de = ((struct minfs_dir_entry *) bh->b_data) + i;
if (de->ino != 0) {
/* found it */
if (strcmp(name, de->name) == 0) {
dprintk("Found entry %s on position: %zd\n",
name, i);
final_de = de;
break;
}
}
}
/* bh needs to be released by caller */
return final_de;
}
static struct dentry *minfs_lookup(struct inode *dir,
struct dentry *dentry, unsigned int flags)
{
struct super_block *sb = dir->i_sb;
struct minfs_dir_entry *de;
struct buffer_head *bh = NULL;
struct inode *inode = NULL;
dentry->d_op = sb->s_root->d_op;
de = minfs_find_entry(dentry, &bh);
if (de != NULL) {
dprintk("getting entry: name: %s, ino: %d\n",
de->name, de->ino);
inode = minfs_iget(sb, de->ino);
if (IS_ERR(inode))
return ERR_CAST(inode);
}
d_add(dentry, inode);
brelse(bh);
dprintk("looked up dentry %s\n", dentry->d_name.name);
return NULL;
}
/*
* create a new VFS inode, do basic initialization and fill imap
*/
static struct inode *minfs_new_inode(struct inode *dir)
{
struct super_block *sb = dir->i_sb;
struct minfs_sb_info *sbi = sb->s_fs_info;
struct inode *inode;
int idx;
idx = find_first_zero_bit(&sbi->imap, MINFS_NUM_INODES);
if (idx == MINFS_NUM_INODES) {
printk(LOG_LEVEL "no space left in imap\n");
return NULL;
}
__test_and_set_bit(idx, &sbi->imap);
mark_buffer_dirty(sbi->sbh);
inode = new_inode(sb);
inode->i_uid = current_fsuid();
inode->i_gid = current_fsgid();
inode->i_ino = idx;
inode->i_mtime = inode->i_atime = inode->i_ctime = get_current_timespec64();
inode->i_blocks = 0;
insert_inode_hash(inode);
return inode;
}
/*
* add dentry link on parent inode disk structure
*/
static int minfs_add_link(struct dentry *dentry, struct inode *inode)
{
struct buffer_head *bh;
struct inode *dir = dentry->d_parent->d_inode;
struct minfs_inode_info *mii = container_of(dir,
struct minfs_inode_info, vfs_inode);
struct super_block *sb = dir->i_sb;
struct minfs_dir_entry *de;
int i;
int err = 0;
bh = sb_bread(sb, mii->data_block);
for (i = 0; i < MINFS_NUM_ENTRIES; i++) {
de = (struct minfs_dir_entry *) bh->b_data + i;
if (de->ino == 0)
break;
}
if (i == MINFS_NUM_ENTRIES) {
err = -ENOSPC;
goto out;
}
de->ino = inode->i_ino;
memcpy(de->name, dentry->d_name.name, MINFS_NAME_LEN);
dir->i_mtime = dir->i_ctime = get_current_timespec64();
mark_buffer_dirty(bh);
out:
brelse(bh);
return err;
}
/*
* create a VFS file inode; use simple operations
*/
static int minfs_create(struct inode *dir, struct dentry *dentry,
umode_t mode, bool excl)
{
struct inode *inode;
struct minfs_inode_info *mii;
int err;
inode = minfs_new_inode(dir);
if (inode == NULL) {
printk(LOG_LEVEL "error allocating new inode\n");
err = -ENOSPC;
goto err_new_inode;
}
inode->i_mode = mode;
inode->i_op = &minfs_file_inode_operations;
inode->i_fop = &minfs_file_operations;
inode->i_mapping->a_ops = &minfs_aops;
mii = container_of(inode, struct minfs_inode_info, vfs_inode);
mii->data_block = MINFS_FIRST_DATA_BLOCK + inode->i_ino;
err = minfs_add_link(dentry, inode);
if (err != 0)
goto err_add_link;
d_instantiate(dentry, inode);
mark_inode_dirty(inode);
dprintk("new file inode created (ino = %lu)\n", inode->i_ino);
return 0;
err_add_link:
inode_dec_link_count(inode);
iput(inode);
err_new_inode:
return err;
}
// dentryに絡むフィールドをクリア
static int minfs_unlink(struct inode *dir, struct dentry *dentry) {
int err = -ENOENT;
struct inode *inode = d_inode(dentry);
struct buffer_head *bh;
struct minfs_dir_entry *de;
// こっちはsuperblockではなく
// ディレクトリ構造の方の永続化
de = minfs_find_entry(dentry, &bh);
if(!de)
goto end_unlink;
de->ino = 0;
mark_buffer_dirty(bh);
brelse(bh);
inode->i_ctime = inode->i_mtime = current_time(inode);
mark_inode_dirty(inode);
inode->i_ctime = dir->i_ctime;
inode_dec_link_count(inode);
return 0;
end_unlink:
dprintk("error @ minfs_unlink");
return err;
}
static struct inode_operations minfs_dir_inode_operations = {
.lookup = minfs_lookup,
.create = minfs_create,
.unlink = minfs_unlink,
};
/*
* allocate and initialize VFS inode; filling is done in new_inode or
* minfs_iget
*/
static struct inode *minfs_alloc_inode(struct super_block *s)
{
struct minfs_inode_info *mii;
/* allocate minfs_inode_info */
mii = kzalloc(sizeof(struct minfs_inode_info), GFP_KERNEL);
if (mii == NULL)
return NULL;
inode_init_once(&mii->vfs_inode);
return &mii->vfs_inode;
}
static void minfs_destroy_inode(struct inode *inode)
{
kfree(container_of(inode, struct minfs_inode_info, vfs_inode));
}
/*
* write VFS inode contents to disk inode
*/
static int minfs_write_inode(struct inode *inode, struct
writeback_control *wbc)
{
struct super_block *sb = inode->i_sb;
struct minfs_inode *mi;
struct minfs_inode_info *mii = container_of(inode,
struct minfs_inode_info, vfs_inode);
struct buffer_head *bh;
int err = 0;
bh = sb_bread(sb, MINFS_INODE_BLOCK);
if (bh == NULL) {
printk(LOG_LEVEL "could not read block\n");
err = -ENOMEM;
goto out;
}
mi = (struct minfs_inode *) bh->b_data + inode->i_ino;
/* fill disk inode */
mi->mode = inode->i_mode;
mi->uid = kuid_to_uint(inode->i_uid);
mi->gid = kgid_to_uint(inode->i_gid);
mi->size = inode->i_size;
mi->data_block = mii->data_block;
dprintk("mode is %05o; data_block is %d\n", mi->mode, mii->data_block);
mark_buffer_dirty(bh);
brelse(bh);
dprintk("wrote inode %lu (size %llu)\n", inode->i_ino, inode->i_size);
out:
return err;
}
static void minfs_put_super(struct super_block *sb)
{
struct minfs_sb_info *sbi = sb->s_fs_info;
/* free superblock buffer head */
mark_buffer_dirty(sbi->sbh);
brelse(sbi->sbh);
dprintk("released superblock resources\n");
}
/*
minixのminix_evict_inodeを参考にしている
*/
static void minfs_evict_inode(struct inode* inode) {
truncate_inode_pages_final(&inode->i_data);
if(inode->i_nlink == 0) {
inode->i_size = 0;
//!
//ここでminixだとminix_truncate(inode)をしている
}
invalidate_inode_buffers(inode);
clear_inode(inode);
if(inode->i_nlink == 0) {
struct super_block *sb = inode->i_sb;
/*
struct buffer_head *bh = sb_bread(sb, MINFS_SUPER_BLOCK);
struct minfs_super_block *ms = (struct minfs_super_block*)bh->b_data;
*/
struct minfs_sb_info *sbi = sb->s_fs_info;
clear_bit(inode->i_ino, &sbi->imap);
mark_buffer_dirty(sbi->sbh);
// brelse(sbi->sbh);
}
}
static int minfs_statfs(struct dentry *dentry, struct kstatfs *buf) {
struct super_block *sb = dentry->d_sb;
struct minfs_sb_info *sbi = sb->s_fs_info;
buf->f_type = sb->s_magic;
buf->f_bsize = sb->s_blocksize;
buf->f_namelen = MINFS_NAME_LEN;
// buf->f_blockas =
// buf->f_bfree =
// buf->f_bavail = buf->f_bfree;
// f_files includes root inode
buf->f_files = MINFS_NUM_INODES;
buf->f_ffree = buf->f_files - hweight_long(sbi->imap);
return 0;
}
static const struct super_operations minfs_ops = {
.statfs = minfs_statfs,
.alloc_inode = minfs_alloc_inode,
.destroy_inode = minfs_destroy_inode,
.evict_inode = minfs_evict_inode,
.write_inode = minfs_write_inode,
.put_super = minfs_put_super,
};
static int minfs_fill_super(struct super_block *s, void *data, int silent)
{
struct minfs_sb_info *sbi;
struct minfs_super_block *ms;
struct inode *root_inode;
struct dentry *root_dentry;
struct buffer_head *bh;
int ret = -EINVAL;
sbi = kzalloc(sizeof(struct minfs_sb_info), GFP_KERNEL);
if (!sbi)
return -ENOMEM;
s->s_fs_info = sbi;
/* set block size for superblock */
if (!sb_set_blocksize(s, MINFS_BLOCK_SIZE))
goto out_bad_blocksize;
/* set maximum size for a file */
s->s_maxbytes = MAX_LFS_FILESIZE;
/* read first block from disk (contains disk superblock) */
bh = sb_bread(s, MINFS_SUPER_BLOCK);
if (bh == NULL)
goto out_bad_sb;
/* extract disk superblock */
ms = (struct minfs_super_block *) bh->b_data;
/* fill sbi with information from disk superblock */
if (ms->magic == MINFS_MAGIC) {
sbi->version = ms->version;
sbi->imap = ms->imap;
} else
goto out_bad_magic;
s->s_magic = MINFS_MAGIC;
s->s_op = &minfs_ops;
/* allocate root inode and root dentry */
root_inode = minfs_iget(s, MINFS_ROOT_INODE);
if (!root_inode)
goto out_bad_inode;
root_dentry = d_make_root(root_inode);
if (!root_dentry)
goto out_iput;
s->s_root = root_dentry;
/* store superblock buffer_head for further use */
sbi->sbh = bh;
dprintk("superblock filled\n");
return 0;
out_iput:
iput(root_inode);
out_bad_inode:
printk(LOG_LEVEL "bad inode\n");
out_bad_magic:
printk(LOG_LEVEL "bad magic number\n");
brelse(bh);
out_bad_sb:
printk(LOG_LEVEL "error reading buffer_head\n");
out_bad_blocksize:
printk(LOG_LEVEL "bad block size\n");
s->s_fs_info = NULL;
kfree(sbi);
return ret;
}
static struct dentry *minfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, minfs_fill_super);
}
static struct file_system_type minfs_fs_type = {
.owner = THIS_MODULE,
.name = "minfs",
.mount = minfs_mount,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
static int __init minfs_init(void)
{
int err;
err = register_filesystem(&minfs_fs_type);
if (err) {
printk(LOG_LEVEL "register_filesystem failed\n");
return err;
}
dprintk("registered filesystem\n");
return 0;
}
static void __exit minfs_exit(void)
{
unregister_filesystem(&minfs_fs_type);
dprintk("unregistered filesystem\n");
}
module_init(minfs_init);
module_exit(minfs_exit);