common: use mount/umount helpers everywhere
[xfstests-dev.git] / tests / btrfs / 005
1 #! /bin/bash
2 # FS QA Test No. btrfs/005
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
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
27 here="`pwd`"
28 tmp=/tmp/$$
29 cnt=119
30 filesize=48000
31
32 status=1        # failure is the default!
33 trap "_cleanup; exit \$status" 0 1 2 3 15
34
35 _cleanup()
36 {
37     cd /
38     rm -f $tmp.*
39 }
40
41 _create_file()
42 {
43         if [ $1 -ne 2 ]; then
44                 tmpfile="$SCRATCH_MNT/tmp_file"
45         else
46                 mkdir -p $SCRATCH_MNT/tmp_dir
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         # start = -1 is invalid, should fail
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         # len = -1 is invalid, should fail
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_UTIL_PROG filesystem defragment $str $SCRATCH_MNT/tmp_file >> $seqres.full 2>&1
81         else
82                 if [ "$1" = "1" ];then
83                         $BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT/tmp_file >> $seqres.full 2>&1
84                 elif [ "$1" = "2" ];then
85                         $BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT/tmp_dir >> $seqres.full 2>&1
86                 elif [ "$1" = "3" ];then
87                         $BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT >> $seqres.full 2>&1
88                 fi
89         fi
90         ret_val=$?
91         _scratch_remount
92         # Older defrag returned "20" for success
93         # e9393c2 btrfs-progs: defrag return zero on success
94         if [ $ret_val -ne 0 -a $ret_val -ne 20 ]; then
95                 echo "btrfs filesystem defragment failed!"
96         fi
97 }
98
99 _checksum()
100 {
101         md5sum -c /tmp/checksum > /dev/null 2>&1
102         if [ $? -ne 0 ]; then
103                 echo "md5 checksum failed!"
104         fi
105 }
106
107 _cleanup_defrag()
108 {
109         _scratch_unmount > /dev/null 2>&1
110 }
111
112 _setup_defrag()
113 {
114         _scratch_unmount > /dev/null 2>&1
115         _scratch_mkfs > /dev/null 2>&1
116         _scratch_mount
117         _create_file $1
118 }
119
120 _rundefrag()
121 {
122         _setup_defrag $1
123         _btrfs_online_defrag $1 $2 $3
124         _checksum
125         _cleanup_defrag
126         _check_scratch_fs
127 }
128
129 # get standard environment, filters and checks
130 . ./common/rc
131 . ./common/filter
132 . ./common/defrag
133
134 # real QA test starts here
135 _supported_fs btrfs
136 _supported_os Linux
137 _require_scratch
138
139 rm -f $seqres.full
140
141 _scratch_mkfs >/dev/null 2>&1
142 _scratch_mount
143 _require_defrag
144
145 echo "defrag object | defragment range | defragment compress"
146 echo "a single file | default | off"
147 _rundefrag 1 1 1
148
149 echo "a single file | default |  on"
150 _rundefrag 1 1 2
151
152 echo "a single file | start < 0 && 0 < len < file size | off (should fail)"
153 _rundefrag 1 2 1
154
155 echo "a single file | start > file size && 0 < len < file size | off"
156 _rundefrag 1 3 1
157
158 echo "a single file | start = 0 && len < 0 | off (should fail)"
159 _rundefrag 1 4 1
160
161 echo "a single file | start = 0 && len > file size | off"
162 _rundefrag 1 5 1
163
164 echo "a single file | start = 0 && 0 < len < file size | off"
165 _rundefrag 1 6 1
166
167 echo "a directory | default | off"
168 _rundefrag 2 1 1
169
170 echo "a filesystem | default | off"
171 _rundefrag 3 1 1
172
173 status=0
174 exit