Rename _scratch_mount to _scratch_cycle_mount
[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" defragment
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         # Return failure if $3 isn't between $1 and $2; let caller decide
57         # action if it's not, we just return status here.
58         if [ "$min" -gt "$ext_cnt" ]; then
59                 echo "Found $ext_cnt extents  min:$max"
60                 return 1;
61         fi
62         if [ "$max" -ne -1 ] && [ "$ext_cnt" -gt "$max" ]; then
63                 echo "Found $ext_cnt max: $max"
64                 return 1;
65         fi
66
67         if [ $max -ne $min ]; then
68             echo "in_range($min, $max)"
69         else
70             echo "$ext_cnt"
71         fi
72         # success
73         return 0
74 }
75
76 # Defrag file, check it, and remove it.
77 _defrag()
78 {
79         min_before=0
80         max_before=-1
81         min_after=0
82         max_after=-1
83         csum=1
84         mtime=1
85
86         while [ $# -gt 1 ]
87         do
88             case $1
89                 in
90                 --min_before)
91                     [ -z "$2" ] && _fail "missing argument for --min_before"
92                     min_before=$2
93                     shift
94                     ;;
95                 --max_before)
96                     [ -z "$2" ] && _fail "missing argument for --max_before"
97                     max_before=$2
98                     shift
99                     ;;
100                 --min_after)
101                     [ -z "$2" ] && _fail "missing argument for --min_after"
102                     min_after=$2
103                     shift
104                     ;;
105                 --max_after)
106                     [ -z "$2" ] && _fail "missing argument for --max_after"
107                     max_after=$2
108                     shift
109                     ;;
110                 --before)
111                     [ -z "$2" ] && _fail "missing argument for --before"
112                     min_before=$2
113                     max_before=$2
114                     shift
115                     ;;
116                 --after)
117                     [ -z "$2" ] && _fail "missing argument for --after"
118                     min_after=$2
119                     max_after=$2
120                     shift
121                     ;;
122                 --no_csum)
123                     csum=
124                     ;;
125                 --no_mtime)
126                     mtime=
127                     ;;
128                 --no_unlink)
129                     no_unlink=1
130                     ;;
131                 *)
132                     _fail "invalid argument to common/dump function: $1"
133                     ;;
134             esac
135             shift
136         done
137
138         echo -n "Before: "
139         ext_before=$(_extent_count $1)
140         _check_extent_count $min_before $max_before $ext_before
141         [ $? -eq 0 ] || _notrun "Could not adequately fragment file"
142
143         [ ! -z $csum ] && CSUM_BEFORE=`md5sum $1`
144         STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
145         $DEFRAG_PROG -v $1 >> $seqres.full 2>&1
146
147         _scratch_cycle_mount
148         STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
149         [ ! -z $csum ] && CSUM_AFTER=`md5sum $1`
150
151         echo -n "After: "
152         ext_after=$(_extent_count $1)
153         _check_extent_count $min_after $max_after $ext_after
154         [ $? -eq 0 ] || _fail "Failed to adequately defragment file"
155
156         [ "$ext_before" -lt "$ext_after" ] && \
157                 _fail "Number of extents increased after defragmentation," \
158                       " before:$ext_before, after:$ext_after"
159         if [ ! -z $csum ] && [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
160                 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
161         fi
162         if [ ! -z $mtime ] && [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
163                 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
164         fi
165         [ -z $no_unlink ] && rm -f $1
166 }
167