2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Red Hat, Inc., Tomas Racek <tracek@redhat.com>
7 # Test that filesystem sends discard requests only on free blocks
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
13 status=1 # failure is the default!
14 trap "_cleanup; exit \$status" 0 1 2 3 15
18 # remove previous $seqres.full before test
21 _supported_fs ext4 xfs btrfs
25 _require_xfs_io_command "fiemap"
26 if [ "$FSTYP" = "btrfs" ]; then
27 # 3g for btrfs to have distinct bgs
28 _require_fs_space $TEST_DIR 3145728
31 _require_fs_space $TEST_DIR 307200
35 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
36 [ "$FSTYP" = "btrfs" ] && _require_btrfs_command inspect-internal dump-super
37 [ "$FSTYP" = "btrfs" ] && _require_btrfs_command inspect-internal dump-tree
41 $UMOUNT_PROG $loop_dev &> /dev/null
42 _destroy_loop_device $loop_dev
43 if [ $status -eq 0 ]; then
51 # It's not a good idea to be running tools against the image file
52 # backing a live filesystem because the filesystem could be maintaining
53 # in-core state that will perturb the free space map on umount. Stick
54 # to established convention which requires the filesystem to be
55 # unmounted while we probe the underlying file.
56 $UMOUNT_PROG $loop_mnt
57 $XFS_IO_PROG -F -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
58 _mount $loop_dev $loop_mnt
65 $UMOUNT_PROG $loop_mnt
66 $DUMPE2FS_PROG $img_file 2>&1 | grep " Free blocks" | cut -d ":" -f2- | \
67 tr ',' '\n' | $SED_PROG 's/^ //' | \
68 $AWK_PROG -v spb=$sectors_per_block 'BEGIN{FS="-"};
70 if($2 != "") # range of blocks
71 print spb * $1, spb * ($2 + 1) - 1;
72 else # just single block
73 print spb * $1, spb * ($1 + 1) - 1;
77 agsize=`$XFS_INFO_PROG $loop_mnt | $SED_PROG -n 's/.*agsize=\(.*\) blks.*/\1/p'`
78 # Convert free space (agno, block, length) to (start sector, end sector)
79 $UMOUNT_PROG $loop_mnt
80 $XFS_DB_PROG -r -c "freesp -d" $img_file | $SED_PROG '/^.*from/,$d'| \
81 $AWK_PROG -v spb=$sectors_per_block -v agsize=$agsize \
82 '{ print spb * ($1 * agsize + $2), spb * ($1 * agsize + $2 + $3) - 1 }'
85 local device_size=$($BTRFS_UTIL_PROG filesystem show --raw $loop_mnt 2>&1 \
86 | sed -n "s/^.*size \([0-9]*\).*$/\1/p")
88 local nodesize=$($BTRFS_UTIL_PROG inspect-internal dump-super $img_file \
89 | sed -n 's/nodesize\s*\(.*\)/\1/p')
91 # Get holes within block groups
92 $BTRFS_UTIL_PROG inspect-internal dump-tree -t extent $img_file \
93 | $AWK_PROG -v sectorsize=512 -v nodesize=$nodesize -f $here/src/parse-extent-tree.awk
95 # Get holes within unallocated space on disk
96 $BTRFS_UTIL_PROG inspect-internal dump-tree -t dev $img_file \
97 | $AWK_PROG -v sectorsize=512 -v devsize=$device_size -f $here/src/parse-dev-tree.awk
105 # Merges consecutive ranges from two input files
109 tmp_file=$tmp/sectors.tmp
111 cat $file1 $file2 | sort -n > $tmp_file
113 read line < $tmp_file
117 # Continue from second line
118 sed -i "1d" $tmp_file
120 curr_start=${line% *}
123 if [ `expr $end + 1` -ge $curr_start ]; then
124 if [ $curr_end -gt $end ]; then
143 img_file=$TEST_DIR/$$.fs
144 dd if=/dev/zero of=$img_file bs=1M count=$fssize &> /dev/null
146 loop_dev=$(_create_loop_device $img_file)
147 loop_mnt=$tmp/loop_mnt
149 fiemap_ref="$tmp/reference"
150 fiemap_after="$tmp/after"
151 free_sectors="$tmp/free_sectors"
152 merged_sectors="$tmp/merged_free_sectors"
156 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
157 [ "$FSTYP" = "btrfs" ] && MKFS_OPTIONS="$MKFS_OPTIONS -f -dsingle -msingle"
160 _mount $loop_dev $loop_mnt
162 echo -n "Generating garbage on loop..."
163 # Goal is to fill it up, ignore any errors.
164 for i in `seq 1 10`; do
165 mkdir $loop_mnt/$i &> /dev/null
166 cp -r $here/* $loop_mnt/$i &> /dev/null || break
169 # Get reference fiemap, this can contain i.e. uninitialized inode table
171 get_holes $img_file > $fiemap_ref
174 find $loop_mnt -type f -print | $AWK_PROG \
175 'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
178 echo -n "Running fstrim..."
179 $FSTRIM_PROG $loop_mnt &> /dev/null
182 echo -n "Detecting interesting holes in image..."
183 # Get after-trim fiemap
185 get_holes $img_file > $fiemap_after
188 echo -n "Comparing holes to the reported space from FS..."
190 block_size=$(_get_block_size $loop_mnt/)
191 sectors_per_block=`expr $block_size / 512`
193 # Obtain free space from filesystem
194 get_free_sectors > $free_sectors
195 # Merge original holes with free sectors
196 merge_ranges $fiemap_ref $free_sectors > $merged_sectors
198 # Check that all holes after fstrim call were already present before or
199 # that they match free space reported from FS
203 if ! $AWK_PROG -v s=$from -v e=$to \
204 '{ if ($1 <= s && e <= $2) found = 1};
205 END { if(found) exit 0; else exit 1}' $merged_sectors
207 echo "Sectors $from-$to are not marked as free!"
209 # Dump the state to make it easier to debug this...
210 echo free_sectors >> $seqres.full
211 sort -g < $free_sectors >> $seqres.full
212 echo fiemap_ref >> $seqres.full
213 sort -g < $fiemap_ref >> $seqres.full
214 echo merged_sectors >> $seqres.full
215 sort -g < $merged_sectors >> $seqres.full
216 echo fiemap_after >> $seqres.full
217 sort -g < $fiemap_after >> $seqres.full