7f4a630d488a39b4ed340c34003c9fcd63418d43
[xfstests-dev.git] / 284
1 #! /bin/bash
2 # FS QA Test No. 284
3 #
4 # Btrfs Online defragmentation tests
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2012 Fujitsu Liu Bo.  All Rights Reserved.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #-----------------------------------------------------------------------
22 #
23 # creator
24 owner=liubo2009@cn.fujitsu.com
25
26 seq=`basename $0`
27 echo "QA output created by $seq"
28 here="`pwd`"
29 tmp=/tmp/$$
30 cnt=11999
31 filesize=48000
32
33 status=1        # failure is the default!
34 trap "_cleanup; exit \$status" 0 1 2 3 15
35
36 _cleanup()
37 {
38     cd /
39     rm -f $tmp.*
40 }
41
42 _create_file()
43 {
44         if [ $1 -ne 2 ]; then
45                 tmpfile="$SCRATCH_MNT/tmp_file"
46         else
47                 mkdir -p $SCRATCH_MNT/tmp_dir
48                 tmpfile="$SCRATCH_MNT/tmp_dir/tmp_file"
49         fi
50                 
51         for i in `seq $cnt -1 0`; do
52                 dd if=/dev/zero of=$tmpfile bs=4k count=1 \
53                  conv=notrunc seek=$i oflag=sync &>/dev/null
54         done
55         # get md5sum
56         md5sum $tmpfile > /tmp/checksum
57 }
58
59 _btrfs_online_defrag()
60 {
61         str=""
62         if [ "$2" = "2" ];then
63                 str="$str -s -1 -l $((filesize / 2)) "
64         elif [ "$2" = "3" ];then
65                 str="$str -s $((filesize + 1)) -l $((filesize / 2)) "
66                 HAVE_DEFRAG=1
67         elif [ "$2" = "4" ];then
68                 str="$str -l -1 "
69         elif [ "$2" = "5" ];then
70                 str="$str -l $((filesize + 1)) "
71         elif [ "$2" = "6" ];then
72                 str="$str -l $((filesize / 2)) "
73         fi
74
75         if [ "$3" = "2" ];then
76                 str="$str -c "
77         fi
78
79         if [ "$str" != "" ]; then
80                 btrfs filesystem defragment $str $SCRATCH_MNT/tmp_file
81         else
82                 if [ "$1" = "1" ];then
83                         btrfs filesystem defragment $SCRATCH_MNT/tmp_file
84                 elif [ "$1" = "2" ];then
85                         btrfs filesystem defragment $SCRATCH_MNT/tmp_dir
86                 elif [ "$1" = "3" ];then
87                         btrfs filesystem defragment $SCRATCH_MNT
88                 fi
89         fi
90         ret_val=$?
91         _scratch_remount
92         if [ $ret_val -ne 20 ];then
93                 echo "btrfs filesystem defragment failed! err is $ret_val"
94         fi
95 }
96
97 _checksum()
98 {
99         md5sum -c /tmp/checksum > /dev/null 2>&1
100         if [ $? -ne 0 ]; then
101                 echo "md5 checksum failed!"
102         fi
103 }
104
105 _cleanup_defrag()
106 {
107         umount $SCRATCH_MNT > /dev/null 2>&1
108 }
109
110 _setup_defrag()
111 {
112         umount $SCRATCH_MNT > /dev/null 2>&1
113         _scratch_mkfs > /dev/null 2>&1
114         _scratch_mount
115         _create_file $1
116 }
117
118 _rundefrag()
119 {
120         _setup_defrag $1
121         _btrfs_online_defrag $1 $2 $3
122         _checksum
123         _cleanup_defrag
124         _check_scratch_fs
125 }
126
127 # get standard environment, filters and checks
128 . ./common.rc
129 . ./common.filter
130 . ./common.defrag
131
132 # real QA test starts here
133 _supported_fs btrfs
134 _supported_os Linux
135
136 _setup_testdir
137 ## We require scratch so that we'll have free contiguous space
138 _require_scratch
139 _scratch_mkfs >/dev/null 2>&1
140 _scratch_mount
141 _require_defrag
142
143 echo "defrag object | defragment range | defragment compress"
144 echo "a single file |  default | off"
145 _rundefrag 1 1 1
146
147 echo "a single file | default |  on"
148 _rundefrag 1 1 2
149
150 echo "a single file | start < 0 && 0 < len < file size | off"
151 _rundefrag 1 2 1
152
153 echo "a single file | start > file size && 0 < len < file size | off"
154 _rundefrag 1 3 1
155
156 echo "a single file | start = 0 && len < 0 | off"
157 _rundefrag 1 4 1
158
159 echo "a single file | start = 0 && len > file size | off"
160 _rundefrag 1 5 1
161
162 echo "a single file | start = 0 && 0 < len < file size | off"
163 _rundefrag 1 6 1
164
165 echo "a directory | default | off"
166 _rundefrag 2 1 1
167
168 echo "a filesystem | default | off"
169 _rundefrag 3 1 1
170
171 status=0
172 exit