ext4: Test read-only external journal device
[xfstests-dev.git] / tests / ext4 / 002
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2009 Christoph Hellwig.
4 # Copyright (c) 2020 Lukas Czerner.
5 #
6 # FS QA Test No. 002
7 #
8 # Copied from tests generic/050 and adjusted to support testing
9 # read-only external journal device on ext4.
10 #
11 # Check out various mount/remount/unmount scenarious on a read-only
12 # logdev blockdev.
13 #
14 # This problem has been addressed with proposed kernel patch
15 # https://lore.kernel.org/linux-ext4/20200717090605.2612-1-lczerner@redhat.com/
16 #
17 seqfull=$0
18 seq=`basename $0`
19 seqres=$RESULT_DIR/$seq
20 echo "QA output created by $seq"
21
22 here=`pwd`
23 tmp=/tmp/$$
24 status=1        # failure is the default!
25
26 _cleanup()
27 {
28         cd /
29         blockdev --setrw $SCRATCH_LOGDEV
30         blockdev --setrw $SCRATCH_DEV
31 }
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 # get standard environment, filters and checks
35 . ./common/rc
36 . ./common/filter
37
38 # real QA test starts here
39 _supported_fs ext4 ext3
40 _supported_os Linux
41
42 _require_scratch_nocheck
43 _require_scratch_shutdown
44 _require_logdev
45 _require_local_device $SCRATCH_DEV
46 _require_local_device $SCRATCH_LOGDEV
47 _require_norecovery
48
49 _scratch_mkfs >/dev/null 2>&1
50 _require_metadata_journaling $SCRATCH_DEV
51
52 #
53 # Mark the log device read-only
54 #
55 echo "setting log device read-only"
56 blockdev --setro $SCRATCH_LOGDEV
57
58 #
59 # Mount it, and make sure we can't write to it, and we can unmount it again
60 #
61 echo "mounting with read-only log device:"
62 _try_scratch_mount 2>&1 | _filter_ro_mount | _filter_scratch
63 if [ "${PIPESTATUS[0]}" -eq 0 ]; then
64         echo "touching file on read-only filesystem (should fail)"
65         touch $SCRATCH_MNT/foo 2>&1 | _filter_scratch
66 fi
67
68 echo "unmounting read-only filesystem"
69 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
70
71 echo "setting log device read-write"
72 blockdev --setrw $SCRATCH_LOGDEV
73
74 echo "mounting with read-write log device:"
75 _try_scratch_mount 2>&1 | _filter_scratch
76
77 echo "touch files"
78 touch $SCRATCH_MNT/{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}
79
80 echo "going down:"
81 _scratch_shutdown -f
82
83 echo "unmounting shutdown filesystem:"
84 _scratch_unmount 2>&1 | _filter_scratch
85
86 echo "setting log device read-only"
87 blockdev --setro $SCRATCH_LOGDEV
88
89 #
90 # Mounting a filesystem that requires log-recovery fails even with
91 # -o norecovery unless the fs device is read only or it's mounted
92 # read only
93 #
94 echo "mounting filesystem that needs recovery with a read-only log device:"
95 _try_scratch_mount 2>&1 | _filter_ro_mount | _filter_scratch
96
97 echo "unmounting read-only filesystem"
98 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
99
100
101 echo "mounting filesystem with -o norecovery with a read-only log device:"
102 _try_scratch_mount -o norecovery 2>&1 | _filter_ro_mount | _filter_scratch
103 echo "unmounting read-only filesystem"
104 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
105
106 #
107 # This is the way out if the log device really is read-only.
108 # Doesn't mean it's a good idea in practice, more a last resort
109 # data recovery hack. Either the underlying fs device needs
110 # to be read only as well, or we mount the file system read only
111 #
112 echo "setting fs device read-only"
113 blockdev --setro $SCRATCH_DEV
114 echo "mounting filesystem with -o norecovery with a read-only fs and log device:"
115 _try_scratch_mount -o norecovery 2>&1 | _filter_ro_mount | _filter_scratch
116 echo "unmounting read-only filesystem"
117 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
118 echo "setting fs device read-write"
119 blockdev --setrw $SCRATCH_DEV
120
121 echo "mounting filesystem with -o norecovery,ro with a read-only log device:"
122 _try_scratch_mount -o norecovery,ro 2>&1 | _filter_ro_mount | _filter_scratch
123 echo "unmounting read-only filesystem"
124 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
125
126 echo "setting log device read-write"
127 blockdev --setrw $SCRATCH_LOGDEV
128
129 #
130 # But log recovery is performed when mount with -o ro as long as
131 # the underlying device is not write protected.
132 #
133 echo "mounting filesystem that needs recovery with -o ro:"
134 _try_scratch_mount -o ro 2>&1 | _filter_scratch
135
136 # success, all done
137 echo "*** done"
138 rm -f $seqres.full
139 status=0