xfs: test that the needsrepair feature works as advertised
[xfstests-dev.git] / tests / xfs / 154
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Copyright (c) 2021 Oracle.  All Rights Reserved.
4 #
5 # FS QA Test No. 154
6 #
7 # Make sure that the kernel won't mount a filesystem if repair forcibly sets
8 # NEEDSREPAIR while fixing metadata.  Corrupt a directory in such a way as
9 # to force repair to write an invalid dirent value as a sentinel to trigger a
10 # repair activity in a later phase.  Use a debug knob in xfs_repair to abort
11 # the repair immediately after forcing the flag on.
12
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1    # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31
32 # real QA test starts here
33 _supported_fs xfs
34 _require_scratch_nocheck
35 _require_scratch_xfs_crc                # needsrepair only exists for v5
36 _require_libxfs_debug_flag LIBXFS_DEBUG_WRITE_CRASH
37
38 rm -f $seqres.full
39
40 # Set up a real filesystem for our actual test
41 _scratch_mkfs >> $seqres.full
42
43 # Create a directory large enough to have a dir data block.  2k worth of
44 # dirent names ought to do it.
45 _scratch_mount
46 mkdir -p $SCRATCH_MNT/fubar
47 for i in $(seq 0 256 2048); do
48         fname=$(printf "%0255d" $i)
49         ln -s -f urk $SCRATCH_MNT/fubar/$fname
50 done
51 inum=$(stat -c '%i' $SCRATCH_MNT/fubar)
52 _scratch_unmount
53
54 # Fuzz the directory
55 _scratch_xfs_db -x -c "inode $inum" -c "dblock 0" \
56         -c "fuzz -d bu[2].inumber add" >> $seqres.full
57
58 # Try to repair the directory, force it to crash after setting needsrepair
59 LIBXFS_DEBUG_WRITE_CRASH=ddev=2 _scratch_xfs_repair 2>> $seqres.full
60 test $? -eq 137 || echo "repair should have been killed??"
61
62 # We can't mount, right?
63 _check_scratch_xfs_features NEEDSREPAIR
64 _try_scratch_mount &> $tmp.mount
65 res=$?
66 _filter_scratch < $tmp.mount
67 if [ $res -eq 0 ]; then
68         echo "Should not be able to mount after needsrepair crash"
69         _scratch_unmount
70 fi
71
72 # Repair properly this time and retry the mount
73 _scratch_xfs_repair 2>> $seqres.full
74 _check_scratch_xfs_features NEEDSREPAIR
75
76 _scratch_mount
77
78 # success, all done
79 status=0
80 exit