Skip to content

Commit debd1c0

Browse files
lorddoskiaskdave
authored andcommitted
btrfs: Ensure replaced device doesn't have pending chunk allocation
Recent FITRIM work, namely bbbf724 ("btrfs: combine device update operations during transaction commit") combined the way certain operations are recoded in a transaction. As a result an ASSERT was added in dev_replace_finish to ensure the new code works correctly. Unfortunately I got reports that it's possible to trigger the assert, meaning that during a device replace it's possible to have an unfinished chunk allocation on the source device. This is supposed to be prevented by the fact that a transaction is committed before finishing the replace oepration and alter acquiring the chunk mutex. This is not sufficient since by the time the transaction is committed and the chunk mutex acquired it's possible to allocate a chunk depending on the workload being executed on the replaced device. This bug has been present ever since device replace was introduced but there was never code which checks for it. The correct way to fix is to ensure that there is no pending device modification operation when the chunk mutex is acquire and if there is repeat transaction commit. Unfortunately it's not possible to just exclude the source device from btrfs_fs_devices::dev_alloc_list since this causes ENOSPC to be hit in transaction commit. Fixing that in another way would need to add special cases to handle the last writes and forbid new ones. The looped transaction fix is more obvious, and can be easily backported. The runtime of dev-replace is long so there's no noticeable delay caused by that. Reported-by: David Sterba <dsterba@suse.com> Fixes: 391cd9d ("Btrfs: fix unprotected alloc list insertion during the finishing procedure of replace") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 4e9845e commit debd1c0

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

‎fs/btrfs/dev-replace.c‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,33 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
603603
}
604604
btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
605605

606-
trans = btrfs_start_transaction(root, 0);
607-
if (IS_ERR(trans)) {
608-
mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
609-
return PTR_ERR(trans);
606+
/*
607+
* We have to use this loop approach because at this point src_device
608+
* has to be available for transaction commit to complete, yet new
609+
* chunks shouldn't be allocated on the device.
610+
*/
611+
while (1) {
612+
trans = btrfs_start_transaction(root, 0);
613+
if (IS_ERR(trans)) {
614+
mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
615+
return PTR_ERR(trans);
616+
}
617+
ret = btrfs_commit_transaction(trans);
618+
WARN_ON(ret);
619+
620+
/* Prevent write_all_supers() during the finishing procedure */
621+
mutex_lock(&fs_info->fs_devices->device_list_mutex);
622+
/* Prevent new chunks being allocated on the source device */
623+
mutex_lock(&fs_info->chunk_mutex);
624+
625+
if (!list_empty(&src_device->post_commit_list)) {
626+
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
627+
mutex_unlock(&fs_info->chunk_mutex);
628+
} else {
629+
break;
630+
}
610631
}
611-
ret = btrfs_commit_transaction(trans);
612-
WARN_ON(ret);
613632

614-
/* keep away write_all_supers() during the finishing procedure */
615-
mutex_lock(&fs_info->fs_devices->device_list_mutex);
616-
mutex_lock(&fs_info->chunk_mutex);
617633
down_write(&dev_replace->rwsem);
618634
dev_replace->replace_state =
619635
scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
@@ -662,7 +678,6 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
662678
btrfs_device_set_disk_total_bytes(tgt_device,
663679
src_device->disk_total_bytes);
664680
btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
665-
ASSERT(list_empty(&src_device->post_commit_list));
666681
tgt_device->commit_total_bytes = src_device->commit_total_bytes;
667682
tgt_device->commit_bytes_used = src_device->bytes_used;
668683

0 commit comments

Comments
 (0)