misc: move exit status into trap handler
[xfstests-dev.git] / tests / xfs / 189
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 189
6 #
7 # Test remount behaviour
8 # Initial motivation was for pv#985710 and pv#983964
9 #
10 # mount(8) adds all options from mtab and fstab to the mount command line.  So
11 # the filesystem either must not reject any option at all if it can't change it,
12 # or compare the value on the command line to the existing state and only reject
13 # it if it would change something that can't be changed.
14 #
15 # Test this behaviour by mounting a filesystem read-only with a non- default
16 # option and then try to remount it rw.
17 #
18 # note that mount(8) doesn't add the options when specifying both the device
19 # node and mount point, so test out the various mounting alternatives
20 #
21 # <---- Bbbzzzzzzztttt ---->
22 #
23 # Right, but the kernel /proc/mounts output in no way reflects what mount passes
24 # into the kernel, so the entire assumption of this test that what mount outputs
25 # is the same as what it inputs is completely wrong.
26 #
27 # Hence the test now checks to see if the expected options are in the mount
28 # options in /etc/mtab rather than looking for an exact match. Hence the tests
29 # check what we know should change, not what mount thinks has changed. As a
30 # result, the test now passes regardless of whether mount or the kernel controls
31 # the contents of /etc/mtab....
32 #
33 # <---- Normal programming is resumed ---->
34 #
35 #
36 seq=`basename $0`
37 seqres=$RESULT_DIR/$seq
38 echo "QA output created by $seq"
39
40 here=`pwd`
41 tmp=/tmp/$$
42 status=1        # failure is the default!
43 trap "_cleanup; exit \$status" 0 1 2 3 15
44 tag="added by qa $seq"
45
46 rm -f $seqres.full
47
48 _cleanup()
49 {
50         cd /
51         _scratch_unmount 2>/dev/null
52         _putback_scratch_fstab
53         rm -f $tmp.*
54 }
55
56 _scratch_filter()
57 {
58         sed -e "s#$SCRATCH_DEV#SCRATCH_DEV#" \
59             -e "s#$SCRATCH_MNT#SCRATCH_MNT#" \
60             -e "s#,context.*s0\"##"
61 }
62
63 #
64 # the output from /proc/mounts in no way matches what mount puts into the kernel
65 # as options. Work around it as best possible by matching the strings passed in
66 # rather than assuming they are the only options that will be set. If they match
67 # output them to the output file so that the golden image and the filtering
68 # doesn't need to care about what options may or may not be present in /etc/mtab
69 #
70 _check_mount()
71 {
72         rw_or_ro=$1
73         expected_val=$2
74
75         [ -z "$expected_val" ] && expected_val=$1
76
77         _mount | grep $SCRATCH_MNT | _scratch_filter | \
78                 tee -a $seqres.full |
79                 grep $rw_or_ro | grep $expected_val > /dev/null
80         if [ $? -eq 0 ]; then
81                 echo -n "SCRATCH_DEV on SCRATCH_MNT type xfs ($rw_or_ro"
82                 if [ ! -z "$2" ]; then
83                         echo -n ",$2"
84                 fi
85                 echo ")"
86         fi
87 }
88
89 _test_remount_rw()
90 {
91         # use filestreams as a hopefully never default option
92         echo
93         echo "try remount ro,filestreams -> rw,filestreams"
94         echo
95         _try_scratch_mount -o ro,filestreams
96         [ $? -eq 0 ] || echo "ro,filestreams mount failed unexpectedly"
97         _check_mount ro filestreams
98
99         for dev_mnt in $SCRATCH_DEV $SCRATCH_MNT "$SCRATCH_DEV $SCRATCH_MNT"; do
100                 echo "mounting given: $dev_mnt" | _scratch_filter
101                 _mount -o remount,rw,filestreams $dev_mnt
102                 [ $? -eq 0 ] || echo "remount rw failed"
103                 _check_mount rw filestreams
104         done
105
106         _scratch_unmount
107
108         # remount ignores attr2, and noattr2 mount option does does not result
109         # in any "attr2" specific option in /proc/mounts, so we can only check
110         # for ro/rw here.
111         echo
112         echo "try remount ro,noattr2 -> rw,attr2"
113         echo
114         _try_scratch_mount -o ro,noattr2
115         [ $? -eq 0 ] || echo "ro,noattr2 mount failed unexpectedly"
116         _check_mount ro
117
118         for dev_mnt in $SCRATCH_DEV $SCRATCH_MNT "$SCRATCH_DEV $SCRATCH_MNT"; do
119                 echo "mounting given: $dev_mnt" | _scratch_filter
120                 _mount -o remount,rw,attr2 $dev_mnt
121                 [ $? -eq 0 ] || echo "remount rw,attr2 failed"
122                 _check_mount rw
123         done
124
125         _scratch_unmount
126 }
127
128 #
129 # make sure we really can write to a filesystem after remount,rw
130 #
131 _test_remount_write()
132 {
133         echo
134         echo "try touching file after remount ro -> rw with options"
135         echo
136         _try_scratch_mount
137         [ $? -eq 0 ] || echo "mount (1) failed unexpectedly"
138
139         touch $SCRATCH_MNT/foobar
140         [ $? -eq 0 ] || echo "touch (1) failed unexpectedly"
141
142         _scratch_unmount
143
144         _try_scratch_mount -o ro
145         [ $? -eq 0 ] || echo "mount (2) failed unexpectedly"
146
147         _mount -o remount,rw,filestreams $SCRATCH_MNT
148         [ $? -eq 0 ] || echo "remount failed unexpectedly"
149
150         touch $SCRATCH_MNT/foobar
151         [ $? -eq 0 ] || echo "touch (2) failed unexpectedly"
152
153         _scratch_unmount
154 }
155
156 #
157 # barrier is the only option we can change besides ro<->rw which is partially
158 # handled by the VFS and tested elsewhere.  Make sure mount accepts going
159 # from barrier (which also is the default) to nobarrier and back.
160 #
161 _test_remount_barrier()
162 {
163         echo
164         echo "Do remount barrier tests"
165         echo
166
167         # mention barrier explicitly even if it's currently the default just to be sure
168         _try_scratch_mount -o barrier > $tmp.barriermount 2>&1
169
170         # If the kernel doesn't recognize 'barrier' as a mount option then
171         # just fake the output.  The barrier option was removed in 4.19.
172         if grep -q "bad option" $tmp.barriermount; then
173                 cat << ENDL
174 SCRATCH_DEV on SCRATCH_MNT type xfs (rw)
175 SCRATCH_DEV on SCRATCH_MNT type xfs (rw,nobarrier)
176 SCRATCH_DEV on SCRATCH_MNT type xfs (rw)
177 ENDL
178                 return
179         fi
180
181         [ $? -eq 0 ] || echo "mount failed unexpectedly!"
182         _check_mount rw
183
184         _try_scratch_mount -o remount,nobarrier
185         [ $? -eq 0 ] || _fail "remount nobarrier failed"
186         _check_mount rw nobarrier
187
188         _try_scratch_mount -o remount,barrier
189         [ $? -eq 0 ] || _fail "remount barrier failed"
190         _check_mount rw
191
192         _scratch_unmount
193 }
194
195 #
196 # Example fstab entry
197 # /dev/sdb2            /mnt/scratch1        xfs       defaults 0 0
198 #
199 _add_scratch_fstab()
200 {
201         # comment out any existing SCRATCH_DEV
202         $SED_PROG -i "s;$SCRATCH_DEV;#$SCRATCH_DEV;" /etc/fstab
203
204         # add our fstab entry
205         echo "$SCRATCH_DEV $SCRATCH_MNT xfs defaults 0 0 # $tag" >> /etc/fstab
206 }
207
208 _modify_scratch_fstab()
209 {
210         opts=$1
211
212         # modify our fstab entry that we added
213         # modify opts by looking for last word which has non-space chars
214         $SED_PROG -i "s; [^ ]* 0 0 # $tag; $opts 0 0 # $tag;" /etc/fstab
215 }
216
217 _putback_scratch_fstab()
218 {
219         # uncomment out any existing SCRATCH_DEV
220         $SED_PROG -i "s;#$SCRATCH_DEV;$SCRATCH_DEV;" /etc/fstab
221
222         # remove the one we added at the end
223         $SED_PROG -i "/# $tag/d" /etc/fstab
224 }
225
226 # get standard environment, filters and checks
227 . ./common/rc
228 . ./common/filter
229 . ./common/attr
230
231 # real QA test starts here
232 _supported_fs xfs
233
234 _require_no_realtime
235 _require_scratch
236 _require_noattr2
237
238 unset SCRATCH_RTDEV
239 unset SCRATCH_LOGDEV
240
241 _scratch_mkfs_xfs | _filter_mkfs 2>/dev/null
242
243 _add_scratch_fstab
244 _test_remount_rw
245 _test_remount_write
246
247 echo
248 echo "add noikeep to fstab for scratch"
249 _modify_scratch_fstab noikeep # noikeep is not default for non dmapi
250 _test_remount_rw
251
252 _putback_scratch_fstab
253 _test_remount_barrier
254
255 # success, all done
256 echo "*** done"
257 status=0