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