1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_btree.h"
17 #include "xfs_refcount_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trace.h"
22 #include "xfs_cksum.h"
23 #include "xfs_trans.h"
25 #include "xfs_refcount.h"
28 /* Allowable refcount adjustment amounts. */
29 enum xfs_refc_adjust_op {
30 XFS_REFCOUNT_ADJUST_INCREASE = 1,
31 XFS_REFCOUNT_ADJUST_DECREASE = -1,
32 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
33 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
36 STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
37 xfs_agblock_t agbno, xfs_extlen_t aglen,
38 struct xfs_defer_ops *dfops);
39 STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
40 xfs_agblock_t agbno, xfs_extlen_t aglen,
41 struct xfs_defer_ops *dfops);
44 * Look up the first record less than or equal to [bno, len] in the btree
48 xfs_refcount_lookup_le(
49 struct xfs_btree_cur *cur,
53 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
55 cur->bc_rec.rc.rc_startblock = bno;
56 cur->bc_rec.rc.rc_blockcount = 0;
57 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
61 * Look up the first record greater than or equal to [bno, len] in the btree
65 xfs_refcount_lookup_ge(
66 struct xfs_btree_cur *cur,
70 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
72 cur->bc_rec.rc.rc_startblock = bno;
73 cur->bc_rec.rc.rc_blockcount = 0;
74 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
78 * Look up the first record equal to [bno, len] in the btree
82 xfs_refcount_lookup_eq(
83 struct xfs_btree_cur *cur,
87 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
89 cur->bc_rec.rc.rc_startblock = bno;
90 cur->bc_rec.rc.rc_blockcount = 0;
91 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
94 /* Convert on-disk record to in-core format. */
96 xfs_refcount_btrec_to_irec(
97 union xfs_btree_rec *rec,
98 struct xfs_refcount_irec *irec)
100 irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock);
101 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
102 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
106 * Get the data from the pointed-to record.
109 xfs_refcount_get_rec(
110 struct xfs_btree_cur *cur,
111 struct xfs_refcount_irec *irec,
114 struct xfs_mount *mp = cur->bc_mp;
115 xfs_agnumber_t agno = cur->bc_private.a.agno;
116 union xfs_btree_rec *rec;
118 xfs_agblock_t realstart;
120 error = xfs_btree_get_rec(cur, &rec, stat);
124 xfs_refcount_btrec_to_irec(rec, irec);
126 agno = cur->bc_private.a.agno;
127 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
130 /* handle special COW-staging state */
131 realstart = irec->rc_startblock;
132 if (realstart & XFS_REFC_COW_START) {
133 if (irec->rc_refcount != 1)
135 realstart &= ~XFS_REFC_COW_START;
136 } else if (irec->rc_refcount < 2) {
140 /* check for valid extent range, including overflow */
141 if (!xfs_verify_agbno(mp, agno, realstart))
143 if (realstart > realstart + irec->rc_blockcount)
145 if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1))
148 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
151 trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno, irec);
156 "Refcount BTree record corruption in AG %d detected!", agno);
158 "Start block 0x%x, block count 0x%x, references 0x%x",
159 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
160 return -EFSCORRUPTED;
164 * Update the record referred to by cur to the value given
165 * by [bno, len, refcount].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
170 struct xfs_btree_cur *cur,
171 struct xfs_refcount_irec *irec)
173 union xfs_btree_rec rec;
176 trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec);
177 rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock);
178 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
179 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
180 error = xfs_btree_update(cur, &rec);
182 trace_xfs_refcount_update_error(cur->bc_mp,
183 cur->bc_private.a.agno, error, _RET_IP_);
188 * Insert the record referred to by cur to the value given
189 * by [bno, len, refcount].
190 * This either works (return 0) or gets an EFSCORRUPTED error.
194 struct xfs_btree_cur *cur,
195 struct xfs_refcount_irec *irec,
200 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec);
201 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
202 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
203 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
204 error = xfs_btree_insert(cur, i);
207 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
211 trace_xfs_refcount_insert_error(cur->bc_mp,
212 cur->bc_private.a.agno, error, _RET_IP_);
217 * Remove the record referred to by cur, then set the pointer to the spot
218 * where the record could be re-inserted, in case we want to increment or
219 * decrement the cursor.
220 * This either works (return 0) or gets an EFSCORRUPTED error.
224 struct xfs_btree_cur *cur,
227 struct xfs_refcount_irec irec;
231 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
234 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
235 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec);
236 error = xfs_btree_delete(cur, i);
237 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
240 error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec);
243 trace_xfs_refcount_delete_error(cur->bc_mp,
244 cur->bc_private.a.agno, error, _RET_IP_);
249 * Adjusting the Reference Count
251 * As stated elsewhere, the reference count btree (refcbt) stores
252 * >1 reference counts for extents of physical blocks. In this
253 * operation, we're either raising or lowering the reference count of
254 * some subrange stored in the tree:
256 * <------ adjustment range ------>
257 * ----+ +---+-----+ +--+--------+---------
258 * 2 | | 3 | 4 | |17| 55 | 10
259 * ----+ +---+-----+ +--+--------+---------
260 * X axis is physical blocks number;
261 * reference counts are the numbers inside the rectangles
263 * The first thing we need to do is to ensure that there are no
264 * refcount extents crossing either boundary of the range to be
265 * adjusted. For any extent that does cross a boundary, split it into
266 * two extents so that we can increment the refcount of one of the
269 * <------ adjustment range ------>
270 * ----+ +---+-----+ +--+--------+----+----
271 * 2 | | 3 | 2 | |17| 55 | 10 | 10
272 * ----+ +---+-----+ +--+--------+----+----
274 * For this next step, let's assume that all the physical blocks in
275 * the adjustment range are mapped to a file and are therefore in use
276 * at least once. Therefore, we can infer that any gap in the
277 * refcount tree within the adjustment range represents a physical
278 * extent with refcount == 1:
280 * <------ adjustment range ------>
281 * ----+---+---+-----+-+--+--------+----+----
282 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
283 * ----+---+---+-----+-+--+--------+----+----
286 * For each extent that falls within the interval range, figure out
287 * which extent is to the left or the right of that extent. Now we
288 * have a left, current, and right extent. If the new reference count
289 * of the center extent enables us to merge left, center, and right
290 * into one record covering all three, do so. If the center extent is
291 * at the left end of the range, abuts the left extent, and its new
292 * reference count matches the left extent's record, then merge them.
293 * If the center extent is at the right end of the range, abuts the
294 * right extent, and the reference counts match, merge those. In the
295 * example, we can left merge (assuming an increment operation):
297 * <------ adjustment range ------>
298 * --------+---+-----+-+--+--------+----+----
299 * 2 | 3 | 2 |1|17| 55 | 10 | 10
300 * --------+---+-----+-+--+--------+----+----
303 * For all other extents within the range, adjust the reference count
304 * or delete it if the refcount falls below 2. If we were
305 * incrementing, the end result looks like this:
307 * <------ adjustment range ------>
308 * --------+---+-----+-+--+--------+----+----
309 * 2 | 4 | 3 |2|18| 56 | 11 | 10
310 * --------+---+-----+-+--+--------+----+----
312 * The result of a decrement operation looks as such:
314 * <------ adjustment range ------>
315 * ----+ +---+ +--+--------+----+----
316 * 2 | | 2 | |16| 54 | 9 | 10
317 * ----+ +---+ +--+--------+----+----
320 * The blocks marked "D" are freed; the blocks marked "1" are only
321 * referenced once and therefore the record is removed from the
325 /* Next block after this extent. */
326 static inline xfs_agblock_t
328 struct xfs_refcount_irec *rc)
330 return rc->rc_startblock + rc->rc_blockcount;
334 * Split a refcount extent that crosses agbno.
337 xfs_refcount_split_extent(
338 struct xfs_btree_cur *cur,
342 struct xfs_refcount_irec rcext, tmp;
346 *shape_changed = false;
347 error = xfs_refcount_lookup_le(cur, agbno, &found_rec);
353 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
356 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
357 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
360 *shape_changed = true;
361 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_private.a.agno,
364 /* Establish the right extent. */
366 tmp.rc_startblock = agbno;
367 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
368 error = xfs_refcount_update(cur, &tmp);
372 /* Insert the left extent. */
374 tmp.rc_blockcount = agbno - rcext.rc_startblock;
375 error = xfs_refcount_insert(cur, &tmp, &found_rec);
378 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
382 trace_xfs_refcount_split_extent_error(cur->bc_mp,
383 cur->bc_private.a.agno, error, _RET_IP_);
388 * Merge the left, center, and right extents.
391 xfs_refcount_merge_center_extents(
392 struct xfs_btree_cur *cur,
393 struct xfs_refcount_irec *left,
394 struct xfs_refcount_irec *center,
395 struct xfs_refcount_irec *right,
396 unsigned long long extlen,
402 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
403 cur->bc_private.a.agno, left, center, right);
406 * Make sure the center and right extents are not in the btree.
407 * If the center extent was synthesized, the first delete call
408 * removes the right extent and we skip the second deletion.
409 * If center and right were in the btree, then the first delete
410 * call removes the center and the second one removes the right
413 error = xfs_refcount_lookup_ge(cur, center->rc_startblock,
417 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
419 error = xfs_refcount_delete(cur, &found_rec);
422 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
424 if (center->rc_refcount > 1) {
425 error = xfs_refcount_delete(cur, &found_rec);
428 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
432 /* Enlarge the left extent. */
433 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
437 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
439 left->rc_blockcount = extlen;
440 error = xfs_refcount_update(cur, left);
448 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
449 cur->bc_private.a.agno, error, _RET_IP_);
454 * Merge with the left extent.
457 xfs_refcount_merge_left_extent(
458 struct xfs_btree_cur *cur,
459 struct xfs_refcount_irec *left,
460 struct xfs_refcount_irec *cleft,
461 xfs_agblock_t *agbno,
467 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
468 cur->bc_private.a.agno, left, cleft);
470 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
471 if (cleft->rc_refcount > 1) {
472 error = xfs_refcount_lookup_le(cur, cleft->rc_startblock,
476 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
479 error = xfs_refcount_delete(cur, &found_rec);
482 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
486 /* Enlarge the left extent. */
487 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
491 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
493 left->rc_blockcount += cleft->rc_blockcount;
494 error = xfs_refcount_update(cur, left);
498 *agbno += cleft->rc_blockcount;
499 *aglen -= cleft->rc_blockcount;
503 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
504 cur->bc_private.a.agno, error, _RET_IP_);
509 * Merge with the right extent.
512 xfs_refcount_merge_right_extent(
513 struct xfs_btree_cur *cur,
514 struct xfs_refcount_irec *right,
515 struct xfs_refcount_irec *cright,
521 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
522 cur->bc_private.a.agno, cright, right);
525 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
528 if (cright->rc_refcount > 1) {
529 error = xfs_refcount_lookup_le(cur, cright->rc_startblock,
533 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
536 error = xfs_refcount_delete(cur, &found_rec);
539 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
543 /* Enlarge the right extent. */
544 error = xfs_refcount_lookup_le(cur, right->rc_startblock,
548 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
550 right->rc_startblock -= cright->rc_blockcount;
551 right->rc_blockcount += cright->rc_blockcount;
552 error = xfs_refcount_update(cur, right);
556 *aglen -= cright->rc_blockcount;
560 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
561 cur->bc_private.a.agno, error, _RET_IP_);
565 #define XFS_FIND_RCEXT_SHARED 1
566 #define XFS_FIND_RCEXT_COW 2
568 * Find the left extent and the one after it (cleft). This function assumes
569 * that we've already split any extent crossing agbno.
572 xfs_refcount_find_left_extents(
573 struct xfs_btree_cur *cur,
574 struct xfs_refcount_irec *left,
575 struct xfs_refcount_irec *cleft,
580 struct xfs_refcount_irec tmp;
584 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
585 error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec);
591 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
594 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
596 if (xfs_refc_next(&tmp) != agbno)
598 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
600 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
602 /* We have a left extent; retrieve (or invent) the next right one */
605 error = xfs_btree_increment(cur, 0, &found_rec);
609 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
612 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
615 /* if tmp starts at the end of our range, just use that */
616 if (tmp.rc_startblock == agbno)
620 * There's a gap in the refcntbt at the start of the
621 * range we're interested in (refcount == 1) so
622 * synthesize the implied extent and pass it back.
623 * We assume here that the agbno/aglen range was
624 * passed in from a data fork extent mapping and
625 * therefore is allocated to exactly one owner.
627 cleft->rc_startblock = agbno;
628 cleft->rc_blockcount = min(aglen,
629 tmp.rc_startblock - agbno);
630 cleft->rc_refcount = 1;
634 * No extents, so pretend that there's one covering the whole
637 cleft->rc_startblock = agbno;
638 cleft->rc_blockcount = aglen;
639 cleft->rc_refcount = 1;
641 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_private.a.agno,
646 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
647 cur->bc_private.a.agno, error, _RET_IP_);
652 * Find the right extent and the one before it (cright). This function
653 * assumes that we've already split any extents crossing agbno + aglen.
656 xfs_refcount_find_right_extents(
657 struct xfs_btree_cur *cur,
658 struct xfs_refcount_irec *right,
659 struct xfs_refcount_irec *cright,
664 struct xfs_refcount_irec tmp;
668 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
669 error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec);
675 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
678 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
680 if (tmp.rc_startblock != agbno + aglen)
682 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
684 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
686 /* We have a right extent; retrieve (or invent) the next left one */
689 error = xfs_btree_decrement(cur, 0, &found_rec);
693 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
696 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
699 /* if tmp ends at the end of our range, just use that */
700 if (xfs_refc_next(&tmp) == agbno + aglen)
704 * There's a gap in the refcntbt at the end of the
705 * range we're interested in (refcount == 1) so
706 * create the implied extent and pass it back.
707 * We assume here that the agbno/aglen range was
708 * passed in from a data fork extent mapping and
709 * therefore is allocated to exactly one owner.
711 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
712 cright->rc_blockcount = right->rc_startblock -
713 cright->rc_startblock;
714 cright->rc_refcount = 1;
718 * No extents, so pretend that there's one covering the whole
721 cright->rc_startblock = agbno;
722 cright->rc_blockcount = aglen;
723 cright->rc_refcount = 1;
725 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_private.a.agno,
726 cright, right, agbno + aglen);
730 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
731 cur->bc_private.a.agno, error, _RET_IP_);
735 /* Is this extent valid? */
738 struct xfs_refcount_irec *rc)
740 return rc->rc_startblock != NULLAGBLOCK;
744 * Try to merge with any extents on the boundaries of the adjustment range.
747 xfs_refcount_merge_extents(
748 struct xfs_btree_cur *cur,
749 xfs_agblock_t *agbno,
751 enum xfs_refc_adjust_op adjust,
755 struct xfs_refcount_irec left = {0}, cleft = {0};
756 struct xfs_refcount_irec cright = {0}, right = {0};
758 unsigned long long ulen;
761 *shape_changed = false;
763 * Find the extent just below agbno [left], just above agbno [cleft],
764 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
767 error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno,
771 error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno,
776 /* No left or right extent to merge; exit. */
777 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
780 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
781 (cleft.rc_blockcount == cright.rc_blockcount);
783 /* Try to merge left, cleft, and right. cleft must == cright. */
784 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount +
786 if (xfs_refc_valid(&left) && xfs_refc_valid(&right) &&
787 xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal &&
788 left.rc_refcount == cleft.rc_refcount + adjust &&
789 right.rc_refcount == cleft.rc_refcount + adjust &&
790 ulen < MAXREFCEXTLEN) {
791 *shape_changed = true;
792 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
793 &right, ulen, aglen);
796 /* Try to merge left and cleft. */
797 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount;
798 if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) &&
799 left.rc_refcount == cleft.rc_refcount + adjust &&
800 ulen < MAXREFCEXTLEN) {
801 *shape_changed = true;
802 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
808 * If we just merged left + cleft and cleft == cright,
809 * we no longer have a cright to merge with right. We're done.
815 /* Try to merge cright and right. */
816 ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount;
817 if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) &&
818 right.rc_refcount == cright.rc_refcount + adjust &&
819 ulen < MAXREFCEXTLEN) {
820 *shape_changed = true;
821 return xfs_refcount_merge_right_extent(cur, &right, &cright,
829 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
830 * true incorrectly is a shutdown FS; the penalty for guessing false
831 * incorrectly is more transaction rolls than might be necessary.
832 * Be conservative here.
835 xfs_refcount_still_have_space(
836 struct xfs_btree_cur *cur)
838 unsigned long overhead;
840 overhead = cur->bc_private.a.priv.refc.shape_changes *
841 xfs_allocfree_log_count(cur->bc_mp, 1);
842 overhead *= cur->bc_mp->m_sb.sb_blocksize;
845 * Only allow 2 refcount extent updates per transaction if the
846 * refcount continue update "error" has been injected.
848 if (cur->bc_private.a.priv.refc.nr_ops > 2 &&
849 XFS_TEST_ERROR(false, cur->bc_mp,
850 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
853 if (cur->bc_private.a.priv.refc.nr_ops == 0)
855 else if (overhead > cur->bc_tp->t_log_res)
857 return cur->bc_tp->t_log_res - overhead >
858 cur->bc_private.a.priv.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
862 * Adjust the refcounts of middle extents. At this point we should have
863 * split extents that crossed the adjustment range; merged with adjacent
864 * extents; and updated agbno/aglen to reflect the merges. Therefore,
865 * all we have to do is update the extents inside [agbno, agbno + aglen].
868 xfs_refcount_adjust_extents(
869 struct xfs_btree_cur *cur,
870 xfs_agblock_t *agbno,
872 enum xfs_refc_adjust_op adj,
873 struct xfs_defer_ops *dfops,
874 struct xfs_owner_info *oinfo)
876 struct xfs_refcount_irec ext, tmp;
878 int found_rec, found_tmp;
881 /* Merging did all the work already. */
885 error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec);
889 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
890 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
894 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
895 ext.rc_blockcount = 0;
900 * Deal with a hole in the refcount tree; if a file maps to
901 * these blocks and there's no refcountbt record, pretend that
902 * there is one with refcount == 1.
904 if (ext.rc_startblock != *agbno) {
905 tmp.rc_startblock = *agbno;
906 tmp.rc_blockcount = min(*aglen,
907 ext.rc_startblock - *agbno);
908 tmp.rc_refcount = 1 + adj;
909 trace_xfs_refcount_modify_extent(cur->bc_mp,
910 cur->bc_private.a.agno, &tmp);
913 * Either cover the hole (increment) or
914 * delete the range (decrement).
916 if (tmp.rc_refcount) {
917 error = xfs_refcount_insert(cur, &tmp,
921 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
922 found_tmp == 1, out_error);
923 cur->bc_private.a.priv.refc.nr_ops++;
925 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
926 cur->bc_private.a.agno,
928 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
929 tmp.rc_blockcount, oinfo);
932 (*agbno) += tmp.rc_blockcount;
933 (*aglen) -= tmp.rc_blockcount;
935 error = xfs_refcount_lookup_ge(cur, *agbno,
941 /* Stop if there's nothing left to modify */
942 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
946 * Adjust the reference count and either update the tree
947 * (incr) or free the blocks (decr).
949 if (ext.rc_refcount == MAXREFCOUNT)
951 ext.rc_refcount += adj;
952 trace_xfs_refcount_modify_extent(cur->bc_mp,
953 cur->bc_private.a.agno, &ext);
954 if (ext.rc_refcount > 1) {
955 error = xfs_refcount_update(cur, &ext);
958 cur->bc_private.a.priv.refc.nr_ops++;
959 } else if (ext.rc_refcount == 1) {
960 error = xfs_refcount_delete(cur, &found_rec);
963 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
964 found_rec == 1, out_error);
965 cur->bc_private.a.priv.refc.nr_ops++;
968 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
969 cur->bc_private.a.agno,
971 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
972 ext.rc_blockcount, oinfo);
976 error = xfs_btree_increment(cur, 0, &found_rec);
981 (*agbno) += ext.rc_blockcount;
982 (*aglen) -= ext.rc_blockcount;
987 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
988 cur->bc_private.a.agno, error, _RET_IP_);
992 /* Adjust the reference count of a range of AG blocks. */
995 struct xfs_btree_cur *cur,
998 xfs_agblock_t *new_agbno,
999 xfs_extlen_t *new_aglen,
1000 enum xfs_refc_adjust_op adj,
1001 struct xfs_defer_ops *dfops,
1002 struct xfs_owner_info *oinfo)
1005 int shape_changes = 0;
1010 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1011 trace_xfs_refcount_increase(cur->bc_mp, cur->bc_private.a.agno,
1014 trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_private.a.agno,
1018 * Ensure that no rcextents cross the boundary of the adjustment range.
1020 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1026 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1033 * Try to merge with the left or right extents of the range.
1035 error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj,
1036 XFS_FIND_RCEXT_SHARED, &shape_changed);
1042 cur->bc_private.a.priv.refc.shape_changes++;
1044 /* Now that we've taken care of the ends, adjust the middle extents */
1045 error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen,
1053 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_private.a.agno,
1058 /* Clean up after calling xfs_refcount_finish_one. */
1060 xfs_refcount_finish_one_cleanup(
1061 struct xfs_trans *tp,
1062 struct xfs_btree_cur *rcur,
1065 struct xfs_buf *agbp;
1069 agbp = rcur->bc_private.a.agbp;
1070 xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1072 xfs_trans_brelse(tp, agbp);
1076 * Process one of the deferred refcount operations. We pass back the
1077 * btree cursor to maintain our lock on the btree between calls.
1078 * This saves time and eliminates a buffer deadlock between the
1079 * superblock and the AGF because we'll always grab them in the same
1083 xfs_refcount_finish_one(
1084 struct xfs_trans *tp,
1085 struct xfs_defer_ops *dfops,
1086 enum xfs_refcount_intent_type type,
1087 xfs_fsblock_t startblock,
1088 xfs_extlen_t blockcount,
1089 xfs_fsblock_t *new_fsb,
1090 xfs_extlen_t *new_len,
1091 struct xfs_btree_cur **pcur)
1093 struct xfs_mount *mp = tp->t_mountp;
1094 struct xfs_btree_cur *rcur;
1095 struct xfs_buf *agbp = NULL;
1097 xfs_agnumber_t agno;
1099 xfs_agblock_t new_agbno;
1100 unsigned long nr_ops = 0;
1101 int shape_changes = 0;
1103 agno = XFS_FSB_TO_AGNO(mp, startblock);
1104 ASSERT(agno != NULLAGNUMBER);
1105 bno = XFS_FSB_TO_AGBNO(mp, startblock);
1107 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
1108 type, XFS_FSB_TO_AGBNO(mp, startblock),
1111 if (XFS_TEST_ERROR(false, mp,
1112 XFS_ERRTAG_REFCOUNT_FINISH_ONE))
1116 * If we haven't gotten a cursor or the cursor AG doesn't match
1117 * the startblock, get one now.
1120 if (rcur != NULL && rcur->bc_private.a.agno != agno) {
1121 nr_ops = rcur->bc_private.a.priv.refc.nr_ops;
1122 shape_changes = rcur->bc_private.a.priv.refc.shape_changes;
1123 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1128 error = xfs_alloc_read_agf(tp->t_mountp, tp, agno,
1129 XFS_ALLOC_FLAG_FREEING, &agbp);
1133 return -EFSCORRUPTED;
1135 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, dfops);
1140 rcur->bc_private.a.priv.refc.nr_ops = nr_ops;
1141 rcur->bc_private.a.priv.refc.shape_changes = shape_changes;
1146 case XFS_REFCOUNT_INCREASE:
1147 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1148 new_len, XFS_REFCOUNT_ADJUST_INCREASE, dfops, NULL);
1149 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1151 case XFS_REFCOUNT_DECREASE:
1152 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1153 new_len, XFS_REFCOUNT_ADJUST_DECREASE, dfops, NULL);
1154 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1156 case XFS_REFCOUNT_ALLOC_COW:
1157 *new_fsb = startblock + blockcount;
1159 error = __xfs_refcount_cow_alloc(rcur, bno, blockcount, dfops);
1161 case XFS_REFCOUNT_FREE_COW:
1162 *new_fsb = startblock + blockcount;
1164 error = __xfs_refcount_cow_free(rcur, bno, blockcount, dfops);
1168 error = -EFSCORRUPTED;
1170 if (!error && *new_len > 0)
1171 trace_xfs_refcount_finish_one_leftover(mp, agno, type,
1172 bno, blockcount, new_agbno, *new_len);
1176 xfs_trans_brelse(tp, agbp);
1182 * Record a refcount intent for later processing.
1186 struct xfs_mount *mp,
1187 struct xfs_defer_ops *dfops,
1188 enum xfs_refcount_intent_type type,
1189 xfs_fsblock_t startblock,
1190 xfs_extlen_t blockcount)
1192 struct xfs_refcount_intent *ri;
1194 trace_xfs_refcount_defer(mp, XFS_FSB_TO_AGNO(mp, startblock),
1195 type, XFS_FSB_TO_AGBNO(mp, startblock),
1198 ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
1199 KM_SLEEP | KM_NOFS);
1200 INIT_LIST_HEAD(&ri->ri_list);
1202 ri->ri_startblock = startblock;
1203 ri->ri_blockcount = blockcount;
1205 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
1210 * Increase the reference count of the blocks backing a file's extent.
1213 xfs_refcount_increase_extent(
1214 struct xfs_mount *mp,
1215 struct xfs_defer_ops *dfops,
1216 struct xfs_bmbt_irec *PREV)
1218 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1221 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_INCREASE,
1222 PREV->br_startblock, PREV->br_blockcount);
1226 * Decrease the reference count of the blocks backing a file's extent.
1229 xfs_refcount_decrease_extent(
1230 struct xfs_mount *mp,
1231 struct xfs_defer_ops *dfops,
1232 struct xfs_bmbt_irec *PREV)
1234 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1237 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_DECREASE,
1238 PREV->br_startblock, PREV->br_blockcount);
1242 * Given an AG extent, find the lowest-numbered run of shared blocks
1243 * within that range and return the range in fbno/flen. If
1244 * find_end_of_shared is set, return the longest contiguous extent of
1245 * shared blocks; if not, just return the first extent we find. If no
1246 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1247 * and 0, respectively.
1250 xfs_refcount_find_shared(
1251 struct xfs_btree_cur *cur,
1252 xfs_agblock_t agbno,
1254 xfs_agblock_t *fbno,
1256 bool find_end_of_shared)
1258 struct xfs_refcount_irec tmp;
1263 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_private.a.agno,
1266 /* By default, skip the whole range */
1267 *fbno = NULLAGBLOCK;
1270 /* Try to find a refcount extent that crosses the start */
1271 error = xfs_refcount_lookup_le(cur, agbno, &have);
1275 /* No left extent, look at the next one */
1276 error = xfs_btree_increment(cur, 0, &have);
1282 error = xfs_refcount_get_rec(cur, &tmp, &i);
1285 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1287 /* If the extent ends before the start, look at the next one */
1288 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1289 error = xfs_btree_increment(cur, 0, &have);
1294 error = xfs_refcount_get_rec(cur, &tmp, &i);
1297 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1300 /* If the extent starts after the range we want, bail out */
1301 if (tmp.rc_startblock >= agbno + aglen)
1304 /* We found the start of a shared extent! */
1305 if (tmp.rc_startblock < agbno) {
1306 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1307 tmp.rc_startblock = agbno;
1310 *fbno = tmp.rc_startblock;
1311 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1312 if (!find_end_of_shared)
1315 /* Otherwise, find the end of this shared extent */
1316 while (*fbno + *flen < agbno + aglen) {
1317 error = xfs_btree_increment(cur, 0, &have);
1322 error = xfs_refcount_get_rec(cur, &tmp, &i);
1325 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1326 if (tmp.rc_startblock >= agbno + aglen ||
1327 tmp.rc_startblock != *fbno + *flen)
1329 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1333 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1334 cur->bc_private.a.agno, *fbno, *flen);
1338 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1339 cur->bc_private.a.agno, error, _RET_IP_);
1344 * Recovering CoW Blocks After a Crash
1346 * Due to the way that the copy on write mechanism works, there's a window of
1347 * opportunity in which we can lose track of allocated blocks during a crash.
1348 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1349 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1350 * no longer in the free space btree but are not otherwise recorded anywhere
1351 * until the write completes and the blocks are mapped into the file. A crash
1352 * in between allocation and remapping results in the replacement blocks being
1353 * lost. This situation is exacerbated by the CoW extent size hint because
1354 * allocations can hang around for long time.
1356 * However, there is a place where we can record these allocations before they
1357 * become mappings -- the reference count btree. The btree does not record
1358 * extents with refcount == 1, so we can record allocations with a refcount of
1359 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1360 * no conflict with shared block records. These mappings should be created
1361 * when we allocate blocks to the CoW fork and deleted when they're removed
1362 * from the CoW fork.
1364 * Minor nit: records for in-progress CoW allocations and records for shared
1365 * extents must never be merged, to preserve the property that (except for CoW
1366 * allocations) there are no refcount btree entries with refcount == 1. The
1367 * only time this could potentially happen is when unsharing a block that's
1368 * adjacent to CoW allocations, so we must be careful to avoid this.
1370 * At mount time we recover lost CoW allocations by searching the refcount
1371 * btree for these refcount == 1 mappings. These represent CoW allocations
1372 * that were in progress at the time the filesystem went down, so we can free
1373 * them to get the space back.
1375 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1376 * several reasons -- first, EFIs pin the tail of the log and would have to be
1377 * periodically relogged to avoid filling up the log. Second, CoW completions
1378 * will have to file an EFD and create new EFIs for whatever remains in the
1379 * CoW fork; this partially takes care of (1) but extent-size reservations
1380 * will have to periodically relog even if there's no writeout in progress.
1381 * This can happen if the CoW extent size hint is set, which you really want.
1382 * Third, EFIs cannot currently be automatically relogged into newer
1383 * transactions to advance the log tail. Fourth, stuffing the log full of
1384 * EFIs places an upper bound on the number of CoW allocations that can be
1385 * held filesystem-wide at any given time. Recording them in the refcount
1386 * btree doesn't require us to maintain any state in memory and doesn't pin
1390 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1391 * in that they're not referenced anywhere else in the filesystem, so we
1392 * stash them in the refcount btree with a refcount of 1 until either file
1393 * remapping (or CoW cancellation) happens.
1396 xfs_refcount_adjust_cow_extents(
1397 struct xfs_btree_cur *cur,
1398 xfs_agblock_t agbno,
1400 enum xfs_refc_adjust_op adj)
1402 struct xfs_refcount_irec ext, tmp;
1404 int found_rec, found_tmp;
1409 /* Find any overlapping refcount records */
1410 error = xfs_refcount_lookup_ge(cur, agbno, &found_rec);
1413 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1417 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks +
1419 ext.rc_blockcount = 0;
1420 ext.rc_refcount = 0;
1424 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1425 /* Adding a CoW reservation, there should be nothing here. */
1426 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1427 ext.rc_startblock >= agbno + aglen, out_error);
1429 tmp.rc_startblock = agbno;
1430 tmp.rc_blockcount = aglen;
1431 tmp.rc_refcount = 1;
1432 trace_xfs_refcount_modify_extent(cur->bc_mp,
1433 cur->bc_private.a.agno, &tmp);
1435 error = xfs_refcount_insert(cur, &tmp,
1439 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1440 found_tmp == 1, out_error);
1442 case XFS_REFCOUNT_ADJUST_COW_FREE:
1443 /* Removing a CoW reservation, there should be one extent. */
1444 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1445 ext.rc_startblock == agbno, out_error);
1446 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1447 ext.rc_blockcount == aglen, out_error);
1448 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1449 ext.rc_refcount == 1, out_error);
1451 ext.rc_refcount = 0;
1452 trace_xfs_refcount_modify_extent(cur->bc_mp,
1453 cur->bc_private.a.agno, &ext);
1454 error = xfs_refcount_delete(cur, &found_rec);
1457 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1458 found_rec == 1, out_error);
1466 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1467 cur->bc_private.a.agno, error, _RET_IP_);
1472 * Add or remove refcount btree entries for CoW reservations.
1475 xfs_refcount_adjust_cow(
1476 struct xfs_btree_cur *cur,
1477 xfs_agblock_t agbno,
1479 enum xfs_refc_adjust_op adj)
1484 agbno += XFS_REFC_COW_START;
1487 * Ensure that no rcextents cross the boundary of the adjustment range.
1489 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1493 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1498 * Try to merge with the left or right extents of the range.
1500 error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj,
1501 XFS_FIND_RCEXT_COW, &shape_changed);
1505 /* Now that we've taken care of the ends, adjust the middle extents */
1506 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1513 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_private.a.agno,
1519 * Record a CoW allocation in the refcount btree.
1522 __xfs_refcount_cow_alloc(
1523 struct xfs_btree_cur *rcur,
1524 xfs_agblock_t agbno,
1526 struct xfs_defer_ops *dfops)
1528 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_private.a.agno,
1531 /* Add refcount btree reservation */
1532 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1533 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1537 * Remove a CoW allocation from the refcount btree.
1540 __xfs_refcount_cow_free(
1541 struct xfs_btree_cur *rcur,
1542 xfs_agblock_t agbno,
1544 struct xfs_defer_ops *dfops)
1546 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_private.a.agno,
1549 /* Remove refcount btree reservation */
1550 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1551 XFS_REFCOUNT_ADJUST_COW_FREE);
1554 /* Record a CoW staging extent in the refcount btree. */
1556 xfs_refcount_alloc_cow_extent(
1557 struct xfs_mount *mp,
1558 struct xfs_defer_ops *dfops,
1564 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1567 error = __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_ALLOC_COW,
1572 /* Add rmap entry */
1573 return xfs_rmap_alloc_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1574 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1577 /* Forget a CoW staging event in the refcount btree. */
1579 xfs_refcount_free_cow_extent(
1580 struct xfs_mount *mp,
1581 struct xfs_defer_ops *dfops,
1587 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1590 /* Remove rmap entry */
1591 error = xfs_rmap_free_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1592 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1596 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_FREE_COW,
1600 struct xfs_refcount_recovery {
1601 struct list_head rr_list;
1602 struct xfs_refcount_irec rr_rrec;
1605 /* Stuff an extent on the recovery list. */
1607 xfs_refcount_recover_extent(
1608 struct xfs_btree_cur *cur,
1609 union xfs_btree_rec *rec,
1612 struct list_head *debris = priv;
1613 struct xfs_refcount_recovery *rr;
1615 if (be32_to_cpu(rec->refc.rc_refcount) != 1)
1616 return -EFSCORRUPTED;
1618 rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), KM_SLEEP);
1619 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1620 list_add_tail(&rr->rr_list, debris);
1625 /* Find and remove leftover CoW reservations. */
1627 xfs_refcount_recover_cow_leftovers(
1628 struct xfs_mount *mp,
1629 xfs_agnumber_t agno)
1631 struct xfs_trans *tp;
1632 struct xfs_btree_cur *cur;
1633 struct xfs_buf *agbp;
1634 struct xfs_refcount_recovery *rr, *n;
1635 struct list_head debris;
1636 union xfs_btree_irec low;
1637 union xfs_btree_irec high;
1638 struct xfs_defer_ops dfops;
1640 xfs_agblock_t agbno;
1643 if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
1646 INIT_LIST_HEAD(&debris);
1649 * In this first part, we use an empty transaction to gather up
1650 * all the leftover CoW extents so that we can subsequently
1651 * delete them. The empty transaction is used to avoid
1652 * a buffer lock deadlock if there happens to be a loop in the
1653 * refcountbt because we're allowed to re-grab a buffer that is
1654 * already attached to our transaction. When we're done
1655 * recording the CoW debris we cancel the (empty) transaction
1656 * and everything goes away cleanly.
1658 error = xfs_trans_alloc_empty(mp, &tp);
1662 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
1669 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
1671 /* Find all the leftover CoW staging extents. */
1672 memset(&low, 0, sizeof(low));
1673 memset(&high, 0, sizeof(high));
1674 low.rc.rc_startblock = XFS_REFC_COW_START;
1675 high.rc.rc_startblock = -1U;
1676 error = xfs_btree_query_range(cur, &low, &high,
1677 xfs_refcount_recover_extent, &debris);
1680 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1681 xfs_trans_brelse(tp, agbp);
1682 xfs_trans_cancel(tp);
1684 /* Now iterate the list to free the leftovers */
1685 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1686 /* Set up transaction. */
1687 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1691 trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec);
1693 /* Free the orphan record */
1694 xfs_defer_init(&dfops, &fsb);
1695 agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START;
1696 fsb = XFS_AGB_TO_FSB(mp, agno, agbno);
1697 error = xfs_refcount_free_cow_extent(mp, &dfops, fsb,
1698 rr->rr_rrec.rc_blockcount);
1702 /* Free the block. */
1703 xfs_bmap_add_free(mp, &dfops, fsb,
1704 rr->rr_rrec.rc_blockcount, NULL);
1706 error = xfs_defer_finish(&tp, &dfops);
1710 error = xfs_trans_commit(tp);
1714 list_del(&rr->rr_list);
1720 xfs_defer_cancel(&dfops);
1722 xfs_trans_cancel(tp);
1724 /* Free the leftover list */
1725 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1726 list_del(&rr->rr_list);
1732 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1733 xfs_trans_brelse(tp, agbp);
1737 /* Is there a record covering a given extent? */
1739 xfs_refcount_has_record(
1740 struct xfs_btree_cur *cur,
1745 union xfs_btree_irec low;
1746 union xfs_btree_irec high;
1748 memset(&low, 0, sizeof(low));
1749 low.rc.rc_startblock = bno;
1750 memset(&high, 0xFF, sizeof(high));
1751 high.rc.rc_startblock = bno + len - 1;
1753 return xfs_btree_has_record(cur, &low, &high, exists);