summaryrefslogtreecommitdiff
path: root/fs/btrfs/fs.h
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@kernel.org>2025-12-04 23:04:54 -0800
committerDavid Sterba <dsterba@suse.com>2026-02-03 06:38:32 +0100
commitfe11ac191ce0ad910f6fda0c628bcff19fcff47d (patch)
tree892986ee59cf9604e430d5ff3641ee8ca41f3b08 /fs/btrfs/fs.h
parentb39b26e017c7889181cb84032e22bef72e81cf29 (diff)
downloadlinux-next-fe11ac191ce0ad910f6fda0c628bcff19fcff47d.tar.gz
linux-next-fe11ac191ce0ad910f6fda0c628bcff19fcff47d.zip
btrfs: switch to library APIs for checksums
Make btrfs use the library APIs instead of crypto_shash, for all checksum computations. This has many benefits: - Allows future checksum types, e.g. XXH3 or CRC64, to be more easily supported. Only a library API will be needed, not crypto_shash too. - Eliminates the overhead of the generic crypto layer, including an indirect call for every function call and other API overhead. A microbenchmark of btrfs_check_read_bio() with crc32c checksums shows a speedup from 658 cycles to 608 cycles per 4096-byte block. - Decreases the stack usage of btrfs by reducing the size of checksum contexts from 384 bytes to 240 bytes, and by eliminating the need for some functions to declare a checksum context at all. - Increases reliability. The library functions always succeed and return void. In contrast, crypto_shash can fail and return errors. Also, the library functions are guaranteed to be available when btrfs is loaded; there's no longer any need to use module softdeps to try to work around the crypto modules sometimes not being loaded. - Fixes a bug where blake2b checksums didn't work on kernels booted with fips=1. Since btrfs checksums are for integrity only, it's fine for them to use non-FIPS-approved algorithms. Note that with having to handle 4 algorithms instead of just 1-2, this commit does result in a slightly positive diffstat. That being said, this wouldn't have been the case if btrfs had actually checked for errors from crypto_shash, which technically it should have been doing. Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Neal Gompa <neal@gompa.dev> Signed-off-by: Eric Biggers <ebiggers@kernel.org> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'fs/btrfs/fs.h')
-rw-r--r--fs/btrfs/fs.h23
1 files changed, 18 insertions, 5 deletions
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 8ffbc40ebe45..458a24206935 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -3,6 +3,8 @@
#ifndef BTRFS_FS_H
#define BTRFS_FS_H
+#include <crypto/blake2b.h>
+#include <crypto/sha2.h>
#include <linux/blkdev.h>
#include <linux/sizes.h>
#include <linux/time64.h>
@@ -24,6 +26,7 @@
#include <linux/wait_bit.h>
#include <linux/sched.h>
#include <linux/rbtree.h>
+#include <linux/xxhash.h>
#include <uapi/linux/btrfs.h>
#include <uapi/linux/btrfs_tree.h>
#include "extent-io-tree.h"
@@ -35,7 +38,6 @@ struct inode;
struct super_block;
struct kobject;
struct reloc_control;
-struct crypto_shash;
struct ulist;
struct btrfs_device;
struct btrfs_block_group;
@@ -850,9 +852,10 @@ struct btrfs_fs_info {
u32 sectorsize_bits;
u32 block_min_order;
u32 block_max_order;
+ u32 stripesize;
u32 csum_size;
u32 csums_per_leaf;
- u32 stripesize;
+ u32 csum_type;
/*
* Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
@@ -864,8 +867,6 @@ struct btrfs_fs_info {
spinlock_t swapfile_pins_lock;
struct rb_root swapfile_pins;
- struct crypto_shash *csum_shash;
-
/* Type of exclusive operation running, protected by super_lock */
enum btrfs_exclusive_operation exclusive_operation;
@@ -1057,8 +1058,20 @@ int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
u16 btrfs_csum_type_size(u16 type);
int btrfs_super_csum_size(const struct btrfs_super_block *s);
const char *btrfs_super_csum_name(u16 csum_type);
-const char *btrfs_super_csum_driver(u16 csum_type);
size_t __attribute_const__ btrfs_get_num_csums(void);
+struct btrfs_csum_ctx {
+ u16 csum_type;
+ union {
+ u32 crc32;
+ struct xxh64_state xxh64;
+ struct sha256_ctx sha256;
+ struct blake2b_ctx blake2b;
+ };
+};
+void btrfs_csum(u16 csum_type, const u8 *data, size_t len, u8 *out);
+void btrfs_csum_init(struct btrfs_csum_ctx *ctx, u16 csum_type);
+void btrfs_csum_update(struct btrfs_csum_ctx *ctx, const u8 *data, size_t len);
+void btrfs_csum_final(struct btrfs_csum_ctx *ctx, u8 *out);
static inline bool btrfs_is_empty_uuid(const u8 *uuid)
{