forked from Ninals-GitHub/Learning-Ext2-Filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme2fs_super.c
2113 lines (1862 loc) · 62.9 KB
/
me2fs_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
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
992
993
994
995
996
997
998
999
1000
/********************************************************************************
File : me2fs_super.c
Description : super block operations for my ext2 file sytem
********************************************************************************/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/parser.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include "me2fs.h"
#include "me2fs_util.h"
#include "me2fs_super.h"
#include "me2fs_block.h"
#include "me2fs_inode.h"
#include "me2fs_ialloc.h"
#include "me2fs_sysfs.h"
/*
=================================================================================
Prototype Statement
=================================================================================
*/
/*
---------------------------------------------------------------------------------
For Super Block Operations
---------------------------------------------------------------------------------
*/
static int me2fsFillSuperBlock( struct super_block *sb,
void *data,
int silent );
static struct inode *me2fsAllocInode( struct super_block *sb );
static void me2fsDestroyInode( struct inode *inode );
static void me2fsPutSuperBlock( struct super_block *sb );
static int me2fsSyncFs( struct super_block *sb, int wait );
static int me2fsStatfs( struct dentry *dentry, struct kstatfs *buf );
static int me2fsRemount( struct super_block *sb,
int *flags,
char *data );
static int me2fsShowOptions( struct seq_file *seq, struct dentry *root );
static int me2fsFreeze( struct super_block *sb );
static int me2fsUnfreeze( struct super_block *sb );
/*
---------------------------------------------------------------------------------
For Seq File Operations
---------------------------------------------------------------------------------
*/
static int optionsOpenFs( struct inode *inode, struct file *file );
static int optionsSeqShow( struct seq_file *seq, void *offset );
/*
---------------------------------------------------------------------------------
Helper Functions
---------------------------------------------------------------------------------
*/
static void dbgPrintExt2SB( struct ext2_super_block *esb );
static void
dbgPrintExt2Bgd( struct ext2_group_desc *group_desc, unsigned long group_no );
static void dbgPrintMe2fsInfo( struct me2fs_sb_info *msi );
static loff_t me2fsMaxFileSize( struct super_block *sb );
static void me2fsInitInodeOnce( void *object );
static unsigned long
getDescriptorLocation( struct super_block *sb,
unsigned long logic_sb_block,
int num_bg );
static void me2fsSyncSuper( struct super_block *sb,
struct ext2_super_block *esb,
int wait );
static void clearSuperError( struct super_block *sb );
static int parseOptions( char *options, struct super_block *sb );
static unsigned long getSbBlock( void **data );
/*
=================================================================================
DEFINES
=================================================================================
*/
/*
==================================================================================
Management
==================================================================================
*/
/*
---------------------------------------------------------------------------------
Super Block Operations
---------------------------------------------------------------------------------
*/
//static void me2fs_dirty_inode( struct inode* inode, int flags )
//{ DBGPRINT( "<ME2FS>dirty_inode\n" ); }
//static int me2fs_write_inode( struct inode* inode, struct writeback_control *wbc )
//{ DBGPRINT( "<ME2FS>write_inode\n" ); return 0; }
//static int me2fs_drop_inode( struct inode *inode )
//{ DBGPRINT( "<ME2FS>drop_inode\n" ); return 0; }
//static void me2fs_evict_inode( struct inode *inode )
//{ DBGPRINT( "<ME2FS>evict_inode\n" ); }
//static int me2fs_sync_fs( struct super_block *sb, int wait )
//{ DBGPRINT( "<ME2FS>sync_fs\n" ); return 0; }
//static int me2fs_freeze_fs( struct super_block *sb )
//{ DBGPRINT( "<ME2FS>freeze_fs\n" ); return 0; }
//static int me2fs_unfreeze_fs( struct super_block *sb )
//{ DBGPRINT( "<ME2FS>unfreeze_fs\n" ); return 0; }
//static int me2fs_statfs( struct dentry *dentry, struct kstatfs *buf )
//{ DBGPRINT( "<ME2FS>statfs\n" ); return 0; }
//static int me2fs_remount_fs( struct super_block *sb, int *len, char *buf )
//{ DBGPRINT( "<ME2FS>remount_fs\n" ); return 0; }
//static void me2fs_umount_begin( struct super_block *sb )
//{ DBGPRINT( "<ME2FS>unmount_begin\n" ); }
//static int me2fs_show_options( struct seq_file *seq_file, struct dentry *dentry )
//{ DBGPRINT( "<ME2FS>show_options\n" ); return 0; }
static struct super_operations me2fs_super_ops = {
.alloc_inode = me2fsAllocInode,
.destroy_inode = me2fsDestroyInode,
// .dirty_inode = me2fs_dirty_inode,
.write_inode = me2fsWriteInode,
// .drop_inode = me2fs_drop_inode,
.evict_inode = me2fsEvictInode,
.put_super = me2fsPutSuperBlock,
.sync_fs = me2fsSyncFs,
.freeze_fs = me2fsFreeze,
.unfreeze_fs = me2fsUnfreeze,
.statfs = me2fsStatfs,
.remount_fs = me2fsRemount,
// .umount_begin = me2fs_umount_begin,
.show_options = me2fsShowOptions,
};
/*
---------------------------------------------------------------------------------
Export Operations
---------------------------------------------------------------------------------
*/
//static struct export_operations me2fs_export_ops;
/*
---------------------------------------------------------------------------------
Extended Attribute Handlers
---------------------------------------------------------------------------------
*/
#if 0
const static struct xattr_handler *me2fs_xattr_handler[ ] =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
#endif
/*
---------------------------------------------------------------------------------
seq options file operations
---------------------------------------------------------------------------------
*/
static const struct file_operations me2fs_seq_options_fops =
{
.owner = THIS_MODULE,
.open = optionsOpenFs,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
/*
---------------------------------------------------------------------------------
inode cache
---------------------------------------------------------------------------------
*/
static struct kmem_cache *me2fs_inode_cachep;
/*
---------------------------------------------------------------------------------
Token of Options
---------------------------------------------------------------------------------
*/
enum
{
Opt_bsd_df,
Opt_minix_df,
Opt_grpid,
Opt_nogrpid,
Opt_resgid,
Opt_resuid,
Opt_sb,
Opt_err_cont,
Opt_err_panic,
Opt_err_ro,
Opt_nouid32,
Opt_nocheck,
Opt_debug,
Opt_oldalloc,
Opt_orlov,
Opt_nobh,
Opt_user_xattr,
Opt_nouser_xattr,
Opt_acl,
Opt_noacl,
Opt_xip,
Opt_ignore,
Opt_err,
Opt_quota,
Opt_usrquota,
Opt_grpquota,
Opt_reservation,
Opt_noreservation,
};
static const match_table_t tokens =
{
{ Opt_bsd_df, "bsddf" },
{ Opt_minix_df, "minixdf" },
{ Opt_grpid, "grpid" },
{ Opt_grpid, "bsdgroups" },
{ Opt_nogrpid, "nogrpid" },
{ Opt_nogrpid, "sysvgroups" },
{ Opt_resgid, "resgid=%u" },
{ Opt_resuid, "resuid=%u" },
{ Opt_sb, "sb=%u" },
{ Opt_err_cont, "errors=continue" },
{ Opt_err_panic, "errors=panic" },
{ Opt_err_ro, "errors=remount-ro" },
{ Opt_nouid32, "nouid32" },
{ Opt_nocheck, "check=none" },
{ Opt_nocheck, "nocheck" },
{ Opt_debug, "debug" },
{ Opt_oldalloc, "oldalloc" },
{ Opt_orlov, "orlov" },
{ Opt_nobh, "nobh" },
{ Opt_user_xattr, "user_xattr" },
{ Opt_nouser_xattr, "nouser_xattr" },
{ Opt_acl, "acl" },
{ Opt_noacl, "noacl" },
{ Opt_xip, "xip" },
{ Opt_grpquota, "grpquota" },
{ Opt_quota, "quota" },
{ Opt_ignore, "noquota" },
{ Opt_usrquota, "usrquota" },
{ Opt_reservation, "reservation" },
{ Opt_noreservation, "noreservation" },
{ Opt_err, NULL },
};
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Open Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsInitInodeCache
Input :void
Output :void
Return :void
Description :initialize inode cache
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsInitInodeCache( void )
{
me2fs_inode_cachep = kmem_cache_create( "me2fs_inode_cachep",
sizeof( struct me2fs_inode_info ),
0,
( SLAB_RECLAIM_ACCOUNT |
SLAB_MEM_SPREAD ),
me2fsInitInodeOnce );
if( !me2fs_inode_cachep )
{
return( -ENOMEM );
}
return( 0 );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsDestroyInodeCache
Input :void
Output :void
Return :void
Description :destroy inode cache
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsDestroyInodeCache( void )
{
kmem_cache_destroy( me2fs_inode_cachep );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsMountBlockDev
Input :struct file_system_type *fs_type
< file system type >
int flags
< mount flags >
const char *dev_name
< device name >
void *data
< user data >
Output :void
Return :struct dentry *
< root dentry >
Description :mount me2fs over block device
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
struct dentry*
me2fsMountBlockDev( struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *data )
{
return( mount_bdev( fs_type, flags, dev_name, data, me2fsFillSuperBlock ) );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsWriteSuper
Input :struct super_block *sb
< vfs super block >
Output :void
Return :void
Description :write super block
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsWriteSuper( struct super_block *sb )
{
if( !( sb->s_flags & MS_RDONLY ) )
{
me2fsSyncFs( sb, 1 );
}
}
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Local Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
==================================================================================
Function :me2fsFillSuperBlock
Input :struct super_block *sb
< vfs super block object >
void *data
< user data >
int silent
< silent flag >
Output :void
Return :void
Description :fill my ext2 super block object
==================================================================================
*/
static int me2fsFillSuperBlock( struct super_block *sb,
void *data,
int silent )
{
struct buffer_head *bh;
struct ext2_super_block *esb;
struct me2fs_sb_info *msi;
struct inode *root;
int block_size;
int ret = -EINVAL;
int i;
int err;
unsigned long sb_block;
unsigned long def_mount_opts;
/* ------------------------------------------------------------------------ */
/* parse mount options to get super block location */
/* ------------------------------------------------------------------------ */
sb_block = getSbBlock( &data );
/* ------------------------------------------------------------------------ */
/* allocate memory to me2fs_sb_info */
/* ------------------------------------------------------------------------ */
msi = kzalloc( sizeof( struct me2fs_sb_info ), GFP_KERNEL );
if( !msi )
{
ME2FS_ERROR( "<ME2FS>error: unable to alloc me2fs_sb_info\n" );
ret = -ENOMEM;
return( ret );
}
/* set me2fs information to vfs super block */
sb->s_fs_info = ( void* )msi;
/* ------------------------------------------------------------------------ */
/* allocate memory to spin locks for block group */
/* ------------------------------------------------------------------------ */
msi->s_blockgroup_lock = kzalloc( sizeof( struct blockgroup_lock ),
GFP_KERNEL );
if( !msi->s_blockgroup_lock )
{
ME2FS_ERROR( "<ME2FS>error: unabel to alloc s_blockgroup_lock\n" );
kfree( msi );
return( -ENOMEM );
}
/* ------------------------------------------------------------------------ */
/* set device's block size and size bits to super block */
/* ------------------------------------------------------------------------ */
block_size = sb_min_blocksize( sb, BLOCK_SIZE );
DBGPRINT( "<ME2FS>Fill Super! block_size = %d\n", block_size );
DBGPRINT( "<ME2FS>s_blocksize_bits = %d\n", sb->s_blocksize_bits );
DBGPRINT( "<ME2FS>default block size is : %d\n", BLOCK_SIZE );
if( !block_size )
{
ME2FS_ERROR( "<ME2FS>error: unable to set blocksize\n" );
goto error_read_sb;
}
/* ------------------------------------------------------------------------ */
/* read super block */
/* ------------------------------------------------------------------------ */
if( !( bh = sb_bread( sb, sb_block ) ) )
{
ME2FS_ERROR( "<ME2FS>failed to bread super block\n" );
goto error_read_sb;
}
esb = ( struct ext2_super_block* )( bh->b_data );
/* ------------------------------------------------------------------------ */
/* check magic number */
/* ------------------------------------------------------------------------ */
sb->s_magic = le16_to_cpu( esb->s_magic );
if( sb->s_magic != ME2FS_SUPER_MAGIC )
{
ME2FS_ERROR( "<ME2FS>error : magic of super block is %lu\n", sb->s_magic );
goto error_mount;
}
/* ------------------------------------------------------------------------ */
/* check default options and mount options */
/* ------------------------------------------------------------------------ */
def_mount_opts = le32_to_cpu( esb->s_default_mount_opts );
if( def_mount_opts & EXT2_DEFM_DEBUG )
{
msi->s_mount_opt |= EXT2_MOUNT_DEBUG;
DBGPRINT( "<ME2FS>%s:option:set debug flag\n", __func__ );
}
if( def_mount_opts & EXT2_DEFM_BSDGROUPS )
{
msi->s_mount_opt |= EXT2_MOUNT_GRPID;
DBGPRINT( "<ME2FS>%s:option:set grpid flag\n", __func__ );
}
if( def_mount_opts & EXT2_DEFM_UID16 )
{
msi->s_mount_opt |= EXT2_MOUNT_NO_UID32;
DBGPRINT( "<ME2FS>%s:option:set nou_id32 flag\n", __func__ );
}
if( def_mount_opts & EXT2_DEFM_XATTR_USER )
{
msi->s_mount_opt |= EXT2_MOUNT_XATTR_USER;
DBGPRINT( "<ME2FS>%s:option:set xattr_user flag\n", __func__ );
}
if( def_mount_opts & EXT2_DEFM_ACL )
{
msi->s_mount_opt |= EXT2_MOUNT_POSIX_ACL;
DBGPRINT( "<ME2FS>%s:option:set posix_acl flag\n", __func__ );
}
if( le16_to_cpu( esb->s_errors ) == EXT2_ERRORS_PANIC )
{
msi->s_mount_opt |= EXT2_MOUNT_ERRORS_PANIC;
}
else if( le16_to_cpu( esb->s_errors ) == EXT2_ERRORS_CONTINUE )
{
msi->s_mount_opt |= EXT2_MOUNT_ERRORS_CONT;
}
else
{
msi->s_mount_opt |= EXT2_MOUNT_ERRORS_RO;
}
/* deaults(disk information cache) */
msi->s_resuid = make_kuid( &init_user_ns, le16_to_cpu( esb->s_def_resuid ) );
msi->s_resgid = make_kgid( &init_user_ns, le16_to_cpu( esb->s_def_resgid ) );
if( !parseOptions( ( char* )data, sb ) )
{
goto error_mount;
}
msi->s_mount_opt |= EXT2_MOUNT_RESERVATION;
msi->s_mount_opt &= ~EXT2_MOUNT_OLDALLOC;
msi->s_mount_opt &= ~EXT2_MOUNT_MINIX_DF;
sb->s_flags = sb->s_flags & ~MS_POSIXACL;
if( msi->s_mount_opt & EXT2_MOUNT_POSIX_ACL )
{
sb->s_flags |= MS_POSIXACL;
}
if( ( le32_to_cpu( esb->s_rev_level ) == EXT2_GOOD_OLD_REV ) &&
esb->s_feature_incompat & cpu_to_le32( ~EXT2_FEATURE_INCOMPAT_SUPP ) )
{
ME2FS_ERROR( "<ME2FS>%s:error:unsupported optional features(%x)",
__func__,
le32_to_cpu( esb->s_feature_incompat &
cpu_to_le32( ~EXT2_FEATURE_INCOMPAT_SUPP ) ) );
goto error_mount;
}
if( !( sb->s_flags & MS_RDONLY ) &&
esb->s_feature_incompat & cpu_to_le32( ~EXT2_FEATURE_INCOMPAT_SUPP ) )
{
ME2FS_ERROR( "<ME2FS>%s:error:unsupported optional features(%x)",
__func__,
le32_to_cpu( esb->s_feature_incompat &
cpu_to_le32( ~EXT2_FEATURE_INCOMPAT_SUPP ) ) );
goto error_mount;
}
/* ------------------------------------------------------------------------ */
/* check revison */
/* ------------------------------------------------------------------------ */
if( ME2FS_OLD_REV == le32_to_cpu( esb->s_rev_level ) )
{
ME2FS_ERROR( "<ME2FS>error : cannot mount old revision\n" );
goto error_mount;
}
dbgPrintExt2SB( esb );
/* ------------------------------------------------------------------------ */
/* set up me2fs super block information */
/* ------------------------------------------------------------------------ */
/* buffer cache information */
msi->s_esb = esb;
msi->s_sbh = bh;
/* super block(disk information cache) */
msi->s_sb_block = sb_block;
//msi->s_mount_opt = le32_to_cpu( esb->s_default_mount_opts );
msi->s_mount_state = le16_to_cpu( esb->s_state );
if( msi->s_mount_state != EXT2_VALID_FS )
{
ME2FS_ERROR( "<ME2FS>error : cannot mount invalid fs\n" );
goto error_mount;
}
if( le16_to_cpu( esb->s_errors ) != EXT2_ERRORS_CONTINUE )
{
if( le16_to_cpu( esb->s_errors ) != EXT2_ERRORS_CONTINUE )
{
ME2FS_ERROR( "<ME2FS>error : cannot mount read only fs\n" );
}
else
{
ME2FS_ERROR( "<ME2FS>error : fs panic detected\n" );
}
goto error_mount;
}
if( le32_to_cpu( esb->s_rev_level ) != EXT2_DYNAMIC_REV )
{
ME2FS_ERROR( "<ME2FS>error : cannot mount unsupported revision\n" );
goto error_mount;
}
/* inode(disk information cache) */
msi->s_inode_size = le16_to_cpu( esb->s_inode_size );
if( ( msi->s_inode_size < 128 ) ||
!is_power_of_2( msi->s_inode_size ) ||
( block_size < msi->s_inode_size ) )
{
ME2FS_ERROR( "<ME2FS>error : cannot mount unsupported inode size %u\n",
msi->s_inode_size );
goto error_mount;
}
msi->s_first_ino = le32_to_cpu( esb->s_first_ino );
msi->s_inodes_per_group = le32_to_cpu( esb->s_inodes_per_group );
msi->s_inodes_per_block = sb->s_blocksize / msi->s_inode_size;
if( msi->s_inodes_per_block == 0 )
{
ME2FS_ERROR( "<ME2FS>error : bad inodes per block\n" );
goto error_mount;
}
msi->s_itb_per_group = msi->s_inodes_per_group / msi->s_inodes_per_block;
/* group(disk information cache) */
msi->s_blocks_per_group = le32_to_cpu( esb->s_blocks_per_group );
if( msi->s_blocks_per_group == 0 )
{
ME2FS_ERROR( "<ME2FS>eroor : bad blocks per group\n" );
goto error_mount;
}
msi->s_groups_count = ( ( le32_to_cpu( esb->s_blocks_count )
- le32_to_cpu( esb->s_first_data_block ) - 1 )
/ msi->s_blocks_per_group ) + 1;
msi->s_desc_per_block = sb->s_blocksize / sizeof( struct ext2_group_desc );
msi->s_gdb_count = ( msi->s_groups_count + msi->s_desc_per_block - 1 )
/ msi->s_desc_per_block;
/* fragment(disk information cache) */
msi->s_frag_size = 1024 << le32_to_cpu( esb->s_log_frag_size );
if( msi->s_frag_size == 0 )
{
ME2FS_ERROR( "<ME2FS>eroor : bad fragment size\n" );
goto error_mount;
}
msi->s_frags_per_block = sb->s_blocksize / msi->s_frag_size;
msi->s_frags_per_group = le32_to_cpu( esb->s_frags_per_group );
/* deaults(disk information cache) */
//msi->s_resuid = make_kuid( &init_user_ns, le16_to_cpu( esb->s_def_resuid ) );
//msi->s_resgid = make_kgid( &init_user_ns, le16_to_cpu( esb->s_def_resgid ) );
dbgPrintMe2fsInfo( msi );
/* ------------------------------------------------------------------------ */
/* make procfs entries */
/* ------------------------------------------------------------------------ */
if( me2fsGetProcRoot( ) )
{
msi->s_proc = proc_mkdir( sb->s_id, me2fsGetProcRoot( ) );
}
if( msi->s_proc )
{
proc_create_data( "options", S_IRUGO, msi->s_proc,
&me2fs_seq_options_fops, sb );
}
/* ------------------------------------------------------------------------ */
/* read block group descriptor table */
/* ------------------------------------------------------------------------ */
msi->s_group_desc = kmalloc( msi->s_gdb_count * sizeof( struct buffer_head* ),
GFP_KERNEL );
if( !msi->s_group_desc )
{
ME2FS_ERROR( "<ME2FS>error : alloc memory for group desc is failed\n" );
goto error_mount_proc;
}
for( i = 0 ; i < msi->s_gdb_count ; i++ )
{
unsigned long block;
block = getDescriptorLocation( sb, sb_block, i );
if( !( msi->s_group_desc[ i ] = sb_bread( sb, block ) ) )
//if( !( msi->s_group_desc[ i ] = sb_bread( sb, sb_block + i + 1 ) ) )
{
ME2FS_ERROR( "<ME2FS>error : cannot read " );
ME2FS_ERROR( "block group descriptor[ group=%d ]\n", i );
goto error_mount_phase2;
}
}
/* ------------------------------------------------------------------------ */
/* sanity check for group descriptors */
/* ------------------------------------------------------------------------ */
for( i = 0 ; i < msi->s_groups_count ; i++ )
{
struct ext2_group_desc *gdesc;
unsigned long first_block;
unsigned long last_block;
unsigned long ar_block;
DBGPRINT( "<ME2FS>Block Count %d\n", i );
if( !( gdesc = me2fsGetGroupDescriptor( sb, i ) ) )
{
/* corresponding group descriptor does not exist */
goto error_mount_phase2;
}
first_block = ext2GetFirstBlockNum( sb, i );
if( i == ( msi->s_groups_count - 1 ) )
{
last_block = le32_to_cpu( esb->s_blocks_count ) - 1;
}
else
{
last_block = first_block + ( esb->s_blocks_per_group - 1 );
}
DBGPRINT( "<ME2FS>first = %lu, last = %lu\n", first_block, last_block );
ar_block = le32_to_cpu( gdesc->bg_block_bitmap );
if( ( ar_block < first_block ) || ( last_block < ar_block ) )
{
ME2FS_ERROR( "<ME2FS>error : block num of block bitmap is " );
ME2FS_ERROR( "insanity [ group=%d, first=%lu, block=%lu, last=%lu ]\n",
i, first_block, ar_block, last_block );
goto error_mount_phase2;
}
ar_block = le32_to_cpu( gdesc->bg_inode_bitmap );
if( ( ar_block < first_block ) || ( last_block < ar_block ) )
{
ME2FS_ERROR( "<ME2FS>error : block num of inode bitmap is " );
ME2FS_ERROR( "insanity [ group=%d, first=%lu, block=%lu, last=%lu ]\n",
i, first_block, ar_block, last_block );
goto error_mount_phase2;
}
ar_block = le32_to_cpu( gdesc->bg_inode_table );
if( ( ar_block < first_block ) || ( last_block < ar_block ) )
{
ME2FS_ERROR( "<ME2FS>error : block num of inode table is " );
ME2FS_ERROR( "insanity [ group=%d, first=%lu, block=%lu, last=%lu ]\n",
i, first_block, ar_block, last_block );
goto error_mount_phase2;
}
dbgPrintExt2Bgd( gdesc, i );
}
/* ------------------------------------------------------------------------ */
/* initialize reservation window */
/* ------------------------------------------------------------------------ */
msi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
msi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
msi->s_rsv_window_head.rsv_alloc_hit = 0;
msi->s_rsv_window_head.rsv_goal_size = 0;
me2fsInsertReserveWindow( sb, &msi->s_rsv_window_head );
/* ------------------------------------------------------------------------ */
/* initialize exclusive locks */
/* ------------------------------------------------------------------------ */
spin_lock_init( &msi->s_rsv_window_lock );
spin_lock_init( &msi->s_lock );
bgl_lock_init( msi->s_blockgroup_lock );
err = percpu_counter_init( &msi->s_freeblocks_counter,
me2fsCountFreeBlocks( sb ) );
if( err )
{
ME2FS_ERROR( "<ME2FS>cannot allocate memory for percpu counter" );
ME2FS_ERROR( "[s_freeblocks_counter]\n" );
goto error_mount_phase3;
}
err = percpu_counter_init( &msi->s_freeinodes_counter,
me2fsCountFreeInodes( sb ) );
if( err )
{
ME2FS_ERROR( "<ME2FS>cannot allocate memory for percpu counter" );
ME2FS_ERROR( "[s_freeinodes_counter]\n" );
goto error_mount_phase3;
}
err = percpu_counter_init( &msi->s_dirs_counter,
me2fsCountDirectories( sb ) );
if( err )
{
ME2FS_ERROR( "<ME2FS>cannot allocate memory for percpu counter" );
ME2FS_ERROR( "[s_dirs_counter]\n" );
goto error_mount_phase3;
}
/* ------------------------------------------------------------------------ */
/* add kobject to sysfs */
/* ------------------------------------------------------------------------ */
err = me2fsKobjInitAndAdd( sb, msi );
if( err )
{
ME2FS_ERROR( "<ME2FS>cannot add kobject to sysfs\n" );
goto error_mount_phase3;
}
/* ------------------------------------------------------------------------ */
/* set up vfs super block */
/* ------------------------------------------------------------------------ */
sb->s_op = &me2fs_super_ops;
//sb->s_export_op = &me2fs_export_ops;
//sb->s_xattr = me2fs_xattr_handler;
sb->s_maxbytes = me2fsMaxFileSize( sb );
sb->s_max_links = ME2FS_LINK_MAX;
DBGPRINT( "<ME2FS>max file size = %lld\n", sb->s_maxbytes );
root = me2fsGetVfsInode( sb, ME2FS_EXT2_ROOT_INO );
//root = iget_locked( sb, ME2FS_EXT2_ROOT_INO );
if( IS_ERR( root ) )
{
ME2FS_ERROR( "<ME2FS>error : failed to get root inode\n" );
ret = PTR_ERR( root );
goto error_mount_phase4;
}
if( !S_ISDIR( root->i_mode ) )
{
ME2FS_ERROR( "<ME2FS>root is not directory!!!!\n" );
}
sb->s_root = d_make_root( root );
//sb->s_root = d_alloc_root( root_ino );
if( !sb->s_root )
{
ME2FS_ERROR( "<ME2FS>error : failed to make root\n" );
ret = -ENOMEM;
goto error_mount_phase4;
}
le16_add_cpu( &esb->s_mnt_count, 1 );
me2fsWriteSuper( sb );
DBGPRINT( "<ME2FS> me2fs is mounted !\n" );
return( 0 );
/* ------------------------------------------------------------------------ */
/* unregister kset */
/* ------------------------------------------------------------------------ */
error_mount_phase4:
me2fsKobjRemove( msi );
/* ------------------------------------------------------------------------ */
/* destroy percpu counter */
/* ------------------------------------------------------------------------ */
error_mount_phase3:
percpu_counter_destroy( &msi->s_freeblocks_counter );
percpu_counter_destroy( &msi->s_freeinodes_counter );
percpu_counter_destroy( &msi->s_dirs_counter );
/* ------------------------------------------------------------------------ */
/* release buffer for group descriptors */
/* ------------------------------------------------------------------------ */
error_mount_phase2:
for( i = 0 ; i < msi->s_gdb_count ; i++ )
{
brelse( msi->s_group_desc[ i ] );
}
kfree( msi->s_group_desc );
/* ------------------------------------------------------------------------ */
/* remove procfs entries */
/* ------------------------------------------------------------------------ */
error_mount_proc:
if( msi->s_proc )
{
remove_proc_entry( "options", msi->s_proc );
remove_proc_entry( sb->s_id, me2fsGetProcRoot( ) );
}
/* ------------------------------------------------------------------------ */
/* release buffer cache for read super block */
/* ------------------------------------------------------------------------ */
error_mount:
brelse( bh );
/* ------------------------------------------------------------------------ */
/* release me2fs super block information */
/* ------------------------------------------------------------------------ */
error_read_sb:
sb->s_fs_info = NULL;
kfree( msi->s_blockgroup_lock );
kfree( msi );
return( ret );
}
/*
==================================================================================
Function :me2fsPutSuperBlock
Input :struct super_block *sb
< vfs super block >
Output :void
Return :void
Description :put super block
==================================================================================
*/
static void me2fsPutSuperBlock( struct super_block *sb )
{
struct me2fs_sb_info *msi;
int i;
msi = ME2FS_SB( sb );
/* ------------------------------------------------------------------------ */
/* destroy percpu counter */
/* ------------------------------------------------------------------------ */
percpu_counter_destroy( &msi->s_freeblocks_counter );
percpu_counter_destroy( &msi->s_freeinodes_counter );
percpu_counter_destroy( &msi->s_dirs_counter );
/* ------------------------------------------------------------------------ */
/* synchronize super block */
/* ------------------------------------------------------------------------ */
if( !( sb->s_flags & MS_RDONLY ) )
{
struct ext2_super_block *esb;
esb = msi->s_esb;
spin_lock( &msi->s_lock );
{
esb->s_state = cpu_to_le16( msi->s_mount_state );
}
spin_unlock( &msi->s_lock );
me2fsSyncSuper( sb, esb, 1 );
}
/* ------------------------------------------------------------------------ */
/* remove entries from procfs */
/* ------------------------------------------------------------------------ */
if( msi->s_proc )
{
remove_proc_entry( "options", msi->s_proc );
remove_proc_entry( sb->s_id, me2fsGetProcRoot( ) );
}
/* ------------------------------------------------------------------------ */
/* remove entries from sysfs */
/* ------------------------------------------------------------------------ */
me2fsKobjRemove( msi );
/* ------------------------------------------------------------------------ */
/* release buffer cache for block group descriptor */
/* ------------------------------------------------------------------------ */
for( i = 0 ; i < msi->s_gdb_count ; i++ )
{
if( msi->s_group_desc[ i ] )
{
brelse( msi->s_group_desc[ i ] );
}
}
kfree( msi->s_group_desc );
/* ------------------------------------------------------------------------ */
/* release buffer cache for super block */
/* ------------------------------------------------------------------------ */
brelse( msi->s_sbh );
sb->s_fs_info = NULL;
kfree( msi->s_blockgroup_lock );
kfree( msi );
}
/*
==================================================================================
Function :dbgPrintExt2SB
Input :struct ext2_super_block *esb
< super block of ext2 file system >
Output :void
Return :void
Description :print ext2 super block
==================================================================================
*/
static void dbgPrintExt2SB( struct ext2_super_block *esb )
{
unsigned int value;
int i;
value = ( unsigned int )( le32_to_cpu( esb->s_inodes_count ) );
DBGPRINT( "<ME2FS>s_inode_count = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_blocks_count ) );
DBGPRINT( "<ME2FS>s_blocks_count = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_r_blocks_count ) );
DBGPRINT( "<ME2FS>s_r_blocks_count = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_free_blocks_count ) );
DBGPRINT( "<ME2FS>s_free_blocks_count = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_free_inodes_count ) );
DBGPRINT( "<ME2FS>s_free_inodes_count = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_first_data_block ) );
DBGPRINT( "<ME2FS>s_first_data_block = %u\n", value );
value = ( unsigned int )( le32_to_cpu( esb->s_log_block_size ) );
DBGPRINT( "<ME2FS>s_log_block_size = %u\n", value );