2 * resize2fs.c --- ext2 main routine
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
16 * Resizing a filesystem consists of the following phases:
18 * 1. Adjust superblock and write out new parts of the inode
20 * 2. Determine blocks which need to be relocated, and copy the
21 * contents of blocks from their old locations to the new ones.
22 * 3. Scan the inode table, doing the following:
23 * a. If blocks have been moved, update the block
24 * pointers in the inodes and indirect blocks to
25 * point at the new block locations.
26 * b. If parts of the inode table need to be evacuated,
27 * copy inodes from their old locations to their
29 * c. If (b) needs to be done, note which blocks contain
30 * directory information, since we will need to
31 * update the directory information.
32 * 4. Update the directory blocks with the new inode locations.
33 * 5. Move the inode tables, if necessary.
37 #include "resize2fs.h"
40 #ifdef __linux__ /* Kludge for debugging */
41 #define RESIZE2FS_DEBUG
44 static void fix_uninit_block_bitmaps(ext2_filsys fs
);
45 static errcode_t
adjust_superblock(ext2_resize_t rfs
, blk64_t new_size
);
46 static errcode_t
blocks_to_move(ext2_resize_t rfs
);
47 static errcode_t
block_mover(ext2_resize_t rfs
);
48 static errcode_t
inode_scan_and_fix(ext2_resize_t rfs
);
49 static errcode_t
inode_ref_fix(ext2_resize_t rfs
);
50 static errcode_t
move_itables(ext2_resize_t rfs
);
51 static errcode_t
fix_resize_inode(ext2_filsys fs
);
52 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
);
53 static errcode_t
fix_sb_journal_backup(ext2_filsys fs
);
54 static errcode_t
mark_table_blocks(ext2_filsys fs
,
55 ext2fs_block_bitmap bmap
);
56 static errcode_t
clear_sparse_super2_last_group(ext2_resize_t rfs
);
57 static errcode_t
reserve_sparse_super2_last_group(ext2_resize_t rfs
,
58 ext2fs_block_bitmap meta_bmap
);
61 * Some helper CPP macros
63 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
64 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
66 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
67 ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
68 (fs)->inode_blocks_per_group)))
70 /* Some bigalloc helper macros which are more succint... */
71 #define B2C(x) EXT2FS_B2C(fs, (x))
72 #define C2B(x) EXT2FS_C2B(fs, (x))
73 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
74 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
75 #define LT_CLSTR(x, y) (B2C(x) < B2C(y))
76 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
77 #define GT_CLSTR(x, y) (B2C(x) > B2C(y))
79 static int lazy_itable_init
;
82 * This is the top-level routine which does the dirty deed....
84 errcode_t
resize_fs(ext2_filsys fs
, blk64_t
*new_size
, int flags
,
85 errcode_t (*progress
)(ext2_resize_t rfs
, int pass
,
87 unsigned long max_val
))
91 struct resource_track rtrack
, overall_track
;
94 * Create the data structure
96 retval
= ext2fs_get_mem(sizeof(struct ext2_resize_struct
), &rfs
);
100 memset(rfs
, 0, sizeof(struct ext2_resize_struct
));
105 rfs
->progress
= progress
;
107 init_resource_track(&overall_track
, "overall resize2fs", fs
->io
);
108 init_resource_track(&rtrack
, "read_bitmaps", fs
->io
);
109 retval
= ext2fs_read_bitmaps(fs
);
112 print_resource_track(rfs
, &rtrack
, fs
->io
);
114 fs
->super
->s_state
|= EXT2_ERROR_FS
;
115 ext2fs_mark_super_dirty(fs
);
118 init_resource_track(&rtrack
, "fix_uninit_block_bitmaps 1", fs
->io
);
119 fix_uninit_block_bitmaps(fs
);
120 print_resource_track(rfs
, &rtrack
, fs
->io
);
121 retval
= ext2fs_dup_handle(fs
, &rfs
->new_fs
);
125 init_resource_track(&rtrack
, "adjust_superblock", fs
->io
);
126 retval
= adjust_superblock(rfs
, *new_size
);
129 print_resource_track(rfs
, &rtrack
, fs
->io
);
132 init_resource_track(&rtrack
, "fix_uninit_block_bitmaps 2", fs
->io
);
133 fix_uninit_block_bitmaps(rfs
->new_fs
);
134 print_resource_track(rfs
, &rtrack
, fs
->io
);
135 /* Clear the block bitmap uninit flag for the last block group */
136 ext2fs_bg_flags_clear(rfs
->new_fs
, rfs
->new_fs
->group_desc_count
- 1,
137 EXT2_BG_BLOCK_UNINIT
);
139 *new_size
= ext2fs_blocks_count(rfs
->new_fs
->super
);
141 init_resource_track(&rtrack
, "blocks_to_move", fs
->io
);
142 retval
= blocks_to_move(rfs
);
145 print_resource_track(rfs
, &rtrack
, fs
->io
);
147 #ifdef RESIZE2FS_DEBUG
148 if (rfs
->flags
& RESIZE_DEBUG_BMOVE
)
149 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
150 ext2fs_free_blocks_count(rfs
->old_fs
->super
),
151 ext2fs_free_blocks_count(rfs
->new_fs
->super
),
155 init_resource_track(&rtrack
, "block_mover", fs
->io
);
156 retval
= block_mover(rfs
);
159 print_resource_track(rfs
, &rtrack
, fs
->io
);
161 init_resource_track(&rtrack
, "inode_scan_and_fix", fs
->io
);
162 retval
= inode_scan_and_fix(rfs
);
165 print_resource_track(rfs
, &rtrack
, fs
->io
);
167 init_resource_track(&rtrack
, "inode_ref_fix", fs
->io
);
168 retval
= inode_ref_fix(rfs
);
171 print_resource_track(rfs
, &rtrack
, fs
->io
);
173 init_resource_track(&rtrack
, "move_itables", fs
->io
);
174 retval
= move_itables(rfs
);
177 print_resource_track(rfs
, &rtrack
, fs
->io
);
179 init_resource_track(&rtrack
, "calculate_summary_stats", fs
->io
);
180 retval
= ext2fs_calculate_summary_stats(rfs
->new_fs
);
183 print_resource_track(rfs
, &rtrack
, fs
->io
);
185 init_resource_track(&rtrack
, "fix_resize_inode", fs
->io
);
186 retval
= fix_resize_inode(rfs
->new_fs
);
189 print_resource_track(rfs
, &rtrack
, fs
->io
);
191 init_resource_track(&rtrack
, "fix_sb_journal_backup", fs
->io
);
192 retval
= fix_sb_journal_backup(rfs
->new_fs
);
195 print_resource_track(rfs
, &rtrack
, fs
->io
);
197 retval
= clear_sparse_super2_last_group(rfs
);
201 rfs
->new_fs
->super
->s_state
&= ~EXT2_ERROR_FS
;
202 rfs
->new_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
204 print_resource_track(rfs
, &overall_track
, fs
->io
);
205 retval
= ext2fs_close(rfs
->new_fs
);
211 ext2fs_free(rfs
->old_fs
);
214 ext2fs_free_mem(&rfs
->itable_buf
);
215 if (rfs
->reserve_blocks
)
216 ext2fs_free_block_bitmap(rfs
->reserve_blocks
);
217 if (rfs
->move_blocks
)
218 ext2fs_free_block_bitmap(rfs
->move_blocks
);
219 ext2fs_free_mem(&rfs
);
225 ext2fs_free(rfs
->new_fs
);
229 ext2fs_free_mem(&rfs
->itable_buf
);
230 ext2fs_free_mem(&rfs
);
235 * Clean up the bitmaps for unitialized bitmaps
237 static void fix_uninit_block_bitmaps(ext2_filsys fs
)
243 if (!ext2fs_has_group_desc_csum(fs
))
246 for (g
=0; g
< fs
->group_desc_count
; g
++) {
247 if (!(ext2fs_bg_flags_test(fs
, g
, EXT2_BG_BLOCK_UNINIT
)))
250 blk
= ext2fs_group_first_block2(fs
, g
);
251 lblk
= ext2fs_group_last_block2(fs
, g
);
252 ext2fs_unmark_block_bitmap_range2(fs
->block_map
, blk
,
255 ext2fs_reserve_super_and_bgd(fs
, g
, fs
->block_map
);
256 ext2fs_mark_block_bitmap2(fs
->block_map
,
257 ext2fs_block_bitmap_loc(fs
, g
));
258 ext2fs_mark_block_bitmap2(fs
->block_map
,
259 ext2fs_inode_bitmap_loc(fs
, g
));
260 for (i
= 0, blk
= ext2fs_inode_table_loc(fs
, g
);
261 i
< (unsigned int) fs
->inode_blocks_per_group
;
263 ext2fs_mark_block_bitmap2(fs
->block_map
, blk
);
267 /* --------------------------------------------------------------------
269 * Resize processing, phase 1.
271 * In this phase we adjust the in-memory superblock information, and
272 * initialize any new parts of the inode table. The new parts of the
273 * inode table are created in virgin disk space, so we can abort here
274 * without any side effects.
275 * --------------------------------------------------------------------
279 * If the group descriptor's bitmap and inode table blocks are valid,
280 * release them in the new filesystem data structure, and mark them as
281 * reserved so the old inode table blocks don't get overwritten.
283 static errcode_t
free_gdp_blocks(ext2_filsys fs
,
284 ext2fs_block_bitmap reserve_blocks
,
291 ext2fs_block_bitmap bg_map
= NULL
;
292 errcode_t retval
= 0;
293 dgrp_t count
= old_fs
->group_desc_count
- fs
->group_desc_count
;
295 /* If bigalloc, don't free metadata living in the same cluster */
296 if (EXT2FS_CLUSTER_RATIO(fs
) > 1) {
297 retval
= ext2fs_allocate_block_bitmap(fs
, "bgdata", &bg_map
);
301 retval
= mark_table_blocks(fs
, bg_map
);
306 for (i
= group
; i
< group
+ count
; i
++) {
307 blk
= ext2fs_block_bitmap_loc(old_fs
, i
);
309 (blk
< ext2fs_blocks_count(fs
->super
)) &&
310 !(bg_map
&& ext2fs_test_block_bitmap2(bg_map
, blk
))) {
311 ext2fs_block_alloc_stats2(fs
, blk
, -1);
312 ext2fs_mark_block_bitmap2(reserve_blocks
, blk
);
315 blk
= ext2fs_inode_bitmap_loc(old_fs
, i
);
317 (blk
< ext2fs_blocks_count(fs
->super
)) &&
318 !(bg_map
&& ext2fs_test_block_bitmap2(bg_map
, blk
))) {
319 ext2fs_block_alloc_stats2(fs
, blk
, -1);
320 ext2fs_mark_block_bitmap2(reserve_blocks
, blk
);
323 blk
= ext2fs_inode_table_loc(old_fs
, i
);
325 j
< fs
->inode_blocks_per_group
; j
++, blk
++) {
326 if (blk
>= ext2fs_blocks_count(fs
->super
) ||
327 (bg_map
&& ext2fs_test_block_bitmap2(bg_map
, blk
)))
329 ext2fs_block_alloc_stats2(fs
, blk
, -1);
330 ext2fs_mark_block_bitmap2(reserve_blocks
, blk
);
336 ext2fs_free_block_bitmap(bg_map
);
341 * This routine is shared by the online and offline resize routines.
342 * All of the information which is adjusted in memory is done here.
344 errcode_t
adjust_fs_info(ext2_filsys fs
, ext2_filsys old_fs
,
345 ext2fs_block_bitmap reserve_blocks
, blk64_t new_size
)
348 blk64_t overhead
= 0;
350 blk64_t blk
, group_block
;
352 blk64_t old_numblocks
, numblocks
, adjblocks
;
353 unsigned long i
, j
, old_desc_blocks
;
354 unsigned int meta_bg
, meta_bg_size
;
355 int has_super
, csum_flag
;
356 unsigned long long new_inodes
; /* u64 to check for overflow */
359 ext2fs_blocks_count_set(fs
->super
, new_size
);
362 fs
->group_desc_count
= ext2fs_div64_ceil(ext2fs_blocks_count(fs
->super
) -
363 fs
->super
->s_first_data_block
,
364 EXT2_BLOCKS_PER_GROUP(fs
->super
));
365 if (fs
->group_desc_count
== 0)
366 return EXT2_ET_TOOSMALL
;
367 fs
->desc_blocks
= ext2fs_div_ceil(fs
->group_desc_count
,
368 EXT2_DESC_PER_BLOCK(fs
->super
));
371 * Overhead is the number of bookkeeping blocks per group. It
372 * includes the superblock backup, the group descriptor
373 * backups, the inode bitmap, the block bitmap, and the inode
376 overhead
= (int) (2 + fs
->inode_blocks_per_group
);
378 if (ext2fs_bg_has_super(fs
, fs
->group_desc_count
- 1))
379 overhead
+= 1 + fs
->desc_blocks
+
380 fs
->super
->s_reserved_gdt_blocks
;
383 * See if the last group is big enough to support the
384 * necessary data structures. If not, we need to get rid of
387 rem
= (ext2fs_blocks_count(fs
->super
) - fs
->super
->s_first_data_block
) %
388 fs
->super
->s_blocks_per_group
;
389 if ((fs
->group_desc_count
== 1) && rem
&& (rem
< overhead
))
390 return EXT2_ET_TOOSMALL
;
391 if ((fs
->group_desc_count
> 1) && rem
&& (rem
< overhead
+50)) {
392 ext2fs_blocks_count_set(fs
->super
,
393 ext2fs_blocks_count(fs
->super
) - rem
);
397 * Adjust the number of inodes
399 new_inodes
=(unsigned long long) fs
->super
->s_inodes_per_group
* fs
->group_desc_count
;
400 if (new_inodes
> ~0U) {
401 fprintf(stderr
, _("inodes (%llu) must be less than %u"),
403 return EXT2_ET_TOO_MANY_INODES
;
405 fs
->super
->s_inodes_count
= fs
->super
->s_inodes_per_group
*
406 fs
->group_desc_count
;
409 * Adjust the number of free blocks
411 blk
= ext2fs_blocks_count(old_fs
->super
);
412 if (blk
> ext2fs_blocks_count(fs
->super
))
413 ext2fs_free_blocks_count_set(fs
->super
,
414 ext2fs_free_blocks_count(fs
->super
) -
415 (blk
- ext2fs_blocks_count(fs
->super
)));
417 ext2fs_free_blocks_count_set(fs
->super
,
418 ext2fs_free_blocks_count(fs
->super
) +
419 (ext2fs_blocks_count(fs
->super
) - blk
));
422 * Adjust the number of reserved blocks
424 percent
= (ext2fs_r_blocks_count(old_fs
->super
) * 100.0) /
425 ext2fs_blocks_count(old_fs
->super
);
426 ext2fs_r_blocks_count_set(fs
->super
,
427 (percent
* ext2fs_blocks_count(fs
->super
) /
431 * Adjust the bitmaps for size
433 retval
= ext2fs_resize_inode_bitmap2(fs
->super
->s_inodes_count
,
434 fs
->super
->s_inodes_count
,
436 if (retval
) goto errout
;
438 real_end
= (((blk64_t
) EXT2_BLOCKS_PER_GROUP(fs
->super
) *
439 fs
->group_desc_count
)) - 1 +
440 fs
->super
->s_first_data_block
;
441 retval
= ext2fs_resize_block_bitmap2(new_size
- 1,
442 real_end
, fs
->block_map
);
443 if (retval
) goto errout
;
446 * If we are growing the file system, also grow the size of
447 * the reserve_blocks bitmap
449 if (reserve_blocks
&& new_size
> ext2fs_blocks_count(old_fs
->super
)) {
450 retval
= ext2fs_resize_block_bitmap2(new_size
- 1,
451 real_end
, reserve_blocks
);
452 if (retval
) goto errout
;
456 * Reallocate the group descriptors as necessary.
458 if (old_fs
->desc_blocks
!= fs
->desc_blocks
) {
459 retval
= ext2fs_resize_mem(old_fs
->desc_blocks
*
461 fs
->desc_blocks
* fs
->blocksize
,
465 if (fs
->desc_blocks
> old_fs
->desc_blocks
)
466 memset((char *) fs
->group_desc
+
467 (old_fs
->desc_blocks
* fs
->blocksize
), 0,
468 (fs
->desc_blocks
- old_fs
->desc_blocks
) *
473 * If the resize_inode feature is set, and we are changing the
474 * number of descriptor blocks, then adjust
475 * s_reserved_gdt_blocks if possible to avoid needing to move
476 * the inode table either now or in the future.
478 if ((fs
->super
->s_feature_compat
&
479 EXT2_FEATURE_COMPAT_RESIZE_INODE
) &&
480 (old_fs
->desc_blocks
!= fs
->desc_blocks
)) {
483 new = ((int) fs
->super
->s_reserved_gdt_blocks
) +
484 (old_fs
->desc_blocks
- fs
->desc_blocks
);
487 if (new > (int) fs
->blocksize
/4)
488 new = fs
->blocksize
/4;
489 fs
->super
->s_reserved_gdt_blocks
= new;
493 * Update the location of the backup superblocks if the
494 * sparse_super2 feature is enabled.
496 if (fs
->super
->s_feature_compat
& EXT4_FEATURE_COMPAT_SPARSE_SUPER2
) {
497 dgrp_t last_bg
= fs
->group_desc_count
- 1;
498 dgrp_t old_last_bg
= old_fs
->group_desc_count
- 1;
500 if (last_bg
> old_last_bg
) {
501 if (old_fs
->group_desc_count
== 1)
502 fs
->super
->s_backup_bgs
[0] = 1;
503 if (old_fs
->group_desc_count
== 1 &&
504 fs
->super
->s_backup_bgs
[0])
505 fs
->super
->s_backup_bgs
[0] = last_bg
;
506 else if (fs
->super
->s_backup_bgs
[1])
507 fs
->super
->s_backup_bgs
[1] = last_bg
;
508 } else if (last_bg
< old_last_bg
) {
509 if (fs
->super
->s_backup_bgs
[0] > last_bg
)
510 fs
->super
->s_backup_bgs
[0] = 0;
511 if (fs
->super
->s_backup_bgs
[1] > last_bg
)
512 fs
->super
->s_backup_bgs
[1] = 0;
514 old_fs
->super
->s_backup_bgs
[1] == old_last_bg
)
515 fs
->super
->s_backup_bgs
[1] = last_bg
;
520 * If we are shrinking the number of block groups, we're done
523 if (old_fs
->group_desc_count
> fs
->group_desc_count
) {
525 * Check the block groups that we are chopping off
526 * and free any blocks associated with their metadata
528 retval
= free_gdp_blocks(fs
, reserve_blocks
, old_fs
,
529 fs
->group_desc_count
);
534 * Fix the count of the last (old) block group
536 old_numblocks
= (ext2fs_blocks_count(old_fs
->super
) -
537 old_fs
->super
->s_first_data_block
) %
538 old_fs
->super
->s_blocks_per_group
;
540 old_numblocks
= old_fs
->super
->s_blocks_per_group
;
541 if (old_fs
->group_desc_count
== fs
->group_desc_count
) {
542 numblocks
= (ext2fs_blocks_count(fs
->super
) -
543 fs
->super
->s_first_data_block
) %
544 fs
->super
->s_blocks_per_group
;
546 numblocks
= fs
->super
->s_blocks_per_group
;
548 numblocks
= fs
->super
->s_blocks_per_group
;
549 i
= old_fs
->group_desc_count
- 1;
550 ext2fs_bg_free_blocks_count_set(fs
, i
, ext2fs_bg_free_blocks_count(fs
, i
) + (numblocks
- old_numblocks
));
551 ext2fs_group_desc_csum_set(fs
, i
);
554 * If the number of block groups is staying the same, we're
555 * done and can exit now. (If the number block groups is
556 * shrinking, we had exited earlier.)
558 if (old_fs
->group_desc_count
>= fs
->group_desc_count
) {
564 * Initialize the new block group descriptors
566 group_block
= ext2fs_group_first_block2(fs
,
567 old_fs
->group_desc_count
);
568 csum_flag
= ext2fs_has_group_desc_csum(fs
);
569 if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK
) == 0)
570 lazy_itable_init
= 1;
571 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
572 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
574 old_desc_blocks
= fs
->desc_blocks
+
575 fs
->super
->s_reserved_gdt_blocks
;
578 * If we changed the number of block_group descriptor blocks,
579 * we need to make sure they are all marked as reserved in the
580 * file systems's block allocation map.
582 for (i
= 0; i
< old_fs
->group_desc_count
; i
++)
583 ext2fs_reserve_super_and_bgd(fs
, i
, fs
->block_map
);
585 for (i
= old_fs
->group_desc_count
;
586 i
< fs
->group_desc_count
; i
++) {
587 memset(ext2fs_group_desc(fs
, fs
->group_desc
, i
), 0,
588 sizeof(struct ext2_group_desc
));
591 ext2fs_bg_flags_zap(fs
, i
);
593 ext2fs_bg_flags_set(fs
, i
, EXT2_BG_INODE_UNINIT
);
594 if (!lazy_itable_init
)
595 ext2fs_bg_flags_set(fs
, i
,
596 EXT2_BG_INODE_ZEROED
);
597 ext2fs_bg_itable_unused_set(fs
, i
,
598 fs
->super
->s_inodes_per_group
);
601 numblocks
= ext2fs_group_blocks_count(fs
, i
);
602 if ((i
< fs
->group_desc_count
- 1) && csum_flag
)
603 ext2fs_bg_flags_set(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
605 has_super
= ext2fs_bg_has_super(fs
, i
);
607 ext2fs_block_alloc_stats2(fs
, group_block
, +1);
610 meta_bg_size
= EXT2_DESC_PER_BLOCK(fs
->super
);
611 meta_bg
= i
/ meta_bg_size
;
612 if (!(fs
->super
->s_feature_incompat
&
613 EXT2_FEATURE_INCOMPAT_META_BG
) ||
614 (meta_bg
< fs
->super
->s_first_meta_bg
)) {
616 for (j
=0; j
< old_desc_blocks
; j
++)
617 ext2fs_block_alloc_stats2(fs
,
618 group_block
+ 1 + j
, +1);
619 adjblocks
+= old_desc_blocks
;
624 if (((i
% meta_bg_size
) == 0) ||
625 ((i
% meta_bg_size
) == 1) ||
626 ((i
% meta_bg_size
) == (meta_bg_size
-1)))
627 ext2fs_block_alloc_stats2(fs
,
628 group_block
+ has_super
, +1);
631 adjblocks
+= 2 + fs
->inode_blocks_per_group
;
633 numblocks
-= adjblocks
;
634 ext2fs_free_blocks_count_set(fs
->super
,
635 ext2fs_free_blocks_count(fs
->super
) - adjblocks
);
636 fs
->super
->s_free_inodes_count
+=
637 fs
->super
->s_inodes_per_group
;
638 ext2fs_bg_free_blocks_count_set(fs
, i
, numblocks
);
639 ext2fs_bg_free_inodes_count_set(fs
, i
,
640 fs
->super
->s_inodes_per_group
);
641 ext2fs_bg_used_dirs_count_set(fs
, i
, 0);
642 ext2fs_group_desc_csum_set(fs
, i
);
644 retval
= ext2fs_allocate_group_table(fs
, i
, 0);
645 if (retval
) goto errout
;
647 group_block
+= fs
->super
->s_blocks_per_group
;
652 * Mark all of the metadata blocks as reserved so they won't
653 * get allocated by the call to ext2fs_allocate_group_table()
654 * in blocks_to_move(), where we allocate new blocks to
655 * replace those allocation bitmap and inode table blocks
656 * which have to get relocated to make space for an increased
657 * number of the block group descriptors.
660 mark_table_blocks(fs
, reserve_blocks
);
667 * This routine adjusts the superblock and other data structures, both
668 * in disk as well as in memory...
670 static errcode_t
adjust_superblock(ext2_resize_t rfs
, blk64_t new_size
)
672 ext2_filsys fs
= rfs
->new_fs
;
677 unsigned long max_group
;
679 ext2fs_mark_super_dirty(fs
);
680 ext2fs_mark_bb_dirty(fs
);
681 ext2fs_mark_ib_dirty(fs
);
683 retval
= ext2fs_allocate_block_bitmap(fs
, _("reserved blocks"),
684 &rfs
->reserve_blocks
);
688 retval
= adjust_fs_info(fs
, rfs
->old_fs
, rfs
->reserve_blocks
, new_size
);
693 * Check to make sure there are enough inodes
695 if ((rfs
->old_fs
->super
->s_inodes_count
-
696 rfs
->old_fs
->super
->s_free_inodes_count
) >
697 rfs
->new_fs
->super
->s_inodes_count
) {
703 * If we are shrinking the number block groups, we're done and
706 if (rfs
->old_fs
->group_desc_count
> fs
->group_desc_count
) {
712 * If the number of block groups is staying the same, we're
713 * done and can exit now. (If the number block groups is
714 * shrinking, we had exited earlier.)
716 if (rfs
->old_fs
->group_desc_count
>= fs
->group_desc_count
) {
722 * If we are using uninit_bg (aka GDT_CSUM) and the kernel
723 * supports lazy inode initialization, we can skip
724 * initializing the inode table.
726 if (lazy_itable_init
&& ext2fs_has_group_desc_csum(fs
)) {
732 * Initialize the inode table
734 retval
= ext2fs_get_array(fs
->blocksize
, fs
->inode_blocks_per_group
,
739 memset(rfs
->itable_buf
, 0, fs
->blocksize
* fs
->inode_blocks_per_group
);
740 group_block
= ext2fs_group_first_block2(fs
,
741 rfs
->old_fs
->group_desc_count
);
742 adj
= rfs
->old_fs
->group_desc_count
;
743 max_group
= fs
->group_desc_count
- adj
;
745 retval
= rfs
->progress(rfs
, E2_RSZ_EXTEND_ITABLE_PASS
,
750 for (i
= rfs
->old_fs
->group_desc_count
;
751 i
< fs
->group_desc_count
; i
++) {
753 * Write out the new inode table
755 retval
= io_channel_write_blk64(fs
->io
,
756 ext2fs_inode_table_loc(fs
, i
),
757 fs
->inode_blocks_per_group
,
759 if (retval
) goto errout
;
761 io_channel_flush(fs
->io
);
763 retval
= rfs
->progress(rfs
, E2_RSZ_EXTEND_ITABLE_PASS
,
764 i
- adj
+ 1, max_group
);
768 group_block
+= fs
->super
->s_blocks_per_group
;
770 io_channel_flush(fs
->io
);
777 /* --------------------------------------------------------------------
779 * Resize processing, phase 2.
781 * In this phase we adjust determine which blocks need to be moved, in
782 * blocks_to_move(). We then copy the blocks to their ultimate new
783 * destinations using block_mover(). Since we are copying blocks to
784 * their new locations, again during this pass we can abort without
786 * --------------------------------------------------------------------
790 * This helper function creates a block bitmap with all of the
791 * filesystem meta-data blocks.
793 static errcode_t
mark_table_blocks(ext2_filsys fs
,
794 ext2fs_block_bitmap bmap
)
799 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
800 ext2fs_reserve_super_and_bgd(fs
, i
, bmap
);
803 * Mark the blocks used for the inode table
805 blk
= ext2fs_inode_table_loc(fs
, i
);
807 ext2fs_mark_block_bitmap_range2(bmap
, blk
,
808 fs
->inode_blocks_per_group
);
811 * Mark block used for the block bitmap
813 blk
= ext2fs_block_bitmap_loc(fs
, i
);
815 ext2fs_mark_block_bitmap2(bmap
, blk
);
818 * Mark block used for the inode bitmap
820 blk
= ext2fs_inode_bitmap_loc(fs
, i
);
822 ext2fs_mark_block_bitmap2(bmap
, blk
);
828 * This function checks to see if a particular block (either a
829 * superblock or a block group descriptor) overlaps with an inode or
830 * block bitmap block, or with the inode table.
832 static void mark_fs_metablock(ext2_resize_t rfs
,
833 ext2fs_block_bitmap meta_bmap
,
834 int group
, blk64_t blk
)
836 ext2_filsys fs
= rfs
->new_fs
;
838 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
839 ext2fs_block_alloc_stats2(fs
, blk
, +1);
842 * Check to see if we overlap with the inode or block bitmap,
843 * or the inode tables. If not, and the block is in use, then
844 * mark it as a block to be moved.
846 if (IS_BLOCK_BM(fs
, group
, blk
)) {
847 ext2fs_block_bitmap_loc_set(fs
, group
, 0);
848 rfs
->needed_blocks
++;
851 if (IS_INODE_BM(fs
, group
, blk
)) {
852 ext2fs_inode_bitmap_loc_set(fs
, group
, 0);
853 rfs
->needed_blocks
++;
856 if (IS_INODE_TB(fs
, group
, blk
)) {
857 ext2fs_inode_table_loc_set(fs
, group
, 0);
858 rfs
->needed_blocks
++;
861 if (fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
864 for (i
=0; i
< rfs
->old_fs
->group_desc_count
; i
++) {
865 if (IS_BLOCK_BM(fs
, i
, blk
)) {
866 ext2fs_block_bitmap_loc_set(fs
, i
, 0);
867 rfs
->needed_blocks
++;
870 if (IS_INODE_BM(fs
, i
, blk
)) {
871 ext2fs_inode_bitmap_loc_set(fs
, i
, 0);
872 rfs
->needed_blocks
++;
875 if (IS_INODE_TB(fs
, i
, blk
)) {
876 ext2fs_inode_table_loc_set(fs
, i
, 0);
877 rfs
->needed_blocks
++;
883 if (ext2fs_has_group_desc_csum(fs
) &&
884 (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
))) {
886 * If the block bitmap is uninitialized, which means
887 * nothing other than standard metadata in use.
890 } else if (ext2fs_test_block_bitmap2(rfs
->old_fs
->block_map
, blk
) &&
891 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
892 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
893 rfs
->needed_blocks
++;
899 * This routine marks and unmarks reserved blocks in the new block
900 * bitmap. It also determines which blocks need to be moved and
901 * places this information into the move_blocks bitmap.
903 static errcode_t
blocks_to_move(ext2_resize_t rfs
)
906 dgrp_t i
, max_groups
, g
;
907 blk64_t blk
, group_blk
;
908 blk64_t old_blocks
, new_blocks
, group_end
, cluster_freed
;
910 unsigned int meta_bg
, meta_bg_size
;
912 ext2_filsys fs
, old_fs
;
913 ext2fs_block_bitmap meta_bmap
, new_meta_bmap
= NULL
;
917 old_fs
= rfs
->old_fs
;
918 if (ext2fs_blocks_count(old_fs
->super
) > ext2fs_blocks_count(fs
->super
))
921 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
926 retval
= ext2fs_allocate_block_bitmap(fs
, _("meta-data blocks"),
931 retval
= mark_table_blocks(old_fs
, meta_bmap
);
938 * If we're shrinking the filesystem, we need to move any
939 * group's metadata blocks (either allocation bitmaps or the
940 * inode table) which are beyond the end of the new
943 new_size
= ext2fs_blocks_count(fs
->super
);
944 if (new_size
< ext2fs_blocks_count(old_fs
->super
)) {
945 for (g
= 0; g
< fs
->group_desc_count
; g
++) {
948 * ext2fs_allocate_group_table will re-allocate any
949 * metadata blocks whose location is set to zero.
951 if (ext2fs_block_bitmap_loc(fs
, g
) >= new_size
) {
952 ext2fs_block_bitmap_loc_set(fs
, g
, 0);
955 if (ext2fs_inode_bitmap_loc(fs
, g
) >= new_size
) {
956 ext2fs_inode_bitmap_loc_set(fs
, g
, 0);
959 if ((ext2fs_inode_table_loc(fs
, g
) +
960 fs
->inode_blocks_per_group
) > new_size
) {
961 ext2fs_inode_table_loc_set(fs
, g
, 0);
966 retval
= ext2fs_allocate_group_table(fs
, g
, 0);
974 * If we're shrinking the filesystem, we need to move all of
975 * the blocks that don't fit any more
977 for (blk
= ext2fs_blocks_count(fs
->super
);
978 blk
< ext2fs_blocks_count(old_fs
->super
); blk
++) {
979 g
= ext2fs_group_of_blk2(fs
, blk
);
980 if (ext2fs_has_group_desc_csum(fs
) &&
981 ext2fs_bg_flags_test(old_fs
, g
, EXT2_BG_BLOCK_UNINIT
)) {
983 * The block bitmap is uninitialized, so skip
984 * to the next block group.
986 blk
= ext2fs_group_first_block2(fs
, g
+1) - 1;
989 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
990 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
991 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
992 rfs
->needed_blocks
++;
994 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
997 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
) {
998 old_blocks
= old_fs
->super
->s_first_meta_bg
;
999 new_blocks
= fs
->super
->s_first_meta_bg
;
1001 old_blocks
= old_fs
->desc_blocks
+ old_fs
->super
->s_reserved_gdt_blocks
;
1002 new_blocks
= fs
->desc_blocks
+ fs
->super
->s_reserved_gdt_blocks
;
1005 retval
= reserve_sparse_super2_last_group(rfs
, meta_bmap
);
1009 if (old_blocks
== new_blocks
) {
1014 max_groups
= fs
->group_desc_count
;
1015 if (max_groups
> old_fs
->group_desc_count
)
1016 max_groups
= old_fs
->group_desc_count
;
1017 group_blk
= old_fs
->super
->s_first_data_block
;
1019 * If we're reducing the number of descriptor blocks, this
1020 * makes life easy. :-) We just have to mark some extra
1023 if (old_blocks
> new_blocks
) {
1024 if (EXT2FS_CLUSTER_RATIO(fs
) > 1) {
1025 retval
= ext2fs_allocate_block_bitmap(fs
,
1026 _("new meta blocks"),
1031 retval
= mark_table_blocks(fs
, new_meta_bmap
);
1036 for (i
= 0; i
< max_groups
; i
++) {
1037 if (!ext2fs_bg_has_super(fs
, i
)) {
1038 group_blk
+= fs
->super
->s_blocks_per_group
;
1041 group_end
= group_blk
+ 1 + old_blocks
;
1042 for (blk
= group_blk
+ 1 + new_blocks
;
1044 if (new_meta_bmap
== NULL
||
1045 !ext2fs_test_block_bitmap2(new_meta_bmap
,
1048 EXT2FS_CLUSTER_RATIO(fs
) -
1050 EXT2FS_CLUSTER_MASK(fs
));
1051 if (cluster_freed
> group_end
- blk
)
1052 cluster_freed
= group_end
- blk
;
1053 ext2fs_block_alloc_stats2(fs
, blk
, -1);
1054 blk
+= EXT2FS_CLUSTER_RATIO(fs
);
1055 rfs
->needed_blocks
-= cluster_freed
;
1058 rfs
->needed_blocks
--;
1061 group_blk
+= fs
->super
->s_blocks_per_group
;
1067 * If we're increasing the number of descriptor blocks, life
1068 * gets interesting....
1070 meta_bg_size
= EXT2_DESC_PER_BLOCK(fs
->super
);
1071 flex_bg
= fs
->super
->s_feature_incompat
&
1072 EXT4_FEATURE_INCOMPAT_FLEX_BG
;
1073 /* first reserve all of the existing fs meta blocks */
1074 for (i
= 0; i
< max_groups
; i
++) {
1075 has_super
= ext2fs_bg_has_super(fs
, i
);
1077 mark_fs_metablock(rfs
, meta_bmap
, i
, group_blk
);
1079 meta_bg
= i
/ meta_bg_size
;
1080 if (!(fs
->super
->s_feature_incompat
&
1081 EXT2_FEATURE_INCOMPAT_META_BG
) ||
1082 (meta_bg
< fs
->super
->s_first_meta_bg
)) {
1084 for (blk
= group_blk
+1;
1085 blk
< group_blk
+ 1 + new_blocks
; blk
++)
1086 mark_fs_metablock(rfs
, meta_bmap
,
1092 if (((i
% meta_bg_size
) == 0) ||
1093 ((i
% meta_bg_size
) == 1) ||
1094 ((i
% meta_bg_size
) == (meta_bg_size
-1)))
1095 mark_fs_metablock(rfs
, meta_bmap
, i
,
1096 group_blk
+ has_super
);
1100 * Reserve the existing meta blocks that we know
1101 * aren't to be moved.
1103 * For flex_bg file systems, in order to avoid
1104 * overwriting fs metadata (especially inode table
1105 * blocks) belonging to a different block group when
1106 * we are relocating the inode tables, we need to
1107 * reserve all existing fs metadata blocks.
1109 if (ext2fs_block_bitmap_loc(fs
, i
))
1110 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1111 ext2fs_block_bitmap_loc(fs
, i
));
1112 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1113 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1114 ext2fs_block_bitmap_loc(old_fs
, i
));
1116 if (ext2fs_inode_bitmap_loc(fs
, i
))
1117 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1118 ext2fs_inode_bitmap_loc(fs
, i
));
1119 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1120 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
,
1121 ext2fs_inode_bitmap_loc(old_fs
, i
));
1123 if (ext2fs_inode_table_loc(fs
, i
))
1124 ext2fs_mark_block_bitmap_range2(rfs
->reserve_blocks
,
1125 ext2fs_inode_table_loc(fs
, i
),
1126 fs
->inode_blocks_per_group
);
1127 else if (flex_bg
&& i
< old_fs
->group_desc_count
)
1128 ext2fs_mark_block_bitmap_range2(rfs
->reserve_blocks
,
1129 ext2fs_inode_table_loc(old_fs
, i
),
1130 old_fs
->inode_blocks_per_group
);
1132 group_blk
+= rfs
->new_fs
->super
->s_blocks_per_group
;
1135 /* Allocate the missing data structures */
1136 for (i
= 0; i
< max_groups
; i
++) {
1137 if (ext2fs_inode_table_loc(fs
, i
) &&
1138 ext2fs_inode_bitmap_loc(fs
, i
) &&
1139 ext2fs_block_bitmap_loc(fs
, i
))
1142 retval
= ext2fs_allocate_group_table(fs
, i
,
1143 rfs
->reserve_blocks
);
1148 * For those structures that have changed, we need to
1151 if (ext2fs_block_bitmap_loc(old_fs
, i
) !=
1152 (blk
= ext2fs_block_bitmap_loc(fs
, i
))) {
1153 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1154 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1155 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1156 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1159 if (ext2fs_inode_bitmap_loc(old_fs
, i
) !=
1160 (blk
= ext2fs_inode_bitmap_loc(fs
, i
))) {
1161 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1162 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1163 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1164 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1169 * The inode table, if we need to relocate it, is
1170 * handled specially. We have to reserve the blocks
1171 * for both the old and the new inode table, since we
1172 * can't have the inode table be destroyed during the
1173 * block relocation phase.
1175 if (ext2fs_inode_table_loc(fs
, i
) == ext2fs_inode_table_loc(old_fs
, i
))
1176 continue; /* inode table not moved */
1178 rfs
->needed_blocks
+= fs
->inode_blocks_per_group
;
1181 * Mark the new inode table as in use in the new block
1182 * allocation bitmap, and move any blocks that might
1185 for (blk
= ext2fs_inode_table_loc(fs
, i
), j
=0;
1186 j
< fs
->inode_blocks_per_group
; j
++, blk
++) {
1187 ext2fs_block_alloc_stats2(fs
, blk
, +1);
1188 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
1189 !ext2fs_test_block_bitmap2(meta_bmap
, blk
))
1190 ext2fs_mark_block_bitmap2(rfs
->move_blocks
,
1195 * Make sure the old inode table is reserved in the
1196 * block reservation bitmap.
1198 for (blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
), j
=0;
1199 j
< fs
->inode_blocks_per_group
; j
++, blk
++)
1200 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
1206 ext2fs_free_block_bitmap(new_meta_bmap
);
1208 ext2fs_free_block_bitmap(meta_bmap
);
1214 * This helper function tries to allocate a new block. We try to
1215 * avoid hitting the original group descriptor blocks at least at
1216 * first, since we want to make it possible to recover from a badly
1217 * aborted resize operation as much as possible.
1219 * In the future, I may further modify this routine to balance out
1220 * where we get the new blocks across the various block groups.
1221 * Ideally we would allocate blocks that corresponded with the block
1222 * group of the containing inode, and keep contiguous blocks
1223 * together. However, this very difficult to do efficiently, since we
1224 * don't have the necessary information up front.
1228 #define DESPERATION 2
1230 static void init_block_alloc(ext2_resize_t rfs
)
1232 rfs
->alloc_state
= AVOID_OLD
;
1233 rfs
->new_blk
= rfs
->new_fs
->super
->s_first_data_block
;
1235 /* HACK for testing */
1236 if (ext2fs_blocks_count(rfs
->new_fs
->super
) >
1237 ext2fs_blocks_count(rfs
->old_fs
->super
))
1238 rfs
->new_blk
= ext2fs_blocks_count(rfs
->old_fs
->super
);
1242 static blk64_t
get_new_block(ext2_resize_t rfs
)
1244 ext2_filsys fs
= rfs
->new_fs
;
1247 if (rfs
->new_blk
>= ext2fs_blocks_count(fs
->super
)) {
1248 if (rfs
->alloc_state
== DESPERATION
)
1251 #ifdef RESIZE2FS_DEBUG
1252 if (rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1253 printf("Going into desperation mode "
1254 "for block allocations\n");
1256 rfs
->alloc_state
= DESPERATION
;
1257 rfs
->new_blk
= fs
->super
->s_first_data_block
;
1260 if (ext2fs_test_block_bitmap2(fs
->block_map
, rfs
->new_blk
) ||
1261 ext2fs_test_block_bitmap2(rfs
->reserve_blocks
,
1263 ((rfs
->alloc_state
== AVOID_OLD
) &&
1264 (rfs
->new_blk
< ext2fs_blocks_count(rfs
->old_fs
->super
)) &&
1265 ext2fs_test_block_bitmap2(rfs
->old_fs
->block_map
,
1270 return rfs
->new_blk
;
1274 static errcode_t
resize2fs_get_alloc_block(ext2_filsys fs
, blk64_t goal
,
1277 ext2_resize_t rfs
= (ext2_resize_t
) fs
->priv_data
;
1280 blk
= get_new_block(rfs
);
1284 #ifdef RESIZE2FS_DEBUG
1285 if (rfs
->flags
& 0xF)
1286 printf("get_alloc_block allocating %llu\n", blk
);
1289 ext2fs_mark_block_bitmap2(rfs
->old_fs
->block_map
, blk
);
1290 ext2fs_mark_block_bitmap2(rfs
->new_fs
->block_map
, blk
);
1291 *ret
= (blk64_t
) blk
;
1295 static errcode_t
block_mover(ext2_resize_t rfs
)
1297 blk64_t blk
, old_blk
, new_blk
;
1298 ext2_filsys fs
= rfs
->new_fs
;
1299 ext2_filsys old_fs
= rfs
->old_fs
;
1304 ext2_badblocks_list badblock_list
= 0;
1305 int bb_modified
= 0;
1307 fs
->get_alloc_block
= resize2fs_get_alloc_block
;
1308 old_fs
->get_alloc_block
= resize2fs_get_alloc_block
;
1310 retval
= ext2fs_read_bb_inode(old_fs
, &badblock_list
);
1314 new_blk
= fs
->super
->s_first_data_block
;
1315 if (!rfs
->itable_buf
) {
1316 retval
= ext2fs_get_array(fs
->blocksize
,
1317 fs
->inode_blocks_per_group
,
1322 retval
= ext2fs_create_extent_table(&rfs
->bmap
, 0);
1327 * The first step is to figure out where all of the blocks
1330 to_move
= moved
= 0;
1331 init_block_alloc(rfs
);
1332 for (blk
= B2C(old_fs
->super
->s_first_data_block
);
1333 blk
< ext2fs_blocks_count(old_fs
->super
);
1334 blk
+= EXT2FS_CLUSTER_RATIO(fs
)) {
1335 if (!ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
))
1337 if (!ext2fs_test_block_bitmap2(rfs
->move_blocks
, blk
))
1339 if (ext2fs_badblocks_list_test(badblock_list
, blk
)) {
1340 ext2fs_badblocks_list_del(badblock_list
, blk
);
1345 new_blk
= get_new_block(rfs
);
1350 ext2fs_block_alloc_stats2(fs
, new_blk
, +1);
1351 ext2fs_add_extent_entry(rfs
->bmap
, B2C(blk
), B2C(new_blk
));
1357 ext2fs_free_extent_table(rfs
->bmap
);
1365 * Step two is to actually move the blocks
1367 retval
= ext2fs_iterate_extent(rfs
->bmap
, 0, 0, 0);
1368 if (retval
) goto errout
;
1370 if (rfs
->progress
) {
1371 retval
= (rfs
->progress
)(rfs
, E2_RSZ_BLOCK_RELOC_PASS
,
1377 retval
= ext2fs_iterate_extent(rfs
->bmap
, &old_blk
, &new_blk
, &size
);
1378 if (retval
) goto errout
;
1381 old_blk
= C2B(old_blk
);
1382 new_blk
= C2B(new_blk
);
1384 #ifdef RESIZE2FS_DEBUG
1385 if (rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1386 printf("Moving %llu blocks %llu->%llu\n",
1387 size
, old_blk
, new_blk
);
1391 if (c
> fs
->inode_blocks_per_group
)
1392 c
= fs
->inode_blocks_per_group
;
1393 retval
= io_channel_read_blk64(fs
->io
, old_blk
, c
,
1395 if (retval
) goto errout
;
1396 retval
= io_channel_write_blk64(fs
->io
, new_blk
, c
,
1398 if (retval
) goto errout
;
1403 if (rfs
->progress
) {
1404 io_channel_flush(fs
->io
);
1405 retval
= (rfs
->progress
)(rfs
,
1406 E2_RSZ_BLOCK_RELOC_PASS
,
1412 io_channel_flush(fs
->io
);
1416 if (badblock_list
) {
1417 if (!retval
&& bb_modified
)
1418 retval
= ext2fs_update_bb_inode(old_fs
,
1420 ext2fs_badblocks_list_free(badblock_list
);
1426 /* --------------------------------------------------------------------
1428 * Resize processing, phase 3
1430 * --------------------------------------------------------------------
1435 * The extent translation table is stored in clusters so we need to
1436 * take special care when mapping a source block number to its
1437 * destination block number.
1439 static __u64
extent_translate(ext2_filsys fs
, ext2_extent extent
, __u64 old_loc
)
1441 __u64 new_block
= C2B(ext2fs_extent_translate(extent
, B2C(old_loc
)));
1444 new_block
+= old_loc
& (EXT2FS_CLUSTER_RATIO(fs
) - 1);
1448 struct process_block_struct
{
1452 struct ext2_inode
* inode
;
1459 static int process_block(ext2_filsys fs
, blk64_t
*block_nr
,
1460 e2_blkcnt_t blockcnt
,
1461 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1462 int ref_offset
EXT2FS_ATTR((unused
)), void *priv_data
)
1464 struct process_block_struct
*pb
;
1466 blk64_t block
, new_block
;
1469 pb
= (struct process_block_struct
*) priv_data
;
1471 if (pb
->rfs
->bmap
) {
1472 new_block
= extent_translate(fs
, pb
->rfs
->bmap
, block
);
1474 *block_nr
= new_block
;
1475 ret
|= BLOCK_CHANGED
;
1477 #ifdef RESIZE2FS_DEBUG
1478 if (pb
->rfs
->flags
& RESIZE_DEBUG_BMOVE
)
1479 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1480 pb
->old_ino
, blockcnt
, block
,
1488 * If we moved inodes and metadata_csum is enabled, we must force the
1489 * extent block to be rewritten with new checksum.
1491 if (EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
1492 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
1494 pb
->old_ino
!= pb
->ino
)
1495 ret
|= BLOCK_CHANGED
;
1498 retval
= ext2fs_add_dir_block2(fs
->dblist
, pb
->ino
,
1499 block
, (int) blockcnt
);
1511 static errcode_t
progress_callback(ext2_filsys fs
,
1512 ext2_inode_scan scan
EXT2FS_ATTR((unused
)),
1513 dgrp_t group
, void * priv_data
)
1515 ext2_resize_t rfs
= (ext2_resize_t
) priv_data
;
1519 * This check is to protect against old ext2 libraries. It
1520 * shouldn't be needed against new libraries.
1525 if (rfs
->progress
) {
1526 io_channel_flush(fs
->io
);
1527 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_SCAN_PASS
,
1528 group
+1, fs
->group_desc_count
);
1536 static errcode_t
migrate_ea_block(ext2_resize_t rfs
, ext2_ino_t ino
,
1537 struct ext2_inode
*inode
, int *changed
)
1543 /* No EA block or no remapping? Quit early. */
1544 if (ext2fs_file_acl_block(rfs
->old_fs
, inode
) == 0 && !rfs
->bmap
)
1546 new_block
= extent_translate(rfs
->old_fs
, rfs
->bmap
,
1547 ext2fs_file_acl_block(rfs
->old_fs
, inode
));
1551 /* Set the new ACL block */
1552 ext2fs_file_acl_block_set(rfs
->old_fs
, inode
, new_block
);
1554 /* Update checksum */
1555 if (EXT2_HAS_RO_COMPAT_FEATURE(rfs
->new_fs
->super
,
1556 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
)) {
1557 err
= ext2fs_get_mem(rfs
->old_fs
->blocksize
, &buf
);
1560 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1561 err
= ext2fs_read_ext_attr3(rfs
->old_fs
, new_block
, buf
, ino
);
1562 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1565 err
= ext2fs_write_ext_attr3(rfs
->old_fs
, new_block
, buf
, ino
);
1572 ext2fs_free_mem(&buf
);
1576 static errcode_t
inode_scan_and_fix(ext2_resize_t rfs
)
1578 struct process_block_struct pb
;
1579 ext2_ino_t ino
, new_inode
;
1580 struct ext2_inode
*inode
= NULL
;
1581 ext2_inode_scan scan
= NULL
;
1583 char *block_buf
= 0;
1584 ext2_ino_t start_to_move
;
1588 if ((rfs
->old_fs
->group_desc_count
<=
1589 rfs
->new_fs
->group_desc_count
) &&
1594 * Save the original size of the old filesystem, and
1595 * temporarily set the size to be the new size if the new size
1596 * is larger. We need to do this to avoid catching an error
1597 * by the block iterator routines
1599 orig_size
= ext2fs_blocks_count(rfs
->old_fs
->super
);
1600 if (orig_size
< ext2fs_blocks_count(rfs
->new_fs
->super
))
1601 ext2fs_blocks_count_set(rfs
->old_fs
->super
,
1602 ext2fs_blocks_count(rfs
->new_fs
->super
));
1604 retval
= ext2fs_open_inode_scan(rfs
->old_fs
, 0, &scan
);
1605 if (retval
) goto errout
;
1607 retval
= ext2fs_init_dblist(rfs
->old_fs
, 0);
1608 if (retval
) goto errout
;
1609 retval
= ext2fs_get_array(rfs
->old_fs
->blocksize
, 3, &block_buf
);
1610 if (retval
) goto errout
;
1612 start_to_move
= (rfs
->new_fs
->group_desc_count
*
1613 rfs
->new_fs
->super
->s_inodes_per_group
);
1615 if (rfs
->progress
) {
1616 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_SCAN_PASS
,
1617 0, rfs
->old_fs
->group_desc_count
);
1621 ext2fs_set_inode_callback(scan
, progress_callback
, (void *) rfs
);
1625 new_inode
= EXT2_FIRST_INODE(rfs
->new_fs
->super
);
1626 inode_size
= EXT2_INODE_SIZE(rfs
->new_fs
->super
);
1627 inode
= malloc(inode_size
);
1633 * First, copy all of the inodes that need to be moved
1634 * elsewhere in the inode table
1637 retval
= ext2fs_get_next_inode_full(scan
, &ino
, inode
, inode_size
);
1638 if (retval
) goto errout
;
1642 if (inode
->i_links_count
== 0 && ino
!= EXT2_RESIZE_INO
)
1643 continue; /* inode not in use */
1645 pb
.is_dir
= LINUX_S_ISDIR(inode
->i_mode
);
1648 /* Remap EA block */
1649 retval
= migrate_ea_block(rfs
, ino
, inode
, &pb
.changed
);
1654 if (ino
<= start_to_move
)
1655 goto remap_blocks
; /* Don't need to move inode. */
1658 * Find a new inode. Now that extents and directory blocks
1659 * are tied to the inode number through the checksum, we must
1660 * set up the new inode before we start rewriting blocks.
1662 retval
= ext2fs_new_inode(rfs
->new_fs
, 0, 0, 0, &new_inode
);
1666 ext2fs_inode_alloc_stats2(rfs
->new_fs
, new_inode
, +1,
1668 inode
->i_ctime
= time(0);
1669 retval
= ext2fs_write_inode_full(rfs
->old_fs
, new_inode
,
1675 #ifdef RESIZE2FS_DEBUG
1676 if (rfs
->flags
& RESIZE_DEBUG_INODEMAP
)
1677 printf("Inode moved %u->%u\n", ino
, new_inode
);
1680 retval
= ext2fs_create_extent_table(&rfs
->imap
, 0);
1684 ext2fs_add_extent_entry(rfs
->imap
, ino
, new_inode
);
1688 retval
= ext2fs_write_inode_full(rfs
->old_fs
,
1695 * Update inodes to point to new blocks; schedule directory
1696 * blocks for inode remapping. Need to write out dir blocks
1697 * with new inode numbers if we have metadata_csum enabled.
1699 if (ext2fs_inode_has_valid_blocks2(rfs
->old_fs
, inode
) &&
1700 (rfs
->bmap
|| pb
.is_dir
)) {
1703 pb
.has_extents
= inode
->i_flags
& EXT4_EXTENTS_FL
;
1704 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1705 retval
= ext2fs_block_iterate3(rfs
->old_fs
,
1706 new_inode
, 0, block_buf
,
1707 process_block
, &pb
);
1708 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1715 } else if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) &&
1716 (rfs
->bmap
|| pb
.is_dir
)) {
1717 /* inline data dir; update it too */
1718 retval
= ext2fs_add_dir_block2(rfs
->old_fs
->dblist
,
1724 io_channel_flush(rfs
->old_fs
->io
);
1727 ext2fs_blocks_count_set(rfs
->old_fs
->super
, orig_size
);
1729 ext2fs_free_extent_table(rfs
->bmap
);
1733 ext2fs_close_inode_scan(scan
);
1735 ext2fs_free_mem(&block_buf
);
1740 /* --------------------------------------------------------------------
1742 * Resize processing, phase 4.
1744 * --------------------------------------------------------------------
1750 unsigned int max_dirs
;
1754 static int check_and_change_inodes(ext2_ino_t dir
,
1755 int entry
EXT2FS_ATTR((unused
)),
1756 struct ext2_dir_entry
*dirent
, int offset
,
1757 int blocksize
EXT2FS_ATTR((unused
)),
1758 char *buf
EXT2FS_ATTR((unused
)),
1761 struct istruct
*is
= (struct istruct
*) priv_data
;
1762 struct ext2_inode inode
;
1763 ext2_ino_t new_inode
;
1767 if (is
->rfs
->progress
&& offset
== 0) {
1768 io_channel_flush(is
->rfs
->old_fs
->io
);
1769 is
->err
= (is
->rfs
->progress
)(is
->rfs
,
1770 E2_RSZ_INODE_REF_UPD_PASS
,
1771 ++is
->num
, is
->max_dirs
);
1773 return DIRENT_ABORT
;
1777 * If we have checksums enabled and the inode wasn't present in the
1778 * old fs, then we must rewrite all dir blocks with new checksums.
1780 if (EXT2_HAS_RO_COMPAT_FEATURE(is
->rfs
->old_fs
->super
,
1781 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
1782 !ext2fs_test_inode_bitmap2(is
->rfs
->old_fs
->inode_map
, dir
))
1783 ret
|= DIRENT_CHANGED
;
1788 new_inode
= ext2fs_extent_translate(is
->rfs
->imap
, dirent
->inode
);
1792 #ifdef RESIZE2FS_DEBUG
1793 if (is
->rfs
->flags
& RESIZE_DEBUG_INODEMAP
)
1794 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1795 dir
, ext2fs_dirent_name_len(dirent
), dirent
->name
,
1796 dirent
->inode
, new_inode
);
1799 dirent
->inode
= new_inode
;
1801 /* Update the directory mtime and ctime */
1802 retval
= ext2fs_read_inode(is
->rfs
->old_fs
, dir
, &inode
);
1804 inode
.i_mtime
= inode
.i_ctime
= time(0);
1805 is
->err
= ext2fs_write_inode(is
->rfs
->old_fs
, dir
, &inode
);
1807 return ret
| DIRENT_ABORT
;
1810 return ret
| DIRENT_CHANGED
;
1813 static errcode_t
inode_ref_fix(ext2_resize_t rfs
)
1822 * Now, we iterate over all of the directories to update the
1826 is
.max_dirs
= ext2fs_dblist_count2(rfs
->old_fs
->dblist
);
1830 if (rfs
->progress
) {
1831 retval
= (rfs
->progress
)(rfs
, E2_RSZ_INODE_REF_UPD_PASS
,
1837 rfs
->old_fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1838 retval
= ext2fs_dblist_dir_iterate(rfs
->old_fs
->dblist
,
1839 DIRENT_FLAG_INCLUDE_EMPTY
, 0,
1840 check_and_change_inodes
, &is
);
1841 rfs
->old_fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1849 if (rfs
->progress
&& (is
.num
< is
.max_dirs
))
1850 (rfs
->progress
)(rfs
, E2_RSZ_INODE_REF_UPD_PASS
,
1851 is
.max_dirs
, is
.max_dirs
);
1854 ext2fs_free_extent_table(rfs
->imap
);
1860 /* --------------------------------------------------------------------
1862 * Resize processing, phase 5.
1864 * In this phase we actually move the inode table around, and then
1865 * update the summary statistics. This is scary, since aborting here
1866 * will potentially scramble the filesystem. (We are moving the
1867 * inode tables around in place, and so the potential for lost data,
1868 * or at the very least scrambling the mapping between filenames and
1869 * inode numbers is very high in case of a power failure here.)
1870 * --------------------------------------------------------------------
1875 * A very scary routine --- this one moves the inode table around!!!
1877 * After this you have to use the rfs->new_fs file handle to read and
1880 static errcode_t
move_itables(ext2_resize_t rfs
)
1884 dgrp_t i
, max_groups
;
1885 ext2_filsys fs
= rfs
->new_fs
;
1887 blk64_t old_blk
, new_blk
, blk
, cluster_freed
;
1889 int j
, to_move
, moved
;
1890 ext2fs_block_bitmap new_bmap
= NULL
;
1892 max_groups
= fs
->group_desc_count
;
1893 if (max_groups
> rfs
->old_fs
->group_desc_count
)
1894 max_groups
= rfs
->old_fs
->group_desc_count
;
1896 size
= fs
->blocksize
* fs
->inode_blocks_per_group
;
1897 if (!rfs
->itable_buf
) {
1898 retval
= ext2fs_get_mem(size
, &rfs
->itable_buf
);
1903 if (EXT2FS_CLUSTER_RATIO(fs
) > 1) {
1904 retval
= ext2fs_allocate_block_bitmap(fs
, _("new meta blocks"),
1909 retval
= mark_table_blocks(fs
, new_bmap
);
1915 * Figure out how many inode tables we need to move
1917 to_move
= moved
= 0;
1918 for (i
=0; i
< max_groups
; i
++)
1919 if (ext2fs_inode_table_loc(rfs
->old_fs
, i
) !=
1920 ext2fs_inode_table_loc(fs
, i
))
1926 if (rfs
->progress
) {
1927 retval
= rfs
->progress(rfs
, E2_RSZ_MOVE_ITABLE_PASS
,
1933 rfs
->old_fs
->flags
|= EXT2_FLAG_MASTER_SB_ONLY
;
1935 for (i
=0; i
< max_groups
; i
++) {
1936 old_blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
);
1937 new_blk
= ext2fs_inode_table_loc(fs
, i
);
1938 diff
= new_blk
- old_blk
;
1940 #ifdef RESIZE2FS_DEBUG
1941 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
1942 printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1943 i
, old_blk
, new_blk
, diff
);
1951 retval
= io_channel_read_blk64(fs
->io
, old_blk
,
1952 fs
->inode_blocks_per_group
,
1957 * The end of the inode table segment often contains
1958 * all zeros, and we're often only moving the inode
1959 * table down a block or two. If so, we can optimize
1960 * things by not rewriting blocks that we know to be zero
1963 for (cp
= rfs
->itable_buf
+size
-1, n
=0; n
< size
; n
++, cp
--)
1966 n
= n
>> EXT2_BLOCK_SIZE_BITS(fs
->super
);
1967 #ifdef RESIZE2FS_DEBUG
1968 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
1969 printf("%d blocks of zeros...\n", n
);
1971 num
= fs
->inode_blocks_per_group
;
1975 retval
= io_channel_write_blk64(fs
->io
, new_blk
,
1976 num
, rfs
->itable_buf
);
1978 io_channel_write_blk64(fs
->io
, old_blk
,
1979 num
, rfs
->itable_buf
);
1983 retval
= io_channel_write_blk64(fs
->io
,
1984 old_blk
+ fs
->inode_blocks_per_group
,
1985 diff
, (rfs
->itable_buf
+
1986 (fs
->inode_blocks_per_group
- diff
) *
1992 for (blk
= ext2fs_inode_table_loc(rfs
->old_fs
, i
), j
=0;
1993 j
< fs
->inode_blocks_per_group
;) {
1994 if (new_bmap
== NULL
||
1995 !ext2fs_test_block_bitmap2(new_bmap
, blk
)) {
1996 ext2fs_block_alloc_stats2(fs
, blk
, -1);
1997 cluster_freed
= EXT2FS_CLUSTER_RATIO(fs
) -
1998 (blk
& EXT2FS_CLUSTER_MASK(fs
));
1999 blk
+= cluster_freed
;
2007 ext2fs_inode_table_loc_set(rfs
->old_fs
, i
, new_blk
);
2008 ext2fs_group_desc_csum_set(rfs
->old_fs
, i
);
2009 ext2fs_mark_super_dirty(rfs
->old_fs
);
2010 ext2fs_flush(rfs
->old_fs
);
2012 if (rfs
->progress
) {
2013 retval
= rfs
->progress(rfs
, E2_RSZ_MOVE_ITABLE_PASS
,
2019 mark_table_blocks(fs
, fs
->block_map
);
2021 #ifdef RESIZE2FS_DEBUG
2022 if (rfs
->flags
& RESIZE_DEBUG_ITABLEMOVE
)
2023 printf("Inode table move finished.\n");
2029 ext2fs_free_block_bitmap(new_bmap
);
2034 * This function is used when expanding a file system. It frees the
2035 * superblock and block group descriptor blocks from the block group
2036 * which is no longer the last block group.
2038 static errcode_t
clear_sparse_super2_last_group(ext2_resize_t rfs
)
2040 ext2_filsys fs
= rfs
->new_fs
;
2041 ext2_filsys old_fs
= rfs
->old_fs
;
2043 dgrp_t old_last_bg
= rfs
->old_fs
->group_desc_count
- 1;
2044 dgrp_t last_bg
= fs
->group_desc_count
- 1;
2045 blk64_t sb
, old_desc
;
2048 if (!(fs
->super
->s_feature_compat
& EXT4_FEATURE_COMPAT_SPARSE_SUPER2
))
2051 if (last_bg
<= old_last_bg
)
2054 if (fs
->super
->s_backup_bgs
[0] == old_fs
->super
->s_backup_bgs
[0] &&
2055 fs
->super
->s_backup_bgs
[1] == old_fs
->super
->s_backup_bgs
[1])
2058 if (old_fs
->super
->s_backup_bgs
[0] != old_last_bg
&&
2059 old_fs
->super
->s_backup_bgs
[1] != old_last_bg
)
2062 if (fs
->super
->s_backup_bgs
[0] == old_last_bg
||
2063 fs
->super
->s_backup_bgs
[1] == old_last_bg
)
2066 retval
= ext2fs_super_and_bgd_loc2(rfs
->old_fs
, old_last_bg
,
2067 &sb
, &old_desc
, NULL
, &num
);
2072 ext2fs_unmark_block_bitmap2(fs
->block_map
, sb
);
2074 ext2fs_unmark_block_bitmap_range2(fs
->block_map
, old_desc
, num
);
2079 * This function is used when shrinking a file system. We need to
2080 * utilize blocks from what will be the new last block group for the
2081 * backup superblock and block group descriptor blocks.
2082 * Unfortunately, those blocks may be used by other files or fs
2083 * metadata blocks. We need to mark them as being in use.
2085 static errcode_t
reserve_sparse_super2_last_group(ext2_resize_t rfs
,
2086 ext2fs_block_bitmap meta_bmap
)
2088 ext2_filsys fs
= rfs
->new_fs
;
2089 ext2_filsys old_fs
= rfs
->old_fs
;
2091 dgrp_t old_last_bg
= rfs
->old_fs
->group_desc_count
- 1;
2092 dgrp_t last_bg
= fs
->group_desc_count
- 1;
2094 blk64_t blk
, sb
, old_desc
;
2098 if (!(fs
->super
->s_feature_compat
& EXT4_FEATURE_COMPAT_SPARSE_SUPER2
))
2101 if (last_bg
>= old_last_bg
)
2104 if (fs
->super
->s_backup_bgs
[0] == old_fs
->super
->s_backup_bgs
[0] &&
2105 fs
->super
->s_backup_bgs
[1] == old_fs
->super
->s_backup_bgs
[1])
2108 if (fs
->super
->s_backup_bgs
[0] != last_bg
&&
2109 fs
->super
->s_backup_bgs
[1] != last_bg
)
2112 if (old_fs
->super
->s_backup_bgs
[0] == last_bg
||
2113 old_fs
->super
->s_backup_bgs
[1] == last_bg
)
2116 retval
= ext2fs_super_and_bgd_loc2(rfs
->new_fs
, last_bg
,
2117 &sb
, &old_desc
, NULL
, &num
);
2122 fputs(_("Should never happen! No sb in last super_sparse bg?\n"),
2126 if (old_desc
&& old_desc
!= sb
+1) {
2127 fputs(_("Should never happen! Unexpected old_desc in "
2128 "super_sparse bg?\n"),
2132 num
= (old_desc
) ? num
: 1;
2134 /* Reserve the backup blocks */
2135 ext2fs_mark_block_bitmap_range2(fs
->block_map
, sb
, num
);
2137 for (g
= 0; g
< fs
->group_desc_count
; g
++) {
2140 mb
= ext2fs_block_bitmap_loc(fs
, g
);
2141 if ((mb
>= sb
) && (mb
< sb
+ num
)) {
2142 ext2fs_block_bitmap_loc_set(fs
, g
, 0);
2145 mb
= ext2fs_inode_bitmap_loc(fs
, g
);
2146 if ((mb
>= sb
) && (mb
< sb
+ num
)) {
2147 ext2fs_inode_bitmap_loc_set(fs
, g
, 0);
2150 mb
= ext2fs_inode_table_loc(fs
, g
);
2151 if ((mb
< sb
+ num
) &&
2152 (sb
< mb
+ fs
->inode_blocks_per_group
)) {
2153 ext2fs_inode_table_loc_set(fs
, g
, 0);
2157 retval
= ext2fs_allocate_group_table(fs
, g
, 0);
2163 for (blk
= sb
, i
= 0; i
< num
; blk
++, i
++) {
2164 if (ext2fs_test_block_bitmap2(old_fs
->block_map
, blk
) &&
2165 !ext2fs_test_block_bitmap2(meta_bmap
, blk
)) {
2166 ext2fs_mark_block_bitmap2(rfs
->move_blocks
, blk
);
2167 rfs
->needed_blocks
++;
2169 ext2fs_mark_block_bitmap2(rfs
->reserve_blocks
, blk
);
2175 * Fix the resize inode
2177 static errcode_t
fix_resize_inode(ext2_filsys fs
)
2179 struct ext2_inode inode
;
2181 char *block_buf
= NULL
;
2183 if (!(fs
->super
->s_feature_compat
&
2184 EXT2_FEATURE_COMPAT_RESIZE_INODE
))
2187 retval
= ext2fs_get_mem(fs
->blocksize
, &block_buf
);
2188 if (retval
) goto errout
;
2190 retval
= ext2fs_read_inode(fs
, EXT2_RESIZE_INO
, &inode
);
2191 if (retval
) goto errout
;
2193 ext2fs_iblk_set(fs
, &inode
, 1);
2195 retval
= ext2fs_write_inode(fs
, EXT2_RESIZE_INO
, &inode
);
2196 if (retval
) goto errout
;
2198 if (!inode
.i_block
[EXT2_DIND_BLOCK
]) {
2200 * Avoid zeroing out block #0; that's rude. This
2201 * should never happen anyway since the filesystem
2202 * should be fsck'ed and we assume it is consistent.
2204 fprintf(stderr
, "%s",
2205 _("Should never happen: resize inode corrupt!\n"));
2209 memset(block_buf
, 0, fs
->blocksize
);
2211 retval
= io_channel_write_blk64(fs
->io
, inode
.i_block
[EXT2_DIND_BLOCK
],
2213 if (retval
) goto errout
;
2215 retval
= ext2fs_create_resize_inode(fs
);
2221 ext2fs_free_mem(&block_buf
);
2226 * Finally, recalculate the summary information
2228 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
2232 unsigned int group
= 0;
2233 unsigned int count
= 0;
2234 blk64_t total_blocks_free
= 0;
2235 int total_inodes_free
= 0;
2238 blk64_t super_blk
, old_desc_blk
, new_desc_blk
;
2239 int old_desc_blocks
;
2242 * First calculate the block statistics
2244 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
);
2245 ext2fs_super_and_bgd_loc2(fs
, group
, &super_blk
, &old_desc_blk
,
2247 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
2248 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2250 old_desc_blocks
= fs
->desc_blocks
+
2251 fs
->super
->s_reserved_gdt_blocks
;
2252 for (blk
= B2C(fs
->super
->s_first_data_block
);
2253 blk
< ext2fs_blocks_count(fs
->super
);
2254 blk
+= EXT2FS_CLUSTER_RATIO(fs
)) {
2256 !(EQ_CLSTR(blk
, super_blk
) ||
2257 ((old_desc_blk
&& old_desc_blocks
&&
2258 GE_CLSTR(blk
, old_desc_blk
) &&
2259 LT_CLSTR(blk
, old_desc_blk
+ old_desc_blocks
))) ||
2260 ((new_desc_blk
&& EQ_CLSTR(blk
, new_desc_blk
))) ||
2261 EQ_CLSTR(blk
, ext2fs_block_bitmap_loc(fs
, group
)) ||
2262 EQ_CLSTR(blk
, ext2fs_inode_bitmap_loc(fs
, group
)) ||
2263 ((GE_CLSTR(blk
, ext2fs_inode_table_loc(fs
, group
)) &&
2264 LT_CLSTR(blk
, ext2fs_inode_table_loc(fs
, group
)
2265 + fs
->inode_blocks_per_group
))))) ||
2266 (!ext2fs_fast_test_block_bitmap2(fs
->block_map
, blk
))) {
2268 total_blocks_free
++;
2271 if ((count
== fs
->super
->s_clusters_per_group
) ||
2272 EQ_CLSTR(blk
, ext2fs_blocks_count(fs
->super
)-1)) {
2273 ext2fs_bg_free_blocks_count_set(fs
, group
, group_free
);
2274 ext2fs_group_desc_csum_set(fs
, group
);
2276 if (group
>= fs
->group_desc_count
)
2280 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_BLOCK_UNINIT
);
2281 ext2fs_super_and_bgd_loc2(fs
, group
, &super_blk
,
2284 if (fs
->super
->s_feature_incompat
&
2285 EXT2_FEATURE_INCOMPAT_META_BG
)
2286 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2288 old_desc_blocks
= fs
->desc_blocks
+
2289 fs
->super
->s_reserved_gdt_blocks
;
2292 total_blocks_free
= C2B(total_blocks_free
);
2293 ext2fs_free_blocks_count_set(fs
->super
, total_blocks_free
);
2296 * Next, calculate the inode statistics
2302 /* Protect loop from wrap-around if s_inodes_count maxed */
2303 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
);
2304 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
2306 !ext2fs_fast_test_inode_bitmap2(fs
->inode_map
, ino
)) {
2308 total_inodes_free
++;
2311 if ((count
== fs
->super
->s_inodes_per_group
) ||
2312 (ino
== fs
->super
->s_inodes_count
)) {
2313 ext2fs_bg_free_inodes_count_set(fs
, group
, group_free
);
2314 ext2fs_group_desc_csum_set(fs
, group
);
2316 if (group
>= fs
->group_desc_count
)
2320 uninit
= ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
);
2323 fs
->super
->s_free_inodes_count
= total_inodes_free
;
2324 ext2fs_mark_super_dirty(fs
);
2329 * Journal may have been relocated; update the backup journal blocks
2330 * in the superblock.
2332 static errcode_t
fix_sb_journal_backup(ext2_filsys fs
)
2335 struct ext2_inode inode
;
2337 if (!(fs
->super
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
))
2340 /* External journal? Nothing to do. */
2341 if (fs
->super
->s_journal_dev
&& !fs
->super
->s_journal_inum
)
2344 retval
= ext2fs_read_inode(fs
, fs
->super
->s_journal_inum
, &inode
);
2347 memcpy(fs
->super
->s_jnl_blocks
, inode
.i_block
, EXT2_N_BLOCKS
*4);
2348 fs
->super
->s_jnl_blocks
[15] = inode
.i_size_high
;
2349 fs
->super
->s_jnl_blocks
[16] = inode
.i_size
;
2350 fs
->super
->s_jnl_backup_type
= EXT3_JNL_BACKUP_BLOCKS
;
2351 ext2fs_mark_super_dirty(fs
);
2355 static int calc_group_overhead(ext2_filsys fs
, blk64_t grp
,
2356 int old_desc_blocks
)
2358 blk64_t super_blk
, old_desc_blk
, new_desc_blk
;
2361 /* inode table blocks plus allocation bitmaps */
2362 overhead
= fs
->inode_blocks_per_group
+ 2;
2364 ext2fs_super_and_bgd_loc2(fs
, grp
, &super_blk
,
2365 &old_desc_blk
, &new_desc_blk
, 0);
2366 if ((grp
== 0) || super_blk
)
2369 overhead
+= old_desc_blocks
;
2370 else if (new_desc_blk
)
2377 * calcluate the minimum number of blocks the given fs can be resized to
2379 blk64_t
calculate_minimum_resize_size(ext2_filsys fs
, int flags
)
2381 ext2_ino_t inode_count
;
2382 dgrp_t groups
, flex_groups
;
2383 blk64_t blks_needed
, data_blocks
;
2384 blk64_t grp
, data_needed
, last_start
;
2385 blk64_t overhead
= 0;
2386 int old_desc_blocks
;
2387 int flexbg_size
= 1 << fs
->super
->s_log_groups_per_flex
;
2390 * first figure out how many group descriptors we need to
2391 * handle the number of inodes we have
2393 inode_count
= fs
->super
->s_inodes_count
-
2394 fs
->super
->s_free_inodes_count
;
2395 blks_needed
= ext2fs_div_ceil(inode_count
,
2396 fs
->super
->s_inodes_per_group
) *
2397 EXT2_BLOCKS_PER_GROUP(fs
->super
);
2398 groups
= ext2fs_div64_ceil(blks_needed
,
2399 EXT2_BLOCKS_PER_GROUP(fs
->super
));
2400 #ifdef RESIZE2FS_DEBUG
2401 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2402 printf("fs has %d inodes, %d groups required.\n",
2403 inode_count
, groups
);
2407 * number of old-style block group descriptor blocks
2409 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
2410 old_desc_blocks
= fs
->super
->s_first_meta_bg
;
2412 old_desc_blocks
= fs
->desc_blocks
+
2413 fs
->super
->s_reserved_gdt_blocks
;
2415 /* calculate how many blocks are needed for data */
2416 data_needed
= ext2fs_blocks_count(fs
->super
) -
2417 ext2fs_free_blocks_count(fs
->super
);
2419 for (grp
= 0; grp
< fs
->group_desc_count
; grp
++)
2420 data_needed
-= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2421 #ifdef RESIZE2FS_DEBUG
2422 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2423 printf("fs requires %llu data blocks.\n", data_needed
);
2427 * For ext4 we need to allow for up to a flex_bg worth of
2428 * inode tables of slack space so the resize operation can be
2429 * guaranteed to finish.
2431 flex_groups
= groups
;
2432 if (fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
2433 dgrp_t remainder
= groups
& (flexbg_size
- 1);
2435 flex_groups
+= flexbg_size
- remainder
;
2436 if (flex_groups
> fs
->group_desc_count
)
2437 flex_groups
= fs
->group_desc_count
;
2441 * figure out how many data blocks we have given the number of groups
2442 * we need for our inodes
2444 data_blocks
= groups
* EXT2_BLOCKS_PER_GROUP(fs
->super
);
2446 for (grp
= 0; grp
< flex_groups
; grp
++) {
2447 overhead
= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2450 * we want to keep track of how much data we can store in
2451 * the groups leading up to the last group so we can determine
2452 * how big the last group needs to be
2454 if (grp
< (groups
- 1))
2455 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
) -
2458 if (data_blocks
> overhead
)
2459 data_blocks
-= overhead
;
2463 #ifdef RESIZE2FS_DEBUG
2464 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2465 printf("With %d group(s), we have %llu blocks available.\n",
2466 groups
, data_blocks
);
2470 * if we need more group descriptors in order to accomodate our data
2471 * then we need to add them here
2473 blks_needed
= data_needed
;
2474 while (blks_needed
> data_blocks
) {
2475 blk64_t remainder
= blks_needed
- data_blocks
;
2478 /* figure out how many more groups we need for the data */
2479 extra_grps
= ext2fs_div64_ceil(remainder
,
2480 EXT2_BLOCKS_PER_GROUP(fs
->super
));
2482 data_blocks
+= extra_grps
* EXT2_BLOCKS_PER_GROUP(fs
->super
);
2484 /* ok we have to account for the last group */
2485 overhead
= calc_group_overhead(fs
, groups
-1, old_desc_blocks
);
2486 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
) - overhead
;
2489 groups
+= extra_grps
;
2490 if (!(fs
->super
->s_feature_incompat
&
2491 EXT4_FEATURE_INCOMPAT_FLEX_BG
))
2492 flex_groups
= groups
;
2493 else if (groups
> flex_groups
) {
2494 dgrp_t r
= groups
& (flexbg_size
- 1);
2496 flex_groups
= groups
+ flexbg_size
- r
;
2497 if (flex_groups
> fs
->group_desc_count
)
2498 flex_groups
= fs
->group_desc_count
;
2501 for (; grp
< flex_groups
; grp
++) {
2502 overhead
= calc_group_overhead(fs
, grp
,
2506 * again, we need to see how much data we cram into
2507 * all of the groups leading up to the last group
2509 if (grp
< groups
- 1)
2510 last_start
+= EXT2_BLOCKS_PER_GROUP(fs
->super
)
2513 data_blocks
-= overhead
;
2516 #ifdef RESIZE2FS_DEBUG
2517 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2518 printf("Added %d extra group(s), "
2519 "blks_needed %llu, data_blocks %llu, "
2520 "last_start %llu\n", extra_grps
, blks_needed
,
2521 data_blocks
, last_start
);
2525 /* now for the fun voodoo */
2527 if ((fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) &&
2528 (grp
& ~(flexbg_size
- 1)) == 0)
2529 grp
= grp
& ~(flexbg_size
- 1);
2531 for (; grp
< flex_groups
; grp
++)
2532 overhead
+= calc_group_overhead(fs
, grp
, old_desc_blocks
);
2534 #ifdef RESIZE2FS_DEBUG
2535 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2536 printf("Last group's overhead is %llu\n", overhead
);
2540 * if this is the case then the last group is going to have data in it
2541 * so we need to adjust the size of the last group accordingly
2543 if (last_start
< blks_needed
) {
2544 blk64_t remainder
= blks_needed
- last_start
;
2546 #ifdef RESIZE2FS_DEBUG
2547 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2548 printf("Need %llu data blocks in last group\n",
2552 * 50 is a magic number that mkfs/resize uses to see if its
2553 * even worth making/resizing the fs. basically you need to
2554 * have at least 50 blocks in addition to the blocks needed
2555 * for the metadata in the last group
2558 overhead
+= remainder
;
2564 overhead
+= fs
->super
->s_first_data_block
;
2565 #ifdef RESIZE2FS_DEBUG
2566 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2567 printf("Final size of last group is %lld\n", overhead
);
2570 /* Add extra slack for bigalloc file systems */
2571 if (EXT2FS_CLUSTER_RATIO(fs
) > 1)
2572 overhead
+= EXT2FS_CLUSTER_RATIO(fs
) * 2;
2575 * since our last group doesn't have to be BLOCKS_PER_GROUP
2576 * large, we only do groups-1, and then add the number of
2577 * blocks needed to handle the group descriptor metadata+data
2580 blks_needed
= (groups
-1) * EXT2_BLOCKS_PER_GROUP(fs
->super
);
2581 blks_needed
+= overhead
;
2584 * Make sure blks_needed covers the end of the inode table in
2585 * the last block group.
2587 overhead
= ext2fs_inode_table_loc(fs
, groups
-1) +
2588 fs
->inode_blocks_per_group
;
2589 if (blks_needed
< overhead
)
2590 blks_needed
= overhead
;
2592 #ifdef RESIZE2FS_DEBUG
2593 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2594 printf("Estimated blocks needed: %llu\n", blks_needed
);
2598 * If at this point we've already added up more "needed" than
2599 * the current size, just return current size as minimum.
2601 if (blks_needed
>= ext2fs_blocks_count(fs
->super
))
2602 return ext2fs_blocks_count(fs
->super
);
2604 * We need to reserve a few extra blocks if extents are
2605 * enabled, in case we need to grow the extent tree. The more
2606 * we shrink the file system, the more space we need.
2608 * The absolute worst case is every single data block is in
2609 * the part of the file system that needs to be evacuated,
2610 * with each data block needs to be in its own extent, and
2611 * with each inode needing at least one extent block.
2613 if (fs
->super
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_EXTENTS
) {
2614 blk64_t safe_margin
= (ext2fs_blocks_count(fs
->super
) -
2616 unsigned int exts_per_blk
= (fs
->blocksize
/
2617 sizeof(struct ext3_extent
)) - 1;
2618 blk64_t worst_case
= ((data_needed
+ exts_per_blk
- 1) /
2621 if (worst_case
< inode_count
)
2622 worst_case
= inode_count
;
2624 if (safe_margin
> worst_case
)
2625 safe_margin
= worst_case
;
2627 #ifdef RESIZE2FS_DEBUG
2628 if (flags
& RESIZE_DEBUG_MIN_CALC
)
2629 printf("Extents safety margin: %llu\n", safe_margin
);
2631 blks_needed
+= safe_margin
;