METADATA_CSUM: inode bitmap verification added
authorngkaho1234 <ngkaho1234@gmail.com>
Wed, 28 Oct 2015 10:14:36 +0000 (18:14 +0800)
committerngkaho1234 <ngkaho1234@gmail.com>
Wed, 28 Oct 2015 10:14:36 +0000 (18:14 +0800)
lwext4/ext4_ialloc.c

index 2b1d49a73b918b69d4b5051b010f7add48494f85..7bafea74a6bef3e316fdd2cbbc2cd2a55ee0c822 100644 (file)
@@ -129,6 +129,34 @@ void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb,
 
 }
 
+#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 @@ int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
        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);
@@ -242,6 +279,15 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
                                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 */
                        uint32_t inodes_in_group =
                            ext4_inodes_in_group_cnt(sb, bgid);