xfs/007: fix regressions on V4 filesystems
[xfstests-dev.git] / tests / xfs / 307
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 307
6 #
7 # Test recovery of "lost" CoW blocks:
8 # - Use the debugger to fake a leftover CoW extent
9 # - See if xfs_repair fixes it
10 #
11 . ./common/preamble
12 _begin_fstest auto quick clone
13
14 # Import common functions.
15 . ./common/filter
16 . ./common/reflink
17
18 # real QA test starts here
19 _supported_fs xfs
20 _require_scratch_reflink
21
22 echo "Format"
23 _scratch_mkfs > $seqres.full 2>&1
24 _scratch_mount >> $seqres.full
25 is_rmap=$($XFS_INFO_PROG $SCRATCH_MNT | grep -c "rmapbt=1")
26 _scratch_unmount
27
28 _get_agf_data() {
29         field="$1"
30         shift
31
32         _scratch_xfs_db -c 'agf 1' "$@" -c "p $field"  | awk '{print $3}'
33 }
34
35 _set_agf_data() {
36         field="$1"
37         value="$2"
38         shift; shift
39
40         _scratch_xfs_db -x -c 'agf 1' "$@" -c "write $field -- $value"  >> $seqres.full
41 }
42
43 _get_sb_data() {
44         _scratch_xfs_get_sb_field "$@"
45 }
46
47 _set_sb_data() {
48         _scratch_xfs_set_sb_field "$@" >> $seqres.full
49 }
50
51 _filter_leftover() {
52         grep "^leftover" | sed -e "s/[0-9]\+/NR/g"
53 }
54
55 _dump_status() {
56         echo "** " "$@"
57         _scratch_xfs_db -c 'sb 0' -c p
58         echo "** agf header"
59         _scratch_xfs_db -c 'agf 1' -c p
60         echo "** refcntbt"
61         _scratch_xfs_db -c 'agf 1' -c 'addr refcntroot' -c p
62         echo "** rmapbt"
63         test $is_rmap -gt 0 && _scratch_xfs_db -c 'agf 1' -c 'addr rmaproot' -c p
64         echo "** bnobt"
65         _scratch_xfs_db -c 'agf 1' -c 'addr bnoroot' -c p
66         echo "** cntbt"
67         _scratch_xfs_db -c 'agf 1' -c 'addr cntroot' -c p
68 }
69
70 echo "We need AG1 to have a single free extent"
71 bno_lvl=$(_get_agf_data level -c 'addr bnoroot')
72 bno_nr=$(_get_agf_data numrecs -c 'addr bnoroot')
73 refc_lvl=$(_get_agf_data level -c 'addr refcntroot')
74 refc_nr=$(_get_agf_data numrecs -c 'addr refcntroot')
75
76 test $bno_lvl -eq 0 || echo "  AG 1 bnobt must only have one level"
77 test $bno_nr -eq 1 || echo "  AG 1 bnobt must only have one record"
78 test $refc_lvl -eq 0 || echo "  AG 1 refcountbt must only have one level"
79 test $refc_nr -eq 0 || echo "  AG 1 refcountbt must only have one record"
80
81 if [ $is_rmap -gt 0 ]; then
82         rmap_lvl=$(_get_agf_data level -c 'addr rmaproot')
83         rmap_nr=$(_get_agf_data numrecs -c 'addr rmaproot')
84         test $rmap_lvl -eq 0 || echo "  AG 1 rmapbt must only have one level"
85 fi
86
87 echo "Find our extent and old counter values"
88 bno=$(_get_agf_data "recs[1].startblock" -c 'addr bnoroot')
89 len=$(_get_agf_data "recs[1].blockcount" -c 'addr bnoroot')
90 agf_freeblks=$(_get_agf_data freeblks)
91 sb_fdblocks=$(_get_sb_data fdblocks)
92
93 test $len -ge 200 || echo "  AG 1 doesn't have enough free blocks"
94
95 # Take the last 100 blocks of the free extent
96 debris_len=100
97 debris_bno=$((bno + len - debris_len))
98
99 echo "Remove the extent from the freesp btrees"
100 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr bnoroot'
101 _set_agf_data "recs[1].blockcount" $((len - debris_len)) -c 'addr cntroot'
102 _set_agf_data longest $((len - debris_len))
103 _set_agf_data freeblks $((agf_freeblks - debris_len))
104 _set_sb_data fdblocks $((sb_fdblocks - debris_len))
105
106 echo "Add the extent to the refcount btree"
107 _set_agf_data numrecs 1 -c 'addr refcntroot'
108 _set_agf_data "recs[1].startblock" $debris_bno -c 'addr refcntroot'
109 _set_agf_data "recs[1].blockcount" $debris_len -c 'addr refcntroot'
110 _set_agf_data "recs[1].refcount" 1 -c 'addr refcntroot'
111 _set_agf_data "recs[1].cowflag" 1 -c 'addr refcntroot'
112
113 if [ $is_rmap -gt 0 ]; then
114         rmap_nr=$((rmap_nr + 1))
115         _set_agf_data numrecs $rmap_nr -c 'addr rmaproot'
116         _set_agf_data "recs[$rmap_nr].startblock" $debris_bno -c 'addr rmaproot'
117         _set_agf_data "recs[$rmap_nr].blockcount" $debris_len -c 'addr rmaproot'
118         _set_agf_data "recs[$rmap_nr].owner" -9 -c 'addr rmaproot'
119         _set_agf_data "recs[$rmap_nr].offset" 0 -c 'addr rmaproot'
120 fi
121
122 _dump_status "broken fs config" >> $seqres.full
123
124 echo "Look for leftover warning in xfs_check"
125 _scratch_xfs_check | _filter_leftover
126
127 echo "Look for leftover warning in xfs_repair"
128 _scratch_xfs_repair -n 2>&1 | _filter_leftover
129
130 echo "Fix filesystem"
131 _scratch_xfs_repair >> $seqres.full 2>&1 || echo "xfs_repair failed?"
132
133 _dump_status "supposedly fixed fs config" >> $seqres.full
134
135 echo "Look for no more leftover warning in xfs_check"
136 _scratch_xfs_check | _filter_leftover
137
138 echo "Look for no more leftover warning in xfs_repair"
139 _scratch_xfs_repair -n 2>&1 | _filter_leftover
140
141 # success, all done
142 status=0
143 exit