321bbee9909915237f772f10ea0f61012c63b601
[xfstests-dev.git] / tests / btrfs / 154
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Oracle.  All Rights Reserved.
4 #
5 # FS QA Test 154
6 #
7 # Test for reappearing missing device functionality.
8 #   This test will fail without the btrfs kernel patch
9 #   [PATCH] btrfs: handle dynamically reappearing missing device
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14
15 here=`pwd`
16 tmp=/tmp/$$
17 status=1        # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         cd /
23         rm -f $tmp.*
24 }
25
26 # get standard environment, filters and checks
27 . ./common/rc
28 . ./common/filter
29 . ./common/module
30
31 # remove previous $seqres.full before test
32 rm -f $seqres.full
33
34 # real QA test starts here
35
36 _supported_fs btrfs
37 _supported_os Linux
38 _require_scratch_dev_pool 2
39 _require_btrfs_forget_or_module_loadable
40
41 _scratch_dev_pool_get 2
42
43 DEV1=`echo $SCRATCH_DEV_POOL | awk '{print $1}'`
44 DEV2=`echo $SCRATCH_DEV_POOL | awk '{print $2}'`
45
46 echo DEV1=$DEV1 >> $seqres.full
47 echo DEV2=$DEV2 >> $seqres.full
48
49 # Balance won't be successful if filled too much
50 DEV1_SZ=`blockdev --getsize64 $DEV1`
51 DEV2_SZ=`blockdev --getsize64 $DEV2`
52
53 # get min
54 MAX_FS_SZ=`echo -e "$DEV1_SZ\n$DEV2_SZ" | sort -n | head -1`
55 # Need disks with more than 2G
56 if [ $MAX_FS_SZ -lt 2000000000 ]; then
57         _scratch_dev_pool_put
58         _notrun "Smallest dev size $MAX_FS_SZ, Need at least 2G"
59 fi
60
61 MAX_FS_SZ=100000000
62 bs="1M"
63 COUNT=$(($MAX_FS_SZ / 1000000))
64 CHECKPOINT1=0
65 CHECKPOINT2=0
66
67 setup()
68 {
69         echo >> $seqres.full
70         echo "MAX_FS_SZ=$MAX_FS_SZ COUNT=$COUNT" >> $seqres.full
71         echo "setup"
72         echo "-----setup-----" >> $seqres.full
73         _scratch_pool_mkfs "-mraid1 -draid1" >> $seqres.full 2>&1
74         _scratch_mount >> $seqres.full 2>&1
75         dd if=/dev/urandom of="$SCRATCH_MNT"/tf bs=$bs count=1 \
76                                         >>$seqres.full 2>&1
77         _run_btrfs_util_prog filesystem show -m ${SCRATCH_MNT}
78         _run_btrfs_util_prog filesystem df $SCRATCH_MNT
79         COUNT=$(( $COUNT - 1 ))
80         echo "unmount" >> $seqres.full
81         _scratch_unmount
82 }
83
84 degrade_mount_write()
85 {
86         echo >> $seqres.full
87         echo "--degraded mount: max_fs_sz $max_fs_sz bytes--" >> $seqres.full
88         echo
89         echo "degraded mount"
90
91         echo "clean btrfs ko" >> $seqres.full
92         # un-scan the btrfs devices
93         _btrfs_forget_or_module_reload
94         _mount -o degraded $DEV1 $SCRATCH_MNT >>$seqres.full 2>&1
95         cnt=$(( $COUNT/10 ))
96         dd if=/dev/urandom of="$SCRATCH_MNT"/tf1 bs=$bs count=$cnt \
97                                         >>$seqres.full 2>&1
98         COUNT=$(( $COUNT - $cnt ))
99         _run_btrfs_util_prog filesystem show -m $SCRATCH_MNT
100         _run_btrfs_util_prog filesystem df $SCRATCH_MNT
101         CHECKPOINT1=`md5sum $SCRATCH_MNT/tf1`
102         echo $SCRATCH_MNT/tf1:$CHECKPOINT1 >> $seqres.full
103 }
104
105 scan_missing_dev_and_write()
106 {
107         echo >> $seqres.full
108         echo "--scan missing $DEV2--" >> $seqres.full
109         echo
110         echo "scan missing dev and write"
111
112         _run_btrfs_util_prog device scan $DEV2
113
114         echo >> $seqres.full
115
116         _run_btrfs_util_prog filesystem show -m ${SCRATCH_MNT}
117         _run_btrfs_util_prog filesystem df ${SCRATCH_MNT}
118
119         dd if=/dev/urandom of="$SCRATCH_MNT"/tf2 bs=$bs count=$COUNT \
120                                                 >>$seqres.full 2>&1
121         CHECKPOINT2=`md5sum $SCRATCH_MNT/tf2`
122         echo $SCRATCH_MNT/tf2:$CHECKPOINT2 >> $seqres.full
123 }
124
125 balance_convert()
126 {
127         echo >> $seqres.full
128         echo "----- run balance -----" >> $seqres.full
129         echo
130         echo "run balance"
131
132         _run_btrfs_balance_start -dconvert=raid1 \
133                                  -mconvert=raid1 $SCRATCH_MNT >> $seqres.full
134 }
135
136 verify()
137 {
138         echo >> $seqres.full
139         echo "--mount reconstructed dev only and check md5sum--" >> $seqres.full
140         echo
141         echo "mount reconstructed dev only and check md5sum"
142         echo "unmount" >> $seqres.full
143
144         _scratch_unmount
145         _btrfs_forget_or_module_reload
146         _mount -o degraded $DEV2 $SCRATCH_MNT >>$seqres.full 2>&1
147         verify_checkpoint1=`md5sum $SCRATCH_MNT/tf1`
148         verify_checkpoint2=`md5sum $SCRATCH_MNT/tf2`
149
150         if [ "$verify_checkpoint1" != "$CHECKPOINT1" ]; then
151                 echo "checkpoint1 on reappeared disk does not match after balance"
152         fi
153
154         if [ "$verify_checkpoint2" != "$CHECKPOINT2" ]; then
155                 echo "checkpoint2 on reappeared disk does not match after balance"
156         fi
157 }
158
159 setup
160 degrade_mount_write
161 scan_missing_dev_and_write
162 balance_convert
163 verify
164
165 $UMOUNT_PROG $DEV2
166 _scratch_dev_pool_put
167
168 status=0
169 exit