Contributors: 15
Author Tokens Token Proportion Commits Commit Proportion
Johannes Thumshirn 2275 93.05% 6 19.35%
Feifei Xu 35 1.43% 2 6.45%
Chris Mason 34 1.39% 6 19.35%
Josef Bacik 15 0.61% 3 9.68%
Christoph Hellwig 15 0.61% 1 3.23%
Nikolay Borisov 15 0.61% 1 3.23%
Jeff Mahoney 12 0.49% 1 3.23%
Qu Wenruo 11 0.45% 2 6.45%
Zheng Yan 11 0.45% 3 9.68%
Omar Sandoval 8 0.33% 1 3.23%
Ilya Dryomov 5 0.20% 1 3.23%
Josef Whiter 5 0.20% 1 3.23%
Li Zefan 2 0.08% 1 3.23%
David Sterba 1 0.04% 1 3.23%
Miao Xie 1 0.04% 1 3.23%
Total 2445 31

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2024 Western Digital Corporation or its affiliates.
 */

#include <linux/sizes.h>
#include "../fs.h"
#include "../disk-io.h"
#include "../transaction.h"
#include "../volumes.h"
#include "../raid-stripe-tree.h"
#include "btrfs-tests.h"

#define RST_TEST_NUM_DEVICES	(2)
#define RST_TEST_RAID1_TYPE	(BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_RAID1)

typedef int (*test_func_t)(struct btrfs_trans_handle *trans);

static struct btrfs_device *btrfs_device_by_devid(struct btrfs_fs_devices *fs_devices,
						  u64 devid)
{
	struct btrfs_device *dev;

	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
		if (dev->devid == devid)
			return dev;
	}

	return NULL;
}

/*
 * Test a 64K RST write on a 2 disk RAID1 at a logical address of 1M and then
 * delete the 1st 32K, making the new start address 1M+32K.
 */
static int test_front_delete(struct btrfs_trans_handle *trans)
{
	struct btrfs_fs_info *fs_info = trans->fs_info;
	struct btrfs_io_context *bioc;
	struct btrfs_io_stripe io_stripe = { 0 };
	u64 map_type = RST_TEST_RAID1_TYPE;
	u64 logical = SZ_1M;
	u64 len = SZ_64K;
	int ret;

	bioc = alloc_btrfs_io_context(fs_info, logical, RST_TEST_NUM_DEVICES);
	if (!bioc) {
		test_std_err(TEST_ALLOC_IO_CONTEXT);
		ret = -ENOMEM;
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	bioc->map_type = map_type;
	bioc->size = len;

	for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_io_stripe *stripe = &bioc->stripes[i];

		stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i);
		if (!stripe->dev) {
			test_err("cannot find device with devid %d", i);
			ret = -EINVAL;
			goto out;
		}

		stripe->physical = logical + i * SZ_1G;
	}

	ret = btrfs_insert_one_raid_extent(trans, bioc);
	if (ret) {
		test_err("inserting RAID extent failed: %d", ret);
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical) {
		test_err("invalid physical address, expected %llu got %llu",
			 logical, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_64K) {
		test_err("invalid stripe length, expected %llu got %llu",
			 (u64)SZ_64K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical, SZ_32K);
	if (ret) {
		test_err("deleting RAID extent [%llu, %llu] failed", logical,
			 logical + SZ_32K);
		goto out;
	}

	len = SZ_32K;
	ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_32K, &len,
					   map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed",
			 logical + SZ_32K, logical + SZ_32K + len);
		goto out;
	}

	if (io_stripe.physical != logical + SZ_32K) {
		test_err("invalid physical address, expected %llu, got %llu",
			 logical + SZ_32K, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_32K) {
		test_err("invalid stripe length, expected %llu, got %llu",
			 (u64)SZ_32K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (!ret) {
		ret = -EINVAL;
		test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail",
			 logical, logical + SZ_32K);
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical + SZ_32K, SZ_32K);
out:
	btrfs_put_bioc(bioc);
	return ret;
}

/*
 * Test a 64K RST write on a 2 disk RAID1 at a logical address of 1M and then
 * truncate the stripe extent down to 32K.
 */
static int test_tail_delete(struct btrfs_trans_handle *trans)
{
	struct btrfs_fs_info *fs_info = trans->fs_info;
	struct btrfs_io_context *bioc;
	struct btrfs_io_stripe io_stripe = { 0 };
	u64 map_type = RST_TEST_RAID1_TYPE;
	u64 logical = SZ_1M;
	u64 len = SZ_64K;
	int ret;

	bioc = alloc_btrfs_io_context(fs_info, logical, RST_TEST_NUM_DEVICES);
	if (!bioc) {
		test_std_err(TEST_ALLOC_IO_CONTEXT);
		ret = -ENOMEM;
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	bioc->map_type = map_type;
	bioc->size = len;

	for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_io_stripe *stripe = &bioc->stripes[i];

		stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i);
		if (!stripe->dev) {
			test_err("cannot find device with devid %d", i);
			ret = -EINVAL;
			goto out;
		}

		stripe->physical = logical + i * SZ_1G;
	}

	ret = btrfs_insert_one_raid_extent(trans, bioc);
	if (ret) {
		test_err("inserting RAID extent failed: %d", ret);
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	if (!io_stripe.dev) {
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical) {
		test_err("invalid physical address, expected %llu got %llu",
			 logical, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_64K) {
		test_err("invalid stripe length, expected %llu got %llu",
			 (u64)SZ_64K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical + SZ_32K, SZ_32K);
	if (ret) {
		test_err("deleting RAID extent [%llu, %llu] failed",
			 logical + SZ_32K, logical + SZ_64K);
		goto out;
	}

	len = SZ_32K;
	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical) {
		test_err("invalid physical address, expected %llu, got %llu",
			 logical, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_32K) {
		test_err("invalid stripe length, expected %llu, got %llu",
			 (u64)SZ_32K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical, len);
	if (ret)
		test_err("deleting RAID extent [%llu, %llu] failed", logical,
			 logical + len);

out:
	btrfs_put_bioc(bioc);
	return ret;
}

/*
 * Test a 64K RST write on a 2 disk RAID1 at a logical address of 1M and then
 * overwrite the whole range giving it new physical address at an offset of 1G.
 * The intent of this test is to exercise the 'update_raid_extent_item()'
 * function called be btrfs_insert_one_raid_extent().
 */
static int test_create_update_delete(struct btrfs_trans_handle *trans)
{
	struct btrfs_fs_info *fs_info = trans->fs_info;
	struct btrfs_io_context *bioc;
	struct btrfs_io_stripe io_stripe = { 0 };
	u64 map_type = RST_TEST_RAID1_TYPE;
	u64 logical = SZ_1M;
	u64 len = SZ_64K;
	int ret;

	bioc = alloc_btrfs_io_context(fs_info, logical, RST_TEST_NUM_DEVICES);
	if (!bioc) {
		test_std_err(TEST_ALLOC_IO_CONTEXT);
		ret = -ENOMEM;
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	bioc->map_type = map_type;
	bioc->size = len;

	for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_io_stripe *stripe = &bioc->stripes[i];

		stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i);
		if (!stripe->dev) {
			test_err("cannot find device with devid %d", i);
			ret = -EINVAL;
			goto out;
		}

		stripe->physical = logical + i * SZ_1G;
	}

	ret = btrfs_insert_one_raid_extent(trans, bioc);
	if (ret) {
		test_err("inserting RAID extent failed: %d", ret);
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	if (!io_stripe.dev) {
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical) {
		test_err("invalid physical address, expected %llu got %llu",
			 logical, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_64K) {
		test_err("invalid stripe length, expected %llu got %llu",
			 (u64)SZ_64K, len);
		ret = -EINVAL;
		goto out;
	}

	for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_io_stripe *stripe = &bioc->stripes[i];

		stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i);
		if (!stripe->dev) {
			test_err("cannot find device with devid %d", i);
			ret = -EINVAL;
			goto out;
		}

		stripe->physical = SZ_1G + logical + i * SZ_1G;
	}

	ret = btrfs_insert_one_raid_extent(trans, bioc);
	if (ret) {
		test_err("updating RAID extent failed: %d", ret);
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret) {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical + SZ_1G) {
		test_err("invalid physical address, expected %llu, got %llu",
			 logical + SZ_1G, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_64K) {
		test_err("invalid stripe length, expected %llu, got %llu",
			 (u64)SZ_64K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical, len);
	if (ret)
		test_err("deleting RAID extent [%llu, %llu] failed", logical,
			 logical + len);

out:
	btrfs_put_bioc(bioc);
	return ret;
}

/*
 * Test a simple 64K RST write on a 2 disk RAID1 at a logical address of 1M.
 * The "physical" copy on device 0 is at 1M, on device 1 it is at 1G+1M.
 */
static int test_simple_create_delete(struct btrfs_trans_handle *trans)
{
	struct btrfs_fs_info *fs_info = trans->fs_info;
	struct btrfs_io_context *bioc;
	struct btrfs_io_stripe io_stripe = { 0 };
	u64 map_type = RST_TEST_RAID1_TYPE;
	u64 logical = SZ_1M;
	u64 len = SZ_64K;
	int ret;

	bioc = alloc_btrfs_io_context(fs_info, logical, RST_TEST_NUM_DEVICES);
	if (!bioc) {
		test_std_err(TEST_ALLOC_IO_CONTEXT);
		ret = -ENOMEM;
		goto out;
	}

	bioc->map_type = map_type;
	bioc->size = SZ_64K;

	for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_io_stripe *stripe = &bioc->stripes[i];

		stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i);
		if (!stripe->dev) {
			test_err("cannot find device with devid %d", i);
			ret = -EINVAL;
			goto out;
		}

		stripe->physical = logical + i * SZ_1G;
	}

	ret = btrfs_insert_one_raid_extent(trans, bioc);
	if (ret) {
		test_err("inserting RAID extent failed: %d", ret);
		goto out;
	}

	io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0);
	if (!io_stripe.dev) {
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
	if (ret)  {
		test_err("lookup of RAID extent [%llu, %llu] failed", logical,
			 logical + len);
		goto out;
	}

	if (io_stripe.physical != logical) {
		test_err("invalid physical address, expected %llu got %llu",
			 logical, io_stripe.physical);
		ret = -EINVAL;
		goto out;
	}

	if (len != SZ_64K) {
		test_err("invalid stripe length, expected %llu got %llu",
			 (u64)SZ_64K, len);
		ret = -EINVAL;
		goto out;
	}

	ret = btrfs_delete_raid_extent(trans, logical, len);
	if (ret)
		test_err("deleting RAID extent [%llu, %llu] failed", logical,
			 logical + len);

out:
	btrfs_put_bioc(bioc);
	return ret;
}

static const test_func_t tests[] = {
	test_simple_create_delete,
	test_create_update_delete,
	test_tail_delete,
	test_front_delete,
};

static int run_test(test_func_t test, u32 sectorsize, u32 nodesize)
{
	struct btrfs_trans_handle trans;
	struct btrfs_fs_info *fs_info;
	struct btrfs_root *root = NULL;
	int ret;

	fs_info = btrfs_alloc_dummy_fs_info(sectorsize, nodesize);
	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;
	}
	btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
					BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE);
	root->root_key.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
	root->root_key.type = BTRFS_ROOT_ITEM_KEY;
	root->root_key.offset = 0;
	fs_info->stripe_root = root;
	root->fs_info->tree_root = root;

	root->node = alloc_test_extent_buffer(root->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 < RST_TEST_NUM_DEVICES; i++) {
		struct btrfs_device *dev;

		dev = btrfs_alloc_dummy_device(fs_info);
		if (IS_ERR(dev)) {
			test_err("cannot allocate device");
			ret = PTR_ERR(dev);
			goto out;
		}
		dev->devid = i;
	}

	btrfs_init_dummy_trans(&trans, root->fs_info);
	ret = test(&trans);
	if (ret)
		goto out;

out:
	btrfs_free_dummy_root(root);
	btrfs_free_dummy_fs_info(fs_info);

	return ret;
}

int btrfs_test_raid_stripe_tree(u32 sectorsize, u32 nodesize)
{
	int ret = 0;

	test_msg("running raid-stripe-tree tests");
	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
		ret = run_test(tests[i], sectorsize, nodesize);
		if (ret) {
			test_err("test-case %ps failed with %d\n", tests[i], ret);
			goto out;
		}
	}

out:
	return ret;
}