btrfs: add a test for some disk caching usecases
[xfstests-dev.git] / tests / btrfs / 219
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 Facebook.  All Rights Reserved.
4 #
5 # FS QA Test 219
6 #
7 # Test a variety of stale device usecases.  We cache the device and generation
8 # to make sure we do not allow stale devices, which can end up with some wonky
9 # behavior for loop back devices.  This was changed with
10 #
11 #   btrfs: allow single disk devices to mount with older generations
12 #
13 # But I've added a few other test cases so it's clear what we expect to happen
14 # currently.
15 #
16
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1        # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28         cd /
29         rm -f $tmp.*
30         if [ ! -z "$loop_mnt" ]; then
31                 $UMOUNT_PROG $loop_mnt
32                 rm -rf $loop_mnt
33         fi
34         [ ! -z "$loop_mnt1" ] && rm -rf $loop_mnt1
35         [ ! -z "$fs_img1" ] && rm -rf $fs_img1
36         [ ! -z "$fs_img2" ] && rm -rf $fs_img2
37         [ ! -z "$loop_dev" ] && _destroy_loop_device $loop_dev
38 }
39
40 # get standard environment, filters and checks
41 . ./common/rc
42 . ./common/filter
43
44 # remove previous $seqres.full before test
45 rm -f $seqres.full
46
47 # real QA test starts here
48
49 _supported_fs btrfs
50 _supported_os Linux
51 _require_test
52 _require_loop
53 _require_btrfs_forget_or_module_loadable
54
55 loop_mnt=$TEST_DIR/$seq.mnt
56 loop_mnt1=$TEST_DIR/$seq.mnt1
57 fs_img1=$TEST_DIR/$seq.img1
58 fs_img2=$TEST_DIR/$seq.img2
59
60 mkdir $loop_mnt
61 mkdir $loop_mnt1
62
63 $XFS_IO_PROG -f -c "truncate 256m" $fs_img1 >>$seqres.full 2>&1
64
65 _mkfs_dev $fs_img1 >>$seqres.full 2>&1
66 cp $fs_img1 $fs_img2
67
68 # Normal single device case, should pass just fine
69 _mount -o loop $fs_img1 $loop_mnt > /dev/null  2>&1 || \
70         _fail "Couldn't do initial mount"
71 $UMOUNT_PROG $loop_mnt
72
73 _btrfs_forget_or_module_reload
74
75 # Now mount the new version again to get the higher generation cached, umount
76 # and try to mount the old version.  Mount the new version again just for good
77 # measure.
78 loop_dev=`_create_loop_device $fs_img1`
79
80 _mount $loop_dev $loop_mnt > /dev/null 2>&1 || \
81         _fail "Failed to mount the second time"
82 $UMOUNT_PROG $loop_mnt
83
84 _mount -o loop $fs_img2 $loop_mnt > /dev/null 2>&1 || \
85         _fail "We couldn't mount the old generation"
86 $UMOUNT_PROG $loop_mnt
87
88 _mount $loop_dev $loop_mnt > /dev/null 2>&1 || \
89         _fail "Failed to mount the second time"
90 $UMOUNT_PROG $loop_mnt
91
92 # Now we definitely can't mount them at the same time, because we're still tied
93 # to the limitation of one fs_devices per fsid.
94 _btrfs_forget_or_module_reload
95
96 _mount $loop_dev $loop_mnt > /dev/null 2>&1 || \
97         _fail "Failed to mount the third time"
98 _mount -o loop $fs_img2 $loop_mnt1 > /dev/null 2>&1 && \
99         _fail "We were allowed to mount when we should have failed"
100
101 # success, all done
102 echo "Silence is golden"
103 status=0
104 exit