// 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)
#define SZ_48K (SZ_32K + SZ_16K)
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 creating a range of three extents and then punch a hole in the middle,
* deleting all of the middle extents and partially deleting the "book ends".
*/
static int test_punch_hole_3extents(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 logical1 = SZ_1M;
u64 len1 = SZ_1M;
u64 logical2 = logical1 + len1;
u64 len2 = SZ_1M;
u64 logical3 = logical2 + len2;
u64 len3 = SZ_1M;
u64 hole_start = logical1 + SZ_256K;
u64 hole_len = SZ_2M;
int ret;
bioc = alloc_btrfs_io_context(fs_info, logical1, 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);
/* Prepare for the test, 1st create 3 x 1M extents. */
bioc->map_type = map_type;
bioc->size = len1;
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 = logical1 + i * SZ_1G;
}
ret = btrfs_insert_one_raid_extent(trans, bioc);
if (ret) {
test_err("inserting RAID extent failed: %d", ret);
goto out;
}
bioc->logical = logical2;
bioc->size = len2;
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 = logical2 + i * SZ_1G;
}
ret = btrfs_insert_one_raid_extent(trans, bioc);
if (ret) {
test_err("inserting RAID extent failed: %d", ret);
goto out;
}
bioc->logical = logical3;
bioc->size = len3;
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 = logical3 + i * SZ_1G;
}
ret = btrfs_insert_one_raid_extent(trans, bioc);
if (ret) {
test_err("inserting RAID extent failed: %d", ret);
goto out;
}
/*
* Delete a range starting at logical1 + 256K and 2M in length. Extent
* 1 is truncated to 256k length, extent 2 is completely dropped and
* extent 3 is moved 256K to the right.
*/
ret = btrfs_delete_raid_extent(trans, hole_start, hole_len);
if (ret)