btrfs: some cleanups and optimizations for extent maps
Subsystems (2)
cover btrfs: some cleanups and optimizations for extent maps
From: Filipe Manana <[email protected]>
These do some cleanups around extent maps and their tests, as well as some optimizations. These are part of a larger work around extent maps, but as they are fairly independent and the remainder will take a longer while, sending these out separately. More details on the change logs.
Filipe Manana (11):
btrfs: assert extent map is not in a list when setting it up
btrfs: tests: fix error messages for test case 4 of extent map tests
btrfs: tests: do not ignore NULL extent maps for extent maps tests
btrfs: tests: print all values as decimal in messages for extent map tests
btrfs: unexport add_extent_mapping()
btrfs: remove redundant value assignment at btrfs_add_extent_mapping()
btrfs: log messages at unpin_extent_range() during unexpected cases
btrfs: avoid useless rbtree iterations when attempting to merge extent map
btrfs: make extent_map_end() argument const
btrfs: refactor mergable_maps() for more readability
btrfs: use the flags of an extent map to identify the compression type
fs/btrfs/compression.c | 4 +- fs/btrfs/defrag.c | 8 +- fs/btrfs/extent_io.c | 13 ++- fs/btrfs/extent_map.c | 139 ++++++++++++++++-------------- fs/btrfs/extent_map.h | 64 +++++++++++--- fs/btrfs/file-item.c | 9 +- fs/btrfs/file.c | 10 +-- fs/btrfs/inode.c | 35 ++++---- fs/btrfs/relocation.c | 2 +- fs/btrfs/tests/extent-map-tests.c | 103 +++++++++++++--------- fs/btrfs/tests/inode-tests.c | 60 ++++++------- fs/btrfs/tree-log.c | 16 ++-- include/trace/events/btrfs.h | 21 +++-- 13 files changed, 272 insertions(+), 212 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/11 btrfs: assert extent map is not in a list when setting it up
From: Filipe Manana <[email protected]>
When setting up a new extent map, at setup_extent_mapping(), we're doing a list move operation to add the extent map the tree's list of modified extents. This is confusing because at this point the extent map can not be in any list, because it's a new extent map. So replace the list move with a list add and add an assertion that checks that the extent map is not currently in any list.
--- fs/btrfs/extent_map.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/extent_map.c +3 -1
@@ -345,8 +345,10 @@ static inline void setup_extent_mapping(struct extent_map_tree *tree, em->mod_start = em->start; em->mod_len = em->len; +ASSERT (list_empty (&em->list)); +if (modified) -list_move (&em->list, &tree->modified_extents); +list_add (&em->list, &tree->modified_extents);else try_merge_map (tree, em); }
Signature · 1 line
2.40.1
PATCH 2/11 btrfs: tests: fix error messages for test case 4 of extent map tests
In reply to [email protected]From: Filipe Manana <[email protected]>
In test case 4 for extent maps, if we error out we are supposed to print in interval but instead of printing a non-inclusive end offset, we are printing the length of the interval, which makes it confusing. So fix that to print the exclusive end offset instead.
--- fs/btrfs/tests/extent-map-tests.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/tests/extent-map-tests.c +2 -2
@@ -388,13 +388,13 @@ static int __test_case_4(struct btrfs_fs_info *fs_info,write_unlock (&em_tree->lock);if (ret) {test_err ("case4 [0x%llx 0x%llx): ret %d" , - start, len, ret); + start, start + len, ret); goto out; }if (em && (start < em->start || start + len >extent_map_end (em))) {test_err ("case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)" , - start, len, ret, em->start, em->len, em->block_start, + start, start + len, ret, em->start, em->len, em->block_start, em->block_len); ret = -EINVAL ; }
Signature · 1 line
2.40.1
PATCH 3/11 btrfs: tests: do not ignore NULL extent maps for extent maps tests
In reply to [email protected]From: Filipe Manana <[email protected]>
Several of the extent map tests call btrfs_add_extent_mapping() which is supposed to succeed and return an extent map through the pointer to pointer argument. However the tests are deliberately ignoring a NULL extent map, which is not expected to happen. So change the tests to error out if a NULL extent map is found.
--- fs/btrfs/tests/extent-map-tests.c | 40 +++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-)
fs/btrfs/tests/extent-map-tests.c +30 -10
@@ -121,9 +121,14 @@ static int test_case_1(struct btrfs_fs_info *fs_info,test_err ("case1 [%llu %llu]: ret %d" , start, start + len, ret); goto out; } -if (em && - (em->start !=0 ||extent_map_end (em) !=SZ_16K || - em->block_start !=0 || em->block_len !=SZ_16K )) { +if (!em) { +test_err ("case1 [%llu %llu]: no extent map returned" , + start, start + len); + ret = -ENOENT ; + goto out; + } +if (em->start !=0 ||extent_map_end (em) !=SZ_16K || + em->block_start !=0 || em->block_len !=SZ_16K ) { test_err( "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu", start, start + len, ret, em->start, em->len,
@@ -209,9 +214,13 @@ static int test_case_2(struct btrfs_fs_info *fs_info,test_err ("case2 [0 1K]: ret %d" , ret); goto out; } -if (em && - (em->start !=0 ||extent_map_end (em) !=SZ_1K || - em->block_start !=EXTENT_MAP_INLINE || em->block_len != (u64)-1 )) { +if (!em) { +test_err ("case2 [0 1K]: no extent map returned" ); + ret = -ENOENT ; + goto out; + } +if (em->start !=0 ||extent_map_end (em) !=SZ_1K || + em->block_start !=EXTENT_MAP_INLINE || em->block_len != (u64)-1 ) { test_err( "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu", ret, em->start, em->len, em->block_start,
@@ -272,13 +281,18 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, start, start + len, ret); goto out; } +if (!em) { +test_err ("case3 [0x%llx 0x%llx): no extent map returned" , + start, start + len); + ret = -ENOENT ; + goto out; + }/* * Since bytes within em are contiguous, em->block_start is identical to * em->start. */ -if (em && - (start < em->start || start + len >extent_map_end (em) || - em->start != em->block_start || em->len != em->block_len)) { +if (start < em->start || start + len >extent_map_end (em) || + em->start != em->block_start || em->len != em->block_len) { test_err( "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)", start, start + len, ret, em->start, em->len,
@@ -391,7 +405,13 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, start, start + len, ret); goto out; } -if (em && (start < em->start || start + len >extent_map_end (em))) { +if (!em) { +test_err ("case4 [0x%llx 0x%llx): no extent map returned" , + start, start + len); + ret = -ENOENT ; + goto out; + } +if (start < em->start || start + len >extent_map_end (em)) { test_err( "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)", start, start + len, ret, em->start, em->len, em->block_start,
Signature · 1 line
2.40.1
PATCH 4/11 btrfs: tests: print all values as decimal in messages for extent map tests
In reply to [email protected]From: Filipe Manana <[email protected]>
Some error messages of the extent map tests print decimal values of start offsets and lengths, while other are oddly printing in hexadecimal, which is far less human friendly, specially taking into consideration that all the values are small and multiples of 4K, so it's a lot easier to read them as decimal values. Change the format specifiers to print as decimal instead.
--- fs/btrfs/tests/extent-map-tests.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/tests/extent-map-tests.c +7 -7
@@ -25,7 +25,7 @@ static void free_extent_map_tree(struct extent_map_tree *em_tree)#ifdef CONFIG_BTRFS_DEBUG if (refcount_read (&em->refs) !=1 ) {test_err ( -"em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d" , +"em leak: em (start %llu len %llu block_start %llu block_len %llu) refs %d" , em->start, em->len, em->block_start, em->block_len,refcount_read (&em->refs));
@@ -277,12 +277,12 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, ret =btrfs_add_extent_mapping (fs_info, em_tree, &em, start, len);write_unlock (&em_tree->lock);if (ret) { -test_err ("case3 [0x%llx 0x%llx): ret %d" , +test_err ("case3 [%llu %llu): ret %d" , start, start + len, ret); goto out; }if (!em) { -test_err ("case3 [0x%llx 0x%llx): no extent map returned" , +test_err ("case3 [%llu %llu): no extent map returned" , start, start + len); ret = -ENOENT ; goto out;
@@ -294,7 +294,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info,if (start < em->start || start + len >extent_map_end (em) || em->start != em->block_start || em->len != em->block_len) {test_err ( -"case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)" , +"case3 [%llu %llu): ret %d em (start %llu len %llu block_start %llu block_len %llu)" , start, start + len, ret, em->start, em->len, em->block_start, em->block_len); ret = -EINVAL ;
@@ -401,19 +401,19 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, ret =btrfs_add_extent_mapping (fs_info, em_tree, &em, start, len);write_unlock (&em_tree->lock);if (ret) { -test_err ("case4 [0x%llx 0x%llx): ret %d" , +test_err ("case4 [%llu %llu): ret %d" , start, start + len, ret); goto out; }if (!em) { -test_err ("case4 [0x%llx 0x%llx): no extent map returned" , +test_err ("case4 [%llu %llu): no extent map returned" , start, start + len); ret = -ENOENT ; goto out; }if (start < em->start || start + len >extent_map_end (em)) {test_err ( -"case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)" , +"case4 [%llu %llu): ret %d, added wrong em (start %llu len %llu block_start %llu block_len %llu)" , start, start + len, ret, em->start, em->len, em->block_start, em->block_len); ret = -EINVAL ;
Signature · 1 line
2.40.1
PATCH 5/11 btrfs: unexport add_extent_mapping()
In reply to [email protected]From: Filipe Manana <[email protected]>
There's no need to export add_extent_mapping(), as it's only used inside extent_map.c and in the self tests. For the tests we can use instead btrfs_add_extent_mapping(), which will accomplish exactly the same as we don't expect collisions in any of them. So unexport it and make the tests use btrfs_add_extent_mapping() instead of add_extent_mapping().
--- fs/btrfs/extent_map.c | 4 +-- fs/btrfs/extent_map.h | 2 -- fs/btrfs/tests/extent-map-tests.c | 45 ++++++++++++++++--------------- 3 files changed, 25 insertions(+), 26 deletions(-)
fs/btrfs/extent_map.c +2 -2
@@ -366,8 +366,8 @@ static inline void setup_extent_mapping(struct extent_map_tree *tree,* into the tree directly, with an additional reference taken, or a * reference dropped if the merge attempt was successful. */ -int add_extent_mapping (struct extent_map_tree *tree, -struct extent_map *em,int modified) +static int add_extent_mapping (struct extent_map_tree *tree, +struct extent_map *em,int modified) {int ret =0 ;
fs/btrfs/extent_map.h +0 -2
@@ -74,8 +74,6 @@ static inline u64 extent_map_end(struct extent_map *em)void extent_map_tree_init (struct extent_map_tree *tree);struct extent_map *lookup_extent_mapping (struct extent_map_tree *tree,u64 start,u64 len); -int add_extent_mapping (struct extent_map_tree *tree, -struct extent_map *em,int modified);void remove_extent_mapping (struct extent_map_tree *tree,struct extent_map *em);int split_extent_map (struct btrfs_inode *inode,u64 start,u64 len,u64 pre,u64 new_logical);
fs/btrfs/tests/extent-map-tests.c +23 -22
173 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH 6/11 btrfs: remove redundant value assignment at btrfs_add_extent_mapping()
In reply to [email protected]From: Filipe Manana <[email protected]>
At btrfs_add_extent_mapping(), in case add_extent_mapping() returned -EEXIST, it's pointless to assign 0 to 'ret' since we will assign a value to it shortly after, without 'ret' being used before that. So remove that pointless assignment.
--- fs/btrfs/extent_map.c | 2 -- 1 file changed, 2 deletions(-)
fs/btrfs/extent_map.c +0 -2
@@ -586,8 +586,6 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,if (ret == -EEXIST ) {struct extent_map *existing; - ret =0 ; - existing =search_extent_mapping (em_tree, start, len);trace_btrfs_handle_em_exist (fs_info, existing, em, start, len);
Signature · 1 line
2.40.1
PATCH 7/11 btrfs: log messages at unpin_extent_range() during unexpected cases
In reply to [email protected]From: Filipe Manana <[email protected]>
At unpin_extent_range() we trigger a WARN_ON() when we don't find an extent map or we find one with a start offset not matching the start offset of the target range. This however isn't very useful for debugging because:
1) We don't know which condition was triggered, as they are both in the same WARN_ON() call;
2) We don't know which inode was affected, from which root, for which range, what's the start offset of the extent map, and so on.
So trigger a separate warning for each case and log a message for each case providing information about the inode, its root, the target range, the generation and the start offset of the extent map we found.
--- fs/btrfs/extent_map.c | 22 ++++++++++++++++------ fs/btrfs/extent_map.h | 2 +- fs/btrfs/inode.c | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-)
fs/btrfs/extent_map.c +16 -6
@@ -280,7 +280,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)/* * Unpin an extent from the cache. * -* @tree: tree to unpin the extent in +* @inode: the inode from which we are unpinning an extent range * @start: logical offset in the file * @len: length of the extent * @gen: generation that this extent has been modified in
@@ -289,9 +289,10 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)* to the generation that actually added the file item to the inode so we know * we need to sync this extent when we call fsync(). */ -int unpin_extent_cache (struct extent_map_tree *tree,u64 start,u64 len, -u64 gen) +int unpin_extent_cache (struct btrfs_inode *inode,u64 start,u64 len,u64 gen) { +struct btrfs_fs_info *fs_info = inode->root->fs_info; +struct extent_map_tree *tree = &inode->extent_tree;int ret =0 ;struct extent_map *em;bool prealloc = false;
@@ -299,10 +300,19 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,write_lock (&tree->lock); em =lookup_extent_mapping (tree, start, len); -WARN_ON (!em || em->start != start); - -if (!em) +if (WARN_ON (!em)) { +btrfs_warn (fs_info, +"no extent map found for inode %llu (root %lld) when unpinning extent range [%llu, %llu), generation %llu" , +btrfs_ino (inode),btrfs_root_id (inode->root), + start, len, gen); goto out; + } + +if (WARN_ON (em->start != start)) +btrfs_warn (fs_info, +"found extent map for inode %llu (root %lld) with unexpected start offset %llu when unpinning extent range [%llu, %llu), generation %llu" , +btrfs_ino (inode),btrfs_root_id (inode->root), + em->start, start, len, gen); em->generation = gen;clear_bit (EXTENT_FLAG_PINNED , &em->flags);
fs/btrfs/extent_map.h +1 -1
@@ -82,7 +82,7 @@ struct extent_map *alloc_extent_map(void);void free_extent_map (struct extent_map *em);int __init extent_map_init (void );void __cold extent_map_exit (void ); -int unpin_extent_cache (struct extent_map_tree *tree,u64 start,u64 len,u64 gen); +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);struct extent_map *search_extent_mapping (struct extent_map_tree *tree,u64 start,u64 len);
fs/btrfs/inode.c +1 -1
@@ -3127,7 +3127,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) ordered_extent->disk_num_bytes); } } -unpin_extent_cache (&inode->extent_tree, ordered_extent->file_offset, +unpin_extent_cache (inode, ordered_extent->file_offset, ordered_extent->num_bytes, trans->transid);if (ret <0 ) {btrfs_abort_transaction (trans, ret);
Signature · 1 line
2.40.1
PATCH 8/11 btrfs: avoid useless rbtree iterations when attempting to merge extent map
In reply to [email protected]From: Filipe Manana <[email protected]>
When trying to merge an extent map that was just inserted or unpinned, we will try to merge it with any adjacent extent map that is suitable.
However we will only check if our extent map is mergeable after searching for the previous and next extent maps in the rbtree, meaning that we are doing unnecessary calls to rb_prev() and rb_next() in case our extent map is not mergeable (it's compressed, in the list of modifed extents, being logged or pinned), wasting CPU time chasing rbtree pointers and pulling in unnecessary cache lines.
So change the logic to check first if an extent map is mergeable before searching for the next and previous extent maps in the rbtree.
--- fs/btrfs/extent_map.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-)
fs/btrfs/extent_map.c +21 -17
@@ -187,31 +187,32 @@ static inline u64 extent_map_block_end(const struct extent_map *em)return em->block_start + em->block_len; } -/* Check to see if two extent_map structs are adjacent and safe to merge. */ -static int mergable_maps (struct extent_map *prev,struct extent_map *next) +static bool can_merge_extent_map (const struct extent_map *em) { -if (test_bit (EXTENT_FLAG_PINNED , &prev->flags)) -return 0 ; +if (test_bit (EXTENT_FLAG_PINNED , &em->flags)) +return false; -/* -* don't merge compressed extents, we need to know their -* actual size -*/ -if (test_bit (EXTENT_FLAG_COMPRESSED , &prev->flags)) -return 0 ; +/* Don't merge compressed extents, we need to know their actual size. */ +if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)) +return false; -if (test_bit (EXTENT_FLAG_LOGGING , &prev->flags) || -test_bit (EXTENT_FLAG_LOGGING , &next->flags)) -return 0 ; +if (test_bit (EXTENT_FLAG_LOGGING , &em->flags)) +return false;/* * We don't want to merge stuff that hasn't been written to the log yet * since it may not reflect exactly what is on disk, and that would be * bad. */ -if (!list_empty (&prev->list) || !list_empty (&next->list)) -return 0 ; +if (!list_empty (&em->list)) +return false; + +return true; +} +/* Check to see if two extent_map structs are adjacent and safe to merge. */ +static int mergable_maps (struct extent_map *prev,struct extent_map *next) +{ if (extent_map_end(prev) == next->start && prev->flags == next->flags && ((next->block_start == EXTENT_MAP_HOLE &&
@@ -241,11 +242,14 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)if (refcount_read (&em->refs) >2 )return ; +if (!can_merge_extent_map (em)) +return ; +if (em->start !=0 ) { rb =rb_prev (&em->rb_node);if (rb) merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&mergable_maps (merge, em)) { +if (rb &&can_merge_extent_map (merge) &&mergable_maps (merge, em)) { em->start = merge->start; em->orig_start = merge->orig_start; em->len += merge->len;
@@ -265,7 +269,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) rb =rb_next (&em->rb_node);if (rb) merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&mergable_maps (em, merge)) { +if (rb &&can_merge_extent_map (merge) &&mergable_maps (em, merge)) { em->len += merge->len; em->block_len += merge->block_len;rb_erase_cached (&merge->rb_node, &tree->map);
Signature · 1 line
2.40.1
PATCH 9/11 btrfs: make extent_map_end() argument const
In reply to [email protected]From: Filipe Manana <[email protected]>
The extent map pointer argument for extent_map_end() can be const as we are not modifyng anything in the extent map. So make it const, as it will allow further changes to callers that have a const extent map pointer.
--- fs/btrfs/extent_map.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_map.h +1 -1
@@ -64,7 +64,7 @@ static inline int extent_map_in_tree(const struct extent_map *em)return !RB_EMPTY_NODE (&em->rb_node); } -static inline u64 extent_map_end (struct extent_map *em) +static inline u64 extent_map_end (const struct extent_map *em) {if (em->start + em->len < em->start)return (u64)-1 ;
Signature · 1 line
2.40.1
PATCH 10/11 btrfs: refactor mergable_maps() for more readability
In reply to [email protected]From: Filipe Manana <[email protected]>
At mergable_maps() Instead of having a single if statement with many ORed and ANDed conditions, refacator it with mulple if statements that check a single condition and return immediately once a requirement fails. This makes it easier to read.
Also change the return type from int to bool, make the arguments const and rename the function from mergable_maps() to mergeable_maps().
--- fs/btrfs/extent_map.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
fs/btrfs/extent_map.c +14 -14
@@ -211,19 +211,19 @@ static bool can_merge_extent_map(const struct extent_map *em) }/* Check to see if two extent_map structs are adjacent and safe to merge. */ -static int mergable_maps (struct extent_map *prev,struct extent_map *next) +static bool mergeable_maps (const struct extent_map *prev,const struct extent_map *next) { -if (extent_map_end (prev) == next->start && - prev->flags == next->flags && - ((next->block_start ==EXTENT_MAP_HOLE && - prev->block_start ==EXTENT_MAP_HOLE ) || - (next->block_start ==EXTENT_MAP_INLINE && - prev->block_start ==EXTENT_MAP_INLINE ) || - (next->block_start <EXTENT_MAP_LAST_BYTE -1 && - next->block_start ==extent_map_block_end (prev)))) { -return 1 ; - } -return 0 ; +if (extent_map_end (prev) != next->start) +return false; + +if (prev->flags != next->flags) +return false; + +if (next->block_start <EXTENT_MAP_LAST_BYTE -1 ) +return next->block_start ==extent_map_block_end (prev); + +/* HOLES and INLINE extents */ +return next->block_start == prev->block_start; }static void try_merge_map (struct extent_map_tree *tree,struct extent_map *em)
@@ -249,7 +249,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) rb =rb_prev (&em->rb_node);if (rb) merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&can_merge_extent_map (merge) &&mergable_maps (merge, em)) { +if (rb &&can_merge_extent_map (merge) &&mergeable_maps (merge, em)) { em->start = merge->start; em->orig_start = merge->orig_start; em->len += merge->len;
@@ -269,7 +269,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) rb =rb_next (&em->rb_node);if (rb) merge = rb_entry(rb, struct extent_map, rb_node); -if (rb &&can_merge_extent_map (merge) &&mergable_maps (em, merge)) { +if (rb &&can_merge_extent_map (merge) &&mergeable_maps (em, merge)) { em->len += merge->len; em->block_len += merge->block_len;rb_erase_cached (&merge->rb_node, &tree->map);
Signature · 1 line
2.40.1
PATCH 11/11 btrfs: use the flags of an extent map to identify the compression type
In reply to [email protected]From: Filipe Manana <[email protected]>
Currently, in struct extent_map, we use an unsigned int (32 bits) to identify the compression type of an extent and an unsigned long (64 bits on a 64 bits platform, 32 bits otherwise) for flags. We are only using 6 different flags, so an unsigned long is excessive and we can use flags to identify the compression type instead of using a dedicated 32 bits field.
We can easily have tens or hundreds of thousands (or more) of extent maps on busy and large filesystems, specially with compression enabled or many or large files with tons of small extents. So it's convenient to have the extent_map structure as small as possible in order to use less memory.
So removed the compression type field from struct extent_map, use flags to identify the compression type and shorten the flags field from an unsigned long to a u32. This saves 8 bytes (on 64 bits platforms) and reduces the size of the structure from 136 bytes down to 128 bytes, using now only two cache lines, and increases the number of extent maps we can have per 4K page from 30 to 32. By using a u32 for the flags instead of an unsigned long, we no longer use test_bit(), set_bit() and clear_bit(), but that level of atomicity is not needed as most flags are never cleared once set (before adding an extent map to the tree), and the ones that can be cleared or set after an extent map is added to the tree, are always performed while holding the write lock on the extent map tree, while the reader holds a lock on the tree or tests for a flag that never changes once the extent map is in the tree (such as compression flags).
--- fs/btrfs/compression.c | 4 +-- fs/btrfs/defrag.c | 8 ++--- fs/btrfs/extent_io.c | 13 ++++--- fs/btrfs/extent_map.c | 51 ++++++++++++-------------- fs/btrfs/extent_map.h | 58 +++++++++++++++++++++++++----- fs/btrfs/file-item.c | 9 ++--- fs/btrfs/file.c | 10 +++--- fs/btrfs/inode.c | 33 ++++++++--------- fs/btrfs/relocation.c | 2 +- fs/btrfs/tests/extent-map-tests.c | 4 +-- fs/btrfs/tests/inode-tests.c | 60 +++++++++++++++---------------- fs/btrfs/tree-log.c | 16 +++++---- include/trace/events/btrfs.h | 21 ++++++----- 13 files changed, 158 insertions(+), 131 deletions(-)
fs/btrfs/compression.c +2 -2
@@ -584,7 +584,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) goto out; } -ASSERT (em->compress_type !=BTRFS_COMPRESS_NONE ); +ASSERT (extent_map_is_compressed (em)); compressed_len = em->block_len; cb = alloc_compressed_bio(inode, file_offset, REQ_OP_READ,
@@ -596,7 +596,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) cb->len = bbio->bio.bi_iter.bi_size; cb->compressed_len = compressed_len; - cb->compress_type = em->compress_type; + cb->compress_type =extent_map_compression (em); cb->orig_bbio = bbio;free_extent_map (em);
fs/btrfs/defrag.c +4 -4
@@ -775,7 +775,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,* this em, as either we don't care about the generation, or the * merged extent map will be rejected anyway. */ -if (em &&test_bit (EXTENT_FLAG_MERGED , &em->flags) && +if (em && (em->flags &EXTENT_FLAG_MERGED ) && newer_than && em->generation >= newer_than) {free_extent_map (em); em =NULL ;
@@ -802,7 +802,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,static u32 get_extent_max_capacity (const struct btrfs_fs_info *fs_info,const struct extent_map *em) { -if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)) +if (extent_map_is_compressed (em))return BTRFS_MAX_COMPRESSED ;return fs_info->max_extent_size; }
@@ -828,7 +828,7 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,/* No more em or hole */ if (!next || next->block_start >=EXTENT_MAP_LAST_BYTE ) goto out; -if (test_bit (EXTENT_FLAG_PREALLOC , &next->flags)) +if (next->flags &EXTENT_FLAG_PREALLOC ) goto out;/* * If the next extent is at its max capacity, defragging current extent
@@ -998,7 +998,7 @@ static int defrag_collect_targets(struct btrfs_inode *inode,/* Skip holes and preallocated extents. */ if (em->block_start ==EXTENT_MAP_HOLE || -test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) + (em->flags &EXTENT_FLAG_PREALLOC )) goto next;/* Skip older extent */
fs/btrfs/extent_io.c +6 -7
@@ -1032,8 +1032,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,BUG_ON (extent_map_end (em) <= cur);BUG_ON (end < cur); -if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)) - compress_type = em->compress_type; + compress_type =extent_map_compression (em); iosize =min (extent_map_end (em) - cur, end - cur +1 ); iosize =ALIGN (iosize, blocksize);
@@ -1042,7 +1041,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,else disk_bytenr = em->block_start + extent_offset; block_start = em->block_start; -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) +if (em->flags &EXTENT_FLAG_PREALLOC ) block_start =EXTENT_MAP_HOLE ;/*
@@ -1079,7 +1078,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,* is a corner case so we prioritize correctness over * non-optimal behavior (submitting 2 bios for the same extent). */ -if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags) && +if (compress_type !=BTRFS_COMPRESS_NONE && prev_em_start && *prev_em_start != (u64)-1 && *prev_em_start != em->start) force_bio_submit = true;
@@ -1358,7 +1357,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, block_start = em->block_start; disk_bytenr = em->block_start + extent_offset; -ASSERT (!test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)); +ASSERT (!extent_map_is_compressed (em));ASSERT (block_start !=EXTENT_MAP_HOLE );ASSERT (block_start !=EXTENT_MAP_INLINE );
@@ -2359,7 +2358,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)write_unlock (&map->lock);break ; } -if (test_bit (EXTENT_FLAG_PINNED , &em->flags) || +if ((em->flags &EXTENT_FLAG_PINNED ) || em->start != start) {write_unlock (&map->lock);free_extent_map (em);
@@ -2376,7 +2375,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)* extra reference on the em. */ if (list_empty (&em->list) || -test_bit (EXTENT_FLAG_LOGGING , &em->flags)) + (em->flags &EXTENT_FLAG_LOGGING )) goto remove_em;/* * If it's in the list of modified extents, remove it
fs/btrfs/extent_map.c +22 -29
178 lines not on this page · raw message
fs/btrfs/extent_map.h +50 -8
89 lines not on this page · raw message
fs/btrfs/file-item.c +3 -6
@@ -1294,8 +1294,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,return ; }if (compress_type !=BTRFS_COMPRESS_NONE ) { -set_bit (EXTENT_FLAG_COMPRESSED , &em->flags); - em->compress_type = compress_type; +extent_map_set_compression (em, compress_type); em->block_start = bytenr; em->block_len = em->orig_block_len; }else {
@@ -1303,7 +1302,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, em->block_start = bytenr; em->block_len = em->len;if (type ==BTRFS_FILE_EXTENT_PREALLOC ) -set_bit (EXTENT_FLAG_PREALLOC , &em->flags); + em->flags |=EXTENT_FLAG_PREALLOC ; } }else if (type ==BTRFS_FILE_EXTENT_INLINE ) { em->block_start =EXTENT_MAP_INLINE ;
@@ -1315,9 +1314,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,*/ em->orig_start =EXTENT_MAP_HOLE ; em->block_len = (u64)-1 ; - em->compress_type = compress_type; -if (compress_type !=BTRFS_COMPRESS_NONE ) -set_bit (EXTENT_FLAG_COMPRESSED , &em->flags); +extent_map_set_compression (em, compress_type); }else {btrfs_err (fs_info, "unknown file extent item type %d, inode %llu, offset %llu, "
fs/btrfs/file.c +4 -6
@@ -2150,7 +2150,6 @@ static int fill_holes(struct btrfs_trans_handle *trans, hole_em->block_start =EXTENT_MAP_HOLE ; hole_em->block_len =0 ; hole_em->orig_block_len =0 ; - hole_em->compress_type =BTRFS_COMPRESS_NONE ; hole_em->generation = trans->transid; ret =btrfs_replace_extent_map_range (inode, hole_em, true);
@@ -2839,7 +2838,7 @@ static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,if (em->block_start ==EXTENT_MAP_HOLE ) ret =RANGE_BOUNDARY_HOLE ; -else if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) +else if (em->flags &EXTENT_FLAG_PREALLOC ) ret =RANGE_BOUNDARY_PREALLOC_EXTENT ;else ret =RANGE_BOUNDARY_WRITTEN_EXTENT ;
@@ -2879,8 +2878,7 @@ static int btrfs_zero_range(struct inode *inode,* extents and holes, we drop all the existing extents and allocate a * new prealloc extent, so that we get a larger contiguous disk extent. */ -if (em->start <= alloc_start && -test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) { +if (em->start <= alloc_start && (em->flags &EXTENT_FLAG_PREALLOC )) {const u64 em_end = em->start + em->len;if (em_end >= offset + len) {
@@ -2915,7 +2913,7 @@ static int btrfs_zero_range(struct inode *inode, goto out; } -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) { +if (em->flags &EXTENT_FLAG_PREALLOC ) {free_extent_map (em); ret =btrfs_fallocate_update_isize (inode, offset + len, mode);
@@ -3136,7 +3134,7 @@ static long btrfs_fallocate(struct file *file, int mode, last_byte =ALIGN (last_byte, blocksize);if (em->block_start ==EXTENT_MAP_HOLE || (cur_offset >= inode->i_size && - !test_bit (EXTENT_FLAG_PREALLOC , &em->flags))) { + !(em->flags &EXTENT_FLAG_PREALLOC ))) {const u64 range_len = last_byte - cur_offset; ret =add_falloc_range (&reserve_list, cur_offset, range_len);
fs/btrfs/inode.c +15 -18
@@ -4898,7 +4898,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) last_byte =ALIGN (last_byte, fs_info->sectorsize); hole_size = last_byte - cur_offset; -if (!test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) { +if (!(em->flags &EXTENT_FLAG_PREALLOC )) {struct extent_map *hole_em; err =maybe_insert_hole (inode, cur_offset, hole_size);
@@ -4926,7 +4926,6 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) hole_em->block_len =0 ; hole_em->orig_block_len =0 ; hole_em->ram_bytes = hole_size; - hole_em->compress_type =BTRFS_COMPRESS_NONE ; hole_em->generation =btrfs_get_fs_generation (fs_info); err =btrfs_replace_extent_map_range (inode, hole_em, true);
@@ -7274,13 +7273,11 @@ static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, em->orig_block_len = orig_block_len; em->ram_bytes = ram_bytes; em->generation =-1 ; -set_bit (EXTENT_FLAG_PINNED , &em->flags); -if (type ==BTRFS_ORDERED_PREALLOC ) { -set_bit (EXTENT_FLAG_FILLING , &em->flags); - }else if (type ==BTRFS_ORDERED_COMPRESSED ) { -set_bit (EXTENT_FLAG_COMPRESSED , &em->flags); - em->compress_type = compress_type; - } + em->flags |=EXTENT_FLAG_PINNED ; +if (type ==BTRFS_ORDERED_PREALLOC ) + em->flags |=EXTENT_FLAG_FILLING ; +else if (type ==BTRFS_ORDERED_COMPRESSED ) +extent_map_set_compression (em, compress_type); ret =btrfs_replace_extent_map_range (inode, em, true);if (ret) {
@@ -7320,10 +7317,10 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map,* just use the extent. * */ -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags) || +if ((em->flags &EXTENT_FLAG_PREALLOC ) || ((BTRFS_I (inode)->flags &BTRFS_INODE_NODATACOW ) && em->block_start !=EXTENT_MAP_HOLE )) { -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) +if (em->flags &EXTENT_FLAG_PREALLOC ) type =BTRFS_ORDERED_PREALLOC ;else type =BTRFS_ORDERED_NOCOW ;
@@ -7558,7 +7555,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,* to buffered IO. Don't blame me, this is the price we pay for using * the generic code. */ -if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags) || +if (extent_map_is_compressed (em) || em->block_start ==EXTENT_MAP_INLINE ) {free_extent_map (em);/*
@@ -7654,7 +7651,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,* that, since we have locked only the parts we are performing I/O in. */ if ((em->block_start ==EXTENT_MAP_HOLE ) || - (test_bit (EXTENT_FLAG_PREALLOC , &em->flags) && !write)) { + ((em->flags &EXTENT_FLAG_PREALLOC ) && !write)) { iomap->addr =IOMAP_NULL_ADDR ; iomap->type =IOMAP_HOLE ; }else {
@@ -9654,7 +9651,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, em->block_len = ins.offset; em->orig_block_len = ins.offset; em->ram_bytes = ins.offset; -set_bit (EXTENT_FLAG_PREALLOC , &em->flags); + em->flags |=EXTENT_FLAG_PREALLOC ; em->generation = trans->transid; ret =btrfs_replace_extent_map_range (BTRFS_I (inode), em, true);
@@ -10135,12 +10132,12 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, encoded->len =min_t (u64,extent_map_end (em), inode->vfs_inode.i_size) - iocb->ki_pos;if (em->block_start ==EXTENT_MAP_HOLE || -test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) { + (em->flags &EXTENT_FLAG_PREALLOC )) { disk_bytenr =EXTENT_MAP_HOLE ; count =min_t (u64, count, encoded->len); encoded->len = count; encoded->unencoded_len = count; - }else if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)) { + }else if (extent_map_is_compressed (em)) { disk_bytenr = em->block_start;/* * Bail if the buffer isn't large enough to return the whole
@@ -10155,7 +10152,7 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, encoded->unencoded_len = em->ram_bytes; encoded->unencoded_offset = iocb->ki_pos - em->orig_start; ret =btrfs_encoded_io_compression_from_extent (fs_info, - em->compress_type); +extent_map_compression (em));if (ret <0 ) goto out_em; encoded->compression = ret;
@@ -10703,7 +10700,7 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, ret = -EINVAL ; goto out; } -if (test_bit (EXTENT_FLAG_COMPRESSED , &em->flags)) { +if (extent_map_is_compressed (em)) {btrfs_warn (fs_info,"swapfile must not be compressed" ); ret = -EINVAL ; goto out;
fs/btrfs/relocation.c +1 -1
@@ -2951,7 +2951,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod em->len = end +1 - start; em->block_len = em->len; em->block_start = block_start; -set_bit (EXTENT_FLAG_PINNED , &em->flags); + em->flags |=EXTENT_FLAG_PINNED ;lock_extent (&BTRFS_I (inode)->io_tree, start, end, &cached_state); ret =btrfs_replace_extent_map_range (BTRFS_I (inode), em, false);
fs/btrfs/tests/extent-map-tests.c +2 -2
@@ -480,7 +480,7 @@ static int add_compressed_extent(struct btrfs_fs_info *fs_info, em->len = len; em->block_start = block_start; em->block_len =SZ_4K ; -set_bit (EXTENT_FLAG_COMPRESSED , &em->flags); + em->flags |=EXTENT_FLAG_COMPRESS_ZLIB ;write_lock (&em_tree->lock); ret =btrfs_add_extent_mapping (fs_info, em_tree, &em, em->start, em->len);write_unlock (&em_tree->lock);
@@ -763,7 +763,7 @@ static int test_case_7(struct btrfs_fs_info *fs_info) em->len =SZ_16K ; em->block_start =0 ; em->block_len =SZ_4K ; -set_bit (EXTENT_FLAG_PINNED , &em->flags); + em->flags |=EXTENT_FLAG_PINNED ;write_lock (&em_tree->lock); ret =btrfs_add_extent_mapping (fs_info, em_tree, &em, em->start, em->len);write_unlock (&em_tree->lock);
fs/btrfs/tests/inode-tests.c +30 -30
207 lines not on this page · raw message
fs/btrfs/tree-log.c +9 -7
@@ -4519,7 +4519,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans,int ret =0 ;if (inode->flags &BTRFS_INODE_NODATASUM || -test_bit (EXTENT_FLAG_PREALLOC , &em->flags) || + (em->flags &EXTENT_FLAG_PREALLOC ) || em->block_start ==EXTENT_MAP_HOLE )return 0 ;
@@ -4582,7 +4582,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans,return 0 ;/* If we're compressed we have to save the entire range of csums. */ -if (em->compress_type) { +if (extent_map_is_compressed (em)) { csum_offset =0 ; csum_len =max (em->block_len, em->orig_block_len); }else {
@@ -4622,18 +4622,20 @@ static int log_one_extent(struct btrfs_trans_handle *trans,struct btrfs_file_extent_item fi = {0 };struct extent_buffer *leaf;struct btrfs_key key; +enum btrfs_compression_type compress_type;u64 extent_offset = em->start - em->orig_start;u64 block_len;int ret;btrfs_set_stack_file_extent_generation (&fi, trans->transid); -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags)) +if (em->flags &EXTENT_FLAG_PREALLOC )btrfs_set_stack_file_extent_type (&fi,BTRFS_FILE_EXTENT_PREALLOC );else btrfs_set_stack_file_extent_type (&fi,BTRFS_FILE_EXTENT_REG ); block_len =max (em->block_len, em->orig_block_len); -if (em->compress_type !=BTRFS_COMPRESS_NONE ) { + compress_type =extent_map_compression (em); +if (compress_type !=BTRFS_COMPRESS_NONE ) {btrfs_set_stack_file_extent_disk_bytenr (&fi, em->block_start);btrfs_set_stack_file_extent_disk_num_bytes (&fi, block_len); }else if (em->block_start <EXTENT_MAP_LAST_BYTE ) {
@@ -4645,7 +4647,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans,btrfs_set_stack_file_extent_offset (&fi, extent_offset);btrfs_set_stack_file_extent_num_bytes (&fi, em->len);btrfs_set_stack_file_extent_ram_bytes (&fi, em->ram_bytes); -btrfs_set_stack_file_extent_compression (&fi, em->compress_type); +btrfs_set_stack_file_extent_compression (&fi, compress_type); ret =log_extent_csums (trans, inode, log, em, ctx);if (ret)
@@ -4858,13 +4860,13 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,continue ;/* We log prealloc extents beyond eof later. */ -if (test_bit (EXTENT_FLAG_PREALLOC , &em->flags) && +if ((em->flags &EXTENT_FLAG_PREALLOC ) && em->start >=i_size_read (&inode->vfs_inode))continue ;/* Need a ref to keep it from getting evicted from cache */ refcount_inc (&em->refs); -set_bit (EXTENT_FLAG_LOGGING , &em->flags); + em->flags |=EXTENT_FLAG_LOGGING ;list_add_tail (&em->list, &extents); num++; }
include/trace/events/btrfs.h +10 -11
@@ -272,11 +272,13 @@ DEFINE_EVENT(btrfs__inode, btrfs_inode_evict,#define show_map_flags (flag) \ __print_flags(flag, "|", \ - { (1 << EXTENT_FLAG_PINNED), "PINNED" },\ - { (1 << EXTENT_FLAG_COMPRESSED), "COMPRESSED" },\ - { (1 << EXTENT_FLAG_PREALLOC), "PREALLOC" },\ - { (1 << EXTENT_FLAG_LOGGING), "LOGGING" },\ - { (1 << EXTENT_FLAG_FILLING), "FILLING" }) + { EXTENT_FLAG_PINNED, "PINNED" },\ + { EXTENT_FLAG_COMPRESS_ZLIB, "COMPRESS_ZLIB" },\ + { EXTENT_FLAG_COMPRESS_LZO, "COMPRESS_LZO" },\ + { EXTENT_FLAG_COMPRESS_ZSTD, "COMPRESS_ZSTD" },\ + { EXTENT_FLAG_PREALLOC, "PREALLOC" },\ + { EXTENT_FLAG_LOGGING, "LOGGING" },\ + { EXTENT_FLAG_FILLING, "FILLING" }) TRACE_EVENT_CONDITION(btrfs_get_extent,
@@ -295,9 +297,8 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, __field( u64, orig_start ) __field( u64, block_start ) __field( u64, block_len ) - __field( unsigned long, flags ) + __field( u32, flags )__field ( int, refs ) - __field( unsigned int, compress_type ) ), TP_fast_assign_btrfs(root->fs_info,
@@ -310,13 +311,11 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, __entry->block_len = map->block_len; __entry->flags = map->flags; __entry->refs =refcount_read (&map->refs); - __entry->compress_type = map->compress_type; ), TP_printk_btrfs("root=%llu(%s) ino=%llu start=%llu len=%llu " "orig_start=%llu block_start=%llu(%s) " - "block_len=%llu flags=%s refs=%u " - "compress_type=%u", + "block_len=%llu flags=%s refs=%u", show_root_type(__entry->root_objectid), __entry->ino, __entry->start,
@@ -325,7 +324,7 @@ TRACE_EVENT_CONDITION(btrfs_get_extent,show_map_type (__entry->block_start), __entry->block_len,show_map_flags (__entry->flags), - __entry->refs, __entry->compress_type) + __entry->refs) ); TRACE_EVENT(btrfs_handle_em_exist,
Signature · 1 line
2.40.1
Re: [PATCH 11/11] btrfs: use the flags of an extent map to identify the compression type
Quoted 19 lines · On Mon, Dec 04, 2023 at 04:20:33PM +0000, [email protected] wrote:
fs/btrfs/extent_map.h +19
-0+else if (type ==BTRFS_COMPRESS_LZO ) + em->flags |=EXTENT_FLAG_COMPRESS_LZO ; +else if (type ==BTRFS_COMPRESS_ZSTD ) + em->flags |=EXTENT_FLAG_COMPRESS_ZSTD ; +} + +static inline enum btrfs_compression_type extent_map_compression (const struct extent_map *em) +{ +if (em->flags &EXTENT_FLAG_COMPRESS_ZLIB ) +return BTRFS_COMPRESS_ZLIB ; + +if (em->flags &EXTENT_FLAG_COMPRESS_LZO ) +return BTRFS_COMPRESS_LZO ; + +if (em->flags &EXTENT_FLAG_COMPRESS_ZSTD ) +return BTRFS_COMPRESS_ZSTD ; + +return BTRFS_COMPRESS_NONE ; +}
fs/btrfs/extent_map.h struct extent_map_tree { · line 101
Quoted 10 lines
fs/btrfs/extent_map.h +10
-0+ +/* +* More efficient way to determine if extent is compressed, instead of using +* 'extent_map_compression() != BTRFS_COMPRESS_NONE'. +*/ +static inline bool extent_map_is_compressed (const struct extent_map *em) +{ +return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB | +EXTENT_FLAG_COMPRESS_LZO | +EXTENT_FLAG_COMPRESS_ZSTD )) !=0 ;
As a minor optimizations, you could add another bit flag when any of the zlib/lzo/std is set and test just that.
Otherwise, I like the changes and space savings.
Re: [PATCH 11/11] btrfs: use the flags of an extent map to identify the compression type
Quoted 26 lines · David Sterba
Quoted 24 lines · On Mon, Dec 04, 2023 at 04:20:33PM +0000, [email protected] wrote:
fs/btrfs/extent_map.h +24
-0+else if (type ==BTRFS_COMPRESS_LZO ) + em->flags |=EXTENT_FLAG_COMPRESS_LZO ; +else if (type ==BTRFS_COMPRESS_ZSTD ) + em->flags |=EXTENT_FLAG_COMPRESS_ZSTD ; +} + +static inline enum btrfs_compression_type extent_map_compression (const struct extent_map *em) +{ +if (em->flags &EXTENT_FLAG_COMPRESS_ZLIB ) +return BTRFS_COMPRESS_ZLIB ; + +if (em->flags &EXTENT_FLAG_COMPRESS_LZO ) +return BTRFS_COMPRESS_LZO ; + +if (em->flags &EXTENT_FLAG_COMPRESS_ZSTD ) +return BTRFS_COMPRESS_ZSTD ; + +return BTRFS_COMPRESS_NONE ; +} + +/* +* More efficient way to determine if extent is compressed, instead of using +* 'extent_map_compression() != BTRFS_COMPRESS_NONE'. +*/
Quoted 8 lines
fs/btrfs/extent_map.h struct extent_map_tree { · line 101
Quoted 5 lines
fs/btrfs/extent_map.h +5
-0+static inline bool extent_map_is_compressed (const struct extent_map *em) +{ +return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB | +EXTENT_FLAG_COMPRESS_LZO | +EXTENT_FLAG_COMPRESS_ZSTD )) !=0 ;As a minor optimizations, you could add another bit flag when any of the zlib/lzo/std is set and test just that.
How is that an optimization? The compiler ORs those compression flags at compile time, it would be equivalent.
Quoted 2 lines
Otherwise, I like the changes and space savings.
Re: [PATCH 11/11] btrfs: use the flags of an extent map to identify the compression type
Quoted 36 lines · Filipe Manana
Quoted 26 lines · David Sterba
Quoted 24 lines · On Mon, Dec 04, 2023 at 04:20:33PM +0000, [email protected] wrote:
fs/btrfs/extent_map.h +24
-0+else if (type ==BTRFS_COMPRESS_LZO ) + em->flags |=EXTENT_FLAG_COMPRESS_LZO ; +else if (type ==BTRFS_COMPRESS_ZSTD ) + em->flags |=EXTENT_FLAG_COMPRESS_ZSTD ; +} + +static inline enum btrfs_compression_type extent_map_compression (const struct extent_map *em) +{ +if (em->flags &EXTENT_FLAG_COMPRESS_ZLIB ) +return BTRFS_COMPRESS_ZLIB ; + +if (em->flags &EXTENT_FLAG_COMPRESS_LZO ) +return BTRFS_COMPRESS_LZO ; + +if (em->flags &EXTENT_FLAG_COMPRESS_ZSTD ) +return BTRFS_COMPRESS_ZSTD ; + +return BTRFS_COMPRESS_NONE ; +} + +/* +* More efficient way to determine if extent is compressed, instead of using +* 'extent_map_compression() != BTRFS_COMPRESS_NONE'. +*/ Quoted 8 lines
fs/btrfs/extent_map.h struct extent_map_tree { · line 101
Quoted 5 lines
fs/btrfs/extent_map.h +5
-0+static inline bool extent_map_is_compressed (const struct extent_map *em) +{ +return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB | +EXTENT_FLAG_COMPRESS_LZO | +EXTENT_FLAG_COMPRESS_ZSTD )) !=0 ;As a minor optimizations, you could add another bit flag when any of the zlib/lzo/std is set and test just that.
Quoted 2 lines
How is that an optimization? The compiler ORs those compression flags at compile time, it would be equivalent.
Right, it's not the separate test_bit() anymore that would require the 'lock' prefix, it's indeed one constant in one instruction.
Quoted 7 lines · On Mon, Dec 04, 2023 at 04:20:22PM +0000, [email protected] wrote:
From: Filipe Manana <[email protected]>
These do some cleanups around extent maps and their tests, as well as some optimizations. These are part of a larger work around extent maps, but as they are fairly independent and the remainder will take a longer while, sending these out separately. More details on the change logs.
Quoted 12 lines
Filipe Manana (11):
btrfs: assert extent map is not in a list when setting it up
btrfs: tests: fix error messages for test case 4 of extent map tests
btrfs: tests: do not ignore NULL extent maps for extent maps tests
btrfs: tests: print all values as decimal in messages for extent map tests
btrfs: unexportadd_extent_mapping()
btrfs: remove redundant value assignment atbtrfs_add_extent_mapping()
btrfs: log messages atunpin_extent_range()during unexpected cases
btrfs: avoid useless rbtree iterations when attempting to merge extent map
btrfs: makeextent_map_end()argument const
btrfs: refactormergable_maps()for more readability
btrfs: use the flags of an extent map to identify the compression type
Added to misc-next, thanks.