Whamcloud - gitweb
libext2fs: allocate clusters to files in expand_dir.c and mkjournal.c
[tools/e2fsprogs.git] / lib / ext2fs / alloc.c
index 3439445..fbecdbe 100644 (file)
@@ -4,10 +4,9 @@
  * Copyright (C) 1993, 1994, 1995, 1996 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 <stdio.h>
@@ -30,7 +29,7 @@
  * Check for uninit block bitmaps and deal with them appropriately
  */
 static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
-                         dgrp_t group)
+                              dgrp_t group)
 {
        blk_t           i;
        blk64_t         blk, super_blk, old_desc_blk, new_desc_blk;
@@ -38,7 +37,7 @@ static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
 
        if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
                                         EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
-           !(fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT))
+           !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
                return;
 
        blk = (group * fs->super->s_blocks_per_group) +
@@ -53,22 +52,23 @@ static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
        else
                old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
 
+       for (i=0; i < fs->super->s_blocks_per_group; i++, blk++)
+               ext2fs_fast_unmark_block_bitmap2(map, blk);
+
        for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
                if ((blk == super_blk) ||
                    (old_desc_blk && old_desc_blocks &&
                     (blk >= old_desc_blk) &&
                     (blk < old_desc_blk + old_desc_blocks)) ||
                    (new_desc_blk && (blk == new_desc_blk)) ||
-                   (blk == fs->group_desc[group].bg_block_bitmap) ||
-                   (blk == fs->group_desc[group].bg_inode_bitmap) ||
-                   (blk >= fs->group_desc[group].bg_inode_table &&
-                    (blk < fs->group_desc[group].bg_inode_table
+                   (blk == ext2fs_block_bitmap_loc(fs, group)) ||
+                   (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
+                   (blk >= ext2fs_inode_table_loc(fs, group) &&
+                    (blk < ext2fs_inode_table_loc(fs, group)
                      + fs->inode_blocks_per_group)))
-                       ext2fs_fast_mark_block_bitmap(map, blk);
-               else
-                       ext2fs_fast_unmark_block_bitmap(map, blk);
+                       ext2fs_fast_mark_block_bitmap2(map, blk);
        }
-       fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
+       ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
        ext2fs_group_desc_csum_set(fs, group);
 }
 
@@ -82,14 +82,14 @@ static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
 
        if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
                                         EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
-           !(fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT))
+           !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
                return;
 
        ino = (group * fs->super->s_inodes_per_group) + 1;
        for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
-               ext2fs_fast_unmark_inode_bitmap(map, ino);
+               ext2fs_fast_unmark_inode_bitmap2(map, ino);
 
-       fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
+       ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
        check_block_uninit(fs, fs->block_map, group);
 }
 
@@ -129,14 +129,14 @@ errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
                        check_inode_uninit(fs, map, (i - 1) /
                                           EXT2_INODES_PER_GROUP(fs->super));
 
-               if (!ext2fs_fast_test_inode_bitmap(map, i))
+               if (!ext2fs_fast_test_inode_bitmap2(map, i))
                        break;
                i++;
                if (i > fs->super->s_inodes_count)
                        i = EXT2_FIRST_INODE(fs->super);
        } while (i != start_inode);
 
-       if (ext2fs_test_inode_bitmap(map, i))
+       if (ext2fs_test_inode_bitmap2(map, i))
                return EXT2_ET_INODE_ALLOC_FAIL;
        *ret = i;
        return 0;
@@ -150,6 +150,7 @@ errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
                           ext2fs_block_bitmap map, blk64_t *ret)
 {
        blk64_t i;
+       int     c_ratio;
 
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
@@ -157,9 +158,10 @@ errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
                map = fs->block_map;
        if (!map)
                return EXT2_ET_NO_BLOCK_BITMAP;
-       if (!goal || (goal >= fs->super->s_blocks_count))
+       if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
                goal = fs->super->s_first_data_block;
        i = goal;
+       c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
        check_block_uninit(fs, map,
                           (i - fs->super->s_first_data_block) /
                           EXT2_BLOCKS_PER_GROUP(fs->super));
@@ -170,13 +172,12 @@ errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
                                           (i - fs->super->s_first_data_block) /
                                           EXT2_BLOCKS_PER_GROUP(fs->super));
 
-               /* FIXME-64 */
-               if (!ext2fs_fast_test_block_bitmap(map, (blk_t) i)) {
+               if (!ext2fs_fast_test_block_bitmap2(map, i)) {
                        *ret = i;
                        return 0;
                }
-               i++;
-               if (i >= fs->super->s_blocks_count)
+               i = (i + c_ratio) & ~(c_ratio - 1);
+               if (i >= ext2fs_blocks_count(fs->super))
                        i = fs->super->s_first_data_block;
        } while (i != goal);
        return EXT2_ET_BLOCK_ALLOC_FAIL;
@@ -218,7 +219,6 @@ errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
                        goto fail;
        } else {
                if (!fs->block_map) {
-                       /* FIXME-64 */
                        retval = ext2fs_read_block_bitmap(fs);
                        if (retval)
                                goto fail;
@@ -256,7 +256,8 @@ errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
                                 int num, ext2fs_block_bitmap map, blk64_t *ret)
 {
-       blk64_t b = start;
+       blk64_t b = start, c = 0;
+       int     c_ratio;
 
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
@@ -270,16 +271,17 @@ errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
                finish = start;
        if (!num)
                num = 1;
+       c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
+       b &= ~(c_ratio - 1);
+       finish &= ~(c_ratio -1);
        do {
-               if (b+num-1 > fs->super->s_blocks_count)
+               if (b+num-1 > ext2fs_blocks_count(fs->super))
                        b = fs->super->s_first_data_block;
-               /* FIXME-64 */
-               if (ext2fs_fast_test_block_bitmap_range(map, (blk_t) b,
-                                                       (blk_t) num)) {
+               if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
                        *ret = b;
                        return 0;
                }
-               b++;
+               b += c_ratio;
        } while (b != finish);
        return EXT2_ET_BLOCK_ALLOC_FAIL;
 }