forked from data-accelerator/e2fsprogs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresize2fs.c
3235 lines (2850 loc) · 88.2 KB
/
resize2fs.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
/*
* resize2fs.c --- ext2 main routine
*
* Copyright (C) 1997, 1998 by Theodore Ts'o and
* PowerQuest, Inc.
*
* Copyright (C) 1999, 2000 by Theodore Ts'o
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
/*
* Resizing a filesystem consists of the following phases:
*
* 1. Adjust superblock and write out new parts of the inode
* table
* 2. Determine blocks which need to be relocated, and copy the
* contents of blocks from their old locations to the new ones.
* 3. Scan the inode table, doing the following:
* a. If blocks have been moved, update the block
* pointers in the inodes and indirect blocks to
* point at the new block locations.
* b. If parts of the inode table need to be evacuated,
* copy inodes from their old locations to their
* new ones.
* c. If (b) needs to be done, note which blocks contain
* directory information, since we will need to
* update the directory information.
* 4. Update the directory blocks with the new inode locations.
* 5. Move the inode tables, if necessary.
*/
#include "config.h"
#include "resize2fs.h"
#include <time.h>
#ifdef __linux__ /* Kludge for debugging */
#define RESIZE2FS_DEBUG
#endif
static void fix_uninit_block_bitmaps(ext2_filsys fs);
static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
static errcode_t blocks_to_move(ext2_resize_t rfs);
static errcode_t block_mover(ext2_resize_t rfs);
static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
static errcode_t inode_ref_fix(ext2_resize_t rfs);
static errcode_t move_itables(ext2_resize_t rfs);
static errcode_t fix_resize_inode(ext2_filsys fs);
static errcode_t resize2fs_calculate_summary_stats(ext2_filsys fs);
static errcode_t fix_sb_journal_backup(ext2_filsys fs);
static errcode_t mark_table_blocks(ext2_filsys fs,
ext2fs_block_bitmap bmap);
static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs);
static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
ext2fs_block_bitmap meta_bmap);
static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size);
static errcode_t move_bg_metadata(ext2_resize_t rfs);
static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs);
/*
* Some helper functions to check if a block is in a metadata area
*/
static inline int is_block_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
{
return blk == ext2fs_block_bitmap_loc(fs, grp);
}
static inline int is_inode_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
{
return blk == ext2fs_inode_bitmap_loc(fs, grp);
}
static int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
{
return blk >= ext2fs_inode_table_loc(fs, grp) &&
blk < (ext2fs_inode_table_loc(fs, grp) +
fs->inode_blocks_per_group);
}
/* Some bigalloc helper macros which are more succinct... */
#define B2C(x) EXT2FS_B2C(fs, (x))
#define C2B(x) EXT2FS_C2B(fs, (x))
#define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
#define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
#define LT_CLSTR(x, y) (B2C(x) < B2C(y))
#define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
#define GT_CLSTR(x, y) (B2C(x) > B2C(y))
static int lazy_itable_init;
/*
* This is the top-level routine which does the dirty deed....
*/
errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
errcode_t (*progress)(ext2_resize_t rfs, int pass,
unsigned long cur,
unsigned long max_val))
{
ext2_resize_t rfs;
errcode_t retval;
struct resource_track rtrack, overall_track;
/*
* Create the data structure
*/
retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
if (retval)
return retval;
memset(rfs, 0, sizeof(struct ext2_resize_struct));
fs->priv_data = rfs;
rfs->old_fs = fs;
rfs->flags = flags;
rfs->itable_buf = 0;
rfs->progress = progress;
init_resource_track(&overall_track, "overall resize2fs", fs->io);
init_resource_track(&rtrack, "read_bitmaps", fs->io);
retval = ext2fs_read_bitmaps(fs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
fs->super->s_state |= EXT2_ERROR_FS;
ext2fs_mark_super_dirty(fs);
ext2fs_flush(fs);
init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
fix_uninit_block_bitmaps(fs);
print_resource_track(rfs, &rtrack, fs->io);
retval = ext2fs_dup_handle(fs, &rfs->new_fs);
if (retval)
goto errout;
init_resource_track(&rtrack, "resize_group_descriptors", fs->io);
retval = resize_group_descriptors(rfs, *new_size);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "move_bg_metadata", fs->io);
retval = move_bg_metadata(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "zero_high_bits_in_metadata", fs->io);
retval = zero_high_bits_in_inodes(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "adjust_superblock", fs->io);
retval = adjust_superblock(rfs, *new_size);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
fix_uninit_block_bitmaps(rfs->new_fs);
print_resource_track(rfs, &rtrack, fs->io);
/* Clear the block bitmap uninit flag for the last block group */
ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
EXT2_BG_BLOCK_UNINIT);
*new_size = ext2fs_blocks_count(rfs->new_fs->super);
init_resource_track(&rtrack, "blocks_to_move", fs->io);
retval = blocks_to_move(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
#ifdef RESIZE2FS_DEBUG
if (rfs->flags & RESIZE_DEBUG_BMOVE)
printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
(unsigned long long) ext2fs_free_blocks_count(rfs->old_fs->super),
(unsigned long long) ext2fs_free_blocks_count(rfs->new_fs->super),
(unsigned long long) rfs->needed_blocks);
#endif
init_resource_track(&rtrack, "block_mover", fs->io);
retval = block_mover(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
retval = inode_scan_and_fix(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "inode_ref_fix", fs->io);
retval = inode_ref_fix(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "move_itables", fs->io);
retval = move_itables(rfs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
retval = clear_sparse_super2_last_group(rfs);
if (retval)
goto errout;
init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
retval = resize2fs_calculate_summary_stats(rfs->new_fs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "fix_resize_inode", fs->io);
retval = fix_resize_inode(rfs->new_fs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
retval = fix_sb_journal_backup(rfs->new_fs);
if (retval)
goto errout;
print_resource_track(rfs, &rtrack, fs->io);
retval = ext2fs_set_gdt_csum(rfs->new_fs);
if (retval)
goto errout;
rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
print_resource_track(rfs, &overall_track, fs->io);
retval = ext2fs_close_free(&rfs->new_fs);
if (retval)
goto errout;
rfs->flags = flags;
ext2fs_free(rfs->old_fs);
rfs->old_fs = NULL;
if (rfs->itable_buf)
ext2fs_free_mem(&rfs->itable_buf);
if (rfs->reserve_blocks)
ext2fs_free_block_bitmap(rfs->reserve_blocks);
if (rfs->move_blocks)
ext2fs_free_block_bitmap(rfs->move_blocks);
ext2fs_free_mem(&rfs);
return 0;
errout:
if (rfs->new_fs) {
ext2fs_free(rfs->new_fs);
rfs->new_fs = NULL;
}
if (rfs->itable_buf)
ext2fs_free_mem(&rfs->itable_buf);
ext2fs_free_mem(&rfs);
return retval;
}
/* Keep the size of the group descriptor region constant */
static void adjust_reserved_gdt_blocks(ext2_filsys old_fs, ext2_filsys fs)
{
if (ext2fs_has_feature_resize_inode(fs->super) &&
(old_fs->desc_blocks != fs->desc_blocks)) {
int new;
new = ((int) fs->super->s_reserved_gdt_blocks) +
(old_fs->desc_blocks - fs->desc_blocks);
if (new < 0)
new = 0;
if (new > (int) fs->blocksize/4)
new = fs->blocksize/4;
fs->super->s_reserved_gdt_blocks = new;
}
}
/* Toggle 64bit mode */
static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
{
void *o, *n, *new_group_desc;
dgrp_t i;
int copy_size;
errcode_t retval;
if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
return 0;
if (new_size != ext2fs_blocks_count(rfs->new_fs->super) ||
ext2fs_blocks_count(rfs->new_fs->super) >= (1ULL << 32) ||
(rfs->flags & RESIZE_DISABLE_64BIT &&
rfs->flags & RESIZE_ENABLE_64BIT))
return EXT2_ET_INVALID_ARGUMENT;
if (rfs->flags & RESIZE_DISABLE_64BIT) {
ext2fs_clear_feature_64bit(rfs->new_fs->super);
rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
} else if (rfs->flags & RESIZE_ENABLE_64BIT) {
ext2fs_set_feature_64bit(rfs->new_fs->super);
rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
}
if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
EXT2_DESC_SIZE(rfs->new_fs->super))
return 0;
o = rfs->new_fs->group_desc;
rfs->new_fs->desc_blocks = ext2fs_div_ceil(
rfs->old_fs->group_desc_count,
EXT2_DESC_PER_BLOCK(rfs->new_fs->super));
retval = ext2fs_get_arrayzero(rfs->new_fs->desc_blocks,
rfs->old_fs->blocksize, &new_group_desc);
if (retval)
return retval;
n = new_group_desc;
if (EXT2_DESC_SIZE(rfs->old_fs->super) <=
EXT2_DESC_SIZE(rfs->new_fs->super))
copy_size = EXT2_DESC_SIZE(rfs->old_fs->super);
else
copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
memcpy(n, o, copy_size);
n = (char *)n + EXT2_DESC_SIZE(rfs->new_fs->super);
o = (char *)o + EXT2_DESC_SIZE(rfs->old_fs->super);
}
ext2fs_free_mem(&rfs->new_fs->group_desc);
rfs->new_fs->group_desc = new_group_desc;
for (i = 0; i < rfs->old_fs->group_desc_count; i++)
ext2fs_group_desc_csum_set(rfs->new_fs, i);
adjust_reserved_gdt_blocks(rfs->old_fs, rfs->new_fs);
return 0;
}
/* Move bitmaps/inode tables out of the way. */
static errcode_t move_bg_metadata(ext2_resize_t rfs)
{
dgrp_t i;
blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
ext2fs_block_bitmap old_map, new_map;
int old, new;
errcode_t retval;
int cluster_ratio;
if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
return 0;
retval = ext2fs_allocate_block_bitmap(rfs->old_fs, "oldfs", &old_map);
if (retval)
return retval;
retval = ext2fs_allocate_block_bitmap(rfs->new_fs, "newfs", &new_map);
if (retval)
goto out;
if (ext2fs_has_feature_meta_bg(rfs->old_fs->super)) {
old_desc_blocks = rfs->old_fs->super->s_first_meta_bg;
new_desc_blocks = rfs->new_fs->super->s_first_meta_bg;
} else {
old_desc_blocks = rfs->old_fs->desc_blocks +
rfs->old_fs->super->s_reserved_gdt_blocks;
new_desc_blocks = rfs->new_fs->desc_blocks +
rfs->new_fs->super->s_reserved_gdt_blocks;
}
/* Construct bitmaps of super/descriptor blocks in old and new fs */
for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, i, &b, &c, &d,
NULL);
if (retval)
goto out;
if (b)
ext2fs_mark_block_bitmap2(old_map, b);
for (j = 0; c != 0 && j < old_desc_blocks; j++)
ext2fs_mark_block_bitmap2(old_map, c + j);
if (d)
ext2fs_mark_block_bitmap2(old_map, d);
retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, i, &b, &c, &d,
NULL);
if (retval)
goto out;
if (b)
ext2fs_mark_block_bitmap2(new_map, b);
for (j = 0; c != 0 && j < new_desc_blocks; j++)
ext2fs_mark_block_bitmap2(new_map, c + j);
if (d)
ext2fs_mark_block_bitmap2(new_map, d);
}
cluster_ratio = EXT2FS_CLUSTER_RATIO(rfs->new_fs);
/* Find changes in block allocations for bg metadata */
for (b = EXT2FS_B2C(rfs->old_fs,
rfs->old_fs->super->s_first_data_block);
b < ext2fs_blocks_count(rfs->new_fs->super);
b += cluster_ratio) {
old = ext2fs_test_block_bitmap2(old_map, b);
new = ext2fs_test_block_bitmap2(new_map, b);
if (old && !new) {
/* mark old_map, unmark new_map */
if (cluster_ratio == 1)
ext2fs_unmark_block_bitmap2(
rfs->new_fs->block_map, b);
} else if (!old && new)
; /* unmark old_map, mark new_map */
else {
ext2fs_unmark_block_bitmap2(old_map, b);
ext2fs_unmark_block_bitmap2(new_map, b);
}
}
/*
* new_map now shows blocks that have been newly allocated.
* old_map now shows blocks that have been newly freed.
*/
/*
* Move any conflicting bitmaps and inode tables. Ensure that we
* don't try to free clusters associated with bitmaps or tables.
*/
for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
b = ext2fs_block_bitmap_loc(rfs->new_fs, i);
if (ext2fs_test_block_bitmap2(new_map, b))
ext2fs_block_bitmap_loc_set(rfs->new_fs, i, 0);
else if (ext2fs_test_block_bitmap2(old_map, b))
ext2fs_unmark_block_bitmap2(old_map, b);
b = ext2fs_inode_bitmap_loc(rfs->new_fs, i);
if (ext2fs_test_block_bitmap2(new_map, b))
ext2fs_inode_bitmap_loc_set(rfs->new_fs, i, 0);
else if (ext2fs_test_block_bitmap2(old_map, b))
ext2fs_unmark_block_bitmap2(old_map, b);
c = ext2fs_inode_table_loc(rfs->new_fs, i);
for (b = 0;
b < rfs->new_fs->inode_blocks_per_group;
b++) {
if (ext2fs_test_block_bitmap2(new_map, b + c))
ext2fs_inode_table_loc_set(rfs->new_fs, i, 0);
else if (ext2fs_test_block_bitmap2(old_map, b + c))
ext2fs_unmark_block_bitmap2(old_map, b + c);
}
}
/* Free unused clusters */
for (b = 0;
cluster_ratio > 1 && b < ext2fs_blocks_count(rfs->new_fs->super);
b += cluster_ratio)
if (ext2fs_test_block_bitmap2(old_map, b))
ext2fs_unmark_block_bitmap2(rfs->new_fs->block_map, b);
out:
if (old_map)
ext2fs_free_block_bitmap(old_map);
if (new_map)
ext2fs_free_block_bitmap(new_map);
return retval;
}
/* Zero out the high bits of extent fields */
static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode *inode)
{
ext2_extent_handle_t handle;
struct ext2fs_extent extent;
int op = EXT2_EXTENT_ROOT;
errcode_t errcode;
if (!(inode->i_flags & EXT4_EXTENTS_FL))
return 0;
errcode = ext2fs_extent_open(fs, ino, &handle);
if (errcode)
return errcode;
while (1) {
errcode = ext2fs_extent_get(handle, op, &extent);
if (errcode)
break;
op = EXT2_EXTENT_NEXT_SIB;
if (extent.e_pblk > (1ULL << 32)) {
extent.e_pblk &= (1ULL << 32) - 1;
errcode = ext2fs_extent_replace(handle, 0, &extent);
if (errcode)
break;
}
}
/* Ok if we run off the end */
if (errcode == EXT2_ET_EXTENT_NO_NEXT)
errcode = 0;
ext2fs_extent_free(handle);
return errcode;
}
/* Zero out the high bits of inodes. */
static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
{
ext2_filsys fs = rfs->old_fs;
int length = EXT2_INODE_SIZE(fs->super);
struct ext2_inode *inode = NULL;
ext2_inode_scan scan = NULL;
errcode_t retval;
ext2_ino_t ino;
if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
return 0;
if (fs->super->s_creator_os == EXT2_OS_HURD)
return 0;
retval = ext2fs_open_inode_scan(fs, 0, &scan);
if (retval)
return retval;
retval = ext2fs_get_mem(length, &inode);
if (retval)
goto out;
do {
retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
if (retval)
goto out;
if (!ino)
break;
if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
continue;
/*
* Here's how we deal with high block number fields:
*
* - i_size_high has been been written out with i_size_lo
* since the ext2 days, so no conversion is needed.
*
* - i_blocks_hi is guarded by both the huge_file feature and
* inode flags and has always been written out with
* i_blocks_lo if the feature is set. The field is only
* ever read if both feature and inode flag are set, so
* we don't need to zero it now.
*
* - i_file_acl_high can be uninitialized, so zero it if
* it isn't already.
*/
if (inode->osd2.linux2.l_i_file_acl_high) {
inode->osd2.linux2.l_i_file_acl_high = 0;
retval = ext2fs_write_inode_full(fs, ino, inode,
length);
if (retval)
goto out;
}
retval = zero_high_bits_in_extents(fs, ino, inode);
if (retval)
goto out;
} while (ino);
out:
if (inode)
ext2fs_free_mem(&inode);
if (scan)
ext2fs_close_inode_scan(scan);
return retval;
}
/*
* Clean up the bitmaps for uninitialized bitmaps
*/
static void fix_uninit_block_bitmaps(ext2_filsys fs)
{
blk64_t blk, lblk;
dgrp_t g;
unsigned int i;
if (!ext2fs_has_group_desc_csum(fs))
return;
for (g=0; g < fs->group_desc_count; g++) {
if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
continue;
blk = ext2fs_group_first_block2(fs, g);
lblk = ext2fs_group_last_block2(fs, g);
ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
lblk - blk + 1);
ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
ext2fs_mark_block_bitmap2(fs->block_map,
ext2fs_block_bitmap_loc(fs, g));
ext2fs_mark_block_bitmap2(fs->block_map,
ext2fs_inode_bitmap_loc(fs, g));
for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
i < fs->inode_blocks_per_group;
i++, blk++)
ext2fs_mark_block_bitmap2(fs->block_map, blk);
}
}
/* --------------------------------------------------------------------
*
* Resize processing, phase 1.
*
* In this phase we adjust the in-memory superblock information, and
* initialize any new parts of the inode table. The new parts of the
* inode table are created in virgin disk space, so we can abort here
* without any side effects.
* --------------------------------------------------------------------
*/
/*
* If the group descriptor's bitmap and inode table blocks are valid,
* release them in the new filesystem data structure, and mark them as
* reserved so the old inode table blocks don't get overwritten.
*/
static errcode_t free_gdp_blocks(ext2_filsys fs,
ext2fs_block_bitmap reserve_blocks,
ext2_filsys old_fs,
dgrp_t group)
{
blk64_t blk;
unsigned int j;
dgrp_t i;
ext2fs_block_bitmap bg_map = NULL;
errcode_t retval = 0;
dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
/* If bigalloc, don't free metadata living in the same cluster */
if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
if (retval)
goto out;
retval = mark_table_blocks(fs, bg_map);
if (retval)
goto out;
}
for (i = group; i < group + count; i++) {
blk = ext2fs_block_bitmap_loc(old_fs, i);
if (blk &&
(blk < ext2fs_blocks_count(fs->super)) &&
!(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
ext2fs_block_alloc_stats2(fs, blk, -1);
ext2fs_mark_block_bitmap2(reserve_blocks, blk);
}
blk = ext2fs_inode_bitmap_loc(old_fs, i);
if (blk &&
(blk < ext2fs_blocks_count(fs->super)) &&
!(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
ext2fs_block_alloc_stats2(fs, blk, -1);
ext2fs_mark_block_bitmap2(reserve_blocks, blk);
}
blk = ext2fs_inode_table_loc(old_fs, i);
for (j = 0;
j < fs->inode_blocks_per_group; j++, blk++) {
if (blk >= ext2fs_blocks_count(fs->super) ||
(bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
continue;
ext2fs_block_alloc_stats2(fs, blk, -1);
ext2fs_mark_block_bitmap2(reserve_blocks, blk);
}
}
out:
if (bg_map)
ext2fs_free_block_bitmap(bg_map);
return retval;
}
/*
* This routine is shared by the online and offline resize routines.
* All of the information which is adjusted in memory is done here.
*/
errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
{
errcode_t retval;
blk64_t overhead = 0;
blk64_t rem;
blk64_t blk, group_block;
blk64_t real_end;
blk64_t old_numblocks, numblocks, adjblocks;
unsigned long i, j, old_desc_blocks;
unsigned int meta_bg, meta_bg_size;
int has_super, csum_flag, has_bg;
unsigned long long new_inodes; /* u64 to check for overflow */
double percent;
ext2fs_blocks_count_set(fs->super, new_size);
fs->super->s_overhead_clusters = 0;
retry:
fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
fs->super->s_first_data_block,
EXT2_BLOCKS_PER_GROUP(fs->super));
if (fs->group_desc_count == 0)
return EXT2_ET_TOOSMALL;
fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
EXT2_DESC_PER_BLOCK(fs->super));
/*
* Overhead is the number of bookkeeping blocks per group. It
* includes the superblock backup, the group descriptor
* backups, the inode bitmap, the block bitmap, and the inode
* table.
*/
overhead = (int) (2 + fs->inode_blocks_per_group);
has_bg = 0;
if (ext2fs_has_feature_sparse_super2(fs->super)) {
/*
* We have to do this manually since
* super->s_backup_bgs hasn't been set up yet.
*/
if (fs->group_desc_count == 2)
has_bg = fs->super->s_backup_bgs[0] != 0;
else
has_bg = fs->super->s_backup_bgs[1] != 0;
} else
has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
if (has_bg)
overhead += 1 + fs->desc_blocks +
fs->super->s_reserved_gdt_blocks;
/*
* See if the last group is big enough to support the
* necessary data structures. If not, we need to get rid of
* it.
*/
rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
fs->super->s_blocks_per_group;
if ((fs->group_desc_count == 1) && rem && (rem < overhead))
return EXT2_ET_TOOSMALL;
if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
ext2fs_blocks_count_set(fs->super,
ext2fs_blocks_count(fs->super) - rem);
goto retry;
}
/*
* Adjust the number of inodes
*/
new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
if (new_inodes > ~0U) {
new_inodes = (unsigned long long) fs->super->s_inodes_per_group * (fs->group_desc_count - 1);
if (new_inodes <= ~0U) {
unsigned long long new_blocks =
((unsigned long long) fs->super->s_blocks_per_group *
(fs->group_desc_count - 1)) + fs->super->s_first_data_block;
ext2fs_blocks_count_set(fs->super, new_blocks);
goto retry;
}
fprintf(stderr, _("inodes (%llu) must be less than %u\n"),
(unsigned long long) new_inodes, ~0U);
return EXT2_ET_TOO_MANY_INODES;
}
fs->super->s_inodes_count = fs->super->s_inodes_per_group *
fs->group_desc_count;
/*
* Adjust the number of free blocks
*/
blk = ext2fs_blocks_count(old_fs->super);
if (blk > ext2fs_blocks_count(fs->super))
ext2fs_free_blocks_count_set(fs->super,
ext2fs_free_blocks_count(fs->super) -
(blk - ext2fs_blocks_count(fs->super)));
else
ext2fs_free_blocks_count_set(fs->super,
ext2fs_free_blocks_count(fs->super) +
(ext2fs_blocks_count(fs->super) - blk));
/*
* Adjust the number of reserved blocks
*/
percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
ext2fs_blocks_count(old_fs->super);
ext2fs_r_blocks_count_set(fs->super,
(percent * ext2fs_blocks_count(fs->super) /
100.0));
/*
* Adjust the bitmaps for size
*/
retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
fs->super->s_inodes_count,
fs->inode_map);
if (retval) goto errout;
real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
fs->super->s_first_data_block;
retval = ext2fs_resize_block_bitmap2(new_size - 1,
real_end, fs->block_map);
if (retval) goto errout;
/*
* If we are growing the file system, also grow the size of
* the reserve_blocks bitmap
*/
if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
retval = ext2fs_resize_block_bitmap2(new_size - 1,
real_end, reserve_blocks);
if (retval) goto errout;
}
/*
* Reallocate the group descriptors as necessary.
*/
if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super) &&
old_fs->desc_blocks != fs->desc_blocks) {
retval = ext2fs_resize_mem(old_fs->desc_blocks *
fs->blocksize,
fs->desc_blocks * fs->blocksize,
&fs->group_desc);
if (retval)
goto errout;
if (fs->desc_blocks > old_fs->desc_blocks)
memset((char *) fs->group_desc +
(old_fs->desc_blocks * fs->blocksize), 0,
(fs->desc_blocks - old_fs->desc_blocks) *
fs->blocksize);
}
/*
* If the resize_inode feature is set, and we are changing the
* number of descriptor blocks, then adjust
* s_reserved_gdt_blocks if possible to avoid needing to move
* the inode table either now or in the future.
*
* Note: If we're converting to 64bit mode, we did this earlier.
*/
if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super))
adjust_reserved_gdt_blocks(old_fs, fs);
if (ext2fs_has_feature_meta_bg(fs->super) &&
(fs->super->s_first_meta_bg > fs->desc_blocks)) {
ext2fs_clear_feature_meta_bg(fs->super);
fs->super->s_first_meta_bg = 0;
}
/*
* Update the location of the backup superblocks if the
* sparse_super2 feature is enabled.
*/
if (ext2fs_has_feature_sparse_super2(fs->super)) {
dgrp_t last_bg = fs->group_desc_count - 1;
dgrp_t old_last_bg = old_fs->group_desc_count - 1;
if (last_bg > old_last_bg) {
if (old_fs->group_desc_count == 1)
fs->super->s_backup_bgs[0] = 1;
if ((old_fs->group_desc_count < 3 &&
fs->group_desc_count > 2) ||
fs->super->s_backup_bgs[1])
fs->super->s_backup_bgs[1] = last_bg;
} else if (last_bg < old_last_bg) {
if (fs->super->s_backup_bgs[0] > last_bg)
fs->super->s_backup_bgs[0] = 0;
if (fs->super->s_backup_bgs[1] > last_bg)
fs->super->s_backup_bgs[1] = 0;
if (last_bg > 1 &&
old_fs->super->s_backup_bgs[1] == old_last_bg)
fs->super->s_backup_bgs[1] = last_bg;
}
}
/*
* If we are shrinking the number of block groups, we're done
* and can exit now.
*/
if (old_fs->group_desc_count > fs->group_desc_count) {
/*
* Check the block groups that we are chopping off
* and free any blocks associated with their metadata
*/
retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
fs->group_desc_count);
goto errout;
}
/*
* Fix the count of the last (old) block group
*/
old_numblocks = (ext2fs_blocks_count(old_fs->super) -
old_fs->super->s_first_data_block) %
old_fs->super->s_blocks_per_group;
if (!old_numblocks)
old_numblocks = old_fs->super->s_blocks_per_group;
if (old_fs->group_desc_count == fs->group_desc_count) {
numblocks = (ext2fs_blocks_count(fs->super) -
fs->super->s_first_data_block) %
fs->super->s_blocks_per_group;
if (!numblocks)
numblocks = fs->super->s_blocks_per_group;
} else
numblocks = fs->super->s_blocks_per_group;
i = old_fs->group_desc_count - 1;
ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
ext2fs_group_desc_csum_set(fs, i);
/*
* If the number of block groups is staying the same, we're
* done and can exit now. (If the number block groups is
* shrinking, we had exited earlier.)
*/
if (old_fs->group_desc_count >= fs->group_desc_count) {
retval = 0;
goto errout;
}
/*
* Initialize the new block group descriptors
*/
group_block = ext2fs_group_first_block2(fs,
old_fs->group_desc_count);
csum_flag = ext2fs_has_group_desc_csum(fs);
if (getenv("RESIZE2FS_FORCE_LAZY_ITABLE_INIT") ||
(!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0))
lazy_itable_init = 1;
if (ext2fs_has_feature_meta_bg(fs->super))
old_desc_blocks = fs->super->s_first_meta_bg;
else
old_desc_blocks = fs->desc_blocks +
fs->super->s_reserved_gdt_blocks;
/*
* If we changed the number of block_group descriptor blocks,
* we need to make sure they are all marked as reserved in the
* filesystem's block allocation map.
*/
for (i = 0; i < old_fs->group_desc_count; i++)
ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
for (i = old_fs->group_desc_count;
i < fs->group_desc_count; i++) {
memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
sizeof(struct ext2_group_desc));
adjblocks = 0;
ext2fs_bg_flags_zap(fs, i);
if (csum_flag) {
ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
if (!lazy_itable_init)
ext2fs_bg_flags_set(fs, i,
EXT2_BG_INODE_ZEROED);
ext2fs_bg_itable_unused_set(fs, i,
fs->super->s_inodes_per_group);
}
numblocks = ext2fs_group_blocks_count(fs, i);
if ((i < fs->group_desc_count - 1) && csum_flag)
ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
has_super = ext2fs_bg_has_super(fs, i);
if (has_super) {
ext2fs_block_alloc_stats2(fs, group_block, +1);
adjblocks++;
}
meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
meta_bg = i / meta_bg_size;
if (!ext2fs_has_feature_meta_bg(fs->super) ||
(meta_bg < fs->super->s_first_meta_bg)) {
if (has_super) {
for (j=0; j < old_desc_blocks; j++)
ext2fs_block_alloc_stats2(fs,
group_block + 1 + j, +1);
adjblocks += old_desc_blocks;
}
} else {
if (has_super)
has_super = 1;
if (((i % meta_bg_size) == 0) ||
((i % meta_bg_size) == 1) ||
((i % meta_bg_size) == (meta_bg_size-1)))
ext2fs_block_alloc_stats2(fs,
group_block + has_super, +1);
}
adjblocks += 2 + fs->inode_blocks_per_group;
numblocks -= adjblocks;
ext2fs_free_blocks_count_set(fs->super,
ext2fs_free_blocks_count(fs->super) - adjblocks);