ext4_extent_full.c: print warning messages when extent block checksum verification...
[lwext4.git] / lwext4 / ext4_balloc.c
index cd7119180be976ea000a534d73f78a3e4235d616..3fd29b2077fba1a4ebf13467733316bfade9f8f6 100644 (file)
@@ -78,37 +78,39 @@ uint64_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s,
        return baddr;
 }
 
+#if CONFIG_META_CSUM_ENABLE
 static uint32_t ext4_balloc_bitmap_csum(struct ext4_sblock *sb,
                                        void *bitmap)
 {
        uint32_t checksum = 0;
-       if (ext4_sb_has_feature_read_only(sb,
-                               EXT4_FRO_COM_METADATA_CSUM)) {
+       if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
                uint32_t blocks_per_group =
                        ext4_get32(sb, blocks_per_group);
 
                /* First calculate crc32 checksum against fs uuid */
-               checksum = ext4_crc32c(~0, sb->uuid, sizeof(sb->uuid));
+               checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
+                               sizeof(sb->uuid));
                /* Then calculate crc32 checksum against block_group_desc */
                checksum = ext4_crc32c(checksum, bitmap,
                                     blocks_per_group / 8);
        }
        return checksum;
 }
-
-/*
- * BIG FAT NOTES:
- *       Currently we do not verify the checksum of bitmaps.
- */
+#else
+#define ext4_balloc_bitmap_csum(...) 0
+#endif
 
 void ext4_balloc_set_bitmap_csum(struct ext4_sblock *sb,
                                 struct ext4_bgroup *bg,
-                                void *bitmap)
+                                void *bitmap __unused)
 {
        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 (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
+               return;
        
        /* See if we need to assign a 32bit checksum */
        bg->block_bitmap_csum_lo = lo_checksum;
@@ -117,6 +119,33 @@ void ext4_balloc_set_bitmap_csum(struct ext4_sblock *sb,
 
 }
 
+#if CONFIG_META_CSUM_ENABLE
+static bool
+ext4_balloc_verify_bitmap_csum(struct ext4_sblock *sb,
+                              struct ext4_bgroup *bg,
+                              void *bitmap __unused)
+{
+       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 (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
+               return true;
+       
+       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;
@@ -143,6 +172,15 @@ int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, ext4_fsblk_t baddr)
                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,
@@ -196,8 +234,7 @@ int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t first
        uint32_t block_group_last =
            ext4_balloc_get_bgid_of_block(sb, first + count - 1);
 
-       if (!ext4_sb_has_feature_incompatible(sb,
-                                             EXT4_FINCOM_FLEX_BG)) {
+       if (!ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG)) {
                /*It is not possible without flex_bg that blocks are continuous
                 * and and last block belongs to other bg.*/
                ext4_assert(block_group_first == ext4_balloc_get_bgid_of_block(
@@ -228,6 +265,15 @@ int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t first
                        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;
 
@@ -334,6 +380,15 @@ int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_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 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);
@@ -439,6 +494,15 @@ goal_failed:
                        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 =
@@ -553,6 +617,15 @@ int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_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 */
        *free = ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group);