shithub: lwext4

Download patch

ref: b6fafb2a2ce5207d032594a80ce3cdd33bcc34b6
parent: cdf912a89fb48d1bf9dead24d2948a46fe199a70
author: ngkaho1234 <[email protected]>
date: Wed Oct 28 14:14:22 EDT 2015

METADATA_CSUM: block bitmap verification added

--- a/lwext4/ext4_balloc.c
+++ b/lwext4/ext4_balloc.c
@@ -121,6 +121,34 @@
 
 }
 
+#if CONFIG_META_CSUM_ENABLE
+static bool
+ext4_balloc_verify_bitmap_csum(struct ext4_sblock *sb,
+			       struct ext4_bgroup *bg,
+			       void *bitmap __unused)
+{
+
+	if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
+		return true;
+
+	int desc_size = ext4_sb_get_desc_size(sb);
+	uint32_t checksum = ext4_balloc_bitmap_csum(sb, bitmap);
+	uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
+		 hi_checksum = to_le16(checksum >> 16);
+	
+	if (bg->block_bitmap_csum_lo != lo_checksum)
+		return false;
+
+	if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
+		if (bg->block_bitmap_csum_hi != hi_checksum)
+			return false;
+
+	return true;
+}
+#else
+#define ext4_balloc_verify_bitmap_csum(...) true
+#endif
+
 int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, ext4_fsblk_t baddr)
 {
 	struct ext4_fs *fs = inode_ref->fs;
@@ -147,6 +175,15 @@
 		return rc;
 	}
 
+	if (!ext4_balloc_verify_bitmap_csum(sb,
+			       bg_ref.block_group,
+			       bitmap_block.data)) {
+		ext4_dbg(DEBUG_BALLOC,
+			DBG_WARN "Bitmap checksum failed."
+			"Group: %" PRIu32"\n",
+			bg_ref.index);
+	}
+
 	/* Modify bitmap */
 	ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
 	ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group,
@@ -231,6 +268,15 @@
 			return rc;
 		}
 
+		if (!ext4_balloc_verify_bitmap_csum(sb,
+				       bg_ref.block_group,
+				       bitmap_block.data)) {
+			ext4_dbg(DEBUG_BALLOC,
+				DBG_WARN "Bitmap checksum failed."
+				"Group: %" PRIu32"\n",
+				bg_ref.index);
+		}
+
 		uint32_t free_cnt =
 		    ext4_sb_get_block_size(sb) * 8 - index_in_group_first;
 
@@ -337,6 +383,15 @@
 		return rc;
 	}
 
+	if (!ext4_balloc_verify_bitmap_csum(sb,
+			       bg_ref.block_group,
+			       bitmap_block.data)) {
+		ext4_dbg(DEBUG_BALLOC,
+			DBG_WARN "Bitmap checksum failed."
+			"Group: %" PRIu32"\n",
+			bg_ref.index);
+	}
+
 	/* Check if goal is free */
 	if (ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group)) {
 		ext4_bmap_bit_set(bitmap_block.data, index_in_group);
@@ -442,6 +497,15 @@
 			return rc;
 		}
 
+		if (!ext4_balloc_verify_bitmap_csum(sb,
+				       bg_ref.block_group,
+				       bitmap_block.data)) {
+			ext4_dbg(DEBUG_BALLOC,
+				DBG_WARN "Bitmap checksum failed."
+				"Group: %" PRIu32"\n",
+				bg_ref.index);
+		}
+
 		/* Compute indexes */
 		first_in_group = ext4_balloc_get_block_of_bgid(sb, bgid);
 		index_in_group =
@@ -554,6 +618,15 @@
 	if (rc != EOK) {
 		ext4_fs_put_block_group_ref(&bg_ref);
 		return rc;
+	}
+
+	if (!ext4_balloc_verify_bitmap_csum(sb,
+			       bg_ref.block_group,
+			       bitmap_block.data)) {
+		ext4_dbg(DEBUG_BALLOC,
+			DBG_WARN "Bitmap checksum failed."
+			"Group: %" PRIu32"\n",
+			bg_ref.index);
 	}
 
 	/* Check if block is free */