defrag: require extents support for ext4 defrag
[xfstests-dev.git] / common / defrag
1 ##/bin/bash
2 #
3 # Copyright (c) 2009 Eric Sandeen
4 # All Rights Reserved.
5 #
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.
9 #
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.
14 #
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
18 #
19 #
20 # Functions useful for defragmentation tests
21 #
22
23 _require_defrag()
24 {
25     case "$FSTYP" in
26     xfs)
27         DEFRAG_PROG="$XFS_FSR_PROG"
28         ;;
29     ext4|ext4dev)
30         DEFRAG_PROG="$E4DEFRAG_PROG"
31         dumpe2fs -h $SCRATCH_DEV 2> /dev/null | grep -wq extent || \
32              _notrun "file system does not have extents, needed for defrag"
33         ;;
34     btrfs)
35         DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
36         ;;
37     *)
38         _notrun "defragmentation not supported for fstype \"$FSTYP\""
39         ;;
40     esac
41
42     _require_command "$DEFRAG_PROG" defragment
43     _require_xfs_io_command "fiemap"
44 }
45
46 _extent_count()
47 {
48         $XFS_IO_PROG -c "fiemap" $1  >> $seqres.full 2>&1
49         $XFS_IO_PROG -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l| $AWK_PROG '{print $1}'
50 }
51
52 _check_extent_count()
53 {
54         min=$1
55         max=$2
56         ext_cnt=$3
57
58         # Return failure if $3 isn't between $1 and $2; let caller decide
59         # action if it's not, we just return status here.
60         if [ "$min" -gt "$ext_cnt" ]; then
61                 echo "Found $ext_cnt extents  min:$max"
62                 return 1;
63         fi
64         if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
65                 echo "Found $ext_cnt max: $max"
66                 return 1;
67         fi
68
69         if [ $max -ne $min ]; then
70             echo "in_range($min, $max)"
71         else
72             echo "$ext_cnt"
73         fi
74         # success
75         return 0
76 }
77
78 # Defrag file, check it, and remove it.
79 _defrag()
80 {
81         min_before=0
82         max_before=-1
83         min_after=0
84         max_after=-1
85         csum=1
86         mtime=1
87
88         while [ $# -gt 1 ]
89         do
90             case $1
91                 in
92                 --min_before)
93                     [ -z "$2" ] && _fail "missing argument for --min_before"
94                     min_before=$2
95                     shift
96                     ;;
97                 --max_before)
98                     [ -z "$2" ] && _fail "missing argument for --max_before"
99                     max_before=$2
100                     shift
101                     ;;
102                 --min_after)
103                     [ -z "$2" ] && _fail "missing argument for --min_after"
104                     min_after=$2
105                     shift
106                     ;;
107                 --max_after)
108                     [ -z "$2" ] && _fail "missing argument for --max_after"
109                     max_after=$2
110                     shift
111                     ;;
112                 --before)
113                     [ -z "$2" ] && _fail "missing argument for --before"
114                     min_before=$2
115                     max_before=$2
116                     shift
117                     ;;
118                 --after)
119                     [ -z "$2" ] && _fail "missing argument for --after"
120                     min_after=$2
121                     max_after=$2
122                     shift
123                     ;;
124                 --no_csum)
125                     csum=
126                     ;;
127                 --no_mtime)
128                     mtime=
129                     ;;
130                 --no_unlink)
131                     no_unlink=1
132                     ;;
133                 *)
134                     _fail "invalid argument to common/dump function: $1"
135                     ;;
136             esac
137             shift
138         done
139
140         echo -n "Before: "
141         ext_before=$(_extent_count $1)
142         _check_extent_count $min_before $max_before $ext_before
143         [ $? -eq 0 ] || _notrun "Could not adequately fragment file"
144
145         [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
146         STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
147         $DEFRAG_PROG -v $1 >> $seqres.full 2>&1
148
149         _scratch_cycle_mount
150         STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
151         [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
152
153         echo -n "After: "
154         ext_after=$(_extent_count $1)
155         _check_extent_count $min_after $max_after $ext_after
156         [ $? -eq 0 ] || _fail "Failed to adequately defragment file"
157
158         [ "$ext_before" -lt "$ext_after" ] && \
159                 _fail "Number of extents increased after defragmentation," \
160                       " before:$ext_before, after:$ext_after"
161         if [ ! -z $csum ] && [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
162                 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
163         fi
164         if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
165                 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
166         fi
167         [ -z $no_unlink ] && rm -f $1
168 }
169