123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
#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;
}
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);
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;
}
ret = btrfs_delete_raid_extent(trans, hole_start, hole_len);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
hole_start, hole_start + hole_len);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type,
0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical1, logical1 + len1);
goto out;
}
if (io_stripe.physical != logical1) {
test_err("invalid physical address, expected %llu, got %llu",
logical1, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len1 != SZ_256K) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_256K, len1);
ret = -EINVAL;
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type,
0, &io_stripe);
if (ret != -ENODATA) {
test_err("lookup of RAID extent [%llu, %llu] succeeded should fail",
logical2, logical2 + len2);
ret = -EINVAL;
goto out;
}
logical3 += SZ_256K;
ret = btrfs_get_raid_extent_offset(fs_info, logical3, &len3, map_type,
0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical3, logical3 + len3);
goto out;
}
if (io_stripe.physical != logical3) {
test_err("invalid physical address, expected %llu, got %llu",
logical3 + SZ_256K, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len3 != SZ_1M - SZ_256K) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_1M - SZ_256K, len3);
ret = -EINVAL;
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical1, len1);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
logical1, logical1 + len1);
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical3, len3);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
logical1, logical1 + len1);
goto out;
}
out:
btrfs_put_bioc(bioc);
return ret;
}
static int test_delete_two_extents(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;
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);
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;
}
ret = btrfs_delete_raid_extent(trans, logical1, len1 + len2);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
logical1, logical1 + len1 + len2);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type,
0, &io_stripe);
if (ret != -ENODATA) {
test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail",
logical1, len1);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type,
0, &io_stripe);
if (ret != -ENODATA) {
test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail",
logical2, len2);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical3, &len3, map_type,
0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical3, len3);
goto out;
}
if (io_stripe.physical != logical3) {
test_err("invalid physical address, expected %llu, got %llu",
logical3, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len3 != SZ_1M) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_1M, len3);
ret = -EINVAL;
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical3, len3);
out:
btrfs_put_bioc(bioc);
return ret;
}
static int test_punch_hole(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 hole_start = logical1 + SZ_32K;
u64 hole_len = SZ_64K;
u64 logical2 = hole_start + hole_len;
u64 len = SZ_1M;
u64 len1 = SZ_32K;
u64 len2 = len - len1 - hole_len;
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);
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 = logical1 + 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, logical1, &len, map_type, 0,
&io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed", logical1,
logical1 + len);
goto out;
}
if (io_stripe.physical != logical1) {
test_err("invalid physical address, expected %llu got %llu",
logical1, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len != SZ_1M) {
test_err("invalid stripe length, expected %llu got %llu",
(u64)SZ_1M, len);
ret = -EINVAL;
goto out;
}
ret = btrfs_delete_raid_extent(trans, hole_start, hole_len);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
hole_start, hole_start + hole_len);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type,
0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical1, logical1 + len1);
goto out;
}
if (io_stripe.physical != logical1) {
test_err("invalid physical address, expected %llu, got %llu",
logical1, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len1 != SZ_32K) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_32K, len1);
ret = -EINVAL;
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type,
0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed", logical2,
logical2 + len2);
goto out;
}
if (io_stripe.physical != logical2) {
test_err("invalid physical address, expected %llu, got %llu",
logical2, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len2 != len - len1 - hole_len) {
test_err("invalid length, expected %llu, got %llu",
len - len1 - hole_len, len2);
ret = -EINVAL;
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, hole_start, &hole_len,
map_type, 0, &io_stripe);
if (ret != -ENODATA) {
ret = -EINVAL;
test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail",
hole_start, hole_start + SZ_64K);
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical1, len1);
if (ret)
goto out;
ret = btrfs_delete_raid_extent(trans, logical2, len2);
out:
btrfs_put_bioc(bioc);
return ret;
}
static int test_front_delete_prev_item(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 logical2 = SZ_2M;
u64 len = SZ_1M;
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);
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 = 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;
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;
}
ret = btrfs_delete_raid_extent(trans, logical1 + SZ_512K, SZ_1M);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
logical1 + SZ_512K, (u64)SZ_1M);
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len, map_type, 0,
&io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed", logical1,
logical1 + len);
goto out;
}
if (io_stripe.physical != logical1) {
test_err("invalid physical address, expected %llu got %llu",
logical1, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len != SZ_512K) {
test_err("invalid stripe length, expected %llu got %llu",
(u64)SZ_512K, len);
ret = -EINVAL;
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical2 + SZ_512K, &len,
map_type, 0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical2 + SZ_512K, logical2 + len);
goto out;
}
if (io_stripe.physical != logical2 + SZ_512K) {
test_err("invalid physical address, expected %llu got %llu",
logical2 + SZ_512K, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len != SZ_512K) {
test_err("invalid stripe length, expected %llu got %llu",
(u64)SZ_512K, len);
ret = -EINVAL;
goto out;
}
len = SZ_1M;
ret = btrfs_get_raid_extent_offset(fs_info, logical1 + SZ_512K, &len,
map_type, 0, &io_stripe);
if (ret != -ENODATA) {
test_err("lookup of RAID [%llu, %llu] succeeded, should fail",
logical1 + SZ_512K, logical1 + SZ_512K + len);
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical1, SZ_512K);
if (ret)
goto out;
ret = btrfs_delete_raid_extent(trans, logical2 + SZ_512K, SZ_512K);
out:
btrfs_put_bioc(bioc);
return ret;
}
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_16K);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed", logical,
logical + SZ_16K);
goto out;
}
len -= SZ_16K;
ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_16K, &len,
map_type, 0, &io_stripe);
if (ret) {
test_err("lookup of RAID extent [%llu, %llu] failed",
logical + SZ_16K, logical + SZ_64K);
goto out;
}
if (io_stripe.physical != logical + SZ_16K) {
test_err("invalid physical address, expected %llu, got %llu",
logical + SZ_16K, io_stripe.physical);
ret = -EINVAL;
goto out;
}
if (len != SZ_48K) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_48K, len);
ret = -EINVAL;
goto out;
}
ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe);
if (ret != -ENODATA) {
ret = -EINVAL;
test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail",
logical, logical + SZ_16K);
goto out;
}
ret = btrfs_delete_raid_extent(trans, logical + SZ_16K, SZ_48K);
out:
btrfs_put_bioc(bioc);
return ret;
}
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_48K, SZ_16K);
if (ret) {
test_err("deleting RAID extent [%llu, %llu] failed",
logical + SZ_48K, logical + SZ_64K);
goto out;
}
len = SZ_48K;
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_48K) {
test_err("invalid stripe length, expected %llu, got %llu",
(u64)SZ_48K, len);
ret = -EINVAL;
goto out;
}
len = SZ_16K;
ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_48K, &len,
map_type, 0, &io_stripe);
if (ret != -ENODATA) {
test_err("lookup of RAID extent [%llu, %llu] succeeded should fail",
logical + SZ_48K, logical + SZ_64K);
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 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;
}
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,
test_front_delete_prev_item,
test_punch_hole,
test_punch_hole_3extents,
test_delete_two_extents,
};
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_incompat_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;
}