416a89d347788a3c059288c430026b49d5d28535
[xfstests-dev.git] / tests / shared / 298
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Red Hat, Inc., Tomas Racek <tracek@redhat.com>
4 #
5 # FS QA Test No. 298
6 #
7 # Test that filesystem sends discard requests only on free blocks
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 status=1        # failure is the default!
14 trap "_cleanup; exit \$status" 0 1 2 3 15
15
16 . ./common/rc
17
18 _supported_fs ext4 xfs btrfs
19 _require_test
20 _require_loop
21 _require_fstrim
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
26         fssize=3000
27 else
28         _require_fs_space $TEST_DIR 307200
29         fssize=300
30 fi
31
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
35
36 _cleanup()
37 {
38         $UMOUNT_PROG $loop_dev &> /dev/null
39         _destroy_loop_device $loop_dev
40         if [ $status -eq 0 ]; then
41                 rm -rf $tmp
42                 rm $img_file
43         fi
44 }
45
46 get_holes()
47 {
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
56 }
57
58 get_free_sectors()
59 {
60         case $FSTYP in
61         ext4)
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="-"};
66                      NF {
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;
71                      }'
72         ;;
73         xfs)
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 }'
80         ;;
81         btrfs)
82                 local device_size=$($BTRFS_UTIL_PROG filesystem show --raw $loop_mnt 2>&1 \
83                         | sed -n "s/^.*size \([0-9]*\).*$/\1/p")
84
85                 local nodesize=$($BTRFS_UTIL_PROG inspect-internal dump-super $img_file  \
86                         | sed -n 's/nodesize\s*\(.*\)/\1/p')
87
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
91
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
95
96         ;;
97         esac
98 }
99
100 merge_ranges()
101 {
102         # Merges consecutive ranges from two input files
103         file1=$1
104         file2=$2
105
106         tmp_file=$tmp/sectors.tmp
107
108         cat $file1 $file2 | sort -n > $tmp_file
109
110         read line < $tmp_file
111         start=${line% *}
112         end=${line#* }
113
114         # Continue from second line
115         sed -i "1d" $tmp_file
116         while read line; do
117                 curr_start=${line% *}
118                 curr_end=${line#* }
119
120                 if [ `expr $end + 1` -ge $curr_start ]; then
121                         if [ $curr_end -gt $end ]; then
122                                 end=$curr_end
123                         fi
124                 else
125                         echo $start $end
126                         start=$curr_start
127                         end=$curr_end
128                 fi
129         done < $tmp_file
130
131         # Print last line
132         echo $start $end
133
134         rm $tmp_file
135 }
136
137 here=`pwd`
138 tmp=`mktemp -d`
139
140 img_file=$TEST_DIR/$$.fs
141 dd if=/dev/zero of=$img_file bs=1M count=$fssize &> /dev/null
142
143 loop_dev=$(_create_loop_device $img_file)
144 loop_mnt=$tmp/loop_mnt
145
146 fiemap_ref="$tmp/reference"
147 fiemap_after="$tmp/after"
148 free_sectors="$tmp/free_sectors"
149 merged_sectors="$tmp/merged_free_sectors"
150
151 mkdir $loop_mnt
152
153 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
154 [ "$FSTYP" = "btrfs" ] && MKFS_OPTIONS="$MKFS_OPTIONS -f -dsingle -msingle"
155
156 _mkfs_dev $loop_dev
157 _mount $loop_dev $loop_mnt
158
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
164 done
165
166 # Get reference fiemap, this can contain i.e. uninitialized inode table
167 sync
168 get_holes $img_file > $fiemap_ref
169
170 # Delete some files
171 find $loop_mnt -type f -print | $AWK_PROG \
172         'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
173 echo "done."
174
175 echo -n "Running fstrim..."
176 $FSTRIM_PROG $loop_mnt &> /dev/null
177 echo "done."
178
179 echo -n "Detecting interesting holes in image..."
180 # Get after-trim fiemap
181 sync
182 get_holes $img_file > $fiemap_after
183 echo "done."
184
185 echo -n "Comparing holes to the reported space from FS..."
186 # Get block size
187 block_size=$(_get_block_size $loop_mnt/)
188 sectors_per_block=`expr $block_size / 512`
189
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
194
195 # Check that all holes after fstrim call were already present before or
196 # that they match free space reported from FS
197 while read line; do
198         from=${line% *}
199         to=${line#* }
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
203         then
204                 echo "Sectors $from-$to are not marked as free!"
205
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
215                 exit
216         fi
217 done < $fiemap_after
218 echo "done."
219
220 status=0
221 exit