2b5ad00570c681dfdcd5305fbdcbc80dc90eb967
[xfstests-dev.git] / tests / xfs / 010
1 #!/bin/bash
2 # FS QA Test No. xfs/010
3 #
4 # Test xfs_repair of the free inode btree (finobt). Make a couple targeted
5 # corruptions and verify that xfs_repair detects and repairs the filesystem to
6 # a consistent state.
7 #
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it would be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #
24 #-----------------------------------------------------------------------
25 #
26
27 seq=`basename $0`
28 seqres=$RESULT_DIR/$seq
29 echo "QA output created by $seq"
30
31 here=`pwd`
32 tmp=/tmp/$$
33 status=1        # failure is the default!
34
35 # get standard environment, filters and checks
36 . ./common/rc
37 . ./common/filter
38 . ./common/repair
39
40 _cleanup()
41 {
42         cd /
43         umount $SCRATCH_MNT 2>/dev/null
44         rm -f $tmp.*
45 }
46 trap "_cleanup; exit \$status" 0 1 2 3 15
47
48 _sparse_inode_populate()
49 {
50         dir=$1
51         count=$2
52
53         for i in $(seq 0 $count)
54         do
55                 touch $dir/$i
56         done
57
58         # Inode chunks are allocated 64 inodes at a time. If we remove 1 out of
59         # every 32 we allocated above, we'll end up leaving an inode or two free
60         # in each chunk. This ensures that most records are inserted into the
61         # finobt.
62         for i in $(seq 0 32 $count)
63         do
64                 rm -f $dir/$i
65         done
66 }
67
68 _filter_dbval()
69 {
70         awk '{ print $3 }'
71 }
72
73 _corrupt_finobt_records()
74 {
75         dev=$1
76
77         # determine the root block of the finobt
78         free_root=`$XFS_DB_PROG -c "agi 0" -c "p free_root" $dev |
79                         _filter_dbval`
80
81         # Corrupt a freecount value. This should never exceed 64.
82         $XFS_DB_PROG -x -c "fsb $free_root" -c "type inobt" \
83                 -c "write recs[1].freecount 70" $dev
84
85         # Create a corrupted non-free record, which should never appear in the
86         # finobt.
87         $XFS_DB_PROG -x -c "fsb $free_root" -c "type inobt" \
88                  -c "write recs[2].freecount 0" $dev
89         $XFS_DB_PROG -x -c "fsb $free_root" -c "type inobt" \
90                 -c "write recs[2].free 0" $dev
91 }
92
93 _corrupt_finobt_root()
94 {
95         dev=$1
96
97         # nuke the agi finobt root fields
98         $XFS_DB_PROG -x -c "agi 0" -c "write free_root 0" $dev
99         $XFS_DB_PROG -x -c "agi 0" -c "write free_level 0" $dev
100 }
101
102 # real QA test starts here
103 _supported_fs xfs
104 _supported_os Linux
105
106 _require_scratch
107 _require_xfs_mkfs_finobt
108 _require_xfs_finobt
109
110 rm -f $seqres.full
111
112 _scratch_mkfs_xfs "-m crc=1,finobt=1 -d agcount=2" | _filter_mkfs 2>$seqres.full
113
114 # sparsely populate the fs such that we create records with free inodes
115 _scratch_mount
116 _sparse_inode_populate $SCRATCH_MNT 999
117 umount $SCRATCH_MNT
118
119 # corrupt some finobt records
120 _corrupt_finobt_records $SCRATCH_DEV
121
122 # repair should detect the inconsistencies
123 _scratch_xfs_repair 2>&1 | _filter_repair
124 _check_scratch_fs
125
126 # nuke the finobt root, repair will have to regenerate from the inobt
127 _corrupt_finobt_root $SCRATCH_DEV
128
129 _scratch_xfs_repair 2>&1 | _filter_repair
130 _check_scratch_fs
131
132 status=0
133 exit