xfs/007: fix regressions on V4 filesystems
[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 . ./common/preamble
14 _begin_fstest auto quick
15
16 # Import common functions.
17 . ./common/filter
18
19 # real QA test starts here
20
21 # Modify as appropriate.
22 _supported_fs xfs
23 _require_scratch_nocheck
24 _require_xfs_mkfs_finobt
25
26 # Skip the verifier "xfs_check_agi_freecount()" which verify the number of free
27 # inodes in the AGI is correct, when XFS_DEBUG is enabled
28 _require_no_xfs_debug
29
30 filter_dmesg()
31 {
32         local warn1="Internal error xfs_trans_cancel.*fs/xfs/xfs_trans\.c.*"
33
34         sed -e "s#$warn1#Intentional error in xfs_trans_cancel#"
35 }
36
37 # If enable free inode B+tree, this case will fail on xfs_dialloc_ag_update_inobt,
38 # that's not what we want to test. Due to finobt feature is not necessary for this
39 # test, so disable it directly.
40 _scratch_mkfs_xfs -m finobt=0 | _filter_mkfs 2>$tmp.mkfs >> $seqres.full
41
42 # On V5 filesystem, this case can't trigger bug because it doesn't read inodes
43 # we are allocating from disk - it simply overwrites them with new inode
44 # information. So use ikeep mount option to stop that.
45 source $tmp.mkfs
46 mount_opt=""
47 if [ $_fs_has_crcs -eq 1 ]; then
48         mount_opt="-o ikeep"
49 fi
50
51 blksz=$(_scratch_xfs_get_sb_field blocksize)
52 agcount=$(_scratch_xfs_get_sb_field agcount)
53
54 _scratch_mount $mount_opt
55 # Create a directory for later allocation in same AG (AG 0, due to this's an
56 # empty XFS for now)
57 mkdir $SCRATCH_MNT/dir
58
59 # Allocate 1 block for testfile
60 $XFS_IO_PROG -fc "pwrite 0 $blksz" -c fsync $SCRATCH_MNT/dir/testfile >> $seqres.full
61 inum=`stat -c %i $SCRATCH_MNT/dir/testfile`
62 _scratch_unmount
63
64 # Find the AG which contains testfile
65 agi=`_scratch_xfs_db -c "convert inode $inum agno" | sed -e 's/^.*(\([0-9]*\).*$/\1/g'`
66
67 # Due to we only allocate 1 block for testfile, and this's the only one data
68 # block we use. So we use single level inobt, So the ${agi}->root->recs[1]
69 # should be the only one record points the chunk which contains testfile's
70 # inode.
71 # An exmaple of inode record is as below:
72 #   recs[1] = [startino,freecount,free] 1:[1024,59,0xffffffffffffffe0]
73 freecount=$(_scratch_xfs_get_metadata_field "recs[1].freecount" \
74                                             "agi $agi" "addr root")
75 fmask=$(_scratch_xfs_get_metadata_field "recs[1].free" "agi $agi" "addr root")
76
77 # fmask shift right 1 bit, and freecount++, to mark testfile inode as free in
78 # inobt. (But the inode itself isn't freed, it still has allocated block)
79 freecount="$((freecount + 1))"
80 fmask="$((fmask / 2))"
81 _scratch_xfs_set_metadata_field "recs[1].freecount" "$freecount" \
82                                 "agi $agi" "addr root" >/dev/null
83 _scratch_xfs_set_metadata_field "recs[1].free" "$fmask" \
84                                 "agi $agi" "addr root" >/dev/null
85
86 # Mount again and create a new inode cover that inode we just 'freed' from inobt
87 _scratch_mount $mount_opt
88 $XFS_IO_PROG -fc "pwrite 0 $blksz" -c fsync $SCRATCH_MNT/dir/newfile 2>&1 | \
89         grep -i "Structure needs cleaning" | _filter_scratch
90
91 # filter a intentional internal errors
92 _check_dmesg filter_dmesg
93
94 # success, all done
95 status=0
96 exit