Whamcloud - gitweb
LU-1365 resize2fs: clear uninit if allocating from new group
[tools/e2fsprogs.git] / resize / resize2fs.c
index caff422..198cbd9 100644 (file)
@@ -202,7 +202,7 @@ errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
        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;
 
@@ -436,8 +436,7 @@ retry:
                                            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);
@@ -490,6 +489,13 @@ retry:
                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.
@@ -909,12 +915,12 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
        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;
@@ -999,13 +1005,15 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
                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)
@@ -1026,15 +1034,42 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
         * 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;
                }
@@ -1180,6 +1215,8 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
        retval = 0;
 
 errout:
+       if (new_meta_bmap)
+               ext2fs_free_block_bitmap(new_meta_bmap);
        if (meta_bmap)
                ext2fs_free_block_bitmap(meta_bmap);
 
@@ -1252,6 +1289,7 @@ static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
 {
        ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
        blk64_t blk;
+       int group;
 
        blk = get_new_block(rfs);
        if (!blk)
@@ -1264,6 +1302,12 @@ static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
 
        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;
 }
@@ -1547,8 +1591,8 @@ static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
        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;
        }
@@ -1640,7 +1684,8 @@ errout:
                ext2fs_close_inode_scan(scan);
        if (block_buf)
                ext2fs_free_mem(&block_buf);
-       free(inode);
+       if (inode)
+               ext2fs_free_mem(&inode);
        return retval;
 }
 
@@ -1779,9 +1824,10 @@ static errcode_t move_itables(ext2_resize_t rfs)
        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)
@@ -1794,6 +1840,17 @@ static errcode_t move_itables(ext2_resize_t rfs)
                        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
         */
@@ -1803,8 +1860,10 @@ static errcode_t move_itables(ext2_resize_t rfs)
                    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,
@@ -1828,6 +1887,8 @@ static errcode_t move_itables(ext2_resize_t rfs)
 
                if (!diff)
                        continue;
+               if (diff < 0)
+                       diff = 0;
 
                retval = io_channel_read_blk64(fs->io, old_blk,
                                               fs->inode_blocks_per_group,
@@ -1871,8 +1932,19 @@ static errcode_t move_itables(ext2_resize_t rfs)
                }
 
                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);
@@ -1892,9 +1964,11 @@ static errcode_t move_itables(ext2_resize_t rfs)
        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;
 }
 
@@ -1991,7 +2065,7 @@ static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
                      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);
@@ -2247,12 +2321,11 @@ static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
 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;
 
        /*
@@ -2263,7 +2336,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
                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
@@ -2297,19 +2370,22 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
         * 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);
 
                /*
@@ -2317,11 +2393,14 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
                 * 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)
@@ -2333,21 +2412,35 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
         * 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);
 
@@ -2355,41 +2448,31 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
                         * 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);
@@ -2399,8 +2482,8 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
         * 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)
@@ -2426,12 +2509,17 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
                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;
 
        /*
@@ -2458,10 +2546,26 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
         * 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);