Patchwork [V2] e2fsprogs: add ext2fs_group_blocks_count helper

login
register
mail settings
Submitter Eric Sandeen
Date July 20, 2011, 9:57 p.m.
Message ID <4E274F57.8020007@redhat.com>
Download mbox | patch
Permalink /patch/105848/
State Superseded
Headers show

Comments

Eric Sandeen - July 20, 2011, 9:57 p.m.
Code to count the number of blocks in the last partial
group is cut and pasted around the e2fsprogs codebase, and
is wrong in at least one instancem as pointed out by
Yongqiang Yang (but not fixed in this patch).

Making this a helper function should improve matters.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

(I have not fixed up the spot Yongqiang Yang discovered,
but this helper should now make that fix easy to do.)

V2: Sorry, first patch was against an old tree.

 lib/ext2fs/alloc_sb.c |   10 +---------
 lib/ext2fs/blknum.c   |   19 +++++++++++++++++++
 lib/ext2fs/closefs.c  |    9 +--------
 lib/ext2fs/ext2fs.h   |    1 +
 resize/online.c       |    7 +------
 resize/resize2fs.c    |   14 ++------------
 6 files changed, 25 insertions(+), 35 deletions(-)


--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eric Sandeen - July 21, 2011, 1:55 p.m.
On 7/20/11 4:57 PM, Eric Sandeen wrote:
> Code to count the number of blocks in the last partial
> group is cut and pasted around the e2fsprogs codebase, and
> is wrong in at least one instancem as pointed out by
> Yongqiang Yang (but not fixed in this patch).
> 
> Making this a helper function should improve matters.
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---

Argh V3 coming today, I got a little too fast and loose sending this one
at the end of the day yesterday didn't I:

> @@ -499,18 +499,8 @@ retry:
>  		ext2fs_bg_flags_zap(fs, i);
>  		if (csum_flag)
>  			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
> -		if (i == fs->group_desc_count-1) {
> -			numblocks = (ext2fs_blocks_count(fs->super) -
> -				     fs->super->s_first_data_block) %
> -					     fs->super->s_blocks_per_group;
> -			if (!numblocks)
> -				numblocks = fs->super->s_blocks_per_group;
> -		} else {
> -			numblocks = fs->super->s_blocks_per_group;
> -			if (csum_flag)
> -				ext2fs_bg_flags_set(fs, i,
> -						    EXT2_BG_BLOCK_UNINIT);

still need to do that flag set with the new helper.

- Eric

> -		}
> +
> +		numblocks = ext2fs_group_blocks_count(fs, i);
>  
>  		has_super = ext2fs_bg_has_super(fs, i);
>  		if (has_super) {
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yongqiang Yang - July 21, 2011, 2:57 p.m.
On Thu, Jul 21, 2011 at 9:55 PM, Eric Sandeen <sandeen@redhat.com> wrote:
> On 7/20/11 4:57 PM, Eric Sandeen wrote:
>> Code to count the number of blocks in the last partial
>> group is cut and pasted around the e2fsprogs codebase, and
>> is wrong in at least one instancem as pointed out by
>> Yongqiang Yang (but not fixed in this patch).
>>
>> Making this a helper function should improve matters.
>>
>> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
>> ---
>
> Argh V3 coming today, I got a little too fast and loose sending this one
> at the end of the day yesterday didn't I:
Hi Eric,

It seemed you did not send out the v3 patch.   You just pointed out
what to be added in the V3 patch.

Yongqiang.
>
>> @@ -499,18 +499,8 @@ retry:
>>               ext2fs_bg_flags_zap(fs, i);
>>               if (csum_flag)
>>                       ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
>> -             if (i == fs->group_desc_count-1) {
>> -                     numblocks = (ext2fs_blocks_count(fs->super) -
>> -                                  fs->super->s_first_data_block) %
>> -                                          fs->super->s_blocks_per_group;
>> -                     if (!numblocks)
>> -                             numblocks = fs->super->s_blocks_per_group;
>> -             } else {
>> -                     numblocks = fs->super->s_blocks_per_group;
>> -                     if (csum_flag)
>> -                             ext2fs_bg_flags_set(fs, i,
>> -                                                 EXT2_BG_BLOCK_UNINIT);
>
> still need to do that flag set with the new helper.
>
> - Eric
>
>> -             }
>> +
>> +             numblocks = ext2fs_group_blocks_count(fs, i);
>>
>>               has_super = ext2fs_bg_has_super(fs, i);
>>               if (has_super) {
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
Eric Sandeen - July 21, 2011, 3:06 p.m.
On 7/21/11 9:57 AM, Yongqiang Yang wrote:
> On Thu, Jul 21, 2011 at 9:55 PM, Eric Sandeen <sandeen@redhat.com> wrote:
>> On 7/20/11 4:57 PM, Eric Sandeen wrote:
>>> Code to count the number of blocks in the last partial
>>> group is cut and pasted around the e2fsprogs codebase, and
>>> is wrong in at least one instancem as pointed out by
>>> Yongqiang Yang (but not fixed in this patch).
>>>
>>> Making this a helper function should improve matters.
>>>
>>> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
>>> ---
>>
>> Argh V3 coming today, I got a little too fast and loose sending this one
>> at the end of the day yesterday didn't I:
> Hi Eric,
> 
> It seemed you did not send out the v3 patch.   You just pointed out
> what to be added in the V3 patch.

That is correct.  :)

I'll send it out a bit later after I carefully review this time :(

-Eric

> Yongqiang.
>>
>>> @@ -499,18 +499,8 @@ retry:
>>>               ext2fs_bg_flags_zap(fs, i);
>>>               if (csum_flag)
>>>                       ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
>>> -             if (i == fs->group_desc_count-1) {
>>> -                     numblocks = (ext2fs_blocks_count(fs->super) -
>>> -                                  fs->super->s_first_data_block) %
>>> -                                          fs->super->s_blocks_per_group;
>>> -                     if (!numblocks)
>>> -                             numblocks = fs->super->s_blocks_per_group;
>>> -             } else {
>>> -                     numblocks = fs->super->s_blocks_per_group;
>>> -                     if (csum_flag)
>>> -                             ext2fs_bg_flags_set(fs, i,
>>> -                                                 EXT2_BG_BLOCK_UNINIT);
>>
>> still need to do that flag set with the new helper.
>>
>> - Eric
>>
>>> -             }
>>> +
>>> +             numblocks = ext2fs_group_blocks_count(fs, i);
>>>
>>>               has_super = ext2fs_bg_has_super(fs, i);
>>>               if (has_super) {
>>>
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
>>
> 
> 
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/lib/ext2fs/alloc_sb.c b/lib/ext2fs/alloc_sb.c
index 09786a7..2e32389 100644
--- a/lib/ext2fs/alloc_sb.c
+++ b/lib/ext2fs/alloc_sb.c
@@ -74,15 +74,7 @@  int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 	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  ;
diff --git a/lib/ext2fs/blknum.c b/lib/ext2fs/blknum.c
index b3e6dca..f1bbe9b 100644
--- a/lib/ext2fs/blknum.c
+++ b/lib/ext2fs/blknum.c
@@ -43,6 +43,25 @@  blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group)
 }
 
 /*
+ * Return the number of blocks in a group
+ */
+int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group)
+{
+	int num_blocks;
+
+	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;
+
+	return num_blocks;
+}
+
+/*
  * Return the inode data block count
  */
 blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
diff --git a/lib/ext2fs/closefs.c b/lib/ext2fs/closefs.c
index 952f496..51ef63c 100644
--- a/lib/ext2fs/closefs.c
+++ b/lib/ext2fs/closefs.c
@@ -152,14 +152,7 @@  int ext2fs_super_and_bgd_loc(ext2_filsys fs,
 					&ret_new_desc_blk2,
 					&ret_used_blks);
 
-	if (group == fs->group_desc_count-1) {
-		numblocks = (ext2fs_blocks_count(fs->super) -
-			     (blk64_t) fs->super->s_first_data_block) %
-			(blk64_t) fs->super->s_blocks_per_group;
-		if (!numblocks)
-			numblocks = fs->super->s_blocks_per_group;
-	} else
-		numblocks = fs->super->s_blocks_per_group;
+	numblocks = ext2fs_group_blocks_count(fs, group);
 
 	if (ret_super_blk)
 		*ret_super_blk = (blk_t)ret_super_blk2;
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index dc83fb0..60a903a 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -757,6 +757,7 @@  extern errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
 extern dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t);
 extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
+extern int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
 					 struct ext2_inode *inode);
 extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
diff --git a/resize/online.c b/resize/online.c
index 1d8d4ec..c2b98c6 100644
--- a/resize/online.c
+++ b/resize/online.c
@@ -135,12 +135,7 @@  errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
 		input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i);
 		input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i);
 		input.inode_table = ext2fs_inode_table_loc(new_fs, i);
-		input.blocks_count = sb->s_blocks_per_group;
-		if (i == new_fs->group_desc_count-1) {
-			input.blocks_count = ext2fs_blocks_count(new_fs->super) -
-				sb->s_first_data_block -
-				(i * sb->s_blocks_per_group);
-		}
+		input.blocks_count = ext2fs_group_blocks_count(new_fs, i);
 		input.reserved_blocks = (blk_t) (percent * input.blocks_count
 						 / 100.0);
 
diff --git a/resize/resize2fs.c b/resize/resize2fs.c
index 45ea5f4..a8e7d51 100644
--- a/resize/resize2fs.c
+++ b/resize/resize2fs.c
@@ -499,18 +499,8 @@  retry:
 		ext2fs_bg_flags_zap(fs, i);
 		if (csum_flag)
 			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED);
-		if (i == fs->group_desc_count-1) {
-			numblocks = (ext2fs_blocks_count(fs->super) -
-				     fs->super->s_first_data_block) %
-					     fs->super->s_blocks_per_group;
-			if (!numblocks)
-				numblocks = fs->super->s_blocks_per_group;
-		} else {
-			numblocks = fs->super->s_blocks_per_group;
-			if (csum_flag)
-				ext2fs_bg_flags_set(fs, i,
-						    EXT2_BG_BLOCK_UNINIT);
-		}
+
+		numblocks = ext2fs_group_blocks_count(fs, i);
 
 		has_super = ext2fs_bg_has_super(fs, i);
 		if (has_super) {