xfs: Fix the situation that mount operation rejects corrupted XFS
[xfstests-dev.git] / tests / xfs / 097
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015 Oracle, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 097
6 #
7 # Create and populate an XFS filesystem, corrupt the finobt, then see how
8 # the kernel and xfs_repair deal with it.
9 #
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 trap "_cleanup; exit \$status" 0 1 2 3 15
18
19 _cleanup()
20 {
21     cd /
22     #rm -f $tmp.*
23 }
24
25 # get standard environment, filters and checks
26 . ./common/rc
27 . ./common/filter
28 . ./common/attr
29 . ./common/populate
30
31 # real QA test starts here
32 _supported_fs xfs
33 _supported_os Linux
34
35 _require_scratch
36 _require_xfs_mkfs_finobt
37 _require_xfs_finobt
38 test -n "${FORCE_FUZZ}" || _require_scratch_xfs_crc
39 _require_attrs
40 _require_populate_commands
41 _require_xfs_db_blocktrash_z_command
42 test -z "${FUZZ_ARGS}" && FUZZ_ARGS="-n 8 -3"
43
44 rm -f $seqres.full
45 TESTDIR="${SCRATCH_MNT}/scratchdir"
46 TESTFILE="${TESTDIR}/testfile"
47
48 echo "+ create scratch fs"
49 _scratch_mkfs_xfs > /dev/null
50
51 echo "+ mount fs image"
52 _scratch_mount
53 $XFS_INFO_PROG "${SCRATCH_MNT}" | grep -q "finobt=1" || _notrun "finobt not enabled"
54 blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
55
56 echo "+ make some files"
57 mkdir -p "${TESTDIR}"
58 for x in `seq 1 1024`; do
59         touch "${SCRATCH_MNT}/junk.${x}"
60         inode="$(stat -c '%i' "${SCRATCH_MNT}/junk.${x}")"
61         if [ "$x" -gt 512 ] && [ "$((inode % 64))" -eq 0 ]; then
62                 mv "${SCRATCH_MNT}/junk.${x}" "${TESTFILE}.1"
63                 break
64         fi
65 done
66 for x in `seq 2 64`; do
67         touch "${TESTFILE}.${x}"
68 done
69 inode="$(stat -c '%i' "${TESTFILE}.1")"
70 agcount="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g')"
71 umount "${SCRATCH_MNT}"
72
73 echo "+ check fs"
74 _scratch_xfs_repair -n >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
75
76 echo "+ corrupt image"
77 for ag in $(seq 1 $((agcount - 1))) 0; do
78         _scratch_xfs_db -x -c "agi ${ag}" -c "agi ${ag}" -c "addr free_root" -c "stack" -c "blocktrash -x 32 -y $((blksz * 8)) -z ${FUZZ_ARGS}" >> $seqres.full 2>&1
79 done
80
81 echo "+ mount image && modify files"
82 broken=1
83 if _try_scratch_mount >> $seqres.full 2>&1; then
84
85         broken=0
86         for x in `seq 65 70`; do
87                 touch "${TESTFILE}.${x}" 2> /dev/null && broken=0
88         done
89         umount "${SCRATCH_MNT}"
90 fi
91 echo "broken: ${broken}"
92
93 echo "+ repair fs"
94 _scratch_xfs_repair >> $seqres.full 2>&1
95
96 echo "+ mount image (2)"
97 _scratch_mount
98
99 echo "+ chattr -R -i"
100 $CHATTR_PROG -R -f -i "${SCRATCH_MNT}/"
101
102 echo "+ modify files (2)"
103 broken=0
104 for x in `seq 65 70`; do
105         touch "${TESTFILE}.${x}" || broken=1
106 done
107 echo "broken: ${broken}"
108 umount "${SCRATCH_MNT}"
109
110 echo "+ check fs (2)"
111 _scratch_xfs_repair -n >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
112
113 status=0
114 exit