2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_defer.h"
29 #include "xfs_btree.h"
31 #include "xfs_refcount_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_errortag.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36 #include "xfs_cksum.h"
37 #include "xfs_trans.h"
39 #include "xfs_refcount.h"
42 /* Allowable refcount adjustment amounts. */
43 enum xfs_refc_adjust_op {
44 XFS_REFCOUNT_ADJUST_INCREASE = 1,
45 XFS_REFCOUNT_ADJUST_DECREASE = -1,
46 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
47 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
50 STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
51 xfs_agblock_t agbno, xfs_extlen_t aglen,
52 struct xfs_defer_ops *dfops);
53 STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
54 xfs_agblock_t agbno, xfs_extlen_t aglen,
55 struct xfs_defer_ops *dfops);
58 * Look up the first record less than or equal to [bno, len] in the btree
62 xfs_refcount_lookup_le(
63 struct xfs_btree_cur *cur,
67 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.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_LE, stat);
75 * Look up the first record greater than or equal to [bno, len] in the btree
79 xfs_refcount_lookup_ge(
80 struct xfs_btree_cur *cur,
84 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.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_GE, stat);
92 * Look up the first record equal to [bno, len] in the btree
96 xfs_refcount_lookup_eq(
97 struct xfs_btree_cur *cur,
101 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno,
103 cur->bc_rec.rc.rc_startblock = bno;
104 cur->bc_rec.rc.rc_blockcount = 0;
105 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
108 /* Convert on-disk record to in-core format. */
110 xfs_refcount_btrec_to_irec(
111 union xfs_btree_rec *rec,
112 struct xfs_refcount_irec *irec)
114 irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock);
115 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
116 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
120 * Get the data from the pointed-to record.
123 xfs_refcount_get_rec(
124 struct xfs_btree_cur *cur,
125 struct xfs_refcount_irec *irec,
128 union xfs_btree_rec *rec;
131 error = xfs_btree_get_rec(cur, &rec, stat);
132 if (!error && *stat == 1) {
133 xfs_refcount_btrec_to_irec(rec, irec);
134 trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno,
141 * Update the record referred to by cur to the value given
142 * by [bno, len, refcount].
143 * This either works (return 0) or gets an EFSCORRUPTED error.
147 struct xfs_btree_cur *cur,
148 struct xfs_refcount_irec *irec)
150 union xfs_btree_rec rec;
153 trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec);
154 rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock);
155 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
156 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
157 error = xfs_btree_update(cur, &rec);
159 trace_xfs_refcount_update_error(cur->bc_mp,
160 cur->bc_private.a.agno, error, _RET_IP_);
165 * Insert the record referred to by cur to the value given
166 * by [bno, len, refcount].
167 * This either works (return 0) or gets an EFSCORRUPTED error.
171 struct xfs_btree_cur *cur,
172 struct xfs_refcount_irec *irec,
177 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec);
178 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
179 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
180 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
181 error = xfs_btree_insert(cur, i);
184 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
188 trace_xfs_refcount_insert_error(cur->bc_mp,
189 cur->bc_private.a.agno, error, _RET_IP_);
194 * Remove the record referred to by cur, then set the pointer to the spot
195 * where the record could be re-inserted, in case we want to increment or
196 * decrement the cursor.
197 * This either works (return 0) or gets an EFSCORRUPTED error.
201 struct xfs_btree_cur *cur,
204 struct xfs_refcount_irec irec;
208 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
211 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
212 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec);
213 error = xfs_btree_delete(cur, i);
214 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
217 error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec);
220 trace_xfs_refcount_delete_error(cur->bc_mp,
221 cur->bc_private.a.agno, error, _RET_IP_);
226 * Adjusting the Reference Count
228 * As stated elsewhere, the reference count btree (refcbt) stores
229 * >1 reference counts for extents of physical blocks. In this
230 * operation, we're either raising or lowering the reference count of
231 * some subrange stored in the tree:
233 * <------ adjustment range ------>
234 * ----+ +---+-----+ +--+--------+---------
235 * 2 | | 3 | 4 | |17| 55 | 10
236 * ----+ +---+-----+ +--+--------+---------
237 * X axis is physical blocks number;
238 * reference counts are the numbers inside the rectangles
240 * The first thing we need to do is to ensure that there are no
241 * refcount extents crossing either boundary of the range to be
242 * adjusted. For any extent that does cross a boundary, split it into
243 * two extents so that we can increment the refcount of one of the
246 * <------ adjustment range ------>
247 * ----+ +---+-----+ +--+--------+----+----
248 * 2 | | 3 | 2 | |17| 55 | 10 | 10
249 * ----+ +---+-----+ +--+--------+----+----
251 * For this next step, let's assume that all the physical blocks in
252 * the adjustment range are mapped to a file and are therefore in use
253 * at least once. Therefore, we can infer that any gap in the
254 * refcount tree within the adjustment range represents a physical
255 * extent with refcount == 1:
257 * <------ adjustment range ------>
258 * ----+---+---+-----+-+--+--------+----+----
259 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
260 * ----+---+---+-----+-+--+--------+----+----
263 * For each extent that falls within the interval range, figure out
264 * which extent is to the left or the right of that extent. Now we
265 * have a left, current, and right extent. If the new reference count
266 * of the center extent enables us to merge left, center, and right
267 * into one record covering all three, do so. If the center extent is
268 * at the left end of the range, abuts the left extent, and its new
269 * reference count matches the left extent's record, then merge them.
270 * If the center extent is at the right end of the range, abuts the
271 * right extent, and the reference counts match, merge those. In the
272 * example, we can left merge (assuming an increment operation):
274 * <------ adjustment range ------>
275 * --------+---+-----+-+--+--------+----+----
276 * 2 | 3 | 2 |1|17| 55 | 10 | 10
277 * --------+---+-----+-+--+--------+----+----
280 * For all other extents within the range, adjust the reference count
281 * or delete it if the refcount falls below 2. If we were
282 * incrementing, the end result looks like this:
284 * <------ adjustment range ------>
285 * --------+---+-----+-+--+--------+----+----
286 * 2 | 4 | 3 |2|18| 56 | 11 | 10
287 * --------+---+-----+-+--+--------+----+----
289 * The result of a decrement operation looks as such:
291 * <------ adjustment range ------>
292 * ----+ +---+ +--+--------+----+----
293 * 2 | | 2 | |16| 54 | 9 | 10
294 * ----+ +---+ +--+--------+----+----
297 * The blocks marked "D" are freed; the blocks marked "1" are only
298 * referenced once and therefore the record is removed from the
302 /* Next block after this extent. */
303 static inline xfs_agblock_t
305 struct xfs_refcount_irec *rc)
307 return rc->rc_startblock + rc->rc_blockcount;
311 * Split a refcount extent that crosses agbno.
314 xfs_refcount_split_extent(
315 struct xfs_btree_cur *cur,
319 struct xfs_refcount_irec rcext, tmp;
323 *shape_changed = false;
324 error = xfs_refcount_lookup_le(cur, agbno, &found_rec);
330 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
333 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
334 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
337 *shape_changed = true;
338 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_private.a.agno,
341 /* Establish the right extent. */
343 tmp.rc_startblock = agbno;
344 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
345 error = xfs_refcount_update(cur, &tmp);
349 /* Insert the left extent. */
351 tmp.rc_blockcount = agbno - rcext.rc_startblock;
352 error = xfs_refcount_insert(cur, &tmp, &found_rec);
355 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
359 trace_xfs_refcount_split_extent_error(cur->bc_mp,
360 cur->bc_private.a.agno, error, _RET_IP_);
365 * Merge the left, center, and right extents.
368 xfs_refcount_merge_center_extents(
369 struct xfs_btree_cur *cur,
370 struct xfs_refcount_irec *left,
371 struct xfs_refcount_irec *center,
372 struct xfs_refcount_irec *right,
373 unsigned long long extlen,
379 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
380 cur->bc_private.a.agno, left, center, right);
383 * Make sure the center and right extents are not in the btree.
384 * If the center extent was synthesized, the first delete call
385 * removes the right extent and we skip the second deletion.
386 * If center and right were in the btree, then the first delete
387 * call removes the center and the second one removes the right
390 error = xfs_refcount_lookup_ge(cur, center->rc_startblock,
394 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
396 error = xfs_refcount_delete(cur, &found_rec);
399 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
401 if (center->rc_refcount > 1) {
402 error = xfs_refcount_delete(cur, &found_rec);
405 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
409 /* Enlarge the left extent. */
410 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
414 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
416 left->rc_blockcount = extlen;
417 error = xfs_refcount_update(cur, left);
425 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
426 cur->bc_private.a.agno, error, _RET_IP_);
431 * Merge with the left extent.
434 xfs_refcount_merge_left_extent(
435 struct xfs_btree_cur *cur,
436 struct xfs_refcount_irec *left,
437 struct xfs_refcount_irec *cleft,
438 xfs_agblock_t *agbno,
444 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
445 cur->bc_private.a.agno, left, cleft);
447 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
448 if (cleft->rc_refcount > 1) {
449 error = xfs_refcount_lookup_le(cur, cleft->rc_startblock,
453 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
456 error = xfs_refcount_delete(cur, &found_rec);
459 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
463 /* Enlarge the left extent. */
464 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
468 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
470 left->rc_blockcount += cleft->rc_blockcount;
471 error = xfs_refcount_update(cur, left);
475 *agbno += cleft->rc_blockcount;
476 *aglen -= cleft->rc_blockcount;
480 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
481 cur->bc_private.a.agno, error, _RET_IP_);
486 * Merge with the right extent.
489 xfs_refcount_merge_right_extent(
490 struct xfs_btree_cur *cur,
491 struct xfs_refcount_irec *right,
492 struct xfs_refcount_irec *cright,
498 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
499 cur->bc_private.a.agno, cright, right);
502 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
505 if (cright->rc_refcount > 1) {
506 error = xfs_refcount_lookup_le(cur, cright->rc_startblock,
510 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
513 error = xfs_refcount_delete(cur, &found_rec);
516 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
520 /* Enlarge the right extent. */
521 error = xfs_refcount_lookup_le(cur, right->rc_startblock,
525 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
527 right->rc_startblock -= cright->rc_blockcount;
528 right->rc_blockcount += cright->rc_blockcount;
529 error = xfs_refcount_update(cur, right);
533 *aglen -= cright->rc_blockcount;
537 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
538 cur->bc_private.a.agno, error, _RET_IP_);
542 #define XFS_FIND_RCEXT_SHARED 1
543 #define XFS_FIND_RCEXT_COW 2
545 * Find the left extent and the one after it (cleft). This function assumes
546 * that we've already split any extent crossing agbno.
549 xfs_refcount_find_left_extents(
550 struct xfs_btree_cur *cur,
551 struct xfs_refcount_irec *left,
552 struct xfs_refcount_irec *cleft,
557 struct xfs_refcount_irec tmp;
561 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
562 error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec);
568 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
571 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
573 if (xfs_refc_next(&tmp) != agbno)
575 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
577 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
579 /* We have a left extent; retrieve (or invent) the next right one */
582 error = xfs_btree_increment(cur, 0, &found_rec);
586 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
589 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
592 /* if tmp starts at the end of our range, just use that */
593 if (tmp.rc_startblock == agbno)
597 * There's a gap in the refcntbt at the start of the
598 * range we're interested in (refcount == 1) so
599 * synthesize the implied extent and pass it back.
600 * We assume here that the agbno/aglen range was
601 * passed in from a data fork extent mapping and
602 * therefore is allocated to exactly one owner.
604 cleft->rc_startblock = agbno;
605 cleft->rc_blockcount = min(aglen,
606 tmp.rc_startblock - agbno);
607 cleft->rc_refcount = 1;
611 * No extents, so pretend that there's one covering the whole
614 cleft->rc_startblock = agbno;
615 cleft->rc_blockcount = aglen;
616 cleft->rc_refcount = 1;
618 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_private.a.agno,
623 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
624 cur->bc_private.a.agno, error, _RET_IP_);
629 * Find the right extent and the one before it (cright). This function
630 * assumes that we've already split any extents crossing agbno + aglen.
633 xfs_refcount_find_right_extents(
634 struct xfs_btree_cur *cur,
635 struct xfs_refcount_irec *right,
636 struct xfs_refcount_irec *cright,
641 struct xfs_refcount_irec tmp;
645 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
646 error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec);
652 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
655 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
657 if (tmp.rc_startblock != agbno + aglen)
659 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
661 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
663 /* We have a right extent; retrieve (or invent) the next left one */
666 error = xfs_btree_decrement(cur, 0, &found_rec);
670 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
673 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
676 /* if tmp ends at the end of our range, just use that */
677 if (xfs_refc_next(&tmp) == agbno + aglen)
681 * There's a gap in the refcntbt at the end of the
682 * range we're interested in (refcount == 1) so
683 * create the implied extent and pass it back.
684 * We assume here that the agbno/aglen range was
685 * passed in from a data fork extent mapping and
686 * therefore is allocated to exactly one owner.
688 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
689 cright->rc_blockcount = right->rc_startblock -
690 cright->rc_startblock;
691 cright->rc_refcount = 1;
695 * No extents, so pretend that there's one covering the whole
698 cright->rc_startblock = agbno;
699 cright->rc_blockcount = aglen;
700 cright->rc_refcount = 1;
702 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_private.a.agno,
703 cright, right, agbno + aglen);
707 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
708 cur->bc_private.a.agno, error, _RET_IP_);
712 /* Is this extent valid? */
715 struct xfs_refcount_irec *rc)
717 return rc->rc_startblock != NULLAGBLOCK;
721 * Try to merge with any extents on the boundaries of the adjustment range.
724 xfs_refcount_merge_extents(
725 struct xfs_btree_cur *cur,
726 xfs_agblock_t *agbno,
728 enum xfs_refc_adjust_op adjust,
732 struct xfs_refcount_irec left = {0}, cleft = {0};
733 struct xfs_refcount_irec cright = {0}, right = {0};
735 unsigned long long ulen;
738 *shape_changed = false;
740 * Find the extent just below agbno [left], just above agbno [cleft],
741 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
744 error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno,
748 error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno,
753 /* No left or right extent to merge; exit. */
754 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
757 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
758 (cleft.rc_blockcount == cright.rc_blockcount);
760 /* Try to merge left, cleft, and right. cleft must == cright. */
761 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount +
763 if (xfs_refc_valid(&left) && xfs_refc_valid(&right) &&
764 xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal &&
765 left.rc_refcount == cleft.rc_refcount + adjust &&
766 right.rc_refcount == cleft.rc_refcount + adjust &&
767 ulen < MAXREFCEXTLEN) {
768 *shape_changed = true;
769 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
770 &right, ulen, aglen);
773 /* Try to merge left and cleft. */
774 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount;
775 if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) &&
776 left.rc_refcount == cleft.rc_refcount + adjust &&
777 ulen < MAXREFCEXTLEN) {
778 *shape_changed = true;
779 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
785 * If we just merged left + cleft and cleft == cright,
786 * we no longer have a cright to merge with right. We're done.
792 /* Try to merge cright and right. */
793 ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount;
794 if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) &&
795 right.rc_refcount == cright.rc_refcount + adjust &&
796 ulen < MAXREFCEXTLEN) {
797 *shape_changed = true;
798 return xfs_refcount_merge_right_extent(cur, &right, &cright,
806 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
807 * true incorrectly is a shutdown FS; the penalty for guessing false
808 * incorrectly is more transaction rolls than might be necessary.
809 * Be conservative here.
812 xfs_refcount_still_have_space(
813 struct xfs_btree_cur *cur)
815 unsigned long overhead;
817 overhead = cur->bc_private.a.priv.refc.shape_changes *
818 xfs_allocfree_log_count(cur->bc_mp, 1);
819 overhead *= cur->bc_mp->m_sb.sb_blocksize;
822 * Only allow 2 refcount extent updates per transaction if the
823 * refcount continue update "error" has been injected.
825 if (cur->bc_private.a.priv.refc.nr_ops > 2 &&
826 XFS_TEST_ERROR(false, cur->bc_mp,
827 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
830 if (cur->bc_private.a.priv.refc.nr_ops == 0)
832 else if (overhead > cur->bc_tp->t_log_res)
834 return cur->bc_tp->t_log_res - overhead >
835 cur->bc_private.a.priv.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
839 * Adjust the refcounts of middle extents. At this point we should have
840 * split extents that crossed the adjustment range; merged with adjacent
841 * extents; and updated agbno/aglen to reflect the merges. Therefore,
842 * all we have to do is update the extents inside [agbno, agbno + aglen].
845 xfs_refcount_adjust_extents(
846 struct xfs_btree_cur *cur,
847 xfs_agblock_t *agbno,
849 enum xfs_refc_adjust_op adj,
850 struct xfs_defer_ops *dfops,
851 struct xfs_owner_info *oinfo)
853 struct xfs_refcount_irec ext, tmp;
855 int found_rec, found_tmp;
858 /* Merging did all the work already. */
862 error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec);
866 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
867 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
871 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
872 ext.rc_blockcount = 0;
877 * Deal with a hole in the refcount tree; if a file maps to
878 * these blocks and there's no refcountbt record, pretend that
879 * there is one with refcount == 1.
881 if (ext.rc_startblock != *agbno) {
882 tmp.rc_startblock = *agbno;
883 tmp.rc_blockcount = min(*aglen,
884 ext.rc_startblock - *agbno);
885 tmp.rc_refcount = 1 + adj;
886 trace_xfs_refcount_modify_extent(cur->bc_mp,
887 cur->bc_private.a.agno, &tmp);
890 * Either cover the hole (increment) or
891 * delete the range (decrement).
893 if (tmp.rc_refcount) {
894 error = xfs_refcount_insert(cur, &tmp,
898 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
899 found_tmp == 1, out_error);
900 cur->bc_private.a.priv.refc.nr_ops++;
902 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
903 cur->bc_private.a.agno,
905 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
906 tmp.rc_blockcount, oinfo);
909 (*agbno) += tmp.rc_blockcount;
910 (*aglen) -= tmp.rc_blockcount;
912 error = xfs_refcount_lookup_ge(cur, *agbno,
918 /* Stop if there's nothing left to modify */
919 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
923 * Adjust the reference count and either update the tree
924 * (incr) or free the blocks (decr).
926 if (ext.rc_refcount == MAXREFCOUNT)
928 ext.rc_refcount += adj;
929 trace_xfs_refcount_modify_extent(cur->bc_mp,
930 cur->bc_private.a.agno, &ext);
931 if (ext.rc_refcount > 1) {
932 error = xfs_refcount_update(cur, &ext);
935 cur->bc_private.a.priv.refc.nr_ops++;
936 } else if (ext.rc_refcount == 1) {
937 error = xfs_refcount_delete(cur, &found_rec);
940 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
941 found_rec == 1, out_error);
942 cur->bc_private.a.priv.refc.nr_ops++;
945 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
946 cur->bc_private.a.agno,
948 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
949 ext.rc_blockcount, oinfo);
953 error = xfs_btree_increment(cur, 0, &found_rec);
958 (*agbno) += ext.rc_blockcount;
959 (*aglen) -= ext.rc_blockcount;
964 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
965 cur->bc_private.a.agno, error, _RET_IP_);
969 /* Adjust the reference count of a range of AG blocks. */
972 struct xfs_btree_cur *cur,
975 xfs_agblock_t *new_agbno,
976 xfs_extlen_t *new_aglen,
977 enum xfs_refc_adjust_op adj,
978 struct xfs_defer_ops *dfops,
979 struct xfs_owner_info *oinfo)
982 int shape_changes = 0;
987 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
988 trace_xfs_refcount_increase(cur->bc_mp, cur->bc_private.a.agno,
991 trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_private.a.agno,
995 * Ensure that no rcextents cross the boundary of the adjustment range.
997 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1003 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1010 * Try to merge with the left or right extents of the range.
1012 error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj,
1013 XFS_FIND_RCEXT_SHARED, &shape_changed);
1019 cur->bc_private.a.priv.refc.shape_changes++;
1021 /* Now that we've taken care of the ends, adjust the middle extents */
1022 error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen,
1030 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_private.a.agno,
1035 /* Clean up after calling xfs_refcount_finish_one. */
1037 xfs_refcount_finish_one_cleanup(
1038 struct xfs_trans *tp,
1039 struct xfs_btree_cur *rcur,
1042 struct xfs_buf *agbp;
1046 agbp = rcur->bc_private.a.agbp;
1047 xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1049 xfs_trans_brelse(tp, agbp);
1053 * Process one of the deferred refcount operations. We pass back the
1054 * btree cursor to maintain our lock on the btree between calls.
1055 * This saves time and eliminates a buffer deadlock between the
1056 * superblock and the AGF because we'll always grab them in the same
1060 xfs_refcount_finish_one(
1061 struct xfs_trans *tp,
1062 struct xfs_defer_ops *dfops,
1063 enum xfs_refcount_intent_type type,
1064 xfs_fsblock_t startblock,
1065 xfs_extlen_t blockcount,
1066 xfs_fsblock_t *new_fsb,
1067 xfs_extlen_t *new_len,
1068 struct xfs_btree_cur **pcur)
1070 struct xfs_mount *mp = tp->t_mountp;
1071 struct xfs_btree_cur *rcur;
1072 struct xfs_buf *agbp = NULL;
1074 xfs_agnumber_t agno;
1076 xfs_agblock_t new_agbno;
1077 unsigned long nr_ops = 0;
1078 int shape_changes = 0;
1080 agno = XFS_FSB_TO_AGNO(mp, startblock);
1081 ASSERT(agno != NULLAGNUMBER);
1082 bno = XFS_FSB_TO_AGBNO(mp, startblock);
1084 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
1085 type, XFS_FSB_TO_AGBNO(mp, startblock),
1088 if (XFS_TEST_ERROR(false, mp,
1089 XFS_ERRTAG_REFCOUNT_FINISH_ONE))
1093 * If we haven't gotten a cursor or the cursor AG doesn't match
1094 * the startblock, get one now.
1097 if (rcur != NULL && rcur->bc_private.a.agno != agno) {
1098 nr_ops = rcur->bc_private.a.priv.refc.nr_ops;
1099 shape_changes = rcur->bc_private.a.priv.refc.shape_changes;
1100 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1105 error = xfs_alloc_read_agf(tp->t_mountp, tp, agno,
1106 XFS_ALLOC_FLAG_FREEING, &agbp);
1110 return -EFSCORRUPTED;
1112 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, dfops);
1117 rcur->bc_private.a.priv.refc.nr_ops = nr_ops;
1118 rcur->bc_private.a.priv.refc.shape_changes = shape_changes;
1123 case XFS_REFCOUNT_INCREASE:
1124 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1125 new_len, XFS_REFCOUNT_ADJUST_INCREASE, dfops, NULL);
1126 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1128 case XFS_REFCOUNT_DECREASE:
1129 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1130 new_len, XFS_REFCOUNT_ADJUST_DECREASE, dfops, NULL);
1131 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1133 case XFS_REFCOUNT_ALLOC_COW:
1134 *new_fsb = startblock + blockcount;
1136 error = __xfs_refcount_cow_alloc(rcur, bno, blockcount, dfops);
1138 case XFS_REFCOUNT_FREE_COW:
1139 *new_fsb = startblock + blockcount;
1141 error = __xfs_refcount_cow_free(rcur, bno, blockcount, dfops);
1145 error = -EFSCORRUPTED;
1147 if (!error && *new_len > 0)
1148 trace_xfs_refcount_finish_one_leftover(mp, agno, type,
1149 bno, blockcount, new_agbno, *new_len);
1153 xfs_trans_brelse(tp, agbp);
1159 * Record a refcount intent for later processing.
1163 struct xfs_mount *mp,
1164 struct xfs_defer_ops *dfops,
1165 enum xfs_refcount_intent_type type,
1166 xfs_fsblock_t startblock,
1167 xfs_extlen_t blockcount)
1169 struct xfs_refcount_intent *ri;
1171 trace_xfs_refcount_defer(mp, XFS_FSB_TO_AGNO(mp, startblock),
1172 type, XFS_FSB_TO_AGBNO(mp, startblock),
1175 ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
1176 KM_SLEEP | KM_NOFS);
1177 INIT_LIST_HEAD(&ri->ri_list);
1179 ri->ri_startblock = startblock;
1180 ri->ri_blockcount = blockcount;
1182 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
1187 * Increase the reference count of the blocks backing a file's extent.
1190 xfs_refcount_increase_extent(
1191 struct xfs_mount *mp,
1192 struct xfs_defer_ops *dfops,
1193 struct xfs_bmbt_irec *PREV)
1195 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1198 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_INCREASE,
1199 PREV->br_startblock, PREV->br_blockcount);
1203 * Decrease the reference count of the blocks backing a file's extent.
1206 xfs_refcount_decrease_extent(
1207 struct xfs_mount *mp,
1208 struct xfs_defer_ops *dfops,
1209 struct xfs_bmbt_irec *PREV)
1211 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1214 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_DECREASE,
1215 PREV->br_startblock, PREV->br_blockcount);
1219 * Given an AG extent, find the lowest-numbered run of shared blocks
1220 * within that range and return the range in fbno/flen. If
1221 * find_end_of_shared is set, return the longest contiguous extent of
1222 * shared blocks; if not, just return the first extent we find. If no
1223 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1224 * and 0, respectively.
1227 xfs_refcount_find_shared(
1228 struct xfs_btree_cur *cur,
1229 xfs_agblock_t agbno,
1231 xfs_agblock_t *fbno,
1233 bool find_end_of_shared)
1235 struct xfs_refcount_irec tmp;
1240 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_private.a.agno,
1243 /* By default, skip the whole range */
1244 *fbno = NULLAGBLOCK;
1247 /* Try to find a refcount extent that crosses the start */
1248 error = xfs_refcount_lookup_le(cur, agbno, &have);
1252 /* No left extent, look at the next one */
1253 error = xfs_btree_increment(cur, 0, &have);
1259 error = xfs_refcount_get_rec(cur, &tmp, &i);
1262 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1264 /* If the extent ends before the start, look at the next one */
1265 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1266 error = xfs_btree_increment(cur, 0, &have);
1271 error = xfs_refcount_get_rec(cur, &tmp, &i);
1274 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1277 /* If the extent starts after the range we want, bail out */
1278 if (tmp.rc_startblock >= agbno + aglen)
1281 /* We found the start of a shared extent! */
1282 if (tmp.rc_startblock < agbno) {
1283 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1284 tmp.rc_startblock = agbno;
1287 *fbno = tmp.rc_startblock;
1288 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1289 if (!find_end_of_shared)
1292 /* Otherwise, find the end of this shared extent */
1293 while (*fbno + *flen < agbno + aglen) {
1294 error = xfs_btree_increment(cur, 0, &have);
1299 error = xfs_refcount_get_rec(cur, &tmp, &i);
1302 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1303 if (tmp.rc_startblock >= agbno + aglen ||
1304 tmp.rc_startblock != *fbno + *flen)
1306 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1310 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1311 cur->bc_private.a.agno, *fbno, *flen);
1315 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1316 cur->bc_private.a.agno, error, _RET_IP_);
1321 * Recovering CoW Blocks After a Crash
1323 * Due to the way that the copy on write mechanism works, there's a window of
1324 * opportunity in which we can lose track of allocated blocks during a crash.
1325 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1326 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1327 * no longer in the free space btree but are not otherwise recorded anywhere
1328 * until the write completes and the blocks are mapped into the file. A crash
1329 * in between allocation and remapping results in the replacement blocks being
1330 * lost. This situation is exacerbated by the CoW extent size hint because
1331 * allocations can hang around for long time.
1333 * However, there is a place where we can record these allocations before they
1334 * become mappings -- the reference count btree. The btree does not record
1335 * extents with refcount == 1, so we can record allocations with a refcount of
1336 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1337 * no conflict with shared block records. These mappings should be created
1338 * when we allocate blocks to the CoW fork and deleted when they're removed
1339 * from the CoW fork.
1341 * Minor nit: records for in-progress CoW allocations and records for shared
1342 * extents must never be merged, to preserve the property that (except for CoW
1343 * allocations) there are no refcount btree entries with refcount == 1. The
1344 * only time this could potentially happen is when unsharing a block that's
1345 * adjacent to CoW allocations, so we must be careful to avoid this.
1347 * At mount time we recover lost CoW allocations by searching the refcount
1348 * btree for these refcount == 1 mappings. These represent CoW allocations
1349 * that were in progress at the time the filesystem went down, so we can free
1350 * them to get the space back.
1352 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1353 * several reasons -- first, EFIs pin the tail of the log and would have to be
1354 * periodically relogged to avoid filling up the log. Second, CoW completions
1355 * will have to file an EFD and create new EFIs for whatever remains in the
1356 * CoW fork; this partially takes care of (1) but extent-size reservations
1357 * will have to periodically relog even if there's no writeout in progress.
1358 * This can happen if the CoW extent size hint is set, which you really want.
1359 * Third, EFIs cannot currently be automatically relogged into newer
1360 * transactions to advance the log tail. Fourth, stuffing the log full of
1361 * EFIs places an upper bound on the number of CoW allocations that can be
1362 * held filesystem-wide at any given time. Recording them in the refcount
1363 * btree doesn't require us to maintain any state in memory and doesn't pin
1367 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1368 * in that they're not referenced anywhere else in the filesystem, so we
1369 * stash them in the refcount btree with a refcount of 1 until either file
1370 * remapping (or CoW cancellation) happens.
1373 xfs_refcount_adjust_cow_extents(
1374 struct xfs_btree_cur *cur,
1375 xfs_agblock_t agbno,
1377 enum xfs_refc_adjust_op adj)
1379 struct xfs_refcount_irec ext, tmp;
1381 int found_rec, found_tmp;
1386 /* Find any overlapping refcount records */
1387 error = xfs_refcount_lookup_ge(cur, agbno, &found_rec);
1390 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1394 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks +
1396 ext.rc_blockcount = 0;
1397 ext.rc_refcount = 0;
1401 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1402 /* Adding a CoW reservation, there should be nothing here. */
1403 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1404 ext.rc_startblock >= agbno + aglen, out_error);
1406 tmp.rc_startblock = agbno;
1407 tmp.rc_blockcount = aglen;
1408 tmp.rc_refcount = 1;
1409 trace_xfs_refcount_modify_extent(cur->bc_mp,
1410 cur->bc_private.a.agno, &tmp);
1412 error = xfs_refcount_insert(cur, &tmp,
1416 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1417 found_tmp == 1, out_error);
1419 case XFS_REFCOUNT_ADJUST_COW_FREE:
1420 /* Removing a CoW reservation, there should be one extent. */
1421 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1422 ext.rc_startblock == agbno, out_error);
1423 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1424 ext.rc_blockcount == aglen, out_error);
1425 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1426 ext.rc_refcount == 1, out_error);
1428 ext.rc_refcount = 0;
1429 trace_xfs_refcount_modify_extent(cur->bc_mp,
1430 cur->bc_private.a.agno, &ext);
1431 error = xfs_refcount_delete(cur, &found_rec);
1434 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1435 found_rec == 1, out_error);
1443 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1444 cur->bc_private.a.agno, error, _RET_IP_);
1449 * Add or remove refcount btree entries for CoW reservations.
1452 xfs_refcount_adjust_cow(
1453 struct xfs_btree_cur *cur,
1454 xfs_agblock_t agbno,
1456 enum xfs_refc_adjust_op adj)
1461 agbno += XFS_REFC_COW_START;
1464 * Ensure that no rcextents cross the boundary of the adjustment range.
1466 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1470 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1475 * Try to merge with the left or right extents of the range.
1477 error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj,
1478 XFS_FIND_RCEXT_COW, &shape_changed);
1482 /* Now that we've taken care of the ends, adjust the middle extents */
1483 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1490 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_private.a.agno,
1496 * Record a CoW allocation in the refcount btree.
1499 __xfs_refcount_cow_alloc(
1500 struct xfs_btree_cur *rcur,
1501 xfs_agblock_t agbno,
1503 struct xfs_defer_ops *dfops)
1505 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_private.a.agno,
1508 /* Add refcount btree reservation */
1509 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1510 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1514 * Remove a CoW allocation from the refcount btree.
1517 __xfs_refcount_cow_free(
1518 struct xfs_btree_cur *rcur,
1519 xfs_agblock_t agbno,
1521 struct xfs_defer_ops *dfops)
1523 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_private.a.agno,
1526 /* Remove refcount btree reservation */
1527 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1528 XFS_REFCOUNT_ADJUST_COW_FREE);
1531 /* Record a CoW staging extent in the refcount btree. */
1533 xfs_refcount_alloc_cow_extent(
1534 struct xfs_mount *mp,
1535 struct xfs_defer_ops *dfops,
1541 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1544 error = __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_ALLOC_COW,
1549 /* Add rmap entry */
1550 return xfs_rmap_alloc_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1551 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1554 /* Forget a CoW staging event in the refcount btree. */
1556 xfs_refcount_free_cow_extent(
1557 struct xfs_mount *mp,
1558 struct xfs_defer_ops *dfops,
1564 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1567 /* Remove rmap entry */
1568 error = xfs_rmap_free_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1569 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1573 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_FREE_COW,
1577 struct xfs_refcount_recovery {
1578 struct list_head rr_list;
1579 struct xfs_refcount_irec rr_rrec;
1582 /* Stuff an extent on the recovery list. */
1584 xfs_refcount_recover_extent(
1585 struct xfs_btree_cur *cur,
1586 union xfs_btree_rec *rec,
1589 struct list_head *debris = priv;
1590 struct xfs_refcount_recovery *rr;
1592 if (be32_to_cpu(rec->refc.rc_refcount) != 1)
1593 return -EFSCORRUPTED;
1595 rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), KM_SLEEP);
1596 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1597 list_add_tail(&rr->rr_list, debris);
1602 /* Find and remove leftover CoW reservations. */
1604 xfs_refcount_recover_cow_leftovers(
1605 struct xfs_mount *mp,
1606 xfs_agnumber_t agno)
1608 struct xfs_trans *tp;
1609 struct xfs_btree_cur *cur;
1610 struct xfs_buf *agbp;
1611 struct xfs_refcount_recovery *rr, *n;
1612 struct list_head debris;
1613 union xfs_btree_irec low;
1614 union xfs_btree_irec high;
1615 struct xfs_defer_ops dfops;
1617 xfs_agblock_t agbno;
1620 if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
1623 INIT_LIST_HEAD(&debris);
1626 * In this first part, we use an empty transaction to gather up
1627 * all the leftover CoW extents so that we can subsequently
1628 * delete them. The empty transaction is used to avoid
1629 * a buffer lock deadlock if there happens to be a loop in the
1630 * refcountbt because we're allowed to re-grab a buffer that is
1631 * already attached to our transaction. When we're done
1632 * recording the CoW debris we cancel the (empty) transaction
1633 * and everything goes away cleanly.
1635 error = xfs_trans_alloc_empty(mp, &tp);
1639 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
1646 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
1648 /* Find all the leftover CoW staging extents. */
1649 memset(&low, 0, sizeof(low));
1650 memset(&high, 0, sizeof(high));
1651 low.rc.rc_startblock = XFS_REFC_COW_START;
1652 high.rc.rc_startblock = -1U;
1653 error = xfs_btree_query_range(cur, &low, &high,
1654 xfs_refcount_recover_extent, &debris);
1657 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1658 xfs_trans_brelse(tp, agbp);
1659 xfs_trans_cancel(tp);
1661 /* Now iterate the list to free the leftovers */
1662 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1663 /* Set up transaction. */
1664 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1668 trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec);
1670 /* Free the orphan record */
1671 xfs_defer_init(&dfops, &fsb);
1672 agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START;
1673 fsb = XFS_AGB_TO_FSB(mp, agno, agbno);
1674 error = xfs_refcount_free_cow_extent(mp, &dfops, fsb,
1675 rr->rr_rrec.rc_blockcount);
1679 /* Free the block. */
1680 xfs_bmap_add_free(mp, &dfops, fsb,
1681 rr->rr_rrec.rc_blockcount, NULL);
1683 error = xfs_defer_finish(&tp, &dfops);
1687 error = xfs_trans_commit(tp);
1691 list_del(&rr->rr_list);
1697 xfs_defer_cancel(&dfops);
1699 xfs_trans_cancel(tp);
1701 /* Free the leftover list */
1702 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1703 list_del(&rr->rr_list);
1709 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1710 xfs_trans_brelse(tp, agbp);
1714 /* Is there a record covering a given extent? */
1716 xfs_refcount_has_record(
1717 struct xfs_btree_cur *cur,
1722 union xfs_btree_irec low;
1723 union xfs_btree_irec high;
1725 memset(&low, 0, sizeof(low));
1726 low.rc.rc_startblock = bno;
1727 memset(&high, 0xFF, sizeof(high));
1728 high.rc.rc_startblock = bno + len - 1;
1730 return xfs_btree_has_record(cur, &low, &high, exists);