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.
20 #include "libxfs_priv.h"
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_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
);
91 * Look up the first record equal to [bno, len] in the btree
95 xfs_refcount_lookup_eq(
96 struct xfs_btree_cur
*cur
,
100 trace_xfs_refcount_lookup(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
,
102 cur
->bc_rec
.rc
.rc_startblock
= bno
;
103 cur
->bc_rec
.rc
.rc_blockcount
= 0;
104 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
107 /* Convert on-disk record to in-core format. */
109 xfs_refcount_btrec_to_irec(
110 union xfs_btree_rec
*rec
,
111 struct xfs_refcount_irec
*irec
)
113 irec
->rc_startblock
= be32_to_cpu(rec
->refc
.rc_startblock
);
114 irec
->rc_blockcount
= be32_to_cpu(rec
->refc
.rc_blockcount
);
115 irec
->rc_refcount
= be32_to_cpu(rec
->refc
.rc_refcount
);
119 * Get the data from the pointed-to record.
122 xfs_refcount_get_rec(
123 struct xfs_btree_cur
*cur
,
124 struct xfs_refcount_irec
*irec
,
127 union xfs_btree_rec
*rec
;
130 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
131 if (!error
&& *stat
== 1) {
132 xfs_refcount_btrec_to_irec(rec
, irec
);
133 trace_xfs_refcount_get(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
140 * Update the record referred to by cur to the value given
141 * by [bno, len, refcount].
142 * This either works (return 0) or gets an EFSCORRUPTED error.
146 struct xfs_btree_cur
*cur
,
147 struct xfs_refcount_irec
*irec
)
149 union xfs_btree_rec rec
;
152 trace_xfs_refcount_update(cur
->bc_mp
, cur
->bc_private
.a
.agno
, irec
);
153 rec
.refc
.rc_startblock
= cpu_to_be32(irec
->rc_startblock
);
154 rec
.refc
.rc_blockcount
= cpu_to_be32(irec
->rc_blockcount
);
155 rec
.refc
.rc_refcount
= cpu_to_be32(irec
->rc_refcount
);
156 error
= xfs_btree_update(cur
, &rec
);
158 trace_xfs_refcount_update_error(cur
->bc_mp
,
159 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
164 * Insert the record referred to by cur to the value given
165 * by [bno, len, refcount].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
170 struct xfs_btree_cur
*cur
,
171 struct xfs_refcount_irec
*irec
,
176 trace_xfs_refcount_insert(cur
->bc_mp
, cur
->bc_private
.a
.agno
, irec
);
177 cur
->bc_rec
.rc
.rc_startblock
= irec
->rc_startblock
;
178 cur
->bc_rec
.rc
.rc_blockcount
= irec
->rc_blockcount
;
179 cur
->bc_rec
.rc
.rc_refcount
= irec
->rc_refcount
;
180 error
= xfs_btree_insert(cur
, i
);
181 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
184 trace_xfs_refcount_insert_error(cur
->bc_mp
,
185 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
190 * Remove the record referred to by cur, then set the pointer to the spot
191 * where the record could be re-inserted, in case we want to increment or
192 * decrement the cursor.
193 * This either works (return 0) or gets an EFSCORRUPTED error.
197 struct xfs_btree_cur
*cur
,
200 struct xfs_refcount_irec irec
;
204 error
= xfs_refcount_get_rec(cur
, &irec
, &found_rec
);
207 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
208 trace_xfs_refcount_delete(cur
->bc_mp
, cur
->bc_private
.a
.agno
, &irec
);
209 error
= xfs_btree_delete(cur
, i
);
210 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, *i
== 1, out_error
);
213 error
= xfs_refcount_lookup_ge(cur
, irec
.rc_startblock
, &found_rec
);
216 trace_xfs_refcount_delete_error(cur
->bc_mp
,
217 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
222 * Adjusting the Reference Count
224 * As stated elsewhere, the reference count btree (refcbt) stores
225 * >1 reference counts for extents of physical blocks. In this
226 * operation, we're either raising or lowering the reference count of
227 * some subrange stored in the tree:
229 * <------ adjustment range ------>
230 * ----+ +---+-----+ +--+--------+---------
231 * 2 | | 3 | 4 | |17| 55 | 10
232 * ----+ +---+-----+ +--+--------+---------
233 * X axis is physical blocks number;
234 * reference counts are the numbers inside the rectangles
236 * The first thing we need to do is to ensure that there are no
237 * refcount extents crossing either boundary of the range to be
238 * adjusted. For any extent that does cross a boundary, split it into
239 * two extents so that we can increment the refcount of one of the
242 * <------ adjustment range ------>
243 * ----+ +---+-----+ +--+--------+----+----
244 * 2 | | 3 | 2 | |17| 55 | 10 | 10
245 * ----+ +---+-----+ +--+--------+----+----
247 * For this next step, let's assume that all the physical blocks in
248 * the adjustment range are mapped to a file and are therefore in use
249 * at least once. Therefore, we can infer that any gap in the
250 * refcount tree within the adjustment range represents a physical
251 * extent with refcount == 1:
253 * <------ adjustment range ------>
254 * ----+---+---+-----+-+--+--------+----+----
255 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
256 * ----+---+---+-----+-+--+--------+----+----
259 * For each extent that falls within the interval range, figure out
260 * which extent is to the left or the right of that extent. Now we
261 * have a left, current, and right extent. If the new reference count
262 * of the center extent enables us to merge left, center, and right
263 * into one record covering all three, do so. If the center extent is
264 * at the left end of the range, abuts the left extent, and its new
265 * reference count matches the left extent's record, then merge them.
266 * If the center extent is at the right end of the range, abuts the
267 * right extent, and the reference counts match, merge those. In the
268 * example, we can left merge (assuming an increment operation):
270 * <------ adjustment range ------>
271 * --------+---+-----+-+--+--------+----+----
272 * 2 | 3 | 2 |1|17| 55 | 10 | 10
273 * --------+---+-----+-+--+--------+----+----
276 * For all other extents within the range, adjust the reference count
277 * or delete it if the refcount falls below 2. If we were
278 * incrementing, the end result looks like this:
280 * <------ adjustment range ------>
281 * --------+---+-----+-+--+--------+----+----
282 * 2 | 4 | 3 |2|18| 56 | 11 | 10
283 * --------+---+-----+-+--+--------+----+----
285 * The result of a decrement operation looks as such:
287 * <------ adjustment range ------>
288 * ----+ +---+ +--+--------+----+----
289 * 2 | | 2 | |16| 54 | 9 | 10
290 * ----+ +---+ +--+--------+----+----
293 * The blocks marked "D" are freed; the blocks marked "1" are only
294 * referenced once and therefore the record is removed from the
298 /* Next block after this extent. */
299 static inline xfs_agblock_t
301 struct xfs_refcount_irec
*rc
)
303 return rc
->rc_startblock
+ rc
->rc_blockcount
;
307 * Split a refcount extent that crosses agbno.
310 xfs_refcount_split_extent(
311 struct xfs_btree_cur
*cur
,
315 struct xfs_refcount_irec rcext
, tmp
;
319 *shape_changed
= false;
320 error
= xfs_refcount_lookup_le(cur
, agbno
, &found_rec
);
326 error
= xfs_refcount_get_rec(cur
, &rcext
, &found_rec
);
329 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
330 if (rcext
.rc_startblock
== agbno
|| xfs_refc_next(&rcext
) <= agbno
)
333 *shape_changed
= true;
334 trace_xfs_refcount_split_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
337 /* Establish the right extent. */
339 tmp
.rc_startblock
= agbno
;
340 tmp
.rc_blockcount
-= (agbno
- rcext
.rc_startblock
);
341 error
= xfs_refcount_update(cur
, &tmp
);
345 /* Insert the left extent. */
347 tmp
.rc_blockcount
= agbno
- rcext
.rc_startblock
;
348 error
= xfs_refcount_insert(cur
, &tmp
, &found_rec
);
351 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
355 trace_xfs_refcount_split_extent_error(cur
->bc_mp
,
356 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
361 * Merge the left, center, and right extents.
364 xfs_refcount_merge_center_extents(
365 struct xfs_btree_cur
*cur
,
366 struct xfs_refcount_irec
*left
,
367 struct xfs_refcount_irec
*center
,
368 struct xfs_refcount_irec
*right
,
369 unsigned long long extlen
,
375 trace_xfs_refcount_merge_center_extents(cur
->bc_mp
,
376 cur
->bc_private
.a
.agno
, left
, center
, right
);
379 * Make sure the center and right extents are not in the btree.
380 * If the center extent was synthesized, the first delete call
381 * removes the right extent and we skip the second deletion.
382 * If center and right were in the btree, then the first delete
383 * call removes the center and the second one removes the right
386 error
= xfs_refcount_lookup_ge(cur
, center
->rc_startblock
,
390 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
392 error
= xfs_refcount_delete(cur
, &found_rec
);
395 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
397 if (center
->rc_refcount
> 1) {
398 error
= xfs_refcount_delete(cur
, &found_rec
);
401 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
405 /* Enlarge the left extent. */
406 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
410 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
412 left
->rc_blockcount
= extlen
;
413 error
= xfs_refcount_update(cur
, left
);
421 trace_xfs_refcount_merge_center_extents_error(cur
->bc_mp
,
422 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
427 * Merge with the left extent.
430 xfs_refcount_merge_left_extent(
431 struct xfs_btree_cur
*cur
,
432 struct xfs_refcount_irec
*left
,
433 struct xfs_refcount_irec
*cleft
,
434 xfs_agblock_t
*agbno
,
440 trace_xfs_refcount_merge_left_extent(cur
->bc_mp
,
441 cur
->bc_private
.a
.agno
, left
, cleft
);
443 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
444 if (cleft
->rc_refcount
> 1) {
445 error
= xfs_refcount_lookup_le(cur
, cleft
->rc_startblock
,
449 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
452 error
= xfs_refcount_delete(cur
, &found_rec
);
455 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
459 /* Enlarge the left extent. */
460 error
= xfs_refcount_lookup_le(cur
, left
->rc_startblock
,
464 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
466 left
->rc_blockcount
+= cleft
->rc_blockcount
;
467 error
= xfs_refcount_update(cur
, left
);
471 *agbno
+= cleft
->rc_blockcount
;
472 *aglen
-= cleft
->rc_blockcount
;
476 trace_xfs_refcount_merge_left_extent_error(cur
->bc_mp
,
477 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
482 * Merge with the right extent.
485 xfs_refcount_merge_right_extent(
486 struct xfs_btree_cur
*cur
,
487 struct xfs_refcount_irec
*right
,
488 struct xfs_refcount_irec
*cright
,
494 trace_xfs_refcount_merge_right_extent(cur
->bc_mp
,
495 cur
->bc_private
.a
.agno
, cright
, right
);
498 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
501 if (cright
->rc_refcount
> 1) {
502 error
= xfs_refcount_lookup_le(cur
, cright
->rc_startblock
,
506 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
509 error
= xfs_refcount_delete(cur
, &found_rec
);
512 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
516 /* Enlarge the right extent. */
517 error
= xfs_refcount_lookup_le(cur
, right
->rc_startblock
,
521 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
523 right
->rc_startblock
-= cright
->rc_blockcount
;
524 right
->rc_blockcount
+= cright
->rc_blockcount
;
525 error
= xfs_refcount_update(cur
, right
);
529 *aglen
-= cright
->rc_blockcount
;
533 trace_xfs_refcount_merge_right_extent_error(cur
->bc_mp
,
534 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
538 #define XFS_FIND_RCEXT_SHARED 1
539 #define XFS_FIND_RCEXT_COW 2
541 * Find the left extent and the one after it (cleft). This function assumes
542 * that we've already split any extent crossing agbno.
545 xfs_refcount_find_left_extents(
546 struct xfs_btree_cur
*cur
,
547 struct xfs_refcount_irec
*left
,
548 struct xfs_refcount_irec
*cleft
,
553 struct xfs_refcount_irec tmp
;
557 left
->rc_startblock
= cleft
->rc_startblock
= NULLAGBLOCK
;
558 error
= xfs_refcount_lookup_le(cur
, agbno
- 1, &found_rec
);
564 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
567 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
569 if (xfs_refc_next(&tmp
) != agbno
)
571 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
573 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
575 /* We have a left extent; retrieve (or invent) the next right one */
578 error
= xfs_btree_increment(cur
, 0, &found_rec
);
582 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
585 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
588 /* if tmp starts at the end of our range, just use that */
589 if (tmp
.rc_startblock
== agbno
)
593 * There's a gap in the refcntbt at the start of the
594 * range we're interested in (refcount == 1) so
595 * synthesize the implied extent and pass it back.
596 * We assume here that the agbno/aglen range was
597 * passed in from a data fork extent mapping and
598 * therefore is allocated to exactly one owner.
600 cleft
->rc_startblock
= agbno
;
601 cleft
->rc_blockcount
= min(aglen
,
602 tmp
.rc_startblock
- agbno
);
603 cleft
->rc_refcount
= 1;
607 * No extents, so pretend that there's one covering the whole
610 cleft
->rc_startblock
= agbno
;
611 cleft
->rc_blockcount
= aglen
;
612 cleft
->rc_refcount
= 1;
614 trace_xfs_refcount_find_left_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
619 trace_xfs_refcount_find_left_extent_error(cur
->bc_mp
,
620 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
625 * Find the right extent and the one before it (cright). This function
626 * assumes that we've already split any extents crossing agbno + aglen.
629 xfs_refcount_find_right_extents(
630 struct xfs_btree_cur
*cur
,
631 struct xfs_refcount_irec
*right
,
632 struct xfs_refcount_irec
*cright
,
637 struct xfs_refcount_irec tmp
;
641 right
->rc_startblock
= cright
->rc_startblock
= NULLAGBLOCK
;
642 error
= xfs_refcount_lookup_ge(cur
, agbno
+ aglen
, &found_rec
);
648 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
651 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1, out_error
);
653 if (tmp
.rc_startblock
!= agbno
+ aglen
)
655 if ((flags
& XFS_FIND_RCEXT_SHARED
) && tmp
.rc_refcount
< 2)
657 if ((flags
& XFS_FIND_RCEXT_COW
) && tmp
.rc_refcount
> 1)
659 /* We have a right extent; retrieve (or invent) the next left one */
662 error
= xfs_btree_decrement(cur
, 0, &found_rec
);
666 error
= xfs_refcount_get_rec(cur
, &tmp
, &found_rec
);
669 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, found_rec
== 1,
672 /* if tmp ends at the end of our range, just use that */
673 if (xfs_refc_next(&tmp
) == agbno
+ aglen
)
677 * There's a gap in the refcntbt at the end of the
678 * range we're interested in (refcount == 1) so
679 * create the implied extent and pass it back.
680 * We assume here that the agbno/aglen range was
681 * passed in from a data fork extent mapping and
682 * therefore is allocated to exactly one owner.
684 cright
->rc_startblock
= max(agbno
, xfs_refc_next(&tmp
));
685 cright
->rc_blockcount
= right
->rc_startblock
-
686 cright
->rc_startblock
;
687 cright
->rc_refcount
= 1;
691 * No extents, so pretend that there's one covering the whole
694 cright
->rc_startblock
= agbno
;
695 cright
->rc_blockcount
= aglen
;
696 cright
->rc_refcount
= 1;
698 trace_xfs_refcount_find_right_extent(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
699 cright
, right
, agbno
+ aglen
);
703 trace_xfs_refcount_find_right_extent_error(cur
->bc_mp
,
704 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
708 /* Is this extent valid? */
711 struct xfs_refcount_irec
*rc
)
713 return rc
->rc_startblock
!= NULLAGBLOCK
;
717 * Try to merge with any extents on the boundaries of the adjustment range.
720 xfs_refcount_merge_extents(
721 struct xfs_btree_cur
*cur
,
722 xfs_agblock_t
*agbno
,
724 enum xfs_refc_adjust_op adjust
,
728 struct xfs_refcount_irec left
= {0}, cleft
= {0};
729 struct xfs_refcount_irec cright
= {0}, right
= {0};
731 unsigned long long ulen
;
734 *shape_changed
= false;
736 * Find the extent just below agbno [left], just above agbno [cleft],
737 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
740 error
= xfs_refcount_find_left_extents(cur
, &left
, &cleft
, *agbno
,
744 error
= xfs_refcount_find_right_extents(cur
, &right
, &cright
, *agbno
,
749 /* No left or right extent to merge; exit. */
750 if (!xfs_refc_valid(&left
) && !xfs_refc_valid(&right
))
753 cequal
= (cleft
.rc_startblock
== cright
.rc_startblock
) &&
754 (cleft
.rc_blockcount
== cright
.rc_blockcount
);
756 /* Try to merge left, cleft, and right. cleft must == cright. */
757 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
+
759 if (xfs_refc_valid(&left
) && xfs_refc_valid(&right
) &&
760 xfs_refc_valid(&cleft
) && xfs_refc_valid(&cright
) && cequal
&&
761 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
762 right
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
763 ulen
< MAXREFCEXTLEN
) {
764 *shape_changed
= true;
765 return xfs_refcount_merge_center_extents(cur
, &left
, &cleft
,
766 &right
, ulen
, aglen
);
769 /* Try to merge left and cleft. */
770 ulen
= (unsigned long long)left
.rc_blockcount
+ cleft
.rc_blockcount
;
771 if (xfs_refc_valid(&left
) && xfs_refc_valid(&cleft
) &&
772 left
.rc_refcount
== cleft
.rc_refcount
+ adjust
&&
773 ulen
< MAXREFCEXTLEN
) {
774 *shape_changed
= true;
775 error
= xfs_refcount_merge_left_extent(cur
, &left
, &cleft
,
781 * If we just merged left + cleft and cleft == cright,
782 * we no longer have a cright to merge with right. We're done.
788 /* Try to merge cright and right. */
789 ulen
= (unsigned long long)right
.rc_blockcount
+ cright
.rc_blockcount
;
790 if (xfs_refc_valid(&right
) && xfs_refc_valid(&cright
) &&
791 right
.rc_refcount
== cright
.rc_refcount
+ adjust
&&
792 ulen
< MAXREFCEXTLEN
) {
793 *shape_changed
= true;
794 return xfs_refcount_merge_right_extent(cur
, &right
, &cright
,
802 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
803 * true incorrectly is a shutdown FS; the penalty for guessing false
804 * incorrectly is more transaction rolls than might be necessary.
805 * Be conservative here.
808 xfs_refcount_still_have_space(
809 struct xfs_btree_cur
*cur
)
811 unsigned long overhead
;
813 overhead
= cur
->bc_private
.a
.priv
.refc
.shape_changes
*
814 xfs_allocfree_log_count(cur
->bc_mp
, 1);
815 overhead
*= cur
->bc_mp
->m_sb
.sb_blocksize
;
818 * Only allow 2 refcount extent updates per transaction if the
819 * refcount continue update "error" has been injected.
821 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
> 2 &&
822 XFS_TEST_ERROR(false, cur
->bc_mp
,
823 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE
))
826 if (cur
->bc_private
.a
.priv
.refc
.nr_ops
== 0)
828 else if (overhead
> cur
->bc_tp
->t_log_res
)
830 return cur
->bc_tp
->t_log_res
- overhead
>
831 cur
->bc_private
.a
.priv
.refc
.nr_ops
* XFS_REFCOUNT_ITEM_OVERHEAD
;
835 * Adjust the refcounts of middle extents. At this point we should have
836 * split extents that crossed the adjustment range; merged with adjacent
837 * extents; and updated agbno/aglen to reflect the merges. Therefore,
838 * all we have to do is update the extents inside [agbno, agbno + aglen].
841 xfs_refcount_adjust_extents(
842 struct xfs_btree_cur
*cur
,
843 xfs_agblock_t
*agbno
,
845 enum xfs_refc_adjust_op adj
,
846 struct xfs_defer_ops
*dfops
,
847 struct xfs_owner_info
*oinfo
)
849 struct xfs_refcount_irec ext
, tmp
;
851 int found_rec
, found_tmp
;
854 /* Merging did all the work already. */
858 error
= xfs_refcount_lookup_ge(cur
, *agbno
, &found_rec
);
862 while (*aglen
> 0 && xfs_refcount_still_have_space(cur
)) {
863 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
867 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
;
868 ext
.rc_blockcount
= 0;
873 * Deal with a hole in the refcount tree; if a file maps to
874 * these blocks and there's no refcountbt record, pretend that
875 * there is one with refcount == 1.
877 if (ext
.rc_startblock
!= *agbno
) {
878 tmp
.rc_startblock
= *agbno
;
879 tmp
.rc_blockcount
= min(*aglen
,
880 ext
.rc_startblock
- *agbno
);
881 tmp
.rc_refcount
= 1 + adj
;
882 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
883 cur
->bc_private
.a
.agno
, &tmp
);
886 * Either cover the hole (increment) or
887 * delete the range (decrement).
889 if (tmp
.rc_refcount
) {
890 error
= xfs_refcount_insert(cur
, &tmp
,
894 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
895 found_tmp
== 1, out_error
);
896 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
898 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
899 cur
->bc_private
.a
.agno
,
901 xfs_bmap_add_free(cur
->bc_mp
, dfops
, fsbno
,
902 tmp
.rc_blockcount
, oinfo
);
905 (*agbno
) += tmp
.rc_blockcount
;
906 (*aglen
) -= tmp
.rc_blockcount
;
908 error
= xfs_refcount_lookup_ge(cur
, *agbno
,
914 /* Stop if there's nothing left to modify */
915 if (*aglen
== 0 || !xfs_refcount_still_have_space(cur
))
919 * Adjust the reference count and either update the tree
920 * (incr) or free the blocks (decr).
922 if (ext
.rc_refcount
== MAXREFCOUNT
)
924 ext
.rc_refcount
+= adj
;
925 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
926 cur
->bc_private
.a
.agno
, &ext
);
927 if (ext
.rc_refcount
> 1) {
928 error
= xfs_refcount_update(cur
, &ext
);
931 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
932 } else if (ext
.rc_refcount
== 1) {
933 error
= xfs_refcount_delete(cur
, &found_rec
);
936 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
937 found_rec
== 1, out_error
);
938 cur
->bc_private
.a
.priv
.refc
.nr_ops
++;
941 fsbno
= XFS_AGB_TO_FSB(cur
->bc_mp
,
942 cur
->bc_private
.a
.agno
,
944 xfs_bmap_add_free(cur
->bc_mp
, dfops
, fsbno
,
945 ext
.rc_blockcount
, oinfo
);
949 error
= xfs_btree_increment(cur
, 0, &found_rec
);
954 (*agbno
) += ext
.rc_blockcount
;
955 (*aglen
) -= ext
.rc_blockcount
;
960 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
961 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
965 /* Adjust the reference count of a range of AG blocks. */
968 struct xfs_btree_cur
*cur
,
971 xfs_agblock_t
*new_agbno
,
972 xfs_extlen_t
*new_aglen
,
973 enum xfs_refc_adjust_op adj
,
974 struct xfs_defer_ops
*dfops
,
975 struct xfs_owner_info
*oinfo
)
978 int shape_changes
= 0;
983 if (adj
== XFS_REFCOUNT_ADJUST_INCREASE
)
984 trace_xfs_refcount_increase(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
987 trace_xfs_refcount_decrease(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
991 * Ensure that no rcextents cross the boundary of the adjustment range.
993 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
999 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1006 * Try to merge with the left or right extents of the range.
1008 error
= xfs_refcount_merge_extents(cur
, new_agbno
, new_aglen
, adj
,
1009 XFS_FIND_RCEXT_SHARED
, &shape_changed
);
1015 cur
->bc_private
.a
.priv
.refc
.shape_changes
++;
1017 /* Now that we've taken care of the ends, adjust the middle extents */
1018 error
= xfs_refcount_adjust_extents(cur
, new_agbno
, new_aglen
,
1026 trace_xfs_refcount_adjust_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1031 /* Clean up after calling xfs_refcount_finish_one. */
1033 xfs_refcount_finish_one_cleanup(
1034 struct xfs_trans
*tp
,
1035 struct xfs_btree_cur
*rcur
,
1038 struct xfs_buf
*agbp
;
1042 agbp
= rcur
->bc_private
.a
.agbp
;
1043 xfs_btree_del_cursor(rcur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
1045 xfs_trans_brelse(tp
, agbp
);
1049 * Process one of the deferred refcount operations. We pass back the
1050 * btree cursor to maintain our lock on the btree between calls.
1051 * This saves time and eliminates a buffer deadlock between the
1052 * superblock and the AGF because we'll always grab them in the same
1056 xfs_refcount_finish_one(
1057 struct xfs_trans
*tp
,
1058 struct xfs_defer_ops
*dfops
,
1059 enum xfs_refcount_intent_type type
,
1060 xfs_fsblock_t startblock
,
1061 xfs_extlen_t blockcount
,
1062 xfs_fsblock_t
*new_fsb
,
1063 xfs_extlen_t
*new_len
,
1064 struct xfs_btree_cur
**pcur
)
1066 struct xfs_mount
*mp
= tp
->t_mountp
;
1067 struct xfs_btree_cur
*rcur
;
1068 struct xfs_buf
*agbp
= NULL
;
1070 xfs_agnumber_t agno
;
1072 xfs_agblock_t new_agbno
;
1073 unsigned long nr_ops
= 0;
1074 int shape_changes
= 0;
1076 agno
= XFS_FSB_TO_AGNO(mp
, startblock
);
1077 ASSERT(agno
!= NULLAGNUMBER
);
1078 bno
= XFS_FSB_TO_AGBNO(mp
, startblock
);
1080 trace_xfs_refcount_deferred(mp
, XFS_FSB_TO_AGNO(mp
, startblock
),
1081 type
, XFS_FSB_TO_AGBNO(mp
, startblock
),
1084 if (XFS_TEST_ERROR(false, mp
,
1085 XFS_ERRTAG_REFCOUNT_FINISH_ONE
))
1089 * If we haven't gotten a cursor or the cursor AG doesn't match
1090 * the startblock, get one now.
1093 if (rcur
!= NULL
&& rcur
->bc_private
.a
.agno
!= agno
) {
1094 nr_ops
= rcur
->bc_private
.a
.priv
.refc
.nr_ops
;
1095 shape_changes
= rcur
->bc_private
.a
.priv
.refc
.shape_changes
;
1096 xfs_refcount_finish_one_cleanup(tp
, rcur
, 0);
1101 error
= xfs_alloc_read_agf(tp
->t_mountp
, tp
, agno
,
1102 XFS_ALLOC_FLAG_FREEING
, &agbp
);
1106 return -EFSCORRUPTED
;
1108 rcur
= xfs_refcountbt_init_cursor(mp
, tp
, agbp
, agno
, dfops
);
1113 rcur
->bc_private
.a
.priv
.refc
.nr_ops
= nr_ops
;
1114 rcur
->bc_private
.a
.priv
.refc
.shape_changes
= shape_changes
;
1119 case XFS_REFCOUNT_INCREASE
:
1120 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1121 new_len
, XFS_REFCOUNT_ADJUST_INCREASE
, dfops
, NULL
);
1122 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1124 case XFS_REFCOUNT_DECREASE
:
1125 error
= xfs_refcount_adjust(rcur
, bno
, blockcount
, &new_agbno
,
1126 new_len
, XFS_REFCOUNT_ADJUST_DECREASE
, dfops
, NULL
);
1127 *new_fsb
= XFS_AGB_TO_FSB(mp
, agno
, new_agbno
);
1129 case XFS_REFCOUNT_ALLOC_COW
:
1130 *new_fsb
= startblock
+ blockcount
;
1132 error
= __xfs_refcount_cow_alloc(rcur
, bno
, blockcount
, dfops
);
1134 case XFS_REFCOUNT_FREE_COW
:
1135 *new_fsb
= startblock
+ blockcount
;
1137 error
= __xfs_refcount_cow_free(rcur
, bno
, blockcount
, dfops
);
1141 error
= -EFSCORRUPTED
;
1143 if (!error
&& *new_len
> 0)
1144 trace_xfs_refcount_finish_one_leftover(mp
, agno
, type
,
1145 bno
, blockcount
, new_agbno
, *new_len
);
1149 xfs_trans_brelse(tp
, agbp
);
1155 * Record a refcount intent for later processing.
1159 struct xfs_mount
*mp
,
1160 struct xfs_defer_ops
*dfops
,
1161 enum xfs_refcount_intent_type type
,
1162 xfs_fsblock_t startblock
,
1163 xfs_extlen_t blockcount
)
1165 struct xfs_refcount_intent
*ri
;
1167 trace_xfs_refcount_defer(mp
, XFS_FSB_TO_AGNO(mp
, startblock
),
1168 type
, XFS_FSB_TO_AGBNO(mp
, startblock
),
1171 ri
= kmem_alloc(sizeof(struct xfs_refcount_intent
),
1172 KM_SLEEP
| KM_NOFS
);
1173 INIT_LIST_HEAD(&ri
->ri_list
);
1175 ri
->ri_startblock
= startblock
;
1176 ri
->ri_blockcount
= blockcount
;
1178 xfs_defer_add(dfops
, XFS_DEFER_OPS_TYPE_REFCOUNT
, &ri
->ri_list
);
1183 * Increase the reference count of the blocks backing a file's extent.
1186 xfs_refcount_increase_extent(
1187 struct xfs_mount
*mp
,
1188 struct xfs_defer_ops
*dfops
,
1189 struct xfs_bmbt_irec
*PREV
)
1191 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1194 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_INCREASE
,
1195 PREV
->br_startblock
, PREV
->br_blockcount
);
1199 * Decrease the reference count of the blocks backing a file's extent.
1202 xfs_refcount_decrease_extent(
1203 struct xfs_mount
*mp
,
1204 struct xfs_defer_ops
*dfops
,
1205 struct xfs_bmbt_irec
*PREV
)
1207 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1210 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_DECREASE
,
1211 PREV
->br_startblock
, PREV
->br_blockcount
);
1215 * Given an AG extent, find the lowest-numbered run of shared blocks
1216 * within that range and return the range in fbno/flen. If
1217 * find_end_of_shared is set, return the longest contiguous extent of
1218 * shared blocks; if not, just return the first extent we find. If no
1219 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1220 * and 0, respectively.
1223 xfs_refcount_find_shared(
1224 struct xfs_btree_cur
*cur
,
1225 xfs_agblock_t agbno
,
1227 xfs_agblock_t
*fbno
,
1229 bool find_end_of_shared
)
1231 struct xfs_refcount_irec tmp
;
1236 trace_xfs_refcount_find_shared(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1239 /* By default, skip the whole range */
1240 *fbno
= NULLAGBLOCK
;
1243 /* Try to find a refcount extent that crosses the start */
1244 error
= xfs_refcount_lookup_le(cur
, agbno
, &have
);
1248 /* No left extent, look at the next one */
1249 error
= xfs_btree_increment(cur
, 0, &have
);
1255 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1258 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1260 /* If the extent ends before the start, look at the next one */
1261 if (tmp
.rc_startblock
+ tmp
.rc_blockcount
<= agbno
) {
1262 error
= xfs_btree_increment(cur
, 0, &have
);
1267 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1270 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1273 /* If the extent starts after the range we want, bail out */
1274 if (tmp
.rc_startblock
>= agbno
+ aglen
)
1277 /* We found the start of a shared extent! */
1278 if (tmp
.rc_startblock
< agbno
) {
1279 tmp
.rc_blockcount
-= (agbno
- tmp
.rc_startblock
);
1280 tmp
.rc_startblock
= agbno
;
1283 *fbno
= tmp
.rc_startblock
;
1284 *flen
= min(tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1285 if (!find_end_of_shared
)
1288 /* Otherwise, find the end of this shared extent */
1289 while (*fbno
+ *flen
< agbno
+ aglen
) {
1290 error
= xfs_btree_increment(cur
, 0, &have
);
1295 error
= xfs_refcount_get_rec(cur
, &tmp
, &i
);
1298 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
, i
== 1, out_error
);
1299 if (tmp
.rc_startblock
>= agbno
+ aglen
||
1300 tmp
.rc_startblock
!= *fbno
+ *flen
)
1302 *flen
= min(*flen
+ tmp
.rc_blockcount
, agbno
+ aglen
- *fbno
);
1306 trace_xfs_refcount_find_shared_result(cur
->bc_mp
,
1307 cur
->bc_private
.a
.agno
, *fbno
, *flen
);
1311 trace_xfs_refcount_find_shared_error(cur
->bc_mp
,
1312 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1317 * Recovering CoW Blocks After a Crash
1319 * Due to the way that the copy on write mechanism works, there's a window of
1320 * opportunity in which we can lose track of allocated blocks during a crash.
1321 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1322 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1323 * no longer in the free space btree but are not otherwise recorded anywhere
1324 * until the write completes and the blocks are mapped into the file. A crash
1325 * in between allocation and remapping results in the replacement blocks being
1326 * lost. This situation is exacerbated by the CoW extent size hint because
1327 * allocations can hang around for long time.
1329 * However, there is a place where we can record these allocations before they
1330 * become mappings -- the reference count btree. The btree does not record
1331 * extents with refcount == 1, so we can record allocations with a refcount of
1332 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1333 * no conflict with shared block records. These mappings should be created
1334 * when we allocate blocks to the CoW fork and deleted when they're removed
1335 * from the CoW fork.
1337 * Minor nit: records for in-progress CoW allocations and records for shared
1338 * extents must never be merged, to preserve the property that (except for CoW
1339 * allocations) there are no refcount btree entries with refcount == 1. The
1340 * only time this could potentially happen is when unsharing a block that's
1341 * adjacent to CoW allocations, so we must be careful to avoid this.
1343 * At mount time we recover lost CoW allocations by searching the refcount
1344 * btree for these refcount == 1 mappings. These represent CoW allocations
1345 * that were in progress at the time the filesystem went down, so we can free
1346 * them to get the space back.
1348 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1349 * several reasons -- first, EFIs pin the tail of the log and would have to be
1350 * periodically relogged to avoid filling up the log. Second, CoW completions
1351 * will have to file an EFD and create new EFIs for whatever remains in the
1352 * CoW fork; this partially takes care of (1) but extent-size reservations
1353 * will have to periodically relog even if there's no writeout in progress.
1354 * This can happen if the CoW extent size hint is set, which you really want.
1355 * Third, EFIs cannot currently be automatically relogged into newer
1356 * transactions to advance the log tail. Fourth, stuffing the log full of
1357 * EFIs places an upper bound on the number of CoW allocations that can be
1358 * held filesystem-wide at any given time. Recording them in the refcount
1359 * btree doesn't require us to maintain any state in memory and doesn't pin
1363 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1364 * in that they're not referenced anywhere else in the filesystem, so we
1365 * stash them in the refcount btree with a refcount of 1 until either file
1366 * remapping (or CoW cancellation) happens.
1369 xfs_refcount_adjust_cow_extents(
1370 struct xfs_btree_cur
*cur
,
1371 xfs_agblock_t agbno
,
1373 enum xfs_refc_adjust_op adj
)
1375 struct xfs_refcount_irec ext
, tmp
;
1377 int found_rec
, found_tmp
;
1382 /* Find any overlapping refcount records */
1383 error
= xfs_refcount_lookup_ge(cur
, agbno
, &found_rec
);
1386 error
= xfs_refcount_get_rec(cur
, &ext
, &found_rec
);
1390 ext
.rc_startblock
= cur
->bc_mp
->m_sb
.sb_agblocks
+
1392 ext
.rc_blockcount
= 0;
1393 ext
.rc_refcount
= 0;
1397 case XFS_REFCOUNT_ADJUST_COW_ALLOC
:
1398 /* Adding a CoW reservation, there should be nothing here. */
1399 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1400 ext
.rc_startblock
>= agbno
+ aglen
, out_error
);
1402 tmp
.rc_startblock
= agbno
;
1403 tmp
.rc_blockcount
= aglen
;
1404 tmp
.rc_refcount
= 1;
1405 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1406 cur
->bc_private
.a
.agno
, &tmp
);
1408 error
= xfs_refcount_insert(cur
, &tmp
,
1412 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1413 found_tmp
== 1, out_error
);
1415 case XFS_REFCOUNT_ADJUST_COW_FREE
:
1416 /* Removing a CoW reservation, there should be one extent. */
1417 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1418 ext
.rc_startblock
== agbno
, out_error
);
1419 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1420 ext
.rc_blockcount
== aglen
, out_error
);
1421 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1422 ext
.rc_refcount
== 1, out_error
);
1424 ext
.rc_refcount
= 0;
1425 trace_xfs_refcount_modify_extent(cur
->bc_mp
,
1426 cur
->bc_private
.a
.agno
, &ext
);
1427 error
= xfs_refcount_delete(cur
, &found_rec
);
1430 XFS_WANT_CORRUPTED_GOTO(cur
->bc_mp
,
1431 found_rec
== 1, out_error
);
1439 trace_xfs_refcount_modify_extent_error(cur
->bc_mp
,
1440 cur
->bc_private
.a
.agno
, error
, _RET_IP_
);
1445 * Add or remove refcount btree entries for CoW reservations.
1448 xfs_refcount_adjust_cow(
1449 struct xfs_btree_cur
*cur
,
1450 xfs_agblock_t agbno
,
1452 enum xfs_refc_adjust_op adj
)
1457 agbno
+= XFS_REFC_COW_START
;
1460 * Ensure that no rcextents cross the boundary of the adjustment range.
1462 error
= xfs_refcount_split_extent(cur
, agbno
, &shape_changed
);
1466 error
= xfs_refcount_split_extent(cur
, agbno
+ aglen
, &shape_changed
);
1471 * Try to merge with the left or right extents of the range.
1473 error
= xfs_refcount_merge_extents(cur
, &agbno
, &aglen
, adj
,
1474 XFS_FIND_RCEXT_COW
, &shape_changed
);
1478 /* Now that we've taken care of the ends, adjust the middle extents */
1479 error
= xfs_refcount_adjust_cow_extents(cur
, agbno
, aglen
, adj
);
1486 trace_xfs_refcount_adjust_cow_error(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
1492 * Record a CoW allocation in the refcount btree.
1495 __xfs_refcount_cow_alloc(
1496 struct xfs_btree_cur
*rcur
,
1497 xfs_agblock_t agbno
,
1499 struct xfs_defer_ops
*dfops
)
1501 trace_xfs_refcount_cow_increase(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1504 /* Add refcount btree reservation */
1505 return xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1506 XFS_REFCOUNT_ADJUST_COW_ALLOC
);
1510 * Remove a CoW allocation from the refcount btree.
1513 __xfs_refcount_cow_free(
1514 struct xfs_btree_cur
*rcur
,
1515 xfs_agblock_t agbno
,
1517 struct xfs_defer_ops
*dfops
)
1519 trace_xfs_refcount_cow_decrease(rcur
->bc_mp
, rcur
->bc_private
.a
.agno
,
1522 /* Remove refcount btree reservation */
1523 return xfs_refcount_adjust_cow(rcur
, agbno
, aglen
,
1524 XFS_REFCOUNT_ADJUST_COW_FREE
);
1527 /* Record a CoW staging extent in the refcount btree. */
1529 xfs_refcount_alloc_cow_extent(
1530 struct xfs_mount
*mp
,
1531 struct xfs_defer_ops
*dfops
,
1537 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1540 error
= __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_ALLOC_COW
,
1545 /* Add rmap entry */
1546 return xfs_rmap_alloc_extent(mp
, dfops
, XFS_FSB_TO_AGNO(mp
, fsb
),
1547 XFS_FSB_TO_AGBNO(mp
, fsb
), len
, XFS_RMAP_OWN_COW
);
1550 /* Forget a CoW staging event in the refcount btree. */
1552 xfs_refcount_free_cow_extent(
1553 struct xfs_mount
*mp
,
1554 struct xfs_defer_ops
*dfops
,
1560 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1563 /* Remove rmap entry */
1564 error
= xfs_rmap_free_extent(mp
, dfops
, XFS_FSB_TO_AGNO(mp
, fsb
),
1565 XFS_FSB_TO_AGBNO(mp
, fsb
), len
, XFS_RMAP_OWN_COW
);
1569 return __xfs_refcount_add(mp
, dfops
, XFS_REFCOUNT_FREE_COW
,
1573 struct xfs_refcount_recovery
{
1574 struct list_head rr_list
;
1575 struct xfs_refcount_irec rr_rrec
;
1578 /* Stuff an extent on the recovery list. */
1580 xfs_refcount_recover_extent(
1581 struct xfs_btree_cur
*cur
,
1582 union xfs_btree_rec
*rec
,
1585 struct list_head
*debris
= priv
;
1586 struct xfs_refcount_recovery
*rr
;
1588 if (be32_to_cpu(rec
->refc
.rc_refcount
) != 1)
1589 return -EFSCORRUPTED
;
1591 rr
= kmem_alloc(sizeof(struct xfs_refcount_recovery
), KM_SLEEP
);
1592 xfs_refcount_btrec_to_irec(rec
, &rr
->rr_rrec
);
1593 list_add_tail(&rr
->rr_list
, debris
);
1598 /* Find and remove leftover CoW reservations. */
1600 xfs_refcount_recover_cow_leftovers(
1601 struct xfs_mount
*mp
,
1602 xfs_agnumber_t agno
)
1604 struct xfs_trans
*tp
;
1605 struct xfs_btree_cur
*cur
;
1606 struct xfs_buf
*agbp
;
1607 struct xfs_refcount_recovery
*rr
, *n
;
1608 struct list_head debris
;
1609 union xfs_btree_irec low
;
1610 union xfs_btree_irec high
;
1611 struct xfs_defer_ops dfops
;
1613 xfs_agblock_t agbno
;
1616 if (mp
->m_sb
.sb_agblocks
>= XFS_REFC_COW_START
)
1619 INIT_LIST_HEAD(&debris
);
1622 * In this first part, we use an empty transaction to gather up
1623 * all the leftover CoW extents so that we can subsequently
1624 * delete them. The empty transaction is used to avoid
1625 * a buffer lock deadlock if there happens to be a loop in the
1626 * refcountbt because we're allowed to re-grab a buffer that is
1627 * already attached to our transaction. When we're done
1628 * recording the CoW debris we cancel the (empty) transaction
1629 * and everything goes away cleanly.
1631 error
= xfs_trans_alloc_empty(mp
, &tp
);
1635 error
= xfs_alloc_read_agf(mp
, tp
, agno
, 0, &agbp
);
1642 cur
= xfs_refcountbt_init_cursor(mp
, tp
, agbp
, agno
, NULL
);
1644 /* Find all the leftover CoW staging extents. */
1645 memset(&low
, 0, sizeof(low
));
1646 memset(&high
, 0, sizeof(high
));
1647 low
.rc
.rc_startblock
= XFS_REFC_COW_START
;
1648 high
.rc
.rc_startblock
= -1U;
1649 error
= xfs_btree_query_range(cur
, &low
, &high
,
1650 xfs_refcount_recover_extent
, &debris
);
1653 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1654 xfs_trans_brelse(tp
, agbp
);
1655 xfs_trans_cancel(tp
);
1657 /* Now iterate the list to free the leftovers */
1658 list_for_each_entry_safe(rr
, n
, &debris
, rr_list
) {
1659 /* Set up transaction. */
1660 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0, 0, &tp
);
1664 trace_xfs_refcount_recover_extent(mp
, agno
, &rr
->rr_rrec
);
1666 /* Free the orphan record */
1667 xfs_defer_init(&dfops
, &fsb
);
1668 agbno
= rr
->rr_rrec
.rc_startblock
- XFS_REFC_COW_START
;
1669 fsb
= XFS_AGB_TO_FSB(mp
, agno
, agbno
);
1670 error
= xfs_refcount_free_cow_extent(mp
, &dfops
, fsb
,
1671 rr
->rr_rrec
.rc_blockcount
);
1675 /* Free the block. */
1676 xfs_bmap_add_free(mp
, &dfops
, fsb
,
1677 rr
->rr_rrec
.rc_blockcount
, NULL
);
1679 error
= xfs_defer_finish(&tp
, &dfops
);
1683 error
= xfs_trans_commit(tp
);
1687 list_del(&rr
->rr_list
);
1693 xfs_defer_cancel(&dfops
);
1695 xfs_trans_cancel(tp
);
1697 /* Free the leftover list */
1698 list_for_each_entry_safe(rr
, n
, &debris
, rr_list
) {
1699 list_del(&rr
->rr_list
);
1705 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1706 xfs_trans_brelse(tp
, agbp
);
1710 /* Is there a record covering a given extent? */
1712 xfs_refcount_has_record(
1713 struct xfs_btree_cur
*cur
,
1718 union xfs_btree_irec low
;
1719 union xfs_btree_irec high
;
1721 memset(&low
, 0, sizeof(low
));
1722 low
.rc
.rc_startblock
= bno
;
1723 memset(&high
, 0xFF, sizeof(high
));
1724 high
.rc
.rc_startblock
= bno
+ len
- 1;
1726 return xfs_btree_has_record(cur
, &low
, &high
, exists
);