2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2009 Eric Sandeen. All Rights Reserved.
5 # Functions useful for defragmentation tests
11 # xfs_fsr does preallocates, require "falloc"
12 _require_xfs_io_command "falloc"
13 DEFRAG_PROG="$XFS_FSR_PROG"
16 testfile="$TEST_DIR/$$-test.defrag"
17 donorfile="$TEST_DIR/$$-donor.defrag"
18 bsize=`_get_block_size $TEST_DIR`
19 $XFS_IO_PROG -f -c "pwrite -b $bsize 0 $bsize" $testfile > /dev/null
20 cp $testfile $donorfile
21 echo $testfile | $here/src/e4compact -v -f $donorfile | \
24 rm -f $testfile $donorfile 2>&1 > /dev/null
25 _notrun "$FSTYP test filesystem doesn't support online defrag"
27 rm -f $testfile $donorfile 2>&1 > /dev/null
28 DEFRAG_PROG="$E4DEFRAG_PROG"
31 DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
34 _notrun "defragmentation not supported for fstype \"$FSTYP\""
38 _require_command "$DEFRAG_PROG" defragment
39 _require_xfs_io_command "fiemap"
44 $XFS_IO_PROG -c "fiemap" $1 >> $seqres.full 2>&1
45 $XFS_IO_PROG -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l| $AWK_PROG '{print $1}'
54 # Return failure if $3 isn't between $1 and $2; let caller decide
55 # action if it's not, we just return status here.
56 if [ "$min" -gt "$ext_cnt" ]; then
57 echo "Found $ext_cnt extents min:$max"
60 if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
61 echo "Found $ext_cnt max: $max"
65 if [ $max -ne $min ]; then
66 echo "in_range($min, $max)"
74 # Defrag file, check it, and remove it.
89 [ -z "$2" ] && _fail "missing argument for --min_before"
94 [ -z "$2" ] && _fail "missing argument for --max_before"
99 [ -z "$2" ] && _fail "missing argument for --min_after"
104 [ -z "$2" ] && _fail "missing argument for --max_after"
109 [ -z "$2" ] && _fail "missing argument for --before"
115 [ -z "$2" ] && _fail "missing argument for --after"
130 _fail "invalid argument to common/dump function: $1"
137 ext_before=$(_extent_count $1)
138 _check_extent_count $min_before $max_before $ext_before
139 [ $? -eq 0 ] || _notrun "Could not adequately fragment file"
141 [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
142 STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
143 $DEFRAG_PROG -v $1 >> $seqres.full 2>&1
146 STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
147 [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
150 ext_after=$(_extent_count $1)
151 _check_extent_count $min_after $max_after $ext_after
152 [ $? -eq 0 ] || _fail "Failed to adequately defragment file"
154 [ "$ext_before" -lt "$ext_after" ] && \
155 _fail "Number of extents increased after defragmentation," \
156 " before:$ext_before, after:$ext_after"
157 if [ ! -z $csum ] && [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
158 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
160 if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
161 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
163 [ -z $no_unlink ] && rm -f $1