Skip to content

Commit f07575b

Browse files
fdmananakdave
authored andcommitted
btrfs: make the rule checking more readable for should_cow_block()
It's quite hard and unreadable the way the rule checks are organized in should_cow_block(). We have a single if statement that returns 0 (false) and it checks several conditions, with one them being a negated compound condition which is particularly hard to reason immediately. Improve on this by using multiple if statements, each checking a single condition and returning immediately. Also change the return type from an integer to a boolean, since all we need is to return true or false. At least on x86_64 with Debian's gcc 14.2.0-19, this also reduces the object code size by 64 bytes. Before this change: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1913327 161567 15592 2090486 1fe5f6 fs/btrfs/btrfs.ko After this change: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1913263 161567 15592 2090422 1fe5b6 fs/btrfs/btrfs.ko Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent b7ff7b0 commit f07575b

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

fs/btrfs/ctree.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -613,15 +613,12 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
613613
return ret;
614614
}
615615

616-
static inline int should_cow_block(const struct btrfs_trans_handle *trans,
617-
const struct btrfs_root *root,
618-
const struct extent_buffer *buf)
616+
static inline bool should_cow_block(const struct btrfs_trans_handle *trans,
617+
const struct btrfs_root *root,
618+
const struct extent_buffer *buf)
619619
{
620620
if (btrfs_is_testing(root->fs_info))
621-
return 0;
622-
623-
/* Ensure we can see the FORCE_COW bit */
624-
smp_mb__before_atomic();
621+
return false;
625622

626623
/*
627624
* We do not need to cow a block if
@@ -634,13 +631,25 @@ static inline int should_cow_block(const struct btrfs_trans_handle *trans,
634631
* after we've finished copying src root, we must COW the shared
635632
* block to ensure the metadata consistency.
636633
*/
637-
if (btrfs_header_generation(buf) == trans->transid &&
638-
!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
639-
!(btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
640-
btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
641-
!test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
642-
return 0;
643-
return 1;
634+
635+
if (btrfs_header_generation(buf) != trans->transid)
636+
return true;
637+
638+
if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN))
639+
return true;
640+
641+
/* Ensure we can see the FORCE_COW bit. */
642+
smp_mb__before_atomic();
643+
if (test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
644+
return true;
645+
646+
if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID)
647+
return false;
648+
649+
if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
650+
return true;
651+
652+
return false;
644653
}
645654

646655
/*

0 commit comments

Comments
 (0)