Skip to content

Commit 6d97c6e

Browse files
Johannes Thumshirnkdave
authored andcommitted
btrfs: add boilerplate code for directly including the crypto framework
Add boilerplate code for directly including the crypto framework. This helps us flipping the switch for new algorithms. Reviewed-by: Nikolay Borisov <[email protected]> Signed-off-by: Johannes Thumshirn <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 51bce6c commit 6d97c6e

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

fs/btrfs/ctree.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct btrfs_ref;
7373

7474
/* four bytes for CRC32 */
7575
static const int btrfs_csum_sizes[] = { 4 };
76+
static const char *btrfs_csum_names[] = { "crc32c" };
7677

7778
#define BTRFS_EMPTY_DIR_SIZE 0
7879

@@ -1163,6 +1164,8 @@ struct btrfs_fs_info {
11631164
spinlock_t swapfile_pins_lock;
11641165
struct rb_root swapfile_pins;
11651166

1167+
struct crypto_shash *csum_shash;
1168+
11661169
#ifdef CONFIG_BTRFS_FS_REF_VERIFY
11671170
spinlock_t ref_verify_lock;
11681171
struct rb_root block_tree;
@@ -2454,6 +2457,11 @@ static inline int btrfs_super_csum_size(const struct btrfs_super_block *s)
24542457
return btrfs_csum_sizes[t];
24552458
}
24562459

2460+
static inline const char *btrfs_super_csum_name(u16 csum_type)
2461+
{
2462+
/* csum type is validated at mount time */
2463+
return btrfs_csum_names[csum_type];
2464+
}
24572465

24582466
/*
24592467
* The leaf data grows from end-to-front in the node.

fs/btrfs/disk-io.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/crc32c.h>
2020
#include <linux/sched/mm.h>
2121
#include <asm/unaligned.h>
22+
#include <crypto/hash.h>
2223
#include "ctree.h"
2324
#include "disk-io.h"
2425
#include "transaction.h"
@@ -2256,6 +2257,29 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
22562257
return 0;
22572258
}
22582259

2260+
static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
2261+
{
2262+
struct crypto_shash *csum_shash;
2263+
const char *csum_name = btrfs_super_csum_name(csum_type);
2264+
2265+
csum_shash = crypto_alloc_shash(csum_name, 0, 0);
2266+
2267+
if (IS_ERR(csum_shash)) {
2268+
btrfs_err(fs_info, "error allocating %s hash for checksum",
2269+
csum_name);
2270+
return PTR_ERR(csum_shash);
2271+
}
2272+
2273+
fs_info->csum_shash = csum_shash;
2274+
2275+
return 0;
2276+
}
2277+
2278+
static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
2279+
{
2280+
crypto_free_shash(fs_info->csum_shash);
2281+
}
2282+
22592283
static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
22602284
struct btrfs_fs_devices *fs_devices)
22612285
{
@@ -2820,6 +2844,12 @@ int open_ctree(struct super_block *sb,
28202844
goto fail_alloc;
28212845
}
28222846

2847+
ret = btrfs_init_csum_hash(fs_info, csum_type);
2848+
if (ret) {
2849+
err = ret;
2850+
goto fail_alloc;
2851+
}
2852+
28232853
/*
28242854
* We want to check superblock checksum, the type is stored inside.
28252855
* Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
@@ -2828,7 +2858,7 @@ int open_ctree(struct super_block *sb,
28282858
btrfs_err(fs_info, "superblock checksum mismatch");
28292859
err = -EINVAL;
28302860
brelse(bh);
2831-
goto fail_alloc;
2861+
goto fail_csum;
28322862
}
28332863

28342864
/*
@@ -2865,11 +2895,11 @@ int open_ctree(struct super_block *sb,
28652895
if (ret) {
28662896
btrfs_err(fs_info, "superblock contains fatal errors");
28672897
err = -EINVAL;
2868-
goto fail_alloc;
2898+
goto fail_csum;
28692899
}
28702900

28712901
if (!btrfs_super_root(disk_super))
2872-
goto fail_alloc;
2902+
goto fail_csum;
28732903

28742904
/* check FS state, whether FS is broken. */
28752905
if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
@@ -2891,7 +2921,7 @@ int open_ctree(struct super_block *sb,
28912921
ret = btrfs_parse_options(fs_info, options, sb->s_flags);
28922922
if (ret) {
28932923
err = ret;
2894-
goto fail_alloc;
2924+
goto fail_csum;
28952925
}
28962926

28972927
features = btrfs_super_incompat_flags(disk_super) &
@@ -2901,7 +2931,7 @@ int open_ctree(struct super_block *sb,
29012931
"cannot mount because of unsupported optional features (%llx)",
29022932
features);
29032933
err = -EINVAL;
2904-
goto fail_alloc;
2934+
goto fail_csum;
29052935
}
29062936

29072937
features = btrfs_super_incompat_flags(disk_super);
@@ -2945,7 +2975,7 @@ int open_ctree(struct super_block *sb,
29452975
btrfs_err(fs_info,
29462976
"unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
29472977
nodesize, sectorsize);
2948-
goto fail_alloc;
2978+
goto fail_csum;
29492979
}
29502980

29512981
/*
@@ -2961,7 +2991,7 @@ int open_ctree(struct super_block *sb,
29612991
"cannot mount read-write because of unsupported optional features (%llx)",
29622992
features);
29632993
err = -EINVAL;
2964-
goto fail_alloc;
2994+
goto fail_csum;
29652995
}
29662996

29672997
ret = btrfs_init_workqueues(fs_info, fs_devices);
@@ -3339,6 +3369,8 @@ int open_ctree(struct super_block *sb,
33393369
fail_sb_buffer:
33403370
btrfs_stop_all_workers(fs_info);
33413371
btrfs_free_block_groups(fs_info);
3372+
fail_csum:
3373+
btrfs_free_csum_hash(fs_info);
33423374
fail_alloc:
33433375
fail_iput:
33443376
btrfs_mapping_tree_free(&fs_info->mapping_tree);

0 commit comments

Comments
 (0)