[PATCH 2/4] - e2fsprogs - standardize group block calculations

Eric Sandeen <[email protected]> Fri, 25 Aug 2006 17:04:24 -0500
Newsgroups gmane.comp.file-systems.ext2.devel
Message-ID <[email protected]>
We calculate first & last block in a group enough times, an inline seems appropriate.

Introduce and ext2fs_group_last_block ext2fs_group_last_block which calculate
the first & last block in a given group, inclusive.

ext2fs_group_last_block accomodates the last group, and returns the last block
in the filesystem.

This changes the m_raid_opt test a bit; today ext2fs_allocate_group_table does:

	group_blk = fs->super->s_first_data_block +
		(group * fs->super->s_blocks_per_group);
	
	last_blk = group_blk + fs->super->s_blocks_per_group;
	if (last_blk >= fs->super->s_blocks_count)
		last_blk = fs->super->s_blocks_count - 1;

so for all groups but the last, last_blk is actually the first block of the next
group.  For the last group, it's truly the last block in the group.

Standardizing w/ the inlines exposed this - test output for the last group
changes with this patch.  I'm trying to sort out what it's -supposed- to be
(how is stride supposed to affect filesystem layout?)

Signed-off-by: Eric Sandeen <[email protected]>


Index: e2fsprogs-1.39-16T-2/lib/ext2fs/ext2fs.h
===================================================================
--- e2fsprogs-1.39-16T-2.orig/lib/ext2fs/ext2fs.h
+++ e2fsprogs-1.39-16T-2/lib/ext2fs/ext2fs.h
@@ -967,6 +967,8 @@ extern int ext2fs_test_ib_dirty(ext2_fil
 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
 extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
 extern int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
+extern blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group);
+extern blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group);
 extern blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
 				      struct ext2_inode *inode);
 
@@ -1130,6 +1132,26 @@ _INLINE_ int ext2fs_group_of_ino(ext2_fi
 	return (ino - 1) / fs->super->s_inodes_per_group;
 }
 
+/*
+ * Return the first block (inclusive) in a group
+ */
+_INLINE_ blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group)
+{
+	return fs->super->s_first_data_block +
+		(group * fs->super->s_blocks_per_group);
+}
+
+/*
+ * Return the last block (inclusive) in a group
+ */
+_INLINE_ blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group)
+{
+	return (group == fs->group_desc_count - 1 ?
+		fs->super->s_blocks_count - 1 :
+		ext2fs_group_first_block(fs, group) +
+			(fs->super->s_blocks_per_group - 1));
+}
+
 _INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
 					struct ext2_inode *inode)
 {
Index: e2fsprogs-1.39-16T-2/e2fsck/super.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/e2fsck/super.c
+++ e2fsprogs-1.39-16T-2/e2fsck/super.c
@@ -569,11 +569,9 @@ void check_super_block(e2fsck_t ctx)
 
 	for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
 		pctx.group = i;
-		
-		if (i == fs->group_desc_count - 1)
-			last_block = sb->s_blocks_count - 1;
-		else
-			last_block = first_block + blocks_per_group - 1;
+
+		first_block = ext2fs_group_first_block(fs, i);
+		last_block = ext2fs_group_last_block(fs, i);
 
 		if ((gd->bg_block_bitmap < first_block) ||
 		    (gd->bg_block_bitmap > last_block)) {
@@ -608,7 +606,6 @@ void check_super_block(e2fsck_t ctx)
 		}
 		free_blocks += gd->bg_free_blocks_count;
 		free_inodes += gd->bg_free_inodes_count;
-		first_block += sb->s_blocks_per_group;
 
 		if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
 		    (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
Index: e2fsprogs-1.39-16T-2/lib/ext2fs/alloc_tables.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/lib/ext2fs/alloc_tables.c
+++ e2fsprogs-1.39-16T-2/lib/ext2fs/alloc_tables.c
@@ -34,12 +34,8 @@ errcode_t ext2fs_allocate_group_table(ex
 	blk_t		group_blk, start_blk, last_blk, new_blk, blk;
 	int		j;
 
-	group_blk = fs->super->s_first_data_block +
-		(group * fs->super->s_blocks_per_group);
-	
-	last_blk = group_blk + fs->super->s_blocks_per_group;
-	if (last_blk >= fs->super->s_blocks_count)
-		last_blk = fs->super->s_blocks_count - 1;
+	group_blk = ext2fs_group_first_block(fs, group);
+	last_blk = ext2fs_group_last_block(fs, group);
 
 	if (!bmap)
 		bmap = fs->block_map;
@@ -54,8 +50,8 @@ errcode_t ext2fs_allocate_group_table(ex
 			return retval;
 		start_blk += fs->inode_blocks_per_group;
 		start_blk += ((fs->stride * group) %
-			      (last_blk - start_blk));
-		if (start_blk > last_blk)
+			      (last_blk - start_blk + 1));
+		if (start_blk >= last_blk)
 			start_blk = group_blk;
 	} else
 		start_blk = group_blk;
Index: e2fsprogs-1.39-16T-2/lib/ext2fs/check_desc.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/lib/ext2fs/check_desc.c
+++ e2fsprogs-1.39-16T-2/lib/ext2fs/check_desc.c
@@ -38,11 +38,9 @@ errcode_t ext2fs_check_desc(ext2_filsys 
 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
 	for (i = 0; i < fs->group_desc_count; i++) {
-		if (i == fs->group_desc_count - 1)
-			last_block = fs->super->s_blocks_count - 1;
-		else
-			last_block = first_block +
-				     fs->super->s_blocks_per_group - 1;
+		first_block = ext2fs_group_first_block(fs, i);
+		last_block = ext2fs_group_last_block(fs, i);
+
 		/*
 		 * Check to make sure block bitmap for group is
 		 * located within the group.
@@ -65,8 +63,6 @@ errcode_t ext2fs_check_desc(ext2_filsys 
 		    ((fs->group_desc[i].bg_inode_table +
 		      fs->inode_blocks_per_group) > last_block))
 			return EXT2_ET_GDESC_BAD_INODE_TABLE;
-		
-		first_block += fs->super->s_blocks_per_group;
 	}
 	return 0;
 }
Index: e2fsprogs-1.39-16T-2/misc/dumpe2fs.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/misc/dumpe2fs.c
+++ e2fsprogs-1.39-16T-2/misc/dumpe2fs.c
@@ -153,13 +153,11 @@ static void list_desc (ext2_filsys fs)
 	else
 		old_desc_blocks = fs->desc_blocks;
 	for (i = 0; i < fs->group_desc_count; i++) {
+		first_block = ext2fs_group_first_block(fs, i);
+		last_block = ext2fs_group_last_block(fs, i);
+
 		ext2fs_super_and_bgd_loc(fs, i, &super_blk, 
 					 &old_desc_blk, &new_desc_blk, 0);
-		if (i == fs->group_desc_count - 1)
-			last_block = fs->super->s_blocks_count - 1;
-		else
-			last_block = first_block +
-				     fs->super->s_blocks_per_group - 1;
 
 		printf (_("Group %lu: (Blocks "), i);
 		print_range(first_block, last_block);
@@ -226,7 +224,6 @@ static void list_desc (ext2_filsys fs)
 			fputc('\n', stdout);
 			inode_bitmap += fs->super->s_inodes_per_group / 8;
 		}
-		first_block += fs->super->s_blocks_per_group;
 	}
 }
 
Index: e2fsprogs-1.39-16T-2/e2fsck/pass1.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/e2fsck/pass1.c
+++ e2fsprogs-1.39-16T-2/e2fsck/pass1.c
@@ -1890,6 +1890,7 @@ static void new_table_block(e2fsck_t ctx
 {
 	ext2_filsys fs = ctx->fs;
 	blk_t		old_block = *new_block;
+	blk_t		last_block;
 	int		i;
 	char		*buf;
 	struct problem_context	pctx;
@@ -1900,8 +1901,8 @@ static void new_table_block(e2fsck_t ctx
 	pctx.blk = old_block;
 	pctx.str = name;
 
-	pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
-			first_block + fs->super->s_blocks_per_group,
+	last_block = ext2fs_group_last_block(fs, group);
+	pctx.errcode = ext2fs_get_free_blocks(fs, first_block, last_block,
 					num, ctx->block_found_map, new_block);
 	if (pctx.errcode) {
 		pctx.num = num;
@@ -1952,9 +1953,11 @@ static void handle_fs_bad_blocks(e2fsck_
 {
 	ext2_filsys fs = ctx->fs;
 	dgrp_t		i;
-	int		first_block = fs->super->s_first_data_block;
+	int		first_block;
 
 	for (i = 0; i < fs->group_desc_count; i++) {
+		first_block = ext2fs_group_first_block(fs, i);
+
 		if (ctx->invalid_block_bitmap_flag[i]) {
 			new_table_block(ctx, first_block, i, _("block bitmap"),
 					1, &fs->group_desc[i].bg_block_bitmap);
@@ -1969,7 +1972,6 @@ static void handle_fs_bad_blocks(e2fsck_
 					&fs->group_desc[i].bg_inode_table);
 			ctx->flags |= E2F_FLAG_RESTART;
 		}
-		first_block += fs->super->s_blocks_per_group;
 	}
 	ctx->invalid_bitmaps = 0;
 }
Index: e2fsprogs-1.39-16T-2/tests/m_raid_opt/expect.1
===================================================================
--- e2fsprogs-1.39-16T-2.orig/tests/m_raid_opt/expect.1
+++ e2fsprogs-1.39-16T-2/tests/m_raid_opt/expect.1
@@ -944,8 +944,8 @@ Group 126: (Blocks 129025-130048)
   Free inodes: 32257-32512
 Group 127: (Blocks 130049-131071)
   Group descriptor at 130049
-  Block bitmap at 130744 (+695), Inode bitmap at 130745 (+696)
+  Block bitmap at 130743 (+694), Inode bitmap at 130744 (+695)
   Inode table at 130050-130081 (+1)
   988 free blocks, 256 free inodes, 0 directories
-  Free blocks: 130082-130743, 130746-131071
+  Free blocks: 130082-130742, 130745-131071
   Free inodes: 32513-32768



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642