Skip to content

Commit b3ad2c1

Browse files
lorddoskiaskdave
authored andcommitted
btrfs: selftests: Add support for dummy devices
Add basic infrastructure to create and link dummy btrfs_devices. This will be used in the pending btrfs_rmap_block test which deals with the block groups. Calling btrfs_alloc_dummy_device will link the newly created device to the passed fs_info and the test framework will free them once the test is finished. Signed-off-by: Nikolay Borisov <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 96a1433 commit b3ad2c1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

fs/btrfs/tests/btrfs-tests.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ static void btrfs_destroy_test_fs(void)
8686
unregister_filesystem(&test_type);
8787
}
8888

89+
struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info)
90+
{
91+
struct btrfs_device *dev;
92+
93+
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
94+
if (!dev)
95+
return ERR_PTR(-ENOMEM);
96+
97+
extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
98+
INIT_LIST_HEAD(&dev->dev_list);
99+
list_add(&dev->dev_list, &fs_info->fs_devices->devices);
100+
101+
return dev;
102+
}
103+
104+
static void btrfs_free_dummy_device(struct btrfs_device *dev)
105+
{
106+
extent_io_tree_release(&dev->alloc_state);
107+
kfree(dev);
108+
}
109+
89110
struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
90111
{
91112
struct btrfs_fs_info *fs_info = kzalloc(sizeof(struct btrfs_fs_info),
@@ -132,12 +153,14 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
132153
INIT_LIST_HEAD(&fs_info->dirty_qgroups);
133154
INIT_LIST_HEAD(&fs_info->dead_roots);
134155
INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
156+
INIT_LIST_HEAD(&fs_info->fs_devices->devices);
135157
INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
136158
INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
137159
extent_io_tree_init(fs_info, &fs_info->freed_extents[0],
138160
IO_TREE_FS_INFO_FREED_EXTENTS0, NULL);
139161
extent_io_tree_init(fs_info, &fs_info->freed_extents[1],
140162
IO_TREE_FS_INFO_FREED_EXTENTS1, NULL);
163+
extent_map_tree_init(&fs_info->mapping_tree);
141164
fs_info->pinned_extents = &fs_info->freed_extents[0];
142165
set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
143166

@@ -150,6 +173,7 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
150173
{
151174
struct radix_tree_iter iter;
152175
void **slot;
176+
struct btrfs_device *dev, *tmp;
153177

154178
if (!fs_info)
155179
return;
@@ -180,6 +204,11 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
180204
}
181205
spin_unlock(&fs_info->buffer_lock);
182206

207+
btrfs_mapping_tree_free(&fs_info->mapping_tree);
208+
list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
209+
dev_list) {
210+
btrfs_free_dummy_device(dev);
211+
}
183212
btrfs_free_qgroup_config(fs_info);
184213
btrfs_free_fs_roots(fs_info);
185214
cleanup_srcu_struct(&fs_info->subvol_srcu);

fs/btrfs/tests/btrfs-tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ btrfs_alloc_dummy_block_group(struct btrfs_fs_info *fs_info, unsigned long lengt
4646
void btrfs_free_dummy_block_group(struct btrfs_block_group *cache);
4747
void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
4848
struct btrfs_fs_info *fs_info);
49+
struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info);
4950
#else
5051
static inline int btrfs_run_sanity_tests(void)
5152
{

0 commit comments

Comments
 (0)