1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022-2024 Oracle. All Rights Reserved.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_btree.h"
16 #include "xfs_buf_mem.h"
17 #include "xfs_btree_mem.h"
18 #include "xfs_error.h"
19 #include "scrub/scrub.h"
20 #include "scrub/rcbag_btree.h"
21 #include "scrub/rcbag.h"
22 #include "scrub/trace.h"
26 struct xfbtree xfbtree;
33 struct xfs_buftarg *btp,
39 bag = kzalloc(sizeof(struct rcbag), XCHK_GFP_FLAGS);
46 error = rcbagbt_mem_init(mp, &bag->xfbtree, btp);
62 struct rcbag *bag = *bagp;
64 xfbtree_destroy(&bag->xfbtree);
69 /* Track an rmap in the refcount bag. */
74 const struct xfs_rmap_irec *rmap)
76 struct rcbag_rec bagrec;
77 struct xfs_mount *mp = bag->mp;
78 struct xfs_btree_cur *cur;
82 cur = rcbagbt_mem_cursor(mp, tp, &bag->xfbtree);
83 error = rcbagbt_lookup_eq(cur, rmap, &has);
88 error = rcbagbt_get_rec(cur, &bagrec, &has);
92 error = -EFSCORRUPTED;
96 bagrec.rbg_refcount++;
97 error = rcbagbt_update(cur, &bagrec);
101 bagrec.rbg_startblock = rmap->rm_startblock;
102 bagrec.rbg_blockcount = rmap->rm_blockcount;
103 bagrec.rbg_refcount = 1;
105 error = rcbagbt_insert(cur, &bagrec, &has);
109 error = -EFSCORRUPTED;
114 xfs_btree_del_cursor(cur, 0);
116 error = xfbtree_trans_commit(&bag->xfbtree, tp);
124 xfs_btree_del_cursor(cur, error);
125 xfbtree_trans_cancel(&bag->xfbtree, tp);
129 /* Return the number of records in the bag. */
132 const struct rcbag *rcbag)
134 return rcbag->nr_items;
137 static inline uint32_t rcbag_rec_next_bno(const struct rcbag_rec *r)
139 return r->rbg_startblock + r->rbg_blockcount;
143 * Find the next block where the refcount changes, given the next rmap we
144 * looked at and the ones we're already tracking.
149 struct xfs_trans *tp,
150 const struct xfs_rmap_irec *next_rmap,
154 struct rcbag_rec bagrec;
155 struct xfs_mount *mp = bag->mp;
156 struct xfs_btree_cur *cur;
157 uint32_t next_bno = NULLAGBLOCK;
162 next_bno = next_rmap->rm_startblock;
164 cur = rcbagbt_mem_cursor(mp, tp, &bag->xfbtree);
165 error = xfs_btree_goto_left_edge(cur);
170 error = xfs_btree_increment(cur, 0, &has);
176 error = rcbagbt_get_rec(cur, &bagrec, &has);
180 error = -EFSCORRUPTED;
184 next_bno = min(next_bno, rcbag_rec_next_bno(&bagrec));
188 * We should have found /something/ because either next_rrm is the next
189 * interesting rmap to look at after emitting this refcount extent, or
190 * there are other rmaps in rmap_bag contributing to the current
191 * sharing count. But if something is seriously wrong, bail out.
193 if (next_bno == NULLAGBLOCK) {
194 error = -EFSCORRUPTED;
198 xfs_btree_del_cursor(cur, 0);
200 *next_bnop = next_bno;
204 xfs_btree_del_cursor(cur, error);
208 /* Pop all refcount bag records that end at next_bno */
210 rcbag_remove_ending_at(
212 struct xfs_trans *tp,
215 struct rcbag_rec bagrec;
216 struct xfs_mount *mp = bag->mp;
217 struct xfs_btree_cur *cur;
221 /* go to the right edge of the tree */
222 cur = rcbagbt_mem_cursor(mp, tp, &bag->xfbtree);
223 memset(&cur->bc_rec, 0xFF, sizeof(cur->bc_rec));
224 error = xfs_btree_lookup(cur, XFS_LOOKUP_GE, &has);
229 error = xfs_btree_decrement(cur, 0, &has);
235 error = rcbagbt_get_rec(cur, &bagrec, &has);
239 error = -EFSCORRUPTED;
243 if (rcbag_rec_next_bno(&bagrec) != next_bno)
246 error = xfs_btree_delete(cur, &has);
250 error = -EFSCORRUPTED;
254 bag->nr_items -= bagrec.rbg_refcount;
257 xfs_btree_del_cursor(cur, 0);
258 return xfbtree_trans_commit(&bag->xfbtree, tp);
260 xfs_btree_del_cursor(cur, error);
261 xfbtree_trans_cancel(&bag->xfbtree, tp);
265 /* Dump the rcbag. */
269 struct xfs_trans *tp)
271 struct rcbag_rec bagrec;
272 struct xfs_mount *mp = bag->mp;
273 struct xfs_btree_cur *cur;
274 unsigned long long nr = 0;
278 cur = rcbagbt_mem_cursor(mp, tp, &bag->xfbtree);
279 error = xfs_btree_goto_left_edge(cur);
284 error = xfs_btree_increment(cur, 0, &has);
290 error = rcbagbt_get_rec(cur, &bagrec, &has);
294 error = -EFSCORRUPTED;
298 xfs_err(bag->mp, "[%llu]: bno 0x%x fsbcount 0x%x refcount 0x%llx\n",
300 (unsigned int)bagrec.rbg_startblock,
301 (unsigned int)bagrec.rbg_blockcount,
302 (unsigned long long)bagrec.rbg_refcount);
306 xfs_btree_del_cursor(cur, error);