populate: add _require_populate_commands to check for tools
[xfstests-dev.git] / tests / xfs / 308
1 #! /bin/bash
2 # FS QA Test No. 308
3 #
4 # Test recovery of "lost" CoW blocks:
5 # - Use the debugger to fake a leftover CoW extent
6 # - See if mount/umount fixes it
7 #
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2016, Oracle and/or its affiliates.  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 seq=`basename $0`
26 seqres=$RESULT_DIR/$seq
27 echo "QA output created by $seq"
28
29 here=`pwd`
30 tmp=/tmp/$$
31 status=1    # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 _cleanup()
35 {
36         cd /
37         rm -rf $tmp.*
38 }
39
40 # get standard environment, filters and checks
41 . ./common/rc
42 . ./common/filter
43 . ./common/reflink
44
45 # real QA test starts here
46 _supported_os Linux
47 _supported_fs xfs
48 _require_scratch_reflink
49
50 rm -f $seqres.full
51
52 echo "Format"
53 _scratch_mkfs > $seqres.full 2>&1
54 _scratch_mount >> $seqres.full
55 is_rmap=$(xfs_info $SCRATCH_MNT | grep -c "rmapbt=1")
56 _scratch_unmount
57
58 _get_agf_data() {
59         field="$1"
60         shift
61
62         _scratch_xfs_db -c 'agf 1' "$@" -c "p $field"  | awk '{print $3}'
63 }
64
65 _set_agf_data() {
66         field="$1"
67         value="$2"
68         shift; shift
69
70         _scratch_xfs_db -x -c 'agf 1' "$@" -c "write $field -- $value"  >> $seqres.full
71 }
72
73 _get_sb_data() {
74         field="$1"
75         shift
76
77         _scratch_xfs_db -c 'sb 0' "$@" -c "p $field"  | awk '{print $3}'
78 }
79
80 _set_sb_data() {
81         field="$1"
82         value="$2"
83         shift; shift
84
85         _scratch_xfs_db -x -c 'sb 0' "$@" -c "write $field -- $value"  >> $seqres.full
86 }
87
88 _filter_leftover() {
89         grep "^leftover" | sed -e "s/[0-9]\+/NR/g"
90 }
91
92 _dump_status() {
93         echo "** " "$@"
94         _scratch_xfs_db -c 'sb 0' -c p
95         echo "** agf header"
96         _scratch_xfs_db -c 'agf 1' -c p
97         echo "** refcntbt"
98         _scratch_xfs_db -c 'agf 1' -c 'addr refcntroot' -c p
99         echo "** rmapbt"
100         test $is_rmap -gt 0 && _scratch_xfs_db -c 'agf 1' -c 'addr rmaproot' -c p
101         echo "** bnobt"
102         _scratch_xfs_db -c 'agf 1' -c 'addr bnoroot' -c p
103         echo "** cntbt"
104         _scratch_xfs_db -c 'agf 1' -c 'addr cntroot' -c p
105 }
106
107 echo "We need AG1 to have a single free extent"
108 bno_lvl=$(_get_agf_data level -c 'addr bnoroot')
109 bno_nr=$(_get_agf_data numrecs -c 'addr bnoroot')
110 refc_lvl=$(_get_agf_data level -c 'addr refcntroot')
111 refc_nr=$(_get_agf_data numrecs -c 'addr refcntroot')
112
113 test $bno_lvl -eq 0 || echo "  AG 1 bnobt must only have one level"
114 test $bno_nr -eq 1 || echo "  AG 1 bnobt must only have one record"
115 test $refc_lvl -eq 0 || echo "  AG 1 refcountbt must only have one level"
116 test $refc_nr -eq 0 || echo "  AG 1 refcountbt must only have one record"
117
118 if [ $is_rmap -gt 0 ]; then
119         rmap_lvl=$(_get_agf_data level -c 'addr rmaproot')
120         rmap_nr=$(_get_agf_data numrecs -c 'addr rmaproot')
121         test $rmap_lvl -eq 0 || echo "  AG 1 rmapbt must only have one level"
122 fi
123
124 echo "Find our extent and old counter values"
125 bno=$(_get_agf_data "recs[1].startblock" -c 'addr bnoroot')
126 len=$(_get_agf_data "recs[1].blockcount" -c 'addr bnoroot')
127 agf_freeblks=$(_get_agf_data freeblks)
128 sb_fdblocks=$(_get_sb_data fdblocks)
129
130 test $len -ge 200 || echo "  AG 1 doesn't have enough free blocks"
131
132 # Take the last 100 blocks of the free extent
133 debris_len=100
134 debris_bno=$((bno + len - debris_len))
135
136 echo "Remove the extent from the freesp btrees"
137 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr bnoroot'
138 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr cntroot'
139 _set_agf_data freeblks $((agf_freeblks - debris_len))
140 _set_agf_data longest $((len - debris_len))
141 _set_sb_data fdblocks $((sb_fdblocks - debris_len))
142
143 echo "Add the extent to the refcount btree"
144 _set_agf_data numrecs 1 -c 'addr refcntroot'
145 _set_agf_data "recs[1].startblock" $debris_bno -c 'addr refcntroot'
146 _set_agf_data "recs[1].blockcount" $debris_len -c 'addr refcntroot'
147 _set_agf_data "recs[1].refcount" 1 -c 'addr refcntroot'
148 _set_agf_data "recs[1].cowflag" 1 -c 'addr refcntroot'
149
150 if [ $is_rmap -gt 0 ]; then
151         rmap_nr=$((rmap_nr + 1))
152         _set_agf_data numrecs $rmap_nr -c 'addr rmaproot'
153         _set_agf_data "recs[$rmap_nr].startblock" $debris_bno -c 'addr rmaproot'
154         _set_agf_data "recs[$rmap_nr].blockcount" $debris_len -c 'addr rmaproot'
155         _set_agf_data "recs[$rmap_nr].owner" -9 -c 'addr rmaproot'
156         _set_agf_data "recs[$rmap_nr].offset" 0 -c 'addr rmaproot'
157 fi
158
159 _dump_status "broken fs config" >> $seqres.full
160
161 echo "Look for leftover warning in xfs_check"
162 _scratch_xfs_check | _filter_leftover
163
164 echo "Look for leftover warning in xfs_repair"
165 _scratch_xfs_repair -n 2>&1 | _filter_leftover
166
167 echo "Mount filesystem"
168 _scratch_mount
169 _scratch_unmount
170
171 _dump_status "supposedly fixed fs config" >> $seqres.full
172
173 echo "Look for no more leftover warning in xfs_check"
174 _scratch_xfs_check | _filter_leftover
175
176 echo "Look for no more leftover warning in xfs_repair"
177 _scratch_xfs_repair -n 2>&1 | _filter_leftover
178
179 # success, all done
180 status=0
181 exit