forked from Ninals-GitHub/Learning-Ext2-Filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme2fs_block.c
2028 lines (1766 loc) · 54.7 KB
/
me2fs_block.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_block.c
Description : management blocks of my ext2 file system
*********************************************************************************/
#include <linux/sched.h>
#include <linux/buffer_head.h>
#include <linux/capability.h>
#include <linux/slab.h>
#include "me2fs.h"
#include "me2fs_util.h"
#include "me2fs_block.h"
/*
==================================================================================
Prototype Statement
==================================================================================
*/
static int isGroupSparse( int group );
static int testRoot( int group, int multiple );
struct ext2_reserve_window;
static int hasFreeBlocks( struct me2fs_sb_info *msi );
static struct buffer_head*
readBlockBitmap( struct super_block *sb, unsigned long block_group );
static int
validBlockBitmap( struct super_block *sb,
struct ext2_group_desc *gdesc,
unsigned long block_group,
struct buffer_head *bh );
static int
tryToAllocate( struct super_block *sb,
unsigned long group,
struct buffer_head *bitmap_bh,
unsigned long grp_goal,
unsigned long *count,
struct ext2_reserve_window *my_rsv );
static void
adjustGroupBlocks( struct super_block *sb,
unsigned long group_no,
struct ext2_group_desc *gdesc,
struct buffer_head *bh,
long count );
static inline int
isRsvEmpty( struct ext2_reserve_window *rsv );
static unsigned long
tryToAllocateWithRsv( struct super_block *sb,
unsigned int group,
struct buffer_head *bitmap_bh,
unsigned long grp_goal,
struct ext2_reserve_window_node *my_rsv,
unsigned long *count );
static int
goalInMyReservation( struct ext2_reserve_window *rsv,
unsigned long grp_goal,
unsigned int group,
struct super_block *sb );
static int
allocNewReservation( struct ext2_reserve_window_node *my_rsv,
unsigned long grp_goal,
struct super_block *sb,
unsigned int group,
struct buffer_head *bitmap_bh );
static void
tryToExtendReservation( struct ext2_reserve_window_node *my_rsv,
struct super_block *sb,
int size );
static unsigned long
searchBitmapNextUsableBlock( unsigned long start,
struct buffer_head *bh,
unsigned long end );
static struct ext2_reserve_window_node*
searchReserveWindow( struct rb_root *root, unsigned long goal );
static void removeReserveWindow( struct super_block *sb,
struct ext2_reserve_window_node *rsv );
static void
tryToExtendReservation( struct ext2_reserve_window_node *my_rsv,
struct super_block *sb,
int size );
static int
findNextReservableWindow( struct ext2_reserve_window_node *search_head,
struct ext2_reserve_window_node *my_rsv,
struct super_block *sb,
unsigned long start_block,
unsigned long last_block );
static unsigned long
findNextUsableBlock( int start, struct buffer_head *bh, int end );
/*
==================================================================================
DEFINES
==================================================================================
*/
#define IN_RANGE( b, first, len ) ( ( ( first ) <= ( b ) ) \
&& ( ( b ) <= ( first ) + ( len ) + 1 ) )
/*
==================================================================================
Management
==================================================================================
*/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Open Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsGetGroupDescriptor
Input :struct super_block *sb
< vfs super block >
unsigned int block_group
< block group number >
Output :void
Return :struct ext2_group_desc*
< goup descriptor >
Description :get ext2 group descriptor
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
struct ext2_group_desc*
me2fsGetGroupDescriptor( struct super_block *sb,
unsigned int block_group )
{
struct me2fs_sb_info *msi;
unsigned long gdesc_index;
unsigned long gdesc_offset;
struct ext2_group_desc *group_desc;
msi = ME2FS_SB( sb );
if( msi->s_groups_count <= block_group )
{
ME2FS_ERROR( "<ME2FS>block group number is out of groups count of sb\n" );
ME2FS_ERROR( "<ME2FS>s_groups_count = %lu\n", msi->s_groups_count );
ME2FS_ERROR( "<ME2FS>block_group = %u\n", block_group );
return( NULL );
}
gdesc_index = block_group / msi->s_desc_per_block;
if( !( group_desc =
( struct ext2_group_desc* )( msi->s_group_desc[ gdesc_index ]->b_data ) ) )
{
ME2FS_ERROR( "<ME2FS>canot find %u th group descriptor\n", block_group );
return( NULL );
}
gdesc_offset = block_group % msi->s_desc_per_block;
//return( &group_desc[ gdesc_offset ] );
return( group_desc + gdesc_offset );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsGetGdescBufferCache
Input :struct super_block *sb
< vfs super block >
unsigned int block group
< block group number >
Output :void
Return :struct buffer_head*
< buffer cache to which the block descriptor belongs >
Description :get buffer cache the block descriptor belongs to
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
struct buffer_head*
me2fsGetGdescBufferCache( struct super_block *sb, unsigned int block_group )
{
struct me2fs_sb_info *msi;
unsigned long gdesc_index;
msi = ME2FS_SB( sb );
if( msi->s_groups_count <= block_group )
{
ME2FS_ERROR( "<ME2FS>%s:large block group number", __func__ );
ME2FS_ERROR( "<ME2FS>block_group = %u, s_groups_count = %lu\n",
block_group, msi->s_groups_count );
return( NULL );
}
gdesc_index = block_group / msi->s_desc_per_block;
return( msi->s_group_desc[ gdesc_index ] );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsHasBgSuper
Input :struct super_block *sb
< vfs super block >
int gourp
< group number >
Output :void
Return :int
< result >
Description :does block goup have super block?
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsHasBgSuper( struct super_block *sb, int group )
{
if( ( ME2FS_SB( sb )->s_esb->s_feature_ro_compat &
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER ) &&
( !isGroupSparse( group ) ) )
{
return( 0 );
}
return( 1 );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsCountFreeBlocks
Input :struct super_block *sb
< vfs super block >
Output :void
Return :unsigned long
< number of free blocks in file system >
Description :count number of free blocks in file system
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
unsigned long me2fsCountFreeBlocks( struct super_block *sb )
{
unsigned long desc_count;
int i;
desc_count = 0;
for( i = 0 ; i < ME2FS_SB( sb )->s_groups_count ; i++ )
{
struct ext2_group_desc *gdesc;
if( !( gdesc = me2fsGetGroupDescriptor( sb, i ) ) )
{
continue;
}
desc_count += le16_to_cpu( gdesc->bg_free_blocks_count );
}
return( desc_count );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsNewBlocks
Input :struct inode *inode
< vfs inode >
unsigned long goal
< block number of goal >
unsinged long *count
< number of allocating block >
int *err
< result >
Output :int *err
< result >
Return :unsigned long
< filesystem-wide allocated block >
Description :core block(s) allocation function
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
unsigned long
me2fsNewBlocks( struct inode *inode,
unsigned goal,
unsigned long *count,
int*err )
{
struct super_block *sb;
struct me2fs_sb_info *msi;
struct ext2_super_block *esb;
struct ext2_group_desc *gdesc;
struct buffer_head *bitmap_bh;
struct buffer_head *gdesc_bh;
unsigned long group_no;
unsigned long goal_group;
unsigned long free_blocks;
unsigned long grp_alloc_blk;
unsigned long grp_target_blk;
unsigned long ret_block;
unsigned long num;
unsigned long ngroups;
int bgi;
int performed_allocation;
struct ext2_block_alloc_info *block_i;
struct ext2_reserve_window_node *my_rsv;
unsigned short windowsz;
sb = inode->i_sb;
msi = ME2FS_SB( sb );
esb = msi->s_esb;
bitmap_bh = NULL;
my_rsv = NULL;
windowsz = 0;
ret_block = 0;
num = *count;
performed_allocation = 0;
/* ------------------------------------------------------------------------ */
/* allocate a block from reservation (the filesystem always use reservetion */
/* window). it' a regular file, and the desired window size if greater than */
/* 0 (one could user ioctl command EXT2_IOC_SETRSVSZ to set the window size */
/* to 0 to turn off reservation on that particular file) */
/* ------------------------------------------------------------------------ */
block_i = ME2FS_I( inode )->i_block_alloc_info;
if( block_i )
{
windowsz = block_i->rsv_window_node.rsv_goal_size;
if( 0 < windowsz )
{
my_rsv = &block_i->rsv_window_node;
}
}
if( !hasFreeBlocks( msi ) )
{
*err = -ENOSPC;
goto out;
}
/* ------------------------------------------------------------------------ */
/* test whether the goal block is free */
/* ------------------------------------------------------------------------ */
if( ( goal < le32_to_cpu( esb->s_first_data_block ) )
|| le32_to_cpu( esb->s_blocks_count ) <= goal )
{
goal = le32_to_cpu( esb->s_first_data_block );
}
group_no = ( goal - le32_to_cpu( esb->s_first_data_block ) )
/ msi->s_blocks_per_group;
goal_group = group_no;
retry_alloc:
if( !( gdesc = me2fsGetGroupDescriptor( sb, group_no ) ) )
{
goto io_error;
}
if( !( gdesc_bh = me2fsGetGdescBufferCache( sb, group_no ) ) )
{
goto io_error;
}
free_blocks = le16_to_cpu( gdesc->bg_free_blocks_count );
/* ------------------------------------------------------------------------ */
/* if there is not enough free blocks to make a new reservation, turn off */
/* reservation for this allocation */
/* ------------------------------------------------------------------------ */
if( my_rsv &&
( free_blocks < windowsz ) &&
( isRsvEmpty( &my_rsv->rsv_window ) ) )
{
my_rsv = NULL;
}
if( 0 < free_blocks )
{
grp_target_blk = ( goal - le32_to_cpu( esb->s_first_data_block ) )
% msi->s_blocks_per_group;
bitmap_bh = readBlockBitmap( sb, group_no );
if( !bitmap_bh )
{
goto io_error;
}
#if 0
grp_alloc_blk = tryToAllocate( sb,
group_no,
bitmap_bh,
grp_target_blk,
count,
NULL );
#endif
grp_alloc_blk = tryToAllocateWithRsv( sb,
group_no,
bitmap_bh,
grp_target_blk,
my_rsv,
&num );
if( 0 <= grp_alloc_blk )
{
goto allocated;
}
}
#if 0
/* ------------------------------------------------------------------------ */
/* as for now, i do not implement free block search */
/* ------------------------------------------------------------------------ */
else
{
*err = -ENOSPC;
goto out;
}
#endif
ngroups = msi->s_groups_count;
smp_rmb( );
/* ------------------------------------------------------------------------ */
/* Now search the rest of the group. we assume that group_no and gdesc */
/* correctly point to the last group visited. */
/* ------------------------------------------------------------------------ */
for( bgi = 0 ; bgi < ngroups ; bgi++ )
{
group_no++;
if( ngroups <= group_no )
{
group_no = 0;
}
if( !( gdesc = me2fsGetGroupDescriptor( sb, group_no ) ) )
{
goto io_error;
}
if( !( gdesc_bh = me2fsGetGdescBufferCache( sb, group_no ) ) )
{
goto io_error;
}
free_blocks = le16_to_cpu( gdesc->bg_free_blocks_count );
/* -------------------------------------------------------------------- */
/* skip this group if the number of free blocks is less than half of */
/* the reservation window size. */
/* -------------------------------------------------------------------- */
if( my_rsv && ( free_blocks <= ( windowsz / 2 ) ) )
{
continue;
}
brelse( bitmap_bh );
if( !( bitmap_bh = readBlockBitmap( sb, group_no ) ) )
{
goto io_error;
}
/* -------------------------------------------------------------------- */
/* try to allocate block(s) from this group, without a goal - 1 */
/* -------------------------------------------------------------------- */
grp_alloc_blk = tryToAllocateWithRsv( sb,
group_no,
bitmap_bh,
-1,
my_rsv,
&num );
if( 0 <= grp_alloc_blk )
{
goto allocated;
}
}
/* ------------------------------------------------------------------------ */
/* we may end up a bogus earlie ENOSPC error due to filesystem is "full" */
/* of reservation, but there maybe indeed free blocks available on disk */
/* in this case, we just forget about the reservations just do block */
/* allocaton as without reservations. */
/* ------------------------------------------------------------------------ */
if( my_rsv )
{
my_rsv = NULL;
windowsz = 0;
group_no = goal_group;
goto retry_alloc;
}
/* ------------------------------------------------------------------------ */
/* no space left on the device */
/* ------------------------------------------------------------------------ */
*err = -ENOSPC;
goto out;
allocated:
DBGPRINT( "<ME2FS>%s: using block group = %lu, free blocks = %d \n",
__func__, group_no, gdesc->bg_free_blocks_count );
ret_block = grp_alloc_blk + ext2GetFirstBlockNum( sb, group_no );
if( IN_RANGE( le32_to_cpu( gdesc->bg_block_bitmap ), ret_block, num ) ||
IN_RANGE( le32_to_cpu( gdesc->bg_inode_bitmap ), ret_block, num ) ||
IN_RANGE( ret_block,
le32_to_cpu( gdesc->bg_inode_table ),
ME2FS_SB( sb )->s_itb_per_group ) )
{
ME2FS_ERROR( "<ME2FS>%s:allocating block in system zone -", __func__ );
ME2FS_ERROR( " block from %lu, length %lu\n", ret_block, num );
/* -------------------------------------------------------------------- */
/* tryToAllocate() marked the blocks we allocated as in user. So we */
/* may want to selectively mark some of the blocks as free */
/* -------------------------------------------------------------------- */
goto retry_alloc;
}
performed_allocation = 1;
if( le32_to_cpu( esb->s_blocks_count ) <= ( ret_block + num - 1 ) )
{
ME2FS_ERROR( "<ME2FS>%s:blocks count(%d) <= block(%lu)",
__func__, le32_to_cpu( esb->s_blocks_count ), ret_block);
ME2FS_ERROR( " - block group = %lu \n", group_no );
goto out;
}
adjustGroupBlocks( sb, group_no, gdesc, gdesc_bh, -num );
percpu_counter_sub( &msi->s_freeblocks_counter, num );
mark_buffer_dirty( bitmap_bh );
if( sb->s_flags & MS_SYNCHRONOUS )
{
sync_dirty_buffer( bitmap_bh );
}
*err = 0;
brelse( bitmap_bh );
if( num < *count )
{
#if 0 // quota
dquot_free_block_nodirty( inode, *count - num );
#endif
mark_inode_dirty( inode );
*count = num;
}
return( ret_block );
io_error:
*err = -EIO;
out:
if( !performed_allocation )
{
#if 0 // quota
dquot_free_block_nodirty( inode, *count );
#endif
mark_inode_dirty( inode );
}
brelse( bitmap_bh );
return( 0 );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsFreeblocks
Input :struct inode *inode
< vfs inode to free blocks >
unsigned long block_num
< block number to start to free blocks >
unsigned long count
< number of blocks to free >
Output :void
Return :void
Description :free blocks
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsFreeBlocks( struct inode *inode,
unsigned long block_num,
unsigned long count )
{
struct buffer_head *bitmap_bh;
struct buffer_head *gdesc_bh;
unsigned long block_group;
unsigned long bit;
unsigned long i;
unsigned long overflow;
struct super_block *sb;
struct me2fs_sb_info *msi;
struct ext2_group_desc *gdesc;
struct ext2_super_block *esb;
unsigned freed;
unsigned group_freed;
sb = inode->i_sb;
esb = ME2FS_SB( sb )->s_esb;
bitmap_bh = NULL;
freed = 0;
if( block_num < le32_to_cpu( esb->s_first_data_block ) ||
( block_num + count ) < block_num ||
le32_to_cpu( esb->s_blocks_count) < ( block_num + count ) )
{
ME2FS_ERROR( "<ME2FS>%s:error : freeing blocks not in datazone\n",
__func__ );
ME2FS_ERROR( "<ME2FS>block = %lu, count %lu\n", block_num, count );
goto error_return;
}
msi = ME2FS_SB( sb );
do_more:
overflow = 0;
block_group = ( block_num - le32_to_cpu( esb->s_first_data_block ) )
/ msi->s_blocks_per_group;
bit = ( block_num - le32_to_cpu( esb->s_first_data_block ) )
% msi->s_blocks_per_group;
/* ------------------------------------------------------------------------ */
/* check to see if we are freeing blocks across a group boundary */
/* ------------------------------------------------------------------------ */
if( msi->s_blocks_per_group < ( bit + count ) )
{
overflow = bit + count - msi->s_blocks_per_group;
count = count - overflow;
}
if( likely( bitmap_bh ) )
{
brelse( bitmap_bh );
}
if( !( bitmap_bh = readBlockBitmap( sb, block_group ) ) )
{
goto error_return;
}
gdesc = me2fsGetGroupDescriptor( sb, block_group );
if( !gdesc )
{
goto error_return;
}
gdesc_bh = me2fsGetGdescBufferCache( sb, block_group );
if( IN_RANGE( le32_to_cpu( gdesc->bg_block_bitmap ), block_num, count ) ||
IN_RANGE( le32_to_cpu( gdesc->bg_inode_bitmap ), block_num, count ) ||
IN_RANGE( block_num,
le32_to_cpu( gdesc->bg_inode_table ),
msi->s_itb_per_group ) ||
IN_RANGE( block_num + count - 1,
le32_to_cpu( gdesc->bg_inode_table ),
msi->s_itb_per_group ) )
{
ME2FS_ERROR( "<ME2FS>%s:error:Freeing blocks in system zones\n",
__func__ );
ME2FS_ERROR( "<ME2FS>block = %lu, count = %lu\n", block_num, count );
goto error_return;
}
for( i = 0 , group_freed = 0 ; i < count ; i++ )
{
if( !ext2_clear_bit_atomic( getSbBlockGroupLock( msi, block_group ),
bit + i,
bitmap_bh->b_data ) )
{
ME2FS_ERROR( "<ME2FS>%s:warning:bit already clreaed for block %lu\n",
__func__, block_num );
}
else
{
group_freed++;
}
}
mark_buffer_dirty( bitmap_bh );
if( sb->s_flags & MS_SYNCHRONOUS )
{
sync_dirty_buffer( bitmap_bh );
}
adjustGroupBlocks( sb, block_group, gdesc, gdesc_bh, group_freed );
freed += group_freed;
if( overflow )
{
block_num += count;
count = overflow;
goto do_more;
}
error_return:
brelse( bitmap_bh );
if( freed )
{
percpu_counter_add( &msi->s_freeblocks_counter, freed );
#if 0 // quota
dquot_free_block_nodirty( inode, freed );
#endif
mark_inode_dirty( inode );
}
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsGetBlocksUsedByGroupTable
Input :struct super_block *sb
< vfs super block >
int group
< goupr number >
Output :void
Return :void
Description :get number of blocks used by the group table in group
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
unsigned long
me2fsGetBlocksUsedByGroupTable( struct super_block *sb, int group )
{
if( me2fsHasBgSuper( sb, group ) )
{
return( ME2FS_SB( sb )->s_gdb_count );
}
return( 0 );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsInitBlockAllocInfo
Input :struct inode *inode
< vfs inode >
Output :void
Return :void
Description :allocate block_alloc_info
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsInitBlockAllocInfo( struct inode *inode )
{
struct ext2_block_alloc_info *alloc_info;
alloc_info = kmalloc( sizeof( *alloc_info ), GFP_NOFS );
if( alloc_info )
{
struct ext2_reserve_window_node *rsv;
rsv = &alloc_info->rsv_window_node;
rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
rsv->rsv_alloc_hit = 0;
alloc_info->last_alloc_logical_block = 0;
alloc_info->last_alloc_physical_block = 0;
}
ME2FS_I( inode )->i_block_alloc_info = alloc_info;
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsInsertReserveWindow
Input :struct superbe_block *sb
< vfs super block >
struct ext2_reserve_window_node *rsv
< reservation window node to add >
Output :void
Return :void
Description :insert a window to the block reservation rb tree
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsInsertReserveWindow( struct super_block *sb,
struct ext2_reserve_window_node *rsv )
{
struct rb_root *root;
struct rb_node *node;
unsigned long start;
struct rb_node **p;
struct rb_node *parent;
struct ext2_reserve_window_node *this;
root = &ME2FS_SB( sb )->s_rsv_window_root;
node = &rsv->rsv_node;
start = rsv->rsv_start;
p = &root->rb_node;
parent = NULL;
while( *p )
{
parent = *p;
this = rb_entry( parent, struct ext2_reserve_window_node, rsv_node );
if( start < this->rsv_start )
{
p = &( ( *p )->rb_left );
}
else if( this->rsv_end < start )
{
p = &( ( *p )->rb_right );
}
else
{
DBGPRINT( "<ME2FS>%s:bug on\n", __func__ );
}
}
rb_link_node( node, parent, p );
rb_insert_color( node, root );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsDiscardReservation
Input :struct inode *inode
< vfs inode >
Output :void
Return :void
Description :free block reservation window on last file close, or truncate
or at last input()
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsDiscardReservation( struct inode *inode )
{
struct me2fs_inode_info *mei;
struct ext2_block_alloc_info *block_i;
struct ext2_reserve_window_node *rsv;
spinlock_t *rsv_lock;
mei = ME2FS_I( inode );
if( !( block_i = mei->i_block_alloc_info ) )
{
return;
}
rsv_lock = &ME2FS_SB( inode->i_sb )->s_rsv_window_lock;
rsv = &block_i->rsv_window_node;
if( !isRsvEmpty( &rsv->rsv_window ) )
{
spin_lock( rsv_lock );
{
if( !isRsvEmpty( &rsv->rsv_window ) )
{
removeReserveWindow( inode->i_sb, rsv );
}
}
spin_unlock( rsv_lock );
}
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :void
Input :void
Output :void
Return :void
Description :void
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Local Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
==================================================================================
Function :isGroupSparse
Input :int group
< group number >
Output :void
Return :int
< result >
Description :is group sparsed?
==================================================================================
*/
static int isGroupSparse( int group )
{
if( group <= 1 )
{
return( 1 );
}
return( testRoot( group, 3 ) ||
testRoot( group, 5 ) ||
testRoot( group, 7 ) );
}
/*
==================================================================================
Function :testRoot
Input :int group
< group number >
int multiple
< multiple number >
Output :void
Return :void
Description :test whether given group number is multiples of given
multiple number or not
==================================================================================
*/
static int testRoot( int group, int multiple )
{
int num;
num = multiple;
while( num < group )
{
num *= multiple;
}
return( num == group );
}
/*
==================================================================================
Function :hasFreeBlocks
Input :struct me2fs_sb_info *msi
< me2fs super block information >
Output :void
Return :int
< result >
Description :test whethrer filesystem has free blocks or not
==================================================================================
*/
static int hasFreeBlocks( struct me2fs_sb_info *msi )
{
unsigned long free_blocks;
unsigned long root_blocks;
free_blocks = percpu_counter_read_positive( &msi->s_freeblocks_counter );
root_blocks = le32_to_cpu( msi->s_esb->s_r_blocks_count );
if( ( free_blocks < ( root_blocks + 1 ) )
&& !capable( CAP_SYS_RESOURCE )
&& !uid_eq( msi->s_resuid, current_fsuid( ) )
&& ( gid_eq( msi->s_resgid, GLOBAL_ROOT_GID )
|| !in_group_p( msi->s_resgid ) ) )
{
return( 0 );
}
return( 1 );
}
/*
==================================================================================
Function :readBlockBitmap
Input :struct super_block *sb
< vfs super block >
unsigned long block_group
< block group number to read bitmap >
Output :void
Return :struct buffer_head*
< buffer cache bimap belongs to >
Description :read bitmap for a given block group
==================================================================================
*/
static struct buffer_head*
readBlockBitmap( struct super_block *sb, unsigned long block_group )
{
struct ext2_group_desc *gdesc;
struct buffer_head *bh;
unsigned long bitmap_blk;
if( !( gdesc = me2fsGetGroupDescriptor( sb, block_group ) ) )
{
return( NULL );
}
bitmap_blk = le32_to_cpu( gdesc->bg_block_bitmap );
bh = sb_getblk( sb, bitmap_blk );
if( unlikely( !bh ) )
{
ME2FS_ERROR( "<ME2FS>%s:cannot read block bitmap", __func__ );
ME2FS_ERROR( "block_group=%lu, block_bitmap=%u\n",
block_group, le32_to_cpu( gdesc->bg_block_bitmap ) );