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_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
36 #include "xfs_trans.h"
38 #include "xfs_refcount.h"
41 /* Allowable refcount adjustment amounts. */
42 enum xfs_refc_adjust_op
{
43 XFS_REFCOUNT_ADJUST_INCREASE
= 1,
44 XFS_REFCOUNT_ADJUST_DECREASE
= -1,
45 XFS_REFCOUNT_ADJUST_COW_ALLOC
= 0,
46 XFS_REFCOUNT_ADJUST_COW_FREE
= -1,
49 STATIC
int __xfs_refcount_cow_alloc(struct xfs_btree_cur
*rcur
,
50 xfs_agblock_t agbno
, xfs_extlen_t aglen
,
51 struct xfs_defer_ops
*dfops
);
52 STATIC
int __xfs_refcount_cow_free(struct xfs_btree_cur
*rcur
,
53 xfs_agblock_t agbno
, xfs_extlen_t aglen
,
54 struct xfs_defer_ops
*dfops
);
57 * Look up the first record less than or equal to [bno, len] in the btree
61 xfs_refcount_lookup_le(
62 struct xfs_btree_cur
*cur
,
66 trace_xfs_refcount_lookup(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
,
68 cur
->bc_rec
.rc
.rc_startblock
= bno
;
69 cur
->bc_rec
.rc
.rc_blockcount
= 0;
70 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
74 * Look up the first record greater than or equal to [bno, len] in the btree
78 xfs_refcount_lookup_ge(
79 struct xfs_btree_cur
*cur
,
83 trace_xfs_refcount_lookup(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
,
85 cur
->bc_rec
.rc
.rc_startblock
= bno
;
86 cur
->bc_rec
.rc
.rc_blockcount
= 0;
87 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
90 /* Convert on-disk record to in-core format. */
92 xfs_refcount_btrec_to_irec(
93 union xfs_btree_rec
*rec
,
94 struct xfs_refcount_irec
*irec
)
96 irec
->rc_startblock
= be32_to_cpu(rec
->refc
.rc_startblock
);
97 irec
->rc_blockcount
= be32_to_cpu(rec
->refc
.rc_blockcount
);
98 irec
->rc_refcount
= be32_to_cpu(rec
->refc
.rc_refcount
);
102 * Get the data from the pointed-to record.
105 xfs_refcount_get_rec(
106 struct xfs_btree_cur
*cur
,
107 struct xfs_refcount_irec
*irec
,
110 union xfs_btree_rec
*rec
;
113 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
114 if (!error
&& *stat
== 1) {
115 xfs_refcount_btrec_to_irec(rec
, irec
);
116 trace_xfs_refcount_get(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
123 * Update the record referred to by cur to the value given
124 * by [bno, len, refcount].
125 * This either works (return 0) or gets an EFSCORRUPTED error.
129 struct xfs_btree_cur
*cur
,
130 struct xfs_refcount_irec
*irec
)
132 union xfs_btree_rec rec
;
135 trace_xfs_refcount_update(cur
->bc_mp
, cur
->bc_private
.a
.agno
, irec
);
136 rec
.refc
.rc_startblock
= cpu_to_be32(irec
->rc_startblock
);
137 rec
.refc
.rc_blockcount
= cpu_to_be32(irec
->rc_blockcount
);
138 rec
.refc
.rc_refcount
= cpu_to_be32(irec
->rc_refcount
);
139 error
= xfs_btree_update(cur
, &rec
);
141 trace_xfs_refcount_update_error(cur
->bc_mp
,
142 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
147 * Insert the record referred to by cur to the value given
148 * by [bno, len, refcount].
149 * This either works (return 0) or gets an EFSCORRUPTED error.
153 struct xfs_btree_cur
*cur
,
154 struct xfs_refcount_irec
*irec
,
159 trace_xfs_refcount_insert(cur
->bc_mp
, cur
->bc_private
.a
.agno
, irec
);
160 cur
->bc_rec
.rc
.rc_startblock
= irec
->rc_startblock
;
161 cur
->bc_rec
.rc
.rc_blockcount
= irec
->rc_blockcount
;
162 cur
->bc_rec
.rc
.rc_refcount
= irec
->rc_refcount
;
163 error
= xfs_btree_insert(cur
, i
);
164 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
167 trace_xfs_refcount_insert_error(cur
->bc_mp
,
168 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
173 * Remove the record referred to by cur, then set the pointer to the spot
174 * where the record could be re-inserted, in case we want to increment or
175 * decrement the cursor.
176 * This either works (return 0) or gets an EFSCORRUPTED error.
180 struct xfs_btree_cur
*cur
,
183 struct xfs_refcount_irec irec
;
187 error
= xfs_refcount_get_rec(cur
, &irec
, &found_rec
);
190 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
191 trace_xfs_refcount_delete(cur
->bc_mp
, cur
->bc_private
.a
.agno
, &irec
);
192 error
= xfs_btree_delete(cur
, i
);
193 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
196 error
= xfs_refcount_lookup_ge(cur
, irec
.rc_startblock
, &found_rec
);
199 trace_xfs_refcount_delete_error(cur
->bc_mp
,
200 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
205 * Adjusting the Reference Count
207 * As stated elsewhere, the reference count btree (refcbt) stores
208 * >1 reference counts for extents of physical blocks. In this
209 * operation, we're either raising or lowering the reference count of
210 * some subrange stored in the tree:
212 * <------ adjustment range ------>
213 * ----+ +---+-----+ +--+--------+---------
214 * 2 | | 3 | 4 | |17| 55 | 10
215 * ----+ +---+-----+ +--+--------+---------
216 * X axis is physical blocks number;
217 * reference counts are the numbers inside the rectangles
219 * The first thing we need to do is to ensure that there are no
220 * refcount extents crossing either boundary of the range to be
221 * adjusted. For any extent that does cross a boundary, split it into
222 * two extents so that we can increment the refcount of one of the
225 * <------ adjustment range ------>
226 * ----+ +---+-----+ +--+--------+----+----
227 * 2 | | 3 | 2 | |17| 55 | 10 | 10
228 * ----+ +---+-----+ +--+--------+----+----
230 * For this next step, let's assume that all the physical blocks in
231 * the adjustment range are mapped to a file and are therefore in use
232 * at least once. Therefore, we can infer that any gap in the
233 * refcount tree within the adjustment range represents a physical
234 * extent with refcount == 1:
236 * <------ adjustment range ------>
237 * ----+---+---+-----+-+--+--------+----+----
238 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
239 * ----+---+---+-----+-+--+--------+----+----
242 * For each extent that falls within the interval range, figure out
243 * which extent is to the left or the right of that extent. Now we
244 * have a left, current, and right extent. If the new reference count
245 * of the center extent enables us to merge left, center, and right
246 * into one record covering all three, do so. If the center extent is
247 * at the left end of the range, abuts the left extent, and its new
248 * reference count matches the left extent's record, then merge them.
249 * If the center extent is at the right end of the range, abuts the
250 * right extent, and the reference counts match, merge those. In the
251 * example, we can left merge (assuming an increment operation):
253 * <------ adjustment range ------>
254 * --------+---+-----+-+--+--------+----+----
255 * 2 | 3 | 2 |1|17| 55 | 10 | 10
256 * --------+---+-----+-+--+--------+----+----
259 * For all other extents within the range, adjust the reference count
260 * or delete it if the refcount falls below 2. If we were
261 * incrementing, the end result looks like this:
263 * <------ adjustment range ------>
264 * --------+---+-----+-+--+--------+----+----
265 * 2 | 4 | 3 |2|18| 56 | 11 | 10
266 * --------+---+-----+-+--+--------+----+----
268 * The result of a decrement operation looks as such:
270 * <------ adjustment range ------>
271 * ----+ +---+ +--+--------+----+----
272 * 2 | | 2 | |16| 54 | 9 | 10
273 * ----+ +---+ +--+--------+----+----
276 * The blocks marked "D" are freed; the blocks marked "1" are only
277 * referenced once and therefore the record is removed from the
281 /* Next block after this extent. */
282 static inline xfs_agblock_t
284 struct xfs_refcount_irec
*rc
)
286 return rc
->rc_startblock
+ rc
->rc_blockcount
;
290 * Split a refcount extent that crosses agbno.
293 xfs_refcount_split_extent(
294 struct xfs_btree_cur
*cur
,
298 struct xfs_refcount_irec rcext
, tmp
;
302 *shape_changed
= false;
303 error
= xfs_refcount_lookup_le(cur
, agbno
, &found_rec
);
309 error
= xfs_refcount_get_rec(cur
, &rcext
, &found_rec
);
312 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
313 if (rcext
.rc_startblock
== agbno
|| xfs_refc_next(&rcext
) <= agbno
)
316 *shape_changed
= true;
317 trace_xfs_refcount_split_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
320 /* Establish the right extent. */
322 tmp
.rc_startblock
= agbno
;
323 tmp
.rc_blockcount
-= (agbno
- rcext
.rc_startblock
);
324 error
= xfs_refcount_update(cur
, &tmp
);
328 /* Insert the left extent. */
330 tmp
.rc_blockcount
= agbno
- rcext
.rc_startblock
;
331 error
= xfs_refcount_insert(cur
, &tmp
, &found_rec
);
334 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
338 trace_xfs_refcount_split_extent_error(cur
->bc_mp
,
339 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
344 * Merge the left, center, and right extents.
347 xfs_refcount_merge_center_extents(
348 struct xfs_btree_cur
*cur
,
349 struct xfs_refcount_irec
*left
,
350 struct xfs_refcount_irec
*center
,
351 struct xfs_refcount_irec
*right
,
352 unsigned long long extlen
,
353 xfs_agblock_t
*agbno
,
359 trace_xfs_refcount_merge_center_extents(cur
->bc_mp
,
360 cur
->bc_private
.a
.agno
, left
, center
, right
);
363 * Make sure the center and right extents are not in the btree.
364 * If the center extent was synthesized, the first delete call
365 * removes the right extent and we skip the second deletion.
366 * If center and right were in the btree, then the first delete
367 * call removes the center and the second one removes the right
370 error
= xfs_refcount_lookup_ge(cur
, center
->rc_startblock
,
374 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
376 error
= xfs_refcount_delete(cur
, &found_rec
);
379 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
381 if (center
->rc_refcount
> 1) {
382 error
= xfs_refcount_delete(cur
, &found_rec
);
385 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
389 /* Enlarge the left extent. */
390 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
394 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
396 left
->rc_blockcount
= extlen
;
397 error
= xfs_refcount_update(cur
, left
);
405 trace_xfs_refcount_merge_center_extents_error(cur
->bc_mp
,
406 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
411 * Merge with the left extent.
414 xfs_refcount_merge_left_extent(
415 struct xfs_btree_cur
*cur
,
416 struct xfs_refcount_irec
*left
,
417 struct xfs_refcount_irec
*cleft
,
418 xfs_agblock_t
*agbno
,
424 trace_xfs_refcount_merge_left_extent(cur
->bc_mp
,
425 cur
->bc_private
.a
.agno
, left
, cleft
);
427 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
428 if (cleft
->rc_refcount
> 1) {
429 error
= xfs_refcount_lookup_le(cur
, cleft
->rc_startblock
,
433 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
436 error
= xfs_refcount_delete(cur
, &found_rec
);
439 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
443 /* Enlarge the left extent. */
444 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
448 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
450 left
->rc_blockcount
+= cleft
->rc_blockcount
;
451 error
= xfs_refcount_update(cur
, left
);
455 *agbno
+= cleft
->rc_blockcount
;
456 *aglen
-= cleft
->rc_blockcount
;
460 trace_xfs_refcount_merge_left_extent_error(cur
->bc_mp
,
461 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
466 * Merge with the right extent.
469 xfs_refcount_merge_right_extent(
470 struct xfs_btree_cur
*cur
,
471 struct xfs_refcount_irec
*right
,
472 struct xfs_refcount_irec
*cright
,
473 xfs_agblock_t
*agbno
,
479 trace_xfs_refcount_merge_right_extent(cur
->bc_mp
,
480 cur
->bc_private
.a
.agno
, cright
, right
);
483 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
486 if (cright
->rc_refcount
> 1) {
487 error
= xfs_refcount_lookup_le(cur
, cright
->rc_startblock
,
491 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
494 error
= xfs_refcount_delete(cur
, &found_rec
);
497 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
501 /* Enlarge the right extent. */
502 error
= xfs_refcount_lookup_le(cur
, right
->rc_startblock
,
506 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
508 right
->rc_startblock
-= cright
->rc_blockcount
;
509 right
->rc_blockcount
+= cright
->rc_blockcount
;
510 error
= xfs_refcount_update(cur
, right
);
514 *aglen
-= cright
->rc_blockcount
;
518 trace_xfs_refcount_merge_right_extent_error(cur
->bc_mp
,
519 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
523 #define XFS_FIND_RCEXT_SHARED 1
524 #define XFS_FIND_RCEXT_COW 2
526 * Find the left extent and the one after it (cleft). This function assumes
527 * that we've already split any extent crossing agbno.
530 xfs_refcount_find_left_extents(
531 struct xfs_btree_cur
*cur
,
532 struct xfs_refcount_irec
*left
,
533 struct xfs_refcount_irec
*cleft
,
538 struct xfs_refcount_irec tmp
;
542 left
->rc_startblock
= cleft
->rc_startblock
= NULLAGBLOCK
;
543 error
= xfs_refcount_lookup_le(cur
, agbno
- 1, &found_rec
);
549 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
552 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
554 if (xfs_refc_next(&tmp
) != agbno
)
556 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
558 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
560 /* We have a left extent; retrieve (or invent) the next right one */
563 error
= xfs_btree_increment(cur
, 0, &found_rec
);
567 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
570 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
573 /* if tmp starts at the end of our range, just use that */
574 if (tmp
.rc_startblock
== agbno
)
578 * There's a gap in the refcntbt at the start of the
579 * range we're interested in (refcount == 1) so
580 * synthesize the implied extent and pass it back.
581 * We assume here that the agbno/aglen range was
582 * passed in from a data fork extent mapping and
583 * therefore is allocated to exactly one owner.
585 cleft
->rc_startblock
= agbno
;
586 cleft
->rc_blockcount
= min(aglen
,
587 tmp
.rc_startblock
- agbno
);
588 cleft
->rc_refcount
= 1;
592 * No extents, so pretend that there's one covering the whole
595 cleft
->rc_startblock
= agbno
;
596 cleft
->rc_blockcount
= aglen
;
597 cleft
->rc_refcount
= 1;
599 trace_xfs_refcount_find_left_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
604 trace_xfs_refcount_find_left_extent_error(cur
->bc_mp
,
605 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
610 * Find the right extent and the one before it (cright). This function
611 * assumes that we've already split any extents crossing agbno + aglen.
614 xfs_refcount_find_right_extents(
615 struct xfs_btree_cur
*cur
,
616 struct xfs_refcount_irec
*right
,
617 struct xfs_refcount_irec
*cright
,
622 struct xfs_refcount_irec tmp
;
626 right
->rc_startblock
= cright
->rc_startblock
= NULLAGBLOCK
;
627 error
= xfs_refcount_lookup_ge(cur
, agbno
+ aglen
, &found_rec
);
633 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
636 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
638 if (tmp
.rc_startblock
!= agbno
+ aglen
)
640 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
642 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
644 /* We have a right extent; retrieve (or invent) the next left one */
647 error
= xfs_btree_decrement(cur
, 0, &found_rec
);
651 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
654 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
657 /* if tmp ends at the end of our range, just use that */
658 if (xfs_refc_next(&tmp
) == agbno
+ aglen
)
662 * There's a gap in the refcntbt at the end of the
663 * range we're interested in (refcount == 1) so
664 * create the implied extent and pass it back.
665 * We assume here that the agbno/aglen range was
666 * passed in from a data fork extent mapping and
667 * therefore is allocated to exactly one owner.
669 cright
->rc_startblock
= max(agbno
, xfs_refc_next(&tmp
));
670 cright
->rc_blockcount
= right
->rc_startblock
-
671 cright
->rc_startblock
;
672 cright
->rc_refcount
= 1;
676 * No extents, so pretend that there's one covering the whole
679 cright
->rc_startblock
= agbno
;
680 cright
->rc_blockcount
= aglen
;
681 cright
->rc_refcount
= 1;
683 trace_xfs_refcount_find_right_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
684 cright
, right
, agbno
+ aglen
);
688 trace_xfs_refcount_find_right_extent_error(cur
->bc_mp
,
689 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
693 /* Is this extent valid? */
696 struct xfs_refcount_irec
*rc
)
698 return rc
->rc_startblock
!= NULLAGBLOCK
;
702 * Try to merge with any extents on the boundaries of the adjustment range.
705 xfs_refcount_merge_extents(
706 struct xfs_btree_cur
*cur
,
707 xfs_agblock_t
*agbno
,
709 enum xfs_refc_adjust_op adjust
,
713 struct xfs_refcount_irec left
= {0}, cleft
= {0};
714 struct xfs_refcount_irec cright
= {0}, right
= {0};
716 unsigned long long ulen
;
719 *shape_changed
= false;
721 * Find the extent just below agbno [left], just above agbno [cleft],
722 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
725 error
= xfs_refcount_find_left_extents(cur
, &left
, &cleft
, *agbno
,
729 error
= xfs_refcount_find_right_extents(cur
, &right
, &cright
, *agbno
,
734 /* No left or right extent to merge; exit. */
735 if (!xfs_refc_valid(&left
) && !xfs_refc_valid(&right
))
738 cequal
= (cleft
.rc_startblock
== cright
.rc_startblock
) &&
739 (cleft
.rc_blockcount
== cright
.rc_blockcount
);
741 /* Try to merge left, cleft, and right. cleft must == cright. */
742 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
+
744 if (xfs_refc_valid(&left
) && xfs_refc_valid(&right
) &&
745 xfs_refc_valid(&cleft
) && xfs_refc_valid(&cright
) && cequal
&&
746 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
747 right
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
748 ulen
< MAXREFCEXTLEN
) {
749 *shape_changed
= true;
750 return xfs_refcount_merge_center_extents(cur
, &left
, &cleft
,
751 &right
, ulen
, agbno
, aglen
);
754 /* Try to merge left and cleft. */
755 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
;
756 if (xfs_refc_valid(&left
) && xfs_refc_valid(&cleft
) &&
757 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
758 ulen
< MAXREFCEXTLEN
) {
759 *shape_changed
= true;
760 error
= xfs_refcount_merge_left_extent(cur
, &left
, &cleft
,
766 * If we just merged left + cleft and cleft == cright,
767 * we no longer have a cright to merge with right. We're done.
773 /* Try to merge cright and right. */
774 ulen
= (unsigned long long)right
.rc_blockcount
+ cright
.rc_blockcount
;
775 if (xfs_refc_valid(&right
) && xfs_refc_valid(&cright
) &&
776 right
.rc_refcount
== cright
.rc_refcount
+ adjust
&&
777 ulen
< MAXREFCEXTLEN
) {
778 *shape_changed
= true;
779 return xfs_refcount_merge_right_extent(cur
, &right
, &cright
,
787 * While we're adjusting the refcounts records of an extent, we have
788 * to keep an eye on the number of extents we're dirtying -- run too
789 * many in a single transaction and we'll exceed the transaction's
790 * reservation and crash the fs. Each record adds 12 bytes to the
791 * log (plus any key updates) so we'll conservatively assume 24 bytes
792 * per record. We must also leave space for btree splits on both ends
793 * of the range and space for the CUD and a new CUI.
795 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
796 * true incorrectly is a shutdown FS; the penalty for guessing false
797 * incorrectly is more transaction rolls than might be necessary.
798 * Be conservative here.
801 xfs_refcount_still_have_space(
802 struct xfs_btree_cur
*cur
)
804 unsigned long overhead
;
806 overhead
= cur
->bc_private
.a
.priv
.refc
.shape_changes
*
807 xfs_allocfree_log_count(cur
->bc_mp
, 1);
808 overhead
*= cur
->bc_mp
->m_sb
.sb_blocksize
;
811 * Only allow 2 refcount extent updates per transaction if the
812 * refcount continue update "error" has been injected.
814 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
> 2 &&
815 XFS_TEST_ERROR(false, cur
->bc_mp
,
816 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE
,
817 XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE
))
820 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
== 0)
822 else if (overhead
> cur
->bc_tp
->t_log_res
)
824 return cur
->bc_tp
->t_log_res
- overhead
>
825 cur
->bc_private
.a
.priv
.refc
.nr_ops
* 32;
829 * Adjust the refcounts of middle extents. At this point we should have
830 * split extents that crossed the adjustment range; merged with adjacent
831 * extents; and updated agbno/aglen to reflect the merges. Therefore,
832 * all we have to do is update the extents inside [agbno, agbno + aglen].
835 xfs_refcount_adjust_extents(
836 struct xfs_btree_cur
*cur
,
837 xfs_agblock_t
*agbno
,
839 enum xfs_refc_adjust_op adj
,
840 struct xfs_defer_ops
*dfops
,
841 struct xfs_owner_info
*oinfo
)
843 struct xfs_refcount_irec ext
, tmp
;
845 int found_rec
, found_tmp
;
848 /* Merging did all the work already. */
852 error
= xfs_refcount_lookup_ge(cur
, *agbno
, &found_rec
);
856 while (*aglen
> 0 && xfs_refcount_still_have_space(cur
)) {
857 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
861 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
;
862 ext
.rc_blockcount
= 0;
867 * Deal with a hole in the refcount tree; if a file maps to
868 * these blocks and there's no refcountbt record, pretend that
869 * there is one with refcount == 1.
871 if (ext
.rc_startblock
!= *agbno
) {
872 tmp
.rc_startblock
= *agbno
;
873 tmp
.rc_blockcount
= min(*aglen
,
874 ext
.rc_startblock
- *agbno
);
875 tmp
.rc_refcount
= 1 + adj
;
876 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
877 cur
->bc_private
.a
.agno
, &tmp
);
880 * Either cover the hole (increment) or
881 * delete the range (decrement).
883 if (tmp
.rc_refcount
) {
884 error
= xfs_refcount_insert(cur
, &tmp
,
888 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
889 found_tmp
== 1, out_error
);
890 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
892 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
893 cur
->bc_private
.a
.agno
,
895 xfs_bmap_add_free(cur
->bc_mp
, dfops
, fsbno
,
896 tmp
.rc_blockcount
, oinfo
);
899 (*agbno
) += tmp
.rc_blockcount
;
900 (*aglen
) -= tmp
.rc_blockcount
;
902 error
= xfs_refcount_lookup_ge(cur
, *agbno
,
908 /* Stop if there's nothing left to modify */
909 if (*aglen
== 0 || !xfs_refcount_still_have_space(cur
))
913 * Adjust the reference count and either update the tree
914 * (incr) or free the blocks (decr).
916 if (ext
.rc_refcount
== MAXREFCOUNT
)
918 ext
.rc_refcount
+= adj
;
919 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
920 cur
->bc_private
.a
.agno
, &ext
);
921 if (ext
.rc_refcount
> 1) {
922 error
= xfs_refcount_update(cur
, &ext
);
925 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
926 } else if (ext
.rc_refcount
== 1) {
927 error
= xfs_refcount_delete(cur
, &found_rec
);
930 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
931 found_rec
== 1, out_error
);
932 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
935 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
936 cur
->bc_private
.a
.agno
,
938 xfs_bmap_add_free(cur
->bc_mp
, dfops
, fsbno
,
939 ext
.rc_blockcount
, oinfo
);
943 error
= xfs_btree_increment(cur
, 0, &found_rec
);
948 (*agbno
) += ext
.rc_blockcount
;
949 (*aglen
) -= ext
.rc_blockcount
;
954 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
955 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
959 /* Adjust the reference count of a range of AG blocks. */
962 struct xfs_btree_cur
*cur
,
965 xfs_agblock_t
*new_agbno
,
966 xfs_extlen_t
*new_aglen
,
967 enum xfs_refc_adjust_op adj
,
968 struct xfs_defer_ops
*dfops
,
969 struct xfs_owner_info
*oinfo
)
972 int shape_changes
= 0;
977 if (adj
== XFS_REFCOUNT_ADJUST_INCREASE
)
978 trace_xfs_refcount_increase(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
981 trace_xfs_refcount_decrease(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
985 * Ensure that no rcextents cross the boundary of the adjustment range.
987 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
993 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1000 * Try to merge with the left or right extents of the range.
1002 error
= xfs_refcount_merge_extents(cur
, new_agbno
, new_aglen
, adj
,
1003 XFS_FIND_RCEXT_SHARED
, &shape_changed
);
1009 cur
->bc_private
.a
.priv
.refc
.shape_changes
++;
1011 /* Now that we've taken care of the ends, adjust the middle extents */
1012 error
= xfs_refcount_adjust_extents(cur
, new_agbno
, new_aglen
,
1020 trace_xfs_refcount_adjust_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1025 /* Clean up after calling xfs_refcount_finish_one. */
1027 xfs_refcount_finish_one_cleanup(
1028 struct xfs_trans
*tp
,
1029 struct xfs_btree_cur
*rcur
,
1032 struct xfs_buf
*agbp
;
1036 agbp
= rcur
->bc_private
.a
.agbp
;
1037 xfs_btree_del_cursor(rcur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1039 xfs_trans_brelse(tp
, agbp
);
1043 * Process one of the deferred refcount operations. We pass back the
1044 * btree cursor to maintain our lock on the btree between calls.
1045 * This saves time and eliminates a buffer deadlock between the
1046 * superblock and the AGF because we'll always grab them in the same
1050 xfs_refcount_finish_one(
1051 struct xfs_trans
*tp
,
1052 struct xfs_defer_ops
*dfops
,
1053 enum xfs_refcount_intent_type type
,
1054 xfs_fsblock_t startblock
,
1055 xfs_extlen_t blockcount
,
1056 xfs_fsblock_t
*new_fsb
,
1057 xfs_extlen_t
*new_len
,
1058 struct xfs_btree_cur
**pcur
)
1060 struct xfs_mount
*mp
= tp
->t_mountp
;
1061 struct xfs_btree_cur
*rcur
;
1062 struct xfs_buf
*agbp
= NULL
;
1064 xfs_agnumber_t agno
;
1066 xfs_agblock_t new_agbno
;
1067 unsigned long nr_ops
= 0;
1068 int shape_changes
= 0;
1070 agno
= XFS_FSB_TO_AGNO(mp
, startblock
);
1071 ASSERT(agno
!= NULLAGNUMBER
);
1072 bno
= XFS_FSB_TO_AGBNO(mp
, startblock
);
1074 trace_xfs_refcount_deferred(mp
, XFS_FSB_TO_AGNO(mp
, startblock
),
1075 type
, XFS_FSB_TO_AGBNO(mp
, startblock
),
1078 if (XFS_TEST_ERROR(false, mp
,
1079 XFS_ERRTAG_REFCOUNT_FINISH_ONE
,
1080 XFS_RANDOM_REFCOUNT_FINISH_ONE
))
1084 * If we haven't gotten a cursor or the cursor AG doesn't match
1085 * the startblock, get one now.
1088 if (rcur
!= NULL
&& rcur
->bc_private
.a
.agno
!= agno
) {
1089 nr_ops
= rcur
->bc_private
.a
.priv
.refc
.nr_ops
;
1090 shape_changes
= rcur
->bc_private
.a
.priv
.refc
.shape_changes
;
1091 xfs_refcount_finish_one_cleanup(tp
, rcur
, 0);
1096 error
= xfs_alloc_read_agf(tp
->t_mountp
, tp
, agno
,
1097 XFS_ALLOC_FLAG_FREEING
, &agbp
);
1101 return -EFSCORRUPTED
;
1103 rcur
= xfs_refcountbt_init_cursor(mp
, tp
, agbp
, agno
, dfops
);
1108 rcur
->bc_private
.a
.priv
.refc
.nr_ops
= nr_ops
;
1109 rcur
->bc_private
.a
.priv
.refc
.shape_changes
= shape_changes
;
1114 case XFS_REFCOUNT_INCREASE
:
1115 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1116 new_len
, XFS_REFCOUNT_ADJUST_INCREASE
, dfops
, NULL
);
1117 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1119 case XFS_REFCOUNT_DECREASE
:
1120 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1121 new_len
, XFS_REFCOUNT_ADJUST_DECREASE
, dfops
, NULL
);
1122 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1124 case XFS_REFCOUNT_ALLOC_COW
:
1125 *new_fsb
= startblock
+ blockcount
;
1127 error
= __xfs_refcount_cow_alloc(rcur
, bno
, blockcount
, dfops
);
1129 case XFS_REFCOUNT_FREE_COW
:
1130 *new_fsb
= startblock
+ blockcount
;
1132 error
= __xfs_refcount_cow_free(rcur
, bno
, blockcount
, dfops
);
1136 error
= -EFSCORRUPTED
;
1138 if (!error
&& *new_len
> 0)
1139 trace_xfs_refcount_finish_one_leftover(mp
, agno
, type
,
1140 bno
, blockcount
, new_agbno
, *new_len
);
1144 xfs_trans_brelse(tp
, agbp
);
1150 * Record a refcount intent for later processing.
1154 struct xfs_mount
*mp
,
1155 struct xfs_defer_ops
*dfops
,
1156 enum xfs_refcount_intent_type type
,
1157 xfs_fsblock_t startblock
,
1158 xfs_extlen_t blockcount
)
1160 struct xfs_refcount_intent
*ri
;
1162 trace_xfs_refcount_defer(mp
, XFS_FSB_TO_AGNO(mp
, startblock
),
1163 type
, XFS_FSB_TO_AGBNO(mp
, startblock
),
1166 ri
= kmem_alloc(sizeof(struct xfs_refcount_intent
),
1167 KM_SLEEP
| KM_NOFS
);
1168 INIT_LIST_HEAD(&ri
->ri_list
);
1170 ri
->ri_startblock
= startblock
;
1171 ri
->ri_blockcount
= blockcount
;
1173 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_REFCOUNT
, &ri
->ri_list
);
1178 * Increase the reference count of the blocks backing a file's extent.
1181 xfs_refcount_increase_extent(
1182 struct xfs_mount
*mp
,
1183 struct xfs_defer_ops
*dfops
,
1184 struct xfs_bmbt_irec
*PREV
)
1186 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1189 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_INCREASE
,
1190 PREV
->br_startblock
, PREV
->br_blockcount
);
1194 * Decrease the reference count of the blocks backing a file's extent.
1197 xfs_refcount_decrease_extent(
1198 struct xfs_mount
*mp
,
1199 struct xfs_defer_ops
*dfops
,
1200 struct xfs_bmbt_irec
*PREV
)
1202 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1205 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_DECREASE
,
1206 PREV
->br_startblock
, PREV
->br_blockcount
);
1210 * Given an AG extent, find the lowest-numbered run of shared blocks
1211 * within that range and return the range in fbno/flen. If
1212 * find_end_of_shared is set, return the longest contiguous extent of
1213 * shared blocks; if not, just return the first extent we find. If no
1214 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1215 * and 0, respectively.
1218 xfs_refcount_find_shared(
1219 struct xfs_btree_cur
*cur
,
1220 xfs_agblock_t agbno
,
1222 xfs_agblock_t
*fbno
,
1224 bool find_end_of_shared
)
1226 struct xfs_refcount_irec tmp
;
1231 trace_xfs_refcount_find_shared(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1234 /* By default, skip the whole range */
1235 *fbno
= NULLAGBLOCK
;
1238 /* Try to find a refcount extent that crosses the start */
1239 error
= xfs_refcount_lookup_le(cur
, agbno
, &have
);
1243 /* No left extent, look at the next one */
1244 error
= xfs_btree_increment(cur
, 0, &have
);
1250 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1253 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1255 /* If the extent ends before the start, look at the next one */
1256 if (tmp
.rc_startblock
+ tmp
.rc_blockcount
<= agbno
) {
1257 error
= xfs_btree_increment(cur
, 0, &have
);
1262 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1265 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1268 /* If the extent starts after the range we want, bail out */
1269 if (tmp
.rc_startblock
>= agbno
+ aglen
)
1272 /* We found the start of a shared extent! */
1273 if (tmp
.rc_startblock
< agbno
) {
1274 tmp
.rc_blockcount
-= (agbno
- tmp
.rc_startblock
);
1275 tmp
.rc_startblock
= agbno
;
1278 *fbno
= tmp
.rc_startblock
;
1279 *flen
= min(tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1280 if (!find_end_of_shared
)
1283 /* Otherwise, find the end of this shared extent */
1284 while (*fbno
+ *flen
< agbno
+ aglen
) {
1285 error
= xfs_btree_increment(cur
, 0, &have
);
1290 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1293 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1294 if (tmp
.rc_startblock
>= agbno
+ aglen
||
1295 tmp
.rc_startblock
!= *fbno
+ *flen
)
1297 *flen
= min(*flen
+ tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1301 trace_xfs_refcount_find_shared_result(cur
->bc_mp
,
1302 cur
->bc_private
.a
.agno
, *fbno
, *flen
);
1306 trace_xfs_refcount_find_shared_error(cur
->bc_mp
,
1307 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1312 * Recovering CoW Blocks After a Crash
1314 * Due to the way that the copy on write mechanism works, there's a window of
1315 * opportunity in which we can lose track of allocated blocks during a crash.
1316 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1317 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1318 * no longer in the free space btree but are not otherwise recorded anywhere
1319 * until the write completes and the blocks are mapped into the file. A crash
1320 * in between allocation and remapping results in the replacement blocks being
1321 * lost. This situation is exacerbated by the CoW extent size hint because
1322 * allocations can hang around for long time.
1324 * However, there is a place where we can record these allocations before they
1325 * become mappings -- the reference count btree. The btree does not record
1326 * extents with refcount == 1, so we can record allocations with a refcount of
1327 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1328 * no conflict with shared block records. These mappings should be created
1329 * when we allocate blocks to the CoW fork and deleted when they're removed
1330 * from the CoW fork.
1332 * Minor nit: records for in-progress CoW allocations and records for shared
1333 * extents must never be merged, to preserve the property that (except for CoW
1334 * allocations) there are no refcount btree entries with refcount == 1. The
1335 * only time this could potentially happen is when unsharing a block that's
1336 * adjacent to CoW allocations, so we must be careful to avoid this.
1338 * At mount time we recover lost CoW allocations by searching the refcount
1339 * btree for these refcount == 1 mappings. These represent CoW allocations
1340 * that were in progress at the time the filesystem went down, so we can free
1341 * them to get the space back.
1343 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1344 * several reasons -- first, EFIs pin the tail of the log and would have to be
1345 * periodically relogged to avoid filling up the log. Second, CoW completions
1346 * will have to file an EFD and create new EFIs for whatever remains in the
1347 * CoW fork; this partially takes care of (1) but extent-size reservations
1348 * will have to periodically relog even if there's no writeout in progress.
1349 * This can happen if the CoW extent size hint is set, which you really want.
1350 * Third, EFIs cannot currently be automatically relogged into newer
1351 * transactions to advance the log tail. Fourth, stuffing the log full of
1352 * EFIs places an upper bound on the number of CoW allocations that can be
1353 * held filesystem-wide at any given time. Recording them in the refcount
1354 * btree doesn't require us to maintain any state in memory and doesn't pin
1358 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1359 * in that they're not referenced anywhere else in the filesystem, so we
1360 * stash them in the refcount btree with a refcount of 1 until either file
1361 * remapping (or CoW cancellation) happens.
1364 xfs_refcount_adjust_cow_extents(
1365 struct xfs_btree_cur
*cur
,
1366 xfs_agblock_t agbno
,
1368 enum xfs_refc_adjust_op adj
,
1369 struct xfs_defer_ops
*dfops
,
1370 struct xfs_owner_info
*oinfo
)
1372 struct xfs_refcount_irec ext
, tmp
;
1374 int found_rec
, found_tmp
;
1379 /* Find any overlapping refcount records */
1380 error
= xfs_refcount_lookup_ge(cur
, agbno
, &found_rec
);
1383 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
1387 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
+
1389 ext
.rc_blockcount
= 0;
1390 ext
.rc_refcount
= 0;
1394 case XFS_REFCOUNT_ADJUST_COW_ALLOC
:
1395 /* Adding a CoW reservation, there should be nothing here. */
1396 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1397 ext
.rc_startblock
>= agbno
+ aglen
, out_error
);
1399 tmp
.rc_startblock
= agbno
;
1400 tmp
.rc_blockcount
= aglen
;
1401 tmp
.rc_refcount
= 1;
1402 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1403 cur
->bc_private
.a
.agno
, &tmp
);
1405 error
= xfs_refcount_insert(cur
, &tmp
,
1409 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1410 found_tmp
== 1, out_error
);
1412 case XFS_REFCOUNT_ADJUST_COW_FREE
:
1413 /* Removing a CoW reservation, there should be one extent. */
1414 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1415 ext
.rc_startblock
== agbno
, out_error
);
1416 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1417 ext
.rc_blockcount
== aglen
, out_error
);
1418 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1419 ext
.rc_refcount
== 1, out_error
);
1421 ext
.rc_refcount
= 0;
1422 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1423 cur
->bc_private
.a
.agno
, &ext
);
1424 error
= xfs_refcount_delete(cur
, &found_rec
);
1427 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1428 found_rec
== 1, out_error
);
1436 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
1437 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1442 * Add or remove refcount btree entries for CoW reservations.
1445 xfs_refcount_adjust_cow(
1446 struct xfs_btree_cur
*cur
,
1447 xfs_agblock_t agbno
,
1449 enum xfs_refc_adjust_op adj
,
1450 struct xfs_defer_ops
*dfops
)
1455 agbno
+= XFS_REFC_COW_START
;
1458 * Ensure that no rcextents cross the boundary of the adjustment range.
1460 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
1464 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1469 * Try to merge with the left or right extents of the range.
1471 error
= xfs_refcount_merge_extents(cur
, &agbno
, &aglen
, adj
,
1472 XFS_FIND_RCEXT_COW
, &shape_changed
);
1476 /* Now that we've taken care of the ends, adjust the middle extents */
1477 error
= xfs_refcount_adjust_cow_extents(cur
, agbno
, aglen
, adj
,
1485 trace_xfs_refcount_adjust_cow_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1491 * Record a CoW allocation in the refcount btree.
1494 __xfs_refcount_cow_alloc(
1495 struct xfs_btree_cur
*rcur
,
1496 xfs_agblock_t agbno
,
1498 struct xfs_defer_ops
*dfops
)
1502 trace_xfs_refcount_cow_increase(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1505 /* Add refcount btree reservation */
1506 error
= xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1507 XFS_REFCOUNT_ADJUST_COW_ALLOC
, dfops
);
1511 /* Add rmap entry */
1512 if (xfs_sb_version_hasrmapbt(&rcur
->bc_mp
->m_sb
)) {
1513 error
= xfs_rmap_alloc_extent(rcur
->bc_mp
, dfops
,
1514 rcur
->bc_private
.a
.agno
,
1515 agbno
, aglen
, XFS_RMAP_OWN_COW
);
1524 * Remove a CoW allocation from the refcount btree.
1527 __xfs_refcount_cow_free(
1528 struct xfs_btree_cur
*rcur
,
1529 xfs_agblock_t agbno
,
1531 struct xfs_defer_ops
*dfops
)
1535 trace_xfs_refcount_cow_decrease(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1538 /* Remove refcount btree reservation */
1539 error
= xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1540 XFS_REFCOUNT_ADJUST_COW_FREE
, dfops
);
1544 /* Remove rmap entry */
1545 if (xfs_sb_version_hasrmapbt(&rcur
->bc_mp
->m_sb
)) {
1546 error
= xfs_rmap_free_extent(rcur
->bc_mp
, dfops
,
1547 rcur
->bc_private
.a
.agno
,
1548 agbno
, aglen
, XFS_RMAP_OWN_COW
);
1556 /* Record a CoW staging extent in the refcount btree. */
1558 xfs_refcount_alloc_cow_extent(
1559 struct xfs_mount
*mp
,
1560 struct xfs_defer_ops
*dfops
,
1564 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1567 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_ALLOC_COW
,
1571 /* Forget a CoW staging event in the refcount btree. */
1573 xfs_refcount_free_cow_extent(
1574 struct xfs_mount
*mp
,
1575 struct xfs_defer_ops
*dfops
,
1579 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1582 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_FREE_COW
,
1586 struct xfs_refcount_recovery
{
1587 struct list_head rr_list
;
1588 struct xfs_refcount_irec rr_rrec
;
1591 /* Stuff an extent on the recovery list. */
1593 xfs_refcount_recover_extent(
1594 struct xfs_btree_cur
*cur
,
1595 union xfs_btree_rec
*rec
,
1598 struct list_head
*debris
= priv
;
1599 struct xfs_refcount_recovery
*rr
;
1601 if (be32_to_cpu(rec
->refc
.rc_refcount
) != 1)
1602 return -EFSCORRUPTED
;
1604 rr
= kmem_alloc(sizeof(struct xfs_refcount_recovery
), KM_SLEEP
);
1605 xfs_refcount_btrec_to_irec(rec
, &rr
->rr_rrec
);
1606 list_add_tail(&rr
->rr_list
, debris
);
1611 /* Find and remove leftover CoW reservations. */
1613 xfs_refcount_recover_cow_leftovers(
1614 struct xfs_mount
*mp
,
1615 xfs_agnumber_t agno
)
1617 struct xfs_trans
*tp
;
1618 struct xfs_btree_cur
*cur
;
1619 struct xfs_buf
*agbp
;
1620 struct xfs_refcount_recovery
*rr
, *n
;
1621 struct list_head debris
;
1622 union xfs_btree_irec low
;
1623 union xfs_btree_irec high
;
1624 struct xfs_defer_ops dfops
;
1626 xfs_agblock_t agbno
;
1629 if (mp
->m_sb
.sb_agblocks
>= XFS_REFC_COW_START
)
1632 error
= xfs_alloc_read_agf(mp
, NULL
, agno
, 0, &agbp
);
1635 cur
= xfs_refcountbt_init_cursor(mp
, NULL
, agbp
, agno
, NULL
);
1637 /* Find all the leftover CoW staging extents. */
1638 INIT_LIST_HEAD(&debris
);
1639 memset(&low
, 0, sizeof(low
));
1640 memset(&high
, 0, sizeof(high
));
1641 low
.rc
.rc_startblock
= XFS_REFC_COW_START
;
1642 high
.rc
.rc_startblock
= -1U;
1643 error
= xfs_btree_query_range(cur
, &low
, &high
,
1644 xfs_refcount_recover_extent
, &debris
);
1647 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1648 xfs_buf_relse(agbp
);
1650 /* Now iterate the list to free the leftovers */
1651 list_for_each_entry(rr
, &debris
, rr_list
) {
1652 /* Set up transaction. */
1653 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0, 0, &tp
);
1657 trace_xfs_refcount_recover_extent(mp
, agno
, &rr
->rr_rrec
);
1659 /* Free the orphan record */
1660 xfs_defer_init(&dfops
, &fsb
);
1661 agbno
= rr
->rr_rrec
.rc_startblock
- XFS_REFC_COW_START
;
1662 fsb
= XFS_AGB_TO_FSB(mp
, agno
, agbno
);
1663 error
= xfs_refcount_free_cow_extent(mp
, &dfops
, fsb
,
1664 rr
->rr_rrec
.rc_blockcount
);
1668 /* Free the block. */
1669 xfs_bmap_add_free(mp
, &dfops
, fsb
,
1670 rr
->rr_rrec
.rc_blockcount
, NULL
);
1672 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
1676 error
= xfs_trans_commit(tp
);
1682 /* Free the leftover list */
1683 list_for_each_entry_safe(rr
, n
, &debris
, rr_list
) {
1684 list_del(&rr
->rr_list
);
1690 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1691 xfs_buf_relse(agbp
);
1695 xfs_defer_cancel(&dfops
);
1696 xfs_trans_cancel(tp
);