Patchwork [1/5] libext2fs: Optimize ext2fs_allocate_group_table()

login
register
mail settings
Submitter Andrey Sidorov
Date March 19, 2013, 4:06 p.m.
Message ID <1363709164-3210-2-git-send-email-qrxd43@motorola.com>
Download mbox | patch
Permalink /patch/229102/
State New
Headers show

Comments

Andrey Sidorov - March 19, 2013, 4:06 p.m.
Iterate over groups instead of blocks.

Signed-off-by: Andrey Sidorov <qrxd43@motorola.com>
---
 lib/ext2fs/alloc_tables.c |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

Patch

diff --git a/lib/ext2fs/alloc_tables.c b/lib/ext2fs/alloc_tables.c
index 5e6e556..e51dcee 100644
--- a/lib/ext2fs/alloc_tables.c
+++ b/lib/ext2fs/alloc_tables.c
@@ -205,16 +205,27 @@  errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
 						bmap, &new_blk);
 		if (retval)
 			return retval;
-		for (j=0, blk = new_blk;
-		     j < fs->inode_blocks_per_group;
-		     j++, blk++) {
-			ext2fs_mark_block_bitmap2(bmap, blk);
-			if (flexbg_size) {
-				dgrp_t gr = ext2fs_group_of_blk2(fs, blk);
-				ext2fs_bg_free_blocks_count_set(fs, gr, ext2fs_bg_free_blocks_count(fs, gr) - 1);
-				ext2fs_free_blocks_count_add(fs->super, -1);
-				ext2fs_bg_flags_clear(fs, gr,
-						     EXT2_BG_BLOCK_UNINIT);
+
+		ext2fs_mark_block_bitmap_range2(bmap, new_blk, fs->inode_blocks_per_group);
+		if (flexbg_size) {
+			blk64_t last_blk = new_blk + fs->inode_blocks_per_group - 1;
+			dgrp_t gr = ext2fs_group_of_blk2(fs, new_blk);
+			dgrp_t last_gr = ext2fs_group_of_blk2(fs, last_blk);
+
+			ext2fs_free_blocks_count_add(fs->super, -(blk64_t)(fs->inode_blocks_per_group));
+			for (; gr <= last_gr; ++gr) {
+				blk64_t gr_first_blk = ext2fs_group_first_block2(fs, gr);
+				blk64_t gr_last_blk = ext2fs_group_last_block2(fs, gr);
+				__u32 free_blocks = ext2fs_bg_free_blocks_count(fs, gr);
+				if (gr_first_blk < new_blk)
+					gr_first_blk = new_blk;
+				if (gr_last_blk > last_blk)
+					gr_last_blk = last_blk;
+
+				free_blocks -= (__u32)(gr_last_blk - gr_first_blk + 1);
+
+				ext2fs_bg_free_blocks_count_set(fs, gr, free_blocks);
+				ext2fs_bg_flags_clear(fs, gr, EXT2_BG_BLOCK_UNINIT);
 				ext2fs_group_desc_csum_set(fs, gr);
 			}
 		}