btrfs: Avoid producing dead subvolume trees
PATCH 1/6 btrfs: Avoid producing dead subvolume trees
When a non-shared tree block is cow'd, the reference count of all blocks it points to is increased by one. After the commit finishes, the old block will get freed when dropping the dead tree. At that time, the reference count of all blocks the old block points to will be decreased by one. The increments and decrements cancel out. Actually the COW operation can be done in a smarter way. When a non-shared tree block is cow'd, we free the old block at once, the new block inherits old block's references. When a shared tree block is cow'd, we decrease its reference count by one, and increase the reference count of all blocks it points to by one for the new block.
---
fs/btrfs/ctree.c +178 -61
@@ -244,6 +244,147 @@ int btrfs_copy_root(struct btrfs_trans_h }/* +* check if the tree block can be shared by multiple trees +*/ +int btrfs_block_can_be_shared (struct btrfs_root *root, +struct extent_buffer *buf) +{ +/* +* Tree blocks not in refernece 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. +*/ +if (root->ref_cows && + buf != root->node && buf != root->commit_root && + (btrfs_header_generation (buf) <= +btrfs_root_last_snapshot (&root->root_item) || +btrfs_header_flag (buf,BTRFS_HEADER_FLAG_RELOC ))) +return 1 ; +return 0 ; +} + +static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct extent_buffer *buf, +struct extent_buffer *cow) +{ +u64 refs; +u64 owner; +u64 flags; +u64 new_flags =0 ; +int ret; + +/* +* Backrefs update rules: +* +* Always use full backrefs for extent pointers in tree block +* allocated by tree relocation. +* +* If a shared tree block is no longer referenced by its owner +* tree (btrfs_header_owner(buf) == root->root_key.objectid), +* set the BTRFS_BLOCK_NO_OWNER_REF extent flag and use full +* backrefs for extent pointers in tree block. +* +* If a tree block is been relocating +* (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), +* use full backrefs for extent pointers in tree block. +* The reason for this is some operations (such as drop tree) +* used by the relocation code are only allowed for the full +* backrefs format. +*/ + + owner =btrfs_header_owner (buf); +BUG_ON (owner ==BTRFS_TREE_RELOC_OBJECTID ); + +if (!btrfs_block_can_be_shared (root, buf)) { +if (root->root_key.objectid == +BTRFS_TREE_RELOC_OBJECTID ) { + ret =btrfs_inc_ref (trans, root, cow,1 ); +BUG_ON (ret); + ret =btrfs_dec_ref (trans, root, buf,1 ); +BUG_ON (ret); + } +clean_tree_block (trans, root, buf); +return 0 ; + } + + ret =btrfs_lookup_extent_info (trans, root, buf->start, + buf->len, &refs, &flags); +BUG_ON (ret); +BUG_ON (refs ==0 ); + +/* relocation doesn't change blocks' BTRFS_BLOCK_NO_OWNER_REF flag */ +if ((flags &BTRFS_BLOCK_NO_OWNER_REF ) && + root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) { + ret =btrfs_set_block_flags (trans, root, cow->start, cow->len, +BTRFS_BLOCK_NO_OWNER_REF ); +BUG_ON (ret); + } + +if (refs >1 ) { +if ((owner == root->root_key.objectid || + root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) && + !(flags &BTRFS_BLOCK_FULL_BACKREF )) { + ret =btrfs_inc_ref (trans, root, buf,1 ); +BUG_ON (ret); + +if (root->root_key.objectid == +BTRFS_TREE_RELOC_OBJECTID ) { + ret =btrfs_dec_ref (trans, root, buf,0 ); +BUG_ON (ret); + ret =btrfs_inc_ref (trans, root, cow,1 ); +BUG_ON (ret); + }else { +/* owner == root->root_key.objectid */ +BUG_ON (flags &BTRFS_BLOCK_NO_OWNER_REF ); + new_flags |=BTRFS_BLOCK_NO_OWNER_REF ; + } + new_flags |=BTRFS_BLOCK_FULL_BACKREF ; + }else { + +if (root->root_key.objectid == +BTRFS_TREE_RELOC_OBJECTID ) + ret =btrfs_inc_ref (trans, root, cow,1 ); +else + ret =btrfs_inc_ref (trans, root, cow,0 ); +BUG_ON (ret); + +if (owner == root->root_key.objectid) { +BUG_ON (flags &BTRFS_BLOCK_NO_OWNER_REF ); + new_flags |=BTRFS_BLOCK_NO_OWNER_REF ; + } + } +if (new_flags !=0 ) { + ret =btrfs_set_block_flags (trans, root, buf->start, + buf->len, new_flags); +BUG_ON (ret); + } + }else { +if (flags &BTRFS_BLOCK_FULL_BACKREF ) { +if (root->root_key.objectid == +BTRFS_TREE_RELOC_OBJECTID ) + ret =btrfs_inc_ref (trans, root, cow,1 ); +else + ret =btrfs_inc_ref (trans, root, cow,0 ); +BUG_ON (ret); + ret =btrfs_dec_ref (trans, root, buf,1 ); +BUG_ON (ret); + }else { +/* +* this is the last reference and we don't use +* full backrefs, so this reference must come +* from block's owner tree. +*/ +BUG_ON (owner != root->root_key.objectid); +BUG_ON (flags &BTRFS_BLOCK_NO_OWNER_REF ); + } +clean_tree_block (trans, root, buf); + } +return 0 ; +} + +/* * does the dirty work in cow of a single block. The parent block (if * supplied) is updated to point to the new cow copy. The new buffer is marked * dirty and returned locked. If you modify the block it needs to be marked
@@ -298,83 +439,51 @@ static noinline int __btrfs_cow_block(stcopy_extent_buffer (cow, buf,0 ,0 , cow->len);btrfs_set_header_bytenr (cow, cow->start);btrfs_set_header_generation (cow, trans->transid); -btrfs_set_header_owner (cow, root->root_key.objectid); -btrfs_clear_header_flag (cow,BTRFS_HEADER_FLAG_WRITTEN ); +btrfs_clear_header_flag (cow,BTRFS_HEADER_FLAG_WRITTEN | +BTRFS_HEADER_FLAG_RELOC ); +if (root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) +btrfs_set_header_flag (cow,BTRFS_HEADER_FLAG_RELOC ); +else +btrfs_set_header_owner (cow, root->root_key.objectid);write_extent_buffer (cow, root->fs_info->fsid, (unsigned long )btrfs_header_fsid (cow),BTRFS_FSID_SIZE ); -WARN_ON (btrfs_header_generation (buf) > trans->transid); -if (btrfs_header_generation (buf) != trans->transid) { -u32 nr_extents; - ret =btrfs_inc_ref (trans, root, buf, cow, &nr_extents); -if (ret) -return ret; - - ret =btrfs_cache_ref (trans, root, buf, nr_extents); -WARN_ON (ret); - }else if (btrfs_header_owner (buf) ==BTRFS_TREE_RELOC_OBJECTID ) { -/* -* There are only two places that can drop reference to -* tree blocks owned by living reloc trees, one is here, -* the other place is btrfs_drop_subtree. In both places, -* we check reference count while tree block is locked. -* Furthermore, if reference count is one, it won't get -* increased by someone else. -*/ -u32 refs; - ret =btrfs_lookup_extent_ref (trans, root, buf->start, - buf->len, &refs); -BUG_ON (ret); -if (refs ==1 ) { - ret =btrfs_update_ref (trans, root, buf, cow, -0 , nritems); -clean_tree_block (trans, root, buf); - }else { - ret =btrfs_inc_ref (trans, root, buf, cow,NULL ); - } -BUG_ON (ret); - }else { - ret =btrfs_update_ref (trans, root, buf, cow,0 , nritems); -if (ret) -return ret; -clean_tree_block (trans, root, buf); - } - -if (root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) { - ret =btrfs_reloc_tree_cache_ref (trans, root, cow, buf->start); -WARN_ON (ret); - } +update_ref_for_cow (trans, root, buf, cow);if (buf == root->node) {WARN_ON (parent && parent != buf); +if (root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) + parent_start = buf->start; +else + parent_start =0 ;spin_lock (&root->node_lock); root->node = cow;extent_buffer_get (cow);spin_unlock (&root->node_lock); -if (buf != root->commit_root) { -btrfs_free_extent (trans, root, buf->start, - buf->len, buf->start, - root->root_key.objectid, -btrfs_header_generation (buf), - level,1 ); - } +btrfs_free_extent (trans, root, buf->start, buf->len, + parent_start, root->root_key.objectid, + level,0 );free_extent_buffer (buf);add_root_to_dirty_list (root); }else { +if (root->root_key.objectid ==BTRFS_TREE_RELOC_OBJECTID ) + parent_start = parent->start; +else + parent_start =0 ; + +WARN_ON (trans->transid !=btrfs_header_generation (parent));btrfs_set_node_blockptr (parent, parent_slot, cow->start); -WARN_ON (trans->transid ==0 );btrfs_set_node_ptr_generation (parent, parent_slot, trans->transid);btrfs_mark_buffer_dirty (parent); -WARN_ON (btrfs_header_generation (parent) != trans->transid);btrfs_free_extent (trans, root, buf->start, buf->len, - parent_start,btrfs_header_owner (parent), -btrfs_header_generation (parent), level,1 ); + parent_start, root->root_key.objectid, + level,0 ); }if (unlock_orig)btrfs_tree_unlock (buf);
@@ -384,6 +493,18 @@ static noinline int __btrfs_cow_block(streturn 0 ; } +static int should_cow_block (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct extent_buffer *buf) +{ +if (btrfs_header_generation (buf) == trans->transid && + !btrfs_header_flag (buf,BTRFS_HEADER_FLAG_WRITTEN ) && + !(root->root_key.objectid !=BTRFS_TREE_RELOC_OBJECTID && +btrfs_header_flag (buf,BTRFS_HEADER_FLAG_RELOC ))) +return 0 ; +return 1 ; +} +/* * cows a single block, see __btrfs_cow_block for the real work. * This version of it has extra checks so that a block isn't cow'd more than
@@ -411,9 +532,7 @@ noinline int btrfs_cow_block(struct btrfWARN_ON (1 ); } -if (btrfs_header_generation (buf) == trans->transid && -btrfs_header_owner (buf) == root->root_key.objectid && - !btrfs_header_flag (buf,BTRFS_HEADER_FLAG_WRITTEN )) { +if (!should_cow_block (trans, root, buf)) { *cow_ret = buf;return 0 ; }
@@ -1620,11 +1739,9 @@ again:* then we don't want to set the path blocking, * so we test it here */ -if (btrfs_header_generation (b) == trans->transid && -btrfs_header_owner (b) == root->root_key.objectid && - !btrfs_header_flag (b,BTRFS_HEADER_FLAG_WRITTEN )) { +if (!should_cow_block (trans, root, b)) goto cow_done; - } +btrfs_set_path_blocking (p); wret = btrfs_cow_block(trans, root, b,
fs/btrfs/ctree.h +5 -3
@@ -952,8 +952,9 @@ struct btrfs_root {/* the dirty list is only used by non-reference counted roots */ struct list_head dirty_list; +struct list_head root_list; +spinlock_t list_lock; -struct list_head dead_list;struct list_head orphan_list;/*
@@ -2005,8 +2006,9 @@ int btrfs_find_last_root(struct btrfs_ro btrfs_root_item *item, struct btrfs_key *key);int btrfs_search_root (struct btrfs_root *root,u64 search_start,u64 *found_objectid); -int btrfs_find_dead_roots (struct btrfs_root *root,u64 objectid, -struct btrfs_root *latest_root); +int btrfs_find_dead_roots (struct btrfs_root *root,u64 objectid); +int btrfs_set_root_node (struct btrfs_root_item *item, +struct extent_buffer *node);/* dir-item.c */ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name,
fs/btrfs/disk-io.c +34 -35
160 lines not on this page · raw message
fs/btrfs/root-tree.c +11 -6
@@ -111,6 +111,15 @@ out:return ret; } +int btrfs_set_root_node (struct btrfs_root_item *item, +struct extent_buffer *node) +{ +btrfs_set_root_bytenr (item, node->start); +btrfs_set_root_level (item,btrfs_header_level (node)); +btrfs_set_root_generation (item,btrfs_header_generation (node)); +return 0 ; +} +/* * copy the data in 'item' into the btree */
@@ -164,8 +173,7 @@ int btrfs_insert_root(struct btrfs_trans* offset lower than the latest root. They need to be queued for deletion to * finish what was happening when we crashed. */ -int btrfs_find_dead_roots (struct btrfs_root *root,u64 objectid, -struct btrfs_root *latest) +int btrfs_find_dead_roots (struct btrfs_root *root,u64 objectid) {struct btrfs_root *dead_root;struct btrfs_item *item;
@@ -227,10 +235,7 @@ again: goto err; } -if (objectid ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_add_dead_reloc_root (dead_root); -else - ret =btrfs_add_dead_root (dead_root, latest); + ret =btrfs_add_dead_root (dead_root);if (ret) goto err; goto again;
fs/btrfs/transaction.c +135 -259
575 lines not on this page · raw message
fs/btrfs/transaction.h +4 -8
@@ -62,12 +62,6 @@ struct btrfs_pending_snapshot {struct list_head list; }; -struct btrfs_dirty_root { -struct list_head list; -struct btrfs_root *root; -struct btrfs_root *latest_root; -}; -static inline void btrfs_set_trans_block_group (struct btrfs_trans_handle *trans,struct inode *inode) {
@@ -100,7 +94,8 @@ int btrfs_write_and_wait_transaction(strint btrfs_commit_tree_roots (struct btrfs_trans_handle *trans,struct btrfs_root *root); -int btrfs_add_dead_root (struct btrfs_root *root,struct btrfs_root *latest); +int btrfs_add_dead_root (struct btrfs_root *root); +int btrfs_drop_dead_root (struct btrfs_root *root);int btrfs_defrag_root (struct btrfs_root *root,int cacheonly);int btrfs_clean_old_snapshots (struct btrfs_root *root); int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
@@ -108,7 +103,8 @@ int btrfs_commit_transaction(struct btrfint btrfs_end_transaction_throttle (struct btrfs_trans_handle *trans,struct btrfs_root *root);void btrfs_throttle (struct btrfs_root *root); -int btrfs_record_root_in_trans (struct btrfs_root *root); +int btrfs_record_root_in_trans (struct btrfs_trans_handle *trans, +struct btrfs_root *root);int btrfs_write_and_wait_marked_extents (struct btrfs_root *root,struct extent_io_tree *dirty_pages);#endif
fs/btrfs/tree-log.c +1 -3
@@ -3029,9 +3029,7 @@ again:BUG_ON (!wc.replay_dest); wc.replay_dest->log_root = log; -mutex_lock (&fs_info->trans_mutex); -btrfs_record_root_in_trans (wc.replay_dest); -mutex_unlock (&fs_info->trans_mutex); +btrfs_record_root_in_trans (trans, wc.replay_dest); ret =walk_log_tree (trans, log, &wc);BUG_ON (ret);