1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_btree.h"
16 #include "xfs_refcount_btree.h"
17 #include "xfs_alloc.h"
18 #include "xfs_errortag.h"
19 #include "xfs_error.h"
20 #include "xfs_trace.h"
21 #include "xfs_trans.h"
23 #include "xfs_refcount.h"
26 #include "xfs_health.h"
28 struct kmem_cache *xfs_refcount_intent_cache;
30 /* Allowable refcount adjustment amounts. */
31 enum xfs_refc_adjust_op {
32 XFS_REFCOUNT_ADJUST_INCREASE = 1,
33 XFS_REFCOUNT_ADJUST_DECREASE = -1,
34 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
35 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
38 STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
39 xfs_agblock_t agbno, xfs_extlen_t aglen);
40 STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
41 xfs_agblock_t agbno, xfs_extlen_t aglen);
44 * Look up the first record less than or equal to [bno, len] in the btree
48 xfs_refcount_lookup_le(
49 struct xfs_btree_cur *cur,
50 enum xfs_refc_domain domain,
54 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
55 xfs_refcount_encode_startblock(bno, domain),
57 cur->bc_rec.rc.rc_startblock = bno;
58 cur->bc_rec.rc.rc_blockcount = 0;
59 cur->bc_rec.rc.rc_domain = domain;
60 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
64 * Look up the first record greater than or equal to [bno, len] in the btree
68 xfs_refcount_lookup_ge(
69 struct xfs_btree_cur *cur,
70 enum xfs_refc_domain domain,
74 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
75 xfs_refcount_encode_startblock(bno, domain),
77 cur->bc_rec.rc.rc_startblock = bno;
78 cur->bc_rec.rc.rc_blockcount = 0;
79 cur->bc_rec.rc.rc_domain = domain;
80 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
84 * Look up the first record equal to [bno, len] in the btree
88 xfs_refcount_lookup_eq(
89 struct xfs_btree_cur *cur,
90 enum xfs_refc_domain domain,
94 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
95 xfs_refcount_encode_startblock(bno, domain),
97 cur->bc_rec.rc.rc_startblock = bno;
98 cur->bc_rec.rc.rc_blockcount = 0;
99 cur->bc_rec.rc.rc_domain = domain;
100 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
103 /* Convert on-disk record to in-core format. */
105 xfs_refcount_btrec_to_irec(
106 const union xfs_btree_rec *rec,
107 struct xfs_refcount_irec *irec)
111 start = be32_to_cpu(rec->refc.rc_startblock);
112 if (start & XFS_REFC_COWFLAG) {
113 start &= ~XFS_REFC_COWFLAG;
114 irec->rc_domain = XFS_REFC_DOMAIN_COW;
116 irec->rc_domain = XFS_REFC_DOMAIN_SHARED;
119 irec->rc_startblock = start;
120 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
121 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
124 /* Simple checks for refcount records. */
126 xfs_refcount_check_irec(
127 struct xfs_perag *pag,
128 const struct xfs_refcount_irec *irec)
130 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
131 return __this_address;
133 if (!xfs_refcount_check_domain(irec))
134 return __this_address;
136 /* check for valid extent range, including overflow */
137 if (!xfs_verify_agbext(pag, irec->rc_startblock, irec->rc_blockcount))
138 return __this_address;
140 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
141 return __this_address;
147 xfs_refcount_complain_bad_rec(
148 struct xfs_btree_cur *cur,
150 const struct xfs_refcount_irec *irec)
152 struct xfs_mount *mp = cur->bc_mp;
155 "Refcount BTree record corruption in AG %d detected at %pS!",
156 cur->bc_ag.pag->pag_agno, fa);
158 "Start block 0x%x, block count 0x%x, references 0x%x",
159 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
160 xfs_btree_mark_sick(cur);
161 return -EFSCORRUPTED;
165 * Get the data from the pointed-to record.
168 xfs_refcount_get_rec(
169 struct xfs_btree_cur *cur,
170 struct xfs_refcount_irec *irec,
173 union xfs_btree_rec *rec;
177 error = xfs_btree_get_rec(cur, &rec, stat);
181 xfs_refcount_btrec_to_irec(rec, irec);
182 fa = xfs_refcount_check_irec(cur->bc_ag.pag, irec);
184 return xfs_refcount_complain_bad_rec(cur, fa, irec);
186 trace_xfs_refcount_get(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
191 * Update the record referred to by cur to the value given
192 * by [bno, len, refcount].
193 * This either works (return 0) or gets an EFSCORRUPTED error.
197 struct xfs_btree_cur *cur,
198 struct xfs_refcount_irec *irec)
200 union xfs_btree_rec rec;
204 trace_xfs_refcount_update(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
206 start = xfs_refcount_encode_startblock(irec->rc_startblock,
208 rec.refc.rc_startblock = cpu_to_be32(start);
209 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
210 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
212 error = xfs_btree_update(cur, &rec);
214 trace_xfs_refcount_update_error(cur->bc_mp,
215 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
220 * Insert the record referred to by cur to the value given
221 * by [bno, len, refcount].
222 * This either works (return 0) or gets an EFSCORRUPTED error.
226 struct xfs_btree_cur *cur,
227 struct xfs_refcount_irec *irec,
232 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
234 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
235 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
236 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
237 cur->bc_rec.rc.rc_domain = irec->rc_domain;
239 error = xfs_btree_insert(cur, i);
242 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
243 xfs_btree_mark_sick(cur);
244 error = -EFSCORRUPTED;
250 trace_xfs_refcount_insert_error(cur->bc_mp,
251 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
256 * Remove the record referred to by cur, then set the pointer to the spot
257 * where the record could be re-inserted, in case we want to increment or
258 * decrement the cursor.
259 * This either works (return 0) or gets an EFSCORRUPTED error.
263 struct xfs_btree_cur *cur,
266 struct xfs_refcount_irec irec;
270 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
273 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
274 xfs_btree_mark_sick(cur);
275 error = -EFSCORRUPTED;
278 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_ag.pag->pag_agno, &irec);
279 error = xfs_btree_delete(cur, i);
280 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
281 xfs_btree_mark_sick(cur);
282 error = -EFSCORRUPTED;
287 error = xfs_refcount_lookup_ge(cur, irec.rc_domain, irec.rc_startblock,
291 trace_xfs_refcount_delete_error(cur->bc_mp,
292 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
297 * Adjusting the Reference Count
299 * As stated elsewhere, the reference count btree (refcbt) stores
300 * >1 reference counts for extents of physical blocks. In this
301 * operation, we're either raising or lowering the reference count of
302 * some subrange stored in the tree:
304 * <------ adjustment range ------>
305 * ----+ +---+-----+ +--+--------+---------
306 * 2 | | 3 | 4 | |17| 55 | 10
307 * ----+ +---+-----+ +--+--------+---------
308 * X axis is physical blocks number;
309 * reference counts are the numbers inside the rectangles
311 * The first thing we need to do is to ensure that there are no
312 * refcount extents crossing either boundary of the range to be
313 * adjusted. For any extent that does cross a boundary, split it into
314 * two extents so that we can increment the refcount of one of the
317 * <------ adjustment range ------>
318 * ----+ +---+-----+ +--+--------+----+----
319 * 2 | | 3 | 2 | |17| 55 | 10 | 10
320 * ----+ +---+-----+ +--+--------+----+----
322 * For this next step, let's assume that all the physical blocks in
323 * the adjustment range are mapped to a file and are therefore in use
324 * at least once. Therefore, we can infer that any gap in the
325 * refcount tree within the adjustment range represents a physical
326 * extent with refcount == 1:
328 * <------ adjustment range ------>
329 * ----+---+---+-----+-+--+--------+----+----
330 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
331 * ----+---+---+-----+-+--+--------+----+----
334 * For each extent that falls within the interval range, figure out
335 * which extent is to the left or the right of that extent. Now we
336 * have a left, current, and right extent. If the new reference count
337 * of the center extent enables us to merge left, center, and right
338 * into one record covering all three, do so. If the center extent is
339 * at the left end of the range, abuts the left extent, and its new
340 * reference count matches the left extent's record, then merge them.
341 * If the center extent is at the right end of the range, abuts the
342 * right extent, and the reference counts match, merge those. In the
343 * example, we can left merge (assuming an increment operation):
345 * <------ adjustment range ------>
346 * --------+---+-----+-+--+--------+----+----
347 * 2 | 3 | 2 |1|17| 55 | 10 | 10
348 * --------+---+-----+-+--+--------+----+----
351 * For all other extents within the range, adjust the reference count
352 * or delete it if the refcount falls below 2. If we were
353 * incrementing, the end result looks like this:
355 * <------ adjustment range ------>
356 * --------+---+-----+-+--+--------+----+----
357 * 2 | 4 | 3 |2|18| 56 | 11 | 10
358 * --------+---+-----+-+--+--------+----+----
360 * The result of a decrement operation looks as such:
362 * <------ adjustment range ------>
363 * ----+ +---+ +--+--------+----+----
364 * 2 | | 2 | |16| 54 | 9 | 10
365 * ----+ +---+ +--+--------+----+----
368 * The blocks marked "D" are freed; the blocks marked "1" are only
369 * referenced once and therefore the record is removed from the
373 /* Next block after this extent. */
374 static inline xfs_agblock_t
376 struct xfs_refcount_irec *rc)
378 return rc->rc_startblock + rc->rc_blockcount;
382 * Split a refcount extent that crosses agbno.
385 xfs_refcount_split_extent(
386 struct xfs_btree_cur *cur,
387 enum xfs_refc_domain domain,
391 struct xfs_refcount_irec rcext, tmp;
395 *shape_changed = false;
396 error = xfs_refcount_lookup_le(cur, domain, agbno, &found_rec);
402 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
405 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
406 xfs_btree_mark_sick(cur);
407 error = -EFSCORRUPTED;
410 if (rcext.rc_domain != domain)
412 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
415 *shape_changed = true;
416 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
419 /* Establish the right extent. */
421 tmp.rc_startblock = agbno;
422 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
423 error = xfs_refcount_update(cur, &tmp);
427 /* Insert the left extent. */
429 tmp.rc_blockcount = agbno - rcext.rc_startblock;
430 error = xfs_refcount_insert(cur, &tmp, &found_rec);
433 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
434 xfs_btree_mark_sick(cur);
435 error = -EFSCORRUPTED;
441 trace_xfs_refcount_split_extent_error(cur->bc_mp,
442 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
447 * Merge the left, center, and right extents.
450 xfs_refcount_merge_center_extents(
451 struct xfs_btree_cur *cur,
452 struct xfs_refcount_irec *left,
453 struct xfs_refcount_irec *center,
454 struct xfs_refcount_irec *right,
455 unsigned long long extlen,
461 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
462 cur->bc_ag.pag->pag_agno, left, center, right);
464 ASSERT(left->rc_domain == center->rc_domain);
465 ASSERT(right->rc_domain == center->rc_domain);
468 * Make sure the center and right extents are not in the btree.
469 * If the center extent was synthesized, the first delete call
470 * removes the right extent and we skip the second deletion.
471 * If center and right were in the btree, then the first delete
472 * call removes the center and the second one removes the right
475 error = xfs_refcount_lookup_ge(cur, center->rc_domain,
476 center->rc_startblock, &found_rec);
479 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
480 xfs_btree_mark_sick(cur);
481 error = -EFSCORRUPTED;
485 error = xfs_refcount_delete(cur, &found_rec);
488 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
489 xfs_btree_mark_sick(cur);
490 error = -EFSCORRUPTED;
494 if (center->rc_refcount > 1) {
495 error = xfs_refcount_delete(cur, &found_rec);
498 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
499 xfs_btree_mark_sick(cur);
500 error = -EFSCORRUPTED;
505 /* Enlarge the left extent. */
506 error = xfs_refcount_lookup_le(cur, left->rc_domain,
507 left->rc_startblock, &found_rec);
510 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
511 xfs_btree_mark_sick(cur);
512 error = -EFSCORRUPTED;
516 left->rc_blockcount = extlen;
517 error = xfs_refcount_update(cur, left);
525 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
526 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
531 * Merge with the left extent.
534 xfs_refcount_merge_left_extent(
535 struct xfs_btree_cur *cur,
536 struct xfs_refcount_irec *left,
537 struct xfs_refcount_irec *cleft,
538 xfs_agblock_t *agbno,
544 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
545 cur->bc_ag.pag->pag_agno, left, cleft);
547 ASSERT(left->rc_domain == cleft->rc_domain);
549 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
550 if (cleft->rc_refcount > 1) {
551 error = xfs_refcount_lookup_le(cur, cleft->rc_domain,
552 cleft->rc_startblock, &found_rec);
555 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
556 xfs_btree_mark_sick(cur);
557 error = -EFSCORRUPTED;
561 error = xfs_refcount_delete(cur, &found_rec);
564 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
565 xfs_btree_mark_sick(cur);
566 error = -EFSCORRUPTED;
571 /* Enlarge the left extent. */
572 error = xfs_refcount_lookup_le(cur, left->rc_domain,
573 left->rc_startblock, &found_rec);
576 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
577 xfs_btree_mark_sick(cur);
578 error = -EFSCORRUPTED;
582 left->rc_blockcount += cleft->rc_blockcount;
583 error = xfs_refcount_update(cur, left);
587 *agbno += cleft->rc_blockcount;
588 *aglen -= cleft->rc_blockcount;
592 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
593 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
598 * Merge with the right extent.
601 xfs_refcount_merge_right_extent(
602 struct xfs_btree_cur *cur,
603 struct xfs_refcount_irec *right,
604 struct xfs_refcount_irec *cright,
610 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
611 cur->bc_ag.pag->pag_agno, cright, right);
613 ASSERT(right->rc_domain == cright->rc_domain);
616 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
619 if (cright->rc_refcount > 1) {
620 error = xfs_refcount_lookup_le(cur, cright->rc_domain,
621 cright->rc_startblock, &found_rec);
624 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
625 xfs_btree_mark_sick(cur);
626 error = -EFSCORRUPTED;
630 error = xfs_refcount_delete(cur, &found_rec);
633 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
634 xfs_btree_mark_sick(cur);
635 error = -EFSCORRUPTED;
640 /* Enlarge the right extent. */
641 error = xfs_refcount_lookup_le(cur, right->rc_domain,
642 right->rc_startblock, &found_rec);
645 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
646 xfs_btree_mark_sick(cur);
647 error = -EFSCORRUPTED;
651 right->rc_startblock -= cright->rc_blockcount;
652 right->rc_blockcount += cright->rc_blockcount;
653 error = xfs_refcount_update(cur, right);
657 *aglen -= cright->rc_blockcount;
661 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
662 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
667 * Find the left extent and the one after it (cleft). This function assumes
668 * that we've already split any extent crossing agbno.
671 xfs_refcount_find_left_extents(
672 struct xfs_btree_cur *cur,
673 struct xfs_refcount_irec *left,
674 struct xfs_refcount_irec *cleft,
675 enum xfs_refc_domain domain,
679 struct xfs_refcount_irec tmp;
683 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
684 error = xfs_refcount_lookup_le(cur, domain, agbno - 1, &found_rec);
690 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
693 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
694 xfs_btree_mark_sick(cur);
695 error = -EFSCORRUPTED;
699 if (tmp.rc_domain != domain)
701 if (xfs_refc_next(&tmp) != agbno)
703 /* We have a left extent; retrieve (or invent) the next right one */
706 error = xfs_btree_increment(cur, 0, &found_rec);
710 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
713 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
714 xfs_btree_mark_sick(cur);
715 error = -EFSCORRUPTED;
719 if (tmp.rc_domain != domain)
722 /* if tmp starts at the end of our range, just use that */
723 if (tmp.rc_startblock == agbno)
727 * There's a gap in the refcntbt at the start of the
728 * range we're interested in (refcount == 1) so
729 * synthesize the implied extent and pass it back.
730 * We assume here that the agbno/aglen range was
731 * passed in from a data fork extent mapping and
732 * therefore is allocated to exactly one owner.
734 cleft->rc_startblock = agbno;
735 cleft->rc_blockcount = min(aglen,
736 tmp.rc_startblock - agbno);
737 cleft->rc_refcount = 1;
738 cleft->rc_domain = domain;
743 * No extents, so pretend that there's one covering the whole
746 cleft->rc_startblock = agbno;
747 cleft->rc_blockcount = aglen;
748 cleft->rc_refcount = 1;
749 cleft->rc_domain = domain;
751 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
756 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
757 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
762 * Find the right extent and the one before it (cright). This function
763 * assumes that we've already split any extents crossing agbno + aglen.
766 xfs_refcount_find_right_extents(
767 struct xfs_btree_cur *cur,
768 struct xfs_refcount_irec *right,
769 struct xfs_refcount_irec *cright,
770 enum xfs_refc_domain domain,
774 struct xfs_refcount_irec tmp;
778 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
779 error = xfs_refcount_lookup_ge(cur, domain, agbno + aglen, &found_rec);
785 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
788 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
789 xfs_btree_mark_sick(cur);
790 error = -EFSCORRUPTED;
794 if (tmp.rc_domain != domain)
796 if (tmp.rc_startblock != agbno + aglen)
798 /* We have a right extent; retrieve (or invent) the next left one */
801 error = xfs_btree_decrement(cur, 0, &found_rec);
805 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
808 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
809 xfs_btree_mark_sick(cur);
810 error = -EFSCORRUPTED;
814 if (tmp.rc_domain != domain)
817 /* if tmp ends at the end of our range, just use that */
818 if (xfs_refc_next(&tmp) == agbno + aglen)
822 * There's a gap in the refcntbt at the end of the
823 * range we're interested in (refcount == 1) so
824 * create the implied extent and pass it back.
825 * We assume here that the agbno/aglen range was
826 * passed in from a data fork extent mapping and
827 * therefore is allocated to exactly one owner.
829 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
830 cright->rc_blockcount = right->rc_startblock -
831 cright->rc_startblock;
832 cright->rc_refcount = 1;
833 cright->rc_domain = domain;
838 * No extents, so pretend that there's one covering the whole
841 cright->rc_startblock = agbno;
842 cright->rc_blockcount = aglen;
843 cright->rc_refcount = 1;
844 cright->rc_domain = domain;
846 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
847 cright, right, agbno + aglen);
851 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
852 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
856 /* Is this extent valid? */
859 const struct xfs_refcount_irec *rc)
861 return rc->rc_startblock != NULLAGBLOCK;
864 static inline xfs_nlink_t
865 xfs_refc_merge_refcount(
866 const struct xfs_refcount_irec *irec,
867 enum xfs_refc_adjust_op adjust)
869 /* Once a record hits MAXREFCOUNT, it is pinned there forever */
870 if (irec->rc_refcount == MAXREFCOUNT)
872 return irec->rc_refcount + adjust;
876 xfs_refc_want_merge_center(
877 const struct xfs_refcount_irec *left,
878 const struct xfs_refcount_irec *cleft,
879 const struct xfs_refcount_irec *cright,
880 const struct xfs_refcount_irec *right,
881 bool cleft_is_cright,
882 enum xfs_refc_adjust_op adjust,
883 unsigned long long *ulenp)
885 unsigned long long ulen = left->rc_blockcount;
886 xfs_nlink_t new_refcount;
889 * To merge with a center record, both shoulder records must be
890 * adjacent to the record we want to adjust. This is only true if
891 * find_left and find_right made all four records valid.
893 if (!xfs_refc_valid(left) || !xfs_refc_valid(right) ||
894 !xfs_refc_valid(cleft) || !xfs_refc_valid(cright))
897 /* There must only be one record for the entire range. */
898 if (!cleft_is_cright)
901 /* The shoulder record refcounts must match the new refcount. */
902 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
903 if (left->rc_refcount != new_refcount)
905 if (right->rc_refcount != new_refcount)
909 * The new record cannot exceed the max length. ulen is a ULL as the
910 * individual record block counts can be up to (u32 - 1) in length
911 * hence we need to catch u32 addition overflows here.
913 ulen += cleft->rc_blockcount + right->rc_blockcount;
914 if (ulen >= MAXREFCEXTLEN)
922 xfs_refc_want_merge_left(
923 const struct xfs_refcount_irec *left,
924 const struct xfs_refcount_irec *cleft,
925 enum xfs_refc_adjust_op adjust)
927 unsigned long long ulen = left->rc_blockcount;
928 xfs_nlink_t new_refcount;
931 * For a left merge, the left shoulder record must be adjacent to the
932 * start of the range. If this is true, find_left made left and cleft
933 * contain valid contents.
935 if (!xfs_refc_valid(left) || !xfs_refc_valid(cleft))
938 /* Left shoulder record refcount must match the new refcount. */
939 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
940 if (left->rc_refcount != new_refcount)
944 * The new record cannot exceed the max length. ulen is a ULL as the
945 * individual record block counts can be up to (u32 - 1) in length
946 * hence we need to catch u32 addition overflows here.
948 ulen += cleft->rc_blockcount;
949 if (ulen >= MAXREFCEXTLEN)
956 xfs_refc_want_merge_right(
957 const struct xfs_refcount_irec *cright,
958 const struct xfs_refcount_irec *right,
959 enum xfs_refc_adjust_op adjust)
961 unsigned long long ulen = right->rc_blockcount;
962 xfs_nlink_t new_refcount;
965 * For a right merge, the right shoulder record must be adjacent to the
966 * end of the range. If this is true, find_right made cright and right
967 * contain valid contents.
969 if (!xfs_refc_valid(right) || !xfs_refc_valid(cright))
972 /* Right shoulder record refcount must match the new refcount. */
973 new_refcount = xfs_refc_merge_refcount(cright, adjust);
974 if (right->rc_refcount != new_refcount)
978 * The new record cannot exceed the max length. ulen is a ULL as the
979 * individual record block counts can be up to (u32 - 1) in length
980 * hence we need to catch u32 addition overflows here.
982 ulen += cright->rc_blockcount;
983 if (ulen >= MAXREFCEXTLEN)
990 * Try to merge with any extents on the boundaries of the adjustment range.
993 xfs_refcount_merge_extents(
994 struct xfs_btree_cur *cur,
995 enum xfs_refc_domain domain,
996 xfs_agblock_t *agbno,
998 enum xfs_refc_adjust_op adjust,
1001 struct xfs_refcount_irec left = {0}, cleft = {0};
1002 struct xfs_refcount_irec cright = {0}, right = {0};
1004 unsigned long long ulen;
1007 *shape_changed = false;
1009 * Find the extent just below agbno [left], just above agbno [cleft],
1010 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
1013 error = xfs_refcount_find_left_extents(cur, &left, &cleft, domain,
1017 error = xfs_refcount_find_right_extents(cur, &right, &cright, domain,
1022 /* No left or right extent to merge; exit. */
1023 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
1026 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
1027 (cleft.rc_blockcount == cright.rc_blockcount);
1029 /* Try to merge left, cleft, and right. cleft must == cright. */
1030 if (xfs_refc_want_merge_center(&left, &cleft, &cright, &right, cequal,
1032 *shape_changed = true;
1033 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
1034 &right, ulen, aglen);
1037 /* Try to merge left and cleft. */
1038 if (xfs_refc_want_merge_left(&left, &cleft, adjust)) {
1039 *shape_changed = true;
1040 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
1046 * If we just merged left + cleft and cleft == cright,
1047 * we no longer have a cright to merge with right. We're done.
1053 /* Try to merge cright and right. */
1054 if (xfs_refc_want_merge_right(&cright, &right, adjust)) {
1055 *shape_changed = true;
1056 return xfs_refcount_merge_right_extent(cur, &right, &cright,
1064 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
1065 * true incorrectly is a shutdown FS; the penalty for guessing false
1066 * incorrectly is more transaction rolls than might be necessary.
1067 * Be conservative here.
1070 xfs_refcount_still_have_space(
1071 struct xfs_btree_cur *cur)
1073 unsigned long overhead;
1076 * Worst case estimate: full splits of the free space and rmap btrees
1077 * to handle each of the shape changes to the refcount btree.
1079 overhead = xfs_allocfree_block_count(cur->bc_mp,
1080 cur->bc_refc.shape_changes);
1081 overhead += cur->bc_mp->m_refc_maxlevels;
1082 overhead *= cur->bc_mp->m_sb.sb_blocksize;
1085 * Only allow 2 refcount extent updates per transaction if the
1086 * refcount continue update "error" has been injected.
1088 if (cur->bc_refc.nr_ops > 2 &&
1089 XFS_TEST_ERROR(false, cur->bc_mp,
1090 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
1093 if (cur->bc_refc.nr_ops == 0)
1095 else if (overhead > cur->bc_tp->t_log_res)
1097 return cur->bc_tp->t_log_res - overhead >
1098 cur->bc_refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
1102 * Adjust the refcounts of middle extents. At this point we should have
1103 * split extents that crossed the adjustment range; merged with adjacent
1104 * extents; and updated agbno/aglen to reflect the merges. Therefore,
1105 * all we have to do is update the extents inside [agbno, agbno + aglen].
1108 xfs_refcount_adjust_extents(
1109 struct xfs_btree_cur *cur,
1110 xfs_agblock_t *agbno,
1111 xfs_extlen_t *aglen,
1112 enum xfs_refc_adjust_op adj)
1114 struct xfs_refcount_irec ext, tmp;
1116 int found_rec, found_tmp;
1117 xfs_fsblock_t fsbno;
1119 /* Merging did all the work already. */
1123 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_SHARED, *agbno,
1128 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
1129 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1132 if (!found_rec || ext.rc_domain != XFS_REFC_DOMAIN_SHARED) {
1133 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1134 ext.rc_blockcount = 0;
1135 ext.rc_refcount = 0;
1136 ext.rc_domain = XFS_REFC_DOMAIN_SHARED;
1140 * Deal with a hole in the refcount tree; if a file maps to
1141 * these blocks and there's no refcountbt record, pretend that
1142 * there is one with refcount == 1.
1144 if (ext.rc_startblock != *agbno) {
1145 tmp.rc_startblock = *agbno;
1146 tmp.rc_blockcount = min(*aglen,
1147 ext.rc_startblock - *agbno);
1148 tmp.rc_refcount = 1 + adj;
1149 tmp.rc_domain = XFS_REFC_DOMAIN_SHARED;
1151 trace_xfs_refcount_modify_extent(cur->bc_mp,
1152 cur->bc_ag.pag->pag_agno, &tmp);
1155 * Either cover the hole (increment) or
1156 * delete the range (decrement).
1158 cur->bc_refc.nr_ops++;
1159 if (tmp.rc_refcount) {
1160 error = xfs_refcount_insert(cur, &tmp,
1164 if (XFS_IS_CORRUPT(cur->bc_mp,
1166 xfs_btree_mark_sick(cur);
1167 error = -EFSCORRUPTED;
1171 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
1172 cur->bc_ag.pag->pag_agno,
1174 error = xfs_free_extent_later(cur->bc_tp, fsbno,
1175 tmp.rc_blockcount, NULL,
1176 XFS_AG_RESV_NONE, false);
1181 (*agbno) += tmp.rc_blockcount;
1182 (*aglen) -= tmp.rc_blockcount;
1184 /* Stop if there's nothing left to modify */
1185 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
1188 /* Move the cursor to the start of ext. */
1189 error = xfs_refcount_lookup_ge(cur,
1190 XFS_REFC_DOMAIN_SHARED, *agbno,
1197 * A previous step trimmed agbno/aglen such that the end of the
1198 * range would not be in the middle of the record. If this is
1199 * no longer the case, something is seriously wrong with the
1200 * btree. Make sure we never feed the synthesized record into
1201 * the processing loop below.
1203 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount == 0) ||
1204 XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount > *aglen)) {
1205 xfs_btree_mark_sick(cur);
1206 error = -EFSCORRUPTED;
1211 * Adjust the reference count and either update the tree
1212 * (incr) or free the blocks (decr).
1214 if (ext.rc_refcount == MAXREFCOUNT)
1216 ext.rc_refcount += adj;
1217 trace_xfs_refcount_modify_extent(cur->bc_mp,
1218 cur->bc_ag.pag->pag_agno, &ext);
1219 cur->bc_refc.nr_ops++;
1220 if (ext.rc_refcount > 1) {
1221 error = xfs_refcount_update(cur, &ext);
1224 } else if (ext.rc_refcount == 1) {
1225 error = xfs_refcount_delete(cur, &found_rec);
1228 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1229 xfs_btree_mark_sick(cur);
1230 error = -EFSCORRUPTED;
1235 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
1236 cur->bc_ag.pag->pag_agno,
1238 error = xfs_free_extent_later(cur->bc_tp, fsbno,
1239 ext.rc_blockcount, NULL,
1240 XFS_AG_RESV_NONE, false);
1246 error = xfs_btree_increment(cur, 0, &found_rec);
1251 (*agbno) += ext.rc_blockcount;
1252 (*aglen) -= ext.rc_blockcount;
1257 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1258 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1262 /* Adjust the reference count of a range of AG blocks. */
1264 xfs_refcount_adjust(
1265 struct xfs_btree_cur *cur,
1266 xfs_agblock_t *agbno,
1267 xfs_extlen_t *aglen,
1268 enum xfs_refc_adjust_op adj)
1271 int shape_changes = 0;
1274 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1275 trace_xfs_refcount_increase(cur->bc_mp,
1276 cur->bc_ag.pag->pag_agno, *agbno, *aglen);
1278 trace_xfs_refcount_decrease(cur->bc_mp,
1279 cur->bc_ag.pag->pag_agno, *agbno, *aglen);
1282 * Ensure that no rcextents cross the boundary of the adjustment range.
1284 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1285 *agbno, &shape_changed);
1291 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1292 *agbno + *aglen, &shape_changed);
1299 * Try to merge with the left or right extents of the range.
1301 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_SHARED,
1302 agbno, aglen, adj, &shape_changed);
1308 cur->bc_refc.shape_changes++;
1310 /* Now that we've taken care of the ends, adjust the middle extents */
1311 error = xfs_refcount_adjust_extents(cur, agbno, aglen, adj);
1318 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1323 /* Clean up after calling xfs_refcount_finish_one. */
1325 xfs_refcount_finish_one_cleanup(
1326 struct xfs_trans *tp,
1327 struct xfs_btree_cur *rcur,
1330 struct xfs_buf *agbp;
1334 agbp = rcur->bc_ag.agbp;
1335 xfs_btree_del_cursor(rcur, error);
1337 xfs_trans_brelse(tp, agbp);
1341 * Set up a continuation a deferred refcount operation by updating the intent.
1342 * Checks to make sure we're not going to run off the end of the AG.
1345 xfs_refcount_continue_op(
1346 struct xfs_btree_cur *cur,
1347 struct xfs_refcount_intent *ri,
1348 xfs_agblock_t new_agbno)
1350 struct xfs_mount *mp = cur->bc_mp;
1351 struct xfs_perag *pag = cur->bc_ag.pag;
1353 if (XFS_IS_CORRUPT(mp, !xfs_verify_agbext(pag, new_agbno,
1354 ri->ri_blockcount))) {
1355 xfs_btree_mark_sick(cur);
1356 return -EFSCORRUPTED;
1359 ri->ri_startblock = XFS_AGB_TO_FSB(mp, pag->pag_agno, new_agbno);
1361 ASSERT(xfs_verify_fsbext(mp, ri->ri_startblock, ri->ri_blockcount));
1362 ASSERT(pag->pag_agno == XFS_FSB_TO_AGNO(mp, ri->ri_startblock));
1368 * Process one of the deferred refcount operations. We pass back the
1369 * btree cursor to maintain our lock on the btree between calls.
1370 * This saves time and eliminates a buffer deadlock between the
1371 * superblock and the AGF because we'll always grab them in the same
1375 xfs_refcount_finish_one(
1376 struct xfs_trans *tp,
1377 struct xfs_refcount_intent *ri,
1378 struct xfs_btree_cur **pcur)
1380 struct xfs_mount *mp = tp->t_mountp;
1381 struct xfs_btree_cur *rcur;
1382 struct xfs_buf *agbp = NULL;
1385 unsigned long nr_ops = 0;
1386 int shape_changes = 0;
1388 bno = XFS_FSB_TO_AGBNO(mp, ri->ri_startblock);
1390 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, ri->ri_startblock),
1391 ri->ri_type, XFS_FSB_TO_AGBNO(mp, ri->ri_startblock),
1394 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REFCOUNT_FINISH_ONE))
1398 * If we haven't gotten a cursor or the cursor AG doesn't match
1399 * the startblock, get one now.
1402 if (rcur != NULL && rcur->bc_ag.pag != ri->ri_pag) {
1403 nr_ops = rcur->bc_refc.nr_ops;
1404 shape_changes = rcur->bc_refc.shape_changes;
1405 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1410 error = xfs_alloc_read_agf(ri->ri_pag, tp,
1411 XFS_ALLOC_FLAG_FREEING, &agbp);
1415 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, ri->ri_pag);
1416 rcur->bc_refc.nr_ops = nr_ops;
1417 rcur->bc_refc.shape_changes = shape_changes;
1421 switch (ri->ri_type) {
1422 case XFS_REFCOUNT_INCREASE:
1423 error = xfs_refcount_adjust(rcur, &bno, &ri->ri_blockcount,
1424 XFS_REFCOUNT_ADJUST_INCREASE);
1427 if (ri->ri_blockcount > 0)
1428 error = xfs_refcount_continue_op(rcur, ri, bno);
1430 case XFS_REFCOUNT_DECREASE:
1431 error = xfs_refcount_adjust(rcur, &bno, &ri->ri_blockcount,
1432 XFS_REFCOUNT_ADJUST_DECREASE);
1435 if (ri->ri_blockcount > 0)
1436 error = xfs_refcount_continue_op(rcur, ri, bno);
1438 case XFS_REFCOUNT_ALLOC_COW:
1439 error = __xfs_refcount_cow_alloc(rcur, bno, ri->ri_blockcount);
1442 ri->ri_blockcount = 0;
1444 case XFS_REFCOUNT_FREE_COW:
1445 error = __xfs_refcount_cow_free(rcur, bno, ri->ri_blockcount);
1448 ri->ri_blockcount = 0;
1452 return -EFSCORRUPTED;
1454 if (!error && ri->ri_blockcount > 0)
1455 trace_xfs_refcount_finish_one_leftover(mp, ri->ri_pag->pag_agno,
1456 ri->ri_type, bno, ri->ri_blockcount);
1461 * Record a refcount intent for later processing.
1465 struct xfs_trans *tp,
1466 enum xfs_refcount_intent_type type,
1467 xfs_fsblock_t startblock,
1468 xfs_extlen_t blockcount)
1470 struct xfs_refcount_intent *ri;
1472 trace_xfs_refcount_defer(tp->t_mountp,
1473 XFS_FSB_TO_AGNO(tp->t_mountp, startblock),
1474 type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
1477 ri = kmem_cache_alloc(xfs_refcount_intent_cache,
1478 GFP_KERNEL | __GFP_NOFAIL);
1479 INIT_LIST_HEAD(&ri->ri_list);
1481 ri->ri_startblock = startblock;
1482 ri->ri_blockcount = blockcount;
1484 xfs_refcount_update_get_group(tp->t_mountp, ri);
1485 xfs_defer_add(tp, &ri->ri_list, &xfs_refcount_update_defer_type);
1489 * Increase the reference count of the blocks backing a file's extent.
1492 xfs_refcount_increase_extent(
1493 struct xfs_trans *tp,
1494 struct xfs_bmbt_irec *PREV)
1496 if (!xfs_has_reflink(tp->t_mountp))
1499 __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, PREV->br_startblock,
1500 PREV->br_blockcount);
1504 * Decrease the reference count of the blocks backing a file's extent.
1507 xfs_refcount_decrease_extent(
1508 struct xfs_trans *tp,
1509 struct xfs_bmbt_irec *PREV)
1511 if (!xfs_has_reflink(tp->t_mountp))
1514 __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, PREV->br_startblock,
1515 PREV->br_blockcount);
1519 * Given an AG extent, find the lowest-numbered run of shared blocks
1520 * within that range and return the range in fbno/flen. If
1521 * find_end_of_shared is set, return the longest contiguous extent of
1522 * shared blocks; if not, just return the first extent we find. If no
1523 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1524 * and 0, respectively.
1527 xfs_refcount_find_shared(
1528 struct xfs_btree_cur *cur,
1529 xfs_agblock_t agbno,
1531 xfs_agblock_t *fbno,
1533 bool find_end_of_shared)
1535 struct xfs_refcount_irec tmp;
1540 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1543 /* By default, skip the whole range */
1544 *fbno = NULLAGBLOCK;
1547 /* Try to find a refcount extent that crosses the start */
1548 error = xfs_refcount_lookup_le(cur, XFS_REFC_DOMAIN_SHARED, agbno,
1553 /* No left extent, look at the next one */
1554 error = xfs_btree_increment(cur, 0, &have);
1560 error = xfs_refcount_get_rec(cur, &tmp, &i);
1563 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1564 xfs_btree_mark_sick(cur);
1565 error = -EFSCORRUPTED;
1568 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1571 /* If the extent ends before the start, look at the next one */
1572 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1573 error = xfs_btree_increment(cur, 0, &have);
1578 error = xfs_refcount_get_rec(cur, &tmp, &i);
1581 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1582 xfs_btree_mark_sick(cur);
1583 error = -EFSCORRUPTED;
1586 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1590 /* If the extent starts after the range we want, bail out */
1591 if (tmp.rc_startblock >= agbno + aglen)
1594 /* We found the start of a shared extent! */
1595 if (tmp.rc_startblock < agbno) {
1596 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1597 tmp.rc_startblock = agbno;
1600 *fbno = tmp.rc_startblock;
1601 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1602 if (!find_end_of_shared)
1605 /* Otherwise, find the end of this shared extent */
1606 while (*fbno + *flen < agbno + aglen) {
1607 error = xfs_btree_increment(cur, 0, &have);
1612 error = xfs_refcount_get_rec(cur, &tmp, &i);
1615 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1616 xfs_btree_mark_sick(cur);
1617 error = -EFSCORRUPTED;
1620 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED ||
1621 tmp.rc_startblock >= agbno + aglen ||
1622 tmp.rc_startblock != *fbno + *flen)
1624 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1628 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1629 cur->bc_ag.pag->pag_agno, *fbno, *flen);
1633 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1634 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1639 * Recovering CoW Blocks After a Crash
1641 * Due to the way that the copy on write mechanism works, there's a window of
1642 * opportunity in which we can lose track of allocated blocks during a crash.
1643 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1644 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1645 * no longer in the free space btree but are not otherwise recorded anywhere
1646 * until the write completes and the blocks are mapped into the file. A crash
1647 * in between allocation and remapping results in the replacement blocks being
1648 * lost. This situation is exacerbated by the CoW extent size hint because
1649 * allocations can hang around for long time.
1651 * However, there is a place where we can record these allocations before they
1652 * become mappings -- the reference count btree. The btree does not record
1653 * extents with refcount == 1, so we can record allocations with a refcount of
1654 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1655 * no conflict with shared block records. These mappings should be created
1656 * when we allocate blocks to the CoW fork and deleted when they're removed
1657 * from the CoW fork.
1659 * Minor nit: records for in-progress CoW allocations and records for shared
1660 * extents must never be merged, to preserve the property that (except for CoW
1661 * allocations) there are no refcount btree entries with refcount == 1. The
1662 * only time this could potentially happen is when unsharing a block that's
1663 * adjacent to CoW allocations, so we must be careful to avoid this.
1665 * At mount time we recover lost CoW allocations by searching the refcount
1666 * btree for these refcount == 1 mappings. These represent CoW allocations
1667 * that were in progress at the time the filesystem went down, so we can free
1668 * them to get the space back.
1670 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1671 * several reasons -- first, EFIs pin the tail of the log and would have to be
1672 * periodically relogged to avoid filling up the log. Second, CoW completions
1673 * will have to file an EFD and create new EFIs for whatever remains in the
1674 * CoW fork; this partially takes care of (1) but extent-size reservations
1675 * will have to periodically relog even if there's no writeout in progress.
1676 * This can happen if the CoW extent size hint is set, which you really want.
1677 * Third, EFIs cannot currently be automatically relogged into newer
1678 * transactions to advance the log tail. Fourth, stuffing the log full of
1679 * EFIs places an upper bound on the number of CoW allocations that can be
1680 * held filesystem-wide at any given time. Recording them in the refcount
1681 * btree doesn't require us to maintain any state in memory and doesn't pin
1685 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1686 * in that they're not referenced anywhere else in the filesystem, so we
1687 * stash them in the refcount btree with a refcount of 1 until either file
1688 * remapping (or CoW cancellation) happens.
1691 xfs_refcount_adjust_cow_extents(
1692 struct xfs_btree_cur *cur,
1693 xfs_agblock_t agbno,
1695 enum xfs_refc_adjust_op adj)
1697 struct xfs_refcount_irec ext, tmp;
1699 int found_rec, found_tmp;
1704 /* Find any overlapping refcount records */
1705 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_COW, agbno,
1709 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1712 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec &&
1713 ext.rc_domain != XFS_REFC_DOMAIN_COW)) {
1714 xfs_btree_mark_sick(cur);
1715 error = -EFSCORRUPTED;
1719 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1720 ext.rc_blockcount = 0;
1721 ext.rc_refcount = 0;
1722 ext.rc_domain = XFS_REFC_DOMAIN_COW;
1726 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1727 /* Adding a CoW reservation, there should be nothing here. */
1728 if (XFS_IS_CORRUPT(cur->bc_mp,
1729 agbno + aglen > ext.rc_startblock)) {
1730 xfs_btree_mark_sick(cur);
1731 error = -EFSCORRUPTED;
1735 tmp.rc_startblock = agbno;
1736 tmp.rc_blockcount = aglen;
1737 tmp.rc_refcount = 1;
1738 tmp.rc_domain = XFS_REFC_DOMAIN_COW;
1740 trace_xfs_refcount_modify_extent(cur->bc_mp,
1741 cur->bc_ag.pag->pag_agno, &tmp);
1743 error = xfs_refcount_insert(cur, &tmp,
1747 if (XFS_IS_CORRUPT(cur->bc_mp, found_tmp != 1)) {
1748 xfs_btree_mark_sick(cur);
1749 error = -EFSCORRUPTED;
1753 case XFS_REFCOUNT_ADJUST_COW_FREE:
1754 /* Removing a CoW reservation, there should be one extent. */
1755 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_startblock != agbno)) {
1756 xfs_btree_mark_sick(cur);
1757 error = -EFSCORRUPTED;
1760 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount != aglen)) {
1761 xfs_btree_mark_sick(cur);
1762 error = -EFSCORRUPTED;
1765 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_refcount != 1)) {
1766 xfs_btree_mark_sick(cur);
1767 error = -EFSCORRUPTED;
1771 ext.rc_refcount = 0;
1772 trace_xfs_refcount_modify_extent(cur->bc_mp,
1773 cur->bc_ag.pag->pag_agno, &ext);
1774 error = xfs_refcount_delete(cur, &found_rec);
1777 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1778 xfs_btree_mark_sick(cur);
1779 error = -EFSCORRUPTED;
1789 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1790 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1795 * Add or remove refcount btree entries for CoW reservations.
1798 xfs_refcount_adjust_cow(
1799 struct xfs_btree_cur *cur,
1800 xfs_agblock_t agbno,
1802 enum xfs_refc_adjust_op adj)
1808 * Ensure that no rcextents cross the boundary of the adjustment range.
1810 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1811 agbno, &shape_changed);
1815 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1816 agbno + aglen, &shape_changed);
1821 * Try to merge with the left or right extents of the range.
1823 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_COW, &agbno,
1824 &aglen, adj, &shape_changed);
1828 /* Now that we've taken care of the ends, adjust the middle extents */
1829 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1836 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1842 * Record a CoW allocation in the refcount btree.
1845 __xfs_refcount_cow_alloc(
1846 struct xfs_btree_cur *rcur,
1847 xfs_agblock_t agbno,
1850 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1853 /* Add refcount btree reservation */
1854 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1855 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1859 * Remove a CoW allocation from the refcount btree.
1862 __xfs_refcount_cow_free(
1863 struct xfs_btree_cur *rcur,
1864 xfs_agblock_t agbno,
1867 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1870 /* Remove refcount btree reservation */
1871 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1872 XFS_REFCOUNT_ADJUST_COW_FREE);
1875 /* Record a CoW staging extent in the refcount btree. */
1877 xfs_refcount_alloc_cow_extent(
1878 struct xfs_trans *tp,
1882 struct xfs_mount *mp = tp->t_mountp;
1884 if (!xfs_has_reflink(mp))
1887 __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len);
1889 /* Add rmap entry */
1890 xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1891 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1894 /* Forget a CoW staging event in the refcount btree. */
1896 xfs_refcount_free_cow_extent(
1897 struct xfs_trans *tp,
1901 struct xfs_mount *mp = tp->t_mountp;
1903 if (!xfs_has_reflink(mp))
1906 /* Remove rmap entry */
1907 xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1908 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1909 __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len);
1912 struct xfs_refcount_recovery {
1913 struct list_head rr_list;
1914 struct xfs_refcount_irec rr_rrec;
1917 /* Stuff an extent on the recovery list. */
1919 xfs_refcount_recover_extent(
1920 struct xfs_btree_cur *cur,
1921 const union xfs_btree_rec *rec,
1924 struct list_head *debris = priv;
1925 struct xfs_refcount_recovery *rr;
1927 if (XFS_IS_CORRUPT(cur->bc_mp,
1928 be32_to_cpu(rec->refc.rc_refcount) != 1)) {
1929 xfs_btree_mark_sick(cur);
1930 return -EFSCORRUPTED;
1933 rr = kmalloc(sizeof(struct xfs_refcount_recovery),
1934 GFP_KERNEL | __GFP_NOFAIL);
1935 INIT_LIST_HEAD(&rr->rr_list);
1936 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1938 if (xfs_refcount_check_irec(cur->bc_ag.pag, &rr->rr_rrec) != NULL ||
1939 XFS_IS_CORRUPT(cur->bc_mp,
1940 rr->rr_rrec.rc_domain != XFS_REFC_DOMAIN_COW)) {
1941 xfs_btree_mark_sick(cur);
1943 return -EFSCORRUPTED;
1946 list_add_tail(&rr->rr_list, debris);
1950 /* Find and remove leftover CoW reservations. */
1952 xfs_refcount_recover_cow_leftovers(
1953 struct xfs_mount *mp,
1954 struct xfs_perag *pag)
1956 struct xfs_trans *tp;
1957 struct xfs_btree_cur *cur;
1958 struct xfs_buf *agbp;
1959 struct xfs_refcount_recovery *rr, *n;
1960 struct list_head debris;
1961 union xfs_btree_irec low = {
1962 .rc.rc_domain = XFS_REFC_DOMAIN_COW,
1964 union xfs_btree_irec high = {
1965 .rc.rc_domain = XFS_REFC_DOMAIN_COW,
1966 .rc.rc_startblock = -1U,
1971 /* reflink filesystems mustn't have AGs larger than 2^31-1 blocks */
1972 BUILD_BUG_ON(XFS_MAX_CRC_AG_BLOCKS >= XFS_REFC_COWFLAG);
1973 if (mp->m_sb.sb_agblocks > XFS_MAX_CRC_AG_BLOCKS)
1976 INIT_LIST_HEAD(&debris);
1979 * In this first part, we use an empty transaction to gather up
1980 * all the leftover CoW extents so that we can subsequently
1981 * delete them. The empty transaction is used to avoid
1982 * a buffer lock deadlock if there happens to be a loop in the
1983 * refcountbt because we're allowed to re-grab a buffer that is
1984 * already attached to our transaction. When we're done
1985 * recording the CoW debris we cancel the (empty) transaction
1986 * and everything goes away cleanly.
1988 error = xfs_trans_alloc_empty(mp, &tp);
1992 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
1995 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1997 /* Find all the leftover CoW staging extents. */
1998 error = xfs_btree_query_range(cur, &low, &high,
1999 xfs_refcount_recover_extent, &debris);
2000 xfs_btree_del_cursor(cur, error);
2001 xfs_trans_brelse(tp, agbp);
2002 xfs_trans_cancel(tp);
2006 /* Now iterate the list to free the leftovers */
2007 list_for_each_entry_safe(rr, n, &debris, rr_list) {
2008 /* Set up transaction. */
2009 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
2013 trace_xfs_refcount_recover_extent(mp, pag->pag_agno,
2016 /* Free the orphan record */
2017 fsb = XFS_AGB_TO_FSB(mp, pag->pag_agno,
2018 rr->rr_rrec.rc_startblock);
2019 xfs_refcount_free_cow_extent(tp, fsb,
2020 rr->rr_rrec.rc_blockcount);
2022 /* Free the block. */
2023 error = xfs_free_extent_later(tp, fsb,
2024 rr->rr_rrec.rc_blockcount, NULL,
2025 XFS_AG_RESV_NONE, false);
2029 error = xfs_trans_commit(tp);
2033 list_del(&rr->rr_list);
2039 xfs_trans_cancel(tp);
2041 /* Free the leftover list */
2042 list_for_each_entry_safe(rr, n, &debris, rr_list) {
2043 list_del(&rr->rr_list);
2050 * Scan part of the keyspace of the refcount records and tell us if the area
2051 * has no records, is fully mapped by records, or is partially filled.
2054 xfs_refcount_has_records(
2055 struct xfs_btree_cur *cur,
2056 enum xfs_refc_domain domain,
2059 enum xbtree_recpacking *outcome)
2061 union xfs_btree_irec low;
2062 union xfs_btree_irec high;
2064 memset(&low, 0, sizeof(low));
2065 low.rc.rc_startblock = bno;
2066 memset(&high, 0xFF, sizeof(high));
2067 high.rc.rc_startblock = bno + len - 1;
2068 low.rc.rc_domain = high.rc.rc_domain = domain;
2070 return xfs_btree_has_records(cur, &low, &high, NULL, outcome);
2073 struct xfs_refcount_query_range_info {
2074 xfs_refcount_query_range_fn fn;
2078 /* Format btree record and pass to our callback. */
2080 xfs_refcount_query_range_helper(
2081 struct xfs_btree_cur *cur,
2082 const union xfs_btree_rec *rec,
2085 struct xfs_refcount_query_range_info *query = priv;
2086 struct xfs_refcount_irec irec;
2089 xfs_refcount_btrec_to_irec(rec, &irec);
2090 fa = xfs_refcount_check_irec(cur->bc_ag.pag, &irec);
2092 return xfs_refcount_complain_bad_rec(cur, fa, &irec);
2094 return query->fn(cur, &irec, query->priv);
2097 /* Find all refcount records between two keys. */
2099 xfs_refcount_query_range(
2100 struct xfs_btree_cur *cur,
2101 const struct xfs_refcount_irec *low_rec,
2102 const struct xfs_refcount_irec *high_rec,
2103 xfs_refcount_query_range_fn fn,
2106 union xfs_btree_irec low_brec = { .rc = *low_rec };
2107 union xfs_btree_irec high_brec = { .rc = *high_rec };
2108 struct xfs_refcount_query_range_info query = { .priv = priv, .fn = fn };
2110 return xfs_btree_query_range(cur, &low_brec, &high_brec,
2111 xfs_refcount_query_range_helper, &query);
2115 xfs_refcount_intent_init_cache(void)
2117 xfs_refcount_intent_cache = kmem_cache_create("xfs_refc_intent",
2118 sizeof(struct xfs_refcount_intent),
2121 return xfs_refcount_intent_cache != NULL ? 0 : -ENOMEM;
2125 xfs_refcount_intent_destroy_cache(void)
2127 kmem_cache_destroy(xfs_refcount_intent_cache);
2128 xfs_refcount_intent_cache = NULL;