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