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"
29 $BTRFS_UTIL_PROG inspect-internal dump-tree -t CHUNK $SCRATCH_DEV |\
30 grep -A 1 "CHUNK_ITEM" | grep -B 1 "type DATA" |\
31 grep -Eo "CHUNK_ITEM [[:digit:]]+" | cut -d ' ' -f 2
34 zonesize=$(cat /sys/block/$(_short_dev $SCRATCH_DEV)/queue/chunk_sectors)
35 zonesize=$((zonesize << 9))
37 _scratch_mkfs >/dev/null 2>&1
38 _scratch_mount -o commit=1 # 1s commit time to speed up test
40 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
42 echo $reclaim_threshold > /sys/fs/btrfs/"$uuid"/bg_reclaim_threshold
43 fill_percent=$((reclaim_threshold + 2))
44 rest_percent=$((90 - fill_percent)) # make sure we're not creating a new BG
45 fill_size=$((zonesize * fill_percent / 100))
46 rest=$((zonesize * rest_percent / 100))
48 # step 1, fill FS over $fillsize
49 $XFS_IO_PROG -fc "pwrite 0 $fill_size" $SCRATCH_MNT/$seq.test1 >> $seqres.full
50 $XFS_IO_PROG -fc "pwrite 0 $rest" $SCRATCH_MNT/$seq.test2 >> $seqres.full
51 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
53 zones_before=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
54 echo "Before reclaim: $zones_before zones open" >> $seqres.full
55 old_data_zone=$(get_data_bg)
56 old_data_zone=$((old_data_zone >> 9))
57 printf "Old data zone 0x%x\n" $old_data_zone >> $seqres.full
59 # step 2, delete the 1st $fill_size sized file to trigger reclaim
60 rm $SCRATCH_MNT/$seq.test1
61 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
62 sleep 2 # 1 transaction commit for 'rm' and 1 for balance
64 # check that we don't have more zones open than before
65 zones_after=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
66 echo "After reclaim: $zones_after zones open" >> $seqres.full
68 # Check that old data zone was reset
69 old_wptr=$($BLKZONE_PROG report -o $old_data_zone -c 1 $SCRATCH_DEV |\
70 grep -Eo "wptr 0x[[:xdigit:]]+" | cut -d ' ' -f 2)
71 if [ "$old_wptr" != "0x000000" ]; then
72 _fail "Old wptr still at $old_wptr"
75 new_data_zone=$(get_data_bg)
76 new_data_zone=$((new_data_zone >> 9))
77 printf "New data zone 0x%x\n" $new_data_zone >> $seqres.full
79 # Check that data was really relocated to a different zone
80 if [ $old_data_zone -eq $new_data_zone ]; then
81 echo "New zone same as old zone"
85 echo "Silence is golden"