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="$F2FS_IO_PROG defrag_file"
34 DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
37 _notrun "defragmentation not supported for fstype \"$FSTYP\""
41 _require_command "$DEFRAG_PROG" defragment
42 _require_xfs_io_command "fiemap"
47 $XFS_IO_PROG -c "fiemap" $1 >> $seqres.full 2>&1
48 $XFS_IO_PROG -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l| $AWK_PROG '{print $1}'
57 # Return failure if $3 isn't between $1 and $2; let caller decide
58 # action if it's not, we just return status here.
59 if [ "$min" -gt "$ext_cnt" ]; then
60 echo "Found $ext_cnt extents min:$max"
63 if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
64 echo "Found $ext_cnt max: $max"
68 if [ $max -ne $min ]; then
69 echo "in_range($min, $max)"
77 # Defrag file, check it, and remove it.
92 [ -z "$2" ] && _fail "missing argument for --min_before"
97 [ -z "$2" ] && _fail "missing argument for --max_before"
102 [ -z "$2" ] && _fail "missing argument for --min_after"
107 [ -z "$2" ] && _fail "missing argument for --max_after"
112 [ -z "$2" ] && _fail "missing argument for --before"
118 [ -z "$2" ] && _fail "missing argument for --after"
133 _fail "invalid argument to common/dump function: $1"
140 ext_before=$(_extent_count $1)
141 _check_extent_count $min_before $max_before $ext_before
142 [ $? -eq 0 ] || _notrun "Could not adequately fragment file"
144 [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
145 STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
147 if [ $FSTYP == "f2fs" ]; then
148 local filesize=`ls -l $1 | $AWK_PROG '{print $5}'`
149 $DEFRAG_PROG 0 $filesize $1 >> $seqres.full 2>&1
151 $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