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"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_btree.h"
16 #include "xfs_refcount_btree.h"
17 #include "xfs_alloc.h"
18 #include "xfs_errortag.h"
19 #include "xfs_error.h"
20 #include "xfs_trace.h"
21 #include "xfs_trans.h"
23 #include "xfs_refcount.h"
27 /* Allowable refcount adjustment amounts. */
28 enum xfs_refc_adjust_op {
29 XFS_REFCOUNT_ADJUST_INCREASE = 1,
30 XFS_REFCOUNT_ADJUST_DECREASE = -1,
31 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
32 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
35 STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
36 xfs_agblock_t agbno, xfs_extlen_t aglen);
37 STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
38 xfs_agblock_t agbno, xfs_extlen_t aglen);
41 * Look up the first record less than or equal to [bno, len] in the btree
45 xfs_refcount_lookup_le(
46 struct xfs_btree_cur *cur,
50 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno, bno,
52 cur->bc_rec.rc.rc_startblock = bno;
53 cur->bc_rec.rc.rc_blockcount = 0;
54 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
58 * Look up the first record greater than or equal to [bno, len] in the btree
62 xfs_refcount_lookup_ge(
63 struct xfs_btree_cur *cur,
67 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno, bno,
69 cur->bc_rec.rc.rc_startblock = bno;
70 cur->bc_rec.rc.rc_blockcount = 0;
71 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
75 * Look up the first record equal to [bno, len] in the btree
79 xfs_refcount_lookup_eq(
80 struct xfs_btree_cur *cur,
84 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno, bno,
86 cur->bc_rec.rc.rc_startblock = bno;
87 cur->bc_rec.rc.rc_blockcount = 0;
88 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
91 /* Convert on-disk record to in-core format. */
93 xfs_refcount_btrec_to_irec(
94 union xfs_btree_rec *rec,
95 struct xfs_refcount_irec *irec)
97 irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock);
98 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
99 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
103 * Get the data from the pointed-to record.
106 xfs_refcount_get_rec(
107 struct xfs_btree_cur *cur,
108 struct xfs_refcount_irec *irec,
111 struct xfs_mount *mp = cur->bc_mp;
112 xfs_agnumber_t agno = cur->bc_ag.pag->pag_agno;
113 union xfs_btree_rec *rec;
115 xfs_agblock_t realstart;
117 error = xfs_btree_get_rec(cur, &rec, stat);
121 xfs_refcount_btrec_to_irec(rec, irec);
123 agno = cur->bc_ag.pag->pag_agno;
124 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
127 /* handle special COW-staging state */
128 realstart = irec->rc_startblock;
129 if (realstart & XFS_REFC_COW_START) {
130 if (irec->rc_refcount != 1)
132 realstart &= ~XFS_REFC_COW_START;
133 } else if (irec->rc_refcount < 2) {
137 /* check for valid extent range, including overflow */
138 if (!xfs_verify_agbno(mp, agno, realstart))
140 if (realstart > realstart + irec->rc_blockcount)
142 if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1))
145 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
148 trace_xfs_refcount_get(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
153 "Refcount BTree record corruption in AG %d detected!", agno);
155 "Start block 0x%x, block count 0x%x, references 0x%x",
156 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
157 return -EFSCORRUPTED;
161 * Update the record referred to by cur to the value given
162 * by [bno, len, refcount].
163 * This either works (return 0) or gets an EFSCORRUPTED error.
167 struct xfs_btree_cur *cur,
168 struct xfs_refcount_irec *irec)
170 union xfs_btree_rec rec;
173 trace_xfs_refcount_update(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
174 rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock);
175 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
176 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
177 error = xfs_btree_update(cur, &rec);
179 trace_xfs_refcount_update_error(cur->bc_mp,
180 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
185 * Insert the record referred to by cur to the value given
186 * by [bno, len, refcount].
187 * This either works (return 0) or gets an EFSCORRUPTED error.
191 struct xfs_btree_cur *cur,
192 struct xfs_refcount_irec *irec,
197 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
198 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
199 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
200 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
201 error = xfs_btree_insert(cur, i);
204 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
205 error = -EFSCORRUPTED;
211 trace_xfs_refcount_insert_error(cur->bc_mp,
212 cur->bc_ag.pag->pag_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 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
235 error = -EFSCORRUPTED;
238 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_ag.pag->pag_agno, &irec);
239 error = xfs_btree_delete(cur, i);
240 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
241 error = -EFSCORRUPTED;
246 error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec);
249 trace_xfs_refcount_delete_error(cur->bc_mp,
250 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
255 * Adjusting the Reference Count
257 * As stated elsewhere, the reference count btree (refcbt) stores
258 * >1 reference counts for extents of physical blocks. In this
259 * operation, we're either raising or lowering the reference count of
260 * some subrange stored in the tree:
262 * <------ adjustment range ------>
263 * ----+ +---+-----+ +--+--------+---------
264 * 2 | | 3 | 4 | |17| 55 | 10
265 * ----+ +---+-----+ +--+--------+---------
266 * X axis is physical blocks number;
267 * reference counts are the numbers inside the rectangles
269 * The first thing we need to do is to ensure that there are no
270 * refcount extents crossing either boundary of the range to be
271 * adjusted. For any extent that does cross a boundary, split it into
272 * two extents so that we can increment the refcount of one of the
275 * <------ adjustment range ------>
276 * ----+ +---+-----+ +--+--------+----+----
277 * 2 | | 3 | 2 | |17| 55 | 10 | 10
278 * ----+ +---+-----+ +--+--------+----+----
280 * For this next step, let's assume that all the physical blocks in
281 * the adjustment range are mapped to a file and are therefore in use
282 * at least once. Therefore, we can infer that any gap in the
283 * refcount tree within the adjustment range represents a physical
284 * extent with refcount == 1:
286 * <------ adjustment range ------>
287 * ----+---+---+-----+-+--+--------+----+----
288 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
289 * ----+---+---+-----+-+--+--------+----+----
292 * For each extent that falls within the interval range, figure out
293 * which extent is to the left or the right of that extent. Now we
294 * have a left, current, and right extent. If the new reference count
295 * of the center extent enables us to merge left, center, and right
296 * into one record covering all three, do so. If the center extent is
297 * at the left end of the range, abuts the left extent, and its new
298 * reference count matches the left extent's record, then merge them.
299 * If the center extent is at the right end of the range, abuts the
300 * right extent, and the reference counts match, merge those. In the
301 * example, we can left merge (assuming an increment operation):
303 * <------ adjustment range ------>
304 * --------+---+-----+-+--+--------+----+----
305 * 2 | 3 | 2 |1|17| 55 | 10 | 10
306 * --------+---+-----+-+--+--------+----+----
309 * For all other extents within the range, adjust the reference count
310 * or delete it if the refcount falls below 2. If we were
311 * incrementing, the end result looks like this:
313 * <------ adjustment range ------>
314 * --------+---+-----+-+--+--------+----+----
315 * 2 | 4 | 3 |2|18| 56 | 11 | 10
316 * --------+---+-----+-+--+--------+----+----
318 * The result of a decrement operation looks as such:
320 * <------ adjustment range ------>
321 * ----+ +---+ +--+--------+----+----
322 * 2 | | 2 | |16| 54 | 9 | 10
323 * ----+ +---+ +--+--------+----+----
326 * The blocks marked "D" are freed; the blocks marked "1" are only
327 * referenced once and therefore the record is removed from the
331 /* Next block after this extent. */
332 static inline xfs_agblock_t
334 struct xfs_refcount_irec *rc)
336 return rc->rc_startblock + rc->rc_blockcount;
340 * Split a refcount extent that crosses agbno.
343 xfs_refcount_split_extent(
344 struct xfs_btree_cur *cur,
348 struct xfs_refcount_irec rcext, tmp;
352 *shape_changed = false;
353 error = xfs_refcount_lookup_le(cur, agbno, &found_rec);
359 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
362 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
363 error = -EFSCORRUPTED;
366 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
369 *shape_changed = true;
370 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
373 /* Establish the right extent. */
375 tmp.rc_startblock = agbno;
376 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
377 error = xfs_refcount_update(cur, &tmp);
381 /* Insert the left extent. */
383 tmp.rc_blockcount = agbno - rcext.rc_startblock;
384 error = xfs_refcount_insert(cur, &tmp, &found_rec);
387 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
388 error = -EFSCORRUPTED;
394 trace_xfs_refcount_split_extent_error(cur->bc_mp,
395 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
400 * Merge the left, center, and right extents.
403 xfs_refcount_merge_center_extents(
404 struct xfs_btree_cur *cur,
405 struct xfs_refcount_irec *left,
406 struct xfs_refcount_irec *center,
407 struct xfs_refcount_irec *right,
408 unsigned long long extlen,
414 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
415 cur->bc_ag.pag->pag_agno, left, center, right);
418 * Make sure the center and right extents are not in the btree.
419 * If the center extent was synthesized, the first delete call
420 * removes the right extent and we skip the second deletion.
421 * If center and right were in the btree, then the first delete
422 * call removes the center and the second one removes the right
425 error = xfs_refcount_lookup_ge(cur, center->rc_startblock,
429 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
430 error = -EFSCORRUPTED;
434 error = xfs_refcount_delete(cur, &found_rec);
437 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
438 error = -EFSCORRUPTED;
442 if (center->rc_refcount > 1) {
443 error = xfs_refcount_delete(cur, &found_rec);
446 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
447 error = -EFSCORRUPTED;
452 /* Enlarge the left extent. */
453 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
457 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
458 error = -EFSCORRUPTED;
462 left->rc_blockcount = extlen;
463 error = xfs_refcount_update(cur, left);
471 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
472 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
477 * Merge with the left extent.
480 xfs_refcount_merge_left_extent(
481 struct xfs_btree_cur *cur,
482 struct xfs_refcount_irec *left,
483 struct xfs_refcount_irec *cleft,
484 xfs_agblock_t *agbno,
490 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
491 cur->bc_ag.pag->pag_agno, left, cleft);
493 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
494 if (cleft->rc_refcount > 1) {
495 error = xfs_refcount_lookup_le(cur, cleft->rc_startblock,
499 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
500 error = -EFSCORRUPTED;
504 error = xfs_refcount_delete(cur, &found_rec);
507 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
508 error = -EFSCORRUPTED;
513 /* Enlarge the left extent. */
514 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
518 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
519 error = -EFSCORRUPTED;
523 left->rc_blockcount += cleft->rc_blockcount;
524 error = xfs_refcount_update(cur, left);
528 *agbno += cleft->rc_blockcount;
529 *aglen -= cleft->rc_blockcount;
533 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
534 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
539 * Merge with the right extent.
542 xfs_refcount_merge_right_extent(
543 struct xfs_btree_cur *cur,
544 struct xfs_refcount_irec *right,
545 struct xfs_refcount_irec *cright,
551 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
552 cur->bc_ag.pag->pag_agno, cright, right);
555 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
558 if (cright->rc_refcount > 1) {
559 error = xfs_refcount_lookup_le(cur, cright->rc_startblock,
563 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
564 error = -EFSCORRUPTED;
568 error = xfs_refcount_delete(cur, &found_rec);
571 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
572 error = -EFSCORRUPTED;
577 /* Enlarge the right extent. */
578 error = xfs_refcount_lookup_le(cur, right->rc_startblock,
582 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
583 error = -EFSCORRUPTED;
587 right->rc_startblock -= cright->rc_blockcount;
588 right->rc_blockcount += cright->rc_blockcount;
589 error = xfs_refcount_update(cur, right);
593 *aglen -= cright->rc_blockcount;
597 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
598 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
602 #define XFS_FIND_RCEXT_SHARED 1
603 #define XFS_FIND_RCEXT_COW 2
605 * Find the left extent and the one after it (cleft). This function assumes
606 * that we've already split any extent crossing agbno.
609 xfs_refcount_find_left_extents(
610 struct xfs_btree_cur *cur,
611 struct xfs_refcount_irec *left,
612 struct xfs_refcount_irec *cleft,
617 struct xfs_refcount_irec tmp;
621 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
622 error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec);
628 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
631 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
632 error = -EFSCORRUPTED;
636 if (xfs_refc_next(&tmp) != agbno)
638 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
640 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
642 /* We have a left extent; retrieve (or invent) the next right one */
645 error = xfs_btree_increment(cur, 0, &found_rec);
649 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
652 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
653 error = -EFSCORRUPTED;
657 /* if tmp starts at the end of our range, just use that */
658 if (tmp.rc_startblock == agbno)
662 * There's a gap in the refcntbt at the start of the
663 * range we're interested in (refcount == 1) so
664 * synthesize the implied extent and pass it back.
665 * We assume here that the agbno/aglen range was
666 * passed in from a data fork extent mapping and
667 * therefore is allocated to exactly one owner.
669 cleft->rc_startblock = agbno;
670 cleft->rc_blockcount = min(aglen,
671 tmp.rc_startblock - agbno);
672 cleft->rc_refcount = 1;
676 * No extents, so pretend that there's one covering the whole
679 cleft->rc_startblock = agbno;
680 cleft->rc_blockcount = aglen;
681 cleft->rc_refcount = 1;
683 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
688 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
689 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
694 * Find the right extent and the one before it (cright). This function
695 * assumes that we've already split any extents crossing agbno + aglen.
698 xfs_refcount_find_right_extents(
699 struct xfs_btree_cur *cur,
700 struct xfs_refcount_irec *right,
701 struct xfs_refcount_irec *cright,
706 struct xfs_refcount_irec tmp;
710 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
711 error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec);
717 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
720 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
721 error = -EFSCORRUPTED;
725 if (tmp.rc_startblock != agbno + aglen)
727 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
729 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
731 /* We have a right extent; retrieve (or invent) the next left one */
734 error = xfs_btree_decrement(cur, 0, &found_rec);
738 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
741 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
742 error = -EFSCORRUPTED;
746 /* if tmp ends at the end of our range, just use that */
747 if (xfs_refc_next(&tmp) == agbno + aglen)
751 * There's a gap in the refcntbt at the end of the
752 * range we're interested in (refcount == 1) so
753 * create the implied extent and pass it back.
754 * We assume here that the agbno/aglen range was
755 * passed in from a data fork extent mapping and
756 * therefore is allocated to exactly one owner.
758 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
759 cright->rc_blockcount = right->rc_startblock -
760 cright->rc_startblock;
761 cright->rc_refcount = 1;
765 * No extents, so pretend that there's one covering the whole
768 cright->rc_startblock = agbno;
769 cright->rc_blockcount = aglen;
770 cright->rc_refcount = 1;
772 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
773 cright, right, agbno + aglen);
777 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
778 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
782 /* Is this extent valid? */
785 struct xfs_refcount_irec *rc)
787 return rc->rc_startblock != NULLAGBLOCK;
791 * Try to merge with any extents on the boundaries of the adjustment range.
794 xfs_refcount_merge_extents(
795 struct xfs_btree_cur *cur,
796 xfs_agblock_t *agbno,
798 enum xfs_refc_adjust_op adjust,
802 struct xfs_refcount_irec left = {0}, cleft = {0};
803 struct xfs_refcount_irec cright = {0}, right = {0};
805 unsigned long long ulen;
808 *shape_changed = false;
810 * Find the extent just below agbno [left], just above agbno [cleft],
811 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
814 error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno,
818 error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno,
823 /* No left or right extent to merge; exit. */
824 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
827 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
828 (cleft.rc_blockcount == cright.rc_blockcount);
830 /* Try to merge left, cleft, and right. cleft must == cright. */
831 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount +
833 if (xfs_refc_valid(&left) && xfs_refc_valid(&right) &&
834 xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal &&
835 left.rc_refcount == cleft.rc_refcount + adjust &&
836 right.rc_refcount == cleft.rc_refcount + adjust &&
837 ulen < MAXREFCEXTLEN) {
838 *shape_changed = true;
839 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
840 &right, ulen, aglen);
843 /* Try to merge left and cleft. */
844 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount;
845 if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) &&
846 left.rc_refcount == cleft.rc_refcount + adjust &&
847 ulen < MAXREFCEXTLEN) {
848 *shape_changed = true;
849 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
855 * If we just merged left + cleft and cleft == cright,
856 * we no longer have a cright to merge with right. We're done.
862 /* Try to merge cright and right. */
863 ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount;
864 if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) &&
865 right.rc_refcount == cright.rc_refcount + adjust &&
866 ulen < MAXREFCEXTLEN) {
867 *shape_changed = true;
868 return xfs_refcount_merge_right_extent(cur, &right, &cright,
876 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
877 * true incorrectly is a shutdown FS; the penalty for guessing false
878 * incorrectly is more transaction rolls than might be necessary.
879 * Be conservative here.
882 xfs_refcount_still_have_space(
883 struct xfs_btree_cur *cur)
885 unsigned long overhead;
887 overhead = cur->bc_ag.refc.shape_changes *
888 xfs_allocfree_log_count(cur->bc_mp, 1);
889 overhead *= cur->bc_mp->m_sb.sb_blocksize;
892 * Only allow 2 refcount extent updates per transaction if the
893 * refcount continue update "error" has been injected.
895 if (cur->bc_ag.refc.nr_ops > 2 &&
896 XFS_TEST_ERROR(false, cur->bc_mp,
897 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
900 if (cur->bc_ag.refc.nr_ops == 0)
902 else if (overhead > cur->bc_tp->t_log_res)
904 return cur->bc_tp->t_log_res - overhead >
905 cur->bc_ag.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
909 * Adjust the refcounts of middle extents. At this point we should have
910 * split extents that crossed the adjustment range; merged with adjacent
911 * extents; and updated agbno/aglen to reflect the merges. Therefore,
912 * all we have to do is update the extents inside [agbno, agbno + aglen].
915 xfs_refcount_adjust_extents(
916 struct xfs_btree_cur *cur,
917 xfs_agblock_t *agbno,
919 enum xfs_refc_adjust_op adj,
920 struct xfs_owner_info *oinfo)
922 struct xfs_refcount_irec ext, tmp;
924 int found_rec, found_tmp;
927 /* Merging did all the work already. */
931 error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec);
935 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
936 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
940 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
941 ext.rc_blockcount = 0;
946 * Deal with a hole in the refcount tree; if a file maps to
947 * these blocks and there's no refcountbt record, pretend that
948 * there is one with refcount == 1.
950 if (ext.rc_startblock != *agbno) {
951 tmp.rc_startblock = *agbno;
952 tmp.rc_blockcount = min(*aglen,
953 ext.rc_startblock - *agbno);
954 tmp.rc_refcount = 1 + adj;
955 trace_xfs_refcount_modify_extent(cur->bc_mp,
956 cur->bc_ag.pag->pag_agno, &tmp);
959 * Either cover the hole (increment) or
960 * delete the range (decrement).
962 if (tmp.rc_refcount) {
963 error = xfs_refcount_insert(cur, &tmp,
967 if (XFS_IS_CORRUPT(cur->bc_mp,
969 error = -EFSCORRUPTED;
972 cur->bc_ag.refc.nr_ops++;
974 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
975 cur->bc_ag.pag->pag_agno,
977 xfs_bmap_add_free(cur->bc_tp, fsbno,
978 tmp.rc_blockcount, oinfo);
981 (*agbno) += tmp.rc_blockcount;
982 (*aglen) -= tmp.rc_blockcount;
984 error = xfs_refcount_lookup_ge(cur, *agbno,
990 /* Stop if there's nothing left to modify */
991 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
995 * Adjust the reference count and either update the tree
996 * (incr) or free the blocks (decr).
998 if (ext.rc_refcount == MAXREFCOUNT)
1000 ext.rc_refcount += adj;
1001 trace_xfs_refcount_modify_extent(cur->bc_mp,
1002 cur->bc_ag.pag->pag_agno, &ext);
1003 if (ext.rc_refcount > 1) {
1004 error = xfs_refcount_update(cur, &ext);
1007 cur->bc_ag.refc.nr_ops++;
1008 } else if (ext.rc_refcount == 1) {
1009 error = xfs_refcount_delete(cur, &found_rec);
1012 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1013 error = -EFSCORRUPTED;
1016 cur->bc_ag.refc.nr_ops++;
1019 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
1020 cur->bc_ag.pag->pag_agno,
1022 xfs_bmap_add_free(cur->bc_tp, fsbno, ext.rc_blockcount,
1027 error = xfs_btree_increment(cur, 0, &found_rec);
1032 (*agbno) += ext.rc_blockcount;
1033 (*aglen) -= ext.rc_blockcount;
1038 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1039 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1043 /* Adjust the reference count of a range of AG blocks. */
1045 xfs_refcount_adjust(
1046 struct xfs_btree_cur *cur,
1047 xfs_agblock_t agbno,
1049 xfs_agblock_t *new_agbno,
1050 xfs_extlen_t *new_aglen,
1051 enum xfs_refc_adjust_op adj,
1052 struct xfs_owner_info *oinfo)
1055 int shape_changes = 0;
1060 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1061 trace_xfs_refcount_increase(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1064 trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1068 * Ensure that no rcextents cross the boundary of the adjustment range.
1070 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1076 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1083 * Try to merge with the left or right extents of the range.
1085 error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj,
1086 XFS_FIND_RCEXT_SHARED, &shape_changed);
1092 cur->bc_ag.refc.shape_changes++;
1094 /* Now that we've taken care of the ends, adjust the middle extents */
1095 error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen,
1103 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1108 /* Clean up after calling xfs_refcount_finish_one. */
1110 xfs_refcount_finish_one_cleanup(
1111 struct xfs_trans *tp,
1112 struct xfs_btree_cur *rcur,
1115 struct xfs_buf *agbp;
1119 agbp = rcur->bc_ag.agbp;
1120 xfs_btree_del_cursor(rcur, error);
1122 xfs_trans_brelse(tp, agbp);
1126 * Process one of the deferred refcount operations. We pass back the
1127 * btree cursor to maintain our lock on the btree between calls.
1128 * This saves time and eliminates a buffer deadlock between the
1129 * superblock and the AGF because we'll always grab them in the same
1133 xfs_refcount_finish_one(
1134 struct xfs_trans *tp,
1135 enum xfs_refcount_intent_type type,
1136 xfs_fsblock_t startblock,
1137 xfs_extlen_t blockcount,
1138 xfs_fsblock_t *new_fsb,
1139 xfs_extlen_t *new_len,
1140 struct xfs_btree_cur **pcur)
1142 struct xfs_mount *mp = tp->t_mountp;
1143 struct xfs_btree_cur *rcur;
1144 struct xfs_buf *agbp = NULL;
1147 xfs_agblock_t new_agbno;
1148 unsigned long nr_ops = 0;
1149 int shape_changes = 0;
1150 struct xfs_perag *pag;
1152 pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, startblock));
1153 bno = XFS_FSB_TO_AGBNO(mp, startblock);
1155 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
1156 type, XFS_FSB_TO_AGBNO(mp, startblock),
1159 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REFCOUNT_FINISH_ONE)) {
1165 * If we haven't gotten a cursor or the cursor AG doesn't match
1166 * the startblock, get one now.
1169 if (rcur != NULL && rcur->bc_ag.pag != pag) {
1170 nr_ops = rcur->bc_ag.refc.nr_ops;
1171 shape_changes = rcur->bc_ag.refc.shape_changes;
1172 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1177 error = xfs_alloc_read_agf(tp->t_mountp, tp, pag->pag_agno,
1178 XFS_ALLOC_FLAG_FREEING, &agbp);
1182 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1183 rcur->bc_ag.refc.nr_ops = nr_ops;
1184 rcur->bc_ag.refc.shape_changes = shape_changes;
1189 case XFS_REFCOUNT_INCREASE:
1190 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1191 new_len, XFS_REFCOUNT_ADJUST_INCREASE, NULL);
1192 *new_fsb = XFS_AGB_TO_FSB(mp, pag->pag_agno, new_agbno);
1194 case XFS_REFCOUNT_DECREASE:
1195 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1196 new_len, XFS_REFCOUNT_ADJUST_DECREASE, NULL);
1197 *new_fsb = XFS_AGB_TO_FSB(mp, pag->pag_agno, new_agbno);
1199 case XFS_REFCOUNT_ALLOC_COW:
1200 *new_fsb = startblock + blockcount;
1202 error = __xfs_refcount_cow_alloc(rcur, bno, blockcount);
1204 case XFS_REFCOUNT_FREE_COW:
1205 *new_fsb = startblock + blockcount;
1207 error = __xfs_refcount_cow_free(rcur, bno, blockcount);
1211 error = -EFSCORRUPTED;
1213 if (!error && *new_len > 0)
1214 trace_xfs_refcount_finish_one_leftover(mp, pag->pag_agno, type,
1215 bno, blockcount, new_agbno, *new_len);
1222 * Record a refcount intent for later processing.
1226 struct xfs_trans *tp,
1227 enum xfs_refcount_intent_type type,
1228 xfs_fsblock_t startblock,
1229 xfs_extlen_t blockcount)
1231 struct xfs_refcount_intent *ri;
1233 trace_xfs_refcount_defer(tp->t_mountp,
1234 XFS_FSB_TO_AGNO(tp->t_mountp, startblock),
1235 type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
1238 ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
1240 INIT_LIST_HEAD(&ri->ri_list);
1242 ri->ri_startblock = startblock;
1243 ri->ri_blockcount = blockcount;
1245 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
1249 * Increase the reference count of the blocks backing a file's extent.
1252 xfs_refcount_increase_extent(
1253 struct xfs_trans *tp,
1254 struct xfs_bmbt_irec *PREV)
1256 if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb))
1259 __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, PREV->br_startblock,
1260 PREV->br_blockcount);
1264 * Decrease the reference count of the blocks backing a file's extent.
1267 xfs_refcount_decrease_extent(
1268 struct xfs_trans *tp,
1269 struct xfs_bmbt_irec *PREV)
1271 if (!xfs_sb_version_hasreflink(&tp->t_mountp->m_sb))
1274 __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, PREV->br_startblock,
1275 PREV->br_blockcount);
1279 * Given an AG extent, find the lowest-numbered run of shared blocks
1280 * within that range and return the range in fbno/flen. If
1281 * find_end_of_shared is set, return the longest contiguous extent of
1282 * shared blocks; if not, just return the first extent we find. If no
1283 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1284 * and 0, respectively.
1287 xfs_refcount_find_shared(
1288 struct xfs_btree_cur *cur,
1289 xfs_agblock_t agbno,
1291 xfs_agblock_t *fbno,
1293 bool find_end_of_shared)
1295 struct xfs_refcount_irec tmp;
1300 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1303 /* By default, skip the whole range */
1304 *fbno = NULLAGBLOCK;
1307 /* Try to find a refcount extent that crosses the start */
1308 error = xfs_refcount_lookup_le(cur, agbno, &have);
1312 /* No left extent, look at the next one */
1313 error = xfs_btree_increment(cur, 0, &have);
1319 error = xfs_refcount_get_rec(cur, &tmp, &i);
1322 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1323 error = -EFSCORRUPTED;
1327 /* If the extent ends before the start, look at the next one */
1328 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1329 error = xfs_btree_increment(cur, 0, &have);
1334 error = xfs_refcount_get_rec(cur, &tmp, &i);
1337 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1338 error = -EFSCORRUPTED;
1343 /* If the extent starts after the range we want, bail out */
1344 if (tmp.rc_startblock >= agbno + aglen)
1347 /* We found the start of a shared extent! */
1348 if (tmp.rc_startblock < agbno) {
1349 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1350 tmp.rc_startblock = agbno;
1353 *fbno = tmp.rc_startblock;
1354 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1355 if (!find_end_of_shared)
1358 /* Otherwise, find the end of this shared extent */
1359 while (*fbno + *flen < agbno + aglen) {
1360 error = xfs_btree_increment(cur, 0, &have);
1365 error = xfs_refcount_get_rec(cur, &tmp, &i);
1368 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1369 error = -EFSCORRUPTED;
1372 if (tmp.rc_startblock >= agbno + aglen ||
1373 tmp.rc_startblock != *fbno + *flen)
1375 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1379 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1380 cur->bc_ag.pag->pag_agno, *fbno, *flen);
1384 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1385 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1390 * Recovering CoW Blocks After a Crash
1392 * Due to the way that the copy on write mechanism works, there's a window of
1393 * opportunity in which we can lose track of allocated blocks during a crash.
1394 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1395 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1396 * no longer in the free space btree but are not otherwise recorded anywhere
1397 * until the write completes and the blocks are mapped into the file. A crash
1398 * in between allocation and remapping results in the replacement blocks being
1399 * lost. This situation is exacerbated by the CoW extent size hint because
1400 * allocations can hang around for long time.
1402 * However, there is a place where we can record these allocations before they
1403 * become mappings -- the reference count btree. The btree does not record
1404 * extents with refcount == 1, so we can record allocations with a refcount of
1405 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1406 * no conflict with shared block records. These mappings should be created
1407 * when we allocate blocks to the CoW fork and deleted when they're removed
1408 * from the CoW fork.
1410 * Minor nit: records for in-progress CoW allocations and records for shared
1411 * extents must never be merged, to preserve the property that (except for CoW
1412 * allocations) there are no refcount btree entries with refcount == 1. The
1413 * only time this could potentially happen is when unsharing a block that's
1414 * adjacent to CoW allocations, so we must be careful to avoid this.
1416 * At mount time we recover lost CoW allocations by searching the refcount
1417 * btree for these refcount == 1 mappings. These represent CoW allocations
1418 * that were in progress at the time the filesystem went down, so we can free
1419 * them to get the space back.
1421 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1422 * several reasons -- first, EFIs pin the tail of the log and would have to be
1423 * periodically relogged to avoid filling up the log. Second, CoW completions
1424 * will have to file an EFD and create new EFIs for whatever remains in the
1425 * CoW fork; this partially takes care of (1) but extent-size reservations
1426 * will have to periodically relog even if there's no writeout in progress.
1427 * This can happen if the CoW extent size hint is set, which you really want.
1428 * Third, EFIs cannot currently be automatically relogged into newer
1429 * transactions to advance the log tail. Fourth, stuffing the log full of
1430 * EFIs places an upper bound on the number of CoW allocations that can be
1431 * held filesystem-wide at any given time. Recording them in the refcount
1432 * btree doesn't require us to maintain any state in memory and doesn't pin
1436 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1437 * in that they're not referenced anywhere else in the filesystem, so we
1438 * stash them in the refcount btree with a refcount of 1 until either file
1439 * remapping (or CoW cancellation) happens.
1442 xfs_refcount_adjust_cow_extents(
1443 struct xfs_btree_cur *cur,
1444 xfs_agblock_t agbno,
1446 enum xfs_refc_adjust_op adj)
1448 struct xfs_refcount_irec ext, tmp;
1450 int found_rec, found_tmp;
1455 /* Find any overlapping refcount records */
1456 error = xfs_refcount_lookup_ge(cur, agbno, &found_rec);
1459 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1463 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks +
1465 ext.rc_blockcount = 0;
1466 ext.rc_refcount = 0;
1470 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1471 /* Adding a CoW reservation, there should be nothing here. */
1472 if (XFS_IS_CORRUPT(cur->bc_mp,
1473 agbno + aglen > ext.rc_startblock)) {
1474 error = -EFSCORRUPTED;
1478 tmp.rc_startblock = agbno;
1479 tmp.rc_blockcount = aglen;
1480 tmp.rc_refcount = 1;
1481 trace_xfs_refcount_modify_extent(cur->bc_mp,
1482 cur->bc_ag.pag->pag_agno, &tmp);
1484 error = xfs_refcount_insert(cur, &tmp,
1488 if (XFS_IS_CORRUPT(cur->bc_mp, found_tmp != 1)) {
1489 error = -EFSCORRUPTED;
1493 case XFS_REFCOUNT_ADJUST_COW_FREE:
1494 /* Removing a CoW reservation, there should be one extent. */
1495 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_startblock != agbno)) {
1496 error = -EFSCORRUPTED;
1499 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount != aglen)) {
1500 error = -EFSCORRUPTED;
1503 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_refcount != 1)) {
1504 error = -EFSCORRUPTED;
1508 ext.rc_refcount = 0;
1509 trace_xfs_refcount_modify_extent(cur->bc_mp,
1510 cur->bc_ag.pag->pag_agno, &ext);
1511 error = xfs_refcount_delete(cur, &found_rec);
1514 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1515 error = -EFSCORRUPTED;
1525 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1526 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1531 * Add or remove refcount btree entries for CoW reservations.
1534 xfs_refcount_adjust_cow(
1535 struct xfs_btree_cur *cur,
1536 xfs_agblock_t agbno,
1538 enum xfs_refc_adjust_op adj)
1543 agbno += XFS_REFC_COW_START;
1546 * Ensure that no rcextents cross the boundary of the adjustment range.
1548 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1552 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1557 * Try to merge with the left or right extents of the range.
1559 error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj,
1560 XFS_FIND_RCEXT_COW, &shape_changed);
1564 /* Now that we've taken care of the ends, adjust the middle extents */
1565 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1572 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1578 * Record a CoW allocation in the refcount btree.
1581 __xfs_refcount_cow_alloc(
1582 struct xfs_btree_cur *rcur,
1583 xfs_agblock_t agbno,
1586 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1589 /* Add refcount btree reservation */
1590 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1591 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1595 * Remove a CoW allocation from the refcount btree.
1598 __xfs_refcount_cow_free(
1599 struct xfs_btree_cur *rcur,
1600 xfs_agblock_t agbno,
1603 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1606 /* Remove refcount btree reservation */
1607 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1608 XFS_REFCOUNT_ADJUST_COW_FREE);
1611 /* Record a CoW staging extent in the refcount btree. */
1613 xfs_refcount_alloc_cow_extent(
1614 struct xfs_trans *tp,
1618 struct xfs_mount *mp = tp->t_mountp;
1620 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1623 __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len);
1625 /* Add rmap entry */
1626 xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1627 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1630 /* Forget a CoW staging event in the refcount btree. */
1632 xfs_refcount_free_cow_extent(
1633 struct xfs_trans *tp,
1637 struct xfs_mount *mp = tp->t_mountp;
1639 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1642 /* Remove rmap entry */
1643 xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1644 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1645 __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len);
1648 struct xfs_refcount_recovery {
1649 struct list_head rr_list;
1650 struct xfs_refcount_irec rr_rrec;
1653 /* Stuff an extent on the recovery list. */
1655 xfs_refcount_recover_extent(
1656 struct xfs_btree_cur *cur,
1657 union xfs_btree_rec *rec,
1660 struct list_head *debris = priv;
1661 struct xfs_refcount_recovery *rr;
1663 if (XFS_IS_CORRUPT(cur->bc_mp,
1664 be32_to_cpu(rec->refc.rc_refcount) != 1))
1665 return -EFSCORRUPTED;
1667 rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), 0);
1668 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1669 list_add_tail(&rr->rr_list, debris);
1674 /* Find and remove leftover CoW reservations. */
1676 xfs_refcount_recover_cow_leftovers(
1677 struct xfs_mount *mp,
1678 struct xfs_perag *pag)
1680 struct xfs_trans *tp;
1681 struct xfs_btree_cur *cur;
1682 struct xfs_buf *agbp;
1683 struct xfs_refcount_recovery *rr, *n;
1684 struct list_head debris;
1685 union xfs_btree_irec low;
1686 union xfs_btree_irec high;
1688 xfs_agblock_t agbno;
1691 if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
1694 INIT_LIST_HEAD(&debris);
1697 * In this first part, we use an empty transaction to gather up
1698 * all the leftover CoW extents so that we can subsequently
1699 * delete them. The empty transaction is used to avoid
1700 * a buffer lock deadlock if there happens to be a loop in the
1701 * refcountbt because we're allowed to re-grab a buffer that is
1702 * already attached to our transaction. When we're done
1703 * recording the CoW debris we cancel the (empty) transaction
1704 * and everything goes away cleanly.
1706 error = xfs_trans_alloc_empty(mp, &tp);
1710 error = xfs_alloc_read_agf(mp, tp, pag->pag_agno, 0, &agbp);
1713 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1715 /* Find all the leftover CoW staging extents. */
1716 memset(&low, 0, sizeof(low));
1717 memset(&high, 0, sizeof(high));
1718 low.rc.rc_startblock = XFS_REFC_COW_START;
1719 high.rc.rc_startblock = -1U;
1720 error = xfs_btree_query_range(cur, &low, &high,
1721 xfs_refcount_recover_extent, &debris);
1722 xfs_btree_del_cursor(cur, error);
1723 xfs_trans_brelse(tp, agbp);
1724 xfs_trans_cancel(tp);
1728 /* Now iterate the list to free the leftovers */
1729 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1730 /* Set up transaction. */
1731 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1735 trace_xfs_refcount_recover_extent(mp, pag->pag_agno,
1738 /* Free the orphan record */
1739 agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START;
1740 fsb = XFS_AGB_TO_FSB(mp, pag->pag_agno, agbno);
1741 xfs_refcount_free_cow_extent(tp, fsb,
1742 rr->rr_rrec.rc_blockcount);
1744 /* Free the block. */
1745 xfs_bmap_add_free(tp, fsb, rr->rr_rrec.rc_blockcount, NULL);
1747 error = xfs_trans_commit(tp);
1751 list_del(&rr->rr_list);
1757 xfs_trans_cancel(tp);
1759 /* Free the leftover list */
1760 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1761 list_del(&rr->rr_list);
1767 /* Is there a record covering a given extent? */
1769 xfs_refcount_has_record(
1770 struct xfs_btree_cur *cur,
1775 union xfs_btree_irec low;
1776 union xfs_btree_irec high;
1778 memset(&low, 0, sizeof(low));
1779 low.rc.rc_startblock = bno;
1780 memset(&high, 0xFF, sizeof(high));
1781 high.rc.rc_startblock = bno + len - 1;
1783 return xfs_btree_has_record(cur, &low, &high, exists);