shithub: lwext4

Download patch

ref: 3e1d0ec133eb36e703aad6243cf31aaadef57570
parent: b6fafb2a2ce5207d032594a80ce3cdd33bcc34b6
author: ngkaho1234 <ngkaho1234@gmail.com>
date: Wed Oct 28 14:14:36 EDT 2015

METADATA_CSUM: inode bitmap verification added

--- a/lwext4/ext4_ialloc.c
+++ b/lwext4/ext4_ialloc.c
@@ -129,6 +129,34 @@
 
 }
 
+#if CONFIG_META_CSUM_ENABLE
+static bool
+ext4_ialloc_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_ialloc_bitmap_csum(sb, bitmap);
+	uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
+		 hi_checksum = to_le16(checksum >> 16);
+	
+	if (bg->inode_bitmap_csum_lo != lo_checksum)
+		return false;
+
+	if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
+		if (bg->inode_bitmap_csum_hi != hi_checksum)
+			return false;
+
+	return true;
+}
+#else
+#define ext4_balloc_verify_bitmap_csum(...) true
+#endif
+
 int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
 {
 	struct ext4_sblock *sb = &fs->sb;
@@ -150,6 +178,15 @@
 	if (rc != EOK)
 		return rc;
 
+	if (!ext4_ialloc_verify_bitmap_csum(sb,
+			       bg_ref.block_group,
+			       bitmap_block.data)) {
+		ext4_dbg(DEBUG_IALLOC,
+			DBG_WARN "Bitmap checksum failed."
+			"Group: %" PRIu32"\n",
+			bg_ref.index);
+	}
+
 	/* Free i-node in the bitmap */
 	uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
 	ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
@@ -240,6 +277,15 @@
 			if (rc != EOK) {
 				ext4_fs_put_block_group_ref(&bg_ref);
 				return rc;
+			}
+
+			if (!ext4_ialloc_verify_bitmap_csum(sb,
+					       bg_ref.block_group,
+					       bitmap_block.data)) {
+				ext4_dbg(DEBUG_IALLOC,
+					DBG_WARN "Bitmap checksum failed."
+					"Group: %" PRIu32"\n",
+					bg_ref.index);
 			}
 
 			/* Try to allocate i-node in the bitmap */