Skip to content

Commit 424de6e

Browse files
committed
libext2fs: optimize ext2fs_new_block2()
If there are hundreds of thousands of blocks which are in use before the first free block, it is much, MUCH faster to use ext2fs_find_first_zero_block_bitmap2() instead of searching the allocation bitmap bit by bit. Signed-off-by: "Theodore Ts'o" <[email protected]>
1 parent fccdbac commit 424de6e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

lib/ext2fs/alloc.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
137137
errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
138138
ext2fs_block_bitmap map, blk64_t *ret)
139139
{
140-
blk64_t i;
141-
int c_ratio;
140+
errcode_t retval;
141+
blk64_t b;
142142

143143
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
144144

@@ -148,21 +148,21 @@ errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
148148
return EXT2_ET_NO_BLOCK_BITMAP;
149149
if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
150150
goal = fs->super->s_first_data_block;
151-
i = goal;
152-
c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
153-
if (c_ratio > 1)
154-
goal &= ~EXT2FS_CLUSTER_MASK(fs);
155-
do {
156-
if (!ext2fs_fast_test_block_bitmap2(map, i)) {
157-
clear_block_uninit(fs, ext2fs_group_of_blk2(fs, i));
158-
*ret = i;
159-
return 0;
160-
}
161-
i = (i + c_ratio) & ~(c_ratio - 1);
162-
if (i >= ext2fs_blocks_count(fs->super))
163-
i = fs->super->s_first_data_block;
164-
} while (i != goal);
165-
return EXT2_ET_BLOCK_ALLOC_FAIL;
151+
goal &= ~EXT2FS_CLUSTER_MASK(fs);
152+
153+
retval = ext2fs_find_first_zero_block_bitmap2(map,
154+
goal, ext2fs_blocks_count(fs->super) - 1, &b);
155+
if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
156+
retval = ext2fs_find_first_zero_block_bitmap2(map,
157+
fs->super->s_first_data_block, goal - 1, &b);
158+
if (retval == ENOENT)
159+
return EXT2_ET_BLOCK_ALLOC_FAIL;
160+
if (retval)
161+
return retval;
162+
163+
clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
164+
*ret = b;
165+
return 0;
166166
}
167167

168168
errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,

0 commit comments

Comments
 (0)