+#if CONFIG_META_CSUM_ENABLE
+static uint32_t
+ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref,
+ ext4_fsblk_t blocknr,
+ struct ext4_xattr_header *header)
+{
+ uint32_t checksum = 0;
+ uint64_t le64_blocknr = blocknr;
+ struct ext4_sblock *sb = &inode_ref->fs->sb;
+
+ if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
+ uint32_t orig_checksum;
+
+ /* Preparation: temporarily set bg checksum to 0 */
+ orig_checksum = header->h_checksum;
+ header->h_checksum = 0;
+ /* First calculate crc32 checksum against fs uuid */
+ checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
+ sizeof(sb->uuid));
+ /* Then calculate crc32 checksum block number */
+ checksum = ext4_crc32c(checksum, &le64_blocknr,
+ sizeof(le64_blocknr));
+ /* Finally calculate crc32 checksum against
+ * the entire xattr block */
+ checksum = ext4_crc32c(checksum, header,
+ ext4_sb_get_block_size(sb));
+ header->h_checksum = orig_checksum;
+ }
+ return checksum;
+}
+#else
+#define ext4_xattr_block_checksum(...) 0
+#endif
+
+static void
+ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref,
+ ext4_fsblk_t blocknr __unused,
+ struct ext4_xattr_header *header)
+{
+ struct ext4_sblock *sb = &inode_ref->fs->sb;
+ if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
+ return;
+
+ header->h_checksum =
+ ext4_xattr_block_checksum(inode_ref, blocknr, header);
+}
+