libext2fs: teach the ext2fs_*_block_bitmap_range2() about clusters

The ext2fs_{mark,unmark,test}_block_bitmap2() functions understand
about clusters, and will take block numbers and convert them to
clusters before checking the bitmap.  The
ext2fs_*_block_bitmap_range2() functions did not do this, which made
them inconsistent.  Fortunately, nothing has depended on this
incorrect behavior, and in fact most of the usage of these functions
have only recently been added, and only for optimizations that were
only enabled for non-bigalloc file systems.

So this is a change in previously exported functions, but (a) it
doesn't change the behavior at all for non-bigalloc file systems, and
(b) the change is more likely to fix bugs for bigalloc file systems.
For example, this change fixes a problem with resize2fs and bigalloc
file systems.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
diff --git a/lib/ext2fs/gen_bitmap64.c b/lib/ext2fs/gen_bitmap64.c
index 07d6d52..82a552d 100644
--- a/lib/ext2fs/gen_bitmap64.c
+++ b/lib/ext2fs/gen_bitmap64.c
@@ -624,6 +624,8 @@
 int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap bmap,
 				    blk64_t block, unsigned int num)
 {
+	__u64	end = block + num;
+
 	if (!bmap)
 		return EINVAL;
 
@@ -646,12 +648,26 @@
 
 	INC_STAT(bmap, test_ext_count);
 
+	/* 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+num-1 > bmap->end)) {
+		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST, block,
+				   bmap->description);
+		return;
+	}
+
 	return bmap->bitmap_ops->test_clear_bmap_extent(bmap, block, num);
 }
 
 void ext2fs_mark_block_bitmap_range2(ext2fs_block_bitmap bmap,
 				     blk64_t block, unsigned int num)
 {
+	__u64	end = block + num;
+
 	if (!bmap)
 		return;
 
@@ -670,6 +686,12 @@
 
 	INC_STAT(bmap, mark_ext_count);
 
+	/* 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+num-1 > bmap->end)) {
 		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
 				   bmap->description);
@@ -682,6 +704,8 @@
 void ext2fs_unmark_block_bitmap_range2(ext2fs_block_bitmap bmap,
 				       blk64_t block, unsigned int num)
 {
+	__u64	end = block + num;
+
 	if (!bmap)
 		return;
 
@@ -700,6 +724,12 @@
 
 	INC_STAT(bmap, unmark_ext_count);
 
+	/* 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+num-1 > bmap->end)) {
 		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
 				   bmap->description);