35e857b0eb49f9c706c29694d9d672acceebbd7f
[xfstests-dev.git] / tests / btrfs / 002
1 #!/bin/bash
2 # FS QA Test No. btrfs/002
3 #
4 # Extented btrfs snapshot test cases
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2011 Oracle  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
25 seq=`basename $0`
26 seqres=$RESULT_DIR/$seq
27 echo "QA output created by $seq"
28
29 here=`pwd`
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32
33 _cleanup()
34 {
35     rm -f $tmp.*
36 }
37
38 trap "_cleanup ; exit \$status" 0 1 2 3 15
39
40 # get standard environment, filters and checks
41 . ./common/rc
42 . ./common/filter
43
44 _need_to_be_root
45 _supported_fs btrfs
46 _supported_os Linux
47 _require_scratch
48
49 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
50 _scratch_mount
51
52 # Create and save sha256sum
53 # arg1 FS to generate sha256
54 # arg2 File name to save the sha256 output
55 _save_checksum()
56 {
57         local i=0
58         >$2
59         cd $1
60         for i in `find . -type f`; do sha256sum $i >> $2; done
61         cd $OLDPWD
62 }
63
64 # Verify the sha256sum for a FS
65 # arg1 FS to be tested
66 # arg2 sha256 file
67 _verify_checksum()
68 {
69         cd $1
70         [ -f $2 ] || _fail "checksum file $2 not found"
71         sha256sum -c $2 | grep "FAILED"
72         cd $OLDPWD
73 }
74
75 # Create a snapshot
76 # arg1 dest dir
77 # Return snapshot name in the SNAPNAME
78 _create_snap()
79 {
80         local x
81         [ -d $1 ] || _fail "Destination dir $1 not present"
82         SNAPNAME=`mktemp -u $SCRATCH_MNT/snap.XXXXXX`
83         $BTRFS_UTIL_PROG subvolume snapshot $1 $SNAPNAME > /dev/null || _fail "snapshot create failed"
84 }
85
86 # Reads and writes new data but does not allocate new blocks
87 # arg1 FS to be modified
88 _read_modify_write()
89 {
90         local i
91         local FSIZE
92         for i in `find $1 -type f`
93         do
94                 FSIZE=`stat -t $i | cut -d" " -f2`
95                 dd if=$i of=/dev/null obs=$FSIZE count=1 status=noxfer 2>/dev/null &
96                 dd if=/dev/urandom of=$i obs=$FSIZE count=1 status=noxfer 2>/dev/null &
97         done
98         wait $!
99 }
100
101 # Fills the allocated blocks
102 # arg1 FS in question
103 _fill_blk()
104 {
105         local FSIZE
106         local BLKS
107         local NBLK
108         local FALLOC
109         local WS
110
111         for i in `find /$1 -type f`
112         do
113                 FSIZE=`stat -t $i | cut -d" " -f2`
114                 BLKS=`stat -c "%B" $i`
115                 NBLK=`stat -c "%b" $i`
116                 FALLOC=$(($BLKS * $NBLK))
117                 WS=$(($FALLOC - $FSIZE))
118                 dd if=/dev/urandom of=$i oseek=$FSIZE obs=$WS count=1 status=noxfer 2>/dev/null &
119         done
120         wait $!
121 }
122
123 # Append a random size to the files
124 # arg1 : FS in question
125 _append_file()
126 {
127         local FSIZE
128         local X
129         local N
130         local i
131         N=0
132         for i in `find $1 -type f`
133         do
134                 if [ $N == 0 ]; then
135                         X=$i
136                         FSIZE=`stat -t $X | cut -d" " -f2`
137                         dd if=$X of=$X seek=1 bs=$FSIZE obs=$FSIZE count=1 status=noxfer 2>/dev/null &
138                         N=$(($N+1))
139                         continue
140                 fi
141                 FSIZE=`stat -t $i | cut -d" " -f2`
142                 dd if=$X of=$i seek=1 bs=$FSIZE obs=$FSIZE count=1 status=noxfer 2>/dev/null &
143                 X=$i
144         done
145         wait $!
146 }
147
148 ##################### real QA test starts here###################################
149 # sv1 - is just a name nothing spl
150 firstvol="$SCRATCH_MNT/sv1"
151 $BTRFS_UTIL_PROG subvolume create $firstvol > /dev/null || _fail "btrfs subvolume create $firstvol failed"
152 dirp=`mktemp -duq $firstvol/dir.XXXXXX`
153 _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10 -x
154 SNAPNAME=0
155 _create_snap $firstvol
156 _save_checksum $firstvol $tmp.sv1.sum
157 _verify_checksum $SNAPNAME $tmp.sv1.sum
158
159 #Append1 the files
160 _fill_blk $SNAPNAME
161 _verify_checksum $firstvol $tmp.sv1.sum
162
163 #Append2 the files
164 _append_file $SNAPNAME
165 _verify_checksum $firstvol $tmp.sv1.sum
166
167 #read modify write
168 _read_modify_write $SNAPNAME
169 _verify_checksum $firstvol $tmp.sv1.sum
170
171 #nested snapshot test
172 src_vol=$firstvol
173 for i in `seq 1 7`; do
174         SNAPNAME=0
175         _create_snap $src_vol
176         _verify_checksum $SNAPNAME $tmp.sv1.sum
177         src_vol=$SNAPNAME
178 done
179
180 # file delete test
181 SNAPNAME=0
182 _create_snap $firstvol
183 tname=`echo $SNAPNAME | rev | cut -d"/" -f1 | rev`
184 _save_checksum $SNAPNAME $tmp.$tname.sum
185 \rm -rf $firstvol/*
186 _verify_checksum $SNAPNAME $tmp.$tname.sum
187
188 _scratch_unmount || _fail "unmount failed"
189
190 echo "Silence is golden"
191 status=0; exit