Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / resize / resize2fs.c
index f6c3ede..c9409f4 100644 (file)
@@ -138,6 +138,7 @@ errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags,
        if (retval)
                goto errout;
 
+       rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;        
        retval = ext2fs_close(rfs->new_fs);
        if (retval)
                goto errout;
@@ -186,6 +187,7 @@ errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
        unsigned long   i, j, old_desc_blocks, max_group;
        unsigned int    meta_bg, meta_bg_size;
        int             has_super;
+       unsigned long long new_inodes;  /* u64 to check for overflow */
 
        fs->super->s_blocks_count = new_size;
 
@@ -226,6 +228,12 @@ retry:
        /*
         * Adjust the number of inodes
         */
+       new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
+       if (new_inodes > ~0U) {
+               fprintf(stderr, _("inodes (%llu) must be less than %u"),
+                                  new_inodes, ~0U);
+               return EXT2_ET_TOO_MANY_INODES;
+       }
        fs->super->s_inodes_count = fs->super->s_inodes_per_group *
                fs->group_desc_count;
 
@@ -243,10 +251,10 @@ retry:
        /*
         * Adjust the number of reserved blocks
         */
-       blk = old_fs->super->s_r_blocks_count * 100 /
+       blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
                old_fs->super->s_blocks_count;
-       fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
-                                      / 100);
+       fs->super->s_r_blocks_count = e2p_percent(blk, 
+                                                 fs->super->s_blocks_count);
 
        /*
         * Adjust the bitmaps for size
@@ -296,7 +304,7 @@ retry:
                        (old_fs->desc_blocks - fs->desc_blocks);
                if (new < 0)
                        new = 0;
-               if (new > fs->blocksize/4)
+               if (new > (int) fs->blocksize/4)
                        new = fs->blocksize/4;
                fs->super->s_reserved_gdt_blocks = new;
                if (new == 0)
@@ -375,8 +383,7 @@ retry:
                        ext2fs_mark_block_bitmap(fs->block_map, group_block);
                        adjblocks++;
                }
-               meta_bg_size = (fs->blocksize /
-                               sizeof (struct ext2_group_desc));
+               meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
                meta_bg = i / meta_bg_size;
                if (!(fs->super->s_feature_incompat &
                      EXT2_FEATURE_INCOMPAT_META_BG) ||
@@ -473,7 +480,7 @@ static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size)
        /*
         * Initialize the new block group descriptors
         */
-       retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group,
+       retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
                                &rfs->itable_buf);
        if (retval)
                goto errout;
@@ -536,14 +543,13 @@ errout:
 static errcode_t mark_table_blocks(ext2_filsys fs,
                                   ext2fs_block_bitmap bmap)
 {
-       blk_t                   block, b;
+       blk_t                   b;
        unsigned int            j;
        dgrp_t                  i;
        unsigned long           meta_bg_size;
        unsigned int            old_desc_blocks;
 
-       meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
-       block = fs->super->s_first_data_block;
+       meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
        if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
                old_desc_blocks = fs->super->s_first_meta_bg;
        else
@@ -571,7 +577,6 @@ static errcode_t mark_table_blocks(ext2_filsys fs,
                 */
                ext2fs_mark_block_bitmap(bmap,
                                         fs->group_desc[i].bg_inode_bitmap);
-               block += fs->super->s_blocks_per_group;
        }
        return 0;
 }
@@ -711,7 +716,7 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
         * If we're increasing the number of descriptor blocks, life
         * gets interesting....  
         */
-       meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
+       meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
        for (i = 0; i < max_groups; i++) {
                has_super = ext2fs_bg_has_super(fs, i);
                if (has_super)
@@ -911,7 +916,7 @@ static errcode_t block_mover(ext2_resize_t rfs)
 
        new_blk = fs->super->s_first_data_block;
        if (!rfs->itable_buf) {
-               retval = ext2fs_get_mem(fs->blocksize *
+               retval = ext2fs_get_array(fs->blocksize,
                                        fs->inode_blocks_per_group,
                                        &rfs->itable_buf);
                if (retval)
@@ -1132,7 +1137,7 @@ static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
 
        retval = ext2fs_init_dblist(rfs->old_fs, 0);
        if (retval) goto errout;
-       retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, &block_buf);
+       retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
        if (retval) goto errout;
 
        start_to_move = (rfs->new_fs->group_desc_count *
@@ -1301,7 +1306,9 @@ static int check_and_change_inodes(ext2_ino_t dir,
        retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
        if (retval == 0) {
                inode.i_mtime = inode.i_ctime = time(0);
-               ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
+               is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
+               if (is->err)
+                       return DIRENT_ABORT;
        }
 
        return DIRENT_CHANGED;
@@ -1582,7 +1589,9 @@ static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
        total_free = 0;
        count = 0;
        group = 0;
-       for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
+
+       /* Protect loop from wrap-around if s_inodes_count maxed */
+       for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
                if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
                        group_free++;
                        total_free++;