config: make sure tests check for loop device support
[xfstests-dev.git] / tests / shared / 298
1 #! /bin/bash
2 # FS QA Test No. 298
3 #
4 # Test that filesystem sends discard requests only on free blocks
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2013 Red Hat, Inc., Tomas Racek <tracek@redhat.com>
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #-----------------------------------------------------------------------
22 #
23
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
27
28 status=1        # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
30
31 . ./common/rc
32
33 _supported_fs ext4 xfs
34 _supported_os Linux
35 _require_loop
36 _require_fstrim
37 _require_xfs_io_command "fiemap"
38 _require_fs_space $TEST_DIR 307200
39 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
40
41 _cleanup()
42 {
43         $UMOUNT_PROG $loop_dev &> /dev/null
44         _destroy_loop_device $loop_dev
45         if [ $status -eq 0 ]; then
46                 rm -rf $tmp
47                 rm $img_file
48         fi
49 }
50
51 get_holes()
52 {
53         $XFS_IO_PROG -F -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
54 }
55
56 get_free_sectors()
57 {
58         case $FSTYP in
59         ext4)
60         $DUMPE2FS_PROG $img_file  2>&1 | grep " Free blocks" | cut -d ":" -f2- | \
61                 tr ',' '\n' | $SED_PROG 's/^ //' | \
62                 $AWK_PROG -v spb=$sectors_per_block 'BEGIN{FS="-"};
63                      NF {
64                         if($2 != "") # range of blocks
65                                 print spb * $1, spb * ($2 + 1) - 1;
66                         else            # just single block
67                                 print spb * $1, spb * ($1 + 1) - 1;
68                      }'
69         ;;
70         xfs)
71         agsize=`xfs_info $loop_mnt | $SED_PROG -n 's/.*agsize=\(.*\) blks.*/\1/p'`
72         # Convert free space (agno, block, length) to (start sector, end sector)
73         $UMOUNT_PROG $loop_mnt
74         $XFS_DB_PROG -r -c "freesp -d" $img_file | $SED_PROG '/^.*from/,$d'| \
75                  $AWK_PROG -v spb=$sectors_per_block -v agsize=$agsize \
76                 '{ print spb * ($1 * agsize + $2), spb * ($1 * agsize + $2 + $3) - 1 }'
77         ;;
78         esac
79 }
80
81 merge_ranges()
82 {
83         # Merges consecutive ranges from two input files
84         file1=$1
85         file2=$2
86
87         tmp_file=$tmp/sectors.tmp
88
89         cat $file1 $file2 | sort -n > $tmp_file
90
91         read line < $tmp_file
92         start=${line% *}
93         end=${line#* }
94
95         # Continue from second line
96         sed -i "1d" $tmp_file
97         while read line; do
98                 curr_start=${line% *}
99                 curr_end=${line#* }
100
101                 if [ `expr $end + 1` -ge $curr_start ]; then
102                         if [ $curr_end -gt $end ]; then
103                                 end=$curr_end
104                         fi
105                 else
106                         echo $start $end
107                         start=$curr_start
108                         end=$curr_end
109                 fi
110         done < $tmp_file
111
112         # Print last line
113         echo $start $end
114
115         rm $tmp_file
116 }
117
118 here=`pwd`
119 tmp=`mktemp -d`
120
121 img_file=$TEST_DIR/$$.fs
122 dd if=/dev/zero of=$img_file bs=1M count=300 &> /dev/null
123
124 loop_dev=$(_create_loop_device $img_file)
125 loop_mnt=$tmp/loop_mnt
126
127 fiemap_ref="$tmp/reference"
128 fiemap_after="$tmp/after"
129 free_sectors="$tmp/free_sectors"
130 merged_sectors="$tmp/merged_free_sectors"
131
132 mkdir $loop_mnt
133
134 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
135
136 $MKFS_PROG -t $FSTYP $MKFS_OPTIONS $loop_dev &> /dev/null
137 $MOUNT_PROG $loop_dev $loop_mnt
138
139 echo -n "Generating garbage on loop..."
140 # Goal is to fill it up, ignore any errors.
141 for i in `seq 1 10`; do
142         mkdir $loop_mnt/$i &> /dev/null
143         cp -r $here/* $loop_mnt/$i &> /dev/null || break
144 done
145
146 # Get reference fiemap, this can contain i.e. uninitialized inode table
147 sync
148 get_holes $img_file > $fiemap_ref
149
150 # Delete some files
151 find $loop_mnt -type f -print | $AWK_PROG \
152         'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
153 echo "done."
154
155 echo -n "Running fstrim..."
156 $FSTRIM_PROG $loop_mnt &> /dev/null
157 echo "done."
158
159 echo -n "Detecting interesting holes in image..."
160 # Get after-trim fiemap
161 sync
162 get_holes $img_file > $fiemap_after
163 echo "done."
164
165 echo -n "Comparing holes to the reported space from FS..."
166 # Get block size
167 block_size=$(stat -f -c "%S" $loop_mnt/)
168 sectors_per_block=`expr $block_size / 512`
169
170 # Obtain free space from filesystem
171 get_free_sectors > $free_sectors
172 # Merge original holes with free sectors
173 merge_ranges $fiemap_ref $free_sectors > $merged_sectors
174
175 # Check that all holes after fstrim call were already present before or
176 # that they match free space reported from FS
177 while read line; do
178         from=${line% *}
179         to=${line#* }
180         if ! $AWK_PROG -v s=$from -v e=$to \
181                 '{ if ($1 <= s && e <= $2) found = 1};
182                 END { if(found) exit 0; else exit 1}' $merged_sectors
183         then
184                 echo "Sectors $from-$to are not marked as free!"
185                 exit
186         fi
187 done < $fiemap_after
188 echo "done."
189
190 status=0
191 exit