-
Notifications
You must be signed in to change notification settings - Fork 220
/
simple.c
991 lines (806 loc) · 26 KB
/
simple.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
* A Simple Filesystem for the Linux Kernel.
*
* Initial author: Sankar P <sankar.curiosity@gmail.com>
* License: Creative Commons Zero License - http://creativecommons.org/publicdomain/zero/1.0/
*
* TODO: we need to split it into smaller files
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/version.h>
#include <linux/jbd2.h>
#include <linux/parser.h>
#include <linux/blkdev.h>
#include "super.h"
#ifndef f_dentry
#define f_dentry f_path.dentry
#endif
/* A super block lock that must be used for any critical section operation on the sb,
* such as: updating the free_blocks, inodes_count etc. */
static DEFINE_MUTEX(simplefs_sb_lock);
static DEFINE_MUTEX(simplefs_inodes_mgmt_lock);
/* FIXME: This can be moved to an in-memory structure of the simplefs_inode.
* Because of the global nature of this lock, we cannot create
* new children (without locking) in two different dirs at a time.
* They will get sequentially created. If we move the lock
* to a directory-specific way (by moving it inside inode), the
* insertion of two children in two different directories can be
* done in parallel */
static DEFINE_MUTEX(simplefs_directory_children_update_lock);
static struct kmem_cache *sfs_inode_cachep;
void simplefs_sb_sync(struct super_block *vsb)
{
struct buffer_head *bh = NULL;
struct simplefs_super_block *sb = SIMPLEFS_SB(vsb);
bh = sb_bread(vsb, SIMPLEFS_SUPERBLOCK_BLOCK_NUMBER);
BUG_ON(!bh);
bh->b_data = (char *)sb;
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
}
struct simplefs_inode *simplefs_inode_search(struct super_block *sb,
struct simplefs_inode *start,
struct simplefs_inode *search)
{
uint64_t count = 0;
while (start->inode_no != search->inode_no
&& count < SIMPLEFS_SB(sb)->inodes_count) {
count++;
start++;
}
if (start->inode_no == search->inode_no) {
return start;
}
return NULL;
}
void simplefs_inode_add(struct super_block *vsb, struct simplefs_inode *inode)
{
struct simplefs_super_block *sb = SIMPLEFS_SB(vsb);
struct buffer_head *bh = NULL;
struct simplefs_inode *inode_iterator = NULL;
if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return;
}
bh = sb_bread(vsb, SIMPLEFS_INODESTORE_BLOCK_NUMBER);
BUG_ON(!bh);
inode_iterator = (struct simplefs_inode *)bh->b_data;
if (mutex_lock_interruptible(&simplefs_sb_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return;
}
/* Append the new inode in the end in the inode store */
inode_iterator += sb->inodes_count;
memcpy(inode_iterator, inode, sizeof(struct simplefs_inode));
sb->inodes_count++;
mark_buffer_dirty(bh);
simplefs_sb_sync(vsb);
brelse(bh);
mutex_unlock(&simplefs_sb_lock);
mutex_unlock(&simplefs_inodes_mgmt_lock);
}
/* This function returns a blocknumber which is free.
* The block will be removed from the freeblock list.
*
* In an ideal, production-ready filesystem, we will not be dealing with blocks,
* and instead we will be using extents
*
* If for some reason, the file creation/deletion failed, the block number
* will still be marked as non-free. You need fsck to fix this.*/
int simplefs_sb_get_a_freeblock(struct super_block *vsb, uint64_t * out)
{
struct simplefs_super_block *sb = SIMPLEFS_SB(vsb);
int i;
int ret = 0;
if (mutex_lock_interruptible(&simplefs_sb_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
ret = -EINTR;
goto end;
}
/* Loop until we find a free block. We start the loop from 3,
* as all prior blocks will always be in use */
for (i = 3; i < SIMPLEFS_MAX_FILESYSTEM_OBJECTS_SUPPORTED; i++) {
if (sb->free_blocks & (1 << i)) {
break;
}
}
if (unlikely(i == SIMPLEFS_MAX_FILESYSTEM_OBJECTS_SUPPORTED)) {
printk(KERN_ERR "No more free blocks available");
ret = -ENOSPC;
goto end;
}
*out = i;
/* Remove the identified block from the free list */
sb->free_blocks &= ~(1 << i);
simplefs_sb_sync(vsb);
end:
mutex_unlock(&simplefs_sb_lock);
return ret;
}
static int simplefs_sb_get_objects_count(struct super_block *vsb,
uint64_t * out)
{
struct simplefs_super_block *sb = SIMPLEFS_SB(vsb);
if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return -EINTR;
}
*out = sb->inodes_count;
mutex_unlock(&simplefs_inodes_mgmt_lock);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
static int simplefs_iterate(struct file *filp, struct dir_context *ctx)
#else
static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir)
#endif
{
loff_t pos;
struct inode *inode;
struct super_block *sb;
struct buffer_head *bh;
struct simplefs_inode *sfs_inode;
struct simplefs_dir_record *record;
int i;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
pos = ctx->pos;
#else
pos = filp->f_pos;
#endif
inode = filp->f_dentry->d_inode;
sb = inode->i_sb;
if (pos) {
/* FIXME: We use a hack of reading pos to figure if we have filled in all data.
* We should probably fix this to work in a cursor based model and
* use the tokens correctly to not fill too many data in each cursor based call */
return 0;
}
sfs_inode = SIMPLEFS_INODE(inode);
if (unlikely(!S_ISDIR(sfs_inode->mode))) {
printk(KERN_ERR
"inode [%llu][%lu] for fs object [%s] not a directory\n",
sfs_inode->inode_no, inode->i_ino,
filp->f_dentry->d_name.name);
return -ENOTDIR;
}
bh = sb_bread(sb, sfs_inode->data_block_number);
BUG_ON(!bh);
record = (struct simplefs_dir_record *)bh->b_data;
for (i = 0; i < sfs_inode->dir_children_count; i++) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
dir_emit(ctx, record->filename, SIMPLEFS_FILENAME_MAXLEN,
record->inode_no, DT_UNKNOWN);
ctx->pos += sizeof(struct simplefs_dir_record);
#else
filldir(dirent, record->filename, SIMPLEFS_FILENAME_MAXLEN, pos,
record->inode_no, DT_UNKNOWN);
filp->f_pos += sizeof(struct simplefs_dir_record);
#endif
pos += sizeof(struct simplefs_dir_record);
record++;
}
brelse(bh);
return 0;
}
/* This functions returns a simplefs_inode with the given inode_no
* from the inode store, if it exists. */
struct simplefs_inode *simplefs_get_inode(struct super_block *sb,
uint64_t inode_no)
{
struct simplefs_super_block *sfs_sb = SIMPLEFS_SB(sb);
struct simplefs_inode *sfs_inode = NULL;
struct simplefs_inode *inode_buffer = NULL;
int i;
struct buffer_head *bh;
/* The inode store can be read once and kept in memory permanently while mounting.
* But such a model will not be scalable in a filesystem with
* millions or billions of files (inodes) */
bh = sb_bread(sb, SIMPLEFS_INODESTORE_BLOCK_NUMBER);
BUG_ON(!bh);
sfs_inode = (struct simplefs_inode *)bh->b_data;
#if 0
if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
printk(KERN_ERR "Failed to acquire mutex lock %s +%d\n",
__FILE__, __LINE__);
return NULL;
}
#endif
for (i = 0; i < sfs_sb->inodes_count; i++) {
if (sfs_inode->inode_no == inode_no) {
inode_buffer = kmem_cache_alloc(sfs_inode_cachep, GFP_KERNEL);
memcpy(inode_buffer, sfs_inode, sizeof(*inode_buffer));
break;
}
sfs_inode++;
}
// mutex_unlock(&simplefs_inodes_mgmt_lock);
brelse(bh);
return inode_buffer;
}
ssize_t simplefs_read(struct file * filp, char __user * buf, size_t len,
loff_t * ppos)
{
/* After the commit dd37978c5 in the upstream linux kernel,
* we can use just filp->f_inode instead of the
* f->f_path.dentry->d_inode redirection */
struct simplefs_inode *inode =
SIMPLEFS_INODE(filp->f_path.dentry->d_inode);
struct buffer_head *bh;
char *buffer;
int nbytes;
if (*ppos >= inode->file_size) {
/* Read request with offset beyond the filesize */
return 0;
}
bh = sb_bread(filp->f_path.dentry->d_inode->i_sb,
inode->data_block_number);
if (!bh) {
printk(KERN_ERR "Reading the block number [%llu] failed.",
inode->data_block_number);
return 0;
}
buffer = (char *)bh->b_data;
nbytes = min((size_t) inode->file_size, len);
if (copy_to_user(buf, buffer, nbytes)) {
brelse(bh);
printk(KERN_ERR
"Error copying file contents to the userspace buffer\n");
return -EFAULT;
}
brelse(bh);
*ppos += nbytes;
return nbytes;
}
/* Save the modified inode */
int simplefs_inode_save(struct super_block *sb, struct simplefs_inode *sfs_inode)
{
struct simplefs_inode *inode_iterator;
struct buffer_head *bh;
bh = sb_bread(sb, SIMPLEFS_INODESTORE_BLOCK_NUMBER);
BUG_ON(!bh);
if (mutex_lock_interruptible(&simplefs_sb_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return -EINTR;
}
inode_iterator = simplefs_inode_search(sb,
(struct simplefs_inode *)bh->b_data,
sfs_inode);
if (likely(inode_iterator)) {
memcpy(inode_iterator, sfs_inode, sizeof(*inode_iterator));
printk(KERN_INFO "The inode updated\n");
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
} else {
mutex_unlock(&simplefs_sb_lock);
printk(KERN_ERR
"The new filesize could not be stored to the inode.");
return -EIO;
}
brelse(bh);
mutex_unlock(&simplefs_sb_lock);
return 0;
}
/* FIXME: The write support is rudimentary. I have not figured out a way to do writes
* from particular offsets (even though I have written some untested code for this below) efficiently. */
ssize_t simplefs_write(struct file * filp, const char __user * buf, size_t len,
loff_t * ppos)
{
/* After the commit dd37978c5 in the upstream linux kernel,
* we can use just filp->f_inode instead of the
* f->f_path.dentry->d_inode redirection */
struct inode *inode;
struct simplefs_inode *sfs_inode;
struct buffer_head *bh;
struct super_block *sb;
struct simplefs_super_block *sfs_sb;
handle_t *handle;
char *buffer;
int retval;
sb = filp->f_path.dentry->d_inode->i_sb;
sfs_sb = SIMPLEFS_SB(sb);
handle = jbd2_journal_start(sfs_sb->journal, 1);
if (IS_ERR(handle))
return PTR_ERR(handle);
retval = generic_write_checks(filp, ppos, &len, 0);
if (retval)
return retval;
inode = filp->f_path.dentry->d_inode;
sfs_inode = SIMPLEFS_INODE(inode);
bh = sb_bread(filp->f_path.dentry->d_inode->i_sb,
sfs_inode->data_block_number);
if (!bh) {
printk(KERN_ERR "Reading the block number [%llu] failed.",
sfs_inode->data_block_number);
return 0;
}
buffer = (char *)bh->b_data;
/* Move the pointer until the required byte offset */
buffer += *ppos;
retval = jbd2_journal_get_write_access(handle, bh);
if (WARN_ON(retval)) {
brelse(bh);
sfs_trace("Can't get write access for bh\n");
return retval;
}
if (copy_from_user(buffer, buf, len)) {
brelse(bh);
printk(KERN_ERR
"Error copying file contents from the userspace buffer to the kernel space\n");
return -EFAULT;
}
*ppos += len;
retval = jbd2_journal_dirty_metadata(handle, bh);
if (WARN_ON(retval)) {
brelse(bh);
return retval;
}
handle->h_sync = 1;
retval = jbd2_journal_stop(handle);
if (WARN_ON(retval)) {
brelse(bh);
return retval;
}
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
/* Set new size
* sfs_inode->file_size = max(sfs_inode->file_size, *ppos);
*
* FIXME: What to do if someone writes only some parts in between ?
* The above code will also fail in case a file is overwritten with
* a shorter buffer */
if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return -EINTR;
}
sfs_inode->file_size = *ppos;
retval = simplefs_inode_save(sb, sfs_inode);
if (retval) {
len = retval;
}
mutex_unlock(&simplefs_inodes_mgmt_lock);
return len;
}
const struct file_operations simplefs_file_operations = {
.read = simplefs_read,
.write = simplefs_write,
};
const struct file_operations simplefs_dir_operations = {
.owner = THIS_MODULE,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
.iterate = simplefs_iterate,
#else
.readdir = simplefs_readdir,
#endif
};
struct dentry *simplefs_lookup(struct inode *parent_inode,
struct dentry *child_dentry, unsigned int flags);
static int simplefs_create(struct inode *dir, struct dentry *dentry,
umode_t mode, bool excl);
static int simplefs_mkdir(struct inode *dir, struct dentry *dentry,
umode_t mode);
static struct inode_operations simplefs_inode_ops = {
.create = simplefs_create,
.lookup = simplefs_lookup,
.mkdir = simplefs_mkdir,
};
static int simplefs_create_fs_object(struct inode *dir, struct dentry *dentry,
umode_t mode)
{
struct inode *inode;
struct simplefs_inode *sfs_inode;
struct super_block *sb;
struct simplefs_inode *parent_dir_inode;
struct buffer_head *bh;
struct simplefs_dir_record *dir_contents_datablock;
uint64_t count;
int ret;
if (mutex_lock_interruptible(&simplefs_directory_children_update_lock)) {
sfs_trace("Failed to acquire mutex lock\n");
return -EINTR;
}
sb = dir->i_sb;
ret = simplefs_sb_get_objects_count(sb, &count);
if (ret < 0) {
mutex_unlock(&simplefs_directory_children_update_lock);
return ret;
}
if (unlikely(count >= SIMPLEFS_MAX_FILESYSTEM_OBJECTS_SUPPORTED)) {
/* The above condition can be just == instead of the >= */
printk(KERN_ERR
"Maximum number of objects supported by simplefs is already reached");
mutex_unlock(&simplefs_directory_children_update_lock);
return -ENOSPC;
}
if (!S_ISDIR(mode) && !S_ISREG(mode)) {
printk(KERN_ERR
"Creation request but for neither a file nor a directory");
mutex_unlock(&simplefs_directory_children_update_lock);
return -EINVAL;
}
inode = new_inode(sb);
if (!inode) {
mutex_unlock(&simplefs_directory_children_update_lock);
return -ENOMEM;
}
inode->i_sb = sb;
inode->i_op = &simplefs_inode_ops;
inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
inode->i_ino = (count + SIMPLEFS_START_INO - SIMPLEFS_RESERVED_INODES + 1);
sfs_inode = kmem_cache_alloc(sfs_inode_cachep, GFP_KERNEL);
sfs_inode->inode_no = inode->i_ino;
inode->i_private = sfs_inode;
sfs_inode->mode = mode;
if (S_ISDIR(mode)) {
printk(KERN_INFO "New directory creation request\n");
sfs_inode->dir_children_count = 0;
inode->i_fop = &simplefs_dir_operations;
} else if (S_ISREG(mode)) {
printk(KERN_INFO "New file creation request\n");
sfs_inode->file_size = 0;
inode->i_fop = &simplefs_file_operations;
}
/* First get a free block and update the free map,
* Then add inode to the inode store and update the sb inodes_count,
* Then update the parent directory's inode with the new child.
*
* The above ordering helps us to maintain fs consistency
* even in most crashes
*/
ret = simplefs_sb_get_a_freeblock(sb, &sfs_inode->data_block_number);
if (ret < 0) {
printk(KERN_ERR "simplefs could not get a freeblock");
mutex_unlock(&simplefs_directory_children_update_lock);
return ret;
}
simplefs_inode_add(sb, sfs_inode);
parent_dir_inode = SIMPLEFS_INODE(dir);
bh = sb_bread(sb, parent_dir_inode->data_block_number);
BUG_ON(!bh);
dir_contents_datablock = (struct simplefs_dir_record *)bh->b_data;
/* Navigate to the last record in the directory contents */
dir_contents_datablock += parent_dir_inode->dir_children_count;
dir_contents_datablock->inode_no = sfs_inode->inode_no;
strcpy(dir_contents_datablock->filename, dentry->d_name.name);
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
if (mutex_lock_interruptible(&simplefs_inodes_mgmt_lock)) {
mutex_unlock(&simplefs_directory_children_update_lock);
sfs_trace("Failed to acquire mutex lock\n");
return -EINTR;
}
parent_dir_inode->dir_children_count++;
ret = simplefs_inode_save(sb, parent_dir_inode);
if (ret) {
mutex_unlock(&simplefs_inodes_mgmt_lock);
mutex_unlock(&simplefs_directory_children_update_lock);
/* TODO: Remove the newly created inode from the disk and in-memory inode store
* and also update the superblock, freemaps etc. to reflect the same.
* Basically, Undo all actions done during this create call */
return ret;
}
mutex_unlock(&simplefs_inodes_mgmt_lock);
mutex_unlock(&simplefs_directory_children_update_lock);
inode_init_owner(inode, dir, mode);
d_add(dentry, inode);
return 0;
}
static int simplefs_mkdir(struct inode *dir, struct dentry *dentry,
umode_t mode)
{
/* I believe this is a bug in the kernel, for some reason, the mkdir callback
* does not get the S_IFDIR flag set. Even ext2 sets is explicitly */
return simplefs_create_fs_object(dir, dentry, S_IFDIR | mode);
}
static int simplefs_create(struct inode *dir, struct dentry *dentry,
umode_t mode, bool excl)
{
return simplefs_create_fs_object(dir, dentry, mode);
}
static struct inode *simplefs_iget(struct super_block *sb, int ino)
{
struct inode *inode;
struct simplefs_inode *sfs_inode;
sfs_inode = simplefs_get_inode(sb, ino);
inode = new_inode(sb);
inode->i_ino = ino;
inode->i_sb = sb;
inode->i_op = &simplefs_inode_ops;
if (S_ISDIR(sfs_inode->mode))
inode->i_fop = &simplefs_dir_operations;
else if (S_ISREG(sfs_inode->mode) || ino == SIMPLEFS_JOURNAL_INODE_NUMBER)
inode->i_fop = &simplefs_file_operations;
else
printk(KERN_ERR
"Unknown inode type. Neither a directory nor a file");
/* FIXME: We should store these times to disk and retrieve them */
inode->i_atime = inode->i_mtime = inode->i_ctime =
current_time(inode);
inode->i_private = sfs_inode;
return inode;
}
struct dentry *simplefs_lookup(struct inode *parent_inode,
struct dentry *child_dentry, unsigned int flags)
{
struct simplefs_inode *parent = SIMPLEFS_INODE(parent_inode);
struct super_block *sb = parent_inode->i_sb;
struct buffer_head *bh;
struct simplefs_dir_record *record;
int i;
bh = sb_bread(sb, parent->data_block_number);
BUG_ON(!bh);
sfs_trace("Lookup in: ino=%llu, b=%llu\n",
parent->inode_no, parent->data_block_number);
record = (struct simplefs_dir_record *)bh->b_data;
for (i = 0; i < parent->dir_children_count; i++) {
sfs_trace("Have file: '%s' (ino=%llu)\n",
record->filename, record->inode_no);
if (!strcmp(record->filename, child_dentry->d_name.name)) {
/* FIXME: There is a corner case where if an allocated inode,
* is not written to the inode store, but the inodes_count is
* incremented. Then if the random string on the disk matches
* with the filename that we are comparing above, then we
* will use an invalid uninitialized inode */
struct inode *inode = simplefs_iget(sb, record->inode_no);
inode_init_owner(inode, parent_inode, SIMPLEFS_INODE(inode)->mode);
d_add(child_dentry, inode);
return NULL;
}
record++;
}
printk(KERN_ERR
"No inode found for the filename [%s]\n",
child_dentry->d_name.name);
return NULL;
}
/**
* Simplest
*/
void simplefs_destroy_inode(struct inode *inode)
{
struct simplefs_inode *sfs_inode = SIMPLEFS_INODE(inode);
printk(KERN_INFO "Freeing private data of inode %p (%lu)\n",
sfs_inode, inode->i_ino);
kmem_cache_free(sfs_inode_cachep, sfs_inode);
}
static void simplefs_put_super(struct super_block *sb)
{
struct simplefs_super_block *sfs_sb = SIMPLEFS_SB(sb);
if (sfs_sb->journal)
WARN_ON(jbd2_journal_destroy(sfs_sb->journal) < 0);
sfs_sb->journal = NULL;
}
static const struct super_operations simplefs_sops = {
.destroy_inode = simplefs_destroy_inode,
.put_super = simplefs_put_super,
};
static int simplefs_load_journal(struct super_block *sb, int devnum)
{
struct journal_s *journal;
char b[BDEVNAME_SIZE];
dev_t dev;
struct block_device *bdev;
int hblock, blocksize, len;
struct simplefs_super_block *sfs_sb = SIMPLEFS_SB(sb);
dev = new_decode_dev(devnum);
printk(KERN_INFO "Journal device is: %s\n", __bdevname(dev, b));
bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
if (IS_ERR(bdev))
return 1;
blocksize = sb->s_blocksize;
hblock = bdev_logical_block_size(bdev);
len = SIMPLEFS_MAX_FILESYSTEM_OBJECTS_SUPPORTED;
journal = jbd2_journal_init_dev(bdev, sb->s_bdev, 1, -1, blocksize);
if (!journal) {
printk(KERN_ERR "Can't load journal\n");
return 1;
}
journal->j_private = sb;
sfs_sb->journal = journal;
return 0;
}
static int simplefs_sb_load_journal(struct super_block *sb, struct inode *inode)
{
struct journal_s *journal;
struct simplefs_super_block *sfs_sb = SIMPLEFS_SB(sb);
journal = jbd2_journal_init_inode(inode);
if (!journal) {
printk(KERN_ERR "Can't load journal\n");
return 1;
}
journal->j_private = sb;
sfs_sb->journal = journal;
return 0;
}
#define SIMPLEFS_OPT_JOURNAL_DEV 1
#define SIMPLEFS_OPT_JOURNAL_PATH 2
static const match_table_t tokens = {
{SIMPLEFS_OPT_JOURNAL_DEV, "journal_dev=%u"},
{SIMPLEFS_OPT_JOURNAL_PATH, "journal_path=%s"},
};
static int simplefs_parse_options(struct super_block *sb, char *options)
{
substring_t args[MAX_OPT_ARGS];
int token, ret, arg;
char *p;
while ((p = strsep(&options, ",")) != NULL) {
if (!*p)
continue;
args[0].to = args[0].from = NULL;
token = match_token(p, tokens, args);
switch (token) {
case SIMPLEFS_OPT_JOURNAL_DEV:
if (args->from && match_int(args, &arg))
return 1;
printk(KERN_INFO "Loading journal devnum: %i\n", arg);
if ((ret = simplefs_load_journal(sb, arg)))
return ret;
break;
case SIMPLEFS_OPT_JOURNAL_PATH:
{
char *journal_path;
struct inode *journal_inode;
struct path path;
BUG_ON(!(journal_path = match_strdup(&args[0])));
ret = kern_path(journal_path, LOOKUP_FOLLOW, &path);
if (ret) {
printk(KERN_ERR "could not find journal device path: error %d\n", ret);
kfree(journal_path);
}
journal_inode = path.dentry->d_inode;
path_put(&path);
kfree(journal_path);
if (S_ISBLK(journal_inode->i_mode)) {
unsigned long journal_devnum = new_encode_dev(journal_inode->i_rdev);
if ((ret = simplefs_load_journal(sb, journal_devnum)))
return ret;
} else {
/** Seems didn't work properly */
if ((ret = simplefs_sb_load_journal(sb, journal_inode)))
return ret;
}
break;
}
}
}
return 0;
}
/* This function, as the name implies, Makes the super_block valid and
* fills filesystem specific information in the super block */
int simplefs_fill_super(struct super_block *sb, void *data, int silent)
{
struct inode *root_inode;
struct buffer_head *bh;
struct simplefs_super_block *sb_disk;
int ret = -EPERM;
bh = sb_bread(sb, SIMPLEFS_SUPERBLOCK_BLOCK_NUMBER);
BUG_ON(!bh);
sb_disk = (struct simplefs_super_block *)bh->b_data;
printk(KERN_INFO "The magic number obtained in disk is: [%llu]\n",
sb_disk->magic);
if (unlikely(sb_disk->magic != SIMPLEFS_MAGIC)) {
printk(KERN_ERR
"The filesystem that you try to mount is not of type simplefs. Magicnumber mismatch.");
goto release;
}
if (unlikely(sb_disk->block_size != SIMPLEFS_DEFAULT_BLOCK_SIZE)) {
printk(KERN_ERR
"simplefs seem to be formatted using a non-standard block size.");
goto release;
}
/** XXX: Avoid this hack, by adding one more sb wrapper, but non-disk */
sb_disk->journal = NULL;
printk(KERN_INFO
"simplefs filesystem of version [%llu] formatted with a block size of [%llu] detected in the device.\n",
sb_disk->version, sb_disk->block_size);
/* A magic number that uniquely identifies our filesystem type */
sb->s_magic = SIMPLEFS_MAGIC;
/* For all practical purposes, we will be using this s_fs_info as the super block */
sb->s_fs_info = sb_disk;
sb->s_maxbytes = SIMPLEFS_DEFAULT_BLOCK_SIZE;
sb->s_op = &simplefs_sops;
root_inode = new_inode(sb);
root_inode->i_ino = SIMPLEFS_ROOTDIR_INODE_NUMBER;
inode_init_owner(root_inode, NULL, S_IFDIR);
root_inode->i_sb = sb;
root_inode->i_op = &simplefs_inode_ops;
root_inode->i_fop = &simplefs_dir_operations;
root_inode->i_atime = root_inode->i_mtime = root_inode->i_ctime =
current_time(root_inode);
root_inode->i_private =
simplefs_get_inode(sb, SIMPLEFS_ROOTDIR_INODE_NUMBER);
/* TODO: move such stuff into separate header. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
sb->s_root = d_make_root(root_inode);
#else
sb->s_root = d_alloc_root(root_inode);
if (!sb->s_root)
iput(root_inode);
#endif
if (!sb->s_root) {
ret = -ENOMEM;
goto release;
}
if ((ret = simplefs_parse_options(sb, data)))
goto release;
if (!sb_disk->journal) {
struct inode *journal_inode;
journal_inode = simplefs_iget(sb, SIMPLEFS_JOURNAL_INODE_NUMBER);
ret = simplefs_sb_load_journal(sb, journal_inode);
goto release;
}
ret = jbd2_journal_load(sb_disk->journal);
release:
brelse(bh);
return ret;
}
static struct dentry *simplefs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data)
{
struct dentry *ret;
ret = mount_bdev(fs_type, flags, dev_name, data, simplefs_fill_super);
if (unlikely(IS_ERR(ret)))
printk(KERN_ERR "Error mounting simplefs");
else
printk(KERN_INFO "simplefs is succesfully mounted on [%s]\n",
dev_name);
return ret;
}
static void simplefs_kill_superblock(struct super_block *sb)
{
printk(KERN_INFO
"simplefs superblock is destroyed. Unmount succesful.\n");
/* This is just a dummy function as of now. As our filesystem gets matured,
* we will do more meaningful operations here */
kill_block_super(sb);
return;
}
struct file_system_type simplefs_fs_type = {
.owner = THIS_MODULE,
.name = "simplefs",
.mount = simplefs_mount,
.kill_sb = simplefs_kill_superblock,
.fs_flags = FS_REQUIRES_DEV,
};
static int simplefs_init(void)
{
int ret;
sfs_inode_cachep = kmem_cache_create("sfs_inode_cache",
sizeof(struct simplefs_inode),
0,
(SLAB_RECLAIM_ACCOUNT| SLAB_MEM_SPREAD),
NULL);
if (!sfs_inode_cachep) {
return -ENOMEM;
}
ret = register_filesystem(&simplefs_fs_type);
if (likely(ret == 0))
printk(KERN_INFO "Sucessfully registered simplefs\n");
else
printk(KERN_ERR "Failed to register simplefs. Error:[%d]", ret);
return ret;
}
static void simplefs_exit(void)
{
int ret;
ret = unregister_filesystem(&simplefs_fs_type);
kmem_cache_destroy(sfs_inode_cachep);
if (likely(ret == 0))
printk(KERN_INFO "Sucessfully unregistered simplefs\n");
else
printk(KERN_ERR "Failed to unregister simplefs. Error:[%d]",
ret);
}
module_init(simplefs_init);
module_exit(simplefs_exit);
MODULE_LICENSE("CC0");
MODULE_AUTHOR("Sankar P");