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 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
18 status=1 # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
27 # get standard environment, filters and checks
31 # remove previous $seqres.full before test
34 # real QA test starts here
38 _require_btrfs_command inspect-internal dump-tree
39 _require_btrfs_command filesystem sync
40 _require_command "$BLKZONE_PROG" blkzone
41 _require_zoned_device "$SCRATCH_DEV"
45 $BTRFS_UTIL_PROG inspect-internal dump-tree -t CHUNK $SCRATCH_DEV |\
46 grep -A 1 "CHUNK_ITEM" | grep -B 1 "type DATA" |\
47 grep -Eo "CHUNK_ITEM [[:digit:]]+" | cut -d ' ' -f 2
50 zonesize=$(cat /sys/block/$(_short_dev $SCRATCH_DEV)/queue/chunk_sectors)
51 zonesize=$((zonesize << 9))
53 _scratch_mkfs >/dev/null 2>&1
54 _scratch_mount -o commit=1 # 1s commit time to speed up test
56 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
58 echo $reclaim_threshold > /sys/fs/btrfs/"$uuid"/bg_reclaim_threshold
59 fill_percent=$((reclaim_threshold + 2))
60 rest_percent=$((90 - fill_percent)) # make sure we're not creating a new BG
61 fill_size=$((zonesize * fill_percent / 100))
62 rest=$((zonesize * rest_percent / 100))
64 # step 1, fill FS over $fillsize
65 $XFS_IO_PROG -fc "pwrite 0 $fill_size" $SCRATCH_MNT/$seq.test1 >> $seqres.full
66 $XFS_IO_PROG -fc "pwrite 0 $rest" $SCRATCH_MNT/$seq.test2 >> $seqres.full
67 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
69 zones_before=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
70 echo "Before reclaim: $zones_before zones open" >> $seqres.full
71 old_data_zone=$(get_data_bg)
72 old_data_zone=$((old_data_zone >> 9))
73 printf "Old data zone 0x%x\n" $old_data_zone >> $seqres.full
75 # step 2, delete the 1st $fill_size sized file to trigger reclaim
76 rm $SCRATCH_MNT/$seq.test1
77 $BTRFS_UTIL_PROG filesystem sync $SCRATCH_MNT
78 sleep 2 # 1 transaction commit for 'rm' and 1 for balance
80 # check that we don't have more zones open than before
81 zones_after=$($BLKZONE_PROG report $SCRATCH_DEV | grep -v -e em -e nw | wc -l)
82 echo "After reclaim: $zones_after zones open" >> $seqres.full
84 # Check that old data zone was reset
85 old_wptr=$($BLKZONE_PROG report -o $old_data_zone -c 1 $SCRATCH_DEV |\
86 grep -Eo "wptr 0x[[:xdigit:]]+" | cut -d ' ' -f 2)
87 if [ "$old_wptr" != "0x000000" ]; then
88 _fail "Old wptr still at $old_wptr"
91 new_data_zone=$(get_data_bg)
92 new_data_zone=$((new_data_zone >> 9))
93 printf "New data zone 0x%x\n" $new_data_zone >> $seqres.full
95 # Check that data was really relocated to a different zone
96 if [ $old_data_zone -eq $new_data_zone ]; then
97 echo "New zone same as old zone"
101 echo "Silence is golden"