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 #-----------------------------------------------------------------------
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
28 status=1 # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
33 _supported_fs ext4 xfs
36 _require_xfs_io_fiemap
37 _require_fs_space $TEST_DIR 307200
38 [ "$FSTYP" = "ext4" ] && _require_dumpe2fs
42 $UMOUNT_PROG $loop_dev &> /dev/null
43 _destroy_loop_device $loop_dev
44 if [ $status -eq 0 ]; then
52 $XFS_IO_PROG -F -c fiemap $1 | grep hole | $SED_PROG 's/.*\[\(.*\)\.\.\(.*\)\].*/\1 \2/'
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="-"};
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;
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 }'
82 # Merges consecutive ranges from two input files
86 tmp_file=$tmp/sectors.tmp
88 cat $file1 $file2 | sort -n > $tmp_file
94 # Continue from second line
100 if [ `expr $end + 1` -ge $curr_start ]; then
101 if [ $curr_end -gt $end ]; then
120 img_file=$TEST_DIR/$$.fs
121 dd if=/dev/zero of=$img_file bs=1M count=300 &> /dev/null
123 loop_dev=$(_create_loop_device $img_file)
124 loop_mnt=$tmp/loop_mnt
126 fiemap_ref="$tmp/reference"
127 fiemap_after="$tmp/after"
128 free_sectors="$tmp/free_sectors"
129 merged_sectors="$tmp/merged_free_sectors"
133 [ "$FSTYP" = "xfs" ] && MKFS_OPTIONS="-f $MKFS_OPTIONS"
135 $MKFS_PROG -t $FSTYP $MKFS_OPTIONS $loop_dev &> /dev/null
136 $MOUNT_PROG $loop_dev $loop_mnt
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
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!"