rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
print_resource_track(rfs, &overall_track, fs->io);
- retval = ext2fs_close(rfs->new_fs);
+ retval = ext2fs_close_free(&rfs->new_fs);
if (retval)
goto errout;
fs->inode_map);
if (retval) goto errout;
- real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
- fs->group_desc_count)) - 1 +
+ 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);
fs->super->s_reserved_gdt_blocks = new;
}
+ if ((fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
+ (fs->super->s_first_meta_bg > fs->desc_blocks)) {
+ fs->super->s_feature_incompat &=
+ ~EXT2_FEATURE_INCOMPAT_META_BG;
+ fs->super->s_first_meta_bg = 0;
+ }
+
/*
* Update the location of the backup superblocks if the
* sparse_super2 feature is enabled.
int j, has_super;
dgrp_t i, max_groups, g;
blk64_t blk, group_blk;
- blk64_t old_blocks, new_blocks;
+ blk64_t old_blocks, new_blocks, group_end, cluster_freed;
blk64_t new_size;
unsigned int meta_bg, meta_bg_size;
errcode_t retval;
ext2_filsys fs, old_fs;
- ext2fs_block_bitmap meta_bmap;
+ ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL;
int flex_bg;
fs = rfs->new_fs;
ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
}
- if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
+ if (old_fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
old_blocks = old_fs->super->s_first_meta_bg;
+ else
+ old_blocks = old_fs->desc_blocks +
+ old_fs->super->s_reserved_gdt_blocks;
+ if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
new_blocks = fs->super->s_first_meta_bg;
- } else {
- old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
+ else
new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
- }
retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
if (retval)
* blocks as free.
*/
if (old_blocks > new_blocks) {
+ if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
+ retval = ext2fs_allocate_block_bitmap(fs,
+ _("new meta blocks"),
+ &new_meta_bmap);
+ if (retval)
+ goto errout;
+
+ retval = mark_table_blocks(fs, new_meta_bmap);
+ if (retval)
+ goto errout;
+ }
+
for (i = 0; i < max_groups; i++) {
if (!ext2fs_bg_has_super(fs, i)) {
group_blk += fs->super->s_blocks_per_group;
continue;
}
- for (blk = group_blk+1+new_blocks;
- blk < group_blk+1+old_blocks; blk++) {
- ext2fs_block_alloc_stats2(fs, blk, -1);
+ group_end = group_blk + 1 + old_blocks;
+ for (blk = group_blk + 1 + new_blocks;
+ blk < group_end;) {
+ if (new_meta_bmap == NULL ||
+ !ext2fs_test_block_bitmap2(new_meta_bmap,
+ blk)) {
+ cluster_freed =
+ EXT2FS_CLUSTER_RATIO(fs) -
+ (blk &
+ EXT2FS_CLUSTER_MASK(fs));
+ if (cluster_freed > group_end - blk)
+ cluster_freed = group_end - blk;
+ ext2fs_block_alloc_stats2(fs, blk, -1);
+ blk += EXT2FS_CLUSTER_RATIO(fs);
+ rfs->needed_blocks -= cluster_freed;
+ continue;
+ }
rfs->needed_blocks--;
+ blk++;
}
group_blk += fs->super->s_blocks_per_group;
}
retval = 0;
errout:
+ if (new_meta_bmap)
+ ext2fs_free_block_bitmap(new_meta_bmap);
if (meta_bmap)
ext2fs_free_block_bitmap(meta_bmap);
{
ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
blk64_t blk;
+ int group;
blk = get_new_block(rfs);
if (!blk)
ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
+
+ group = ext2fs_group_of_blk2(rfs->old_fs, blk);
+ ext2fs_clear_block_uninit(rfs->old_fs, group);
+ group = ext2fs_group_of_blk2(rfs->new_fs, blk);
+ ext2fs_clear_block_uninit(rfs->new_fs, group);
+
*ret = (blk64_t) blk;
return 0;
}
pb.error = 0;
new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
- inode = malloc(inode_size);
- if (!inode) {
+ retval = ext2fs_get_mem(inode_size, &inode);
+ if (retval) {
retval = ENOMEM;
goto errout;
}
ext2fs_close_inode_scan(scan);
if (block_buf)
ext2fs_free_mem(&block_buf);
- free(inode);
+ if (inode)
+ ext2fs_free_mem(&inode);
return retval;
}
dgrp_t i, max_groups;
ext2_filsys fs = rfs->new_fs;
char *cp;
- blk64_t old_blk, new_blk, blk;
+ blk64_t old_blk, new_blk, blk, cluster_freed;
errcode_t retval;
int j, to_move, moved;
+ ext2fs_block_bitmap new_bmap = NULL;
max_groups = fs->group_desc_count;
if (max_groups > rfs->old_fs->group_desc_count)
return retval;
}
+ if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
+ retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
+ &new_bmap);
+ if (retval)
+ return retval;
+
+ retval = mark_table_blocks(fs, new_bmap);
+ if (retval)
+ goto errout;
+ }
+
/*
* Figure out how many inode tables we need to move
*/
ext2fs_inode_table_loc(fs, i))
to_move++;
- if (to_move == 0)
- return 0;
+ if (to_move == 0) {
+ retval = 0;
+ goto errout;
+ }
if (rfs->progress) {
retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
if (!diff)
continue;
+ if (diff < 0)
+ diff = 0;
retval = io_channel_read_blk64(fs->io, old_blk,
fs->inode_blocks_per_group,
}
for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
- j < fs->inode_blocks_per_group ; j++, blk++)
- ext2fs_block_alloc_stats2(fs, blk, -1);
+ j < fs->inode_blocks_per_group;) {
+ if (new_bmap == NULL ||
+ !ext2fs_test_block_bitmap2(new_bmap, blk)) {
+ ext2fs_block_alloc_stats2(fs, blk, -1);
+ cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
+ (blk & EXT2FS_CLUSTER_MASK(fs));
+ blk += cluster_freed;
+ j += cluster_freed;
+ continue;
+ }
+ blk++;
+ j++;
+ }
ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
ext2fs_group_desc_csum_set(rfs->old_fs, i);
if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
printf("Inode table move finished.\n");
#endif
- return 0;
+ retval = 0;
errout:
+ if (new_bmap)
+ ext2fs_free_block_bitmap(new_bmap);
return retval;
}
stderr);
exit(1);
}
- if (old_desc != sb+1) {
+ if (old_desc && old_desc != sb+1) {
fputs(_("Should never happen! Unexpected old_desc in "
"super_sparse bg?\n"),
stderr);
blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
{
ext2_ino_t inode_count;
- dgrp_t groups;
+ dgrp_t groups, flex_groups;
blk64_t blks_needed, data_blocks;
blk64_t grp, data_needed, last_start;
blk64_t overhead = 0;
int old_desc_blocks;
- int extra_groups = 0;
int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
/*
fs->super->s_free_inodes_count;
blks_needed = ext2fs_div_ceil(inode_count,
fs->super->s_inodes_per_group) *
- EXT2_BLOCKS_PER_GROUP(fs->super);
+ (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
groups = ext2fs_div64_ceil(blks_needed,
EXT2_BLOCKS_PER_GROUP(fs->super));
#ifdef RESIZE2FS_DEBUG
* inode tables of slack space so the resize operation can be
* guaranteed to finish.
*/
+ flex_groups = groups;
if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
- extra_groups = flexbg_size - (groups & (flexbg_size - 1));
- data_needed += fs->inode_blocks_per_group * extra_groups;
- extra_groups = groups % flexbg_size;
+ dgrp_t remainder = groups & (flexbg_size - 1);
+
+ flex_groups += flexbg_size - remainder;
+ if (flex_groups > fs->group_desc_count)
+ flex_groups = fs->group_desc_count;
}
/*
* figure out how many data blocks we have given the number of groups
* we need for our inodes
*/
- data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
+ data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
last_start = 0;
- for (grp = 0; grp < groups; grp++) {
+ for (grp = 0; grp < flex_groups; grp++) {
overhead = calc_group_overhead(fs, grp, old_desc_blocks);
/*
* the groups leading up to the last group so we can determine
* how big the last group needs to be
*/
- if (grp != (groups - 1))
+ if (grp < (groups - 1))
last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
overhead;
- data_blocks -= overhead;
+ if (data_blocks > overhead)
+ data_blocks -= overhead;
+ else
+ data_blocks = 0;
}
#ifdef RESIZE2FS_DEBUG
if (flags & RESIZE_DEBUG_MIN_CALC)
* if we need more group descriptors in order to accomodate our data
* then we need to add them here
*/
- while (data_needed > data_blocks) {
- blk64_t remainder = data_needed - data_blocks;
+ blks_needed = data_needed;
+ while (blks_needed > data_blocks) {
+ blk64_t remainder = blks_needed - data_blocks;
dgrp_t extra_grps;
/* figure out how many more groups we need for the data */
extra_grps = ext2fs_div64_ceil(remainder,
EXT2_BLOCKS_PER_GROUP(fs->super));
- data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
+ data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
/* ok we have to account for the last group */
overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
- for (grp = groups; grp < groups+extra_grps; grp++) {
+ grp = flex_groups;
+ groups += extra_grps;
+ if (!(fs->super->s_feature_incompat &
+ EXT4_FEATURE_INCOMPAT_FLEX_BG))
+ flex_groups = groups;
+ else if (groups > flex_groups) {
+ dgrp_t r = groups & (flexbg_size - 1);
+
+ flex_groups = groups + flexbg_size - r;
+ if (flex_groups > fs->group_desc_count)
+ flex_groups = fs->group_desc_count;
+ }
+
+ for (; grp < flex_groups; grp++) {
overhead = calc_group_overhead(fs, grp,
old_desc_blocks);
* again, we need to see how much data we cram into
* all of the groups leading up to the last group
*/
- if (grp != (groups + extra_grps - 1))
+ if (grp < groups - 1)
last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
- overhead;
data_blocks -= overhead;
}
- groups += extra_grps;
- extra_groups += extra_grps;
- if (fs->super->s_feature_incompat
- & EXT4_FEATURE_INCOMPAT_FLEX_BG
- && extra_groups > flexbg_size) {
- /*
- * For ext4 we need to allow for up to a flex_bg worth
- * of inode tables of slack space so the resize
- * operation can be guaranteed to finish.
- */
- extra_groups = flexbg_size -
- (groups & (flexbg_size - 1));
- data_needed += (fs->inode_blocks_per_group *
- extra_groups);
- extra_groups = groups % flexbg_size;
- }
#ifdef RESIZE2FS_DEBUG
if (flags & RESIZE_DEBUG_MIN_CALC)
printf("Added %d extra group(s), "
- "data_needed %llu, data_blocks %llu, "
- "last_start %llu\n",
- extra_grps, data_needed, data_blocks,
- last_start);
+ "blks_needed %llu, data_blocks %llu, "
+ "last_start %llu\n", extra_grps, blks_needed,
+ data_blocks, last_start);
#endif
}
/* now for the fun voodoo */
- overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
+ grp = groups - 1;
+ if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
+ (grp & ~(flexbg_size - 1)) == 0)
+ grp = grp & ~(flexbg_size - 1);
+ overhead = 0;
+ for (; grp < flex_groups; grp++)
+ overhead += calc_group_overhead(fs, grp, old_desc_blocks);
+
#ifdef RESIZE2FS_DEBUG
if (flags & RESIZE_DEBUG_MIN_CALC)
printf("Last group's overhead is %llu\n", overhead);
* if this is the case then the last group is going to have data in it
* so we need to adjust the size of the last group accordingly
*/
- if (last_start < data_needed) {
- blk64_t remainder = data_needed - last_start;
+ if (last_start < blks_needed) {
+ blk64_t remainder = blks_needed - last_start;
#ifdef RESIZE2FS_DEBUG
if (flags & RESIZE_DEBUG_MIN_CALC)
printf("Final size of last group is %lld\n", overhead);
#endif
+ /* Add extra slack for bigalloc file systems */
+ if (EXT2FS_CLUSTER_RATIO(fs) > 1)
+ overhead += EXT2FS_CLUSTER_RATIO(fs) * 2;
+
/*
- * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
- * only do groups-1, and then add the number of blocks needed to
- * handle the group descriptor metadata+data that we need
+ * since our last group doesn't have to be BLOCKS_PER_GROUP
+ * large, we only do groups-1, and then add the number of
+ * blocks needed to handle the group descriptor metadata+data
+ * that we need
*/
- blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
+ blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
blks_needed += overhead;
/*
* We need to reserve a few extra blocks if extents are
* enabled, in case we need to grow the extent tree. The more
* we shrink the file system, the more space we need.
+ *
+ * The absolute worst case is every single data block is in
+ * the part of the file system that needs to be evacuated,
+ * with each data block needs to be in its own extent, and
+ * with each inode needing at least one extent block.
*/
if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
blks_needed)/500;
+ unsigned int exts_per_blk = (fs->blocksize /
+ sizeof(struct ext3_extent)) - 1;
+ blk64_t worst_case = ((data_needed + exts_per_blk - 1) /
+ exts_per_blk);
+
+ if (worst_case < inode_count)
+ worst_case = inode_count;
+
+ if (safe_margin > worst_case)
+ safe_margin = worst_case;
+
#ifdef RESIZE2FS_DEBUG
if (flags & RESIZE_DEBUG_MIN_CALC)
printf("Extents safety margin: %llu\n", safe_margin);