lib/: spdx license conversion
[xfstests-dev.git] / tests / ext4 / 013
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. 013
6 #
7 # Create and populate an ext4 filesystem, corrupt an inode, then see how
8 # the kernel and e2fsck 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
30 # real QA test starts here
31 _supported_fs ext4
32 _supported_os Linux
33
34 _require_scratch
35 test -n "${FORCE_FUZZ}" || _require_scratch_ext4_crc
36 _require_attrs
37
38 rm -f $seqres.full
39 TESTDIR="${SCRATCH_MNT}/scratchdir"
40 TESTFILE="${TESTDIR}/testfile"
41
42 echo "+ create scratch fs"
43 _scratch_mkfs_ext4 > /dev/null 2>&1
44
45 echo "+ mount fs image"
46 _scratch_mount
47
48 echo "+ make some files"
49 mkdir -p "${TESTDIR}"
50 for x in `seq 1 1024`; do
51         touch "${SCRATCH_MNT}/junk.${x}"
52         inode="$(stat -c '%i' "${SCRATCH_MNT}/junk.${x}")"
53         if [ "$x" -gt 512 ] && [ "$((inode % 64))" -eq 0 ]; then
54                 mv "${SCRATCH_MNT}/junk.${x}" "${TESTFILE}.1"
55                 break
56         fi
57 done
58 for x in `seq 2 64`; do
59         touch "${TESTFILE}.${x}"
60 done
61 inode="$(stat -c '%i' "${TESTFILE}.1")"
62 umount "${SCRATCH_MNT}"
63
64 echo "+ check fs"
65 e2fsck -fn "${SCRATCH_DEV}" >> $seqres.full 2>&1 || _fail "fsck should not fail"
66
67 echo "+ corrupt image"
68 blk="$(debugfs -R "imap <$inode>" "${SCRATCH_DEV}" 2> /dev/null | grep located | sed -e 's/^.*block \([0-9]*\),.*$/\1/g')"
69 debugfs -w -R "zap_block ${blk}" "${SCRATCH_DEV}" >> $seqres.full 2>&1 || _fail "inode fuzz failed"
70
71 echo "+ mount image"
72 _scratch_mount
73
74 echo "+ modify files"
75 broken=0
76 for x in `seq 1 64`; do
77         #test -e "${TESTFILE}.${x}" || continue
78         stat "${TESTFILE}.${x}" >> $seqres.full 2>&1
79         test $? -ne 0 && broken=1
80         echo moo | dd oflag=append of="${TESTFILE}.${x}" 2>/dev/null
81         test $? -ne 0 && broken=1
82 done
83 echo "broken: ${broken}"
84 umount "${SCRATCH_MNT}"
85
86 echo "+ repair fs"
87 e2fsck -fy "${SCRATCH_DEV}" >> $seqres.full 2>&1
88
89 echo "+ mount image (2)"
90 _scratch_mount
91
92 echo "+ chattr -R -i"
93 $CHATTR_PROG -R -f -i "${SCRATCH_MNT}/"
94
95 echo "+ modify files (2)"
96 broken=0
97 for x in `seq 1 64`; do
98         test -e "${TESTFILE}.${x}" || continue
99         stat "${TESTFILE}.${x}" >> $seqres.full 2>&1
100         test $? -ne 0 && broken=1
101         echo moo | dd oflag=append of="${TESTFILE}.${x}" 2>/dev/null
102         test $? -ne 0 && broken=1
103 done
104 echo "broken: ${broken}"
105 umount "${SCRATCH_MNT}"
106
107 echo "+ check fs (2)"
108 e2fsck -fn "${SCRATCH_DEV}" >> $seqres.full 2>&1 || _fail "fsck should not fail"
109
110 status=0
111 exit