xfstests: add _require_freeze and minor cleanups
[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                 tmpfile="$SCRATCH_MNT/tmp_dir/tmp_file"
48         fi
49                 
50         for i in `seq $cnt -1 0`; do
51                 dd if=/dev/zero of=$tmpfile bs=4k count=1 \
52                  conv=notrunc seek=$i oflag=sync &>/dev/null
53         done
54         # get md5sum
55         md5sum $tmpfile > /tmp/checksum
56 }
57
58 _btrfs_online_defrag()
59 {
60         str=""
61         if [ "$2" = "2" ];then
62                 str="$str -s -1 -l $((filesize / 2)) "
63         elif [ "$2" = "3" ];then
64                 str="$str -s $((filesize + 1)) -l $((filesize / 2)) "
65                 HAVE_DEFRAG=1
66         elif [ "$2" = "4" ];then
67                 str="$str -l -1 "
68         elif [ "$2" = "5" ];then
69                 str="$str -l $((filesize + 1)) "
70         elif [ "$2" = "6" ];then
71                 str="$str -l $((filesize / 2)) "
72         fi
73
74         if [ "$3" = "2" ];then
75                 str="$str -c "
76         fi
77
78         if [ "$str" != "" ]; then
79                 btrfs filesystem defragment $str $SCRATCH_MNT/tmp_file
80         else
81                 if [ "$1" = "1" ];then
82                         btrfs filesystem defragment $SCRATCH_MNT/tmp_file
83                 elif [ "$1" = "2" ];then
84                         btrfs filesystem defragment $SCRATCH_MNT/tmp_dir
85                 elif [ "$1" = "3" ];then
86                         btrfs filesystem defragment $SCRATCH_MNT
87                 fi
88         fi
89         ret_val=$?
90         _scratch_remount
91         if [ $ret_val -ne 20 ];then
92                 echo "btrfs filesystem defragment failed! err is $ret_val"
93         fi
94 }
95
96 _checksum()
97 {
98         md5sum -c /tmp/checksum > /dev/null 2>&1
99         if [ $? -ne 0 ]; then
100                 echo "md5 checksum failed!"
101         fi
102 }
103
104 _cleanup_defrag()
105 {
106         umount $SCRATCH_MNT > /dev/null 2>&1
107 }
108
109 _setup_defrag()
110 {
111         umount $SCRATCH_MNT > /dev/null 2>&1
112         _scratch_mkfs > /dev/null 2>&1
113         _scratch_mount
114         _create_file $1
115 }
116
117 _rundefrag()
118 {
119         _setup_defrag $1
120         _btrfs_online_defrag $1 $2 $3
121         _checksum
122         _cleanup_defrag
123         _check_scratch_fs
124 }
125
126 # get standard environment, filters and checks
127 . ./common.rc
128 . ./common.filter
129 . ./common.defrag
130
131 # real QA test starts here
132 _supported_fs btrfs
133 _supported_os Linux
134
135 _setup_testdir
136 ## We require scratch so that we'll have free contiguous space
137 _require_scratch
138 _scratch_mkfs >/dev/null 2>&1
139 _scratch_mount
140 _require_defrag
141
142 echo "defrag object | defragment range | defragment compress"
143 echo "a single file |  default | off"
144 _rundefrag 1 1 1
145
146 echo "a single file | default |  on"
147 _rundefrag 1 1 2
148
149 echo "a single file | start < 0 && 0 < len < file size | off"
150 _rundefrag 1 2 1
151
152 echo "a single file | start > file size && 0 < len < file size | off"
153 _rundefrag 1 3 1
154
155 echo "a single file | start = 0 && len < 0 | off"
156 _rundefrag 1 4 1
157
158 echo "a single file | start = 0 && len > file size | off"
159 _rundefrag 1 5 1
160
161 echo "a single file | start = 0 && 0 < len < file size | off"
162 _rundefrag 1 6 1
163
164 echo "a directory | default | off"
165 _rundefrag 2 1 1
166
167 echo "a filesystem | default | off"
168 _rundefrag 3 1 1
169
170 status=0
171 exit