generic/050: Fix test failure for filesystems without journal
[xfstests-dev.git] / tests / generic / 050
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2009 Christoph Hellwig.
4 #
5 # FS QA Test No. 050
6 #
7 # Check out various mount/remount/unmount scenarious on a read-only blockdev.
8 #
9 seqfull=$0
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13
14 here=`pwd`
15 tmp=/tmp/$$
16 status=1        # failure is the default!
17
18 _cleanup()
19 {
20         cd /
21         blockdev --setrw $SCRATCH_DEV
22 }
23 trap "_cleanup; exit \$status" 0 1 2 3 15
24
25 # get standard environment, filters and checks
26 . ./common/rc
27 . ./common/filter
28
29 # real QA test starts here
30 _supported_fs generic
31 _supported_os Linux
32
33 _require_scratch_nocheck
34 _require_scratch_shutdown
35 _require_local_device $SCRATCH_DEV
36 _require_norecovery
37
38 # Select appropriate output file
39 features=""
40 if ! _has_metadata_journaling $SCRATCH_DEV >/dev/null; then
41         features="nojournal"
42 fi
43 _link_out_file "$features"
44
45 _scratch_mkfs >/dev/null 2>&1
46
47 filter_ro_mount() {
48         local arg=""
49
50         if [ -n "$expect_mount_failure" ]; then
51                 arg="s|mount: $SCRATCH_MNT: permission denied|mount: device write-protected, mounting read-only|g"
52         fi
53         sed -e "$arg" | _filter_ro_mount
54 }
55
56 # Mounting with quota on XFS requires a writable fs, which means
57 # we expect to fail the ro blockdev test with with EPERM.
58 expect_mount_failure=
59 if [ "$FSTYP" = "xfs" ] && echo "$MOUNT_OPTIONS" | grep -q quota ; then
60         expect_mount_failure=1
61 fi
62
63 #
64 # Mark the device read-only
65 #
66 echo "setting device read-only"
67 blockdev --setro $SCRATCH_DEV
68
69 #
70 # Mount it, and make sure we can't write to it, and we can unmount it again
71 #
72 echo "mounting read-only block device:"
73 _try_scratch_mount 2>&1 | filter_ro_mount
74 if [ "${PIPESTATUS[0]}" -eq 0 ]; then
75         echo "touching file on read-only filesystem (should fail)"
76         touch $SCRATCH_MNT/foo 2>&1 | _filter_scratch
77
78         #
79         # Apparently this used to be broken at some point:
80         #       http://oss.sgi.com/bugzilla/show_bug.cgi?id=807
81         #
82         echo "unmounting read-only filesystem"
83         _scratch_unmount 2>&1 | _filter_scratch
84 elif [ -n "${expect_mount_failure}" ]; then
85         # Mount failed, so simulate EROFS instead of scribbling on root fs
86         echo "touching file on read-only filesystem (should fail)"
87         echo "touch: cannot touch 'SCRATCH_MNT/foo': Read-only file system"
88         echo "unmounting read-only filesystem"
89 else
90         echo "Mount failed, though it wasn't supposed to!"
91 fi
92
93 echo "setting device read-write"
94 blockdev --setrw $SCRATCH_DEV
95
96 echo "mounting read-write block device:"
97 _try_scratch_mount 2>&1 | _filter_scratch
98
99 echo "touch files"
100 touch $SCRATCH_MNT/{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}
101
102 echo "going down:"
103 _scratch_shutdown -f
104
105 echo "unmounting shutdown filesystem:"
106 _scratch_unmount 2>&1 | _filter_scratch
107
108 echo "setting device read-only"
109 blockdev --setro $SCRATCH_DEV
110
111 #
112 # Mounting a filesystem that requires log-recovery fails unless
113 # -o norecovery is used.
114 #
115 echo "mounting filesystem that needs recovery on a read-only device:"
116 _try_scratch_mount 2>&1 | _filter_ro_mount
117
118 echo "unmounting read-only filesystem"
119 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
120
121 #
122 # This is the way out if the underlying device really is read-only.
123 # Doesn't mean it's a good idea in practice, more a last resort
124 # data recovery hack.
125 #
126 echo "mounting filesystem with -o norecovery on a read-only device:"
127 _try_scratch_mount -o norecovery 2>&1 | filter_ro_mount
128 if [ "${PIPESTATUS[0]}" -eq 0 ]; then
129         echo "unmounting read-only filesystem"
130         _scratch_unmount 2>&1 | _filter_scratch
131 elif [ -n "${expect_mount_failure}" ]; then
132         # Mount failed, simulate correct output
133         echo "unmounting read-only filesystem"
134 else
135         echo "Mount failed, though it wasn't supposed to!"
136 fi
137
138 echo "setting device read-write"
139 blockdev --setrw $SCRATCH_DEV
140
141 #
142 # But log recovery is performed when mount with -o ro as long as
143 # the underlying device is not write protected.
144 #
145 echo "mounting filesystem that needs recovery with -o ro:"
146 _try_scratch_mount -o ro 2>&1 | _filter_scratch
147
148 # success, all done
149 echo "*** done"
150 rm -f $seqres.full
151 status=0