common: check if a given rename flag is supported in _require_renameat2
[xfstests-dev.git] / common / defrag
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2009 Eric Sandeen. All Rights Reserved.
4 #
5 # Functions useful for defragmentation tests
6
7 _require_defrag()
8 {
9     case "$FSTYP" in
10     xfs)
11         # xfs_fsr does preallocates, require "falloc"
12         _require_xfs_io_command "falloc"
13         DEFRAG_PROG="$XFS_FSR_PROG"
14         ;;
15     ext4|ext4dev)
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 | \
22                 grep -q "err:95"
23         if [ $? -eq 0 ]; then
24                 rm -f $testfile $donorfile 2>&1 > /dev/null
25                 _notrun "$FSTYP test filesystem doesn't support online defrag"
26         fi
27         rm -f $testfile $donorfile 2>&1 > /dev/null
28         DEFRAG_PROG="$E4DEFRAG_PROG"
29         ;;
30     btrfs)
31         DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
32         ;;
33     *)
34         _notrun "defragmentation not supported for fstype \"$FSTYP\""
35         ;;
36     esac
37
38     _require_command "$DEFRAG_PROG" defragment
39     _require_xfs_io_command "fiemap"
40 }
41
42 _extent_count()
43 {
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}'
46 }
47
48 _check_extent_count()
49 {
50         min=$1
51         max=$2
52         ext_cnt=$3
53
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"
58                 return 1;
59         fi
60         if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
61                 echo "Found $ext_cnt max: $max"
62                 return 1;
63         fi
64
65         if [ $max -ne $min ]; then
66             echo "in_range($min, $max)"
67         else
68             echo "$ext_cnt"
69         fi
70         # success
71         return 0
72 }
73
74 # Defrag file, check it, and remove it.
75 _defrag()
76 {
77         min_before=0
78         max_before=-1
79         min_after=0
80         max_after=-1
81         csum=1
82         mtime=1
83
84         while [ $# -gt 1 ]
85         do
86             case $1
87                 in
88                 --min_before)
89                     [ -z "$2" ] && _fail "missing argument for --min_before"
90                     min_before=$2
91                     shift
92                     ;;
93                 --max_before)
94                     [ -z "$2" ] && _fail "missing argument for --max_before"
95                     max_before=$2
96                     shift
97                     ;;
98                 --min_after)
99                     [ -z "$2" ] && _fail "missing argument for --min_after"
100                     min_after=$2
101                     shift
102                     ;;
103                 --max_after)
104                     [ -z "$2" ] && _fail "missing argument for --max_after"
105                     max_after=$2
106                     shift
107                     ;;
108                 --before)
109                     [ -z "$2" ] && _fail "missing argument for --before"
110                     min_before=$2
111                     max_before=$2
112                     shift
113                     ;;
114                 --after)
115                     [ -z "$2" ] && _fail "missing argument for --after"
116                     min_after=$2
117                     max_after=$2
118                     shift
119                     ;;
120                 --no_csum)
121                     csum=
122                     ;;
123                 --no_mtime)
124                     mtime=
125                     ;;
126                 --no_unlink)
127                     no_unlink=1
128                     ;;
129                 *)
130                     _fail "invalid argument to common/dump function: $1"
131                     ;;
132             esac
133             shift
134         done
135
136         echo -n "Before: "
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"
140
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
144
145         _scratch_cycle_mount
146         STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
147         [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
148
149         echo -n "After: "
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"
153
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)"
159         fi
160         if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
161                 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
162         fi
163         [ -z $no_unlink ] && rm -f $1
164 }
165