btrfs/254: fix non-unique fsid issue
[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 volume
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 lvdev=/dev/mapper/$node
59
60 seq_mnt=$TEST_DIR/$seq.mnt
61 mkdir -p $seq_mnt
62
63 test_forget()
64 {
65         setup_dmdev
66         dmdev=$(realpath $lvdev)
67
68         _mkfs_dev $dmdev
69
70         # Check if we can un-scan using the mapper device path.
71         $BTRFS_UTIL_PROG device scan --forget $lvdev
72
73         # Cleanup
74         $WIPEFS_PROG -a $lvdev > /dev/null 2>&1
75         $BTRFS_UTIL_PROG device scan --forget
76
77         cleanup_dmdev
78 }
79
80 test_add_device()
81 {
82         setup_dmdev
83         dmdev=$(realpath $lvdev)
84         scratch_dev2=$(echo $SCRATCH_DEV_POOL | awk '{print $2}')
85         scratch_dev3=$(echo $SCRATCH_DEV_POOL | awk '{print $3}')
86
87         _mkfs_dev $scratch_dev3
88         _mount $scratch_dev3 $seq_mnt
89
90         _mkfs_dev -draid1 -mraid1 $lvdev $scratch_dev2
91
92         # Added device should free the stale device in the kernel.
93         $BTRFS_UTIL_PROG device add -f $dmdev $seq_mnt > /dev/null 2>&1
94
95         _mount -o degraded $scratch_dev2 $SCRATCH_MNT
96
97         # Check if the missing device is shown.
98         $BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT | \
99                                         _filter_btrfs_filesystem_show
100
101         $UMOUNT_PROG $seq_mnt
102         _scratch_unmount
103         cleanup_dmdev
104 }
105
106 test_forget
107 test_add_device
108
109 _scratch_dev_pool_put
110
111 status=0
112 exit