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