btrfs: REF_COWS bit rework
v2 cover btrfs: REF_COWS bit rework
This small patchset reworks the REF_COWS bit, by renaming it, and remove that bit for data relocation root.
The basic idea of such rework is to reduce the confusion caused by the name REF_COWS.
With the new bit called SHAREABLE, it should be clear that no user can really create snapshot for data reloc tree, thus its tree blocks shouldn't be shareable.
This would make data balance for reloc tree a little simpler.
Changelog:
v2:
- Add new patch to address the log tree check in btrfs_truncate_inode_items()
Thanks for the advice from David, now it's much simpler than original check, and data reloc tree no longer needs extra hanlding
- Grab data reloc root in create_reloc_inode() and btrfs_recover_relocation()
- Comment update
Qu Wenruo (3):
btrfs: Rename BTRFS_ROOT_REF_COWS to BTRFS_ROOT_SHAREABLE
btrfs: inode: Cleanup the log tree exceptions in
btrfs_truncate_inode_items()
btrfs: Don't set SHAREABLE flag for data reloc tree
fs/btrfs/backref.c | 4 ++-- fs/btrfs/backref.h | 2 +- fs/btrfs/block-rsv.c | 2 +- fs/btrfs/ctree.c | 26 +++++++++++++------------- fs/btrfs/ctree.h | 25 +++++++++++++++++++++++-- fs/btrfs/disk-io.c | 27 ++++++++++++++++++++------- fs/btrfs/extent-tree.c | 2 +- fs/btrfs/file.c | 2 +- fs/btrfs/inode.c | 41 ++++++++++++++++++++++------------------- fs/btrfs/ioctl.c | 2 +- fs/btrfs/relocation.c | 41 +++++++++++++++++++---------------------- fs/btrfs/transaction.c | 12 ++++++------ fs/btrfs/tree-defrag.c | 2 +- 13 files changed, 111 insertions(+), 77 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/3 btrfs: Rename BTRFS_ROOT_REF_COWS to BTRFS_ROOT_SHAREABLE
The name BTRFS_ROOT_REF_COWS is not helpful to show what it really means.
In fact, that bit can only be set to those trees:
- Subvolume roots
- Data reloc root
- Reloc roots for above roots
All other trees won't get this bit set.
So just by the result, it is obvious that, roots with this bit set can have tree blocks shared with other trees.
Either shared by snapshots, or by reloc roots (an special snapshot created by relocation).
This patch will rename BTRFS_ROOT_REF_COWS to BTRFS_ROOT_SHAREABLE to make it easier to understand, and update all comment mentioning "reference counted" to follow the rename.
--- fs/btrfs/backref.c | 4 ++-- fs/btrfs/backref.h | 2 +- fs/btrfs/block-rsv.c | 2 +- fs/btrfs/ctree.c | 26 +++++++++++++------------- fs/btrfs/ctree.h | 24 ++++++++++++++++++++++-- fs/btrfs/disk-io.c | 13 +++++++------ fs/btrfs/extent-tree.c | 2 +- fs/btrfs/file.c | 2 +- fs/btrfs/inode.c | 18 ++++++++++-------- fs/btrfs/ioctl.c | 2 +- fs/btrfs/relocation.c | 24 +++++++++++++----------- fs/btrfs/transaction.c | 12 ++++++------ fs/btrfs/tree-defrag.c | 2 +- 13 files changed, 79 insertions(+), 54 deletions(-)
fs/btrfs/backref.c +2 -2
@@ -2702,7 +2702,7 @@ static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, root =btrfs_get_fs_root (fs_info, &root_key, false);if (IS_ERR (root))return PTR_ERR (root); -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) cur->cowonly =1 ;if (btrfs_root_level (&root->root_item) == cur->level) {
@@ -2789,7 +2789,7 @@ static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache, goto out; } upper->owner =btrfs_header_owner (eb); -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) upper->cowonly =1 ;/*
fs/btrfs/backref.h +1 -1
@@ -184,7 +184,7 @@ struct btrfs_backref_node {struct extent_buffer *eb;/* Level of the tree block */ unsigned int level:8; -/* Is the block in non-reference counted tree */ +/* Is the block in a non-shareable tree */ unsigned int cowonly:1;/* 1 if no child node is in the cache */ unsigned int lowest:1;
fs/btrfs/block-rsv.c +1 -1
@@ -458,7 +458,7 @@ static struct btrfs_block_rsv *get_block_rsv(struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_block_rsv *block_rsv =NULL ; -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state) || +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || (root == fs_info->csum_root && trans->adding_csums) || (root == fs_info->uuid_root)) block_rsv = trans->block_rsv;
fs/btrfs/ctree.c +13 -13
@@ -144,9 +144,10 @@ struct extent_buffer *btrfs_root_node(struct btrfs_root *root)return eb; } -/* cowonly root (everything not a reference counted cow subvolume), just get -* put onto a simple dirty list. transaction.c walks this to make sure they -* get properly updated on disk. +/* +* Cowonly root (not shareable trees, everything not subvolume roots or reloc +* roots), just get put onto a simple dirty list. transaction.c walks this to +* make sure they get properly updated on disk. */ static void add_root_to_dirty_list (struct btrfs_root *root) {
@@ -185,9 +186,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,int level;struct btrfs_disk_key disk_key; -WARN_ON (test_bit (BTRFS_ROOT_REF_COWS , &root->state) && +WARN_ON (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && trans->transid != fs_info->running_transaction->transid); -WARN_ON (test_bit (BTRFS_ROOT_REF_COWS , &root->state) && +WARN_ON (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && trans->transid != root->last_trans); level =btrfs_header_level (buf);
@@ -826,12 +827,11 @@ int btrfs_block_can_be_shared(struct btrfs_root *root, struct extent_buffer *buf) {/* -* Tree blocks not in reference counted trees and tree roots -* are never shared. If a block was allocated after the last -* snapshot and the block was not allocated by tree relocation, -* we know the block is not shared. +* Tree blocks not in shareable trees and tree roots are never shared. +* If a block was allocated after the last snapshot and the block was +* not allocated by tree relocation, we know the block is not shared. */ - if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && + if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && buf != root->node && buf != root->commit_root && (btrfs_header_generation(buf) <= btrfs_root_last_snapshot(&root->root_item) ||
@@ -1024,9 +1024,9 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,btrfs_assert_tree_locked (buf); -WARN_ON (test_bit (BTRFS_ROOT_REF_COWS , &root->state) && +WARN_ON (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && trans->transid != fs_info->running_transaction->transid); -WARN_ON (test_bit (BTRFS_ROOT_REF_COWS , &root->state) && +WARN_ON (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && trans->transid != root->last_trans); level =btrfs_header_level (buf);
@@ -1065,7 +1065,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,return ret; } -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state)) { +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) { ret =btrfs_reloc_cow_block (trans, root, buf, cow);if (ret) {btrfs_abort_transaction (trans, ret);
fs/btrfs/ctree.h +22 -2
@@ -969,7 +969,27 @@ enum {* is used to tell us when more checks are required */ BTRFS_ROOT_IN_TRANS_SETUP , -BTRFS_ROOT_REF_COWS , + +/* +* If tree blocks of this root can be shared between other roots. +* Only subvolume trees and their reloc trees have this bit set. +* Conflicts with TRACK_DIRTY bit. +* +* This affects two things: +* - How balance works +* For shareable roots, btrfs needs to use reloc tree and do path +* replacement for balance, and needs various pre/post hooks for +* snapshot creation to handle them. +* +* While for non shareable trees, we just simply do a tree search +* with COW. +* +* - How dirty roots are tracked +* For shareable roots, btrfs_record_root_in_trans() is needed to +* trace them, while non subvolume roots have TRACK_DIRTY bit, they +* don't need manual record. +*/ +BTRFS_ROOT_SHAREABLE ,BTRFS_ROOT_TRACK_DIRTY ,BTRFS_ROOT_IN_RADIX , BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
@@ -1055,7 +1075,7 @@ struct btrfs_root {struct btrfs_key defrag_progress;struct btrfs_key defrag_max; -/* the dirty list is only used by non-reference counted roots */ +/* the dirty list is only used by non-shareable roots */ struct list_head dirty_list;struct list_head root_list;
fs/btrfs/disk-io.c +7 -6
@@ -1275,12 +1275,13 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, root->root_key.offset =BTRFS_TREE_LOG_OBJECTID ;/* -* DON'T set REF_COWS for log trees +* DON'T set SHAREABLE bit for log trees. * -* log trees do not get reference counted because they go away -* before a real commit is actually done. They do store pointers -* to file data extents, and those reference counts still get -* updated (along with back refs to the log tree). +* Log trees are not exposed to user space thus can't be snapshotted, +* and they go away before a real commit is actually done. +* +* They do store pointers to file data extents, and those reference +* counts still get updated (along with back refs to the log tree). */ leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
@@ -1419,7 +1420,7 @@ static int btrfs_init_fs_root(struct btrfs_root *root) goto fail;if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { -set_bit (BTRFS_ROOT_REF_COWS , &root->state); +set_bit (BTRFS_ROOT_SHAREABLE , &root->state);btrfs_check_and_init_root_item (&root->root_item); }
fs/btrfs/extent-tree.c +1 -1
@@ -2442,7 +2442,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, nritems =btrfs_header_nritems (buf); level =btrfs_header_level (buf); -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state) && level ==0 ) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && level ==0 )return 0 ;if (full_backref)
fs/btrfs/file.c +1 -1
@@ -775,7 +775,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,if (start >=BTRFS_I (inode)->disk_i_size && !replace_extent) modify_tree =0 ; - update_refs = (test_bit (BTRFS_ROOT_REF_COWS , &root->state) || + update_refs = (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || root == fs_info->tree_root);while (1 ) { recow =0 ;
fs/btrfs/inode.c +10 -8
@@ -4107,11 +4107,13 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,BUG_ON (new_size >0 && min_type !=BTRFS_EXTENT_DATA_KEY );/* -* for non-free space inodes and ref cows, we want to back off from -* time to time +* for non-free space inodes and non-shareable roots, we want to back +* off from time to time. +* This means all inodes in subvolume roots, reloc roots, and data +* reloc roots. */ if (!btrfs_is_free_space_inode (BTRFS_I (inode)) && -test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) be_nice = true; path =btrfs_alloc_path ();
@@ -4128,7 +4130,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,* not block aligned since we will be keeping the last block of the * extent just the way it is. */ -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state) || +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || root == fs_info->tree_root) btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size, fs_info->sectorsize),
@@ -4240,7 +4242,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, extent_num_bytes); num_dec = (orig_num_bytes - extent_num_bytes); -if (test_bit (BTRFS_ROOT_REF_COWS , +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && extent_start !=0 )inode_sub_bytes (inode, num_dec);
@@ -4256,7 +4258,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, num_dec =btrfs_file_extent_num_bytes (leaf, fi);if (extent_start !=0 ) { found_extent =1 ; -if (test_bit (BTRFS_ROOT_REF_COWS , +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state))inode_sub_bytes (inode, num_dec); }
@@ -4292,7 +4294,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, clear_len = fs_info->sectorsize; } -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state))inode_sub_bytes (inode, item_end +1 - new_size); } delete:
@@ -4333,7 +4335,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, should_throttle = false;if (found_extent && - (test_bit (BTRFS_ROOT_REF_COWS , &root->state) || + (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || root == fs_info->tree_root)) {struct btrfs_ref ref = {0 };
fs/btrfs/ioctl.c +1 -1
@@ -750,7 +750,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,int ret;bool snapshot_force_cow = false; -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return -EINVAL ;if (atomic_read (&root->nr_swapfiles)) {
fs/btrfs/relocation.c +13 -11
@@ -321,7 +321,7 @@ int btrfs_should_ignore_reloc_root(struct btrfs_root *root) {struct btrfs_root *reloc_root; -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;/* This root has been merged with its reloc tree, we can ignore it */
@@ -808,7 +808,7 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, reloc_root =btrfs_read_tree_root (fs_info->tree_root, &root_key);BUG_ON (IS_ERR (reloc_root)); -set_bit (BTRFS_ROOT_REF_COWS , &reloc_root->state); +set_bit (BTRFS_ROOT_SHAREABLE , &reloc_root->state); reloc_root->last_trans = trans->transid;return reloc_root; }
@@ -2018,7 +2018,7 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, next =walk_up_backref (next, edges, &index); root = next->root;BUG_ON (!root); -BUG_ON (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)); +BUG_ON (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state));if (root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) {record_reloc_root_in_trans (trans, root);
@@ -2062,10 +2062,12 @@ struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, }/* -* select a tree root for relocation. return NULL if the block -* is reference counted. we should use do_relocation() in this -* case. return a tree root pointer if the block isn't reference -* counted. return -ENOENT if the block is root of reloc tree. +* Select a tree root for relocation. +* +* Return NULL if the block is not shareable. we should use do_relocation() in this +* case. +* Return a tree root pointer if the block is shareable. +* Return -ENOENT if the block is root of reloc tree. */ static noinline_for_stack struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
@@ -2083,8 +2085,8 @@ struct btrfs_root *select_one_root(struct btrfs_backref_node *node) root = next->root;BUG_ON (!root); -/* no other choice for non-references counted tree */ -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +/* no other choice for non-shareable tree */ +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return root;if (root->root_key.objectid !=BTRFS_TREE_RELOC_OBJECTID )
@@ -2480,7 +2482,7 @@ static int relocate_tree_block(struct btrfs_trans_handle *trans, }if (root) { -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state)) { +if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) {BUG_ON (node->new_bytenr);BUG_ON (!list_empty (&node->list));btrfs_record_root_in_trans (trans, root);
@@ -3765,7 +3767,7 @@ int btrfs_recover_relocation(struct btrfs_root *root) goto out; } -set_bit (BTRFS_ROOT_REF_COWS , &reloc_root->state); +set_bit (BTRFS_ROOT_SHAREABLE , &reloc_root->state);list_add (&reloc_root->root_list, &reloc_roots);if (btrfs_root_refs (&reloc_root->root_item) >0 ) {
fs/btrfs/transaction.c +6 -6
@@ -349,10 +349,10 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info, }/* -* this does all the record keeping required to make sure that a reference -* counted root is properly recorded in a given transaction. This is required +* This does all the record keeping required to make sure that a shareable +* root is properly recorded in a given transaction. This is required * to make sure the old root from before we joined the transaction is deleted -* when the transaction commits +* when the transaction commits. */ static int record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -360,7 +360,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, {struct btrfs_fs_info *fs_info = root->fs_info; -if ((test_bit (BTRFS_ROOT_REF_COWS , &root->state) && +if ((test_bit (BTRFS_ROOT_SHAREABLE , &root->state) && root->last_trans < trans->transid) || force) {WARN_ON (root == fs_info->extent_root);WARN_ON (!force && root->commit_root != root->node);
@@ -439,7 +439,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, {struct btrfs_fs_info *fs_info = root->fs_info; -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;/*
@@ -504,7 +504,7 @@ static inline bool need_reserve_reloc_root(struct btrfs_root *root)struct btrfs_fs_info *fs_info = root->fs_info;if (!fs_info->reloc_ctl || - !test_bit (BTRFS_ROOT_REF_COWS , &root->state) || + !test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID || root->reloc_root)return false;
fs/btrfs/tree-defrag.c +1 -1
@@ -35,7 +35,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, goto out; } -if (!test_bit (BTRFS_ROOT_REF_COWS , &root->state)) +if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state)) goto out; path =btrfs_alloc_path ();
Signature · 1 line
2.26.2
PATCH v2 2/3 btrfs: inode: Cleanup the log tree exceptions in btrfs_truncate_inode_items()
In reply to Qu WenruoThere are a lot of root owner check in btrfs_truncate_inode_items() like:
if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || root == fs_info->tree_root)
But considering that, there are only those trees can have INODE_ITEMs:
- tree root (For v1 space cache)
- subvolume trees
- tree reloc trees
- data reloc tree
- log trees
And since subvolume/tree reloc/data reloc trees all have SHAREABLE bit, and we're checking tree root manually, so above check is just excluding log trees.
This patch will replace two of such checks to a much simpler one:
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
This would merge btrfs_drop_extent_cache() and lock_extent_bits() call into the same if branch.
Also since we're here, add one comment explaining why we don't want to call lock_extent_bits()/drop_extent_cache() on log trees.
Finally replace ALIGN()/ALIGN_DOWN() to round_up()/round_down(), as I'm always bad at determing the alignement direction.
--- fs/btrfs/inode.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
fs/btrfs/inode.c +14 -13
@@ -4101,7 +4101,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,u64 bytes_deleted =0 ;bool be_nice = false;bool should_throttle = false; -const u64 lock_start =ALIGN_DOWN (new_size, fs_info->sectorsize); +const u64 lock_start =round_down (new_size, fs_info->sectorsize);struct extent_state *cached_state =NULL ;BUG_ON (new_size >0 && min_type !=BTRFS_EXTENT_DATA_KEY );
@@ -4121,20 +4121,22 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,return -ENOMEM ; path->reada =READA_BACK ; -if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) -lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , - &cached_state); -/* -* We want to drop from the next block forward in case this new size is -* not block aligned since we will be keeping the last block of the -* extent just the way it is. +* There will be a lot of exceptions for log trees, as log inodes are +* not real inodes, but an anchor for logged inodes. */ -if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root) -btrfs_drop_extent_cache (BTRFS_I (inode),ALIGN (new_size, +if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { +/* +* We want to drop from the next block forward in case this +* new size is not block aligned since we will be keeping the +* last block of the extent just the way it is. +*/ +lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , + &cached_state); +btrfs_drop_extent_cache (BTRFS_I (inode),round_up (new_size, fs_info->sectorsize), (u64)-1 ,0 ); + }/* * This function is also used to drop the items in the log tree before
@@ -4335,8 +4337,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, should_throttle = false;if (found_extent && - (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root)) { + root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) {struct btrfs_ref ref = {0 }; bytes_deleted += extent_num_bytes;
Signature · 1 line
2.26.2
Re: [PATCH v2 2/3] btrfs: inode: Cleanup the log tree exceptions in btrfs_truncate_inode_items()
Quoted 41 lines · Qu Wenruo
There are a lot of root owner check in
btrfs_truncate_inode_items()like:if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || root == fs_info->tree_root)But considering that, there are only those trees can have
INODE_ITEMs:
- tree root (For v1 space cache)
- subvolume trees
- tree reloc trees
- data reloc tree
- log treesAnd since subvolume/tree reloc/data reloc trees all have SHAREABLE bit, and we're checking tree root manually, so above check is just excluding log trees.
This patch will replace two of such checks to a much simpler one:
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)This would merge
btrfs_drop_extent_cache()andlock_extent_bits()call into the same if branch.Also since we're here, add one comment explaining why we don't want to call lock_extent_bits()/drop_extent_cache() on log trees.
Finally replace ALIGN()/ALIGN_DOWN() to round_up()/round_down(), as I'm always bad at determing the alignement direction.
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/inode.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)fs/btrfs/inode.c +0
-0
fs/btrfs/inode.c int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · line 4104
Quoted 5 lines
fs/btrfs/inode.c +1
-1@@ -4101,7 +4101,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,u64 bytes_deleted =0 ;bool be_nice = false;bool should_throttle = false; -const u64 lock_start =ALIGN_DOWN (new_size, fs_info->sectorsize); +const u64 lock_start =round_down (new_size, fs_info->sectorsize);
Hum, seriously? Why does ALIGN_DOWN confuses you? ALIGN, means to align, and the DOWN part is very explicit about the direction.
Quoted 7 lines
fs/btrfs/inode.c +0
-0struct extent_state *cached_state =NULL ;BUG_ON (new_size >0 && min_type !=BTRFS_EXTENT_DATA_KEY );@@ -4121,20 +4121,22 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,return -ENOMEM ; path->reada =READA_BACK ;
fs/btrfs/inode.c int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · line 4126
Quoted 10 lines
fs/btrfs/inode.c +2
-7-if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) -lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , - &cached_state); -/* -* We want to drop from the next block forward in case this new size is -* not block aligned since we will be keeping the last block of the -* extent just the way it is. +* There will be a lot of exceptions for log trees, as log inodes are +* not real inodes, but an anchor for logged inodes.
This is a very confusing sentence, you're saying logged inodes are an "anchor" (whatever that means) to themselves.
Either leave nothing as it was, or just say log tree operations aren't supposed to change anything on the inodes.
Thanks.
Quoted 32 lines
fs/btrfs/inode.c +11
-6*/ -if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root) -btrfs_drop_extent_cache (BTRFS_I (inode),ALIGN (new_size, +if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { +/* +* We want to drop from the next block forward in case this +* new size is not block aligned since we will be keeping the +* last block of the extent just the way it is. +*/ +lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , + &cached_state); +btrfs_drop_extent_cache (BTRFS_I (inode),round_up (new_size, fs_info->sectorsize), (u64)-1 ,0 ); + }/* * This function is also used to drop the items in the log tree before @@ -4335,8 +4337,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, should_throttle = false;if (found_extent && - (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root)) { + root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) {struct btrfs_ref ref = {0 }; bytes_deleted += extent_num_bytes; --2.26.2
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Re: [PATCH v2 2/3] btrfs: inode: Cleanup the log tree exceptions in btrfs_truncate_inode_items()
Quoted 37 lines · Filipe Manana
Quoted 36 lines · Qu Wenruo
There are a lot of root owner check in
btrfs_truncate_inode_items()like:if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) || root == fs_info->tree_root)But considering that, there are only those trees can have
INODE_ITEMs:
- tree root (For v1 space cache)
- subvolume trees
- tree reloc trees
- data reloc tree
- log treesAnd since subvolume/tree reloc/data reloc trees all have SHAREABLE bit, and we're checking tree root manually, so above check is just excluding log trees.
This patch will replace two of such checks to a much simpler one:
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)This would merge
btrfs_drop_extent_cache()andlock_extent_bits()call into the same if branch.Also since we're here, add one comment explaining why we don't want to call lock_extent_bits()/drop_extent_cache() on log trees.
Finally replace ALIGN()/ALIGN_DOWN() to round_up()/round_down(), as I'm always bad at determing the alignement direction.
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/inode.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
Quoted 13 lines
fs/btrfs/inode.c int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · line 4104
Quoted 10 lines
fs/btrfs/inode.c +1
-1@@ -4101,7 +4101,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,u64 bytes_deleted =0 ;bool be_nice = false;bool should_throttle = false; -const u64 lock_start =ALIGN_DOWN (new_size, fs_info->sectorsize); +const u64 lock_start =round_down (new_size, fs_info->sectorsize);Hum, seriously? Why does
ALIGN_DOWNconfuses you? ALIGN, means to align, and the DOWN part is very explicit about the direction.
ALIGN_DOWN() doesn't, but the later ALIGN() needs to check if it's ceiling or floor.
So I unify them to round_up/round_down().
Quoted 8 lines
Quoted 8 lines
fs/btrfs/inode.c +0
-0struct extent_state *cached_state =NULL ;BUG_ON (new_size >0 && min_type !=BTRFS_EXTENT_DATA_KEY );@@ -4121,20 +4121,22 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,return -ENOMEM ; path->reada =READA_BACK ;
Quoted 15 lines
fs/btrfs/inode.c int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, · line 4126
Quoted 10 lines
fs/btrfs/inode.c +2
-7-if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) -lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , - &cached_state); -/* -* We want to drop from the next block forward in case this new size is -* not block aligned since we will be keeping the last block of the -* extent just the way it is. +* There will be a lot of exceptions for log trees, as log inodes are +* not real inodes, but an anchor for logged inodes. This is a very confusing sentence, you're saying logged inodes are an "anchor" (whatever that means) to themselves.
Either leave nothing as it was, or just say log tree operations aren't supposed to change anything on the inodes.
OK, I'll not add such confusing comment then.
Thanks,
Qu
Quoted 37 lines
Thanks.
Quoted 32 lines
fs/btrfs/inode.c +11
-6*/ -if (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root) -btrfs_drop_extent_cache (BTRFS_I (inode),ALIGN (new_size, +if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { +/* +* We want to drop from the next block forward in case this +* new size is not block aligned since we will be keeping the +* last block of the extent just the way it is. +*/ +lock_extent_bits (&BTRFS_I (inode)->io_tree, lock_start, (u64)-1 , + &cached_state); +btrfs_drop_extent_cache (BTRFS_I (inode),round_up (new_size, fs_info->sectorsize), (u64)-1 ,0 ); + }/* * This function is also used to drop the items in the log tree before @@ -4335,8 +4337,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, should_throttle = false;if (found_extent && - (test_bit (BTRFS_ROOT_SHAREABLE , &root->state) || - root == fs_info->tree_root)) { + root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) {struct btrfs_ref ref = {0 }; bytes_deleted += extent_num_bytes; --2.26.2
PATCH v2 3/3 btrfs: Don't set SHAREABLE flag for data reloc tree
In reply to Qu WenruoSHAREABLE flag is set for subvolumes because users can create snapshot for subvolumes, thus sharing tree blocks of them.
But data reloc tree are not exposed to user space, as it's only an internal tree for data relocation, thus it doesn't need the full path replacement treat at all.
This patch will make data reloc tree just a non-shareable tree, and add btrfs_fs_info::data_reloc_root for data reloc tree, so relocation code can grab it from fs_info directly.
This would slightly improve tree relocation, as now data reloc tree can go through regular COW routine to get relocated, without bothering the complex tree reloc tree routine.
--- fs/btrfs/ctree.h | 1 + fs/btrfs/disk-io.c | 14 +++++++++++++- fs/btrfs/relocation.c | 17 ++++++----------- 3 files changed, 20 insertions(+), 12 deletions(-)
fs/btrfs/ctree.h +1 -0
@@ -582,6 +582,7 @@ struct btrfs_fs_info {struct btrfs_root *quota_root;struct btrfs_root *uuid_root;struct btrfs_root *free_space_root; +struct btrfs_root *data_reloc_root;/* the log root tree is a directory of all the other log roots */ struct btrfs_root *log_root_tree;
fs/btrfs/disk-io.c +13 -1
@@ -1419,7 +1419,8 @@ static int btrfs_init_fs_root(struct btrfs_root *root)if (ret) goto fail; -if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { +if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID && + root->root_key.objectid !=BTRFS_DATA_RELOC_TREE_OBJECTID ) {set_bit (BTRFS_ROOT_SHAREABLE , &root->state);btrfs_check_and_init_root_item (&root->root_item); }
@@ -1525,6 +1526,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)btrfs_put_root (fs_info->uuid_root);btrfs_put_root (fs_info->free_space_root);btrfs_put_root (fs_info->fs_root); +btrfs_put_root (fs_info->data_reloc_root);btrfs_check_leaked_roots (fs_info);btrfs_extent_buffer_leak_debug_check (fs_info);kfree (fs_info->super_copy);
@@ -1981,6 +1983,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)free_root_extent_buffers (info->quota_root);free_root_extent_buffers (info->uuid_root);free_root_extent_buffers (info->fs_root); +free_root_extent_buffers (info->data_reloc_root);if (free_chunk_root)free_root_extent_buffers (info->chunk_root);free_root_extent_buffers (info->free_space_root);
@@ -2287,6 +2290,15 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info)set_bit (BTRFS_ROOT_TRACK_DIRTY , &root->state); fs_info->csum_root = root; + location.objectid =BTRFS_DATA_RELOC_TREE_OBJECTID ; + root =btrfs_get_fs_root (fs_info, &location, true); +if (IS_ERR (root)) { + ret =PTR_ERR (root); + goto out; + } +set_bit (BTRFS_ROOT_TRACK_DIRTY , &root->state); + fs_info->data_reloc_root = root; + location.objectid =BTRFS_QUOTA_TREE_OBJECTID ; root =btrfs_read_tree_root (tree_root, &location);if (!IS_ERR (root)) {
fs/btrfs/relocation.c +6 -11
@@ -3470,14 +3470,12 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, {struct inode *inode =NULL ;struct btrfs_trans_handle *trans; -struct btrfs_root *root; +struct btrfs_root *root =btrfs_grab_root (fs_info->data_reloc_root);struct btrfs_key key;u64 objectid;int err =0 ; - root =read_fs_root (fs_info,BTRFS_DATA_RELOC_TREE_OBJECTID ); -if (IS_ERR (root)) -return ERR_CAST (root); +ASSERT (root); trans =btrfs_start_transaction (root,6 );if (IS_ERR (trans)) {
@@ -3870,13 +3868,10 @@ int btrfs_recover_relocation(struct btrfs_root *root)if (err ==0 ) {/* cleanup orphan inode in data relocation tree */ - fs_root =read_fs_root (fs_info,BTRFS_DATA_RELOC_TREE_OBJECTID ); -if (IS_ERR (fs_root)) { - err =PTR_ERR (fs_root); - }else { - err =btrfs_orphan_cleanup (fs_root); -btrfs_put_root (fs_root); - } + fs_root =btrfs_grab_root (fs_info->data_reloc_root); +ASSERT (fs_root); + err =btrfs_orphan_cleanup (fs_root); +btrfs_put_root (fs_root); }return err; }
Signature · 1 line
2.26.2