Skip to content

Commit 6f23277

Browse files
adam900710kdave
authored andcommitted
btrfs: qgroup: don't commit transaction when we already hold the handle
[BUG] When running the following script, btrfs will trigger an ASSERT(): #/bin/bash mkfs.btrfs -f $dev mount $dev $mnt xfs_io -f -c "pwrite 0 1G" $mnt/file sync btrfs quota enable $mnt btrfs quota rescan -w $mnt # Manually set the limit below current usage btrfs qgroup limit 512M $mnt $mnt # Crash happens touch $mnt/file The dmesg looks like this: assertion failed: refcount_read(&trans->use_count) == 1, in fs/btrfs/transaction.c:2022 ------------[ cut here ]------------ kernel BUG at fs/btrfs/ctree.h:3230! invalid opcode: 0000 [#1] SMP PTI RIP: 0010:assertfail.constprop.0+0x18/0x1a [btrfs] btrfs_commit_transaction.cold+0x11/0x5d [btrfs] try_flush_qgroup+0x67/0x100 [btrfs] __btrfs_qgroup_reserve_meta+0x3a/0x60 [btrfs] btrfs_delayed_update_inode+0xaa/0x350 [btrfs] btrfs_update_inode+0x9d/0x110 [btrfs] btrfs_dirty_inode+0x5d/0xd0 [btrfs] touch_atime+0xb5/0x100 iterate_dir+0xf1/0x1b0 __x64_sys_getdents64+0x78/0x110 do_syscall_64+0x33/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7fb5afe588db [CAUSE] In try_flush_qgroup(), we assume we don't hold a transaction handle at all. This is true for data reservation and mostly true for metadata. Since data space reservation always happens before we start a transaction, and for most metadata operation we reserve space in start_transaction(). But there is an exception, btrfs_delayed_inode_reserve_metadata(). It holds a transaction handle, while still trying to reserve extra metadata space. When we hit EDQUOT inside btrfs_delayed_inode_reserve_metadata(), we will join current transaction and commit, while we still have transaction handle from qgroup code. [FIX] Let's check current->journal before we join the transaction. If current->journal is unset or BTRFS_SEND_TRANS_STUB, it means we are not holding a transaction, thus are able to join and then commit transaction. If current->journal is a valid transaction handle, we avoid committing transaction and just end it This is less effective than committing current transaction, as it won't free metadata reserved space, but we may still free some data space before new data writes. Bugzilla: https://bugzilla.suse.com/show_bug.cgi?id=1178634 Fixes: c53e965 ("btrfs: qgroup: try to flush qgroup space when we get -EDQUOT") Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent c334730 commit 6f23277

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

fs/btrfs/qgroup.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3512,6 +3512,7 @@ static int try_flush_qgroup(struct btrfs_root *root)
35123512
{
35133513
struct btrfs_trans_handle *trans;
35143514
int ret;
3515+
bool can_commit = true;
35153516

35163517
/*
35173518
* We don't want to run flush again and again, so if there is a running
@@ -3523,6 +3524,20 @@ static int try_flush_qgroup(struct btrfs_root *root)
35233524
return 0;
35243525
}
35253526

3527+
/*
3528+
* If current process holds a transaction, we shouldn't flush, as we
3529+
* assume all space reservation happens before a transaction handle is
3530+
* held.
3531+
*
3532+
* But there are cases like btrfs_delayed_item_reserve_metadata() where
3533+
* we try to reserve space with one transction handle already held.
3534+
* In that case we can't commit transaction, but at least try to end it
3535+
* and hope the started data writes can free some space.
3536+
*/
3537+
if (current->journal_info &&
3538+
current->journal_info != BTRFS_SEND_TRANS_STUB)
3539+
can_commit = false;
3540+
35263541
ret = btrfs_start_delalloc_snapshot(root);
35273542
if (ret < 0)
35283543
goto out;
@@ -3534,7 +3549,10 @@ static int try_flush_qgroup(struct btrfs_root *root)
35343549
goto out;
35353550
}
35363551

3537-
ret = btrfs_commit_transaction(trans);
3552+
if (can_commit)
3553+
ret = btrfs_commit_transaction(trans);
3554+
else
3555+
ret = btrfs_end_transaction(trans);
35383556
out:
35393557
clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
35403558
wake_up(&root->qgroup_flush_wait);

0 commit comments

Comments
 (0)