b70b9d16589739fc82d63fad94174e7f89ea0356
[xfstests-dev.git] / tests / btrfs / 254
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Anand Jain. All Rights Reserved.
4 # Copyright (c) 2021 Oracle. All Rights Reserved.
5 #
6 # FS QA Test No. 254
7 #
8 # Test if the kernel can free the stale device entries.
9 #
10 # Tests bug fixed by the kernel patch:
11 #       btrfs: harden identification of the stale device
12 #
13 . ./common/preamble
14 _begin_fstest auto quick
15
16 # Override the default cleanup function.
17 node=$seq-test
18 cleanup_dmdev()
19 {
20         _dmsetup_remove $node
21 }
22
23 _cleanup()
24 {
25         cd /
26         rm -f $tmp.*
27         rm -rf $seq_mnt > /dev/null 2>&1
28         cleanup_dmdev
29 }
30
31 # Import common functions.
32 . ./common/filter
33 . ./common/filter.btrfs
34
35 # real QA test starts here
36 _supported_fs btrfs
37 _require_scratch_dev_pool 3
38 _require_block_device $SCRATCH_DEV
39 _require_dm_target linear
40 _require_btrfs_forget_or_module_loadable
41 _require_scratch_nocheck
42 _require_command "$WIPEFS_PROG" wipefs
43
44 _scratch_dev_pool_get 3
45
46 setup_dmdev()
47 {
48         # Some small size.
49         size=$((1024 * 1024 * 1024))
50         size_in_sector=$((size / 512))
51
52         table="0 $size_in_sector linear $SCRATCH_DEV 0"
53         _dmsetup_create $node --table "$table" || \
54                 _fail "setup dm device failed"
55 }
56
57 # Use a known it is much easier to debug.
58 uuid="--uuid 12345678-1234-1234-1234-123456789abc"
59 lvdev=/dev/mapper/$node
60
61 seq_mnt=$TEST_DIR/$seq.mnt
62 mkdir -p $seq_mnt
63
64 test_forget()
65 {
66         setup_dmdev
67         dmdev=$(realpath $lvdev)
68
69         _mkfs_dev $uuid $dmdev
70
71         # Check if we can un-scan using the mapper device path.
72         $BTRFS_UTIL_PROG device scan --forget $lvdev
73
74         # Cleanup
75         $WIPEFS_PROG -a $lvdev > /dev/null 2>&1
76         $BTRFS_UTIL_PROG device scan --forget
77
78         cleanup_dmdev
79 }
80
81 test_add_device()
82 {
83         setup_dmdev
84         dmdev=$(realpath $lvdev)
85         scratch_dev2=$(echo $SCRATCH_DEV_POOL | awk '{print $2}')
86         scratch_dev3=$(echo $SCRATCH_DEV_POOL | awk '{print $3}')
87
88         _mkfs_dev $scratch_dev3
89         _mount $scratch_dev3 $seq_mnt
90
91         _mkfs_dev $uuid -draid1 -mraid1 $lvdev $scratch_dev2
92
93         # Add device should free the device under $uuid in the kernel.
94         $BTRFS_UTIL_PROG device add -f $dmdev $seq_mnt > /dev/null 2>&1
95
96         _mount -o degraded $scratch_dev2 $SCRATCH_MNT
97
98         # Check if the missing device is shown.
99         $BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT | \
100                                         _filter_btrfs_filesystem_show
101
102         $UMOUNT_PROG $seq_mnt
103         _scratch_unmount
104         cleanup_dmdev
105 }
106
107 test_forget
108 test_add_device
109
110 _scratch_dev_pool_put
111
112 status=0
113 exit