f5e36fb25c088fe94e32ec6cdccf8ad8f147f1fd
[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         ;;
32     btrfs)
33         DEFRAG_PROG="$BTRFS_UTIL_PROG filesystem defragment"
34         ;;
35     *)
36         _notrun "defragmentation not supported for fstype \"$FSTYP\""
37         ;;
38     esac
39
40     _require_command $DEFRAG_PROG
41     _require_xfs_io_command "fiemap"
42 }
43
44 _extent_count()
45 {
46         $XFS_IO_PROG -c "fiemap" $1  >> $seqres.full 2>&1
47         $XFS_IO_PROG -c "fiemap" $1 | tail -n +2 | grep -v hole | wc -l| $AWK_PROG '{print $1}'
48 }
49
50 _check_extent_count()
51 {
52         min=$1
53         max=$2
54         ext_cnt=$3
55
56         [ "$min" -gt "$ext_cnt" ] && _fail "Found $ext_cnt extents  min:$max"
57         [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ] && _fail "Found $ext_cnt max: $max"
58
59         if [ $max -ne $min ]; then
60             echo "in_range($min, $max)"
61         else
62             echo "$ext_cnt"
63         fi
64         return $ext_cnt
65 }
66
67 # Defrag file, check it, and remove it.
68 _defrag()
69 {
70         min_before=0
71         max_before=-1
72         min_after=0
73         max_after=-1
74         csum=1
75         mtime=1
76
77         while [ $# -gt 1 ]
78         do
79             case $1
80                 in
81                 --min_before)
82                     [ -z "$2" ] && _fail "missing argument for --min_before"
83                     min_before=$2
84                     shift
85                     ;;
86                 --max_before)
87                     [ -z "$2" ] && _fail "missing argument for --max_before"
88                     max_before=$2
89                     shift
90                     ;;
91                 --min_after)
92                     [ -z "$2" ] && _fail "missing argument for --min_after"
93                     min_after=$2
94                     shift
95                     ;;
96                 --max_after)
97                     [ -z "$2" ] && _fail "missing argument for --max_after"
98                     max_after=$2
99                     shift
100                     ;;
101                 --before)
102                     [ -z "$2" ] && _fail "missing argument for --before"
103                     min_before=$2
104                     max_before=$2
105                     shift
106                     ;;
107                 --after)
108                     [ -z "$2" ] && _fail "missing argument for --after"
109                     min_after=$2
110                     max_after=$2
111                     shift
112                     ;;
113                 --no_csum)
114                     csum=
115                     ;;
116                 --no_mtime)
117                     mtime=
118                     ;;
119                 --no_unlink)
120                     no_unlink=1
121                     ;;
122                 *)
123                     _fail "invalid argument to common/dump function: $1"
124                     ;;
125             esac
126             shift
127         done
128
129         echo -n "Before: "
130         ext_before=$(_extent_count $1)
131         _check_extent_count $min_before $max_before $ext_before
132
133         [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
134         STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
135         $DEFRAG_PROG -v $1 >> $seqres.full 2>&1
136
137         _scratch_remount
138         STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
139         [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
140
141         echo -n "After: "
142         ext_after=$(_extent_count $1)
143         _check_extent_count $min_after $max_after $ext_after
144
145         [ "$ext_before" -lt "$ext_after" ] && \
146                 _fail "Number of extents increased after defragmentation," \
147                       " before:$ext_before, after:$ext_after"
148         if [ ! -z $csum ] && [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
149                 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
150         fi
151         if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
152                 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
153         fi
154         [ -z $no_unlink ] && rm -f $1
155 }
156