Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: qgroup: Search commit root for rescan to avoid missing extent

When doing qgroup rescan using the following script (modified from
btrfs/017 test case), we can sometimes hit qgroup corruption.

------
umount $dev &> /dev/null
umount $mnt &> /dev/null

mkfs.btrfs -f -n 64k $dev
mount $dev $mnt

extent_size=8192

xfs_io -f -d -c "pwrite 0 $extent_size" $mnt/foo > /dev/null
btrfs subvolume snapshot $mnt $mnt/snap

xfs_io -f -c "reflink $mnt/foo" $mnt/foo-reflink > /dev/null
xfs_io -f -c "reflink $mnt/foo" $mnt/snap/foo-reflink > /dev/null
xfs_io -f -c "reflink $mnt/foo" $mnt/snap/foo-reflink2 > /dev/unll
btrfs quota enable $mnt

# -W is the new option to only wait rescan while not starting new one
btrfs quota rescan -W $mnt
btrfs qgroup show -prce $mnt
umount $mnt

# Need to patch btrfs-progs to report qgroup mismatch as error
btrfs check $dev || _fail
------

For fast machine, we can hit some corruption which missed accounting
tree blocks:
------
qgroupid rfer excl max_rfer max_excl parent child
-------- ---- ---- -------- -------- ------ -----
0/5 8.00KiB 0.00B none none --- ---
0/257 8.00KiB 0.00B none none --- ---
------

This is due to the fact that we're always searching commit root for
btrfs_find_all_roots() at qgroup_rescan_leaf(), but the leaf we get is
from current transaction, not commit root.

And if our tree blocks get modified in current transaction, we won't
find any owner in commit root, thus causing the corruption.

Fix it by searching commit root for extent tree for
qgroup_rescan_leaf().

Reported-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>

authored by

Qu Wenruo and committed by
David Sterba
b6debf15 7a1b1e70

+6 -3
+6 -3
fs/btrfs/qgroup.c
··· 2590 2590 struct btrfs_key found; 2591 2591 struct extent_buffer *scratch_leaf = NULL; 2592 2592 struct ulist *roots = NULL; 2593 - struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); 2594 2593 u64 num_bytes; 2595 2594 int slot; 2596 2595 int ret; ··· 2624 2625 btrfs_header_nritems(path->nodes[0]) - 1); 2625 2626 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1; 2626 2627 2627 - btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); 2628 2628 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]); 2629 2629 if (!scratch_leaf) { 2630 2630 ret = -ENOMEM; ··· 2662 2664 btrfs_tree_read_unlock_blocking(scratch_leaf); 2663 2665 free_extent_buffer(scratch_leaf); 2664 2666 } 2665 - btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); 2666 2667 2667 2668 return ret; 2668 2669 } ··· 2678 2681 path = btrfs_alloc_path(); 2679 2682 if (!path) 2680 2683 goto out; 2684 + /* 2685 + * Rescan should only search for commit root, and any later difference 2686 + * should be recorded by qgroup 2687 + */ 2688 + path->search_commit_root = 1; 2689 + path->skip_locking = 1; 2681 2690 2682 2691 err = 0; 2683 2692 while (!err && !btrfs_fs_closing(fs_info)) {