Skip to content

Commit e36f949

Browse files
fdmananakdave
authored andcommitted
btrfs: error out when reallocating block for defrag using a stale transaction
At btrfs_realloc_node() we have these checks to verify we are not using a stale transaction (a past transaction with an unblocked state or higher), and the only thing we do is to trigger two WARN_ON(). This however is a critical problem, highly unexpected and if it happens it's most likely due to a bug, so we should error out and turn the fs into error state so that such issue is much more easily noticed if it's triggered. The problem is critical because in btrfs_realloc_node() we COW tree blocks, and using such stale transaction will lead to not persisting the extent buffers used for the COW operations, as allocating tree block adds the range of the respective extent buffers to the ->dirty_pages iotree of the transaction, and a stale transaction, in the unlocked state or higher, will not flush dirty extent buffers anymore, therefore resulting in not persisting the tree block and resource leaks (not cleaning the dirty_pages iotree for example). So do the following changes: 1) Return -EUCLEAN if we find a stale transaction; 2) Turn the fs into error state, with error -EUCLEAN, so that no transaction can be committed, and generate a stack trace; 3) Combine both conditions into a single if statement, as both are related and have the same error message; 4) Mark the check as unlikely, since this is not expected to ever happen. Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent a2caab2 commit e36f949

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

fs/btrfs/ctree.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,22 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
817817
int progress_passed = 0;
818818
struct btrfs_disk_key disk_key;
819819

820-
WARN_ON(trans->transaction != fs_info->running_transaction);
821-
WARN_ON(trans->transid != fs_info->generation);
820+
/*
821+
* COWing must happen through a running transaction, which always
822+
* matches the current fs generation (it's a transaction with a state
823+
* less than TRANS_STATE_UNBLOCKED). If it doesn't, then turn the fs
824+
* into error state to prevent the commit of any transaction.
825+
*/
826+
if (unlikely(trans->transaction != fs_info->running_transaction ||
827+
trans->transid != fs_info->generation)) {
828+
btrfs_abort_transaction(trans, -EUCLEAN);
829+
btrfs_crit(fs_info,
830+
"unexpected transaction when attempting to reallocate parent %llu for root %llu, transaction %llu running transaction %llu fs generation %llu",
831+
parent->start, btrfs_root_id(root), trans->transid,
832+
fs_info->running_transaction->transid,
833+
fs_info->generation);
834+
return -EUCLEAN;
835+
}
822836

823837
parent_nritems = btrfs_header_nritems(parent);
824838
blocksize = fs_info->nodesize;

0 commit comments

Comments
 (0)