Skip to contentSkip to conversation

fs: ext4: Add metadata checksums support

PATCH New Fredrik Hallenberg 2 participants 6 messages Feb 12, 2021 – Apr 7, 2021 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

Support crc32c checksums in ext4 filesystems with metadata_csum flag active. This includes superblock, inodes, inode and block group tables, directory blocks and journal.

---
 fs/ext4/Kconfig                   |   1 +
 fs/ext4/ext4_common.c             |  91 ++++++++++-------
 fs/ext4/ext4_common.h             |   7 +-
 fs/ext4/ext4_journal.c            | 135 ++++++++++++++++++++-----
 fs/ext4/ext4_journal.h            |  40 +++++++-
 fs/ext4/ext4_write.c              | 162 ++++++++++++++++++++++++++++--
 include/ext4fs.h                  |   7 ++
 include/ext_common.h              |  77 +++++++++++++-
 test/py/tests/test_env.py         |   3 -
 test/py/tests/test_fs/conftest.py |   4 -
 10 files changed, 441 insertions(+), 86 deletions(-)
fs/ext4/Kconfig +1 -0
index 1a913d2b6d..da033428b2 100644
@@ -8,6 +8,7 @@ config FS_EXT4
 config EXT4_WRITE
 	bool "Enable ext4 filesystem write support"
 	depends on FS_EXT4
+	select CRC32C
 	help
 	  This provides support for creating and writing new files to an
 	  existing ext4 filesystem partition.
fs/ext4/ext4_common.c +55 -36

187 lines not on this page · raw message

fs/ext4/ext4_common.h +6 -1
index beaee9c80b..afcd9a072d 100644
@@ -41,6 +41,8 @@
 #define SUPERBLOCK_SIZE	1024
 #define F_FILE			1
 
+#define EXT4_OLD_INODE_SIZE 128
+
 static inline void *zalloc(size_t size)
 {
 	void *p = memalign(ARCH_DMA_MINALIGN, size);
@@ -59,7 +61,6 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
 
 #if defined(CONFIG_EXT4_WRITE)
 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
-uint16_t ext4fs_checksum_update(unsigned int i);
 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags);
 int ext4fs_update_parent_dentry(char *filename, int file_type);
 uint32_t ext4fs_get_new_blk_no(void);
@@ -86,5 +87,9 @@ uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb);
 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks);
 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
 	const struct ext_filesystem *fs);
+void ext4fs_set_superblock_csum(struct ext2_sblock *sb);
+uint32_t ext4_csum(uint32_t crc, const uint8_t *data, unsigned int length);
+void ext4fs_set_journal_superblock_csum(struct journal_superblock_t *sb);
+
 #endif
 #endif
fs/ext4/ext4_journal.c +109 -26

244 lines not on this page · raw message

fs/ext4/ext4_journal.h +38 -2
index 43fb8e7664..9bdbf66971 100644
@@ -22,7 +22,6 @@
 #define EXT2_JOURNAL_INO		8	/* Journal inode */
 #define EXT2_JOURNAL_SUPERBLOCK	0	/* Journal  Superblock number */
 
-#define JBD2_FEATURE_COMPAT_CHECKSUM	0x00000001
 #define EXT3_JOURNAL_MAGIC_NUMBER	0xc03b3998U
 #define TRANSACTION_RUNNING		1
 #define TRANSACTION_COMPLETE		0
@@ -37,6 +36,14 @@
 #define EXT3_JOURNAL_FLAG_DELETED	4
 #define EXT3_JOURNAL_FLAG_LAST_TAG	8
 
+#define JBD2_FEATURE_COMPAT_CHECKSUM		0x00000001
+
+#define JBD2_FEATURE_INCOMPAT_REVOKE		0x00000001
+#define JBD2_FEATURE_INCOMPAT_64BIT		0x00000002
+#define JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT	0x00000004
+#define JBD2_FEATURE_INCOMPAT_CSUM_V2		0x00000008
+#define JBD2_FEATURE_INCOMPAT_CSUM_V3		0x00000010
+
 /* Maximum entries in 1 journal transaction */
 #define MAX_JOURNAL_ENTRIES 100
 struct journal_log {
@@ -90,16 +97,45 @@ struct journal_superblock_t {
 	__be32 s_max_trans_data;	/* Limit of data blocks per trans. */
 
 	/* 0x0050 */
-	__be32 s_padding[44];
+	__u8 s_checksum_type;	/* checksum type */
+	__u8 s_padding2[3];
+	__u32 s_padding[42];
+	__be32 s_checksum;	/* crc32c(superblock) */
 
 	/* 0x0100 */
 	__u8 s_users[16 * 48];	/* ids of all fs'es sharing the log */
 	/* 0x0400 */
 } ;
 
+struct ext3_journal_commit_header {
+	__be32 h_magic;
+	__be32 h_blocktype;
+	__be32 h_sequence;
+	unsigned char h_chksum_type;
+	unsigned char h_chksum_size;
+	unsigned char h_padding[2];
+	__be32 h_chksum[8];
+	__be64 h_commit_sec;
+	__be32 h_commit_nsec;
+};
+
 struct ext3_journal_block_tag {
+	__be32 block;
+	__be16 checksum;	/* truncated crc32c(uuid+seq+block) */
+	__be16 flags;
+	__be32 blocknr_high;	/* only used when INCOMPAT_64BIT is set */
+};
+
+/* Use if FEATURE_INCOMPAT_CSUM_V3 is set */
+struct ext3_journal_block_tag3 {
 	__be32 block;
 	__be32 flags;
+	__be32 blocknr_high;
+	__be32 checksum;	/* crc32c(uuid+seq+block) */
+};
+
+struct ext3_journal_block_tail {
+	__be32 t_checksum;	/* crc32c(uuid+descr_block) */
 };
 
 struct journal_revoke_header_t {
fs/ext4/ext4_write.c +151 -11

220 lines not on this page · raw message

include/ext4fs.h +7 -0
index cb5d9cc0a5..5485388166 100644
@@ -37,6 +37,7 @@ struct disk_partition;
 #define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM 0x0400
 #define EXT4_FEATURE_INCOMPAT_EXTENTS	0x0040
 #define EXT4_FEATURE_INCOMPAT_64BIT	0x0080
+#define EXT4_FEATURE_INCOMPAT_CSUM_SEED	0x2000
 #define EXT4_INDIRECT_BLOCKS		12
 
 #define EXT4_BG_INODE_UNINIT		0x0001
@@ -117,6 +118,11 @@ struct ext_filesystem {
 
 	/* Block Device Descriptor */
 	struct blk_desc *dev_desc;
+
+	uint32_t csum_seed;
+	uint32_t journal_csum_seed;
+	uint32_t journal_tag_size;
+	int journal_csum_version;
 };
 
 struct ext_block_cache {
@@ -130,6 +136,7 @@ extern struct ext2fs_node *ext4fs_file;
 
 #if defined(CONFIG_EXT4_WRITE)
 extern struct ext2_inode *g_parent_inode;
+extern int g_parent_inode_no;
 extern int gd_index;
 extern int gindex;
 
include/ext_common.h +74 -3

116 lines not on this page · raw message

test/py/tests/test_env.py +0 -3
index 940279651d..847b4fbcdd 100644
@@ -417,9 +417,6 @@ def mk_env_ext4(state_test_env):
         try:
             u_boot_utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent)
             u_boot_utils.run_and_log(c, 'mkfs.ext4 %s' % persistent)
-            sb_content = u_boot_utils.run_and_log(c, 'tune2fs -l %s' % persistent)
-            if 'metadata_csum' in sb_content:
-                u_boot_utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
         except CalledProcessError:
             call('rm -f %s' % persistent, shell=True)
             raise
test/py/tests/test_fs/conftest.py +0 -4
index ec70e8c4ef..272cbb639c 100644
@@ -165,10 +165,6 @@ def mk_fs(config, fs_type, size, id):
             % (fs_img, count), shell=True)
         check_call('mkfs.%s %s %s'
             % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
-        if fs_type == 'ext4':
-            sb_content = check_output('tune2fs -l %s' % fs_img, shell=True).decode()
-            if 'metadata_csum' in sb_content:
-                check_call('tune2fs -O ^metadata_csum %s' % fs_img, shell=True)
         return fs_img
     except CalledProcessError:
         call('rm -f %s' % fs_img, shell=True)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

Quoted 5 lines · Fredrik Hallenberg

Support crc32c checksums in ext4 filesystems with metadata_csum flag active. This includes superblock, inodes, inode and block group tables, directory blocks and journal.

I've initially put this in a local branch and run the fs tests on sandbox both with default options and forcing metadata_csum to be enabled, and it worked in both cases. Thanks for posting the patch! Can you let us know on what systems you've tested this?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210212/6eeb8599/attachment.sig>
#2 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

Thanks for trying it out. I have only used it on a custom arm-based board. I have done quite a lot of writing with and without metadata flag and have also tried to trigger errors that require journal recovery. It has worked well but of course it is hard to be sure that the code will handle every case.

Quoted 16 lines · Tom Rini
Quoted 5 lines · Fredrik Hallenberg

Support crc32c checksums in ext4 filesystems with metadata_csum flag active. This includes superblock, inodes, inode and block group tables, directory blocks and journal.

I've initially put this in a local branch and run the fs tests on sandbox both with default options and forcing metadata_csum to be enabled, and it worked in both cases. Thanks for posting the patch! Can you let us know on what systems you've tested this?

--
Tom

#3 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

Anything I can do to improve the patch?

Quoted 24 lines · Fredrik Hallenberg

Thanks for trying it out. I have only used it on a custom arm-based board. I have done quite a lot of writing with and without metadata flag and have also tried to trigger errors that require journal recovery. It has worked well but of course it is hard to be sure that the code will handle every case.

Quoted 16 lines · Tom Rini
Quoted 5 lines · Fredrik Hallenberg

Support crc32c checksums in ext4 filesystems with metadata_csum flag active. This includes superblock, inodes, inode and block group tables, directory blocks and journal.

I've initially put this in a local branch and run the fs tests on sandbox both with default options and forcing metadata_csum to be enabled, and it worked in both cases. Thanks for posting the patch! Can you let us know on what systems you've tested this?

--
Tom

#4 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

Quoted 1 line · Fredrik Hallenberg

Anything I can do to improve the patch?

Barring further comments from others, I will put this in to -next when I open that merge window after the next -rc release. Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210222/8444a036/attachment.sig>
#5 lore raw

PATCH 1/1 fs: ext4: Add metadata checksums support

In reply to Fredrik Hallenberg
Quoted 5 lines · Fredrik Hallenberg

Support crc32c checksums in ext4 filesystems with metadata_csum flag active. This includes superblock, inodes, inode and block group tables, directory blocks and journal.

This fails broadly with an error like:
aarch64: + ls1046ardb_qspi
+(ls1046ardb_qspi) In file included from ../include/linux/bitops.h:22,

+(ls1046ardb_qspi)                  from include/asm/arch/config.h:14,
+(ls1046ardb_qspi)                  from ../include/configs/ls1046a_common.h:32,
+(ls1046ardb_qspi)                  from ../include/configs/ls1046ardb.h:10,
+(ls1046ardb_qspi)                  from include/config.h:4,
+(ls1046ardb_qspi)                  from ../include/common.h:16,
+(ls1046ardb_qspi)                  from ../fs/ext4/ext4_common.c:21:

+(ls1046ardb_qspi) ../fs/ext4/ext4_common.c: In function 'ext4fs_read_inode':
+(ls1046ardb_qspi) ../include/linux/kernel.h:184:17: error: comparison of distinct pointer types lacks a cast [-Werror]
+(ls1046ardb_qspi) 184 | (void) (&_min1 == &_min2); \
+(ls1046ardb_qspi) | ^~
+(ls1046ardb_qspi) ../fs/ext4/ext4_common.c:1655:9: note: in expansion of macro 'min' +(ls1046ardb_qspi) 1655 | size = min(sizeof(struct ext2_inode), fs->inodesz);
+(ls1046ardb_qspi) | ^~~
+(ls1046ardb_qspi) cc1: all warnings being treated as errors
+(ls1046ardb_qspi) make[3]: *** [fs/ext4/ext4_common.o] Error 1
+(ls1046ardb_qspi) make[2]: *** [fs/ext4] Error 2
+(ls1046ardb_qspi) make[1]: *** [fs] Error 2
+(ls1046ardb_qspi) make: *** [sub-make] Error 2

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210407/31a03cb6/attachment.sig>