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