4 # Test that filesystem sends discard requests only on free blocks
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2013 Red Hat, Inc., Tomas Racek <tracek@redhat.com>
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.
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.
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 #-----------------------------------------------------------------------
24 owner=tracek@redhat.com
27 echo "QA output created by $seq"
29 status=1 # failure is the default!
30 trap "_cleanup; exit \$status" 0 1 2 3 15
35 _supported_fs ext4 xfs
38 _require_fs_space $TEST_DIR 307200
39 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
43 $UMOUNT_PROG $loop_dev &> /dev/null
44 _destroy_loop_device $loop_dev
45 if [ $status -eq 0 ]; then
53 $XFS_IO_PROG -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
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="-"};
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;
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 -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 }'
83 # Merges consecutive ranges from two input files
87 tmp_file=$tmp/sectors.tmp
89 cat $file1 $file2 | sort -n > $tmp_file
95 # Continue from second line
101 if [ `expr $end + 1` -ge $curr_start ]; then
102 if [ $curr_end -gt $end ]; then
121 img_file=$TEST_DIR/$$.fs
122 dd if=/dev/zero of=$img_file bs=1M count=300 &> /dev/null
124 loop_dev=$(_create_loop_device $img_file)
125 loop_mnt=$tmp/loop_mnt
127 fiemap_ref="$tmp/reference"
128 fiemap_after="$tmp/after"
129 free_sectors="$tmp/free_sectors"
130 merged_sectors="$tmp/merged_free_sectors"
134 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
136 $MKFS_PROG -t $FSTYP $MKFS_OPTIONS $loop_dev &> /dev/null
137 $MOUNT_PROG $loop_dev $loop_mnt
139 echo -n "Generating garbage on loop..."
140 for i in `seq 1 10`; do
142 cp -r $here/* $loop_mnt/$i
145 # Get reference fiemap, this can contain i.e. uninitialized inode table
147 get_holes $img_file > $fiemap_ref
150 find $loop_mnt -type f -print | $AWK_PROG \
151 'BEGIN {srand()}; {if(rand() > 0.7) print $1;}' | xargs rm
154 echo -n "Running fstrim..."
155 $FSTRIM_PROG $loop_mnt &> /dev/null
158 echo -n "Detecting interesting holes in image..."
159 # Get after-trim fiemap
161 get_holes $img_file > $fiemap_after
164 echo -n "Comparing holes to the reported space from FS..."
166 block_size=$(stat -f -c "%S" $loop_mnt/)
167 sectors_per_block=`expr $block_size / 512`
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
174 # Check that all holes after fstrim call were already present before or
175 # that they match free space reported from FS
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
183 echo "Sectors $from-$to are not marked as free!"