xfs: test inode allocation state missmatch corruption
[xfstests-dev.git] / tests / xfs / 490
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Red Hat Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 490
6 #
7 # Test a corruption when the directory structure and the inobt thinks the inode
8 # is free, but the inode on disk thinks it is still in use.
9 #
10 # This case test same bug (upstream linux commit ee457001ed6c) as xfs/132, but
11 # through different code path.
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 # remove previous $seqres.full before test
33 rm -f $seqres.full
34
35 # real QA test starts here
36
37 # Modify as appropriate.
38 _supported_fs xfs
39 _supported_os Linux
40 _require_scratch_nocheck
41 _require_xfs_mkfs_finobt
42
43 # Skip the verifier "xfs_check_agi_freecount()" which verify the number of free
44 # inodes in the AGI is correct, when XFS_DEBUG is enabled
45 _require_no_xfs_debug
46
47 filter_dmesg()
48 {
49         local warn1="Internal error xfs_trans_cancel.*fs/xfs/xfs_trans\.c.*"
50
51         sed -e "s#$warn1#Intentional error in xfs_trans_cancel#"
52 }
53
54 # If enable free inode B+tree, this case will fail on xfs_dialloc_ag_update_inobt,
55 # that's not what we want to test. Due to finobt feature is not necessary for this
56 # test, so disable it directly.
57 _scratch_mkfs_xfs -m finobt=0 | _filter_mkfs 2>$tmp.mkfs >> $seqres.full
58
59 # On V5 filesystem, this case can't trigger bug because it doesn't read inodes
60 # we are allocating from disk - it simply overwrites them with new inode
61 # information. So use ikeep mount option to stop that.
62 source $tmp.mkfs
63 mount_opt=""
64 if [ $_fs_has_crcs -eq 1 ]; then
65         mount_opt="-o ikeep"
66 fi
67
68 blksz=$(_scratch_xfs_get_sb_field blocksize)
69 agcount=$(_scratch_xfs_get_sb_field agcount)
70
71 _scratch_mount $mount_opt
72 # Create a directory for later allocation in same AG (AG 0, due to this's an
73 # empty XFS for now)
74 mkdir $SCRATCH_MNT/dir
75
76 # Allocate 1 block for testfile
77 $XFS_IO_PROG -fc "pwrite 0 $blksz" -c fsync $SCRATCH_MNT/dir/testfile >> $seqres.full
78 inum=`stat -c %i $SCRATCH_MNT/dir/testfile`
79 _scratch_unmount
80
81 # Find the AG which contains testfile
82 agi=`_scratch_xfs_db -c "convert inode $inum agno" | sed -e 's/^.*(\([0-9]*\).*$/\1/g'`
83
84 # Due to we only allocate 1 block for testfile, and this's the only one data
85 # block we use. So we use single level inobt, So the ${agi}->root->recs[1]
86 # should be the only one record points the chunk which contains testfile's
87 # inode.
88 # An exmaple of inode record is as below:
89 #   recs[1] = [startino,freecount,free] 1:[1024,59,0xffffffffffffffe0]
90 freecount=$(_scratch_xfs_get_metadata_field "recs[1].freecount" \
91                                             "agi $agi" "addr root")
92 fmask=$(_scratch_xfs_get_metadata_field "recs[1].free" "agi $agi" "addr root")
93
94 # fmask shift right 1 bit, and freecount++, to mark testfile inode as free in
95 # inobt. (But the inode itself isn't freed, it still has allocated block)
96 freecount="$((freecount + 1))"
97 fmask="$((fmask / 2))"
98 _scratch_xfs_set_metadata_field "recs[1].freecount" "$freecount" \
99                                 "agi $agi" "addr root" >/dev/null
100 _scratch_xfs_set_metadata_field "recs[1].free" "$fmask" \
101                                 "agi $agi" "addr root" >/dev/null
102
103 # Mount again and create a new inode cover that inode we just 'freed' from inobt
104 _scratch_mount $mount_opt
105 $XFS_IO_PROG -fc "pwrite 0 $blksz" -c fsync $SCRATCH_MNT/dir/newfile 2>&1 | \
106         grep -i "Structure needs cleaning" | _filter_scratch
107
108 # filter a intentional internal errors
109 _check_dmesg filter_dmesg
110
111 # success, all done
112 status=0
113 exit