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