a589c7ce8fc85b295935f777b90889a6140da67d
[xfstests-dev.git] / tests / btrfs / 264
1 #!/bin/bash
2 # FS QA Test No. 264
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 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=/tmp/$$
30 status=1        # failure is the default!
31
32 _cleanup()
33 {
34     rm -f $tmp.*
35 }
36
37 trap "_cleanup ; exit \$status" 0 1 2 3 15
38
39 # get standard environment, filters and checks
40 . ./common.rc
41 . ./common.filter
42
43 _need_to_be_root
44 _supported_fs btrfs
45 _supported_os Linux
46 _require_scratch
47
48 _scratch_mkfs $SCRATCH_DEV_POOL > /dev/null 2>&1 || _fail "mkfs failed"
49 _scratch_mount
50
51 # Create and save sha256sum
52 # arg1 FS to generate sha256
53 # arg2 File name to save the sha256 output
54 _save_checksum()
55 {
56         local i=0
57         >$2
58         cd $1
59         for i in `find . -type f`; do sha256sum $i >> $2; done
60         cd $OLDPWD
61 }
62
63 # Verify the sha256sum for a FS
64 # arg1 FS to be tested
65 # arg2 sha256 file
66 _verify_checksum()
67 {
68         cd $1
69         [ -f $2 ] || _fail "checksum file $2 not found"
70         sha256sum -c $2 | grep "FAILED"
71         cd $OLDPWD
72 }
73
74 # Create a snapshot
75 # arg1 dest dir
76 # Return snapshot name in the SNAPNAME
77 _create_snap()
78 {
79         local x
80         [ -d $1 ] || _fail "Destination dir $1 not present"
81         SNAPNAME=`mktemp -u $SCRATCH_MNT/snap.XXXXXX`
82         btrfs subvolume snapshot $1 $SNAPNAME > /dev/null || _fail "snapshot create failed"
83 }
84
85 # Reads and writes new data but does not allocate new blocks
86 # arg1 FS to be modified
87 _read_modify_write()
88 {
89         local i
90         local FSIZE
91         for i in `find $1 -type f`
92         do
93                 FSIZE=`stat -t $i | cut -d" " -f2`
94                 dd if=$i of=/dev/null obs=$FSIZE count=1 status=noxfer 2>/dev/null &
95                 dd if=/dev/urandom of=$i obs=$FSIZE count=1 status=noxfer 2>/dev/null &
96         done
97         wait $!
98 }
99
100 # Fills the allocated blocks
101 # arg1 FS in question
102 _fill_blk()
103 {
104         local FSIZE
105         local BLKS
106         local NBLK
107         local FALLOC
108         local WS
109
110         for i in `find /$1 -type f`
111         do
112                 FSIZE=`stat -t $i | cut -d" " -f2`
113                 BLKS=`stat -c "%B" $i`
114                 NBLK=`stat -c "%b" $i`
115                 FALLOC=$(($BLKS * $NBLK))
116                 WS=$(($FALLOC - $FSIZE))
117                 dd if=/dev/urandom of=$i oseek=$FSIZE obs=$WS count=1 status=noxfer 2>/dev/null &
118         done
119         wait $!
120 }
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 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 umount $SCRATCH_DEV || _fail "unmount failed"
189
190 echo "Silence is golden"
191 status=0; exit