1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_btree.h"
17 #include "xfs_refcount_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_errortag.h"
20 #include "xfs_trace.h"
21 #include "xfs_cksum.h"
22 #include "xfs_trans.h"
24 #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_private
.a
.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_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_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_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_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_private
.a
.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_private
.a
.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_private
.a
.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_private
.a
.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_private
.a
.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_private
.a
.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 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
208 trace_xfs_refcount_insert_error(cur
->bc_mp
,
209 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
214 * Remove the record referred to by cur, then set the pointer to the spot
215 * where the record could be re-inserted, in case we want to increment or
216 * decrement the cursor.
217 * This either works (return 0) or gets an EFSCORRUPTED error.
221 struct xfs_btree_cur
*cur
,
224 struct xfs_refcount_irec irec
;
228 error
= xfs_refcount_get_rec(cur
, &irec
, &found_rec
);
231 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
232 trace_xfs_refcount_delete(cur
->bc_mp
, cur
->bc_private
.a
.agno
, &irec
);
233 error
= xfs_btree_delete(cur
, i
);
234 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
237 error
= xfs_refcount_lookup_ge(cur
, irec
.rc_startblock
, &found_rec
);
240 trace_xfs_refcount_delete_error(cur
->bc_mp
,
241 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
246 * Adjusting the Reference Count
248 * As stated elsewhere, the reference count btree (refcbt) stores
249 * >1 reference counts for extents of physical blocks. In this
250 * operation, we're either raising or lowering the reference count of
251 * some subrange stored in the tree:
253 * <------ adjustment range ------>
254 * ----+ +---+-----+ +--+--------+---------
255 * 2 | | 3 | 4 | |17| 55 | 10
256 * ----+ +---+-----+ +--+--------+---------
257 * X axis is physical blocks number;
258 * reference counts are the numbers inside the rectangles
260 * The first thing we need to do is to ensure that there are no
261 * refcount extents crossing either boundary of the range to be
262 * adjusted. For any extent that does cross a boundary, split it into
263 * two extents so that we can increment the refcount of one of the
266 * <------ adjustment range ------>
267 * ----+ +---+-----+ +--+--------+----+----
268 * 2 | | 3 | 2 | |17| 55 | 10 | 10
269 * ----+ +---+-----+ +--+--------+----+----
271 * For this next step, let's assume that all the physical blocks in
272 * the adjustment range are mapped to a file and are therefore in use
273 * at least once. Therefore, we can infer that any gap in the
274 * refcount tree within the adjustment range represents a physical
275 * extent with refcount == 1:
277 * <------ adjustment range ------>
278 * ----+---+---+-----+-+--+--------+----+----
279 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
280 * ----+---+---+-----+-+--+--------+----+----
283 * For each extent that falls within the interval range, figure out
284 * which extent is to the left or the right of that extent. Now we
285 * have a left, current, and right extent. If the new reference count
286 * of the center extent enables us to merge left, center, and right
287 * into one record covering all three, do so. If the center extent is
288 * at the left end of the range, abuts the left extent, and its new
289 * reference count matches the left extent's record, then merge them.
290 * If the center extent is at the right end of the range, abuts the
291 * right extent, and the reference counts match, merge those. In the
292 * example, we can left merge (assuming an increment operation):
294 * <------ adjustment range ------>
295 * --------+---+-----+-+--+--------+----+----
296 * 2 | 3 | 2 |1|17| 55 | 10 | 10
297 * --------+---+-----+-+--+--------+----+----
300 * For all other extents within the range, adjust the reference count
301 * or delete it if the refcount falls below 2. If we were
302 * incrementing, the end result looks like this:
304 * <------ adjustment range ------>
305 * --------+---+-----+-+--+--------+----+----
306 * 2 | 4 | 3 |2|18| 56 | 11 | 10
307 * --------+---+-----+-+--+--------+----+----
309 * The result of a decrement operation looks as such:
311 * <------ adjustment range ------>
312 * ----+ +---+ +--+--------+----+----
313 * 2 | | 2 | |16| 54 | 9 | 10
314 * ----+ +---+ +--+--------+----+----
317 * The blocks marked "D" are freed; the blocks marked "1" are only
318 * referenced once and therefore the record is removed from the
322 /* Next block after this extent. */
323 static inline xfs_agblock_t
325 struct xfs_refcount_irec
*rc
)
327 return rc
->rc_startblock
+ rc
->rc_blockcount
;
331 * Split a refcount extent that crosses agbno.
334 xfs_refcount_split_extent(
335 struct xfs_btree_cur
*cur
,
339 struct xfs_refcount_irec rcext
, tmp
;
343 *shape_changed
= false;
344 error
= xfs_refcount_lookup_le(cur
, agbno
, &found_rec
);
350 error
= xfs_refcount_get_rec(cur
, &rcext
, &found_rec
);
353 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
354 if (rcext
.rc_startblock
== agbno
|| xfs_refc_next(&rcext
) <= agbno
)
357 *shape_changed
= true;
358 trace_xfs_refcount_split_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
361 /* Establish the right extent. */
363 tmp
.rc_startblock
= agbno
;
364 tmp
.rc_blockcount
-= (agbno
- rcext
.rc_startblock
);
365 error
= xfs_refcount_update(cur
, &tmp
);
369 /* Insert the left extent. */
371 tmp
.rc_blockcount
= agbno
- rcext
.rc_startblock
;
372 error
= xfs_refcount_insert(cur
, &tmp
, &found_rec
);
375 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
379 trace_xfs_refcount_split_extent_error(cur
->bc_mp
,
380 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
385 * Merge the left, center, and right extents.
388 xfs_refcount_merge_center_extents(
389 struct xfs_btree_cur
*cur
,
390 struct xfs_refcount_irec
*left
,
391 struct xfs_refcount_irec
*center
,
392 struct xfs_refcount_irec
*right
,
393 unsigned long long extlen
,
399 trace_xfs_refcount_merge_center_extents(cur
->bc_mp
,
400 cur
->bc_private
.a
.agno
, left
, center
, right
);
403 * Make sure the center and right extents are not in the btree.
404 * If the center extent was synthesized, the first delete call
405 * removes the right extent and we skip the second deletion.
406 * If center and right were in the btree, then the first delete
407 * call removes the center and the second one removes the right
410 error
= xfs_refcount_lookup_ge(cur
, center
->rc_startblock
,
414 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
416 error
= xfs_refcount_delete(cur
, &found_rec
);
419 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
421 if (center
->rc_refcount
> 1) {
422 error
= xfs_refcount_delete(cur
, &found_rec
);
425 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
429 /* Enlarge the left extent. */
430 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
434 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
436 left
->rc_blockcount
= extlen
;
437 error
= xfs_refcount_update(cur
, left
);
445 trace_xfs_refcount_merge_center_extents_error(cur
->bc_mp
,
446 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
451 * Merge with the left extent.
454 xfs_refcount_merge_left_extent(
455 struct xfs_btree_cur
*cur
,
456 struct xfs_refcount_irec
*left
,
457 struct xfs_refcount_irec
*cleft
,
458 xfs_agblock_t
*agbno
,
464 trace_xfs_refcount_merge_left_extent(cur
->bc_mp
,
465 cur
->bc_private
.a
.agno
, left
, cleft
);
467 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
468 if (cleft
->rc_refcount
> 1) {
469 error
= xfs_refcount_lookup_le(cur
, cleft
->rc_startblock
,
473 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
476 error
= xfs_refcount_delete(cur
, &found_rec
);
479 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
483 /* Enlarge the left extent. */
484 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
488 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
490 left
->rc_blockcount
+= cleft
->rc_blockcount
;
491 error
= xfs_refcount_update(cur
, left
);
495 *agbno
+= cleft
->rc_blockcount
;
496 *aglen
-= cleft
->rc_blockcount
;
500 trace_xfs_refcount_merge_left_extent_error(cur
->bc_mp
,
501 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
506 * Merge with the right extent.
509 xfs_refcount_merge_right_extent(
510 struct xfs_btree_cur
*cur
,
511 struct xfs_refcount_irec
*right
,
512 struct xfs_refcount_irec
*cright
,
518 trace_xfs_refcount_merge_right_extent(cur
->bc_mp
,
519 cur
->bc_private
.a
.agno
, cright
, right
);
522 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
525 if (cright
->rc_refcount
> 1) {
526 error
= xfs_refcount_lookup_le(cur
, cright
->rc_startblock
,
530 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
533 error
= xfs_refcount_delete(cur
, &found_rec
);
536 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
540 /* Enlarge the right extent. */
541 error
= xfs_refcount_lookup_le(cur
, right
->rc_startblock
,
545 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
547 right
->rc_startblock
-= cright
->rc_blockcount
;
548 right
->rc_blockcount
+= cright
->rc_blockcount
;
549 error
= xfs_refcount_update(cur
, right
);
553 *aglen
-= cright
->rc_blockcount
;
557 trace_xfs_refcount_merge_right_extent_error(cur
->bc_mp
,
558 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
562 #define XFS_FIND_RCEXT_SHARED 1
563 #define XFS_FIND_RCEXT_COW 2
565 * Find the left extent and the one after it (cleft). This function assumes
566 * that we've already split any extent crossing agbno.
569 xfs_refcount_find_left_extents(
570 struct xfs_btree_cur
*cur
,
571 struct xfs_refcount_irec
*left
,
572 struct xfs_refcount_irec
*cleft
,
577 struct xfs_refcount_irec tmp
;
581 left
->rc_startblock
= cleft
->rc_startblock
= NULLAGBLOCK
;
582 error
= xfs_refcount_lookup_le(cur
, agbno
- 1, &found_rec
);
588 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
591 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
593 if (xfs_refc_next(&tmp
) != agbno
)
595 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
597 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
599 /* We have a left extent; retrieve (or invent) the next right one */
602 error
= xfs_btree_increment(cur
, 0, &found_rec
);
606 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
609 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
612 /* if tmp starts at the end of our range, just use that */
613 if (tmp
.rc_startblock
== agbno
)
617 * There's a gap in the refcntbt at the start of the
618 * range we're interested in (refcount == 1) so
619 * synthesize the implied extent and pass it back.
620 * We assume here that the agbno/aglen range was
621 * passed in from a data fork extent mapping and
622 * therefore is allocated to exactly one owner.
624 cleft
->rc_startblock
= agbno
;
625 cleft
->rc_blockcount
= min(aglen
,
626 tmp
.rc_startblock
- agbno
);
627 cleft
->rc_refcount
= 1;
631 * No extents, so pretend that there's one covering the whole
634 cleft
->rc_startblock
= agbno
;
635 cleft
->rc_blockcount
= aglen
;
636 cleft
->rc_refcount
= 1;
638 trace_xfs_refcount_find_left_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
643 trace_xfs_refcount_find_left_extent_error(cur
->bc_mp
,
644 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
649 * Find the right extent and the one before it (cright). This function
650 * assumes that we've already split any extents crossing agbno + aglen.
653 xfs_refcount_find_right_extents(
654 struct xfs_btree_cur
*cur
,
655 struct xfs_refcount_irec
*right
,
656 struct xfs_refcount_irec
*cright
,
661 struct xfs_refcount_irec tmp
;
665 right
->rc_startblock
= cright
->rc_startblock
= NULLAGBLOCK
;
666 error
= xfs_refcount_lookup_ge(cur
, agbno
+ aglen
, &found_rec
);
672 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
675 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
677 if (tmp
.rc_startblock
!= agbno
+ aglen
)
679 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
681 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
683 /* We have a right extent; retrieve (or invent) the next left one */
686 error
= xfs_btree_decrement(cur
, 0, &found_rec
);
690 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
693 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
696 /* if tmp ends at the end of our range, just use that */
697 if (xfs_refc_next(&tmp
) == agbno
+ aglen
)
701 * There's a gap in the refcntbt at the end of the
702 * range we're interested in (refcount == 1) so
703 * create the implied extent and pass it back.
704 * We assume here that the agbno/aglen range was
705 * passed in from a data fork extent mapping and
706 * therefore is allocated to exactly one owner.
708 cright
->rc_startblock
= max(agbno
, xfs_refc_next(&tmp
));
709 cright
->rc_blockcount
= right
->rc_startblock
-
710 cright
->rc_startblock
;
711 cright
->rc_refcount
= 1;
715 * No extents, so pretend that there's one covering the whole
718 cright
->rc_startblock
= agbno
;
719 cright
->rc_blockcount
= aglen
;
720 cright
->rc_refcount
= 1;
722 trace_xfs_refcount_find_right_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
723 cright
, right
, agbno
+ aglen
);
727 trace_xfs_refcount_find_right_extent_error(cur
->bc_mp
,
728 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
732 /* Is this extent valid? */
735 struct xfs_refcount_irec
*rc
)
737 return rc
->rc_startblock
!= NULLAGBLOCK
;
741 * Try to merge with any extents on the boundaries of the adjustment range.
744 xfs_refcount_merge_extents(
745 struct xfs_btree_cur
*cur
,
746 xfs_agblock_t
*agbno
,
748 enum xfs_refc_adjust_op adjust
,
752 struct xfs_refcount_irec left
= {0}, cleft
= {0};
753 struct xfs_refcount_irec cright
= {0}, right
= {0};
755 unsigned long long ulen
;
758 *shape_changed
= false;
760 * Find the extent just below agbno [left], just above agbno [cleft],
761 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
764 error
= xfs_refcount_find_left_extents(cur
, &left
, &cleft
, *agbno
,
768 error
= xfs_refcount_find_right_extents(cur
, &right
, &cright
, *agbno
,
773 /* No left or right extent to merge; exit. */
774 if (!xfs_refc_valid(&left
) && !xfs_refc_valid(&right
))
777 cequal
= (cleft
.rc_startblock
== cright
.rc_startblock
) &&
778 (cleft
.rc_blockcount
== cright
.rc_blockcount
);
780 /* Try to merge left, cleft, and right. cleft must == cright. */
781 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
+
783 if (xfs_refc_valid(&left
) && xfs_refc_valid(&right
) &&
784 xfs_refc_valid(&cleft
) && xfs_refc_valid(&cright
) && cequal
&&
785 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
786 right
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
787 ulen
< MAXREFCEXTLEN
) {
788 *shape_changed
= true;
789 return xfs_refcount_merge_center_extents(cur
, &left
, &cleft
,
790 &right
, ulen
, aglen
);
793 /* Try to merge left and cleft. */
794 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
;
795 if (xfs_refc_valid(&left
) && xfs_refc_valid(&cleft
) &&
796 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
797 ulen
< MAXREFCEXTLEN
) {
798 *shape_changed
= true;
799 error
= xfs_refcount_merge_left_extent(cur
, &left
, &cleft
,
805 * If we just merged left + cleft and cleft == cright,
806 * we no longer have a cright to merge with right. We're done.
812 /* Try to merge cright and right. */
813 ulen
= (unsigned long long)right
.rc_blockcount
+ cright
.rc_blockcount
;
814 if (xfs_refc_valid(&right
) && xfs_refc_valid(&cright
) &&
815 right
.rc_refcount
== cright
.rc_refcount
+ adjust
&&
816 ulen
< MAXREFCEXTLEN
) {
817 *shape_changed
= true;
818 return xfs_refcount_merge_right_extent(cur
, &right
, &cright
,
826 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
827 * true incorrectly is a shutdown FS; the penalty for guessing false
828 * incorrectly is more transaction rolls than might be necessary.
829 * Be conservative here.
832 xfs_refcount_still_have_space(
833 struct xfs_btree_cur
*cur
)
835 unsigned long overhead
;
837 overhead
= cur
->bc_private
.a
.priv
.refc
.shape_changes
*
838 xfs_allocfree_log_count(cur
->bc_mp
, 1);
839 overhead
*= cur
->bc_mp
->m_sb
.sb_blocksize
;
842 * Only allow 2 refcount extent updates per transaction if the
843 * refcount continue update "error" has been injected.
845 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
> 2 &&
846 XFS_TEST_ERROR(false, cur
->bc_mp
,
847 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE
))
850 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
== 0)
852 else if (overhead
> cur
->bc_tp
->t_log_res
)
854 return cur
->bc_tp
->t_log_res
- overhead
>
855 cur
->bc_private
.a
.priv
.refc
.nr_ops
* XFS_REFCOUNT_ITEM_OVERHEAD
;
859 * Adjust the refcounts of middle extents. At this point we should have
860 * split extents that crossed the adjustment range; merged with adjacent
861 * extents; and updated agbno/aglen to reflect the merges. Therefore,
862 * all we have to do is update the extents inside [agbno, agbno + aglen].
865 xfs_refcount_adjust_extents(
866 struct xfs_btree_cur
*cur
,
867 xfs_agblock_t
*agbno
,
869 enum xfs_refc_adjust_op adj
,
870 struct xfs_owner_info
*oinfo
)
872 struct xfs_refcount_irec ext
, tmp
;
874 int found_rec
, found_tmp
;
877 /* Merging did all the work already. */
881 error
= xfs_refcount_lookup_ge(cur
, *agbno
, &found_rec
);
885 while (*aglen
> 0 && xfs_refcount_still_have_space(cur
)) {
886 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
890 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
;
891 ext
.rc_blockcount
= 0;
896 * Deal with a hole in the refcount tree; if a file maps to
897 * these blocks and there's no refcountbt record, pretend that
898 * there is one with refcount == 1.
900 if (ext
.rc_startblock
!= *agbno
) {
901 tmp
.rc_startblock
= *agbno
;
902 tmp
.rc_blockcount
= min(*aglen
,
903 ext
.rc_startblock
- *agbno
);
904 tmp
.rc_refcount
= 1 + adj
;
905 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
906 cur
->bc_private
.a
.agno
, &tmp
);
909 * Either cover the hole (increment) or
910 * delete the range (decrement).
912 if (tmp
.rc_refcount
) {
913 error
= xfs_refcount_insert(cur
, &tmp
,
917 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
918 found_tmp
== 1, out_error
);
919 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
921 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
922 cur
->bc_private
.a
.agno
,
924 xfs_bmap_add_free(cur
->bc_tp
, fsbno
,
925 tmp
.rc_blockcount
, oinfo
);
928 (*agbno
) += tmp
.rc_blockcount
;
929 (*aglen
) -= tmp
.rc_blockcount
;
931 error
= xfs_refcount_lookup_ge(cur
, *agbno
,
937 /* Stop if there's nothing left to modify */
938 if (*aglen
== 0 || !xfs_refcount_still_have_space(cur
))
942 * Adjust the reference count and either update the tree
943 * (incr) or free the blocks (decr).
945 if (ext
.rc_refcount
== MAXREFCOUNT
)
947 ext
.rc_refcount
+= adj
;
948 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
949 cur
->bc_private
.a
.agno
, &ext
);
950 if (ext
.rc_refcount
> 1) {
951 error
= xfs_refcount_update(cur
, &ext
);
954 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
955 } else if (ext
.rc_refcount
== 1) {
956 error
= xfs_refcount_delete(cur
, &found_rec
);
959 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
960 found_rec
== 1, out_error
);
961 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
964 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
965 cur
->bc_private
.a
.agno
,
967 xfs_bmap_add_free(cur
->bc_tp
, fsbno
, ext
.rc_blockcount
,
972 error
= xfs_btree_increment(cur
, 0, &found_rec
);
977 (*agbno
) += ext
.rc_blockcount
;
978 (*aglen
) -= ext
.rc_blockcount
;
983 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
984 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
988 /* Adjust the reference count of a range of AG blocks. */
991 struct xfs_btree_cur
*cur
,
994 xfs_agblock_t
*new_agbno
,
995 xfs_extlen_t
*new_aglen
,
996 enum xfs_refc_adjust_op adj
,
997 struct xfs_owner_info
*oinfo
)
1000 int shape_changes
= 0;
1005 if (adj
== XFS_REFCOUNT_ADJUST_INCREASE
)
1006 trace_xfs_refcount_increase(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1009 trace_xfs_refcount_decrease(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1013 * Ensure that no rcextents cross the boundary of the adjustment range.
1015 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
1021 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1028 * Try to merge with the left or right extents of the range.
1030 error
= xfs_refcount_merge_extents(cur
, new_agbno
, new_aglen
, adj
,
1031 XFS_FIND_RCEXT_SHARED
, &shape_changed
);
1037 cur
->bc_private
.a
.priv
.refc
.shape_changes
++;
1039 /* Now that we've taken care of the ends, adjust the middle extents */
1040 error
= xfs_refcount_adjust_extents(cur
, new_agbno
, new_aglen
,
1048 trace_xfs_refcount_adjust_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1053 /* Clean up after calling xfs_refcount_finish_one. */
1055 xfs_refcount_finish_one_cleanup(
1056 struct xfs_trans
*tp
,
1057 struct xfs_btree_cur
*rcur
,
1060 struct xfs_buf
*agbp
;
1064 agbp
= rcur
->bc_private
.a
.agbp
;
1065 xfs_btree_del_cursor(rcur
, error
);
1067 xfs_trans_brelse(tp
, agbp
);
1071 * Process one of the deferred refcount operations. We pass back the
1072 * btree cursor to maintain our lock on the btree between calls.
1073 * This saves time and eliminates a buffer deadlock between the
1074 * superblock and the AGF because we'll always grab them in the same
1078 xfs_refcount_finish_one(
1079 struct xfs_trans
*tp
,
1080 enum xfs_refcount_intent_type type
,
1081 xfs_fsblock_t startblock
,
1082 xfs_extlen_t blockcount
,
1083 xfs_fsblock_t
*new_fsb
,
1084 xfs_extlen_t
*new_len
,
1085 struct xfs_btree_cur
**pcur
)
1087 struct xfs_mount
*mp
= tp
->t_mountp
;
1088 struct xfs_btree_cur
*rcur
;
1089 struct xfs_buf
*agbp
= NULL
;
1091 xfs_agnumber_t agno
;
1093 xfs_agblock_t new_agbno
;
1094 unsigned long nr_ops
= 0;
1095 int shape_changes
= 0;
1097 agno
= XFS_FSB_TO_AGNO(mp
, startblock
);
1098 ASSERT(agno
!= NULLAGNUMBER
);
1099 bno
= XFS_FSB_TO_AGBNO(mp
, startblock
);
1101 trace_xfs_refcount_deferred(mp
, XFS_FSB_TO_AGNO(mp
, startblock
),
1102 type
, XFS_FSB_TO_AGBNO(mp
, startblock
),
1105 if (XFS_TEST_ERROR(false, mp
,
1106 XFS_ERRTAG_REFCOUNT_FINISH_ONE
))
1110 * If we haven't gotten a cursor or the cursor AG doesn't match
1111 * the startblock, get one now.
1114 if (rcur
!= NULL
&& rcur
->bc_private
.a
.agno
!= agno
) {
1115 nr_ops
= rcur
->bc_private
.a
.priv
.refc
.nr_ops
;
1116 shape_changes
= rcur
->bc_private
.a
.priv
.refc
.shape_changes
;
1117 xfs_refcount_finish_one_cleanup(tp
, rcur
, 0);
1122 error
= xfs_alloc_read_agf(tp
->t_mountp
, tp
, agno
,
1123 XFS_ALLOC_FLAG_FREEING
, &agbp
);
1127 return -EFSCORRUPTED
;
1129 rcur
= xfs_refcountbt_init_cursor(mp
, tp
, agbp
, agno
);
1134 rcur
->bc_private
.a
.priv
.refc
.nr_ops
= nr_ops
;
1135 rcur
->bc_private
.a
.priv
.refc
.shape_changes
= shape_changes
;
1140 case XFS_REFCOUNT_INCREASE
:
1141 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1142 new_len
, XFS_REFCOUNT_ADJUST_INCREASE
, NULL
);
1143 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1145 case XFS_REFCOUNT_DECREASE
:
1146 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1147 new_len
, XFS_REFCOUNT_ADJUST_DECREASE
, NULL
);
1148 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1150 case XFS_REFCOUNT_ALLOC_COW
:
1151 *new_fsb
= startblock
+ blockcount
;
1153 error
= __xfs_refcount_cow_alloc(rcur
, bno
, blockcount
);
1155 case XFS_REFCOUNT_FREE_COW
:
1156 *new_fsb
= startblock
+ blockcount
;
1158 error
= __xfs_refcount_cow_free(rcur
, bno
, blockcount
);
1162 error
= -EFSCORRUPTED
;
1164 if (!error
&& *new_len
> 0)
1165 trace_xfs_refcount_finish_one_leftover(mp
, agno
, type
,
1166 bno
, blockcount
, new_agbno
, *new_len
);
1170 xfs_trans_brelse(tp
, agbp
);
1176 * Record a refcount intent for later processing.
1180 struct xfs_trans
*tp
,
1181 enum xfs_refcount_intent_type type
,
1182 xfs_fsblock_t startblock
,
1183 xfs_extlen_t blockcount
)
1185 struct xfs_refcount_intent
*ri
;
1187 trace_xfs_refcount_defer(tp
->t_mountp
,
1188 XFS_FSB_TO_AGNO(tp
->t_mountp
, startblock
),
1189 type
, XFS_FSB_TO_AGBNO(tp
->t_mountp
, startblock
),
1192 ri
= kmem_alloc(sizeof(struct xfs_refcount_intent
),
1193 KM_SLEEP
| KM_NOFS
);
1194 INIT_LIST_HEAD(&ri
->ri_list
);
1196 ri
->ri_startblock
= startblock
;
1197 ri
->ri_blockcount
= blockcount
;
1199 xfs_defer_add(tp
, XFS_DEFER_OPS_TYPE_REFCOUNT
, &ri
->ri_list
);
1204 * Increase the reference count of the blocks backing a file's extent.
1207 xfs_refcount_increase_extent(
1208 struct xfs_trans
*tp
,
1209 struct xfs_bmbt_irec
*PREV
)
1211 if (!xfs_sb_version_hasreflink(&tp
->t_mountp
->m_sb
))
1214 return __xfs_refcount_add(tp
, XFS_REFCOUNT_INCREASE
,
1215 PREV
->br_startblock
, PREV
->br_blockcount
);
1219 * Decrease the reference count of the blocks backing a file's extent.
1222 xfs_refcount_decrease_extent(
1223 struct xfs_trans
*tp
,
1224 struct xfs_bmbt_irec
*PREV
)
1226 if (!xfs_sb_version_hasreflink(&tp
->t_mountp
->m_sb
))
1229 return __xfs_refcount_add(tp
, XFS_REFCOUNT_DECREASE
,
1230 PREV
->br_startblock
, PREV
->br_blockcount
);
1234 * Given an AG extent, find the lowest-numbered run of shared blocks
1235 * within that range and return the range in fbno/flen. If
1236 * find_end_of_shared is set, return the longest contiguous extent of
1237 * shared blocks; if not, just return the first extent we find. If no
1238 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1239 * and 0, respectively.
1242 xfs_refcount_find_shared(
1243 struct xfs_btree_cur
*cur
,
1244 xfs_agblock_t agbno
,
1246 xfs_agblock_t
*fbno
,
1248 bool find_end_of_shared
)
1250 struct xfs_refcount_irec tmp
;
1255 trace_xfs_refcount_find_shared(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1258 /* By default, skip the whole range */
1259 *fbno
= NULLAGBLOCK
;
1262 /* Try to find a refcount extent that crosses the start */
1263 error
= xfs_refcount_lookup_le(cur
, agbno
, &have
);
1267 /* No left extent, look at the next one */
1268 error
= xfs_btree_increment(cur
, 0, &have
);
1274 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1277 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1279 /* If the extent ends before the start, look at the next one */
1280 if (tmp
.rc_startblock
+ tmp
.rc_blockcount
<= agbno
) {
1281 error
= xfs_btree_increment(cur
, 0, &have
);
1286 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1289 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1292 /* If the extent starts after the range we want, bail out */
1293 if (tmp
.rc_startblock
>= agbno
+ aglen
)
1296 /* We found the start of a shared extent! */
1297 if (tmp
.rc_startblock
< agbno
) {
1298 tmp
.rc_blockcount
-= (agbno
- tmp
.rc_startblock
);
1299 tmp
.rc_startblock
= agbno
;
1302 *fbno
= tmp
.rc_startblock
;
1303 *flen
= min(tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1304 if (!find_end_of_shared
)
1307 /* Otherwise, find the end of this shared extent */
1308 while (*fbno
+ *flen
< agbno
+ aglen
) {
1309 error
= xfs_btree_increment(cur
, 0, &have
);
1314 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1317 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1318 if (tmp
.rc_startblock
>= agbno
+ aglen
||
1319 tmp
.rc_startblock
!= *fbno
+ *flen
)
1321 *flen
= min(*flen
+ tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1325 trace_xfs_refcount_find_shared_result(cur
->bc_mp
,
1326 cur
->bc_private
.a
.agno
, *fbno
, *flen
);
1330 trace_xfs_refcount_find_shared_error(cur
->bc_mp
,
1331 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1336 * Recovering CoW Blocks After a Crash
1338 * Due to the way that the copy on write mechanism works, there's a window of
1339 * opportunity in which we can lose track of allocated blocks during a crash.
1340 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1341 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1342 * no longer in the free space btree but are not otherwise recorded anywhere
1343 * until the write completes and the blocks are mapped into the file. A crash
1344 * in between allocation and remapping results in the replacement blocks being
1345 * lost. This situation is exacerbated by the CoW extent size hint because
1346 * allocations can hang around for long time.
1348 * However, there is a place where we can record these allocations before they
1349 * become mappings -- the reference count btree. The btree does not record
1350 * extents with refcount == 1, so we can record allocations with a refcount of
1351 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1352 * no conflict with shared block records. These mappings should be created
1353 * when we allocate blocks to the CoW fork and deleted when they're removed
1354 * from the CoW fork.
1356 * Minor nit: records for in-progress CoW allocations and records for shared
1357 * extents must never be merged, to preserve the property that (except for CoW
1358 * allocations) there are no refcount btree entries with refcount == 1. The
1359 * only time this could potentially happen is when unsharing a block that's
1360 * adjacent to CoW allocations, so we must be careful to avoid this.
1362 * At mount time we recover lost CoW allocations by searching the refcount
1363 * btree for these refcount == 1 mappings. These represent CoW allocations
1364 * that were in progress at the time the filesystem went down, so we can free
1365 * them to get the space back.
1367 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1368 * several reasons -- first, EFIs pin the tail of the log and would have to be
1369 * periodically relogged to avoid filling up the log. Second, CoW completions
1370 * will have to file an EFD and create new EFIs for whatever remains in the
1371 * CoW fork; this partially takes care of (1) but extent-size reservations
1372 * will have to periodically relog even if there's no writeout in progress.
1373 * This can happen if the CoW extent size hint is set, which you really want.
1374 * Third, EFIs cannot currently be automatically relogged into newer
1375 * transactions to advance the log tail. Fourth, stuffing the log full of
1376 * EFIs places an upper bound on the number of CoW allocations that can be
1377 * held filesystem-wide at any given time. Recording them in the refcount
1378 * btree doesn't require us to maintain any state in memory and doesn't pin
1382 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1383 * in that they're not referenced anywhere else in the filesystem, so we
1384 * stash them in the refcount btree with a refcount of 1 until either file
1385 * remapping (or CoW cancellation) happens.
1388 xfs_refcount_adjust_cow_extents(
1389 struct xfs_btree_cur
*cur
,
1390 xfs_agblock_t agbno
,
1392 enum xfs_refc_adjust_op adj
)
1394 struct xfs_refcount_irec ext
, tmp
;
1396 int found_rec
, found_tmp
;
1401 /* Find any overlapping refcount records */
1402 error
= xfs_refcount_lookup_ge(cur
, agbno
, &found_rec
);
1405 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
1409 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
+
1411 ext
.rc_blockcount
= 0;
1412 ext
.rc_refcount
= 0;
1416 case XFS_REFCOUNT_ADJUST_COW_ALLOC
:
1417 /* Adding a CoW reservation, there should be nothing here. */
1418 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1419 ext
.rc_startblock
>= agbno
+ aglen
, out_error
);
1421 tmp
.rc_startblock
= agbno
;
1422 tmp
.rc_blockcount
= aglen
;
1423 tmp
.rc_refcount
= 1;
1424 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1425 cur
->bc_private
.a
.agno
, &tmp
);
1427 error
= xfs_refcount_insert(cur
, &tmp
,
1431 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1432 found_tmp
== 1, out_error
);
1434 case XFS_REFCOUNT_ADJUST_COW_FREE
:
1435 /* Removing a CoW reservation, there should be one extent. */
1436 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1437 ext
.rc_startblock
== agbno
, out_error
);
1438 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1439 ext
.rc_blockcount
== aglen
, out_error
);
1440 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1441 ext
.rc_refcount
== 1, out_error
);
1443 ext
.rc_refcount
= 0;
1444 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1445 cur
->bc_private
.a
.agno
, &ext
);
1446 error
= xfs_refcount_delete(cur
, &found_rec
);
1449 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1450 found_rec
== 1, out_error
);
1458 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
1459 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1464 * Add or remove refcount btree entries for CoW reservations.
1467 xfs_refcount_adjust_cow(
1468 struct xfs_btree_cur
*cur
,
1469 xfs_agblock_t agbno
,
1471 enum xfs_refc_adjust_op adj
)
1476 agbno
+= XFS_REFC_COW_START
;
1479 * Ensure that no rcextents cross the boundary of the adjustment range.
1481 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
1485 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1490 * Try to merge with the left or right extents of the range.
1492 error
= xfs_refcount_merge_extents(cur
, &agbno
, &aglen
, adj
,
1493 XFS_FIND_RCEXT_COW
, &shape_changed
);
1497 /* Now that we've taken care of the ends, adjust the middle extents */
1498 error
= xfs_refcount_adjust_cow_extents(cur
, agbno
, aglen
, adj
);
1505 trace_xfs_refcount_adjust_cow_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1511 * Record a CoW allocation in the refcount btree.
1514 __xfs_refcount_cow_alloc(
1515 struct xfs_btree_cur
*rcur
,
1516 xfs_agblock_t agbno
,
1519 trace_xfs_refcount_cow_increase(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1522 /* Add refcount btree reservation */
1523 return xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1524 XFS_REFCOUNT_ADJUST_COW_ALLOC
);
1528 * Remove a CoW allocation from the refcount btree.
1531 __xfs_refcount_cow_free(
1532 struct xfs_btree_cur
*rcur
,
1533 xfs_agblock_t agbno
,
1536 trace_xfs_refcount_cow_decrease(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1539 /* Remove refcount btree reservation */
1540 return xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1541 XFS_REFCOUNT_ADJUST_COW_FREE
);
1544 /* Record a CoW staging extent in the refcount btree. */
1546 xfs_refcount_alloc_cow_extent(
1547 struct xfs_trans
*tp
,
1551 struct xfs_mount
*mp
= tp
->t_mountp
;
1554 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1557 error
= __xfs_refcount_add(tp
, XFS_REFCOUNT_ALLOC_COW
, fsb
, len
);
1561 /* Add rmap entry */
1562 return xfs_rmap_alloc_extent(tp
, XFS_FSB_TO_AGNO(mp
, fsb
),
1563 XFS_FSB_TO_AGBNO(mp
, fsb
), len
, XFS_RMAP_OWN_COW
);
1566 /* Forget a CoW staging event in the refcount btree. */
1568 xfs_refcount_free_cow_extent(
1569 struct xfs_trans
*tp
,
1573 struct xfs_mount
*mp
= tp
->t_mountp
;
1576 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1579 /* Remove rmap entry */
1580 error
= xfs_rmap_free_extent(tp
, XFS_FSB_TO_AGNO(mp
, fsb
),
1581 XFS_FSB_TO_AGBNO(mp
, fsb
), len
, XFS_RMAP_OWN_COW
);
1585 return __xfs_refcount_add(tp
, XFS_REFCOUNT_FREE_COW
, fsb
, len
);
1588 struct xfs_refcount_recovery
{
1589 struct list_head rr_list
;
1590 struct xfs_refcount_irec rr_rrec
;
1593 /* Stuff an extent on the recovery list. */
1595 xfs_refcount_recover_extent(
1596 struct xfs_btree_cur
*cur
,
1597 union xfs_btree_rec
*rec
,
1600 struct list_head
*debris
= priv
;
1601 struct xfs_refcount_recovery
*rr
;
1603 if (be32_to_cpu(rec
->refc
.rc_refcount
) != 1)
1604 return -EFSCORRUPTED
;
1606 rr
= kmem_alloc(sizeof(struct xfs_refcount_recovery
), KM_SLEEP
);
1607 xfs_refcount_btrec_to_irec(rec
, &rr
->rr_rrec
);
1608 list_add_tail(&rr
->rr_list
, debris
);
1613 /* Find and remove leftover CoW reservations. */
1615 xfs_refcount_recover_cow_leftovers(
1616 struct xfs_mount
*mp
,
1617 xfs_agnumber_t agno
)
1619 struct xfs_trans
*tp
;
1620 struct xfs_btree_cur
*cur
;
1621 struct xfs_buf
*agbp
;
1622 struct xfs_refcount_recovery
*rr
, *n
;
1623 struct list_head debris
;
1624 union xfs_btree_irec low
;
1625 union xfs_btree_irec high
;
1627 xfs_agblock_t agbno
;
1630 if (mp
->m_sb
.sb_agblocks
>= XFS_REFC_COW_START
)
1633 INIT_LIST_HEAD(&debris
);
1636 * In this first part, we use an empty transaction to gather up
1637 * all the leftover CoW extents so that we can subsequently
1638 * delete them. The empty transaction is used to avoid
1639 * a buffer lock deadlock if there happens to be a loop in the
1640 * refcountbt because we're allowed to re-grab a buffer that is
1641 * already attached to our transaction. When we're done
1642 * recording the CoW debris we cancel the (empty) transaction
1643 * and everything goes away cleanly.
1645 error
= xfs_trans_alloc_empty(mp
, &tp
);
1649 error
= xfs_alloc_read_agf(mp
, tp
, agno
, 0, &agbp
);
1656 cur
= xfs_refcountbt_init_cursor(mp
, tp
, agbp
, agno
);
1658 /* Find all the leftover CoW staging extents. */
1659 memset(&low
, 0, sizeof(low
));
1660 memset(&high
, 0, sizeof(high
));
1661 low
.rc
.rc_startblock
= XFS_REFC_COW_START
;
1662 high
.rc
.rc_startblock
= -1U;
1663 error
= xfs_btree_query_range(cur
, &low
, &high
,
1664 xfs_refcount_recover_extent
, &debris
);
1665 xfs_btree_del_cursor(cur
, error
);
1666 xfs_trans_brelse(tp
, agbp
);
1667 xfs_trans_cancel(tp
);
1671 /* Now iterate the list to free the leftovers */
1672 list_for_each_entry_safe(rr
, n
, &debris
, rr_list
) {
1673 /* Set up transaction. */
1674 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0, 0, &tp
);
1678 trace_xfs_refcount_recover_extent(mp
, agno
, &rr
->rr_rrec
);
1680 /* Free the orphan record */
1681 agbno
= rr
->rr_rrec
.rc_startblock
- XFS_REFC_COW_START
;
1682 fsb
= XFS_AGB_TO_FSB(mp
, agno
, agbno
);
1683 error
= xfs_refcount_free_cow_extent(tp
, fsb
,
1684 rr
->rr_rrec
.rc_blockcount
);
1688 /* Free the block. */
1689 xfs_bmap_add_free(tp
, fsb
, rr
->rr_rrec
.rc_blockcount
, NULL
);
1691 error
= xfs_trans_commit(tp
);
1695 list_del(&rr
->rr_list
);
1701 xfs_trans_cancel(tp
);
1703 /* Free the leftover list */
1704 list_for_each_entry_safe(rr
, n
, &debris
, rr_list
) {
1705 list_del(&rr
->rr_list
);
1711 /* Is there a record covering a given extent? */
1713 xfs_refcount_has_record(
1714 struct xfs_btree_cur
*cur
,
1719 union xfs_btree_irec low
;
1720 union xfs_btree_irec high
;
1722 memset(&low
, 0, sizeof(low
));
1723 low
.rc
.rc_startblock
= bno
;
1724 memset(&high
, 0xFF, sizeof(high
));
1725 high
.rc
.rc_startblock
= bno
+ len
- 1;
1727 return xfs_btree_has_record(cur
, &low
, &high
, exists
);