3 # Copyright (c) 2009 Eric Sandeen
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write the Free Software Foundation,
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # Functions useful for defragmentation tests
27 DEFRAG_PROG="$XFS_FSR_PROG"
30 echo $MOUNT_OPTIONS | grep -q dax
32 _notrun "$FSTYP defragmentation not supported with DAX"
34 DEFRAG_PROG="$E4DEFRAG_PROG"
36 dumpe2fs -h $TEST_DEV 2> /dev/null | grep -wq extent || \
37 _notrun "file system does not have extents, needed for defrag"
40 DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
43 _notrun "defragmentation not supported for fstype \"$FSTYP\""
47 _require_command "$DEFRAG_PROG" defragment
48 _require_xfs_io_command "fiemap"
53 $XFS_IO_PROG -c "fiemap" $1 >> $seqres.full 2>&1
54 $XFS_IO_PROG -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l| $AWK_PROG '{print $1}'
63 # Return failure if $3 isn't between $1 and $2; let caller decide
64 # action if it's not, we just return status here.
65 if [ "$min" -gt "$ext_cnt" ]; then
66 echo "Found $ext_cnt extents min:$max"
69 if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
70 echo "Found $ext_cnt max: $max"
74 if [ $max -ne $min ]; then
75 echo "in_range($min, $max)"
83 # Defrag file, check it, and remove it.
98 [ -z "$2" ] && _fail "missing argument for --min_before"
103 [ -z "$2" ] && _fail "missing argument for --max_before"
108 [ -z "$2" ] && _fail "missing argument for --min_after"
113 [ -z "$2" ] && _fail "missing argument for --max_after"
118 [ -z "$2" ] && _fail "missing argument for --before"
124 [ -z "$2" ] && _fail "missing argument for --after"
139 _fail "invalid argument to common/dump function: $1"
146 ext_before=$(_extent_count $1)
147 _check_extent_count $min_before $max_before $ext_before
148 [ $? -eq 0 ] || _notrun "Could not adequately fragment file"
150 [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
151 STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
152 $DEFRAG_PROG -v $1 >> $seqres.full 2>&1
155 STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
156 [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
159 ext_after=$(_extent_count $1)
160 _check_extent_count $min_after $max_after $ext_after
161 [ $? -eq 0 ] || _fail "Failed to adequately defragment file"
163 [ "$ext_before" -lt "$ext_after" ] && \
164 _fail "Number of extents increased after defragmentation," \
165 " before:$ext_before, after:$ext_after"
166 if [ ! -z $csum ] && [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
167 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
169 if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
170 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
172 [ -z $no_unlink ] && rm -f $1