xfstests shared/298: various fixes
[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_fstrim
36 _require_xfs_io_fiemap
37 _require_fs_space $TEST_DIR 307200
38 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
39
40 _cleanup()
41 {
42         $UMOUNT_PROG $loop_dev &> /dev/null
43         _destroy_loop_device $loop_dev
44         if [ $status -eq 0 ]; then
45                 rm -rf $tmp
46                 rm $img_file
47         fi
48 }
49
50 get_holes()
51 {
52         $XFS_IO_PROG -F -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
53 }
54
55 get_free_sectors()
56 {
57         case $FSTYP in
58         ext4)
59         $DUMPE2FS_PROG $img_file  2>&1 | grep " Free blocks" | cut -d ":" -f2- | \
60                 tr ',' '\n' | $SED_PROG 's/^ //' | \
61                 $AWK_PROG -v spb=$sectors_per_block 'BEGIN{FS="-"};
62                      NF {
63                         if($2 != "") # range of blocks
64                                 print spb * $1, spb * ($2 + 1) - 1;
65                         else            # just single block
66                                 print spb * $1, spb * ($1 + 1) - 1;
67                      }'
68         ;;
69         xfs)
70         agsize=`xfs_info $loop_mnt | $SED_PROG -n 's/.*agsize=\(.*\) blks.*/\1/p'`
71         # Convert free space (agno, block, length) to (start sector, end sector)
72         $UMOUNT_PROG $loop_mnt
73         $XFS_DB_PROG -c "freesp -d" $img_file | $SED_PROG '/^.*from/,$d'| \
74                  $AWK_PROG -v spb=$sectors_per_block -v agsize=$agsize \
75                 '{ print spb * ($1 * agsize + $2), spb * ($1 * agsize + $2 + $3) - 1 }'
76         ;;
77         esac
78 }
79
80 merge_ranges()
81 {
82         # Merges consecutive ranges from two input files
83         file1=$1
84         file2=$2
85
86         tmp_file=$tmp/sectors.tmp
87
88         cat $file1 $file2 | sort -n > $tmp_file
89
90         read line < $tmp_file
91         start=${line% *}
92         end=${line#* }
93
94         # Continue from second line
95         sed -i "1d" $tmp_file
96         while read line; do
97                 curr_start=${line% *}
98                 curr_end=${line#* }
99
100                 if [ `expr $end + 1` -ge $curr_start ]; then
101                         if [ $curr_end -gt $end ]; then
102                                 end=$curr_end
103                         fi
104                 else
105                         echo $start $end
106                         start=$curr_start
107                         end=$curr_end
108                 fi
109         done < $tmp_file
110
111         # Print last line
112         echo $start $end
113
114         rm $tmp_file
115 }
116
117 here=`pwd`
118 tmp=`mktemp -d`
119
120 img_file=$TEST_DIR/$$.fs
121 dd if=/dev/zero of=$img_file bs=1M count=300 &> /dev/null
122
123 loop_dev=$(_create_loop_device $img_file)
124 loop_mnt=$tmp/loop_mnt
125
126 fiemap_ref="$tmp/reference"
127 fiemap_after="$tmp/after"
128 free_sectors="$tmp/free_sectors"
129 merged_sectors="$tmp/merged_free_sectors"
130
131 mkdir $loop_mnt
132
133 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
134
135 $MKFS_PROG -t $FSTYP $MKFS_OPTIONS $loop_dev &> /dev/null
136 $MOUNT_PROG $loop_dev $loop_mnt
137
138 echo -n "Generating garbage on loop..."
139 # Goal is to fill it up, ignore any errors.
140 for i in `seq 1 10`; do
141         mkdir $loop_mnt/$i &> /dev/null
142         cp -r $here/* $loop_mnt/$i &> /dev/null || break
143 done
144
145 # Get reference fiemap, this can contain i.e. uninitialized inode table
146 sync
147 get_holes $img_file > $fiemap_ref
148
149 # Delete some files
150 find $loop_mnt -type f -print | $AWK_PROG \
151         'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
152 echo "done."
153
154 echo -n "Running fstrim..."
155 $FSTRIM_PROG $loop_mnt &> /dev/null
156 echo "done."
157
158 echo -n "Detecting interesting holes in image..."
159 # Get after-trim fiemap
160 sync
161 get_holes $img_file > $fiemap_after
162 echo "done."
163
164 echo -n "Comparing holes to the reported space from FS..."
165 # Get block size
166 block_size=$(stat -f -c "%S" $loop_mnt/)
167 sectors_per_block=`expr $block_size / 512`
168
169 # Obtain free space from filesystem
170 get_free_sectors > $free_sectors
171 # Merge original holes with free sectors
172 merge_ranges $fiemap_ref $free_sectors > $merged_sectors
173
174 # Check that all holes after fstrim call were already present before or
175 # that they match free space reported from FS
176 while read line; do
177         from=${line% *}
178         to=${line#* }
179         if ! $AWK_PROG -v s=$from -v e=$to \
180                 '{ if ($1 <= s && e <= $2) found = 1};
181                 END { if(found) exit 0; else exit 1}' $merged_sectors
182         then
183                 echo "Sectors $from-$to are not marked as free!"
184                 exit
185         fi
186 done < $fiemap_after
187 echo "done."
188
189 status=0
190 exit