Whamcloud - gitweb
e2fsck: allow to fix some superblock corruptions
[tools/e2fsprogs.git] / lib / ext2fs / alloc_sb.c
index 37f2140..8530b40 100644 (file)
@@ -5,11 +5,12 @@
  * Copyright (C) 1994, 1995, 1996, 2003 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -46,12 +47,12 @@ int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 {
        blk64_t super_blk, old_desc_blk, new_desc_blk;
        blk_t   used_blks;
-       int     j, old_desc_blocks, num_blocks;
+       int     old_desc_blocks, num_blocks;
 
        ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
                                  &old_desc_blk, &new_desc_blk, &used_blks);
 
-       if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
+       if (ext2fs_has_feature_meta_bg(fs->super))
                old_desc_blocks = fs->super->s_first_meta_bg;
        else
                old_desc_blocks =
@@ -59,27 +60,21 @@ int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 
        if (super_blk || (group == 0))
                ext2fs_mark_block_bitmap2(bmap, super_blk);
+       if ((group == 0) && (fs->blocksize == 1024) &&
+           EXT2FS_CLUSTER_RATIO(fs) > 1)
+               ext2fs_mark_block_bitmap2(bmap, 0);
 
        if (old_desc_blk) {
-               if (fs->super->s_reserved_gdt_blocks && fs->block_map == bmap)
-                       ext2fs_bg_flag_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
-               for (j=0; j < old_desc_blocks; j++)
-                       if (old_desc_blk + j < ext2fs_blocks_count(fs->super))
-                               ext2fs_mark_block_bitmap2(bmap,
-                                                        old_desc_blk + j);
+               num_blocks = old_desc_blocks;
+               if (old_desc_blk + num_blocks >= ext2fs_blocks_count(fs->super))
+                       num_blocks = ext2fs_blocks_count(fs->super) -
+                               old_desc_blk;
+               ext2fs_mark_block_bitmap_range2(bmap, old_desc_blk, num_blocks);
        }
        if (new_desc_blk)
                ext2fs_mark_block_bitmap2(bmap, new_desc_blk);
 
-       if (group == fs->group_desc_count-1) {
-               num_blocks = (ext2fs_blocks_count(fs->super) -
-                            fs->super->s_first_data_block) %
-                       fs->super->s_blocks_per_group;
-               if (!num_blocks)
-                       num_blocks = fs->super->s_blocks_per_group;
-       } else
-               num_blocks = fs->super->s_blocks_per_group;
-
+       num_blocks = ext2fs_group_blocks_count(fs, group);
        num_blocks -= 2 + fs->inode_blocks_per_group + used_blks;
 
        return num_blocks  ;