2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Western Digital Corporation. All Rights Reserved.
7 # Test that zone autoreclaim works as expected, that is: if the dirty
8 # threshold is exceeded the data gets relocated to new block group and the
9 # old block group gets deleted. On block group deletion, the underlying device
10 # zone also needs to be reset.
13 _begin_fstest auto quick zone balance
15 # Import common functions.
18 # real QA test starts here
22 _require_btrfs_command inspect-internal dump-tree
23 _require_btrfs_command filesystem sync
24 _require_command "$BLKZONE_PROG" blkzone
25 _require_zoned_device "$SCRATCH_DEV"
27 # This test requires specific data space usage, skip if we have compression
33 $BTRFS_UTIL_PROG inspect-internal dump-tree -t CHUNK $SCRATCH_DEV |\
34 grep -A 1 "CHUNK_ITEM" | grep -B 1 "type DATA" |\
35 grep -Eo "CHUNK_ITEM [[:digit:]]+" | cut -d ' ' -f 2
38 zonesize=$(cat /sys/block/$(_short_dev $SCRATCH_DEV)/queue/chunk_sectors)
39 zonesize=$((zonesize << 9))
41 _scratch_mkfs >/dev/null 2>&1
42 _scratch_mount -o commit=1 # 1s commit time to speed up test
44 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
46 echo $reclaim_threshold > /sys/fs/btrfs/"$uuid"/bg_reclaim_threshold
47 fill_percent=$((reclaim_threshold + 2))
48 rest_percent=$((90 - fill_percent)) # make sure we're not creating a new BG
49 fill_size=$((zonesize * fill_percent / 100))
50 rest=$((zonesize * rest_percent / 100))
52 # step 1, fill FS over $fillsize
53 $XFS_IO_PROG -fc "pwrite 0 $fill_size" $SCRATCH_MNT/$seq.test1 >> $seqres.full
54 $XFS_IO_PROG -fc "pwrite 0 $rest" $SCRATCH_MNT/$seq.test2 >> $seqres.full
55 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
57 zones_before=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
58 echo "Before reclaim: $zones_before zones open" >> $seqres.full
59 old_data_zone=$(get_data_bg)
60 old_data_zone=$((old_data_zone >> 9))
61 printf "Old data zone 0x%x\n" $old_data_zone >> $seqres.full
63 # step 2, delete the 1st $fill_size sized file to trigger reclaim
64 rm $SCRATCH_MNT/$seq.test1
65 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
66 sleep 2 # 1 transaction commit for 'rm' and 1 for balance
68 # check that we don't have more zones open than before
69 zones_after=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
70 echo "After reclaim: $zones_after zones open" >> $seqres.full
72 # Check that old data zone was reset
73 old_wptr=$($BLKZONE_PROG report -o $old_data_zone -c 1 $SCRATCH_DEV |\
74 grep -Eo "wptr 0x[[:xdigit:]]+" | cut -d ' ' -f 2)
75 if [ "$old_wptr" != "0x000000" ]; then
76 _fail "Old wptr still at $old_wptr"
79 new_data_zone=$(get_data_bg)
80 new_data_zone=$((new_data_zone >> 9))
81 printf "New data zone 0x%x\n" $new_data_zone >> $seqres.full
83 # Check that data was really relocated to a different zone
84 if [ $old_data_zone -eq $new_data_zone ]; then
85 echo "New zone same as old zone"
89 echo "Silence is golden"