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 struct xfs_mount *mp = cur->bc_mp;
129 xfs_agnumber_t agno = cur->bc_private.a.agno;
130 union xfs_btree_rec *rec;
132 xfs_agblock_t realstart;
134 error = xfs_btree_get_rec(cur, &rec, stat);
138 xfs_refcount_btrec_to_irec(rec, irec);
140 agno = cur->bc_private.a.agno;
141 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
144 /* handle special COW-staging state */
145 realstart = irec->rc_startblock;
146 if (realstart & XFS_REFC_COW_START) {
147 if (irec->rc_refcount != 1)
149 realstart &= ~XFS_REFC_COW_START;
150 } else if (irec->rc_refcount < 2) {
154 /* check for valid extent range, including overflow */
155 if (!xfs_verify_agbno(mp, agno, realstart))
157 if (realstart > realstart + irec->rc_blockcount)
159 if (!xfs_verify_agbno(mp, agno, realstart + irec->rc_blockcount - 1))
162 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
165 trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno, irec);
170 "Refcount BTree record corruption in AG %d detected!", agno);
172 "Start block 0x%x, block count 0x%x, references 0x%x",
173 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
174 return -EFSCORRUPTED;
178 * Update the record referred to by cur to the value given
179 * by [bno, len, refcount].
180 * This either works (return 0) or gets an EFSCORRUPTED error.
184 struct xfs_btree_cur *cur,
185 struct xfs_refcount_irec *irec)
187 union xfs_btree_rec rec;
190 trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec);
191 rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock);
192 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
193 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
194 error = xfs_btree_update(cur, &rec);
196 trace_xfs_refcount_update_error(cur->bc_mp,
197 cur->bc_private.a.agno, error, _RET_IP_);
202 * Insert the record referred to by cur to the value given
203 * by [bno, len, refcount].
204 * This either works (return 0) or gets an EFSCORRUPTED error.
208 struct xfs_btree_cur *cur,
209 struct xfs_refcount_irec *irec,
214 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec);
215 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
216 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
217 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
218 error = xfs_btree_insert(cur, i);
221 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
225 trace_xfs_refcount_insert_error(cur->bc_mp,
226 cur->bc_private.a.agno, error, _RET_IP_);
231 * Remove the record referred to by cur, then set the pointer to the spot
232 * where the record could be re-inserted, in case we want to increment or
233 * decrement the cursor.
234 * This either works (return 0) or gets an EFSCORRUPTED error.
238 struct xfs_btree_cur *cur,
241 struct xfs_refcount_irec irec;
245 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
248 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
249 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec);
250 error = xfs_btree_delete(cur, i);
251 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error);
254 error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec);
257 trace_xfs_refcount_delete_error(cur->bc_mp,
258 cur->bc_private.a.agno, error, _RET_IP_);
263 * Adjusting the Reference Count
265 * As stated elsewhere, the reference count btree (refcbt) stores
266 * >1 reference counts for extents of physical blocks. In this
267 * operation, we're either raising or lowering the reference count of
268 * some subrange stored in the tree:
270 * <------ adjustment range ------>
271 * ----+ +---+-----+ +--+--------+---------
272 * 2 | | 3 | 4 | |17| 55 | 10
273 * ----+ +---+-----+ +--+--------+---------
274 * X axis is physical blocks number;
275 * reference counts are the numbers inside the rectangles
277 * The first thing we need to do is to ensure that there are no
278 * refcount extents crossing either boundary of the range to be
279 * adjusted. For any extent that does cross a boundary, split it into
280 * two extents so that we can increment the refcount of one of the
283 * <------ adjustment range ------>
284 * ----+ +---+-----+ +--+--------+----+----
285 * 2 | | 3 | 2 | |17| 55 | 10 | 10
286 * ----+ +---+-----+ +--+--------+----+----
288 * For this next step, let's assume that all the physical blocks in
289 * the adjustment range are mapped to a file and are therefore in use
290 * at least once. Therefore, we can infer that any gap in the
291 * refcount tree within the adjustment range represents a physical
292 * extent with refcount == 1:
294 * <------ adjustment range ------>
295 * ----+---+---+-----+-+--+--------+----+----
296 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
297 * ----+---+---+-----+-+--+--------+----+----
300 * For each extent that falls within the interval range, figure out
301 * which extent is to the left or the right of that extent. Now we
302 * have a left, current, and right extent. If the new reference count
303 * of the center extent enables us to merge left, center, and right
304 * into one record covering all three, do so. If the center extent is
305 * at the left end of the range, abuts the left extent, and its new
306 * reference count matches the left extent's record, then merge them.
307 * If the center extent is at the right end of the range, abuts the
308 * right extent, and the reference counts match, merge those. In the
309 * example, we can left merge (assuming an increment operation):
311 * <------ adjustment range ------>
312 * --------+---+-----+-+--+--------+----+----
313 * 2 | 3 | 2 |1|17| 55 | 10 | 10
314 * --------+---+-----+-+--+--------+----+----
317 * For all other extents within the range, adjust the reference count
318 * or delete it if the refcount falls below 2. If we were
319 * incrementing, the end result looks like this:
321 * <------ adjustment range ------>
322 * --------+---+-----+-+--+--------+----+----
323 * 2 | 4 | 3 |2|18| 56 | 11 | 10
324 * --------+---+-----+-+--+--------+----+----
326 * The result of a decrement operation looks as such:
328 * <------ adjustment range ------>
329 * ----+ +---+ +--+--------+----+----
330 * 2 | | 2 | |16| 54 | 9 | 10
331 * ----+ +---+ +--+--------+----+----
334 * The blocks marked "D" are freed; the blocks marked "1" are only
335 * referenced once and therefore the record is removed from the
339 /* Next block after this extent. */
340 static inline xfs_agblock_t
342 struct xfs_refcount_irec *rc)
344 return rc->rc_startblock + rc->rc_blockcount;
348 * Split a refcount extent that crosses agbno.
351 xfs_refcount_split_extent(
352 struct xfs_btree_cur *cur,
356 struct xfs_refcount_irec rcext, tmp;
360 *shape_changed = false;
361 error = xfs_refcount_lookup_le(cur, agbno, &found_rec);
367 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
370 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
371 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
374 *shape_changed = true;
375 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_private.a.agno,
378 /* Establish the right extent. */
380 tmp.rc_startblock = agbno;
381 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
382 error = xfs_refcount_update(cur, &tmp);
386 /* Insert the left extent. */
388 tmp.rc_blockcount = agbno - rcext.rc_startblock;
389 error = xfs_refcount_insert(cur, &tmp, &found_rec);
392 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
396 trace_xfs_refcount_split_extent_error(cur->bc_mp,
397 cur->bc_private.a.agno, error, _RET_IP_);
402 * Merge the left, center, and right extents.
405 xfs_refcount_merge_center_extents(
406 struct xfs_btree_cur *cur,
407 struct xfs_refcount_irec *left,
408 struct xfs_refcount_irec *center,
409 struct xfs_refcount_irec *right,
410 unsigned long long extlen,
416 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
417 cur->bc_private.a.agno, left, center, right);
420 * Make sure the center and right extents are not in the btree.
421 * If the center extent was synthesized, the first delete call
422 * removes the right extent and we skip the second deletion.
423 * If center and right were in the btree, then the first delete
424 * call removes the center and the second one removes the right
427 error = xfs_refcount_lookup_ge(cur, center->rc_startblock,
431 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
433 error = xfs_refcount_delete(cur, &found_rec);
436 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
438 if (center->rc_refcount > 1) {
439 error = xfs_refcount_delete(cur, &found_rec);
442 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
446 /* Enlarge the left extent. */
447 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
451 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
453 left->rc_blockcount = extlen;
454 error = xfs_refcount_update(cur, left);
462 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
463 cur->bc_private.a.agno, error, _RET_IP_);
468 * Merge with the left extent.
471 xfs_refcount_merge_left_extent(
472 struct xfs_btree_cur *cur,
473 struct xfs_refcount_irec *left,
474 struct xfs_refcount_irec *cleft,
475 xfs_agblock_t *agbno,
481 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
482 cur->bc_private.a.agno, left, cleft);
484 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
485 if (cleft->rc_refcount > 1) {
486 error = xfs_refcount_lookup_le(cur, cleft->rc_startblock,
490 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
493 error = xfs_refcount_delete(cur, &found_rec);
496 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
500 /* Enlarge the left extent. */
501 error = xfs_refcount_lookup_le(cur, left->rc_startblock,
505 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
507 left->rc_blockcount += cleft->rc_blockcount;
508 error = xfs_refcount_update(cur, left);
512 *agbno += cleft->rc_blockcount;
513 *aglen -= cleft->rc_blockcount;
517 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
518 cur->bc_private.a.agno, error, _RET_IP_);
523 * Merge with the right extent.
526 xfs_refcount_merge_right_extent(
527 struct xfs_btree_cur *cur,
528 struct xfs_refcount_irec *right,
529 struct xfs_refcount_irec *cright,
535 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
536 cur->bc_private.a.agno, cright, right);
539 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
542 if (cright->rc_refcount > 1) {
543 error = xfs_refcount_lookup_le(cur, cright->rc_startblock,
547 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
550 error = xfs_refcount_delete(cur, &found_rec);
553 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
557 /* Enlarge the right extent. */
558 error = xfs_refcount_lookup_le(cur, right->rc_startblock,
562 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
564 right->rc_startblock -= cright->rc_blockcount;
565 right->rc_blockcount += cright->rc_blockcount;
566 error = xfs_refcount_update(cur, right);
570 *aglen -= cright->rc_blockcount;
574 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
575 cur->bc_private.a.agno, error, _RET_IP_);
579 #define XFS_FIND_RCEXT_SHARED 1
580 #define XFS_FIND_RCEXT_COW 2
582 * Find the left extent and the one after it (cleft). This function assumes
583 * that we've already split any extent crossing agbno.
586 xfs_refcount_find_left_extents(
587 struct xfs_btree_cur *cur,
588 struct xfs_refcount_irec *left,
589 struct xfs_refcount_irec *cleft,
594 struct xfs_refcount_irec tmp;
598 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
599 error = xfs_refcount_lookup_le(cur, agbno - 1, &found_rec);
605 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
608 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
610 if (xfs_refc_next(&tmp) != agbno)
612 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
614 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
616 /* We have a left extent; retrieve (or invent) the next right one */
619 error = xfs_btree_increment(cur, 0, &found_rec);
623 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
626 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
629 /* if tmp starts at the end of our range, just use that */
630 if (tmp.rc_startblock == agbno)
634 * There's a gap in the refcntbt at the start of the
635 * range we're interested in (refcount == 1) so
636 * synthesize the implied extent and pass it back.
637 * We assume here that the agbno/aglen range was
638 * passed in from a data fork extent mapping and
639 * therefore is allocated to exactly one owner.
641 cleft->rc_startblock = agbno;
642 cleft->rc_blockcount = min(aglen,
643 tmp.rc_startblock - agbno);
644 cleft->rc_refcount = 1;
648 * No extents, so pretend that there's one covering the whole
651 cleft->rc_startblock = agbno;
652 cleft->rc_blockcount = aglen;
653 cleft->rc_refcount = 1;
655 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_private.a.agno,
660 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
661 cur->bc_private.a.agno, error, _RET_IP_);
666 * Find the right extent and the one before it (cright). This function
667 * assumes that we've already split any extents crossing agbno + aglen.
670 xfs_refcount_find_right_extents(
671 struct xfs_btree_cur *cur,
672 struct xfs_refcount_irec *right,
673 struct xfs_refcount_irec *cright,
678 struct xfs_refcount_irec tmp;
682 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
683 error = xfs_refcount_lookup_ge(cur, agbno + aglen, &found_rec);
689 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
692 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error);
694 if (tmp.rc_startblock != agbno + aglen)
696 if ((flags & XFS_FIND_RCEXT_SHARED) && tmp.rc_refcount < 2)
698 if ((flags & XFS_FIND_RCEXT_COW) && tmp.rc_refcount > 1)
700 /* We have a right extent; retrieve (or invent) the next left one */
703 error = xfs_btree_decrement(cur, 0, &found_rec);
707 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
710 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1,
713 /* if tmp ends at the end of our range, just use that */
714 if (xfs_refc_next(&tmp) == agbno + aglen)
718 * There's a gap in the refcntbt at the end of the
719 * range we're interested in (refcount == 1) so
720 * create the implied extent and pass it back.
721 * We assume here that the agbno/aglen range was
722 * passed in from a data fork extent mapping and
723 * therefore is allocated to exactly one owner.
725 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
726 cright->rc_blockcount = right->rc_startblock -
727 cright->rc_startblock;
728 cright->rc_refcount = 1;
732 * No extents, so pretend that there's one covering the whole
735 cright->rc_startblock = agbno;
736 cright->rc_blockcount = aglen;
737 cright->rc_refcount = 1;
739 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_private.a.agno,
740 cright, right, agbno + aglen);
744 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
745 cur->bc_private.a.agno, error, _RET_IP_);
749 /* Is this extent valid? */
752 struct xfs_refcount_irec *rc)
754 return rc->rc_startblock != NULLAGBLOCK;
758 * Try to merge with any extents on the boundaries of the adjustment range.
761 xfs_refcount_merge_extents(
762 struct xfs_btree_cur *cur,
763 xfs_agblock_t *agbno,
765 enum xfs_refc_adjust_op adjust,
769 struct xfs_refcount_irec left = {0}, cleft = {0};
770 struct xfs_refcount_irec cright = {0}, right = {0};
772 unsigned long long ulen;
775 *shape_changed = false;
777 * Find the extent just below agbno [left], just above agbno [cleft],
778 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
781 error = xfs_refcount_find_left_extents(cur, &left, &cleft, *agbno,
785 error = xfs_refcount_find_right_extents(cur, &right, &cright, *agbno,
790 /* No left or right extent to merge; exit. */
791 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
794 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
795 (cleft.rc_blockcount == cright.rc_blockcount);
797 /* Try to merge left, cleft, and right. cleft must == cright. */
798 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount +
800 if (xfs_refc_valid(&left) && xfs_refc_valid(&right) &&
801 xfs_refc_valid(&cleft) && xfs_refc_valid(&cright) && cequal &&
802 left.rc_refcount == cleft.rc_refcount + adjust &&
803 right.rc_refcount == cleft.rc_refcount + adjust &&
804 ulen < MAXREFCEXTLEN) {
805 *shape_changed = true;
806 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
807 &right, ulen, aglen);
810 /* Try to merge left and cleft. */
811 ulen = (unsigned long long)left.rc_blockcount + cleft.rc_blockcount;
812 if (xfs_refc_valid(&left) && xfs_refc_valid(&cleft) &&
813 left.rc_refcount == cleft.rc_refcount + adjust &&
814 ulen < MAXREFCEXTLEN) {
815 *shape_changed = true;
816 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
822 * If we just merged left + cleft and cleft == cright,
823 * we no longer have a cright to merge with right. We're done.
829 /* Try to merge cright and right. */
830 ulen = (unsigned long long)right.rc_blockcount + cright.rc_blockcount;
831 if (xfs_refc_valid(&right) && xfs_refc_valid(&cright) &&
832 right.rc_refcount == cright.rc_refcount + adjust &&
833 ulen < MAXREFCEXTLEN) {
834 *shape_changed = true;
835 return xfs_refcount_merge_right_extent(cur, &right, &cright,
843 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
844 * true incorrectly is a shutdown FS; the penalty for guessing false
845 * incorrectly is more transaction rolls than might be necessary.
846 * Be conservative here.
849 xfs_refcount_still_have_space(
850 struct xfs_btree_cur *cur)
852 unsigned long overhead;
854 overhead = cur->bc_private.a.priv.refc.shape_changes *
855 xfs_allocfree_log_count(cur->bc_mp, 1);
856 overhead *= cur->bc_mp->m_sb.sb_blocksize;
859 * Only allow 2 refcount extent updates per transaction if the
860 * refcount continue update "error" has been injected.
862 if (cur->bc_private.a.priv.refc.nr_ops > 2 &&
863 XFS_TEST_ERROR(false, cur->bc_mp,
864 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
867 if (cur->bc_private.a.priv.refc.nr_ops == 0)
869 else if (overhead > cur->bc_tp->t_log_res)
871 return cur->bc_tp->t_log_res - overhead >
872 cur->bc_private.a.priv.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
876 * Adjust the refcounts of middle extents. At this point we should have
877 * split extents that crossed the adjustment range; merged with adjacent
878 * extents; and updated agbno/aglen to reflect the merges. Therefore,
879 * all we have to do is update the extents inside [agbno, agbno + aglen].
882 xfs_refcount_adjust_extents(
883 struct xfs_btree_cur *cur,
884 xfs_agblock_t *agbno,
886 enum xfs_refc_adjust_op adj,
887 struct xfs_defer_ops *dfops,
888 struct xfs_owner_info *oinfo)
890 struct xfs_refcount_irec ext, tmp;
892 int found_rec, found_tmp;
895 /* Merging did all the work already. */
899 error = xfs_refcount_lookup_ge(cur, *agbno, &found_rec);
903 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
904 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
908 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
909 ext.rc_blockcount = 0;
914 * Deal with a hole in the refcount tree; if a file maps to
915 * these blocks and there's no refcountbt record, pretend that
916 * there is one with refcount == 1.
918 if (ext.rc_startblock != *agbno) {
919 tmp.rc_startblock = *agbno;
920 tmp.rc_blockcount = min(*aglen,
921 ext.rc_startblock - *agbno);
922 tmp.rc_refcount = 1 + adj;
923 trace_xfs_refcount_modify_extent(cur->bc_mp,
924 cur->bc_private.a.agno, &tmp);
927 * Either cover the hole (increment) or
928 * delete the range (decrement).
930 if (tmp.rc_refcount) {
931 error = xfs_refcount_insert(cur, &tmp,
935 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
936 found_tmp == 1, out_error);
937 cur->bc_private.a.priv.refc.nr_ops++;
939 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
940 cur->bc_private.a.agno,
942 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
943 tmp.rc_blockcount, oinfo);
946 (*agbno) += tmp.rc_blockcount;
947 (*aglen) -= tmp.rc_blockcount;
949 error = xfs_refcount_lookup_ge(cur, *agbno,
955 /* Stop if there's nothing left to modify */
956 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
960 * Adjust the reference count and either update the tree
961 * (incr) or free the blocks (decr).
963 if (ext.rc_refcount == MAXREFCOUNT)
965 ext.rc_refcount += adj;
966 trace_xfs_refcount_modify_extent(cur->bc_mp,
967 cur->bc_private.a.agno, &ext);
968 if (ext.rc_refcount > 1) {
969 error = xfs_refcount_update(cur, &ext);
972 cur->bc_private.a.priv.refc.nr_ops++;
973 } else if (ext.rc_refcount == 1) {
974 error = xfs_refcount_delete(cur, &found_rec);
977 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
978 found_rec == 1, out_error);
979 cur->bc_private.a.priv.refc.nr_ops++;
982 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
983 cur->bc_private.a.agno,
985 xfs_bmap_add_free(cur->bc_mp, dfops, fsbno,
986 ext.rc_blockcount, oinfo);
990 error = xfs_btree_increment(cur, 0, &found_rec);
995 (*agbno) += ext.rc_blockcount;
996 (*aglen) -= ext.rc_blockcount;
1001 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1002 cur->bc_private.a.agno, error, _RET_IP_);
1006 /* Adjust the reference count of a range of AG blocks. */
1008 xfs_refcount_adjust(
1009 struct xfs_btree_cur *cur,
1010 xfs_agblock_t agbno,
1012 xfs_agblock_t *new_agbno,
1013 xfs_extlen_t *new_aglen,
1014 enum xfs_refc_adjust_op adj,
1015 struct xfs_defer_ops *dfops,
1016 struct xfs_owner_info *oinfo)
1019 int shape_changes = 0;
1024 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1025 trace_xfs_refcount_increase(cur->bc_mp, cur->bc_private.a.agno,
1028 trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_private.a.agno,
1032 * Ensure that no rcextents cross the boundary of the adjustment range.
1034 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1040 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1047 * Try to merge with the left or right extents of the range.
1049 error = xfs_refcount_merge_extents(cur, new_agbno, new_aglen, adj,
1050 XFS_FIND_RCEXT_SHARED, &shape_changed);
1056 cur->bc_private.a.priv.refc.shape_changes++;
1058 /* Now that we've taken care of the ends, adjust the middle extents */
1059 error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen,
1067 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_private.a.agno,
1072 /* Clean up after calling xfs_refcount_finish_one. */
1074 xfs_refcount_finish_one_cleanup(
1075 struct xfs_trans *tp,
1076 struct xfs_btree_cur *rcur,
1079 struct xfs_buf *agbp;
1083 agbp = rcur->bc_private.a.agbp;
1084 xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1086 xfs_trans_brelse(tp, agbp);
1090 * Process one of the deferred refcount operations. We pass back the
1091 * btree cursor to maintain our lock on the btree between calls.
1092 * This saves time and eliminates a buffer deadlock between the
1093 * superblock and the AGF because we'll always grab them in the same
1097 xfs_refcount_finish_one(
1098 struct xfs_trans *tp,
1099 struct xfs_defer_ops *dfops,
1100 enum xfs_refcount_intent_type type,
1101 xfs_fsblock_t startblock,
1102 xfs_extlen_t blockcount,
1103 xfs_fsblock_t *new_fsb,
1104 xfs_extlen_t *new_len,
1105 struct xfs_btree_cur **pcur)
1107 struct xfs_mount *mp = tp->t_mountp;
1108 struct xfs_btree_cur *rcur;
1109 struct xfs_buf *agbp = NULL;
1111 xfs_agnumber_t agno;
1113 xfs_agblock_t new_agbno;
1114 unsigned long nr_ops = 0;
1115 int shape_changes = 0;
1117 agno = XFS_FSB_TO_AGNO(mp, startblock);
1118 ASSERT(agno != NULLAGNUMBER);
1119 bno = XFS_FSB_TO_AGBNO(mp, startblock);
1121 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
1122 type, XFS_FSB_TO_AGBNO(mp, startblock),
1125 if (XFS_TEST_ERROR(false, mp,
1126 XFS_ERRTAG_REFCOUNT_FINISH_ONE))
1130 * If we haven't gotten a cursor or the cursor AG doesn't match
1131 * the startblock, get one now.
1134 if (rcur != NULL && rcur->bc_private.a.agno != agno) {
1135 nr_ops = rcur->bc_private.a.priv.refc.nr_ops;
1136 shape_changes = rcur->bc_private.a.priv.refc.shape_changes;
1137 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1142 error = xfs_alloc_read_agf(tp->t_mountp, tp, agno,
1143 XFS_ALLOC_FLAG_FREEING, &agbp);
1147 return -EFSCORRUPTED;
1149 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, dfops);
1154 rcur->bc_private.a.priv.refc.nr_ops = nr_ops;
1155 rcur->bc_private.a.priv.refc.shape_changes = shape_changes;
1160 case XFS_REFCOUNT_INCREASE:
1161 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1162 new_len, XFS_REFCOUNT_ADJUST_INCREASE, dfops, NULL);
1163 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1165 case XFS_REFCOUNT_DECREASE:
1166 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1167 new_len, XFS_REFCOUNT_ADJUST_DECREASE, dfops, NULL);
1168 *new_fsb = XFS_AGB_TO_FSB(mp, agno, new_agbno);
1170 case XFS_REFCOUNT_ALLOC_COW:
1171 *new_fsb = startblock + blockcount;
1173 error = __xfs_refcount_cow_alloc(rcur, bno, blockcount, dfops);
1175 case XFS_REFCOUNT_FREE_COW:
1176 *new_fsb = startblock + blockcount;
1178 error = __xfs_refcount_cow_free(rcur, bno, blockcount, dfops);
1182 error = -EFSCORRUPTED;
1184 if (!error && *new_len > 0)
1185 trace_xfs_refcount_finish_one_leftover(mp, agno, type,
1186 bno, blockcount, new_agbno, *new_len);
1190 xfs_trans_brelse(tp, agbp);
1196 * Record a refcount intent for later processing.
1200 struct xfs_mount *mp,
1201 struct xfs_defer_ops *dfops,
1202 enum xfs_refcount_intent_type type,
1203 xfs_fsblock_t startblock,
1204 xfs_extlen_t blockcount)
1206 struct xfs_refcount_intent *ri;
1208 trace_xfs_refcount_defer(mp, XFS_FSB_TO_AGNO(mp, startblock),
1209 type, XFS_FSB_TO_AGBNO(mp, startblock),
1212 ri = kmem_alloc(sizeof(struct xfs_refcount_intent),
1213 KM_SLEEP | KM_NOFS);
1214 INIT_LIST_HEAD(&ri->ri_list);
1216 ri->ri_startblock = startblock;
1217 ri->ri_blockcount = blockcount;
1219 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
1224 * Increase the reference count of the blocks backing a file's extent.
1227 xfs_refcount_increase_extent(
1228 struct xfs_mount *mp,
1229 struct xfs_defer_ops *dfops,
1230 struct xfs_bmbt_irec *PREV)
1232 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1235 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_INCREASE,
1236 PREV->br_startblock, PREV->br_blockcount);
1240 * Decrease the reference count of the blocks backing a file's extent.
1243 xfs_refcount_decrease_extent(
1244 struct xfs_mount *mp,
1245 struct xfs_defer_ops *dfops,
1246 struct xfs_bmbt_irec *PREV)
1248 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1251 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_DECREASE,
1252 PREV->br_startblock, PREV->br_blockcount);
1256 * Given an AG extent, find the lowest-numbered run of shared blocks
1257 * within that range and return the range in fbno/flen. If
1258 * find_end_of_shared is set, return the longest contiguous extent of
1259 * shared blocks; if not, just return the first extent we find. If no
1260 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1261 * and 0, respectively.
1264 xfs_refcount_find_shared(
1265 struct xfs_btree_cur *cur,
1266 xfs_agblock_t agbno,
1268 xfs_agblock_t *fbno,
1270 bool find_end_of_shared)
1272 struct xfs_refcount_irec tmp;
1277 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_private.a.agno,
1280 /* By default, skip the whole range */
1281 *fbno = NULLAGBLOCK;
1284 /* Try to find a refcount extent that crosses the start */
1285 error = xfs_refcount_lookup_le(cur, agbno, &have);
1289 /* No left extent, look at the next one */
1290 error = xfs_btree_increment(cur, 0, &have);
1296 error = xfs_refcount_get_rec(cur, &tmp, &i);
1299 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1301 /* If the extent ends before the start, look at the next one */
1302 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1303 error = xfs_btree_increment(cur, 0, &have);
1308 error = xfs_refcount_get_rec(cur, &tmp, &i);
1311 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1314 /* If the extent starts after the range we want, bail out */
1315 if (tmp.rc_startblock >= agbno + aglen)
1318 /* We found the start of a shared extent! */
1319 if (tmp.rc_startblock < agbno) {
1320 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1321 tmp.rc_startblock = agbno;
1324 *fbno = tmp.rc_startblock;
1325 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1326 if (!find_end_of_shared)
1329 /* Otherwise, find the end of this shared extent */
1330 while (*fbno + *flen < agbno + aglen) {
1331 error = xfs_btree_increment(cur, 0, &have);
1336 error = xfs_refcount_get_rec(cur, &tmp, &i);
1339 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, out_error);
1340 if (tmp.rc_startblock >= agbno + aglen ||
1341 tmp.rc_startblock != *fbno + *flen)
1343 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1347 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1348 cur->bc_private.a.agno, *fbno, *flen);
1352 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1353 cur->bc_private.a.agno, error, _RET_IP_);
1358 * Recovering CoW Blocks After a Crash
1360 * Due to the way that the copy on write mechanism works, there's a window of
1361 * opportunity in which we can lose track of allocated blocks during a crash.
1362 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1363 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1364 * no longer in the free space btree but are not otherwise recorded anywhere
1365 * until the write completes and the blocks are mapped into the file. A crash
1366 * in between allocation and remapping results in the replacement blocks being
1367 * lost. This situation is exacerbated by the CoW extent size hint because
1368 * allocations can hang around for long time.
1370 * However, there is a place where we can record these allocations before they
1371 * become mappings -- the reference count btree. The btree does not record
1372 * extents with refcount == 1, so we can record allocations with a refcount of
1373 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1374 * no conflict with shared block records. These mappings should be created
1375 * when we allocate blocks to the CoW fork and deleted when they're removed
1376 * from the CoW fork.
1378 * Minor nit: records for in-progress CoW allocations and records for shared
1379 * extents must never be merged, to preserve the property that (except for CoW
1380 * allocations) there are no refcount btree entries with refcount == 1. The
1381 * only time this could potentially happen is when unsharing a block that's
1382 * adjacent to CoW allocations, so we must be careful to avoid this.
1384 * At mount time we recover lost CoW allocations by searching the refcount
1385 * btree for these refcount == 1 mappings. These represent CoW allocations
1386 * that were in progress at the time the filesystem went down, so we can free
1387 * them to get the space back.
1389 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1390 * several reasons -- first, EFIs pin the tail of the log and would have to be
1391 * periodically relogged to avoid filling up the log. Second, CoW completions
1392 * will have to file an EFD and create new EFIs for whatever remains in the
1393 * CoW fork; this partially takes care of (1) but extent-size reservations
1394 * will have to periodically relog even if there's no writeout in progress.
1395 * This can happen if the CoW extent size hint is set, which you really want.
1396 * Third, EFIs cannot currently be automatically relogged into newer
1397 * transactions to advance the log tail. Fourth, stuffing the log full of
1398 * EFIs places an upper bound on the number of CoW allocations that can be
1399 * held filesystem-wide at any given time. Recording them in the refcount
1400 * btree doesn't require us to maintain any state in memory and doesn't pin
1404 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1405 * in that they're not referenced anywhere else in the filesystem, so we
1406 * stash them in the refcount btree with a refcount of 1 until either file
1407 * remapping (or CoW cancellation) happens.
1410 xfs_refcount_adjust_cow_extents(
1411 struct xfs_btree_cur *cur,
1412 xfs_agblock_t agbno,
1414 enum xfs_refc_adjust_op adj)
1416 struct xfs_refcount_irec ext, tmp;
1418 int found_rec, found_tmp;
1423 /* Find any overlapping refcount records */
1424 error = xfs_refcount_lookup_ge(cur, agbno, &found_rec);
1427 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1431 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks +
1433 ext.rc_blockcount = 0;
1434 ext.rc_refcount = 0;
1438 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1439 /* Adding a CoW reservation, there should be nothing here. */
1440 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1441 ext.rc_startblock >= agbno + aglen, out_error);
1443 tmp.rc_startblock = agbno;
1444 tmp.rc_blockcount = aglen;
1445 tmp.rc_refcount = 1;
1446 trace_xfs_refcount_modify_extent(cur->bc_mp,
1447 cur->bc_private.a.agno, &tmp);
1449 error = xfs_refcount_insert(cur, &tmp,
1453 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1454 found_tmp == 1, out_error);
1456 case XFS_REFCOUNT_ADJUST_COW_FREE:
1457 /* Removing a CoW reservation, there should be one extent. */
1458 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1459 ext.rc_startblock == agbno, out_error);
1460 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1461 ext.rc_blockcount == aglen, out_error);
1462 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1463 ext.rc_refcount == 1, out_error);
1465 ext.rc_refcount = 0;
1466 trace_xfs_refcount_modify_extent(cur->bc_mp,
1467 cur->bc_private.a.agno, &ext);
1468 error = xfs_refcount_delete(cur, &found_rec);
1471 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp,
1472 found_rec == 1, out_error);
1480 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1481 cur->bc_private.a.agno, error, _RET_IP_);
1486 * Add or remove refcount btree entries for CoW reservations.
1489 xfs_refcount_adjust_cow(
1490 struct xfs_btree_cur *cur,
1491 xfs_agblock_t agbno,
1493 enum xfs_refc_adjust_op adj)
1498 agbno += XFS_REFC_COW_START;
1501 * Ensure that no rcextents cross the boundary of the adjustment range.
1503 error = xfs_refcount_split_extent(cur, agbno, &shape_changed);
1507 error = xfs_refcount_split_extent(cur, agbno + aglen, &shape_changed);
1512 * Try to merge with the left or right extents of the range.
1514 error = xfs_refcount_merge_extents(cur, &agbno, &aglen, adj,
1515 XFS_FIND_RCEXT_COW, &shape_changed);
1519 /* Now that we've taken care of the ends, adjust the middle extents */
1520 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1527 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_private.a.agno,
1533 * Record a CoW allocation in the refcount btree.
1536 __xfs_refcount_cow_alloc(
1537 struct xfs_btree_cur *rcur,
1538 xfs_agblock_t agbno,
1540 struct xfs_defer_ops *dfops)
1542 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_private.a.agno,
1545 /* Add refcount btree reservation */
1546 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1547 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1551 * Remove a CoW allocation from the refcount btree.
1554 __xfs_refcount_cow_free(
1555 struct xfs_btree_cur *rcur,
1556 xfs_agblock_t agbno,
1558 struct xfs_defer_ops *dfops)
1560 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_private.a.agno,
1563 /* Remove refcount btree reservation */
1564 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1565 XFS_REFCOUNT_ADJUST_COW_FREE);
1568 /* Record a CoW staging extent in the refcount btree. */
1570 xfs_refcount_alloc_cow_extent(
1571 struct xfs_mount *mp,
1572 struct xfs_defer_ops *dfops,
1578 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1581 error = __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_ALLOC_COW,
1586 /* Add rmap entry */
1587 return xfs_rmap_alloc_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1588 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1591 /* Forget a CoW staging event in the refcount btree. */
1593 xfs_refcount_free_cow_extent(
1594 struct xfs_mount *mp,
1595 struct xfs_defer_ops *dfops,
1601 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1604 /* Remove rmap entry */
1605 error = xfs_rmap_free_extent(mp, dfops, XFS_FSB_TO_AGNO(mp, fsb),
1606 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1610 return __xfs_refcount_add(mp, dfops, XFS_REFCOUNT_FREE_COW,
1614 struct xfs_refcount_recovery {
1615 struct list_head rr_list;
1616 struct xfs_refcount_irec rr_rrec;
1619 /* Stuff an extent on the recovery list. */
1621 xfs_refcount_recover_extent(
1622 struct xfs_btree_cur *cur,
1623 union xfs_btree_rec *rec,
1626 struct list_head *debris = priv;
1627 struct xfs_refcount_recovery *rr;
1629 if (be32_to_cpu(rec->refc.rc_refcount) != 1)
1630 return -EFSCORRUPTED;
1632 rr = kmem_alloc(sizeof(struct xfs_refcount_recovery), KM_SLEEP);
1633 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1634 list_add_tail(&rr->rr_list, debris);
1639 /* Find and remove leftover CoW reservations. */
1641 xfs_refcount_recover_cow_leftovers(
1642 struct xfs_mount *mp,
1643 xfs_agnumber_t agno)
1645 struct xfs_trans *tp;
1646 struct xfs_btree_cur *cur;
1647 struct xfs_buf *agbp;
1648 struct xfs_refcount_recovery *rr, *n;
1649 struct list_head debris;
1650 union xfs_btree_irec low;
1651 union xfs_btree_irec high;
1652 struct xfs_defer_ops dfops;
1654 xfs_agblock_t agbno;
1657 if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
1660 INIT_LIST_HEAD(&debris);
1663 * In this first part, we use an empty transaction to gather up
1664 * all the leftover CoW extents so that we can subsequently
1665 * delete them. The empty transaction is used to avoid
1666 * a buffer lock deadlock if there happens to be a loop in the
1667 * refcountbt because we're allowed to re-grab a buffer that is
1668 * already attached to our transaction. When we're done
1669 * recording the CoW debris we cancel the (empty) transaction
1670 * and everything goes away cleanly.
1672 error = xfs_trans_alloc_empty(mp, &tp);
1676 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
1683 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
1685 /* Find all the leftover CoW staging extents. */
1686 memset(&low, 0, sizeof(low));
1687 memset(&high, 0, sizeof(high));
1688 low.rc.rc_startblock = XFS_REFC_COW_START;
1689 high.rc.rc_startblock = -1U;
1690 error = xfs_btree_query_range(cur, &low, &high,
1691 xfs_refcount_recover_extent, &debris);
1694 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1695 xfs_trans_brelse(tp, agbp);
1696 xfs_trans_cancel(tp);
1698 /* Now iterate the list to free the leftovers */
1699 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1700 /* Set up transaction. */
1701 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1705 trace_xfs_refcount_recover_extent(mp, agno, &rr->rr_rrec);
1707 /* Free the orphan record */
1708 xfs_defer_init(&dfops, &fsb);
1709 agbno = rr->rr_rrec.rc_startblock - XFS_REFC_COW_START;
1710 fsb = XFS_AGB_TO_FSB(mp, agno, agbno);
1711 error = xfs_refcount_free_cow_extent(mp, &dfops, fsb,
1712 rr->rr_rrec.rc_blockcount);
1716 /* Free the block. */
1717 xfs_bmap_add_free(mp, &dfops, fsb,
1718 rr->rr_rrec.rc_blockcount, NULL);
1720 error = xfs_defer_finish(&tp, &dfops);
1724 error = xfs_trans_commit(tp);
1728 list_del(&rr->rr_list);
1734 xfs_defer_cancel(&dfops);
1736 xfs_trans_cancel(tp);
1738 /* Free the leftover list */
1739 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1740 list_del(&rr->rr_list);
1746 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1747 xfs_trans_brelse(tp, agbp);
1751 /* Is there a record covering a given extent? */
1753 xfs_refcount_has_record(
1754 struct xfs_btree_cur *cur,
1759 union xfs_btree_irec low;
1760 union xfs_btree_irec high;
1762 memset(&low, 0, sizeof(low));
1763 low.rc.rc_startblock = bno;
1764 memset(&high, 0xFF, sizeof(high));
1765 high.rc.rc_startblock = bno + len - 1;
1767 return xfs_btree_has_record(cur, &low, &high, exists);