[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240705-b4-rst-updates-v4-4-f3eed3f2cfad@kernel.org>
Date: Fri, 05 Jul 2024 17:13:50 +0200
From: Johannes Thumshirn <jth@...nel.org>
To: Chris Mason <clm@...com>, Josef Bacik <josef@...icpanda.com>,
David Sterba <dsterba@...e.com>
Cc: linux-btrfs@...r.kernel.org, linux-kernel@...r.kernel.org,
Johannes Thumshirn <johannes.thumshirn@....com>
Subject: [PATCH v4 4/7] btrfs: stripe-tree: add selftests
From: Johannes Thumshirn <johannes.thumshirn@....com>
Add self-tests for the RAID stripe tree code.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@....com>
---
fs/btrfs/Makefile | 3 +-
fs/btrfs/raid-stripe-tree.c | 3 +-
fs/btrfs/raid-stripe-tree.h | 5 +
fs/btrfs/tests/btrfs-tests.c | 3 +
fs/btrfs/tests/btrfs-tests.h | 1 +
fs/btrfs/tests/raid-stripe-tree-tests.c | 322 ++++++++++++++++++++++++++++++++
6 files changed, 335 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index 87617f2968bc..3cfc440c636c 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -43,4 +43,5 @@ btrfs-$(CONFIG_FS_VERITY) += verity.o
btrfs-$(CONFIG_BTRFS_FS_RUN_SANITY_TESTS) += tests/free-space-tests.o \
tests/extent-buffer-tests.o tests/btrfs-tests.o \
tests/extent-io-tests.o tests/inode-tests.o tests/qgroup-tests.o \
- tests/free-space-tree-tests.o tests/extent-map-tests.o
+ tests/free-space-tree-tests.o tests/extent-map-tests.o \
+ tests/raid-stripe-tree-tests.o
diff --git a/fs/btrfs/raid-stripe-tree.c b/fs/btrfs/raid-stripe-tree.c
index d2a6e409b3e8..ba0733c6be76 100644
--- a/fs/btrfs/raid-stripe-tree.c
+++ b/fs/btrfs/raid-stripe-tree.c
@@ -156,7 +156,8 @@ static int replace_raid_extent_item(struct btrfs_trans_handle *trans,
return ret;
}
-static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
+EXPORT_FOR_TESTS
+int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
struct btrfs_io_context *bioc)
{
struct btrfs_fs_info *fs_info = trans->fs_info;
diff --git a/fs/btrfs/raid-stripe-tree.h b/fs/btrfs/raid-stripe-tree.h
index 1ac1c21aac2f..541836421778 100644
--- a/fs/btrfs/raid-stripe-tree.h
+++ b/fs/btrfs/raid-stripe-tree.h
@@ -28,6 +28,11 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
struct btrfs_ordered_extent *ordered_extent);
+#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
+int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
+ struct btrfs_io_context *bioc);
+#endif
+
static inline bool btrfs_need_stripe_tree_update(struct btrfs_fs_info *fs_info,
u64 map_type)
{
diff --git a/fs/btrfs/tests/btrfs-tests.c b/fs/btrfs/tests/btrfs-tests.c
index ce50847e1e01..18e1ab4a0914 100644
--- a/fs/btrfs/tests/btrfs-tests.c
+++ b/fs/btrfs/tests/btrfs-tests.c
@@ -291,6 +291,9 @@ int btrfs_run_sanity_tests(void)
ret = btrfs_test_free_space_tree(sectorsize, nodesize);
if (ret)
goto out;
+ ret = btrfs_test_raid_stripe_tree(sectorsize, nodesize);
+ if (ret)
+ goto out;
}
}
ret = btrfs_test_extent_map();
diff --git a/fs/btrfs/tests/btrfs-tests.h b/fs/btrfs/tests/btrfs-tests.h
index dc2f2ab15fa5..61bcadaf2036 100644
--- a/fs/btrfs/tests/btrfs-tests.h
+++ b/fs/btrfs/tests/btrfs-tests.h
@@ -37,6 +37,7 @@ int btrfs_test_extent_io(u32 sectorsize, u32 nodesize);
int btrfs_test_inodes(u32 sectorsize, u32 nodesize);
int btrfs_test_qgroups(u32 sectorsize, u32 nodesize);
int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize);
+int btrfs_test_raid_stripe_tree(u32 sectorsize, u32 nodesize);
int btrfs_test_extent_map(void);
struct inode *btrfs_new_test_inode(void);
struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize);
diff --git a/fs/btrfs/tests/raid-stripe-tree-tests.c b/fs/btrfs/tests/raid-stripe-tree-tests.c
new file mode 100644
index 000000000000..bec7c210c14c
--- /dev/null
+++ b/fs/btrfs/tests/raid-stripe-tree-tests.c
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024 Western Digital Corporation. All rights reserved.
+ */
+#include <linux/array_size.h>
+#include <linux/sizes.h>
+#include <linux/btrfs.h>
+#include <linux/btrfs_tree.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include "btrfs-tests.h"
+#include "../disk-io.h"
+#include "../transaction.h"
+#include "../volumes.h"
+#include "../raid-stripe-tree.h"
+#include "../block-group.h"
+
+static struct btrfs_io_context *alloc_dummy_bioc(struct btrfs_fs_info *fs_info,
+ u64 logical, u16 total_stripes)
+{
+ struct btrfs_io_context *bioc;
+
+ bioc = kzalloc(sizeof(struct btrfs_io_context) +
+ sizeof(struct btrfs_io_stripe) * total_stripes,
+ GFP_KERNEL);
+
+ if (!bioc)
+ return NULL;
+
+ refcount_set(&bioc->refs, 1);
+
+ bioc->fs_info = fs_info;
+ bioc->replace_stripe_src = -1;
+ bioc->full_stripe_logical = (u64)-1;
+ bioc->logical = logical;
+
+ return bioc;
+}
+
+typedef int (*test_func_t)(struct btrfs_fs_info *);
+
+static int test_stripe_tree_delete_tail(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_trans_handle trans;
+ struct btrfs_io_context *bioc;
+ struct btrfs_io_stripe stripe = { };
+ const u64 map_type = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_RAID1;
+ const int total_stripes = btrfs_bg_type_to_factor(map_type);
+ u64 logical = SZ_8K;
+ u64 length = SZ_64K;
+ u64 read_length;
+ int i;
+ int last = 0;
+ int ret;
+
+ btrfs_init_dummy_trans(&trans, fs_info);
+
+ bioc = alloc_dummy_bioc(fs_info, logical, total_stripes);
+ if (!bioc)
+ return -ENOMEM;
+
+ bioc->size = length;
+ bioc->map_type = map_type;
+ for (i = 0; i < total_stripes; ++i) {
+ struct btrfs_device *dev;
+
+ dev = kzalloc(sizeof(struct btrfs_device), GFP_KERNEL);
+ if (!dev) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ dev->devid = i;
+ bioc->stripes[i].dev = dev;
+ bioc->stripes[i].length = length;
+ bioc->stripes[i].physical = i * SZ_8K;
+ last = i;
+ }
+
+ ret = btrfs_insert_one_raid_extent(&trans, bioc);
+ if (ret)
+ goto out;
+
+ ret = btrfs_delete_raid_extent(&trans, logical, SZ_16K);
+ if (ret)
+ goto out;
+
+ stripe.dev = bioc->stripes[last].dev;
+ read_length = length - SZ_16K;
+ ret = btrfs_get_raid_extent_offset(fs_info, logical,
+ &read_length, map_type, 0, &stripe);
+ if (ret)
+ goto out;
+
+ if (read_length != length - SZ_16K) {
+ test_err("invalid length %llu vs %llu", read_length,
+ length - SZ_16K);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (stripe.physical != bioc->stripes[last].physical) {
+ test_err("invalid physical %llu vs %llu", stripe.physical,
+ bioc->stripes[last].physical);
+ ret = -EINVAL;
+ }
+
+out:
+ for (i = 0; i < total_stripes; i++)
+ kfree(bioc->stripes[i].dev);
+
+ kfree(bioc);
+ return ret;
+}
+
+static int test_stripe_tree_delete_front(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_trans_handle trans;
+ struct btrfs_io_context *bioc;
+ const u64 map_type = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_RAID1;
+ const int total_stripes = btrfs_bg_type_to_factor(map_type);
+ u64 logical = SZ_8K;
+ u64 length = SZ_64K;
+ u64 read_length;
+ struct btrfs_io_stripe stripe = { };
+ int i;
+ int last = 0;
+ int ret;
+
+ btrfs_init_dummy_trans(&trans, fs_info);
+
+ bioc = alloc_dummy_bioc(fs_info, logical, total_stripes);
+ if (!bioc)
+ return -ENOMEM;
+
+ bioc->size = length;
+ bioc->map_type = map_type;
+ for (i = 0; i < total_stripes; i++) {
+ struct btrfs_device *dev;
+
+ dev = kzalloc(sizeof(struct btrfs_device), GFP_KERNEL);
+ if (!dev) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ dev->devid = i;
+ bioc->stripes[i].dev = dev;
+ bioc->stripes[i].length = length;
+ bioc->stripes[i].physical = i * SZ_8K;
+ last = i;
+ }
+
+ ret = btrfs_insert_one_raid_extent(&trans, bioc);
+ if (ret)
+ goto out;
+
+ ret = btrfs_delete_raid_extent(&trans, logical, SZ_8K);
+ if (ret)
+ goto out;
+
+ stripe.dev = bioc->stripes[last].dev;
+ read_length = length - SZ_8K;
+ ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_8K,
+ &read_length, map_type, 0, &stripe);
+ if (ret)
+ goto out;
+
+ if (read_length != length - SZ_8K) {
+ test_err("invalid length %llu vs %llu", read_length,
+ length - SZ_8K);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (stripe.physical != bioc->stripes[last].physical + SZ_8K) {
+ test_err("invalid physical %llu vs %llu", stripe.physical,
+ bioc->stripes[last].physical);
+ ret = -EINVAL;
+ }
+
+out:
+ for (i = 0; i < total_stripes; i++)
+ kfree(bioc->stripes[i].dev);
+
+ kfree(bioc);
+ return ret;
+
+}
+
+static int test_stripe_tree_delete_whole(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_trans_handle trans;
+ struct btrfs_io_context *bioc;
+ const u64 map_type = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_RAID1;
+ const int total_stripes = btrfs_bg_type_to_factor(map_type);
+ u64 logical = SZ_8K;
+ u64 length = SZ_64K;
+ int i;
+ int ret;
+
+ btrfs_init_dummy_trans(&trans, fs_info);
+
+ bioc = alloc_dummy_bioc(fs_info, logical, total_stripes);
+ if (!bioc)
+ return -ENOMEM;
+
+ bioc->size = length;
+ bioc->map_type = map_type;
+ for (i = 0; i < total_stripes; ++i) {
+ struct btrfs_device *dev;
+
+ dev = kzalloc(sizeof(struct btrfs_device), GFP_KERNEL);
+ if (!dev) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ dev->devid = i;
+ bioc->stripes[i].dev = dev;
+ bioc->stripes[i].length = length;
+ bioc->stripes[i].physical = i * SZ_8K;
+ }
+
+ ret = btrfs_insert_one_raid_extent(&trans, bioc);
+ if (ret)
+ goto out;
+
+ ret = btrfs_delete_raid_extent(&trans, logical, length);
+ if (ret)
+ goto out;
+
+ ret = btrfs_header_nritems(fs_info->stripe_root->node);
+ if (ret != 0) {
+ test_err("test failed");
+ ret = -EINVAL;
+ }
+
+out:
+ for (i = 0; i < total_stripes; i++)
+ kfree(bioc->stripes[i].dev);
+
+ kfree(bioc);
+ return ret;
+}
+
+static int test_stripe_tree_delete(struct btrfs_fs_info *fs_info)
+{
+ test_func_t delete_tests[] = {
+ test_stripe_tree_delete_whole,
+ test_stripe_tree_delete_front,
+ test_stripe_tree_delete_tail,
+ };
+ int ret;
+
+ for (int i = 0; i < ARRAY_SIZE(delete_tests); i++) {
+ test_func_t test = delete_tests[i];
+
+ ret = test(fs_info);
+ if (ret)
+ goto out;
+ }
+
+out:
+ return ret;
+}
+
+int btrfs_test_raid_stripe_tree(u32 sectorsize, u32 nodesize)
+{
+ test_func_t tests[] = {
+ test_stripe_tree_delete,
+ };
+ struct btrfs_fs_info *fs_info;
+ struct btrfs_root *root = NULL;
+ int ret = 0;
+
+ test_msg("running raid stripe tree tests");
+
+ fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+ if (!fs_info) {
+ test_std_err(TEST_ALLOC_FS_INFO);
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ root = btrfs_alloc_dummy_root(fs_info);
+ if (IS_ERR(root)) {
+ test_std_err(TEST_ALLOC_ROOT);
+ ret = PTR_ERR(root);
+ goto out;
+ }
+
+ root->root_key.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
+ root->root_key.type = BTRFS_ROOT_ITEM_KEY;
+ root->root_key.offset = 0;
+ btrfs_global_root_insert(root);
+ root->fs_info->stripe_root = root;
+ root->fs_info->tree_root = root;
+ btrfs_set_super_incompat_flags(fs_info->super_copy,
+ BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE);
+
+
+ root->node = alloc_test_extent_buffer(fs_info, nodesize);
+ if (IS_ERR(root->node)) {
+ test_std_err(TEST_ALLOC_EXTENT_BUFFER);
+ ret = PTR_ERR(root->node);
+ goto out;
+ }
+ btrfs_set_header_level(root->node, 0);
+ btrfs_set_header_nritems(root->node, 0);
+ root->alloc_bytenr += 2 * nodesize;
+
+ for (int i = 0; i < ARRAY_SIZE(tests); i++) {
+ test_func_t test = tests[i];
+
+ ret = test(fs_info);
+ if (ret)
+ goto out;
+ }
+out:
+ btrfs_free_dummy_root(root);
+ btrfs_free_dummy_fs_info(fs_info);
+ return ret;
+}
--
2.43.0
Powered by blists - more mailing lists