Skip to content

Commit 0441984

Browse files
Darrick J. Wongtytso
authored andcommitted
ext4: load the crc32c driver if necessary
Obtain a reference to the cryptoapi and crc32c if we mount a filesystem with metadata checksumming enabled. Signed-off-by: Darrick J. Wong <djwong@us.ibm.com> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent d25425f commit 0441984

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎fs/ext4/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ config EXT4_FS
22
tristate "The Extended 4 (ext4) filesystem"
33
select JBD2
44
select CRC16
5+
select CRYPTO
6+
select CRYPTO_CRC32C
57
help
68
This is the next generation of the ext3 filesystem.
79

‎fs/ext4/ext4.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/wait.h>
3030
#include <linux/blockgroup_lock.h>
3131
#include <linux/percpu_counter.h>
32+
#include <crypto/hash.h>
3233
#ifdef __KERNEL__
3334
#include <linux/compat.h>
3435
#endif
@@ -1276,6 +1277,9 @@ struct ext4_sb_info {
12761277

12771278
/* record the last minlen when FITRIM is called. */
12781279
atomic_t s_last_trim_minblks;
1280+
1281+
/* Reference to checksum algorithm driver via cryptoapi */
1282+
struct crypto_shash *s_chksum_driver;
12791283
};
12801284

12811285
static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1639,6 +1643,25 @@ static inline __le16 ext4_rec_len_to_disk(unsigned len, unsigned blocksize)
16391643
#define DX_HASH_HALF_MD4_UNSIGNED 4
16401644
#define DX_HASH_TEA_UNSIGNED 5
16411645

1646+
static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
1647+
const void *address, unsigned int length)
1648+
{
1649+
struct {
1650+
struct shash_desc shash;
1651+
char ctx[crypto_shash_descsize(sbi->s_chksum_driver)];
1652+
} desc;
1653+
int err;
1654+
1655+
desc.shash.tfm = sbi->s_chksum_driver;
1656+
desc.shash.flags = 0;
1657+
*(u32 *)desc.ctx = crc;
1658+
1659+
err = crypto_shash_update(&desc.shash, address, length);
1660+
BUG_ON(err);
1661+
1662+
return *(u32 *)desc.ctx;
1663+
}
1664+
16421665
#ifdef __KERNEL__
16431666

16441667
/* hash info structure used by the directory hash */

‎fs/ext4/super.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ static void ext4_put_super(struct super_block *sb)
915915
unlock_super(sb);
916916
kobject_put(&sbi->s_kobj);
917917
wait_for_completion(&sbi->s_kobj_unregister);
918+
if (sbi->s_chksum_driver)
919+
crypto_free_shash(sbi->s_chksum_driver);
918920
kfree(sbi->s_blockgroup_lock);
919921
kfree(sbi);
920922
}
@@ -3043,6 +3045,18 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
30433045
goto cantfind_ext4;
30443046
}
30453047

3048+
/* Load the checksum driver */
3049+
if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3050+
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3051+
sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3052+
if (IS_ERR(sbi->s_chksum_driver)) {
3053+
ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3054+
ret = PTR_ERR(sbi->s_chksum_driver);
3055+
sbi->s_chksum_driver = NULL;
3056+
goto failed_mount;
3057+
}
3058+
}
3059+
30463060
/* Set defaults before we parse the mount options */
30473061
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
30483062
set_opt(sb, INIT_INODE_TABLE);
@@ -3728,6 +3742,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
37283742
brelse(sbi->s_group_desc[i]);
37293743
ext4_kvfree(sbi->s_group_desc);
37303744
failed_mount:
3745+
if (sbi->s_chksum_driver)
3746+
crypto_free_shash(sbi->s_chksum_driver);
37313747
if (sbi->s_proc) {
37323748
remove_proc_entry("options", sbi->s_proc);
37333749
remove_proc_entry(sb->s_id, ext4_proc_root);

0 commit comments

Comments
 (0)