btrfs: qgroup: skip inconsistent accounting after mount
PATCH 1/1 btrfs: qgroup: skip inconsistent accounting after mount
When full qgroups become inconsistent, qgroup_mark_inconsistent() sets NO_ACCOUNTING to avoid expensive backref walks which cannot repair the counters. This is a runtime flag, so unmounting and mounting the filesystem loses it. Incremental accounting then resumes even when the persistent INCONSISTENT flag is set and no rescan is pending.
On a filesystem with many snapshots, the resulting backref walks can stall transaction commits and operations waiting to join a transaction.
Use qgroup_mark_inconsistent() when loading an inconsistent full-qgroup configuration without a pending rescan. This restores NO_ACCOUNTING and CANCEL_RESCAN. Preserve the existing rescan initialization path, which clears both flags for resumed and newly requested rescans. Simple quotas are unaffected.
---
Prepared with assistance from OpenAI Codex.
Tested on the same for-next base and x86_64 config before/after the fix:
- Full kernel builds and W=1 fs/btrfs/qgroup.o, without compiler warnings.
- QEMU, 620 snapshots, 10 ms read delay: maximum create latency drops from 36.47 s to 2.31 s; btrfs_find_all_roots_safe calls drop from 1476 to 0.
- Manual rescan and mount-time resume complete with inconsistency cleared.
- Fresh-image consistent full, disabled and simple quota smoke tests, including offline read-only filesystem checks after each stage.
fs/btrfs/qgroup.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
fs/btrfs/qgroup.c +11 -1
@@ -615,8 +615,18 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)if (ret >=0 ) {if (test_bit (BTRFS_QGROUP_STATUS_BIT_ON , &fs_info->qgroup_flags))set_bit (BTRFS_FS_QUOTA_ENABLED , &fs_info->flags); -if (test_bit (BTRFS_QGROUP_STATUS_BIT_RESCAN , &fs_info->qgroup_flags)) +if (test_bit (BTRFS_QGROUP_STATUS_BIT_RESCAN , &fs_info->qgroup_flags)) { ret =qgroup_rescan_init (fs_info, rescan_progress,0 ); + }else if (btrfs_qgroup_full_accounting (fs_info) && +test_bit (BTRFS_QGROUP_STATUS_BIT_INCONSISTENT , + &fs_info->qgroup_flags)) { +/* +* Incremental accounting cannot repair inconsistent counters. +* Keep it disabled until a new rescan is started. +*/ +qgroup_mark_inconsistent (fs_info, +"inconsistent qgroup status found at mount" ); + } }else {clear_bit (BTRFS_QGROUP_STATUS_BIT_RESCAN , &fs_info->qgroup_flags);btrfs_sysfs_del_qgroups (fs_info);
base-commit: 50fd5933d3fa2a70acbaf87a21c355f6415974fa