Btrfs: make the chunk allocator completely tree lockless
PATCH 1/1 Btrfs: make the chunk allocator completely tree lockless
When adjusting the enospc rules for relocation I ran into a deadlock because we were relocating the only system chunk and that forced us to try and allocate a new system chunk while holding locks in the chunk tree, which caused us to deadlock. To fix this I've moved all of the dev extent addition and chunk addition out to the delayed chunk completion stuff. We still keep the in-memory stuff which makes sure everything is consistent.
One change I had to make was to search the commit root of the device tree to find a free dev extent, and hold onto any chunk em's that we allocated in that transaction so we do not allocate the same dev extent twice. This has the side effect of fixing a bug with balance that has been there ever since balance existed. Basically you can free a block group and it's dev extent and then immediately allocate that dev extent for a new block group and write stuff to that dev extent, all within the same transaction. So if you happen to crash during a balance you could come back to a completely broken file system. This patch should keep these sort of things from happening in the future since we won't be able to allocate free'd dev extents until after the transaction commits. This has passed all of the xfstests and my super annoying stress test followed by a balance. Thanks,
--- fs/btrfs/extent-tree.c | 15 +++- fs/btrfs/transaction.c | 9 ++ fs/btrfs/transaction.h | 1 + fs/btrfs/volumes.c | 301 ++++++++++++++++++++++-------------------------- fs/btrfs/volumes.h | 6 +- 5 files changed, 165 insertions(+), 167 deletions(-)
fs/btrfs/extent-tree.c +14 -1
@@ -7950,6 +7950,7 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)struct btrfs_space_info *space_info;struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;struct btrfs_device *device; +struct btrfs_trans_handle *trans;u64 min_free;u64 dev_min =1 ;u64 dev_nr =0 ;
@@ -8036,6 +8037,13 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)do_div (min_free, dev_min); } +/* We need to do this so that we can look at pending chunks */ + trans =btrfs_join_transaction (root); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); + goto out; + } +mutex_lock (&root->fs_info->chunk_mutex);list_for_each_entry (device, &fs_devices->alloc_list, dev_alloc_list) {u64 dev_offset;
@@ -8046,7 +8054,7 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)*/ if (device->total_bytes > device->bytes_used + min_free && !device->is_tgtdev_for_dev_replace) { - ret =find_free_dev_extent (device, min_free, + ret =find_free_dev_extent (trans, device, min_free, &dev_offset,NULL );if (!ret) dev_nr++;
@@ -8058,6 +8066,7 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) } }mutex_unlock (&root->fs_info->chunk_mutex); +btrfs_end_transaction (trans, root); out:btrfs_put_block_group (block_group);return ret;
@@ -8423,6 +8432,10 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, sizeof(item));if (ret)btrfs_abort_transaction (trans, extent_root, ret); + ret =btrfs_finish_chunk_alloc (trans, extent_root, + key.objectid, key.offset); +if (ret) +btrfs_abort_transaction (trans, root, ret); } }
fs/btrfs/transaction.c +9 -0
@@ -63,6 +63,14 @@ static void put_transaction(struct btrfs_transaction *transaction)if (atomic_dec_and_test (&transaction->use_count)) {BUG_ON (!list_empty (&transaction->list));WARN_ON (transaction->delayed_refs.root.rb_node); +while (!list_empty (&transaction->pending_chunks)) { +struct extent_map *em; + + em =list_first_entry (&transaction->pending_chunks, + struct extent_map, list); +list_del_init (&em->list); +free_extent_map (em); + }kmem_cache_free (btrfs_transaction_cachep, transaction); } }
@@ -202,6 +210,7 @@ loop:INIT_LIST_HEAD (&cur_trans->pending_snapshots);INIT_LIST_HEAD (&cur_trans->ordered_operations); +INIT_LIST_HEAD (&cur_trans->pending_chunks);list_add_tail (&cur_trans->list, &fs_info->trans_list);extent_io_tree_init (&cur_trans->dirty_pages, fs_info->btree_inode->i_mapping);
fs/btrfs/transaction.h +1 -0
@@ -56,6 +56,7 @@ struct btrfs_transaction {wait_queue_head_t commit_wait;struct list_head pending_snapshots;struct list_head ordered_operations; +struct list_head pending_chunks;struct btrfs_delayed_ref_root delayed_refs;int aborted; };
fs/btrfs/volumes.c +136 -165
469 lines not on this page · raw message
fs/btrfs/volumes.h +5 -1
@@ -316,7 +316,8 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info);int btrfs_pause_balance (struct btrfs_fs_info *fs_info);int btrfs_cancel_balance (struct btrfs_fs_info *fs_info);int btrfs_chunk_readonly (struct btrfs_root *root,u64 chunk_offset); -int find_free_dev_extent (struct btrfs_device *device,u64 num_bytes, +int find_free_dev_extent (struct btrfs_trans_handle *trans, +struct btrfs_device *device,u64 num_bytes,u64 *start,u64 *max_avail);void btrfs_dev_stat_inc_and_print (struct btrfs_device *dev,int index); int btrfs_get_dev_stats(struct btrfs_root *root,
@@ -337,6 +338,9 @@ int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,unsigned long btrfs_full_stripe_len (struct btrfs_root *root,struct btrfs_mapping_tree *map_tree,u64 logical); +int btrfs_finish_chunk_alloc (struct btrfs_trans_handle *trans, +struct btrfs_root *extent_root, +u64 chunk_offset,u64 chunk_size);static inline void btrfs_dev_stat_inc (struct btrfs_device *dev,int index) {
Signature · 1 line
1.7.7.6
Reviewing as requested! It certainly looks reasonable, but I don't have enough history with the code to really say much more than that.
Some questions:
fs/btrfs/extent-tree.c void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, · line 8436
Quoted 6 lines
fs/btrfs/extent-tree.c +2
-0@@ -8423,6 +8432,10 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, sizeof(item));if (ret)btrfs_abort_transaction (trans, extent_root, ret); + ret =btrfs_finish_chunk_alloc (trans, extent_root, + key.objectid, key.offset);
Would this moved call site need to worry about the chunk mutex at all?
fs/btrfs/extent-tree.c void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, · line 8438
Quoted 2 lines
fs/btrfs/extent-tree.c +2
-0+if (ret) +btrfs_abort_transaction (trans, root, ret);
Did you mean to abort the root here, not the extent_root like the one above?
Quoted 14 lines
fs/btrfs/volumes.c +14
-0+static int contains_pending_extent (struct btrfs_trans_handle *trans, +struct btrfs_device *device, +u64 *start,u64 len) +{ +struct extent_map *em; +int ret =0 ; + +list_for_each_entry (em, &trans->transaction->pending_chunks, list) { +struct map_lookup *map; +int i; + + map = (struct map_lookup *)em->bdev; +for (i =0 ; i < map->num_stripes; i++) { +if (map->stripes[i].dev != device)
fs/btrfs/volumes.c out: · line 1008
Quoted 10 lines
fs/btrfs/volumes.c +10
-0+continue ; +if (map->stripes[i].physical >= *start + len || + map->stripes[i].physical + em->orig_block_len <= + *start) +continue ; + *start = map->stripes[i].physical + + em->orig_block_len; + ret =1 ; + } + }
Did you want a 'break;' there? Is it trying to set *start to the last match rather than returning the first?
fs/btrfs/volumes.c error: · line 4026
Quoted 7 lines
fs/btrfs/volumes.c +7
-0+ ret =btrfs_alloc_dev_extent (trans, device, + chunk_root->root_key.objectid, +BTRFS_FIRST_CHUNK_TREE_OBJECTID , + chunk_offset, dev_offset, + stripe_size); +if (ret) + goto out;
The old code tried to free up previous allocations when one failed, but it looks like this deferred alloc doesn't. Do we care?
fs/btrfs/volumes.c error:
Quoted 5 lines
fs/btrfs/volumes.c +1
-3- index =0 ; -while (index < map->num_stripes) { - index++; +for (i =0 ; i < map->num_stripes; i++) {
*hugs*
- z
Quoted 5 lines · Zach Brown
Reviewing as requested! It certainly looks reasonable, but I don't have enough history with the code to really say much more than that.
Some questions:
Quoted 9 lines
fs/btrfs/extent-tree.c void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, · line 8436
Quoted 6 lines
fs/btrfs/extent-tree.c +2
-0@@ -8423,6 +8432,10 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, sizeof(item));if (ret)btrfs_abort_transaction (trans, extent_root, ret); + ret =btrfs_finish_chunk_alloc (trans, extent_root, + key.objectid, key.offset);Would this moved call site need to worry about the chunk mutex at all?
No, we're just using the chunk_mutex to serialize the allocations. So since all the stuff we care about is in memory or in the commit root we only need to have the chunk_mutex around the actuall allocation part.
Quoted 5 lines
fs/btrfs/extent-tree.c void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, · line 8438
Quoted 2 lines
fs/btrfs/extent-tree.c +2
-0+if (ret) +btrfs_abort_transaction (trans, root, ret);Did you mean to abort the root here, not the
extent_rootlike the one above?
I'm not sure it matters but I'll change it for consistency sake.
Quoted 20 lines
Quoted 20 lines
fs/btrfs/volumes.c +19
-0+static int contains_pending_extent (struct btrfs_trans_handle *trans, +struct btrfs_device *device, +u64 *start,u64 len) +{ +struct extent_map *em; +int ret =0 ; + +list_for_each_entry (em, &trans->transaction->pending_chunks, list) { +struct map_lookup *map; +int i; + + map = (struct map_lookup *)em->bdev; +for (i =0 ; i < map->num_stripes; i++) { +if (map->stripes[i].dev != device) +continue ; +if (map->stripes[i].physical >= *start + len || + map->stripes[i].physical + em->orig_block_len <= + *start) +continue ;
Quoted 9 lines
fs/btrfs/volumes.c out: · line 1008
Quoted 5 lines
fs/btrfs/volumes.c +5
-0+ *start = map->stripes[i].physical + + em->orig_block_len; + ret =1 ; + } + }Did you want a 'break;' there? Is it trying to set *start to the last match rather than returning the first?
We want the last, otherwise we'll set it to the end of the first extent we find, go back and do all that searching again, come back here and just adjust it up again if theres another allocation sitting there that butts up to the first entry we found.
Quoted 11 lines
fs/btrfs/volumes.c error: · line 4026
Quoted 7 lines
fs/btrfs/volumes.c +7
-0+ ret =btrfs_alloc_dev_extent (trans, device, + chunk_root->root_key.objectid, +BTRFS_FIRST_CHUNK_TREE_OBJECTID , + chunk_offset, dev_offset, + stripe_size); +if (ret) + goto out;The old code tried to free up previous allocations when one failed, but it looks like this deferred alloc doesn't. Do we care?
So before we could back out of having a chunk allocation at that point because we hadn't let go of the chunk mutex and nobody had been able to allocate from that chunk. With the new code if we fail here we have to abort because we could have made allocations in our new chunk. It is kind of a drawback, but really if this part failed before it was likely because of -EIO which we would likely hit somewhere else in the stack and end up aborted anyway, so no big loss I think?
Quoted 8 lines
fs/btrfs/volumes.c error:
Quoted 5 lines
fs/btrfs/volumes.c +1
-3- index =0 ; -while (index < map->num_stripes) { - index++; +for (i =0 ; i < map->num_stripes; i++) {*hugs*
Thanks for the review! I'll just fix up that one inconsistency and then commit and push to btrfs-next instead of reposting. Thanks,
Josef