diff mbox series

[2/7] e2fsck: Fix indexed dir rehash failure with metadata_csum enabled

Message ID 20200213101602.29096-3-jack@suse.cz
State Accepted
Headers show
Series e2fsprogs: Better handling of indexed directories | expand

Commit Message

Jan Kara Feb. 13, 2020, 10:15 a.m. UTC
E2fsck directory rehashing code can fail with ENOSPC due to a bug in
ext2fs_htree_intnode_maxrecs() which fails to take metadata checksum
into account and thus e.g. e2fsck can decide to create 1 indirect level
of index tree when two are actually needed. Fix the logic to account for
metadata checksum.

Signed-off-by: Jan Kara <jack@suse.cz>
---
 lib/ext2fs/ext2fs.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Andreas Dilger Feb. 14, 2020, 7:28 p.m. UTC | #1
On Feb 13, 2020, at 3:15 AM, Jan Kara <jack@suse.cz> wrote:
> 
> E2fsck directory rehashing code can fail with ENOSPC due to a bug in
> ext2fs_htree_intnode_maxrecs() which fails to take metadata checksum
> into account and thus e.g. e2fsck can decide to create 1 indirect level
> of index tree when two are actually needed. Fix the logic to account for
> metadata checksum.
> 
> Signed-off-by: Jan Kara <jack@suse.cz>

Reviewed-by: Andreas Dilger <adilger@dilger.ca>

> ---
> lib/ext2fs/ext2fs.h | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
> index 93ecf29c568d..5fde3343b1f1 100644
> --- a/lib/ext2fs/ext2fs.h
> +++ b/lib/ext2fs/ext2fs.h
> @@ -1783,7 +1783,6 @@ 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);
> -extern int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks);
> extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
> extern __u64 ext2fs_div64_ceil(__u64 a, __u64 b);
> extern int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry);
> @@ -2015,9 +2014,14 @@ _INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
> 	return (blk_t) ext2fs_inode_data_blocks2(fs, inode);
> }
> 
> -_INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
> +static inline int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
> {
> -	return blocks * ((fs->blocksize - 8) / sizeof(struct ext2_dx_entry));
> +	int csum_size = 0;
> +
> +	if (ext2fs_has_feature_metadata_csum(fs->super))
> +		csum_size = sizeof(struct ext2_dx_tail);
> +	return blocks * ((fs->blocksize - (8 + csum_size)) /
> +						sizeof(struct ext2_dx_entry));
> }
> 
> /*
> --
> 2.16.4
> 


Cheers, Andreas
Theodore Ts'o March 7, 2020, 11:17 p.m. UTC | #2
On Thu, Feb 13, 2020 at 11:15:57AM +0100, Jan Kara wrote:
> E2fsck directory rehashing code can fail with ENOSPC due to a bug in
> ext2fs_htree_intnode_maxrecs() which fails to take metadata checksum
> into account and thus e.g. e2fsck can decide to create 1 indirect level
> of index tree when two are actually needed. Fix the logic to account for
> metadata checksum.
> 
> Signed-off-by: Jan Kara <jack@suse.cz>

Applied with a minor change; I didn't want to make this change:

> -_INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
> +static inline int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)

... because it would make ext2fs_htree_intmode_maxrecs disappear from libext2fs.so.

So I changed this:

> +	if (ext2fs_has_feature_metadata_csum(fs->super))

to this:

+       if ((EXT2_SB(fs->super)->s_feature_ro_compat &
+            EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) != 0)

to fix the inline related compilation errors.

					- Ted
Jan Kara March 16, 2020, 9:30 a.m. UTC | #3
On Sat 07-03-20 18:17:19, Theodore Y. Ts'o wrote:
> On Thu, Feb 13, 2020 at 11:15:57AM +0100, Jan Kara wrote:
> > E2fsck directory rehashing code can fail with ENOSPC due to a bug in
> > ext2fs_htree_intnode_maxrecs() which fails to take metadata checksum
> > into account and thus e.g. e2fsck can decide to create 1 indirect level
> > of index tree when two are actually needed. Fix the logic to account for
> > metadata checksum.
> > 
> > Signed-off-by: Jan Kara <jack@suse.cz>
> 
> Applied with a minor change; I didn't want to make this change:
> 
> > -_INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
> > +static inline int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
> 
> ... because it would make ext2fs_htree_intmode_maxrecs disappear from
> libext2fs.so.

Aha! I was wondering what's going on with those strange inline
statements...

> So I changed this:
> 
> > +	if (ext2fs_has_feature_metadata_csum(fs->super))
> 
> to this:
> 
> +       if ((EXT2_SB(fs->super)->s_feature_ro_compat &
> +            EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) != 0)
> 
> to fix the inline related compilation errors.

Thanks for fixing this!

								Honza
diff mbox series

Patch

diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index 93ecf29c568d..5fde3343b1f1 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -1783,7 +1783,6 @@  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);
-extern int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks);
 extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
 extern __u64 ext2fs_div64_ceil(__u64 a, __u64 b);
 extern int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry);
@@ -2015,9 +2014,14 @@  _INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
 	return (blk_t) ext2fs_inode_data_blocks2(fs, inode);
 }
 
-_INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
+static inline int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
 {
-	return blocks * ((fs->blocksize - 8) / sizeof(struct ext2_dx_entry));
+	int csum_size = 0;
+
+	if (ext2fs_has_feature_metadata_csum(fs->super))
+		csum_size = sizeof(struct ext2_dx_tail);
+	return blocks * ((fs->blocksize - (8 + csum_size)) /
+						sizeof(struct ext2_dx_entry));
 }
 
 /*