Skip to content

Commit 7b7291a

Browse files
fdmananagregkh
authored andcommitted
btrfs: fix race between quota disable and relocation
[ Upstream commit 8a4a0b2 ] If we disable quotas while we have a relocation of a metadata block group that has extents belonging to the quota root, we can cause the relocation to fail with -ENOENT. This is because relocation builds backref nodes for extents of the quota root and later needs to walk the backrefs and access the quota root - however if in between a task disables quotas, it results in deleting the quota root from the root tree (with btrfs_del_root(), called from btrfs_quota_disable(). This can be sporadically triggered by test case btrfs/255 from fstests: $ ./check btrfs/255 FSTYP -- btrfs PLATFORM -- Linux/x86_64 debian0 6.4.0-rc6-btrfs-next-134+ #1 SMP PREEMPT_DYNAMIC Thu Jun 15 11:59:28 WEST 2023 MKFS_OPTIONS -- /dev/sdc MOUNT_OPTIONS -- /dev/sdc /home/fdmanana/btrfs-tests/scratch_1 btrfs/255 6s ... _check_dmesg: something found in dmesg (see /home/fdmanana/git/hub/xfstests/results//btrfs/255.dmesg) - output mismatch (see /home/fdmanana/git/hub/xfstests/results//btrfs/255.out.bad) # --- tests/btrfs/255.out 2023-03-02 21:47:53.876609426 +0000 # +++ /home/fdmanana/git/hub/xfstests/results//btrfs/255.out.bad 2023-06-16 10:20:39.267563212 +0100 # @@ -1,2 +1,4 @@ # QA output created by 255 # +ERROR: error during balancing '/home/fdmanana/btrfs-tests/scratch_1': No such file or directory # +There may be more info in syslog - try dmesg | tail # Silence is golden # ... (Run 'diff -u /home/fdmanana/git/hub/xfstests/tests/btrfs/255.out /home/fdmanana/git/hub/xfstests/results//btrfs/255.out.bad' to see the entire diff) Ran: btrfs/255 Failures: btrfs/255 Failed 1 of 1 tests To fix this make the quota disable operation take the cleaner mutex, as relocation of a block group also takes this mutex. This is also what we do when deleting a subvolume/snapshot, we take the cleaner mutex in the cleaner kthread (at cleaner_kthread()) and then we call btrfs_del_root() at btrfs_drop_snapshot() while under the protection of the cleaner mutex. Fixes: bed92ea ("Btrfs: qgroup implementation and prototypes") CC: stable@vger.kernel.org # 5.4+ Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 44e2afb commit 7b7291a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

fs/btrfs/qgroup.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,12 +1227,23 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
12271227
int ret = 0;
12281228

12291229
/*
1230-
* We need to have subvol_sem write locked, to prevent races between
1231-
* concurrent tasks trying to disable quotas, because we will unlock
1232-
* and relock qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
1230+
* We need to have subvol_sem write locked to prevent races with
1231+
* snapshot creation.
12331232
*/
12341233
lockdep_assert_held_write(&fs_info->subvol_sem);
12351234

1235+
/*
1236+
* Lock the cleaner mutex to prevent races with concurrent relocation,
1237+
* because relocation may be building backrefs for blocks of the quota
1238+
* root while we are deleting the root. This is like dropping fs roots
1239+
* of deleted snapshots/subvolumes, we need the same protection.
1240+
*
1241+
* This also prevents races between concurrent tasks trying to disable
1242+
* quotas, because we will unlock and relock qgroup_ioctl_lock across
1243+
* BTRFS_FS_QUOTA_ENABLED changes.
1244+
*/
1245+
mutex_lock(&fs_info->cleaner_mutex);
1246+
12361247
mutex_lock(&fs_info->qgroup_ioctl_lock);
12371248
if (!fs_info->quota_root)
12381249
goto out;
@@ -1314,6 +1325,7 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
13141325
btrfs_end_transaction(trans);
13151326
else if (trans)
13161327
ret = btrfs_end_transaction(trans);
1328+
mutex_unlock(&fs_info->cleaner_mutex);
13171329

13181330
return ret;
13191331
}

0 commit comments

Comments
 (0)