d8295245232c8b04ef85c2503ea1668688c983f1
[xfstests-dev.git] / tests / xfs / 307
1 #! /bin/bash
2 # FS QA Test No. 307
3 #
4 # Test recovery of "lost" CoW blocks:
5 # - Use the debugger to fake a leftover CoW extent
6 # - See if xfs_repair 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         _scratch_xfs_get_sb_field "$@"
75 }
76
77 _set_sb_data() {
78         _scratch_xfs_set_sb_field "$@" >> $seqres.full
79 }
80
81 _filter_leftover() {
82         grep "^leftover" | sed -e "s/[0-9]\+/NR/g"
83 }
84
85 _dump_status() {
86         echo "** " "$@"
87         _scratch_xfs_db -c 'sb 0' -c p
88         echo "** agf header"
89         _scratch_xfs_db -c 'agf 1' -c p
90         echo "** refcntbt"
91         _scratch_xfs_db -c 'agf 1' -c 'addr refcntroot' -c p
92         echo "** rmapbt"
93         test $is_rmap -gt 0 && _scratch_xfs_db -c 'agf 1' -c 'addr rmaproot' -c p
94         echo "** bnobt"
95         _scratch_xfs_db -c 'agf 1' -c 'addr bnoroot' -c p
96         echo "** cntbt"
97         _scratch_xfs_db -c 'agf 1' -c 'addr cntroot' -c p
98 }
99
100 echo "We need AG1 to have a single free extent"
101 bno_lvl=$(_get_agf_data level -c 'addr bnoroot')
102 bno_nr=$(_get_agf_data numrecs -c 'addr bnoroot')
103 refc_lvl=$(_get_agf_data level -c 'addr refcntroot')
104 refc_nr=$(_get_agf_data numrecs -c 'addr refcntroot')
105
106 test $bno_lvl -eq 0 || echo "  AG 1 bnobt must only have one level"
107 test $bno_nr -eq 1 || echo "  AG 1 bnobt must only have one record"
108 test $refc_lvl -eq 0 || echo "  AG 1 refcountbt must only have one level"
109 test $refc_nr -eq 0 || echo "  AG 1 refcountbt must only have one record"
110
111 if [ $is_rmap -gt 0 ]; then
112         rmap_lvl=$(_get_agf_data level -c 'addr rmaproot')
113         rmap_nr=$(_get_agf_data numrecs -c 'addr rmaproot')
114         test $rmap_lvl -eq 0 || echo "  AG 1 rmapbt must only have one level"
115 fi
116
117 echo "Find our extent and old counter values"
118 bno=$(_get_agf_data "recs[1].startblock" -c 'addr bnoroot')
119 len=$(_get_agf_data "recs[1].blockcount" -c 'addr bnoroot')
120 agf_freeblks=$(_get_agf_data freeblks)
121 sb_fdblocks=$(_get_sb_data fdblocks)
122
123 test $len -ge 200 || echo "  AG 1 doesn't have enough free blocks"
124
125 # Take the last 100 blocks of the free extent
126 debris_len=100
127 debris_bno=$((bno + len - debris_len))
128
129 echo "Remove the extent from the freesp btrees"
130 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr bnoroot'
131 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr cntroot'
132 _set_agf_data freeblks $((agf_freeblks - debris_len))
133 _set_agf_data longest $((len - debris_len))
134 _set_sb_data fdblocks $((sb_fdblocks - debris_len))
135
136 echo "Add the extent to the refcount btree"
137 _set_agf_data numrecs 1 -c 'addr refcntroot'
138 _set_agf_data "recs[1].startblock" $debris_bno -c 'addr refcntroot'
139 _set_agf_data "recs[1].blockcount" $debris_len -c 'addr refcntroot'
140 _set_agf_data "recs[1].refcount" 1 -c 'addr refcntroot'
141 _set_agf_data "recs[1].cowflag" 1 -c 'addr refcntroot'
142
143 if [ $is_rmap -gt 0 ]; then
144         rmap_nr=$((rmap_nr + 1))
145         _set_agf_data numrecs $rmap_nr -c 'addr rmaproot'
146         _set_agf_data "recs[$rmap_nr].startblock" $debris_bno -c 'addr rmaproot'
147         _set_agf_data "recs[$rmap_nr].blockcount" $debris_len -c 'addr rmaproot'
148         _set_agf_data "recs[$rmap_nr].owner" -9 -c 'addr rmaproot'
149         _set_agf_data "recs[$rmap_nr].offset" 0 -c 'addr rmaproot'
150 fi
151
152 _dump_status "broken fs config" >> $seqres.full
153
154 echo "Look for leftover warning in xfs_check"
155 _scratch_xfs_check | _filter_leftover
156
157 echo "Look for leftover warning in xfs_repair"
158 _scratch_xfs_repair -n 2>&1 | _filter_leftover
159
160 echo "Fix filesystem"
161 _scratch_xfs_repair >> $seqres.full 2>&1 || echo "xfs_repair failed?"
162
163 _dump_status "supposedly fixed fs config" >> $seqres.full
164
165 echo "Look for no more leftover warning in xfs_check"
166 _scratch_xfs_check | _filter_leftover
167
168 echo "Look for no more leftover warning in xfs_repair"
169 _scratch_xfs_repair -n 2>&1 | _filter_leftover
170
171 # success, all done
172 status=0
173 exit