diff mbox series

[RFCv1,17/72] libext2fs: Add support for ext2fs_test_block_bitmap_range2_valid()

Message ID 9015303c19c1b3474d880409db60627b0a9de37f.1667822611.git.ritesh.list@gmail.com
State Under Review
Delegated to: Theodore Ts'o
Headers show
Series e2fsprogs: Parallel fsck support | expand

Commit Message

Ritesh Harjani (IBM) Nov. 7, 2022, 12:21 p.m. UTC
From: Wang Shilong <wshilong@ddn.com>

This adds the support in libext2fs to query whether the block range is
valid or not (within range) given the block bitmap.
Also to avoid duplicate warning messages in case of invalid blocks.

This will be later used in pass1 of e2fsck is_blocks_used() function to
check whether the given block range is valid or not to avoid duplicate
warning resulting from ext2fs_test_block_bitmap_range2()

Signed-off-by: Wang Shilong <wshilong@ddn.com>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
---
 lib/ext2fs/bitops.h       |  2 ++
 lib/ext2fs/gen_bitmap64.c | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Comments

Andreas Dilger Dec. 14, 2022, 9:21 p.m. UTC | #1
On Nov 7, 2022, at 5:21 AM, Ritesh Harjani (IBM) <ritesh.list@gmail.com> wrote:
> 
> From: Wang Shilong <wshilong@ddn.com>
> 
> This adds the support in libext2fs to query whether the block range is
> valid or not (within range) given the block bitmap.
> Also to avoid duplicate warning messages in case of invalid blocks.
> 
> This will be later used in pass1 of e2fsck is_blocks_used() function to
> check whether the given block range is valid or not to avoid duplicate
> warning resulting from ext2fs_test_block_bitmap_range2()
> 
> Signed-off-by: Wang Shilong <wshilong@ddn.com>
> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>

I don't think this patch is correct?

> ---
> diff --git a/lib/ext2fs/gen_bitmap64.c b/lib/ext2fs/gen_bitmap64.c
> index c31f942f..a9637cb5 100644
> --- a/lib/ext2fs/gen_bitmap64.c
> +++ b/lib/ext2fs/gen_bitmap64.c
> @@ -731,6 +731,39 @@ int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap gen_bmap,
> 	return bmap->bitmap_ops->test_clear_bmap_extent(bmap, block, num);
> }
> 
> +int ext2fs_test_block_bitmap_range2_valid(ext2fs_block_bitmap bitmap,
> +					  blk64_t block, unsigned int num)
> +{
> +	ext2fs_generic_bitmap_64 bmap = (ext2fs_generic_bitmap_64)bitmap;
> +	__u64	end = block + num;
> +
> +	if (!bmap)
> +		return 0;
> +
> +	if (EXT2FS_IS_32_BITMAP(bmap)) {
> +		if ((block & ~0xffffffffULL) ||
> +		    ((block+num-1) & ~0xffffffffULL)) {
> +			return 0;
> +		}
> +	}

This is bailing out early if the requested bit is > 2^32, but that is
before cluster conversion below.  However, I think the bitmap is actually
stored in clusters, so the 2^32 check seems premature?

> +
> +	if (!EXT2FS_IS_64_BITMAP(bmap))
> +		return 0;
> +
> +	/* convert to clusters if necessary */
> +	block >>= bmap->cluster_bits;
> +	end += (1 << bmap->cluster_bits) - 1;
> +	end >>= bmap->cluster_bits;
> +	num = end - block;
> +
> +	if ((block < bmap->start) || (block > bmap->end) ||
> +	    (block+num-1 > bmap->end))
> +		return 0;
> +
> +	return 1;
> +}

Cheers, Andreas
diff mbox series

Patch

diff --git a/lib/ext2fs/bitops.h b/lib/ext2fs/bitops.h
index 505b3c9c..1facc8dd 100644
--- a/lib/ext2fs/bitops.h
+++ b/lib/ext2fs/bitops.h
@@ -120,6 +120,8 @@  extern int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
 extern void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map);
 extern __u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap);
 extern __u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap);
+extern int ext2fs_test_block_bitmap_range2_valid(ext2fs_block_bitmap bitmap,
+						blk64_t block, unsigned int num);
 
 /* 64-bit versions */
 
diff --git a/lib/ext2fs/gen_bitmap64.c b/lib/ext2fs/gen_bitmap64.c
index c31f942f..a9637cb5 100644
--- a/lib/ext2fs/gen_bitmap64.c
+++ b/lib/ext2fs/gen_bitmap64.c
@@ -731,6 +731,39 @@  int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap gen_bmap,
 	return bmap->bitmap_ops->test_clear_bmap_extent(bmap, block, num);
 }
 
+int ext2fs_test_block_bitmap_range2_valid(ext2fs_block_bitmap bitmap,
+					  blk64_t block, unsigned int num)
+{
+	ext2fs_generic_bitmap_64 bmap = (ext2fs_generic_bitmap_64)bitmap;
+	__u64	end = block + num;
+
+	if (!bmap)
+		return 0;
+
+	if (EXT2FS_IS_32_BITMAP(bmap)) {
+		if ((block & ~0xffffffffULL) ||
+		    ((block+num-1) & ~0xffffffffULL)) {
+			return 0;
+		}
+	}
+
+	if (!EXT2FS_IS_64_BITMAP(bmap))
+		return 0;
+
+	/* convert to clusters if necessary */
+	block >>= bmap->cluster_bits;
+	end += (1 << bmap->cluster_bits) - 1;
+	end >>= bmap->cluster_bits;
+	num = end - block;
+
+	if ((block < bmap->start) || (block > bmap->end) ||
+	    (block+num-1 > bmap->end))
+		return 0;
+
+	return 1;
+}
+
+
 void ext2fs_mark_block_bitmap_range2(ext2fs_block_bitmap gen_bmap,
 				     blk64_t block, unsigned int num)
 {