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