fsx/fsstress: round blocksize properly
[xfstests-dev.git] / tests / btrfs / 003
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/003
6 #
7 # btrfs vol tests
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 dev_removed=0
17 removed_dev_htl=""
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 # Check if all scratch dev pools are deletable
21 deletable_scratch_dev_pool()
22 {
23         local i
24         local x
25         for i in $SCRATCH_DEV_POOL; do
26                 x=`echo $i | cut -d"/" -f 3`
27                 if [ ! -f /sys/class/block/${x}/device/delete ]; then
28                         return 1
29                 fi
30         done
31
32         return 0
33 }
34
35 _cleanup()
36 {
37     cd /
38     rm -f $tmp.*
39     if [ $dev_removed == 1 ]; then
40         _scratch_unmount
41         _devmgt_add "${removed_dev_htl}"
42     fi
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48
49 _supported_fs btrfs
50 _require_scratch
51 _require_scratch_dev_pool 4
52 _require_command "$WIPEFS_PROG" wipefs
53
54 rm -f $seqres.full
55
56 # Test cases related to raid in btrfs
57 _test_raid0()
58 {
59         export MKFS_OPTIONS="-m raid0 -d raid0"
60         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
61         _scratch_mount
62         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
63         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10 -c
64         _scratch_unmount
65 }
66
67 _test_raid1()
68 {
69         export MKFS_OPTIONS="-m raid1 -d raid1"
70         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
71         _scratch_mount
72         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
73         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10 -c
74         _scratch_unmount
75 }
76
77 _test_raid10()
78 {
79         export MKFS_OPTIONS="-m raid10 -d raid10"
80         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
81         _scratch_mount
82         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
83         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
84         _scratch_unmount
85 }
86
87 _test_single()
88 {
89         export MKFS_OPTIONS="-m single -d single"
90         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
91         _scratch_mount
92         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
93         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
94         _scratch_unmount
95 }
96
97 _test_add()
98 {
99         local i
100         local -a devs="( $SCRATCH_DEV_POOL )"
101         local n=${#devs[@]}
102
103         n=$(($n-1))
104
105         export MKFS_OPTIONS=""
106         _scratch_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
107         _scratch_mount
108         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
109         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
110         for i in `seq 2 $n`; do
111                 $WIPEFS_PROG -a ${devs[$i]} >> $seqres.full 2>&1 || \
112                         _fail "wipefs failed"
113                 $BTRFS_UTIL_PROG device add ${devs[$i]} $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "device add failed"
114         done
115         _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
116         _scratch_unmount
117 }
118
119 _test_replace()
120 {
121         local i
122         local devs=( $SCRATCH_DEV_POOL )
123         local n=${#devs[@]}
124         local ds
125         local d
126
127         # If scratch devs are not deletable skip this test
128         if ! deletable_scratch_dev_pool; then return 0; fi
129
130         # exclude the first and the last disk in the disk pool
131         n=$(($n-1))
132         ds=${devs[@]:1:$(($n-1))}
133
134         export MKFS_OPTIONS="-m raid1 -d raid1"
135         _scratch_mkfs "$ds" >> $seqres.full 2>&1 || _fail "tr: mkfs failed"
136         _scratch_mount
137         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
138         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
139
140         #pick the 2nd last disk 
141         ds=${devs[@]:$(($n-1)):1}
142
143         # retrive the HTL for this scsi disk
144         d=`echo $ds|cut -d"/" -f3`
145         removed_dev_htl=`ls -l /sys/class/block/${d} | rev | cut -d "/" -f 3 | rev`
146
147         #fail disk
148         _devmgt_remove ${removed_dev_htl} $ds
149         dev_removed=1
150
151         $BTRFS_UTIL_PROG filesystem show $SCRATCH_DEV | grep "Some devices missing" >> $seqres.full || _fail \
152                                                         "btrfs did not report device missing"
153
154         # add a new disk to btrfs
155         ds=${devs[@]:$(($n)):1}
156         $WIPEFS_PROG -a ${ds} >> $seqres.full 2>&1 || _fail "wipefs failed"
157         $BTRFS_UTIL_PROG device add ${ds} $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "dev add failed"
158         # in some system balance fails if there is no delay (a bug)
159         # putting sleep 10 to work around as of now
160         # sleep 10
161         _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
162
163         # cleaup. add the removed disk
164         _scratch_unmount
165         _devmgt_add "${removed_dev_htl}"
166         dev_removed=0
167 }
168
169 _test_remove()
170 {
171         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
172         _scratch_mount
173         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
174         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
175
176         # pick last dev in the list
177         dev_del=`echo ${SCRATCH_DEV_POOL} | awk '{print $NF}'`
178         $BTRFS_UTIL_PROG device delete $dev_del $SCRATCH_MNT || _fail "btrfs device delete failed"
179         $BTRFS_UTIL_PROG filesystem show $SCRATCH_DEV 2>&1 | grep $dev_del >> $seqres.full && _fail "btrfs still shows the deleted dev"
180         _scratch_unmount
181 }
182
183 _test_raid0
184 _test_raid1
185 _test_raid10
186 _test_single
187 _test_add
188 _test_replace
189 _test_remove
190
191 echo "Silence is golden"
192 status=0; exit