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 _supported_fs ext4 xfs btrfs
22 _require_xfs_io_command "fiemap"
23 if [ "$FSTYP" = "btrfs" ]; then
24 # 3g for btrfs to have distinct bgs
25 _require_fs_space $TEST_DIR 3145728
28 _require_fs_space $TEST_DIR 307200
32 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
33 [ "$FSTYP" = "btrfs" ] && _require_btrfs_command inspect-internal dump-super
34 [ "$FSTYP" = "btrfs" ] && _require_btrfs_command inspect-internal dump-tree
38 $UMOUNT_PROG $loop_dev &> /dev/null
39 _destroy_loop_device $loop_dev
40 if [ $status -eq 0 ]; then
48 # It's not a good idea to be running tools against the image file
49 # backing a live filesystem because the filesystem could be maintaining
50 # in-core state that will perturb the free space map on umount. Stick
51 # to established convention which requires the filesystem to be
52 # unmounted while we probe the underlying file.
53 $UMOUNT_PROG $loop_mnt
54 $XFS_IO_PROG -F -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
55 _mount $loop_dev $loop_mnt
62 $UMOUNT_PROG $loop_mnt
63 $DUMPE2FS_PROG $img_file 2>&1 | grep " Free blocks" | cut -d ":" -f2- | \
64 tr ',' '\n' | $SED_PROG 's/^ //' | \
65 $AWK_PROG -v spb=$sectors_per_block 'BEGIN{FS="-"};
67 if($2 != "") # range of blocks
68 print spb * $1, spb * ($2 + 1) - 1;
69 else # just single block
70 print spb * $1, spb * ($1 + 1) - 1;
74 agsize=`$XFS_INFO_PROG $loop_mnt | $SED_PROG -n 's/.*agsize=\(.*\) blks.*/\1/p'`
75 # Convert free space (agno, block, length) to (start sector, end sector)
76 $UMOUNT_PROG $loop_mnt
77 $XFS_DB_PROG -r -c "freesp -d" $img_file | $SED_PROG '/^.*from/,$d'| \
78 $AWK_PROG -v spb=$sectors_per_block -v agsize=$agsize \
79 '{ print spb * ($1 * agsize + $2), spb * ($1 * agsize + $2 + $3) - 1 }'
82 local device_size=$($BTRFS_UTIL_PROG filesystem show --raw $loop_mnt 2>&1 \
83 | sed -n "s/^.*size \([0-9]*\).*$/\1/p")
85 local nodesize=$($BTRFS_UTIL_PROG inspect-internal dump-super $img_file \
86 | sed -n 's/nodesize\s*\(.*\)/\1/p')
88 # Get holes within block groups
89 $BTRFS_UTIL_PROG inspect-internal dump-tree -t extent $img_file \
90 | $AWK_PROG -v sectorsize=512 -v nodesize=$nodesize -f $here/src/parse-extent-tree.awk
92 # Get holes within unallocated space on disk
93 $BTRFS_UTIL_PROG inspect-internal dump-tree -t dev $img_file \
94 | $AWK_PROG -v sectorsize=512 -v devsize=$device_size -f $here/src/parse-dev-tree.awk
102 # Merges consecutive ranges from two input files
106 tmp_file=$tmp/sectors.tmp
108 cat $file1 $file2 | sort -n > $tmp_file
110 read line < $tmp_file
114 # Continue from second line
115 sed -i "1d" $tmp_file
117 curr_start=${line% *}
120 if [ `expr $end + 1` -ge $curr_start ]; then
121 if [ $curr_end -gt $end ]; then
140 img_file=$TEST_DIR/$$.fs
141 dd if=/dev/zero of=$img_file bs=1M count=$fssize &> /dev/null
143 loop_dev=$(_create_loop_device $img_file)
144 loop_mnt=$tmp/loop_mnt
146 fiemap_ref="$tmp/reference"
147 fiemap_after="$tmp/after"
148 free_sectors="$tmp/free_sectors"
149 merged_sectors="$tmp/merged_free_sectors"
153 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
154 [ "$FSTYP" = "btrfs" ] && MKFS_OPTIONS="$MKFS_OPTIONS -f -dsingle -msingle"
157 _mount $loop_dev $loop_mnt
159 echo -n "Generating garbage on loop..."
160 # Goal is to fill it up, ignore any errors.
161 for i in `seq 1 10`; do
162 mkdir $loop_mnt/$i &> /dev/null
163 cp -r $here/* $loop_mnt/$i &> /dev/null || break
166 # Get reference fiemap, this can contain i.e. uninitialized inode table
168 get_holes $img_file > $fiemap_ref
171 find $loop_mnt -type f -print | $AWK_PROG \
172 'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
175 echo -n "Running fstrim..."
176 $FSTRIM_PROG $loop_mnt &> /dev/null
179 echo -n "Detecting interesting holes in image..."
180 # Get after-trim fiemap
182 get_holes $img_file > $fiemap_after
185 echo -n "Comparing holes to the reported space from FS..."
187 block_size=$(_get_block_size $loop_mnt/)
188 sectors_per_block=`expr $block_size / 512`
190 # Obtain free space from filesystem
191 get_free_sectors > $free_sectors
192 # Merge original holes with free sectors
193 merge_ranges $fiemap_ref $free_sectors > $merged_sectors
195 # Check that all holes after fstrim call were already present before or
196 # that they match free space reported from FS
200 if ! $AWK_PROG -v s=$from -v e=$to \
201 '{ if ($1 <= s && e <= $2) found = 1};
202 END { if(found) exit 0; else exit 1}' $merged_sectors
204 echo "Sectors $from-$to are not marked as free!"
206 # Dump the state to make it easier to debug this...
207 echo free_sectors >> $seqres.full
208 sort -g < $free_sectors >> $seqres.full
209 echo fiemap_ref >> $seqres.full
210 sort -g < $fiemap_ref >> $seqres.full
211 echo merged_sectors >> $seqres.full
212 sort -g < $merged_sectors >> $seqres.full
213 echo fiemap_after >> $seqres.full
214 sort -g < $fiemap_after >> $seqres.full