populate: add _require_populate_commands to check for tools
[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         _scratch_unmount 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                         \
99                 -c "agi 0"                      \
100                 -c "write -c free_root 0"       \
101                 -c "write -c free_level 0"      \
102                 $dev | grep -v "Allowing write of corrupted"
103 }
104
105 # real QA test starts here
106 _supported_fs xfs
107 _supported_os Linux
108
109 _require_scratch
110 _require_xfs_mkfs_finobt
111 _require_xfs_finobt
112
113 rm -f $seqres.full
114
115 _scratch_mkfs_xfs "-m crc=1,finobt=1 -d agcount=2" | _filter_mkfs 2>$seqres.full
116
117 # sparsely populate the fs such that we create records with free inodes
118 _scratch_mount
119 _sparse_inode_populate $SCRATCH_MNT 999
120 _scratch_unmount
121
122 # corrupt some finobt records
123 _corrupt_finobt_records $SCRATCH_DEV
124
125 # repair should detect the inconsistencies
126 _scratch_xfs_repair 2>&1 | _filter_repair
127 _check_scratch_fs
128
129 # nuke the finobt root, repair will have to regenerate from the inobt
130 _corrupt_finobt_root $SCRATCH_DEV
131
132 _scratch_xfs_repair 2>&1 | _filter_repair
133
134 status=0
135 exit