Commit 0a308f80 authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba

btrfs: pass an inode to btrfs_add_extent_mapping()

Instead of passing fs_info and extent map tree arguments to
btrfs_add_extent_mapping(), we can pass an inode instead, as extent maps
are always inserted in the extent map tree of an inode, and the fs_info
can be extracted from the inode (inode->root->fs_info). The only exception
is in the self tests where we allocate an extent map tree and then use it
to insert/update/remove extent maps. However the tests can be changed to
use a test inode and then use the inode's extent map tree.

So change btrfs_add_extent_mapping() to have an inode as an argument
instead of a fs_info and an extent map tree. This reduces the number of
parameters and will also be needed for an upcoming change.
Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 236e3107
...@@ -546,10 +546,9 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, ...@@ -546,10 +546,9 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
} }
/* /*
* Add extent mapping into em_tree. * Add extent mapping into an inode's extent map tree.
* *
* @fs_info: the filesystem * @inode: target inode
* @em_tree: extent tree into which we want to insert the extent mapping
* @em_in: extent we are inserting * @em_in: extent we are inserting
* @start: start of the logical range btrfs_get_extent() is requesting * @start: start of the logical range btrfs_get_extent() is requesting
* @len: length of the logical range btrfs_get_extent() is requesting * @len: length of the logical range btrfs_get_extent() is requesting
...@@ -557,8 +556,8 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, ...@@ -557,8 +556,8 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
* Note that @em_in's range may be different from [start, start+len), * Note that @em_in's range may be different from [start, start+len),
* but they must be overlapped. * but they must be overlapped.
* *
* Insert @em_in into @em_tree. In case there is an overlapping range, handle * Insert @em_in into the inode's extent map tree. In case there is an
* the -EEXIST by either: * overlapping range, handle the -EEXIST by either:
* a) Returning the existing extent in @em_in if @start is within the * a) Returning the existing extent in @em_in if @start is within the
* existing em. * existing em.
* b) Merge the existing extent with @em_in passed in. * b) Merge the existing extent with @em_in passed in.
...@@ -566,12 +565,13 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, ...@@ -566,12 +565,13 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
* Return 0 on success, otherwise -EEXIST. * Return 0 on success, otherwise -EEXIST.
* *
*/ */
int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, int btrfs_add_extent_mapping(struct btrfs_inode *inode,
struct extent_map_tree *em_tree,
struct extent_map **em_in, u64 start, u64 len) struct extent_map **em_in, u64 start, u64 len)
{ {
int ret; int ret;
struct extent_map *em = *em_in; struct extent_map *em = *em_in;
struct extent_map_tree *em_tree = &inode->extent_tree;
struct btrfs_fs_info *fs_info = inode->root->fs_info;
/* /*
* Tree-checker should have rejected any inline extent with non-zero * Tree-checker should have rejected any inline extent with non-zero
......
...@@ -132,8 +132,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen); ...@@ -132,8 +132,7 @@ int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em); void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
struct extent_map *search_extent_mapping(struct extent_map_tree *tree, struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
u64 start, u64 len); u64 start, u64 len);
int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, int btrfs_add_extent_mapping(struct btrfs_inode *inode,
struct extent_map_tree *em_tree,
struct extent_map **em_in, u64 start, u64 len); struct extent_map **em_in, u64 start, u64 len);
void btrfs_drop_extent_map_range(struct btrfs_inode *inode, void btrfs_drop_extent_map_range(struct btrfs_inode *inode,
u64 start, u64 end, u64 start, u64 end,
......
...@@ -6972,7 +6972,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, ...@@ -6972,7 +6972,7 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
} }
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); ret = btrfs_add_extent_mapping(inode, &em, start, len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
out: out:
btrfs_free_path(path); btrfs_free_path(path);
......
...@@ -53,9 +53,9 @@ static void free_extent_map_tree(struct extent_map_tree *em_tree) ...@@ -53,9 +53,9 @@ static void free_extent_map_tree(struct extent_map_tree *em_tree)
* ->add_extent_mapping(0, 16K) * ->add_extent_mapping(0, 16K)
* -> #handle -EEXIST * -> #handle -EEXIST
*/ */
static int test_case_1(struct btrfs_fs_info *fs_info, static int test_case_1(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
struct extent_map_tree *em_tree)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
u64 start = 0; u64 start = 0;
u64 len = SZ_8K; u64 len = SZ_8K;
...@@ -73,7 +73,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, ...@@ -73,7 +73,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info,
em->block_start = 0; em->block_start = 0;
em->block_len = SZ_16K; em->block_len = SZ_16K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [0, 16K)"); test_err("cannot add extent range [0, 16K)");
...@@ -94,7 +94,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, ...@@ -94,7 +94,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info,
em->block_start = SZ_32K; /* avoid merging */ em->block_start = SZ_32K; /* avoid merging */
em->block_len = SZ_4K; em->block_len = SZ_4K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [16K, 20K)"); test_err("cannot add extent range [16K, 20K)");
...@@ -115,7 +115,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, ...@@ -115,7 +115,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info,
em->block_start = start; em->block_start = start;
em->block_len = len; em->block_len = len;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret) { if (ret) {
test_err("case1 [%llu %llu]: ret %d", start, start + len, ret); test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
...@@ -148,9 +148,9 @@ static int test_case_1(struct btrfs_fs_info *fs_info, ...@@ -148,9 +148,9 @@ static int test_case_1(struct btrfs_fs_info *fs_info,
* Reading the inline ending up with EEXIST, ie. read an inline * Reading the inline ending up with EEXIST, ie. read an inline
* extent and discard page cache and read it again. * extent and discard page cache and read it again.
*/ */
static int test_case_2(struct btrfs_fs_info *fs_info, static int test_case_2(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
struct extent_map_tree *em_tree)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
int ret; int ret;
...@@ -166,7 +166,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info, ...@@ -166,7 +166,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info,
em->block_start = EXTENT_MAP_INLINE; em->block_start = EXTENT_MAP_INLINE;
em->block_len = (u64)-1; em->block_len = (u64)-1;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [0, 1K)"); test_err("cannot add extent range [0, 1K)");
...@@ -187,7 +187,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info, ...@@ -187,7 +187,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info,
em->block_start = SZ_4K; em->block_start = SZ_4K;
em->block_len = SZ_4K; em->block_len = SZ_4K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [4K, 8K)"); test_err("cannot add extent range [4K, 8K)");
...@@ -208,7 +208,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info, ...@@ -208,7 +208,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info,
em->block_start = EXTENT_MAP_INLINE; em->block_start = EXTENT_MAP_INLINE;
em->block_len = (u64)-1; em->block_len = (u64)-1;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret) { if (ret) {
test_err("case2 [0 1K]: ret %d", ret); test_err("case2 [0 1K]: ret %d", ret);
...@@ -235,8 +235,9 @@ static int test_case_2(struct btrfs_fs_info *fs_info, ...@@ -235,8 +235,9 @@ static int test_case_2(struct btrfs_fs_info *fs_info,
} }
static int __test_case_3(struct btrfs_fs_info *fs_info, static int __test_case_3(struct btrfs_fs_info *fs_info,
struct extent_map_tree *em_tree, u64 start) struct btrfs_inode *inode, u64 start)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
u64 len = SZ_4K; u64 len = SZ_4K;
int ret; int ret;
...@@ -253,7 +254,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, ...@@ -253,7 +254,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info,
em->block_start = SZ_4K; em->block_start = SZ_4K;
em->block_len = SZ_4K; em->block_len = SZ_4K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [4K, 8K)"); test_err("cannot add extent range [4K, 8K)");
...@@ -274,7 +275,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, ...@@ -274,7 +275,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info,
em->block_start = 0; em->block_start = 0;
em->block_len = SZ_16K; em->block_len = SZ_16K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); ret = btrfs_add_extent_mapping(inode, &em, start, len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret) { if (ret) {
test_err("case3 [%llu %llu): ret %d", test_err("case3 [%llu %llu): ret %d",
...@@ -322,25 +323,25 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, ...@@ -322,25 +323,25 @@ static int __test_case_3(struct btrfs_fs_info *fs_info,
* -> add_extent_mapping() * -> add_extent_mapping()
* -> add_extent_mapping() * -> add_extent_mapping()
*/ */
static int test_case_3(struct btrfs_fs_info *fs_info, static int test_case_3(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
struct extent_map_tree *em_tree)
{ {
int ret; int ret;
ret = __test_case_3(fs_info, em_tree, 0); ret = __test_case_3(fs_info, inode, 0);
if (ret) if (ret)
return ret; return ret;
ret = __test_case_3(fs_info, em_tree, SZ_8K); ret = __test_case_3(fs_info, inode, SZ_8K);
if (ret) if (ret)
return ret; return ret;
ret = __test_case_3(fs_info, em_tree, (12 * SZ_1K)); ret = __test_case_3(fs_info, inode, (12 * SZ_1K));
return ret; return ret;
} }
static int __test_case_4(struct btrfs_fs_info *fs_info, static int __test_case_4(struct btrfs_fs_info *fs_info,
struct extent_map_tree *em_tree, u64 start) struct btrfs_inode *inode, u64 start)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
u64 len = SZ_4K; u64 len = SZ_4K;
int ret; int ret;
...@@ -357,7 +358,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, ...@@ -357,7 +358,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info,
em->block_start = 0; em->block_start = 0;
em->block_len = SZ_8K; em->block_len = SZ_8K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [0, 8K)"); test_err("cannot add extent range [0, 8K)");
...@@ -378,7 +379,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, ...@@ -378,7 +379,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info,
em->block_start = SZ_16K; /* avoid merging */ em->block_start = SZ_16K; /* avoid merging */
em->block_len = 24 * SZ_1K; em->block_len = 24 * SZ_1K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("cannot add extent range [8K, 32K)"); test_err("cannot add extent range [8K, 32K)");
...@@ -398,7 +399,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, ...@@ -398,7 +399,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info,
em->block_start = 0; em->block_start = 0;
em->block_len = SZ_32K; em->block_len = SZ_32K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len); ret = btrfs_add_extent_mapping(inode, &em, start, len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret) { if (ret) {
test_err("case4 [%llu %llu): ret %d", test_err("case4 [%llu %llu): ret %d",
...@@ -450,23 +451,22 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, ...@@ -450,23 +451,22 @@ static int __test_case_4(struct btrfs_fs_info *fs_info,
* # handle -EEXIST when adding * # handle -EEXIST when adding
* # [0, 32K) * # [0, 32K)
*/ */
static int test_case_4(struct btrfs_fs_info *fs_info, static int test_case_4(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
struct extent_map_tree *em_tree)
{ {
int ret; int ret;
ret = __test_case_4(fs_info, em_tree, 0); ret = __test_case_4(fs_info, inode, 0);
if (ret) if (ret)
return ret; return ret;
ret = __test_case_4(fs_info, em_tree, SZ_4K); ret = __test_case_4(fs_info, inode, SZ_4K);
return ret; return ret;
} }
static int add_compressed_extent(struct btrfs_fs_info *fs_info, static int add_compressed_extent(struct btrfs_inode *inode,
struct extent_map_tree *em_tree,
u64 start, u64 len, u64 block_start) u64 start, u64 len, u64 block_start)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
int ret; int ret;
...@@ -482,7 +482,7 @@ static int add_compressed_extent(struct btrfs_fs_info *fs_info, ...@@ -482,7 +482,7 @@ static int add_compressed_extent(struct btrfs_fs_info *fs_info,
em->block_len = SZ_4K; em->block_len = SZ_4K;
em->flags |= EXTENT_FLAG_COMPRESS_ZLIB; em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
free_extent_map(em); free_extent_map(em);
if (ret < 0) { if (ret < 0) {
...@@ -588,53 +588,43 @@ static int validate_range(struct extent_map_tree *em_tree, int index) ...@@ -588,53 +588,43 @@ static int validate_range(struct extent_map_tree *em_tree, int index)
* They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from
* merging the em's. * merging the em's.
*/ */
static int test_case_5(struct btrfs_fs_info *fs_info) static int test_case_5(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
{ {
struct extent_map_tree *em_tree;
struct inode *inode;
u64 start, end; u64 start, end;
int ret; int ret;
test_msg("Running btrfs_drop_extent_map_range tests"); test_msg("Running btrfs_drop_extent_map_range tests");
inode = btrfs_new_test_inode();
if (!inode) {
test_std_err(TEST_ALLOC_INODE);
return -ENOMEM;
}
em_tree = &BTRFS_I(inode)->extent_tree;
/* [0, 12k) */ /* [0, 12k) */
ret = add_compressed_extent(fs_info, em_tree, 0, SZ_4K * 3, 0); ret = add_compressed_extent(inode, 0, SZ_4K * 3, 0);
if (ret) { if (ret) {
test_err("cannot add extent range [0, 12K)"); test_err("cannot add extent range [0, 12K)");
goto out; goto out;
} }
/* [12k, 24k) */ /* [12k, 24k) */
ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 3, SZ_4K * 3, SZ_4K); ret = add_compressed_extent(inode, SZ_4K * 3, SZ_4K * 3, SZ_4K);
if (ret) { if (ret) {
test_err("cannot add extent range [12k, 24k)"); test_err("cannot add extent range [12k, 24k)");
goto out; goto out;
} }
/* [24k, 36k) */ /* [24k, 36k) */
ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 6, SZ_4K * 3, SZ_8K); ret = add_compressed_extent(inode, SZ_4K * 6, SZ_4K * 3, SZ_8K);
if (ret) { if (ret) {
test_err("cannot add extent range [12k, 24k)"); test_err("cannot add extent range [12k, 24k)");
goto out; goto out;
} }
/* [36k, 40k) */ /* [36k, 40k) */
ret = add_compressed_extent(fs_info, em_tree, SZ_32K + SZ_4K, SZ_4K, SZ_4K * 3); ret = add_compressed_extent(inode, SZ_32K + SZ_4K, SZ_4K, SZ_4K * 3);
if (ret) { if (ret) {
test_err("cannot add extent range [12k, 24k)"); test_err("cannot add extent range [12k, 24k)");
goto out; goto out;
} }
/* [40k, 64k) */ /* [40k, 64k) */
ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 10, SZ_4K * 6, SZ_16K); ret = add_compressed_extent(inode, SZ_4K * 10, SZ_4K * 6, SZ_16K);
if (ret) { if (ret) {
test_err("cannot add extent range [12k, 24k)"); test_err("cannot add extent range [12k, 24k)");
goto out; goto out;
...@@ -643,36 +633,36 @@ static int test_case_5(struct btrfs_fs_info *fs_info) ...@@ -643,36 +633,36 @@ static int test_case_5(struct btrfs_fs_info *fs_info)
/* Drop [8k, 12k) */ /* Drop [8k, 12k) */
start = SZ_8K; start = SZ_8K;
end = (3 * SZ_4K) - 1; end = (3 * SZ_4K) - 1;
btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = validate_range(&BTRFS_I(inode)->extent_tree, 0); ret = validate_range(&inode->extent_tree, 0);
if (ret) if (ret)
goto out; goto out;
/* Drop [12k, 20k) */ /* Drop [12k, 20k) */
start = SZ_4K * 3; start = SZ_4K * 3;
end = SZ_16K + SZ_4K - 1; end = SZ_16K + SZ_4K - 1;
btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = validate_range(&BTRFS_I(inode)->extent_tree, 1); ret = validate_range(&inode->extent_tree, 1);
if (ret) if (ret)
goto out; goto out;
/* Drop [28k, 32k) */ /* Drop [28k, 32k) */
start = SZ_32K - SZ_4K; start = SZ_32K - SZ_4K;
end = SZ_32K - 1; end = SZ_32K - 1;
btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = validate_range(&BTRFS_I(inode)->extent_tree, 2); ret = validate_range(&inode->extent_tree, 2);
if (ret) if (ret)
goto out; goto out;
/* Drop [32k, 64k) */ /* Drop [32k, 64k) */
start = SZ_32K; start = SZ_32K;
end = SZ_64K - 1; end = SZ_64K - 1;
btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, false); btrfs_drop_extent_map_range(inode, start, end, false);
ret = validate_range(&BTRFS_I(inode)->extent_tree, 3); ret = validate_range(&inode->extent_tree, 3);
if (ret) if (ret)
goto out; goto out;
out: out:
iput(inode); free_extent_map_tree(&inode->extent_tree);
return ret; return ret;
} }
...@@ -681,23 +671,25 @@ static int test_case_5(struct btrfs_fs_info *fs_info) ...@@ -681,23 +671,25 @@ static int test_case_5(struct btrfs_fs_info *fs_info)
* for areas between two existing ems. Validate it doesn't do this when there * for areas between two existing ems. Validate it doesn't do this when there
* are two unmerged em's side by side. * are two unmerged em's side by side.
*/ */
static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em_tree) static int test_case_6(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
{ {
struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em = NULL; struct extent_map *em = NULL;
int ret; int ret;
ret = add_compressed_extent(fs_info, em_tree, 0, SZ_4K, 0); ret = add_compressed_extent(inode, 0, SZ_4K, 0);
if (ret) if (ret)
goto out; goto out;
ret = add_compressed_extent(fs_info, em_tree, SZ_4K, SZ_4K, 0); ret = add_compressed_extent(inode, SZ_4K, SZ_4K, 0);
if (ret) if (ret)
goto out; goto out;
em = alloc_extent_map(); em = alloc_extent_map();
if (!em) { if (!em) {
test_std_err(TEST_ALLOC_EXTENT_MAP); test_std_err(TEST_ALLOC_EXTENT_MAP);
return -ENOMEM; ret = -ENOMEM;
goto out;
} }
em->start = SZ_4K; em->start = SZ_4K;
...@@ -705,7 +697,7 @@ static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em ...@@ -705,7 +697,7 @@ static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em
em->block_start = SZ_16K; em->block_start = SZ_16K;
em->block_len = SZ_16K; em->block_len = SZ_16K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, 0, SZ_8K); ret = btrfs_add_extent_mapping(inode, &em, 0, SZ_8K);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret != 0) { if (ret != 0) {
...@@ -734,28 +726,19 @@ static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em ...@@ -734,28 +726,19 @@ static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em
* true would mess up the start/end calculations and subsequent splits would be * true would mess up the start/end calculations and subsequent splits would be
* incorrect. * incorrect.
*/ */
static int test_case_7(struct btrfs_fs_info *fs_info) static int test_case_7(struct btrfs_fs_info *fs_info, struct btrfs_inode *inode)
{ {
struct extent_map_tree *em_tree; struct extent_map_tree *em_tree = &inode->extent_tree;
struct extent_map *em; struct extent_map *em;
struct inode *inode;
int ret; int ret;
int ret2;
test_msg("Running btrfs_drop_extent_cache with pinned"); test_msg("Running btrfs_drop_extent_cache with pinned");
inode = btrfs_new_test_inode();
if (!inode) {
test_std_err(TEST_ALLOC_INODE);
return -ENOMEM;
}
em_tree = &BTRFS_I(inode)->extent_tree;
em = alloc_extent_map(); em = alloc_extent_map();
if (!em) { if (!em) {
test_std_err(TEST_ALLOC_EXTENT_MAP); test_std_err(TEST_ALLOC_EXTENT_MAP);
ret = -ENOMEM; return -ENOMEM;
goto out;
} }
/* [0, 16K), pinned */ /* [0, 16K), pinned */
...@@ -765,7 +748,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info) ...@@ -765,7 +748,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info)
em->block_len = SZ_4K; em->block_len = SZ_4K;
em->flags |= EXTENT_FLAG_PINNED; em->flags |= EXTENT_FLAG_PINNED;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("couldn't add extent map"); test_err("couldn't add extent map");
...@@ -786,7 +769,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info) ...@@ -786,7 +769,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info)
em->block_start = SZ_32K; em->block_start = SZ_32K;
em->block_len = SZ_16K; em->block_len = SZ_16K;
write_lock(&em_tree->lock); write_lock(&em_tree->lock);
ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len); ret = btrfs_add_extent_mapping(inode, &em, em->start, em->len);
write_unlock(&em_tree->lock); write_unlock(&em_tree->lock);
if (ret < 0) { if (ret < 0) {
test_err("couldn't add extent map"); test_err("couldn't add extent map");
...@@ -798,7 +781,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info) ...@@ -798,7 +781,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info)
* Drop [0, 36K) This should skip the [0, 4K) extent and then split the * Drop [0, 36K) This should skip the [0, 4K) extent and then split the
* [32K, 48K) extent. * [32K, 48K) extent.
*/ */
btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (36 * SZ_1K) - 1, true); btrfs_drop_extent_map_range(inode, 0, (36 * SZ_1K) - 1, true);
/* Make sure our extent maps look sane. */ /* Make sure our extent maps look sane. */
ret = -EINVAL; ret = -EINVAL;
...@@ -865,7 +848,11 @@ static int test_case_7(struct btrfs_fs_info *fs_info) ...@@ -865,7 +848,11 @@ static int test_case_7(struct btrfs_fs_info *fs_info)
ret = 0; ret = 0;
out: out:
free_extent_map(em); free_extent_map(em);
iput(inode); /* Unpin our extent to prevent warning when removing it below. */
ret2 = unpin_extent_cache(inode, 0, SZ_16K, 0);
if (ret == 0)
ret = ret2;
free_extent_map_tree(em_tree);
return ret; return ret;
} }
...@@ -959,7 +946,8 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info, ...@@ -959,7 +946,8 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info,
int btrfs_test_extent_map(void) int btrfs_test_extent_map(void)
{ {
struct btrfs_fs_info *fs_info = NULL; struct btrfs_fs_info *fs_info = NULL;
struct extent_map_tree *em_tree; struct inode *inode;
struct btrfs_root *root = NULL;
int ret = 0, i; int ret = 0, i;
struct rmap_test_vector rmap_tests[] = { struct rmap_test_vector rmap_tests[] = {
{ {
...@@ -1008,33 +996,42 @@ int btrfs_test_extent_map(void) ...@@ -1008,33 +996,42 @@ int btrfs_test_extent_map(void)
return -ENOMEM; return -ENOMEM;
} }
em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL); inode = btrfs_new_test_inode();
if (!em_tree) { if (!inode) {
test_std_err(TEST_ALLOC_INODE);
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto out;
} }
extent_map_tree_init(em_tree); root = btrfs_alloc_dummy_root(fs_info);
if (IS_ERR(root)) {
test_std_err(TEST_ALLOC_ROOT);
ret = PTR_ERR(root);
root = NULL;
goto out;
}
ret = test_case_1(fs_info, em_tree); BTRFS_I(inode)->root = root;
ret = test_case_1(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_2(fs_info, em_tree); ret = test_case_2(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_3(fs_info, em_tree); ret = test_case_3(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_4(fs_info, em_tree); ret = test_case_4(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_5(fs_info); ret = test_case_5(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_6(fs_info, em_tree); ret = test_case_6(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
ret = test_case_7(fs_info); ret = test_case_7(fs_info, BTRFS_I(inode));
if (ret) if (ret)
goto out; goto out;
...@@ -1046,7 +1043,8 @@ int btrfs_test_extent_map(void) ...@@ -1046,7 +1043,8 @@ int btrfs_test_extent_map(void)
} }
out: out:
kfree(em_tree); iput(inode);
btrfs_free_dummy_root(root);
btrfs_free_dummy_fs_info(fs_info); btrfs_free_dummy_fs_info(fs_info);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment