unify origanization structure of block group cache
v2 cover unify origanization structure of block group cache
From: Su Yue <[email protected]>
In progs, block group caches are stored in btrfs_fs_info::block_group_cache whose type is cache_extent. All block group caches adding/finding/freeing are done in the misleading set/clear_extent_bits ways. However, kernel side uses red-black tree structure in btrfs_fs_info directly. The latter's structure is more reasonable and intuitive.
This patchset transforms structure of block group caches from cache_extent to red-black tree and list.
patch[1] handles error to avoid warning after reform.
patch[2-6] are about rb tree reform things in preparation. patch[7-8] are about dirty block groups linked in transaction in preparation. patch[9] does replace works in action.
patch[10] does cleanup.
This patchset passed progs tests and did not cause any regression.
---
Changelog:
v2:
Adjust block group cache tree seach and lookup functions to progs behaviors.
Use rbtree_postorder_for_each_entry_safe() in patch[9] (Qu WenRuo). Add reviewed-by tags.
Su Yue (10):
btrfs-progs: handle error if btrfs_write_one_block_group() failed
btrfs-progs: block_group: add rb tree related memebers
btrfs-progs: port block group cache tree insertion and lookup
functions
btrfs-progs: reform the function block_group_cache_tree_search()
btrfs-progs: adjust ported block group lookup functions in kernel
version
btrfs-progs: abstract function btrfs_add_block_group_cache()
block-progs: block_group: add dirty_bgs list related memebers
btrfs-progs: pass @trans to functions touch dirty block groups
btrfs-progs: reform block groups caches structure
btrfs-progs: cleanups after block group cache reform
check/main.c | 6 +- check/mode-lowmem.c | 6 +- cmds/rescue-chunk-recover.c | 10 +- ctree.h | 29 ++-- disk-io.c | 4 +- extent-tree.c | 304 +++++++++++++++--------------------- extent_io.h | 2 - image/main.c | 10 +- transaction.c | 8 +- transaction.h | 3 +- 10 files changed, 165 insertions(+), 217 deletions(-)
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 1/10 btrfs-progs: handle error if btrfs_write_one_block_group() failed
From: Su Yue <[email protected]>
Just break loop and return the error code if failed. Functions in the call chain are able to handle it.
--- extent-tree.c | 4 +++- transaction.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
extent-tree.c +3 -1
@@ -1596,9 +1596,11 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) cache = (struct btrfs_block_group_cache *)(unsigned long )ptr; ret =write_one_cache_group (trans, path, cache); +if (ret) +break ; }btrfs_free_path (path); -return 0 ; +return ret; } static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
transaction.c +3 -1
@@ -77,7 +77,9 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans, &root->root_item);if (ret <0 )return ret; -btrfs_write_dirty_block_groups (trans); + ret =btrfs_write_dirty_block_groups (trans); +if (ret) +return ret; }return 0 ; }
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 2/10 btrfs-progs: block_group: add rb tree related memebers
In reply to [email protected]From: Su Yue <[email protected]>
To convert from existed extent_cache to plain rb_tree, add btrfs_block_group_cache::cache_node and btrfs_fs_info::block_group_ cache_tree.
--- ctree.h | 21 ++++++++++++--------- disk-io.c | 2 ++ 2 files changed, 14 insertions(+), 9 deletions(-)
ctree.h +12 -9
@@ -1107,16 +1107,18 @@ struct btrfs_block_group_cache {int cached;int ro;/* -* If the free space extent count exceeds this number, convert the block -* group to bitmaps. -*/ -u32 bitmap_high_thresh; -/* -* If the free space extent count drops below this number, convert the -* block group back to extents. -*/ -u32 bitmap_low_thresh; +* If the free space extent count exceeds this number, convert the block +* group to bitmaps. +*/ +u32 bitmap_high_thresh; +/* +* If the free space extent count drops below this number, convert the +* block group back to extents. +*/ +u32 bitmap_low_thresh; +/* Block group cache stuff */ +struct rb_node cache_node; };struct btrfs_device ;
@@ -1146,6 +1148,7 @@ struct btrfs_fs_info {struct extent_io_tree extent_ins;struct extent_io_tree *excluded_extents; +struct rb_root block_group_cache_tree;/* logical->physical extent mapping */ struct btrfs_mapping_tree mapping_tree;
disk-io.c +2 -0
@@ -797,6 +797,8 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)extent_io_tree_init (&fs_info->block_group_cache);extent_io_tree_init (&fs_info->pinned_extents);extent_io_tree_init (&fs_info->extent_ins); + + fs_info->block_group_cache_tree =RB_ROOT ; fs_info->excluded_extents =NULL ; fs_info->fs_root_tree =RB_ROOT ;
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 3/10 btrfs-progs: port block group cache tree insertion and lookup functions
In reply to [email protected]From: Su Yue <[email protected]>
Simple copy and paste codes, remove useless lock operantions in progs. Th new coming lookup functions are named with suffix _kernel in temporary.
--- extent-tree.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
extent-tree.c +86 -0
@@ -164,6 +164,92 @@ err:return 0 ; } +/* +* This adds the block group to the fs_info rb tree for the block group cache +*/ +static int btrfs_add_block_group_cache (struct btrfs_fs_info *info, +struct btrfs_block_group_cache *block_group) +{ +struct rb_node **p; +struct rb_node *parent =NULL ; +struct btrfs_block_group_cache *cache; + + p = &info->block_group_cache_tree.rb_node; + +while (*p) { + parent = *p; + cache = rb_entry(parent, struct btrfs_block_group_cache, + cache_node); +if (block_group->key.objectid < cache->key.objectid) + p = &(*p)->rb_left; +else if (block_group->key.objectid > cache->key.objectid) + p = &(*p)->rb_right; +else +return -EEXIST ; + } + +rb_link_node (&block_group->cache_node, parent, p); +rb_insert_color (&block_group->cache_node, + &info->block_group_cache_tree); + +return 0 ; +} + +/* +* This will return the block group at or after bytenr if contains is 0, else +* it will return the block group that contains the bytenr +*/ +static struct btrfs_block_group_cache *block_group_cache_tree_search ( +struct btrfs_fs_info *info,u64 bytenr,int contains) +{ +struct btrfs_block_group_cache *cache, *ret =NULL ; +struct rb_node *n; +u64 end, start; + + n = info->block_group_cache_tree.rb_node; + +while (n) { + cache = rb_entry(n, struct btrfs_block_group_cache, + cache_node); + end = cache->key.objectid + cache->key.offset -1 ; + start = cache->key.objectid; + +if (bytenr < start) { +if (!contains && (!ret || start < ret->key.objectid)) + ret = cache; + n = n->rb_left; + }else if (bytenr > start) { +if (contains && bytenr <= end) { + ret = cache; +break ; + } + n = n->rb_right; + }else { + ret = cache; +break ; + } + } +return ret; +} + +/* +* Return the block group that starts at or after bytenr +*/ +struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel ( +struct btrfs_fs_info *info,u64 bytenr) +{ +return block_group_cache_tree_search (info, bytenr,0 ); +} + +/* +* Return the block group that contains the given bytenr +*/ +struct btrfs_block_group_cache *btrfs_lookup_block_group_kernel ( +struct btrfs_fs_info *info,u64 bytenr) +{ +return block_group_cache_tree_search (info, bytenr,1 ); +} +/* * Return the block group that contains @bytenr, otherwise return the next one * that starts after @bytenr
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 4/10 btrfs-progs: reform the function block_group_cache_tree_search()
In reply to [email protected]From: Su Yue <[email protected]>
Change @cotnains to @next of block_group_cache_tree_search(). Now, the function will try to search the block group containing the @bytenr. If not found, return NULL if @next is zero. Or It will return the next block group.
Will be used in the later commit.
--- extent-tree.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
extent-tree.c +10 -5
@@ -196,11 +196,15 @@ static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, }/* -* This will return the block group at or after bytenr if contains is 0, else -* it will return the block group that contains the bytenr +* This will return the block group which contains @bytenr if it exists. +* If found nothing, the return depends on @next. +* +* @next: +* if 0, return NULL if there's no block group containing the bytenr. +* if 1, return the block group which starts after @bytenr. */ static struct btrfs_block_group_cache *block_group_cache_tree_search ( -struct btrfs_fs_info *info,u64 bytenr,int contains) +struct btrfs_fs_info *info,u64 bytenr,int next) {struct btrfs_block_group_cache *cache, *ret =NULL ;struct rb_node *n;
@@ -215,11 +219,11 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search( start = cache->key.objectid;if (bytenr < start) { -if (!contains && (!ret || start < ret->key.objectid)) +if (next && (!ret || start < ret->key.objectid)) ret = cache; n = n->rb_left; }else if (bytenr > start) { -if (contains && bytenr <= end) { +if (bytenr <= end) { ret = cache;break ; }
@@ -229,6 +233,7 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search(break ; } } +return ret; }
Signature · 1 line
2.21.0 (Apple Git-122.2)
Re: [PATCH V2 04/10] btrfs-progs: reform the function block_group_cache_tree_search()
Quoted 10 lines · On 2019/12/18 下午1:18, [email protected] wrote:
From: Su Yue <[email protected]>
Change @cotnains to @next of
block_group_cache_tree_search(). Now, the function will try to search the block group containing the @bytenr. If not found, return NULL if @next is zero. Or It will return the next block group.Will be used in the later commit.
Signed-off-by Su Yue <[email protected]>
The @next looks pretty good, more clear than old @contains.
Thanks,
Qu
Quoted 50 lines
--- extent-tree.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)extent-tree.c +10
-5@@ -196,11 +196,15 @@ static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, }/* -* This will return the block group at or after bytenr if contains is 0, else -* it will return the block group that contains the bytenr +* This will return the block group which contains @bytenr if it exists. +* If found nothing, the return depends on @next. +* +* @next: +* if 0, return NULL if there's no block group containing the bytenr. +* if 1, return the block group which starts after @bytenr. */ static struct btrfs_block_group_cache *block_group_cache_tree_search ( -struct btrfs_fs_info *info,u64 bytenr,int contains) +struct btrfs_fs_info *info,u64 bytenr,int next) {struct btrfs_block_group_cache *cache, *ret =NULL ;struct rb_node *n;@@ -215,11 +219,11 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search( start = cache->key.objectid;if (bytenr < start) { -if (!contains && (!ret || start < ret->key.objectid)) +if (next && (!ret || start < ret->key.objectid)) ret = cache; n = n->rb_left; }else if (bytenr > start) { -if (contains && bytenr <= end) { +if (bytenr <= end) { ret = cache;break ; }@@ -229,6 +233,7 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search(break ; } } +return ret; }
PATCH v2 5/10 btrfs-progs: adjust ported block group lookup functions in kernel version
In reply to [email protected]From: Su Yue <[email protected]>
The are different behavior of btrfs_lookup_first_block_group() and btrfs_lookup_first_block_group_kernel().
There are many palaces calling the lookup function include extent allocation part. It's too complicated to check and change those. It will influence many functionalities in progs.
So here, just make kernel version lookup functions run likely in progs behavior.
--- extent-tree.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
extent-tree.c +4 -3
@@ -238,12 +238,13 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search( }/* -* Return the block group that starts at or after bytenr +* Return the block group that contains @bytenr, otherwise return the next one +* that starts after @bytenr */ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,0 ); +return block_group_cache_tree_search (info, bytenr,1 ); }/*
@@ -252,7 +253,7 @@ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel(struct btrfs_block_group_cache *btrfs_lookup_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,1 ); +return block_group_cache_tree_search (info, bytenr,0 ); }/*
Signature · 1 line
2.21.0 (Apple Git-122.2)
Re: [PATCH V2 05/10] btrfs-progs: adjust ported block group lookup functions in kernel version
Quoted 8 lines · On 2019/12/18 下午1:18, [email protected] wrote:
From: Su Yue <[email protected]>
The are different behavior of
btrfs_lookup_first_block_group()andbtrfs_lookup_first_block_group_kernel().
There are many palaces calling the lookup function include extent allocation part. It's too complicated to check and change those. It will influence many functionalities in progs.
Quoted 4 lines
So here, just make kernel version lookup functions run likely in progs behavior.
Signed-off-by Su Yue <[email protected]>
It should be folded into previous commit, or this will break bisect.
Thanks,
Qu
Quoted 34 lines
--- extent-tree.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)extent-tree.c +4
-3@@ -238,12 +238,13 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search( }/* -* Return the block group that starts at or after bytenr +* Return the block group that contains @bytenr, otherwise return the next one +* that starts after @bytenr */ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,0 ); +return block_group_cache_tree_search (info, bytenr,1 ); }/* @@ -252,7 +253,7 @@ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel(struct btrfs_block_group_cache *btrfs_lookup_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,1 ); +return block_group_cache_tree_search (info, bytenr,0 ); }/*
Re: [PATCH V2 05/10] btrfs-progs: adjust ported block group lookup functions in kernel version
Quoted 11 lines · Qu Wenruo
Quoted 8 lines · On 2019/12/18 下午1:18, [email protected] wrote:
From: Su Yue <[email protected]>
The are different behavior of
btrfs_lookup_first_block_group()andbtrfs_lookup_first_block_group_kernel().
There are many palaces calling the lookup function include extent allocation part. It's too complicated to check and change those. It will influence many functionalities in progs.
Quoted 7 lines
Quoted 4 lines
So here, just make kernel version lookup functions run likely in progs behavior.
Signed-off-by Su Yue <[email protected]>It should be folded into previous commit, or this will break bisect.
Oh, will do.
Thanks for your review.
Quoted 38 lines
Thanks,
QuQuoted 34 lines
--- extent-tree.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)extent-tree.c +4
-3@@ -238,12 +238,13 @@ static struct btrfs_block_group_cache *block_group_cache_tree_search( }/* -* Return the block group that starts at or after bytenr +* Return the block group that contains @bytenr, otherwise return the next one +* that starts after @bytenr */ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,0 ); +return block_group_cache_tree_search (info, bytenr,1 ); }/* @@ -252,7 +253,7 @@ struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel(struct btrfs_block_group_cache *btrfs_lookup_block_group_kernel (struct btrfs_fs_info *info,u64 bytenr) { -return block_group_cache_tree_search (info, bytenr,1 ); +return block_group_cache_tree_search (info, bytenr,0 ); }/*
PATCH v2 6/10 btrfs-progs: abstract function btrfs_add_block_group_cache()
In reply to [email protected]From: Su Yue <[email protected]>
The new function btrfs_add_block_group_cache() abstracts the old set_extent_bits and set_state_private operations.
Rename the rb tree version to btrfs_add_block_group_cache_kernel().
--- extent-tree.c | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-)
extent-tree.c +26 -24
@@ -164,10 +164,31 @@ err:return 0 ; } +static int btrfs_add_block_group_cache (struct btrfs_fs_info *info, +struct btrfs_block_group_cache *cache, +int bits) +{ +int ret; + + ret =set_extent_bits (&info->block_group_cache, cache->key.objectid, + cache->key.objectid + cache->key.offset -1 , + bits); +if (ret) +return ret; + + ret =set_state_private (&info->block_group_cache, cache->key.objectid, + (unsigned long )cache); +if (ret) +clear_extent_bits (&info->block_group_cache, cache->key.objectid, + cache->key.objectid + cache->key.offset -1 , + bits); +return ret; +} +/* * This adds the block group to the fs_info rb tree for the block group cache */ -static int btrfs_add_block_group_cache (struct btrfs_fs_info *info, +static int btrfs_add_block_group_cache_kernel (struct btrfs_fs_info *info,struct btrfs_block_group_cache *block_group) {struct rb_node **p;
@@ -2764,7 +2785,6 @@ error:static int read_one_block_group (struct btrfs_fs_info *fs_info,struct btrfs_path *path) { -struct extent_io_tree *block_group_cache = &fs_info->block_group_cache;struct extent_buffer *leaf = path->nodes[0 ];struct btrfs_space_info *space_info;struct btrfs_block_group_cache *cache;
@@ -2814,11 +2834,7 @@ static int read_one_block_group(struct btrfs_fs_info *fs_info, } cache->space_info = space_info; -set_extent_bits (block_group_cache, cache->key.objectid, - cache->key.objectid + cache->key.offset -1 , - bit |EXTENT_LOCKED ); -set_state_private (block_group_cache, cache->key.objectid, - (unsigned long )cache); +btrfs_add_block_group_cache (fs_info, cache, bit |EXTENT_LOCKED );return 0 ; }
@@ -2870,9 +2886,6 @@ btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,int ret;int bit =0 ;struct btrfs_block_group_cache *cache; -struct extent_io_tree *block_group_cache; - - block_group_cache = &fs_info->block_group_cache; cache =kzalloc (sizeof (*cache),GFP_NOFS );BUG_ON (!cache);
@@ -2889,13 +2902,8 @@ btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,BUG_ON (ret); bit =block_group_state_bits (type); - ret =set_extent_bits (block_group_cache, chunk_offset, - chunk_offset + size -1 , - bit |EXTENT_LOCKED ); -BUG_ON (ret); - ret =set_state_private (block_group_cache, chunk_offset, - (unsigned long )cache); + ret =btrfs_add_block_group_cache (fs_info, cache, bit |EXTENT_LOCKED );BUG_ON (ret);set_avail_alloc_bits (fs_info, type);
@@ -2945,9 +2953,7 @@ int btrfs_make_block_groups(struct btrfs_trans_handle *trans,int bit;struct btrfs_root *extent_root = fs_info->extent_root;struct btrfs_block_group_cache *cache; -struct extent_io_tree *block_group_cache; - block_group_cache = &fs_info->block_group_cache; total_bytes =btrfs_super_total_bytes (fs_info->super_copy); group_align =64 * fs_info->sectorsize;
@@ -2991,12 +2997,8 @@ int btrfs_make_block_groups(struct btrfs_trans_handle *trans, 0, &cache->space_info);BUG_ON (ret);set_avail_alloc_bits (fs_info, group_type); - -set_extent_bits (block_group_cache, cur_start, - cur_start + group_size -1 , - bit |EXTENT_LOCKED ); -set_state_private (block_group_cache, cur_start, - (unsigned long )cache); +btrfs_add_block_group_cache (fs_info, cache, + bit |EXTENT_LOCKED ); cur_start += group_size; }/* then insert all the items */
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 7/10 block-progs: block_group: add dirty_bgs list related memebers
In reply to [email protected]From: Su Yue <[email protected]>
The old style uses extent bit BLOCK_GROUP_DIRTY to mark dirty block groups in extent cache. To replace it, add btrfs_trans_handle::dirty_bgs and btrfs_block_group_cache::dirty_list.
--- ctree.h | 3 +++ extent-tree.c | 4 ++++ transaction.c | 1 + transaction.h | 3 ++- 4 files changed, 10 insertions(+), 1 deletion(-)
ctree.h +3 -0
@@ -1119,6 +1119,9 @@ struct btrfs_block_group_cache {/* Block group cache stuff */ struct rb_node cache_node; + +/* For dirty block groups */ +struct list_head dirty_list; };struct btrfs_device ;
extent-tree.c +4 -0
@@ -2814,6 +2814,8 @@ static int read_one_block_group(struct btrfs_fs_info *fs_info, cache->pinned =0 ; cache->flags =btrfs_block_group_flags (&bgi); cache->used =btrfs_block_group_used (&bgi); +INIT_LIST_HEAD (&cache->dirty_list); +if (cache->flags &BTRFS_BLOCK_GROUP_DATA ) { bit =BLOCK_GROUP_DATA ; }else if (cache->flags &BTRFS_BLOCK_GROUP_SYSTEM ) {
@@ -2895,6 +2897,7 @@ btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type, cache->key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; cache->used = bytes_used; cache->flags = type; +INIT_LIST_HEAD (&cache->dirty_list);exclude_super_stripes (fs_info, cache); ret = update_space_info(fs_info, cache->flags, size, bytes_used,
@@ -2992,6 +2995,7 @@ int btrfs_make_block_groups(struct btrfs_trans_handle *trans, cache->key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; cache->used =0 ; cache->flags = group_type; +INIT_LIST_HEAD (&cache->dirty_list); ret =update_space_info (fs_info, group_type, group_size,0 , &cache->space_info);
transaction.c +1 -0
@@ -52,6 +52,7 @@ struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root, root->last_trans = h->transid; root->commit_root = root->node;extent_buffer_get (root->node); +INIT_LIST_HEAD (&h->dirty_bgs);return h; }
transaction.h +2 -1
@@ -22,6 +22,7 @@#include "kerncompat.h" #include "ctree.h" #include "delayed-ref.h" +#include "kernel-lib/list.h" struct btrfs_trans_handle {struct btrfs_fs_info *fs_info;
@@ -35,7 +36,7 @@ struct btrfs_trans_handle {unsigned long blocks_used;struct btrfs_block_group_cache *block_group;struct btrfs_delayed_ref_root delayed_refs; - +struct list_head dirty_bgs; }; struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root,
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 8/10 btrfs-progs: pass @trans to functions touch dirty block groups
In reply to [email protected]From: Su Yue <[email protected]>
We are going to touch dirty_bgs in trans directly, so every call chain should pass paramemter @trans to end functions.
--- check/main.c | 6 +++--- check/mode-lowmem.c | 6 +++--- cmds/rescue-chunk-recover.c | 6 +++--- ctree.h | 4 ++-- extent-tree.c | 18 +++++++++--------- image/main.c | 5 +++-- 6 files changed, 23 insertions(+), 22 deletions(-)
check/main.c +3 -3
@@ -6651,8 +6651,8 @@ static int delete_extent_records(struct btrfs_trans_handle *trans,u64 bytes = (found_key.type ==BTRFS_EXTENT_ITEM_KEY ) ? found_key.offset : fs_info->nodesize; - ret =btrfs_update_block_group (fs_info->extent_root, - bytenr, bytes,0 ,0 ); + ret =btrfs_update_block_group (trans, bytenr, bytes, +0 ,0 );if (ret)break ; }
@@ -6730,7 +6730,7 @@ static int record_extent(struct btrfs_trans_handle *trans, }btrfs_mark_buffer_dirty (leaf); - ret =btrfs_update_block_group (extent_root, rec->start, + ret =btrfs_update_block_group (trans, rec->start, rec->max_size,1 ,0 );if (ret) goto fail;
check/mode-lowmem.c +3 -3
@@ -735,7 +735,7 @@ static int repair_tree_block_ref(struct btrfs_root *root, }btrfs_mark_buffer_dirty (eb);printf ("Added an extent item [%llu %u]\n" , bytenr, node_size); -btrfs_update_block_group (extent_root, bytenr, node_size,1 ,0 ); +btrfs_update_block_group (trans, bytenr, node_size,1 ,0 ); nrefs->refs[level] =0 ; nrefs->full_backref[level] =
@@ -3292,8 +3292,8 @@ static int repair_extent_data_item(struct btrfs_root *root,btrfs_set_extent_flags (eb, ei,BTRFS_EXTENT_FLAG_DATA );btrfs_mark_buffer_dirty (eb); - ret =btrfs_update_block_group (extent_root, disk_bytenr, - num_bytes,1 ,0 ); + ret =btrfs_update_block_group (trans, disk_bytenr, num_bytes, +1 ,0 );btrfs_release_path (&path); }
cmds/rescue-chunk-recover.c +3 -3
@@ -1084,7 +1084,7 @@ err:return ret; } -static int block_group_free_all_extent (struct btrfs_root *root, +static int block_group_free_all_extent (struct btrfs_trans_handle *trans,struct block_group_record *bg) {struct btrfs_block_group_cache *cache;
@@ -1092,7 +1092,7 @@ static int block_group_free_all_extent(struct btrfs_root *root,u64 start;u64 end; - info = root->fs_info; + info = trans->fs_info; cache =btrfs_lookup_block_group (info, bg->objectid);if (!cache)return -ENOENT ;
@@ -1124,7 +1124,7 @@ static int remove_chunk_extent_item(struct btrfs_trans_handle *trans,if (ret)return ret; - ret =block_group_free_all_extent (root, chunk->bg_rec); + ret =block_group_free_all_extent (trans, chunk->bg_rec);if (ret)return ret; }
ctree.h +2 -2
@@ -2568,8 +2568,8 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 type, u64 chunk_offset, u64 size);int btrfs_make_block_groups (struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info); -int btrfs_update_block_group (struct btrfs_root *root,u64 bytenr,u64 num, -int alloc,int mark_free); +int btrfs_update_block_group (struct btrfs_trans_handle *trans,u64 bytenr, +u64 num,int alloc,int mark_free); int btrfs_record_file_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid, struct btrfs_inode_item *inode,
extent-tree.c +9 -9
@@ -1872,9 +1872,10 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,return 0 ; } -static int update_block_group (struct btrfs_fs_info *info,u64 bytenr, +static int update_block_group (struct btrfs_trans_handle *trans,u64 bytenr,u64 num_bytes,int alloc,int mark_free) { +struct btrfs_fs_info *info = trans->fs_info;struct btrfs_block_group_cache *cache;u64 total = num_bytes;u64 old_val;
@@ -2237,8 +2238,7 @@ static int __free_extent(struct btrfs_trans_handle *trans, goto fail; } -update_block_group (trans->fs_info, bytenr, num_bytes,0 , - mark_free); +update_block_group (trans, bytenr, num_bytes,0 , mark_free); } fail:btrfs_free_path (path);
@@ -2570,7 +2570,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,if (ret)return ret; - ret =update_block_group (fs_info, ins.objectid, fs_info->nodesize,1 , + ret =update_block_group (trans, ins.objectid, fs_info->nodesize,1 ,0 );if (sinfo) {if (fs_info->nodesize > sinfo->bytes_reserved) {
@@ -3026,11 +3026,11 @@ int btrfs_make_block_groups(struct btrfs_trans_handle *trans,return 0 ; } -int btrfs_update_block_group (struct btrfs_root *root, +int btrfs_update_block_group (struct btrfs_trans_handle *trans,u64 bytenr,u64 num_bytes,int alloc,int mark_free) { -return update_block_group (root->fs_info, bytenr, num_bytes, +return update_block_group (trans, bytenr, num_bytes, alloc, mark_free); }
@@ -3444,12 +3444,12 @@ int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans)btrfs_item_key_to_cpu (leaf, &key, slot);if (key.type ==BTRFS_EXTENT_ITEM_KEY ) { bytes_used += key.offset; - ret =btrfs_update_block_group (root, + ret =btrfs_update_block_group (trans, key.objectid, key.offset,1 ,0 );BUG_ON (ret); }else if (key.type ==BTRFS_METADATA_ITEM_KEY ) { bytes_used += fs_info->nodesize; - ret =btrfs_update_block_group (root, + ret =btrfs_update_block_group (trans, key.objectid, fs_info->nodesize,1 ,0 );if (ret) goto out;
@@ -3604,7 +3604,7 @@ static int __btrfs_record_file_extent(struct btrfs_trans_handle *trans, BTRFS_EXTENT_FLAG_DATA);btrfs_mark_buffer_dirty (leaf); - ret =btrfs_update_block_group (root, disk_bytenr, + ret =btrfs_update_block_group (trans, disk_bytenr, num_bytes,1 ,0 );if (ret) goto fail;
image/main.c +3 -2
@@ -2338,8 +2338,9 @@ again:return 0 ; } -static void fixup_block_groups (struct btrfs_fs_info *fs_info) +static void fixup_block_groups (struct btrfs_trans_handle *trans) { +struct btrfs_fs_info *fs_info = trans->fs_info;struct btrfs_block_group_cache *bg;struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;struct cache_extent *ce;
@@ -2499,7 +2500,7 @@ static int fixup_chunks_and_devices(struct btrfs_fs_info *fs_info,return PTR_ERR (trans); } -fixup_block_groups (fs_info); +fixup_block_groups (trans); ret =fixup_dev_extents (trans);if (ret <0 ) goto error;
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 9/10 btrfs-progs: reform block groups caches structure
In reply to [email protected]From: Su Yue <[email protected]>
This commit organises block groups cache in btrfs_fs_info::block_group_cache_tree. And any dirty block groups are linked in transaction_handle::dirty_bgs.
To keep coherence of bisect, it does almost replace in place:
1. Replace the old btrfs group lookup functions with new functions introduced in former commits.
2. set_extent_bits(..., BLOCK_GROUP_DIRYT) things are replaced by linking the block group cache into trans::dirty_bgs. Checking and clearing bits are transformed too.
3. set_extent_bits(..., bit | EXTENT_LOCKED) things are replaced by new the btrfs_add_block_group_cache() which inserts caches into btrfs_fs_info::block_group_cache_tree directly. Other operations are converted to tree operations.
--- cmds/rescue-chunk-recover.c | 4 +- extent-tree.c | 214 ++++++------------------------------ image/main.c | 5 +- transaction.c | 3 +- 4 files changed, 39 insertions(+), 187 deletions(-)
cmds/rescue-chunk-recover.c +2 -2
@@ -1100,8 +1100,8 @@ static int block_group_free_all_extent(struct btrfs_trans_handle *trans, start = cache->key.objectid; end = start + cache->key.offset -1 ; -set_extent_bits (&info->block_group_cache, start, end, -BLOCK_GROUP_DIRTY ); +if (list_empty (&cache->dirty_list)) +list_add_tail (&cache->dirty_list, &trans->dirty_bgs);set_extent_dirty (&info->free_space_cache, start, end); cache->used =0 ;
extent-tree.c +34 -180
372 lines not on this page · raw message
image/main.c +2 -3
@@ -2365,9 +2365,8 @@ static void fixup_block_groups(struct btrfs_trans_handle *trans)/* Update the block group item and mark the bg dirty */ bg->flags = map->type; -set_extent_bits (&fs_info->block_group_cache, ce->start, - ce->start + ce->size -1 ,BLOCK_GROUP_DIRTY ); - +if (list_empty (&bg->dirty_list)) +list_add_tail (&bg->dirty_list, &trans->dirty_bgs);/* * Chunk and bg flags can be different, changing bg flags * without update avail_data/meta_alloc_bits will lead to
transaction.c +1 -2
@@ -203,8 +203,7 @@ commit_tree:* again, we need to exhause both dirty blocks and delayed refs */ while (!RB_EMPTY_ROOT (&trans->delayed_refs.href_root) || -test_range_bit (&fs_info->block_group_cache,0 , (u64)-1 , -BLOCK_GROUP_DIRTY ,0 )) { + !list_empty (&trans->dirty_bgs)) { ret =btrfs_write_dirty_block_groups (trans);if (ret <0 ) goto error;
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v2 10/10 btrfs-progs: cleanups after block group cache reform
In reply to [email protected]From: Su Yue <[email protected]>
btrfs_fs_info::block_group_cache and the bit BLOCK_GROUP_DIRY are useless. So is the block_group_state_bits().
Remove them.
--- ctree.h | 1 - disk-io.c | 2 -- extent-tree.c | 12 ------------ extent_io.h | 2 -- 4 files changed, 17 deletions(-)
ctree.h +0 -1
@@ -1146,7 +1146,6 @@ struct btrfs_fs_info {struct extent_io_tree extent_cache;struct extent_io_tree free_space_cache; -struct extent_io_tree block_group_cache;struct extent_io_tree pinned_extents;struct extent_io_tree extent_ins;struct extent_io_tree *excluded_extents;
disk-io.c +0 -2
@@ -794,7 +794,6 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)extent_io_tree_init (&fs_info->extent_cache);extent_io_tree_init (&fs_info->free_space_cache); -extent_io_tree_init (&fs_info->block_group_cache);extent_io_tree_init (&fs_info->pinned_extents);extent_io_tree_init (&fs_info->extent_ins);
@@ -1069,7 +1068,6 @@ void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)free_mapping_cache_tree (&fs_info->mapping_tree.cache_tree);extent_io_tree_cleanup (&fs_info->extent_cache);extent_io_tree_cleanup (&fs_info->free_space_cache); -extent_io_tree_cleanup (&fs_info->block_group_cache);extent_io_tree_cleanup (&fs_info->pinned_extents);extent_io_tree_cleanup (&fs_info->extent_ins); }
extent-tree.c +0 -12
@@ -329,18 +329,6 @@ wrapped: goto again; } -static int block_group_state_bits (u64 flags) -{ -int bits =0 ; -if (flags &BTRFS_BLOCK_GROUP_DATA ) - bits |=BLOCK_GROUP_DATA ; -if (flags &BTRFS_BLOCK_GROUP_METADATA ) - bits |=BLOCK_GROUP_METADATA ; -if (flags &BTRFS_BLOCK_GROUP_SYSTEM ) - bits |=BLOCK_GROUP_SYSTEM ; -return bits; -} -static struct btrfs_block_group_cache *btrfs_find_block_group (struct btrfs_root *root,struct btrfs_block_group_cache *hint,u64 search_start,int data,int owner)
extent_io.h +0 -2
@@ -47,8 +47,6 @@#define BLOCK_GROUP_METADATA (1U << 2)#define BLOCK_GROUP_SYSTEM (1U << 4) -#define BLOCK_GROUP_DIRTY (1U) -/* * The extent buffer bitmap operations are done with byte granularity instead of * word granularity for two reasons:
Signature · 1 line
2.21.0 (Apple Git-122.2)
Quoted 27 lines · On Wed, Dec 18, 2019 at 01:18:39PM +0800, [email protected] wrote:
From: Su Yue <[email protected]>
In progs, block group caches are stored in
btrfs_fs_info::block_group_cachewhose type iscache_extent. All block group caches adding/finding/freeing are done in the misleading set/clear_extent_bits ways. However, kernel side uses red-black tree structure inbtrfs_fs_infodirectly. The latter's structure is more reasonable and intuitive.This patchset transforms structure of block group caches from
cache_extentto red-black tree and list.patch[1] handles error to avoid warning after reform.
patch[2-6] are about rb tree reform things in preparation. patch[7-8] are about dirty block groups linked in transaction in preparation. patch[9] does replace works in action.
patch[10] does cleanup.This patchset passed progs tests and did not cause any regression.
---
Changelog:
v2:
Adjust block group cache tree seach and lookup functions to progs behaviors.
Userbtree_postorder_for_each_entry_safe()in patch[9] (Qu WenRuo). Add reviewed-by tags.
Quoted 13 lines
Su Yue (10):
btrfs-progs: handle error ifbtrfs_write_one_block_group()failed
btrfs-progs:block_group: add rb tree related memebers
btrfs-progs: port block group cache tree insertion and lookup
functions
btrfs-progs: reform the functionblock_group_cache_tree_search()
btrfs-progs: adjust ported block group lookup functions in kernel
version
btrfs-progs: abstract functionbtrfs_add_block_group_cache()
block-progs:block_group: adddirty_bgslist related memebers
btrfs-progs: pass @trans to functions touch dirty block groups
btrfs-progs: reform block groups caches structure
btrfs-progs: cleanups after block group cache reform
As the patches were reviewed by Qu, I've added them to devel. I've folded patch 5 to 4 as suggested. Thanks.