btrfs/079: fix failure to umount scratch fs due to running filefrag process
[xfstests-dev.git] / tests / btrfs / 027
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Facebook.  All Rights Reserved.
4 #
5 # FS QA Test 027
6 #
7 # Test replace of a missing device on various data and metadata profiles.
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 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         cd /
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # remove previous $seqres.full before test
29 rm -f $seqres.full
30
31 # real QA test starts here
32
33 _supported_fs btrfs
34 _supported_os Linux
35 # we check scratch dev after each loop
36 _require_scratch_nocheck
37 _require_scratch_dev_pool 5
38 _require_scratch_dev_pool_equal_size
39 _btrfs_get_profile_configs replace-missing
40 _require_command "$WIPEFS_PROG" wipefs
41
42 run_test()
43 {
44         local mkfs_opts=$1
45         local ndevs=`echo $SCRATCH_DEV_POOL | wc -w`
46
47         # reserve one for replace target
48         ((ndevs--))
49
50         _scratch_dev_pool_get $ndevs
51         _spare_dev_get
52
53         echo "Test $mkfs_opts" >>$seqres.full
54
55         _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
56         # make sure we created btrfs with desired options
57         if [ $? -ne 0 ]; then
58                 echo "mkfs $mkfs_opts failed"
59                 _spare_dev_put
60                 _scratch_dev_pool_put
61                 return
62         fi
63         _scratch_mount >>$seqres.full 2>&1
64
65         local missing_dev=`echo $SCRATCH_DEV_POOL | awk '{print $2}'`
66         local missing_dev_id=`$BTRFS_UTIL_PROG filesystem show $SCRATCH_MNT | grep $missing_dev | awk '{print $2}'`
67
68         # get some data on the filesystem so there's something to replace
69         _ddt of="$SCRATCH_MNT"/file1 bs=1M count=1 >> $seqres.full 2>&1
70         _ddt of="$SCRATCH_MNT"/file2 bs=1M count=2 >> $seqres.full 2>&1
71         _ddt of="$SCRATCH_MNT"/file3 bs=1M count=4 >> $seqres.full 2>&1
72
73         # nuke a device and remount in degraded mode
74         _scratch_unmount
75         $WIPEFS_PROG -a "$missing_dev" >>$seqres.full 2>&1
76         _scratch_mount -o degraded >>$seqres.full 2>&1
77
78         # replace $missing_dev with $replace_dev and scrub it to double-check
79         $BTRFS_UTIL_PROG replace start -B -f -r $missing_dev_id $SPARE_DEV \
80                 $SCRATCH_MNT >>$seqres.full 2>&1
81         if [ $? -ne 0 ]; then
82                 echo "btrfs replace failed"
83                 _scratch_unmount
84                 _spare_dev_put
85                 _scratch_dev_pool_put
86                 return
87         fi
88         $BTRFS_UTIL_PROG scrub start -B $SCRATCH_MNT >>$seqres.full 2>&1
89         if [ $? -ne 0 ]; then
90                 echo "btrfs scrub failed"
91                 _scratch_unmount
92                 _spare_dev_put
93                 _scratch_dev_pool_put
94                 return
95         fi
96
97         _scratch_unmount
98         # we called _require_scratch_nocheck instead of _require_scratch
99         # do check after test for each profile config
100         _check_scratch_fs
101         _spare_dev_put
102         _scratch_dev_pool_put
103 }
104
105 echo "Silence is golden"
106 for t in "${_btrfs_profile_configs[@]}"; do
107         run_test "$t"
108 done
109
110 status=0
111 exit