Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
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"
15#include "xfs_bmap.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"
22#include "xfs_bit.h"
23#include "xfs_refcount.h"
24#include "xfs_rmap.h"
25#include "xfs_ag.h"
26
27struct kmem_cache *xfs_refcount_intent_cache;
28
29/* Allowable refcount adjustment amounts. */
30enum xfs_refc_adjust_op {
31 XFS_REFCOUNT_ADJUST_INCREASE = 1,
32 XFS_REFCOUNT_ADJUST_DECREASE = -1,
33 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
34 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
35};
36
37STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
38 xfs_agblock_t agbno, xfs_extlen_t aglen);
39STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
40 xfs_agblock_t agbno, xfs_extlen_t aglen);
41
42/*
43 * Look up the first record less than or equal to [bno, len] in the btree
44 * given by cur.
45 */
46int
47xfs_refcount_lookup_le(
48 struct xfs_btree_cur *cur,
49 enum xfs_refc_domain domain,
50 xfs_agblock_t bno,
51 int *stat)
52{
53 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
54 xfs_refcount_encode_startblock(bno, domain),
55 XFS_LOOKUP_LE);
56 cur->bc_rec.rc.rc_startblock = bno;
57 cur->bc_rec.rc.rc_blockcount = 0;
58 cur->bc_rec.rc.rc_domain = domain;
59 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
60}
61
62/*
63 * Look up the first record greater than or equal to [bno, len] in the btree
64 * given by cur.
65 */
66int
67xfs_refcount_lookup_ge(
68 struct xfs_btree_cur *cur,
69 enum xfs_refc_domain domain,
70 xfs_agblock_t bno,
71 int *stat)
72{
73 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
74 xfs_refcount_encode_startblock(bno, domain),
75 XFS_LOOKUP_GE);
76 cur->bc_rec.rc.rc_startblock = bno;
77 cur->bc_rec.rc.rc_blockcount = 0;
78 cur->bc_rec.rc.rc_domain = domain;
79 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
80}
81
82/*
83 * Look up the first record equal to [bno, len] in the btree
84 * given by cur.
85 */
86int
87xfs_refcount_lookup_eq(
88 struct xfs_btree_cur *cur,
89 enum xfs_refc_domain domain,
90 xfs_agblock_t bno,
91 int *stat)
92{
93 trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_ag.pag->pag_agno,
94 xfs_refcount_encode_startblock(bno, domain),
95 XFS_LOOKUP_LE);
96 cur->bc_rec.rc.rc_startblock = bno;
97 cur->bc_rec.rc.rc_blockcount = 0;
98 cur->bc_rec.rc.rc_domain = domain;
99 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
100}
101
102/* Convert on-disk record to in-core format. */
103void
104xfs_refcount_btrec_to_irec(
105 const union xfs_btree_rec *rec,
106 struct xfs_refcount_irec *irec)
107{
108 uint32_t start;
109
110 start = be32_to_cpu(rec->refc.rc_startblock);
111 if (start & XFS_REFC_COWFLAG) {
112 start &= ~XFS_REFC_COWFLAG;
113 irec->rc_domain = XFS_REFC_DOMAIN_COW;
114 } else {
115 irec->rc_domain = XFS_REFC_DOMAIN_SHARED;
116 }
117
118 irec->rc_startblock = start;
119 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
120 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
121}
122
123/*
124 * Get the data from the pointed-to record.
125 */
126int
127xfs_refcount_get_rec(
128 struct xfs_btree_cur *cur,
129 struct xfs_refcount_irec *irec,
130 int *stat)
131{
132 struct xfs_mount *mp = cur->bc_mp;
133 struct xfs_perag *pag = cur->bc_ag.pag;
134 union xfs_btree_rec *rec;
135 int error;
136
137 error = xfs_btree_get_rec(cur, &rec, stat);
138 if (error || !*stat)
139 return error;
140
141 xfs_refcount_btrec_to_irec(rec, irec);
142 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
143 goto out_bad_rec;
144
145 if (!xfs_refcount_check_domain(irec))
146 goto out_bad_rec;
147
148 /* check for valid extent range, including overflow */
149 if (!xfs_verify_agbext(pag, irec->rc_startblock, irec->rc_blockcount))
150 goto out_bad_rec;
151
152 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
153 goto out_bad_rec;
154
155 trace_xfs_refcount_get(cur->bc_mp, pag->pag_agno, irec);
156 return 0;
157
158out_bad_rec:
159 xfs_warn(mp,
160 "Refcount BTree record corruption in AG %d detected!",
161 pag->pag_agno);
162 xfs_warn(mp,
163 "Start block 0x%x, block count 0x%x, references 0x%x",
164 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
165 return -EFSCORRUPTED;
166}
167
168/*
169 * Update the record referred to by cur to the value given
170 * by [bno, len, refcount].
171 * This either works (return 0) or gets an EFSCORRUPTED error.
172 */
173STATIC int
174xfs_refcount_update(
175 struct xfs_btree_cur *cur,
176 struct xfs_refcount_irec *irec)
177{
178 union xfs_btree_rec rec;
179 uint32_t start;
180 int error;
181
182 trace_xfs_refcount_update(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
183
184 start = xfs_refcount_encode_startblock(irec->rc_startblock,
185 irec->rc_domain);
186 rec.refc.rc_startblock = cpu_to_be32(start);
187 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
188 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
189
190 error = xfs_btree_update(cur, &rec);
191 if (error)
192 trace_xfs_refcount_update_error(cur->bc_mp,
193 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
194 return error;
195}
196
197/*
198 * Insert the record referred to by cur to the value given
199 * by [bno, len, refcount].
200 * This either works (return 0) or gets an EFSCORRUPTED error.
201 */
202int
203xfs_refcount_insert(
204 struct xfs_btree_cur *cur,
205 struct xfs_refcount_irec *irec,
206 int *i)
207{
208 int error;
209
210 trace_xfs_refcount_insert(cur->bc_mp, cur->bc_ag.pag->pag_agno, irec);
211
212 cur->bc_rec.rc.rc_startblock = irec->rc_startblock;
213 cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount;
214 cur->bc_rec.rc.rc_refcount = irec->rc_refcount;
215 cur->bc_rec.rc.rc_domain = irec->rc_domain;
216
217 error = xfs_btree_insert(cur, i);
218 if (error)
219 goto out_error;
220 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
221 error = -EFSCORRUPTED;
222 goto out_error;
223 }
224
225out_error:
226 if (error)
227 trace_xfs_refcount_insert_error(cur->bc_mp,
228 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
229 return error;
230}
231
232/*
233 * Remove the record referred to by cur, then set the pointer to the spot
234 * where the record could be re-inserted, in case we want to increment or
235 * decrement the cursor.
236 * This either works (return 0) or gets an EFSCORRUPTED error.
237 */
238STATIC int
239xfs_refcount_delete(
240 struct xfs_btree_cur *cur,
241 int *i)
242{
243 struct xfs_refcount_irec irec;
244 int found_rec;
245 int error;
246
247 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
248 if (error)
249 goto out_error;
250 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
251 error = -EFSCORRUPTED;
252 goto out_error;
253 }
254 trace_xfs_refcount_delete(cur->bc_mp, cur->bc_ag.pag->pag_agno, &irec);
255 error = xfs_btree_delete(cur, i);
256 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
257 error = -EFSCORRUPTED;
258 goto out_error;
259 }
260 if (error)
261 goto out_error;
262 error = xfs_refcount_lookup_ge(cur, irec.rc_domain, irec.rc_startblock,
263 &found_rec);
264out_error:
265 if (error)
266 trace_xfs_refcount_delete_error(cur->bc_mp,
267 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
268 return error;
269}
270
271/*
272 * Adjusting the Reference Count
273 *
274 * As stated elsewhere, the reference count btree (refcbt) stores
275 * >1 reference counts for extents of physical blocks. In this
276 * operation, we're either raising or lowering the reference count of
277 * some subrange stored in the tree:
278 *
279 * <------ adjustment range ------>
280 * ----+ +---+-----+ +--+--------+---------
281 * 2 | | 3 | 4 | |17| 55 | 10
282 * ----+ +---+-----+ +--+--------+---------
283 * X axis is physical blocks number;
284 * reference counts are the numbers inside the rectangles
285 *
286 * The first thing we need to do is to ensure that there are no
287 * refcount extents crossing either boundary of the range to be
288 * adjusted. For any extent that does cross a boundary, split it into
289 * two extents so that we can increment the refcount of one of the
290 * pieces later:
291 *
292 * <------ adjustment range ------>
293 * ----+ +---+-----+ +--+--------+----+----
294 * 2 | | 3 | 2 | |17| 55 | 10 | 10
295 * ----+ +---+-----+ +--+--------+----+----
296 *
297 * For this next step, let's assume that all the physical blocks in
298 * the adjustment range are mapped to a file and are therefore in use
299 * at least once. Therefore, we can infer that any gap in the
300 * refcount tree within the adjustment range represents a physical
301 * extent with refcount == 1:
302 *
303 * <------ adjustment range ------>
304 * ----+---+---+-----+-+--+--------+----+----
305 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
306 * ----+---+---+-----+-+--+--------+----+----
307 * ^
308 *
309 * For each extent that falls within the interval range, figure out
310 * which extent is to the left or the right of that extent. Now we
311 * have a left, current, and right extent. If the new reference count
312 * of the center extent enables us to merge left, center, and right
313 * into one record covering all three, do so. If the center extent is
314 * at the left end of the range, abuts the left extent, and its new
315 * reference count matches the left extent's record, then merge them.
316 * If the center extent is at the right end of the range, abuts the
317 * right extent, and the reference counts match, merge those. In the
318 * example, we can left merge (assuming an increment operation):
319 *
320 * <------ adjustment range ------>
321 * --------+---+-----+-+--+--------+----+----
322 * 2 | 3 | 2 |1|17| 55 | 10 | 10
323 * --------+---+-----+-+--+--------+----+----
324 * ^
325 *
326 * For all other extents within the range, adjust the reference count
327 * or delete it if the refcount falls below 2. If we were
328 * incrementing, the end result looks like this:
329 *
330 * <------ adjustment range ------>
331 * --------+---+-----+-+--+--------+----+----
332 * 2 | 4 | 3 |2|18| 56 | 11 | 10
333 * --------+---+-----+-+--+--------+----+----
334 *
335 * The result of a decrement operation looks as such:
336 *
337 * <------ adjustment range ------>
338 * ----+ +---+ +--+--------+----+----
339 * 2 | | 2 | |16| 54 | 9 | 10
340 * ----+ +---+ +--+--------+----+----
341 * DDDD 111111DD
342 *
343 * The blocks marked "D" are freed; the blocks marked "1" are only
344 * referenced once and therefore the record is removed from the
345 * refcount btree.
346 */
347
348/* Next block after this extent. */
349static inline xfs_agblock_t
350xfs_refc_next(
351 struct xfs_refcount_irec *rc)
352{
353 return rc->rc_startblock + rc->rc_blockcount;
354}
355
356/*
357 * Split a refcount extent that crosses agbno.
358 */
359STATIC int
360xfs_refcount_split_extent(
361 struct xfs_btree_cur *cur,
362 enum xfs_refc_domain domain,
363 xfs_agblock_t agbno,
364 bool *shape_changed)
365{
366 struct xfs_refcount_irec rcext, tmp;
367 int found_rec;
368 int error;
369
370 *shape_changed = false;
371 error = xfs_refcount_lookup_le(cur, domain, agbno, &found_rec);
372 if (error)
373 goto out_error;
374 if (!found_rec)
375 return 0;
376
377 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
378 if (error)
379 goto out_error;
380 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
381 error = -EFSCORRUPTED;
382 goto out_error;
383 }
384 if (rcext.rc_domain != domain)
385 return 0;
386 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
387 return 0;
388
389 *shape_changed = true;
390 trace_xfs_refcount_split_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
391 &rcext, agbno);
392
393 /* Establish the right extent. */
394 tmp = rcext;
395 tmp.rc_startblock = agbno;
396 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
397 error = xfs_refcount_update(cur, &tmp);
398 if (error)
399 goto out_error;
400
401 /* Insert the left extent. */
402 tmp = rcext;
403 tmp.rc_blockcount = agbno - rcext.rc_startblock;
404 error = xfs_refcount_insert(cur, &tmp, &found_rec);
405 if (error)
406 goto out_error;
407 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
408 error = -EFSCORRUPTED;
409 goto out_error;
410 }
411 return error;
412
413out_error:
414 trace_xfs_refcount_split_extent_error(cur->bc_mp,
415 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
416 return error;
417}
418
419/*
420 * Merge the left, center, and right extents.
421 */
422STATIC int
423xfs_refcount_merge_center_extents(
424 struct xfs_btree_cur *cur,
425 struct xfs_refcount_irec *left,
426 struct xfs_refcount_irec *center,
427 struct xfs_refcount_irec *right,
428 unsigned long long extlen,
429 xfs_extlen_t *aglen)
430{
431 int error;
432 int found_rec;
433
434 trace_xfs_refcount_merge_center_extents(cur->bc_mp,
435 cur->bc_ag.pag->pag_agno, left, center, right);
436
437 ASSERT(left->rc_domain == center->rc_domain);
438 ASSERT(right->rc_domain == center->rc_domain);
439
440 /*
441 * Make sure the center and right extents are not in the btree.
442 * If the center extent was synthesized, the first delete call
443 * removes the right extent and we skip the second deletion.
444 * If center and right were in the btree, then the first delete
445 * call removes the center and the second one removes the right
446 * extent.
447 */
448 error = xfs_refcount_lookup_ge(cur, center->rc_domain,
449 center->rc_startblock, &found_rec);
450 if (error)
451 goto out_error;
452 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
453 error = -EFSCORRUPTED;
454 goto out_error;
455 }
456
457 error = xfs_refcount_delete(cur, &found_rec);
458 if (error)
459 goto out_error;
460 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
461 error = -EFSCORRUPTED;
462 goto out_error;
463 }
464
465 if (center->rc_refcount > 1) {
466 error = xfs_refcount_delete(cur, &found_rec);
467 if (error)
468 goto out_error;
469 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
470 error = -EFSCORRUPTED;
471 goto out_error;
472 }
473 }
474
475 /* Enlarge the left extent. */
476 error = xfs_refcount_lookup_le(cur, left->rc_domain,
477 left->rc_startblock, &found_rec);
478 if (error)
479 goto out_error;
480 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
481 error = -EFSCORRUPTED;
482 goto out_error;
483 }
484
485 left->rc_blockcount = extlen;
486 error = xfs_refcount_update(cur, left);
487 if (error)
488 goto out_error;
489
490 *aglen = 0;
491 return error;
492
493out_error:
494 trace_xfs_refcount_merge_center_extents_error(cur->bc_mp,
495 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
496 return error;
497}
498
499/*
500 * Merge with the left extent.
501 */
502STATIC int
503xfs_refcount_merge_left_extent(
504 struct xfs_btree_cur *cur,
505 struct xfs_refcount_irec *left,
506 struct xfs_refcount_irec *cleft,
507 xfs_agblock_t *agbno,
508 xfs_extlen_t *aglen)
509{
510 int error;
511 int found_rec;
512
513 trace_xfs_refcount_merge_left_extent(cur->bc_mp,
514 cur->bc_ag.pag->pag_agno, left, cleft);
515
516 ASSERT(left->rc_domain == cleft->rc_domain);
517
518 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
519 if (cleft->rc_refcount > 1) {
520 error = xfs_refcount_lookup_le(cur, cleft->rc_domain,
521 cleft->rc_startblock, &found_rec);
522 if (error)
523 goto out_error;
524 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
525 error = -EFSCORRUPTED;
526 goto out_error;
527 }
528
529 error = xfs_refcount_delete(cur, &found_rec);
530 if (error)
531 goto out_error;
532 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
533 error = -EFSCORRUPTED;
534 goto out_error;
535 }
536 }
537
538 /* Enlarge the left extent. */
539 error = xfs_refcount_lookup_le(cur, left->rc_domain,
540 left->rc_startblock, &found_rec);
541 if (error)
542 goto out_error;
543 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
544 error = -EFSCORRUPTED;
545 goto out_error;
546 }
547
548 left->rc_blockcount += cleft->rc_blockcount;
549 error = xfs_refcount_update(cur, left);
550 if (error)
551 goto out_error;
552
553 *agbno += cleft->rc_blockcount;
554 *aglen -= cleft->rc_blockcount;
555 return error;
556
557out_error:
558 trace_xfs_refcount_merge_left_extent_error(cur->bc_mp,
559 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
560 return error;
561}
562
563/*
564 * Merge with the right extent.
565 */
566STATIC int
567xfs_refcount_merge_right_extent(
568 struct xfs_btree_cur *cur,
569 struct xfs_refcount_irec *right,
570 struct xfs_refcount_irec *cright,
571 xfs_extlen_t *aglen)
572{
573 int error;
574 int found_rec;
575
576 trace_xfs_refcount_merge_right_extent(cur->bc_mp,
577 cur->bc_ag.pag->pag_agno, cright, right);
578
579 ASSERT(right->rc_domain == cright->rc_domain);
580
581 /*
582 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
583 * remove it.
584 */
585 if (cright->rc_refcount > 1) {
586 error = xfs_refcount_lookup_le(cur, cright->rc_domain,
587 cright->rc_startblock, &found_rec);
588 if (error)
589 goto out_error;
590 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
591 error = -EFSCORRUPTED;
592 goto out_error;
593 }
594
595 error = xfs_refcount_delete(cur, &found_rec);
596 if (error)
597 goto out_error;
598 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
599 error = -EFSCORRUPTED;
600 goto out_error;
601 }
602 }
603
604 /* Enlarge the right extent. */
605 error = xfs_refcount_lookup_le(cur, right->rc_domain,
606 right->rc_startblock, &found_rec);
607 if (error)
608 goto out_error;
609 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
610 error = -EFSCORRUPTED;
611 goto out_error;
612 }
613
614 right->rc_startblock -= cright->rc_blockcount;
615 right->rc_blockcount += cright->rc_blockcount;
616 error = xfs_refcount_update(cur, right);
617 if (error)
618 goto out_error;
619
620 *aglen -= cright->rc_blockcount;
621 return error;
622
623out_error:
624 trace_xfs_refcount_merge_right_extent_error(cur->bc_mp,
625 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
626 return error;
627}
628
629/*
630 * Find the left extent and the one after it (cleft). This function assumes
631 * that we've already split any extent crossing agbno.
632 */
633STATIC int
634xfs_refcount_find_left_extents(
635 struct xfs_btree_cur *cur,
636 struct xfs_refcount_irec *left,
637 struct xfs_refcount_irec *cleft,
638 enum xfs_refc_domain domain,
639 xfs_agblock_t agbno,
640 xfs_extlen_t aglen)
641{
642 struct xfs_refcount_irec tmp;
643 int error;
644 int found_rec;
645
646 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
647 error = xfs_refcount_lookup_le(cur, domain, agbno - 1, &found_rec);
648 if (error)
649 goto out_error;
650 if (!found_rec)
651 return 0;
652
653 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
654 if (error)
655 goto out_error;
656 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
657 error = -EFSCORRUPTED;
658 goto out_error;
659 }
660
661 if (tmp.rc_domain != domain)
662 return 0;
663 if (xfs_refc_next(&tmp) != agbno)
664 return 0;
665 /* We have a left extent; retrieve (or invent) the next right one */
666 *left = tmp;
667
668 error = xfs_btree_increment(cur, 0, &found_rec);
669 if (error)
670 goto out_error;
671 if (found_rec) {
672 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
673 if (error)
674 goto out_error;
675 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
676 error = -EFSCORRUPTED;
677 goto out_error;
678 }
679
680 if (tmp.rc_domain != domain)
681 goto not_found;
682
683 /* if tmp starts at the end of our range, just use that */
684 if (tmp.rc_startblock == agbno)
685 *cleft = tmp;
686 else {
687 /*
688 * There's a gap in the refcntbt at the start of the
689 * range we're interested in (refcount == 1) so
690 * synthesize the implied extent and pass it back.
691 * We assume here that the agbno/aglen range was
692 * passed in from a data fork extent mapping and
693 * therefore is allocated to exactly one owner.
694 */
695 cleft->rc_startblock = agbno;
696 cleft->rc_blockcount = min(aglen,
697 tmp.rc_startblock - agbno);
698 cleft->rc_refcount = 1;
699 cleft->rc_domain = domain;
700 }
701 } else {
702not_found:
703 /*
704 * No extents, so pretend that there's one covering the whole
705 * range.
706 */
707 cleft->rc_startblock = agbno;
708 cleft->rc_blockcount = aglen;
709 cleft->rc_refcount = 1;
710 cleft->rc_domain = domain;
711 }
712 trace_xfs_refcount_find_left_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
713 left, cleft, agbno);
714 return error;
715
716out_error:
717 trace_xfs_refcount_find_left_extent_error(cur->bc_mp,
718 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
719 return error;
720}
721
722/*
723 * Find the right extent and the one before it (cright). This function
724 * assumes that we've already split any extents crossing agbno + aglen.
725 */
726STATIC int
727xfs_refcount_find_right_extents(
728 struct xfs_btree_cur *cur,
729 struct xfs_refcount_irec *right,
730 struct xfs_refcount_irec *cright,
731 enum xfs_refc_domain domain,
732 xfs_agblock_t agbno,
733 xfs_extlen_t aglen)
734{
735 struct xfs_refcount_irec tmp;
736 int error;
737 int found_rec;
738
739 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
740 error = xfs_refcount_lookup_ge(cur, domain, agbno + aglen, &found_rec);
741 if (error)
742 goto out_error;
743 if (!found_rec)
744 return 0;
745
746 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
747 if (error)
748 goto out_error;
749 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
750 error = -EFSCORRUPTED;
751 goto out_error;
752 }
753
754 if (tmp.rc_domain != domain)
755 return 0;
756 if (tmp.rc_startblock != agbno + aglen)
757 return 0;
758 /* We have a right extent; retrieve (or invent) the next left one */
759 *right = tmp;
760
761 error = xfs_btree_decrement(cur, 0, &found_rec);
762 if (error)
763 goto out_error;
764 if (found_rec) {
765 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
766 if (error)
767 goto out_error;
768 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
769 error = -EFSCORRUPTED;
770 goto out_error;
771 }
772
773 if (tmp.rc_domain != domain)
774 goto not_found;
775
776 /* if tmp ends at the end of our range, just use that */
777 if (xfs_refc_next(&tmp) == agbno + aglen)
778 *cright = tmp;
779 else {
780 /*
781 * There's a gap in the refcntbt at the end of the
782 * range we're interested in (refcount == 1) so
783 * create the implied extent and pass it back.
784 * We assume here that the agbno/aglen range was
785 * passed in from a data fork extent mapping and
786 * therefore is allocated to exactly one owner.
787 */
788 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
789 cright->rc_blockcount = right->rc_startblock -
790 cright->rc_startblock;
791 cright->rc_refcount = 1;
792 cright->rc_domain = domain;
793 }
794 } else {
795not_found:
796 /*
797 * No extents, so pretend that there's one covering the whole
798 * range.
799 */
800 cright->rc_startblock = agbno;
801 cright->rc_blockcount = aglen;
802 cright->rc_refcount = 1;
803 cright->rc_domain = domain;
804 }
805 trace_xfs_refcount_find_right_extent(cur->bc_mp, cur->bc_ag.pag->pag_agno,
806 cright, right, agbno + aglen);
807 return error;
808
809out_error:
810 trace_xfs_refcount_find_right_extent_error(cur->bc_mp,
811 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
812 return error;
813}
814
815/* Is this extent valid? */
816static inline bool
817xfs_refc_valid(
818 const struct xfs_refcount_irec *rc)
819{
820 return rc->rc_startblock != NULLAGBLOCK;
821}
822
823static inline xfs_nlink_t
824xfs_refc_merge_refcount(
825 const struct xfs_refcount_irec *irec,
826 enum xfs_refc_adjust_op adjust)
827{
828 /* Once a record hits MAXREFCOUNT, it is pinned there forever */
829 if (irec->rc_refcount == MAXREFCOUNT)
830 return MAXREFCOUNT;
831 return irec->rc_refcount + adjust;
832}
833
834static inline bool
835xfs_refc_want_merge_center(
836 const struct xfs_refcount_irec *left,
837 const struct xfs_refcount_irec *cleft,
838 const struct xfs_refcount_irec *cright,
839 const struct xfs_refcount_irec *right,
840 bool cleft_is_cright,
841 enum xfs_refc_adjust_op adjust,
842 unsigned long long *ulenp)
843{
844 unsigned long long ulen = left->rc_blockcount;
845 xfs_nlink_t new_refcount;
846
847 /*
848 * To merge with a center record, both shoulder records must be
849 * adjacent to the record we want to adjust. This is only true if
850 * find_left and find_right made all four records valid.
851 */
852 if (!xfs_refc_valid(left) || !xfs_refc_valid(right) ||
853 !xfs_refc_valid(cleft) || !xfs_refc_valid(cright))
854 return false;
855
856 /* There must only be one record for the entire range. */
857 if (!cleft_is_cright)
858 return false;
859
860 /* The shoulder record refcounts must match the new refcount. */
861 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
862 if (left->rc_refcount != new_refcount)
863 return false;
864 if (right->rc_refcount != new_refcount)
865 return false;
866
867 /*
868 * The new record cannot exceed the max length. ulen is a ULL as the
869 * individual record block counts can be up to (u32 - 1) in length
870 * hence we need to catch u32 addition overflows here.
871 */
872 ulen += cleft->rc_blockcount + right->rc_blockcount;
873 if (ulen >= MAXREFCEXTLEN)
874 return false;
875
876 *ulenp = ulen;
877 return true;
878}
879
880static inline bool
881xfs_refc_want_merge_left(
882 const struct xfs_refcount_irec *left,
883 const struct xfs_refcount_irec *cleft,
884 enum xfs_refc_adjust_op adjust)
885{
886 unsigned long long ulen = left->rc_blockcount;
887 xfs_nlink_t new_refcount;
888
889 /*
890 * For a left merge, the left shoulder record must be adjacent to the
891 * start of the range. If this is true, find_left made left and cleft
892 * contain valid contents.
893 */
894 if (!xfs_refc_valid(left) || !xfs_refc_valid(cleft))
895 return false;
896
897 /* Left shoulder record refcount must match the new refcount. */
898 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
899 if (left->rc_refcount != new_refcount)
900 return false;
901
902 /*
903 * The new record cannot exceed the max length. ulen is a ULL as the
904 * individual record block counts can be up to (u32 - 1) in length
905 * hence we need to catch u32 addition overflows here.
906 */
907 ulen += cleft->rc_blockcount;
908 if (ulen >= MAXREFCEXTLEN)
909 return false;
910
911 return true;
912}
913
914static inline bool
915xfs_refc_want_merge_right(
916 const struct xfs_refcount_irec *cright,
917 const struct xfs_refcount_irec *right,
918 enum xfs_refc_adjust_op adjust)
919{
920 unsigned long long ulen = right->rc_blockcount;
921 xfs_nlink_t new_refcount;
922
923 /*
924 * For a right merge, the right shoulder record must be adjacent to the
925 * end of the range. If this is true, find_right made cright and right
926 * contain valid contents.
927 */
928 if (!xfs_refc_valid(right) || !xfs_refc_valid(cright))
929 return false;
930
931 /* Right shoulder record refcount must match the new refcount. */
932 new_refcount = xfs_refc_merge_refcount(cright, adjust);
933 if (right->rc_refcount != new_refcount)
934 return false;
935
936 /*
937 * The new record cannot exceed the max length. ulen is a ULL as the
938 * individual record block counts can be up to (u32 - 1) in length
939 * hence we need to catch u32 addition overflows here.
940 */
941 ulen += cright->rc_blockcount;
942 if (ulen >= MAXREFCEXTLEN)
943 return false;
944
945 return true;
946}
947
948/*
949 * Try to merge with any extents on the boundaries of the adjustment range.
950 */
951STATIC int
952xfs_refcount_merge_extents(
953 struct xfs_btree_cur *cur,
954 enum xfs_refc_domain domain,
955 xfs_agblock_t *agbno,
956 xfs_extlen_t *aglen,
957 enum xfs_refc_adjust_op adjust,
958 bool *shape_changed)
959{
960 struct xfs_refcount_irec left = {0}, cleft = {0};
961 struct xfs_refcount_irec cright = {0}, right = {0};
962 int error;
963 unsigned long long ulen;
964 bool cequal;
965
966 *shape_changed = false;
967 /*
968 * Find the extent just below agbno [left], just above agbno [cleft],
969 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
970 * [right].
971 */
972 error = xfs_refcount_find_left_extents(cur, &left, &cleft, domain,
973 *agbno, *aglen);
974 if (error)
975 return error;
976 error = xfs_refcount_find_right_extents(cur, &right, &cright, domain,
977 *agbno, *aglen);
978 if (error)
979 return error;
980
981 /* No left or right extent to merge; exit. */
982 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
983 return 0;
984
985 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
986 (cleft.rc_blockcount == cright.rc_blockcount);
987
988 /* Try to merge left, cleft, and right. cleft must == cright. */
989 if (xfs_refc_want_merge_center(&left, &cleft, &cright, &right, cequal,
990 adjust, &ulen)) {
991 *shape_changed = true;
992 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
993 &right, ulen, aglen);
994 }
995
996 /* Try to merge left and cleft. */
997 if (xfs_refc_want_merge_left(&left, &cleft, adjust)) {
998 *shape_changed = true;
999 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
1000 agbno, aglen);
1001 if (error)
1002 return error;
1003
1004 /*
1005 * If we just merged left + cleft and cleft == cright,
1006 * we no longer have a cright to merge with right. We're done.
1007 */
1008 if (cequal)
1009 return 0;
1010 }
1011
1012 /* Try to merge cright and right. */
1013 if (xfs_refc_want_merge_right(&cright, &right, adjust)) {
1014 *shape_changed = true;
1015 return xfs_refcount_merge_right_extent(cur, &right, &cright,
1016 aglen);
1017 }
1018
1019 return 0;
1020}
1021
1022/*
1023 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
1024 * true incorrectly is a shutdown FS; the penalty for guessing false
1025 * incorrectly is more transaction rolls than might be necessary.
1026 * Be conservative here.
1027 */
1028static bool
1029xfs_refcount_still_have_space(
1030 struct xfs_btree_cur *cur)
1031{
1032 unsigned long overhead;
1033
1034 /*
1035 * Worst case estimate: full splits of the free space and rmap btrees
1036 * to handle each of the shape changes to the refcount btree.
1037 */
1038 overhead = xfs_allocfree_block_count(cur->bc_mp,
1039 cur->bc_ag.refc.shape_changes);
1040 overhead += cur->bc_mp->m_refc_maxlevels;
1041 overhead *= cur->bc_mp->m_sb.sb_blocksize;
1042
1043 /*
1044 * Only allow 2 refcount extent updates per transaction if the
1045 * refcount continue update "error" has been injected.
1046 */
1047 if (cur->bc_ag.refc.nr_ops > 2 &&
1048 XFS_TEST_ERROR(false, cur->bc_mp,
1049 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
1050 return false;
1051
1052 if (cur->bc_ag.refc.nr_ops == 0)
1053 return true;
1054 else if (overhead > cur->bc_tp->t_log_res)
1055 return false;
1056 return cur->bc_tp->t_log_res - overhead >
1057 cur->bc_ag.refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
1058}
1059
1060/*
1061 * Adjust the refcounts of middle extents. At this point we should have
1062 * split extents that crossed the adjustment range; merged with adjacent
1063 * extents; and updated agbno/aglen to reflect the merges. Therefore,
1064 * all we have to do is update the extents inside [agbno, agbno + aglen].
1065 */
1066STATIC int
1067xfs_refcount_adjust_extents(
1068 struct xfs_btree_cur *cur,
1069 xfs_agblock_t *agbno,
1070 xfs_extlen_t *aglen,
1071 enum xfs_refc_adjust_op adj)
1072{
1073 struct xfs_refcount_irec ext, tmp;
1074 int error;
1075 int found_rec, found_tmp;
1076 xfs_fsblock_t fsbno;
1077
1078 /* Merging did all the work already. */
1079 if (*aglen == 0)
1080 return 0;
1081
1082 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_SHARED, *agbno,
1083 &found_rec);
1084 if (error)
1085 goto out_error;
1086
1087 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
1088 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1089 if (error)
1090 goto out_error;
1091 if (!found_rec || ext.rc_domain != XFS_REFC_DOMAIN_SHARED) {
1092 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1093 ext.rc_blockcount = 0;
1094 ext.rc_refcount = 0;
1095 ext.rc_domain = XFS_REFC_DOMAIN_SHARED;
1096 }
1097
1098 /*
1099 * Deal with a hole in the refcount tree; if a file maps to
1100 * these blocks and there's no refcountbt record, pretend that
1101 * there is one with refcount == 1.
1102 */
1103 if (ext.rc_startblock != *agbno) {
1104 tmp.rc_startblock = *agbno;
1105 tmp.rc_blockcount = min(*aglen,
1106 ext.rc_startblock - *agbno);
1107 tmp.rc_refcount = 1 + adj;
1108 tmp.rc_domain = XFS_REFC_DOMAIN_SHARED;
1109
1110 trace_xfs_refcount_modify_extent(cur->bc_mp,
1111 cur->bc_ag.pag->pag_agno, &tmp);
1112
1113 /*
1114 * Either cover the hole (increment) or
1115 * delete the range (decrement).
1116 */
1117 cur->bc_ag.refc.nr_ops++;
1118 if (tmp.rc_refcount) {
1119 error = xfs_refcount_insert(cur, &tmp,
1120 &found_tmp);
1121 if (error)
1122 goto out_error;
1123 if (XFS_IS_CORRUPT(cur->bc_mp,
1124 found_tmp != 1)) {
1125 error = -EFSCORRUPTED;
1126 goto out_error;
1127 }
1128 } else {
1129 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
1130 cur->bc_ag.pag->pag_agno,
1131 tmp.rc_startblock);
1132 xfs_free_extent_later(cur->bc_tp, fsbno,
1133 tmp.rc_blockcount, NULL);
1134 }
1135
1136 (*agbno) += tmp.rc_blockcount;
1137 (*aglen) -= tmp.rc_blockcount;
1138
1139 /* Stop if there's nothing left to modify */
1140 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
1141 break;
1142
1143 /* Move the cursor to the start of ext. */
1144 error = xfs_refcount_lookup_ge(cur,
1145 XFS_REFC_DOMAIN_SHARED, *agbno,
1146 &found_rec);
1147 if (error)
1148 goto out_error;
1149 }
1150
1151 /*
1152 * A previous step trimmed agbno/aglen such that the end of the
1153 * range would not be in the middle of the record. If this is
1154 * no longer the case, something is seriously wrong with the
1155 * btree. Make sure we never feed the synthesized record into
1156 * the processing loop below.
1157 */
1158 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount == 0) ||
1159 XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount > *aglen)) {
1160 error = -EFSCORRUPTED;
1161 goto out_error;
1162 }
1163
1164 /*
1165 * Adjust the reference count and either update the tree
1166 * (incr) or free the blocks (decr).
1167 */
1168 if (ext.rc_refcount == MAXREFCOUNT)
1169 goto skip;
1170 ext.rc_refcount += adj;
1171 trace_xfs_refcount_modify_extent(cur->bc_mp,
1172 cur->bc_ag.pag->pag_agno, &ext);
1173 cur->bc_ag.refc.nr_ops++;
1174 if (ext.rc_refcount > 1) {
1175 error = xfs_refcount_update(cur, &ext);
1176 if (error)
1177 goto out_error;
1178 } else if (ext.rc_refcount == 1) {
1179 error = xfs_refcount_delete(cur, &found_rec);
1180 if (error)
1181 goto out_error;
1182 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1183 error = -EFSCORRUPTED;
1184 goto out_error;
1185 }
1186 goto advloop;
1187 } else {
1188 fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
1189 cur->bc_ag.pag->pag_agno,
1190 ext.rc_startblock);
1191 xfs_free_extent_later(cur->bc_tp, fsbno,
1192 ext.rc_blockcount, NULL);
1193 }
1194
1195skip:
1196 error = xfs_btree_increment(cur, 0, &found_rec);
1197 if (error)
1198 goto out_error;
1199
1200advloop:
1201 (*agbno) += ext.rc_blockcount;
1202 (*aglen) -= ext.rc_blockcount;
1203 }
1204
1205 return error;
1206out_error:
1207 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1208 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1209 return error;
1210}
1211
1212/* Adjust the reference count of a range of AG blocks. */
1213STATIC int
1214xfs_refcount_adjust(
1215 struct xfs_btree_cur *cur,
1216 xfs_agblock_t agbno,
1217 xfs_extlen_t aglen,
1218 xfs_agblock_t *new_agbno,
1219 xfs_extlen_t *new_aglen,
1220 enum xfs_refc_adjust_op adj)
1221{
1222 bool shape_changed;
1223 int shape_changes = 0;
1224 int error;
1225
1226 *new_agbno = agbno;
1227 *new_aglen = aglen;
1228 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1229 trace_xfs_refcount_increase(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1230 agbno, aglen);
1231 else
1232 trace_xfs_refcount_decrease(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1233 agbno, aglen);
1234
1235 /*
1236 * Ensure that no rcextents cross the boundary of the adjustment range.
1237 */
1238 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1239 agbno, &shape_changed);
1240 if (error)
1241 goto out_error;
1242 if (shape_changed)
1243 shape_changes++;
1244
1245 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1246 agbno + aglen, &shape_changed);
1247 if (error)
1248 goto out_error;
1249 if (shape_changed)
1250 shape_changes++;
1251
1252 /*
1253 * Try to merge with the left or right extents of the range.
1254 */
1255 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_SHARED,
1256 new_agbno, new_aglen, adj, &shape_changed);
1257 if (error)
1258 goto out_error;
1259 if (shape_changed)
1260 shape_changes++;
1261 if (shape_changes)
1262 cur->bc_ag.refc.shape_changes++;
1263
1264 /* Now that we've taken care of the ends, adjust the middle extents */
1265 error = xfs_refcount_adjust_extents(cur, new_agbno, new_aglen, adj);
1266 if (error)
1267 goto out_error;
1268
1269 return 0;
1270
1271out_error:
1272 trace_xfs_refcount_adjust_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1273 error, _RET_IP_);
1274 return error;
1275}
1276
1277/* Clean up after calling xfs_refcount_finish_one. */
1278void
1279xfs_refcount_finish_one_cleanup(
1280 struct xfs_trans *tp,
1281 struct xfs_btree_cur *rcur,
1282 int error)
1283{
1284 struct xfs_buf *agbp;
1285
1286 if (rcur == NULL)
1287 return;
1288 agbp = rcur->bc_ag.agbp;
1289 xfs_btree_del_cursor(rcur, error);
1290 if (error)
1291 xfs_trans_brelse(tp, agbp);
1292}
1293
1294/*
1295 * Set up a continuation a deferred refcount operation by updating the intent.
1296 * Checks to make sure we're not going to run off the end of the AG.
1297 */
1298static inline int
1299xfs_refcount_continue_op(
1300 struct xfs_btree_cur *cur,
1301 xfs_fsblock_t startblock,
1302 xfs_agblock_t new_agbno,
1303 xfs_extlen_t new_len,
1304 xfs_fsblock_t *new_fsbno)
1305{
1306 struct xfs_mount *mp = cur->bc_mp;
1307 struct xfs_perag *pag = cur->bc_ag.pag;
1308
1309 if (XFS_IS_CORRUPT(mp, !xfs_verify_agbext(pag, new_agbno, new_len)))
1310 return -EFSCORRUPTED;
1311
1312 *new_fsbno = XFS_AGB_TO_FSB(mp, pag->pag_agno, new_agbno);
1313
1314 ASSERT(xfs_verify_fsbext(mp, *new_fsbno, new_len));
1315 ASSERT(pag->pag_agno == XFS_FSB_TO_AGNO(mp, *new_fsbno));
1316
1317 return 0;
1318}
1319
1320/*
1321 * Process one of the deferred refcount operations. We pass back the
1322 * btree cursor to maintain our lock on the btree between calls.
1323 * This saves time and eliminates a buffer deadlock between the
1324 * superblock and the AGF because we'll always grab them in the same
1325 * order.
1326 */
1327int
1328xfs_refcount_finish_one(
1329 struct xfs_trans *tp,
1330 enum xfs_refcount_intent_type type,
1331 xfs_fsblock_t startblock,
1332 xfs_extlen_t blockcount,
1333 xfs_fsblock_t *new_fsb,
1334 xfs_extlen_t *new_len,
1335 struct xfs_btree_cur **pcur)
1336{
1337 struct xfs_mount *mp = tp->t_mountp;
1338 struct xfs_btree_cur *rcur;
1339 struct xfs_buf *agbp = NULL;
1340 int error = 0;
1341 xfs_agblock_t bno;
1342 xfs_agblock_t new_agbno;
1343 unsigned long nr_ops = 0;
1344 int shape_changes = 0;
1345 struct xfs_perag *pag;
1346
1347 pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, startblock));
1348 bno = XFS_FSB_TO_AGBNO(mp, startblock);
1349
1350 trace_xfs_refcount_deferred(mp, XFS_FSB_TO_AGNO(mp, startblock),
1351 type, XFS_FSB_TO_AGBNO(mp, startblock),
1352 blockcount);
1353
1354 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REFCOUNT_FINISH_ONE)) {
1355 error = -EIO;
1356 goto out_drop;
1357 }
1358
1359 /*
1360 * If we haven't gotten a cursor or the cursor AG doesn't match
1361 * the startblock, get one now.
1362 */
1363 rcur = *pcur;
1364 if (rcur != NULL && rcur->bc_ag.pag != pag) {
1365 nr_ops = rcur->bc_ag.refc.nr_ops;
1366 shape_changes = rcur->bc_ag.refc.shape_changes;
1367 xfs_refcount_finish_one_cleanup(tp, rcur, 0);
1368 rcur = NULL;
1369 *pcur = NULL;
1370 }
1371 if (rcur == NULL) {
1372 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_FREEING,
1373 &agbp);
1374 if (error)
1375 goto out_drop;
1376
1377 rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1378 rcur->bc_ag.refc.nr_ops = nr_ops;
1379 rcur->bc_ag.refc.shape_changes = shape_changes;
1380 }
1381 *pcur = rcur;
1382
1383 switch (type) {
1384 case XFS_REFCOUNT_INCREASE:
1385 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1386 new_len, XFS_REFCOUNT_ADJUST_INCREASE);
1387 if (error)
1388 goto out_drop;
1389 if (*new_len > 0)
1390 error = xfs_refcount_continue_op(rcur, startblock,
1391 new_agbno, *new_len, new_fsb);
1392 break;
1393 case XFS_REFCOUNT_DECREASE:
1394 error = xfs_refcount_adjust(rcur, bno, blockcount, &new_agbno,
1395 new_len, XFS_REFCOUNT_ADJUST_DECREASE);
1396 if (error)
1397 goto out_drop;
1398 if (*new_len > 0)
1399 error = xfs_refcount_continue_op(rcur, startblock,
1400 new_agbno, *new_len, new_fsb);
1401 break;
1402 case XFS_REFCOUNT_ALLOC_COW:
1403 *new_fsb = startblock + blockcount;
1404 *new_len = 0;
1405 error = __xfs_refcount_cow_alloc(rcur, bno, blockcount);
1406 break;
1407 case XFS_REFCOUNT_FREE_COW:
1408 *new_fsb = startblock + blockcount;
1409 *new_len = 0;
1410 error = __xfs_refcount_cow_free(rcur, bno, blockcount);
1411 break;
1412 default:
1413 ASSERT(0);
1414 error = -EFSCORRUPTED;
1415 }
1416 if (!error && *new_len > 0)
1417 trace_xfs_refcount_finish_one_leftover(mp, pag->pag_agno, type,
1418 bno, blockcount, new_agbno, *new_len);
1419out_drop:
1420 xfs_perag_put(pag);
1421 return error;
1422}
1423
1424/*
1425 * Record a refcount intent for later processing.
1426 */
1427static void
1428__xfs_refcount_add(
1429 struct xfs_trans *tp,
1430 enum xfs_refcount_intent_type type,
1431 xfs_fsblock_t startblock,
1432 xfs_extlen_t blockcount)
1433{
1434 struct xfs_refcount_intent *ri;
1435
1436 trace_xfs_refcount_defer(tp->t_mountp,
1437 XFS_FSB_TO_AGNO(tp->t_mountp, startblock),
1438 type, XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
1439 blockcount);
1440
1441 ri = kmem_cache_alloc(xfs_refcount_intent_cache,
1442 GFP_NOFS | __GFP_NOFAIL);
1443 INIT_LIST_HEAD(&ri->ri_list);
1444 ri->ri_type = type;
1445 ri->ri_startblock = startblock;
1446 ri->ri_blockcount = blockcount;
1447
1448 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_REFCOUNT, &ri->ri_list);
1449}
1450
1451/*
1452 * Increase the reference count of the blocks backing a file's extent.
1453 */
1454void
1455xfs_refcount_increase_extent(
1456 struct xfs_trans *tp,
1457 struct xfs_bmbt_irec *PREV)
1458{
1459 if (!xfs_has_reflink(tp->t_mountp))
1460 return;
1461
1462 __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, PREV->br_startblock,
1463 PREV->br_blockcount);
1464}
1465
1466/*
1467 * Decrease the reference count of the blocks backing a file's extent.
1468 */
1469void
1470xfs_refcount_decrease_extent(
1471 struct xfs_trans *tp,
1472 struct xfs_bmbt_irec *PREV)
1473{
1474 if (!xfs_has_reflink(tp->t_mountp))
1475 return;
1476
1477 __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, PREV->br_startblock,
1478 PREV->br_blockcount);
1479}
1480
1481/*
1482 * Given an AG extent, find the lowest-numbered run of shared blocks
1483 * within that range and return the range in fbno/flen. If
1484 * find_end_of_shared is set, return the longest contiguous extent of
1485 * shared blocks; if not, just return the first extent we find. If no
1486 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1487 * and 0, respectively.
1488 */
1489int
1490xfs_refcount_find_shared(
1491 struct xfs_btree_cur *cur,
1492 xfs_agblock_t agbno,
1493 xfs_extlen_t aglen,
1494 xfs_agblock_t *fbno,
1495 xfs_extlen_t *flen,
1496 bool find_end_of_shared)
1497{
1498 struct xfs_refcount_irec tmp;
1499 int i;
1500 int have;
1501 int error;
1502
1503 trace_xfs_refcount_find_shared(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1504 agbno, aglen);
1505
1506 /* By default, skip the whole range */
1507 *fbno = NULLAGBLOCK;
1508 *flen = 0;
1509
1510 /* Try to find a refcount extent that crosses the start */
1511 error = xfs_refcount_lookup_le(cur, XFS_REFC_DOMAIN_SHARED, agbno,
1512 &have);
1513 if (error)
1514 goto out_error;
1515 if (!have) {
1516 /* No left extent, look at the next one */
1517 error = xfs_btree_increment(cur, 0, &have);
1518 if (error)
1519 goto out_error;
1520 if (!have)
1521 goto done;
1522 }
1523 error = xfs_refcount_get_rec(cur, &tmp, &i);
1524 if (error)
1525 goto out_error;
1526 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1527 error = -EFSCORRUPTED;
1528 goto out_error;
1529 }
1530 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1531 goto done;
1532
1533 /* If the extent ends before the start, look at the next one */
1534 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1535 error = xfs_btree_increment(cur, 0, &have);
1536 if (error)
1537 goto out_error;
1538 if (!have)
1539 goto done;
1540 error = xfs_refcount_get_rec(cur, &tmp, &i);
1541 if (error)
1542 goto out_error;
1543 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1544 error = -EFSCORRUPTED;
1545 goto out_error;
1546 }
1547 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1548 goto done;
1549 }
1550
1551 /* If the extent starts after the range we want, bail out */
1552 if (tmp.rc_startblock >= agbno + aglen)
1553 goto done;
1554
1555 /* We found the start of a shared extent! */
1556 if (tmp.rc_startblock < agbno) {
1557 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1558 tmp.rc_startblock = agbno;
1559 }
1560
1561 *fbno = tmp.rc_startblock;
1562 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1563 if (!find_end_of_shared)
1564 goto done;
1565
1566 /* Otherwise, find the end of this shared extent */
1567 while (*fbno + *flen < agbno + aglen) {
1568 error = xfs_btree_increment(cur, 0, &have);
1569 if (error)
1570 goto out_error;
1571 if (!have)
1572 break;
1573 error = xfs_refcount_get_rec(cur, &tmp, &i);
1574 if (error)
1575 goto out_error;
1576 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1577 error = -EFSCORRUPTED;
1578 goto out_error;
1579 }
1580 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED ||
1581 tmp.rc_startblock >= agbno + aglen ||
1582 tmp.rc_startblock != *fbno + *flen)
1583 break;
1584 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1585 }
1586
1587done:
1588 trace_xfs_refcount_find_shared_result(cur->bc_mp,
1589 cur->bc_ag.pag->pag_agno, *fbno, *flen);
1590
1591out_error:
1592 if (error)
1593 trace_xfs_refcount_find_shared_error(cur->bc_mp,
1594 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1595 return error;
1596}
1597
1598/*
1599 * Recovering CoW Blocks After a Crash
1600 *
1601 * Due to the way that the copy on write mechanism works, there's a window of
1602 * opportunity in which we can lose track of allocated blocks during a crash.
1603 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1604 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1605 * no longer in the free space btree but are not otherwise recorded anywhere
1606 * until the write completes and the blocks are mapped into the file. A crash
1607 * in between allocation and remapping results in the replacement blocks being
1608 * lost. This situation is exacerbated by the CoW extent size hint because
1609 * allocations can hang around for long time.
1610 *
1611 * However, there is a place where we can record these allocations before they
1612 * become mappings -- the reference count btree. The btree does not record
1613 * extents with refcount == 1, so we can record allocations with a refcount of
1614 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1615 * no conflict with shared block records. These mappings should be created
1616 * when we allocate blocks to the CoW fork and deleted when they're removed
1617 * from the CoW fork.
1618 *
1619 * Minor nit: records for in-progress CoW allocations and records for shared
1620 * extents must never be merged, to preserve the property that (except for CoW
1621 * allocations) there are no refcount btree entries with refcount == 1. The
1622 * only time this could potentially happen is when unsharing a block that's
1623 * adjacent to CoW allocations, so we must be careful to avoid this.
1624 *
1625 * At mount time we recover lost CoW allocations by searching the refcount
1626 * btree for these refcount == 1 mappings. These represent CoW allocations
1627 * that were in progress at the time the filesystem went down, so we can free
1628 * them to get the space back.
1629 *
1630 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1631 * several reasons -- first, EFIs pin the tail of the log and would have to be
1632 * periodically relogged to avoid filling up the log. Second, CoW completions
1633 * will have to file an EFD and create new EFIs for whatever remains in the
1634 * CoW fork; this partially takes care of (1) but extent-size reservations
1635 * will have to periodically relog even if there's no writeout in progress.
1636 * This can happen if the CoW extent size hint is set, which you really want.
1637 * Third, EFIs cannot currently be automatically relogged into newer
1638 * transactions to advance the log tail. Fourth, stuffing the log full of
1639 * EFIs places an upper bound on the number of CoW allocations that can be
1640 * held filesystem-wide at any given time. Recording them in the refcount
1641 * btree doesn't require us to maintain any state in memory and doesn't pin
1642 * the log.
1643 */
1644/*
1645 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1646 * in that they're not referenced anywhere else in the filesystem, so we
1647 * stash them in the refcount btree with a refcount of 1 until either file
1648 * remapping (or CoW cancellation) happens.
1649 */
1650STATIC int
1651xfs_refcount_adjust_cow_extents(
1652 struct xfs_btree_cur *cur,
1653 xfs_agblock_t agbno,
1654 xfs_extlen_t aglen,
1655 enum xfs_refc_adjust_op adj)
1656{
1657 struct xfs_refcount_irec ext, tmp;
1658 int error;
1659 int found_rec, found_tmp;
1660
1661 if (aglen == 0)
1662 return 0;
1663
1664 /* Find any overlapping refcount records */
1665 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_COW, agbno,
1666 &found_rec);
1667 if (error)
1668 goto out_error;
1669 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1670 if (error)
1671 goto out_error;
1672 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec &&
1673 ext.rc_domain != XFS_REFC_DOMAIN_COW)) {
1674 error = -EFSCORRUPTED;
1675 goto out_error;
1676 }
1677 if (!found_rec) {
1678 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1679 ext.rc_blockcount = 0;
1680 ext.rc_refcount = 0;
1681 ext.rc_domain = XFS_REFC_DOMAIN_COW;
1682 }
1683
1684 switch (adj) {
1685 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1686 /* Adding a CoW reservation, there should be nothing here. */
1687 if (XFS_IS_CORRUPT(cur->bc_mp,
1688 agbno + aglen > ext.rc_startblock)) {
1689 error = -EFSCORRUPTED;
1690 goto out_error;
1691 }
1692
1693 tmp.rc_startblock = agbno;
1694 tmp.rc_blockcount = aglen;
1695 tmp.rc_refcount = 1;
1696 tmp.rc_domain = XFS_REFC_DOMAIN_COW;
1697
1698 trace_xfs_refcount_modify_extent(cur->bc_mp,
1699 cur->bc_ag.pag->pag_agno, &tmp);
1700
1701 error = xfs_refcount_insert(cur, &tmp,
1702 &found_tmp);
1703 if (error)
1704 goto out_error;
1705 if (XFS_IS_CORRUPT(cur->bc_mp, found_tmp != 1)) {
1706 error = -EFSCORRUPTED;
1707 goto out_error;
1708 }
1709 break;
1710 case XFS_REFCOUNT_ADJUST_COW_FREE:
1711 /* Removing a CoW reservation, there should be one extent. */
1712 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_startblock != agbno)) {
1713 error = -EFSCORRUPTED;
1714 goto out_error;
1715 }
1716 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount != aglen)) {
1717 error = -EFSCORRUPTED;
1718 goto out_error;
1719 }
1720 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_refcount != 1)) {
1721 error = -EFSCORRUPTED;
1722 goto out_error;
1723 }
1724
1725 ext.rc_refcount = 0;
1726 trace_xfs_refcount_modify_extent(cur->bc_mp,
1727 cur->bc_ag.pag->pag_agno, &ext);
1728 error = xfs_refcount_delete(cur, &found_rec);
1729 if (error)
1730 goto out_error;
1731 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1732 error = -EFSCORRUPTED;
1733 goto out_error;
1734 }
1735 break;
1736 default:
1737 ASSERT(0);
1738 }
1739
1740 return error;
1741out_error:
1742 trace_xfs_refcount_modify_extent_error(cur->bc_mp,
1743 cur->bc_ag.pag->pag_agno, error, _RET_IP_);
1744 return error;
1745}
1746
1747/*
1748 * Add or remove refcount btree entries for CoW reservations.
1749 */
1750STATIC int
1751xfs_refcount_adjust_cow(
1752 struct xfs_btree_cur *cur,
1753 xfs_agblock_t agbno,
1754 xfs_extlen_t aglen,
1755 enum xfs_refc_adjust_op adj)
1756{
1757 bool shape_changed;
1758 int error;
1759
1760 /*
1761 * Ensure that no rcextents cross the boundary of the adjustment range.
1762 */
1763 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1764 agbno, &shape_changed);
1765 if (error)
1766 goto out_error;
1767
1768 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1769 agbno + aglen, &shape_changed);
1770 if (error)
1771 goto out_error;
1772
1773 /*
1774 * Try to merge with the left or right extents of the range.
1775 */
1776 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_COW, &agbno,
1777 &aglen, adj, &shape_changed);
1778 if (error)
1779 goto out_error;
1780
1781 /* Now that we've taken care of the ends, adjust the middle extents */
1782 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1783 if (error)
1784 goto out_error;
1785
1786 return 0;
1787
1788out_error:
1789 trace_xfs_refcount_adjust_cow_error(cur->bc_mp, cur->bc_ag.pag->pag_agno,
1790 error, _RET_IP_);
1791 return error;
1792}
1793
1794/*
1795 * Record a CoW allocation in the refcount btree.
1796 */
1797STATIC int
1798__xfs_refcount_cow_alloc(
1799 struct xfs_btree_cur *rcur,
1800 xfs_agblock_t agbno,
1801 xfs_extlen_t aglen)
1802{
1803 trace_xfs_refcount_cow_increase(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1804 agbno, aglen);
1805
1806 /* Add refcount btree reservation */
1807 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1808 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1809}
1810
1811/*
1812 * Remove a CoW allocation from the refcount btree.
1813 */
1814STATIC int
1815__xfs_refcount_cow_free(
1816 struct xfs_btree_cur *rcur,
1817 xfs_agblock_t agbno,
1818 xfs_extlen_t aglen)
1819{
1820 trace_xfs_refcount_cow_decrease(rcur->bc_mp, rcur->bc_ag.pag->pag_agno,
1821 agbno, aglen);
1822
1823 /* Remove refcount btree reservation */
1824 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1825 XFS_REFCOUNT_ADJUST_COW_FREE);
1826}
1827
1828/* Record a CoW staging extent in the refcount btree. */
1829void
1830xfs_refcount_alloc_cow_extent(
1831 struct xfs_trans *tp,
1832 xfs_fsblock_t fsb,
1833 xfs_extlen_t len)
1834{
1835 struct xfs_mount *mp = tp->t_mountp;
1836
1837 if (!xfs_has_reflink(mp))
1838 return;
1839
1840 __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len);
1841
1842 /* Add rmap entry */
1843 xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1844 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1845}
1846
1847/* Forget a CoW staging event in the refcount btree. */
1848void
1849xfs_refcount_free_cow_extent(
1850 struct xfs_trans *tp,
1851 xfs_fsblock_t fsb,
1852 xfs_extlen_t len)
1853{
1854 struct xfs_mount *mp = tp->t_mountp;
1855
1856 if (!xfs_has_reflink(mp))
1857 return;
1858
1859 /* Remove rmap entry */
1860 xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1861 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1862 __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len);
1863}
1864
1865struct xfs_refcount_recovery {
1866 struct list_head rr_list;
1867 struct xfs_refcount_irec rr_rrec;
1868};
1869
1870/* Stuff an extent on the recovery list. */
1871STATIC int
1872xfs_refcount_recover_extent(
1873 struct xfs_btree_cur *cur,
1874 const union xfs_btree_rec *rec,
1875 void *priv)
1876{
1877 struct list_head *debris = priv;
1878 struct xfs_refcount_recovery *rr;
1879
1880 if (XFS_IS_CORRUPT(cur->bc_mp,
1881 be32_to_cpu(rec->refc.rc_refcount) != 1))
1882 return -EFSCORRUPTED;
1883
1884 rr = kmalloc(sizeof(struct xfs_refcount_recovery),
1885 GFP_KERNEL | __GFP_NOFAIL);
1886 INIT_LIST_HEAD(&rr->rr_list);
1887 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1888
1889 if (XFS_IS_CORRUPT(cur->bc_mp,
1890 rr->rr_rrec.rc_domain != XFS_REFC_DOMAIN_COW)) {
1891 kfree(rr);
1892 return -EFSCORRUPTED;
1893 }
1894
1895 list_add_tail(&rr->rr_list, debris);
1896 return 0;
1897}
1898
1899/* Find and remove leftover CoW reservations. */
1900int
1901xfs_refcount_recover_cow_leftovers(
1902 struct xfs_mount *mp,
1903 struct xfs_perag *pag)
1904{
1905 struct xfs_trans *tp;
1906 struct xfs_btree_cur *cur;
1907 struct xfs_buf *agbp;
1908 struct xfs_refcount_recovery *rr, *n;
1909 struct list_head debris;
1910 union xfs_btree_irec low;
1911 union xfs_btree_irec high;
1912 xfs_fsblock_t fsb;
1913 int error;
1914
1915 /* reflink filesystems mustn't have AGs larger than 2^31-1 blocks */
1916 BUILD_BUG_ON(XFS_MAX_CRC_AG_BLOCKS >= XFS_REFC_COWFLAG);
1917 if (mp->m_sb.sb_agblocks > XFS_MAX_CRC_AG_BLOCKS)
1918 return -EOPNOTSUPP;
1919
1920 INIT_LIST_HEAD(&debris);
1921
1922 /*
1923 * In this first part, we use an empty transaction to gather up
1924 * all the leftover CoW extents so that we can subsequently
1925 * delete them. The empty transaction is used to avoid
1926 * a buffer lock deadlock if there happens to be a loop in the
1927 * refcountbt because we're allowed to re-grab a buffer that is
1928 * already attached to our transaction. When we're done
1929 * recording the CoW debris we cancel the (empty) transaction
1930 * and everything goes away cleanly.
1931 */
1932 error = xfs_trans_alloc_empty(mp, &tp);
1933 if (error)
1934 return error;
1935
1936 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
1937 if (error)
1938 goto out_trans;
1939 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1940
1941 /* Find all the leftover CoW staging extents. */
1942 memset(&low, 0, sizeof(low));
1943 memset(&high, 0, sizeof(high));
1944 low.rc.rc_domain = high.rc.rc_domain = XFS_REFC_DOMAIN_COW;
1945 high.rc.rc_startblock = -1U;
1946 error = xfs_btree_query_range(cur, &low, &high,
1947 xfs_refcount_recover_extent, &debris);
1948 xfs_btree_del_cursor(cur, error);
1949 xfs_trans_brelse(tp, agbp);
1950 xfs_trans_cancel(tp);
1951 if (error)
1952 goto out_free;
1953
1954 /* Now iterate the list to free the leftovers */
1955 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1956 /* Set up transaction. */
1957 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1958 if (error)
1959 goto out_free;
1960
1961 trace_xfs_refcount_recover_extent(mp, pag->pag_agno,
1962 &rr->rr_rrec);
1963
1964 /* Free the orphan record */
1965 fsb = XFS_AGB_TO_FSB(mp, pag->pag_agno,
1966 rr->rr_rrec.rc_startblock);
1967 xfs_refcount_free_cow_extent(tp, fsb,
1968 rr->rr_rrec.rc_blockcount);
1969
1970 /* Free the block. */
1971 xfs_free_extent_later(tp, fsb, rr->rr_rrec.rc_blockcount, NULL);
1972
1973 error = xfs_trans_commit(tp);
1974 if (error)
1975 goto out_free;
1976
1977 list_del(&rr->rr_list);
1978 kfree(rr);
1979 }
1980
1981 return error;
1982out_trans:
1983 xfs_trans_cancel(tp);
1984out_free:
1985 /* Free the leftover list */
1986 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1987 list_del(&rr->rr_list);
1988 kfree(rr);
1989 }
1990 return error;
1991}
1992
1993/* Is there a record covering a given extent? */
1994int
1995xfs_refcount_has_record(
1996 struct xfs_btree_cur *cur,
1997 enum xfs_refc_domain domain,
1998 xfs_agblock_t bno,
1999 xfs_extlen_t len,
2000 bool *exists)
2001{
2002 union xfs_btree_irec low;
2003 union xfs_btree_irec high;
2004
2005 memset(&low, 0, sizeof(low));
2006 low.rc.rc_startblock = bno;
2007 memset(&high, 0xFF, sizeof(high));
2008 high.rc.rc_startblock = bno + len - 1;
2009 low.rc.rc_domain = high.rc.rc_domain = domain;
2010
2011 return xfs_btree_has_record(cur, &low, &high, exists);
2012}
2013
2014int __init
2015xfs_refcount_intent_init_cache(void)
2016{
2017 xfs_refcount_intent_cache = kmem_cache_create("xfs_refc_intent",
2018 sizeof(struct xfs_refcount_intent),
2019 0, 0, NULL);
2020
2021 return xfs_refcount_intent_cache != NULL ? 0 : -ENOMEM;
2022}
2023
2024void
2025xfs_refcount_intent_destroy_cache(void)
2026{
2027 kmem_cache_destroy(xfs_refcount_intent_cache);
2028 xfs_refcount_intent_cache = NULL;
2029}
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
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"
15#include "xfs_bmap.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"
22#include "xfs_bit.h"
23#include "xfs_refcount.h"
24#include "xfs_rmap.h"
25#include "xfs_ag.h"
26#include "xfs_health.h"
27#include "xfs_refcount_item.h"
28
29struct kmem_cache *xfs_refcount_intent_cache;
30
31/* Allowable refcount adjustment amounts. */
32enum xfs_refc_adjust_op {
33 XFS_REFCOUNT_ADJUST_INCREASE = 1,
34 XFS_REFCOUNT_ADJUST_DECREASE = -1,
35 XFS_REFCOUNT_ADJUST_COW_ALLOC = 0,
36 XFS_REFCOUNT_ADJUST_COW_FREE = -1,
37};
38
39STATIC int __xfs_refcount_cow_alloc(struct xfs_btree_cur *rcur,
40 xfs_agblock_t agbno, xfs_extlen_t aglen);
41STATIC int __xfs_refcount_cow_free(struct xfs_btree_cur *rcur,
42 xfs_agblock_t agbno, xfs_extlen_t aglen);
43
44/*
45 * Look up the first record less than or equal to [bno, len] in the btree
46 * given by cur.
47 */
48int
49xfs_refcount_lookup_le(
50 struct xfs_btree_cur *cur,
51 enum xfs_refc_domain domain,
52 xfs_agblock_t bno,
53 int *stat)
54{
55 trace_xfs_refcount_lookup(cur,
56 xfs_refcount_encode_startblock(bno, domain),
57 XFS_LOOKUP_LE);
58 cur->bc_rec.rc.rc_startblock = bno;
59 cur->bc_rec.rc.rc_blockcount = 0;
60 cur->bc_rec.rc.rc_domain = domain;
61 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
62}
63
64/*
65 * Look up the first record greater than or equal to [bno, len] in the btree
66 * given by cur.
67 */
68int
69xfs_refcount_lookup_ge(
70 struct xfs_btree_cur *cur,
71 enum xfs_refc_domain domain,
72 xfs_agblock_t bno,
73 int *stat)
74{
75 trace_xfs_refcount_lookup(cur,
76 xfs_refcount_encode_startblock(bno, domain),
77 XFS_LOOKUP_GE);
78 cur->bc_rec.rc.rc_startblock = bno;
79 cur->bc_rec.rc.rc_blockcount = 0;
80 cur->bc_rec.rc.rc_domain = domain;
81 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
82}
83
84/*
85 * Look up the first record equal to [bno, len] in the btree
86 * given by cur.
87 */
88int
89xfs_refcount_lookup_eq(
90 struct xfs_btree_cur *cur,
91 enum xfs_refc_domain domain,
92 xfs_agblock_t bno,
93 int *stat)
94{
95 trace_xfs_refcount_lookup(cur,
96 xfs_refcount_encode_startblock(bno, domain),
97 XFS_LOOKUP_LE);
98 cur->bc_rec.rc.rc_startblock = bno;
99 cur->bc_rec.rc.rc_blockcount = 0;
100 cur->bc_rec.rc.rc_domain = domain;
101 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
102}
103
104/* Convert on-disk record to in-core format. */
105void
106xfs_refcount_btrec_to_irec(
107 const union xfs_btree_rec *rec,
108 struct xfs_refcount_irec *irec)
109{
110 uint32_t start;
111
112 start = be32_to_cpu(rec->refc.rc_startblock);
113 if (start & XFS_REFC_COWFLAG) {
114 start &= ~XFS_REFC_COWFLAG;
115 irec->rc_domain = XFS_REFC_DOMAIN_COW;
116 } else {
117 irec->rc_domain = XFS_REFC_DOMAIN_SHARED;
118 }
119
120 irec->rc_startblock = start;
121 irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount);
122 irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount);
123}
124
125/* Simple checks for refcount records. */
126xfs_failaddr_t
127xfs_refcount_check_irec(
128 struct xfs_perag *pag,
129 const struct xfs_refcount_irec *irec)
130{
131 if (irec->rc_blockcount == 0 || irec->rc_blockcount > MAXREFCEXTLEN)
132 return __this_address;
133
134 if (!xfs_refcount_check_domain(irec))
135 return __this_address;
136
137 /* check for valid extent range, including overflow */
138 if (!xfs_verify_agbext(pag, irec->rc_startblock, irec->rc_blockcount))
139 return __this_address;
140
141 if (irec->rc_refcount == 0 || irec->rc_refcount > MAXREFCOUNT)
142 return __this_address;
143
144 return NULL;
145}
146
147static inline int
148xfs_refcount_complain_bad_rec(
149 struct xfs_btree_cur *cur,
150 xfs_failaddr_t fa,
151 const struct xfs_refcount_irec *irec)
152{
153 struct xfs_mount *mp = cur->bc_mp;
154
155 xfs_warn(mp,
156 "Refcount BTree record corruption in AG %d detected at %pS!",
157 cur->bc_group->xg_gno, fa);
158 xfs_warn(mp,
159 "Start block 0x%x, block count 0x%x, references 0x%x",
160 irec->rc_startblock, irec->rc_blockcount, irec->rc_refcount);
161 xfs_btree_mark_sick(cur);
162 return -EFSCORRUPTED;
163}
164
165/*
166 * Get the data from the pointed-to record.
167 */
168int
169xfs_refcount_get_rec(
170 struct xfs_btree_cur *cur,
171 struct xfs_refcount_irec *irec,
172 int *stat)
173{
174 union xfs_btree_rec *rec;
175 xfs_failaddr_t fa;
176 int error;
177
178 error = xfs_btree_get_rec(cur, &rec, stat);
179 if (error || !*stat)
180 return error;
181
182 xfs_refcount_btrec_to_irec(rec, irec);
183 fa = xfs_refcount_check_irec(to_perag(cur->bc_group), irec);
184 if (fa)
185 return xfs_refcount_complain_bad_rec(cur, fa, irec);
186
187 trace_xfs_refcount_get(cur, irec);
188 return 0;
189}
190
191/*
192 * Update the record referred to by cur to the value given
193 * by [bno, len, refcount].
194 * This either works (return 0) or gets an EFSCORRUPTED error.
195 */
196STATIC int
197xfs_refcount_update(
198 struct xfs_btree_cur *cur,
199 struct xfs_refcount_irec *irec)
200{
201 union xfs_btree_rec rec;
202 uint32_t start;
203 int error;
204
205 trace_xfs_refcount_update(cur, irec);
206
207 start = xfs_refcount_encode_startblock(irec->rc_startblock,
208 irec->rc_domain);
209 rec.refc.rc_startblock = cpu_to_be32(start);
210 rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount);
211 rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount);
212
213 error = xfs_btree_update(cur, &rec);
214 if (error)
215 trace_xfs_refcount_update_error(cur, error, _RET_IP_);
216 return error;
217}
218
219/*
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.
223 */
224int
225xfs_refcount_insert(
226 struct xfs_btree_cur *cur,
227 struct xfs_refcount_irec *irec,
228 int *i)
229{
230 int error;
231
232 trace_xfs_refcount_insert(cur, irec);
233
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;
238
239 error = xfs_btree_insert(cur, i);
240 if (error)
241 goto out_error;
242 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
243 xfs_btree_mark_sick(cur);
244 error = -EFSCORRUPTED;
245 goto out_error;
246 }
247
248out_error:
249 if (error)
250 trace_xfs_refcount_insert_error(cur, error, _RET_IP_);
251 return error;
252}
253
254/*
255 * Remove the record referred to by cur, then set the pointer to the spot
256 * where the record could be re-inserted, in case we want to increment or
257 * decrement the cursor.
258 * This either works (return 0) or gets an EFSCORRUPTED error.
259 */
260STATIC int
261xfs_refcount_delete(
262 struct xfs_btree_cur *cur,
263 int *i)
264{
265 struct xfs_refcount_irec irec;
266 int found_rec;
267 int error;
268
269 error = xfs_refcount_get_rec(cur, &irec, &found_rec);
270 if (error)
271 goto out_error;
272 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
273 xfs_btree_mark_sick(cur);
274 error = -EFSCORRUPTED;
275 goto out_error;
276 }
277 trace_xfs_refcount_delete(cur, &irec);
278 error = xfs_btree_delete(cur, i);
279 if (XFS_IS_CORRUPT(cur->bc_mp, *i != 1)) {
280 xfs_btree_mark_sick(cur);
281 error = -EFSCORRUPTED;
282 goto out_error;
283 }
284 if (error)
285 goto out_error;
286 error = xfs_refcount_lookup_ge(cur, irec.rc_domain, irec.rc_startblock,
287 &found_rec);
288out_error:
289 if (error)
290 trace_xfs_refcount_delete_error(cur, error, _RET_IP_);
291 return error;
292}
293
294/*
295 * Adjusting the Reference Count
296 *
297 * As stated elsewhere, the reference count btree (refcbt) stores
298 * >1 reference counts for extents of physical blocks. In this
299 * operation, we're either raising or lowering the reference count of
300 * some subrange stored in the tree:
301 *
302 * <------ adjustment range ------>
303 * ----+ +---+-----+ +--+--------+---------
304 * 2 | | 3 | 4 | |17| 55 | 10
305 * ----+ +---+-----+ +--+--------+---------
306 * X axis is physical blocks number;
307 * reference counts are the numbers inside the rectangles
308 *
309 * The first thing we need to do is to ensure that there are no
310 * refcount extents crossing either boundary of the range to be
311 * adjusted. For any extent that does cross a boundary, split it into
312 * two extents so that we can increment the refcount of one of the
313 * pieces later:
314 *
315 * <------ adjustment range ------>
316 * ----+ +---+-----+ +--+--------+----+----
317 * 2 | | 3 | 2 | |17| 55 | 10 | 10
318 * ----+ +---+-----+ +--+--------+----+----
319 *
320 * For this next step, let's assume that all the physical blocks in
321 * the adjustment range are mapped to a file and are therefore in use
322 * at least once. Therefore, we can infer that any gap in the
323 * refcount tree within the adjustment range represents a physical
324 * extent with refcount == 1:
325 *
326 * <------ adjustment range ------>
327 * ----+---+---+-----+-+--+--------+----+----
328 * 2 |"1"| 3 | 2 |1|17| 55 | 10 | 10
329 * ----+---+---+-----+-+--+--------+----+----
330 * ^
331 *
332 * For each extent that falls within the interval range, figure out
333 * which extent is to the left or the right of that extent. Now we
334 * have a left, current, and right extent. If the new reference count
335 * of the center extent enables us to merge left, center, and right
336 * into one record covering all three, do so. If the center extent is
337 * at the left end of the range, abuts the left extent, and its new
338 * reference count matches the left extent's record, then merge them.
339 * If the center extent is at the right end of the range, abuts the
340 * right extent, and the reference counts match, merge those. In the
341 * example, we can left merge (assuming an increment operation):
342 *
343 * <------ adjustment range ------>
344 * --------+---+-----+-+--+--------+----+----
345 * 2 | 3 | 2 |1|17| 55 | 10 | 10
346 * --------+---+-----+-+--+--------+----+----
347 * ^
348 *
349 * For all other extents within the range, adjust the reference count
350 * or delete it if the refcount falls below 2. If we were
351 * incrementing, the end result looks like this:
352 *
353 * <------ adjustment range ------>
354 * --------+---+-----+-+--+--------+----+----
355 * 2 | 4 | 3 |2|18| 56 | 11 | 10
356 * --------+---+-----+-+--+--------+----+----
357 *
358 * The result of a decrement operation looks as such:
359 *
360 * <------ adjustment range ------>
361 * ----+ +---+ +--+--------+----+----
362 * 2 | | 2 | |16| 54 | 9 | 10
363 * ----+ +---+ +--+--------+----+----
364 * DDDD 111111DD
365 *
366 * The blocks marked "D" are freed; the blocks marked "1" are only
367 * referenced once and therefore the record is removed from the
368 * refcount btree.
369 */
370
371/* Next block after this extent. */
372static inline xfs_agblock_t
373xfs_refc_next(
374 struct xfs_refcount_irec *rc)
375{
376 return rc->rc_startblock + rc->rc_blockcount;
377}
378
379/*
380 * Split a refcount extent that crosses agbno.
381 */
382STATIC int
383xfs_refcount_split_extent(
384 struct xfs_btree_cur *cur,
385 enum xfs_refc_domain domain,
386 xfs_agblock_t agbno,
387 bool *shape_changed)
388{
389 struct xfs_refcount_irec rcext, tmp;
390 int found_rec;
391 int error;
392
393 *shape_changed = false;
394 error = xfs_refcount_lookup_le(cur, domain, agbno, &found_rec);
395 if (error)
396 goto out_error;
397 if (!found_rec)
398 return 0;
399
400 error = xfs_refcount_get_rec(cur, &rcext, &found_rec);
401 if (error)
402 goto out_error;
403 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
404 xfs_btree_mark_sick(cur);
405 error = -EFSCORRUPTED;
406 goto out_error;
407 }
408 if (rcext.rc_domain != domain)
409 return 0;
410 if (rcext.rc_startblock == agbno || xfs_refc_next(&rcext) <= agbno)
411 return 0;
412
413 *shape_changed = true;
414 trace_xfs_refcount_split_extent(cur, &rcext, agbno);
415
416 /* Establish the right extent. */
417 tmp = rcext;
418 tmp.rc_startblock = agbno;
419 tmp.rc_blockcount -= (agbno - rcext.rc_startblock);
420 error = xfs_refcount_update(cur, &tmp);
421 if (error)
422 goto out_error;
423
424 /* Insert the left extent. */
425 tmp = rcext;
426 tmp.rc_blockcount = agbno - rcext.rc_startblock;
427 error = xfs_refcount_insert(cur, &tmp, &found_rec);
428 if (error)
429 goto out_error;
430 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
431 xfs_btree_mark_sick(cur);
432 error = -EFSCORRUPTED;
433 goto out_error;
434 }
435 return error;
436
437out_error:
438 trace_xfs_refcount_split_extent_error(cur, error, _RET_IP_);
439 return error;
440}
441
442/*
443 * Merge the left, center, and right extents.
444 */
445STATIC int
446xfs_refcount_merge_center_extents(
447 struct xfs_btree_cur *cur,
448 struct xfs_refcount_irec *left,
449 struct xfs_refcount_irec *center,
450 struct xfs_refcount_irec *right,
451 unsigned long long extlen,
452 xfs_extlen_t *aglen)
453{
454 int error;
455 int found_rec;
456
457 trace_xfs_refcount_merge_center_extents(cur, left, center, right);
458
459 ASSERT(left->rc_domain == center->rc_domain);
460 ASSERT(right->rc_domain == center->rc_domain);
461
462 /*
463 * Make sure the center and right extents are not in the btree.
464 * If the center extent was synthesized, the first delete call
465 * removes the right extent and we skip the second deletion.
466 * If center and right were in the btree, then the first delete
467 * call removes the center and the second one removes the right
468 * extent.
469 */
470 error = xfs_refcount_lookup_ge(cur, center->rc_domain,
471 center->rc_startblock, &found_rec);
472 if (error)
473 goto out_error;
474 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
475 xfs_btree_mark_sick(cur);
476 error = -EFSCORRUPTED;
477 goto out_error;
478 }
479
480 error = xfs_refcount_delete(cur, &found_rec);
481 if (error)
482 goto out_error;
483 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
484 xfs_btree_mark_sick(cur);
485 error = -EFSCORRUPTED;
486 goto out_error;
487 }
488
489 if (center->rc_refcount > 1) {
490 error = xfs_refcount_delete(cur, &found_rec);
491 if (error)
492 goto out_error;
493 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
494 xfs_btree_mark_sick(cur);
495 error = -EFSCORRUPTED;
496 goto out_error;
497 }
498 }
499
500 /* Enlarge the left extent. */
501 error = xfs_refcount_lookup_le(cur, left->rc_domain,
502 left->rc_startblock, &found_rec);
503 if (error)
504 goto out_error;
505 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
506 xfs_btree_mark_sick(cur);
507 error = -EFSCORRUPTED;
508 goto out_error;
509 }
510
511 left->rc_blockcount = extlen;
512 error = xfs_refcount_update(cur, left);
513 if (error)
514 goto out_error;
515
516 *aglen = 0;
517 return error;
518
519out_error:
520 trace_xfs_refcount_merge_center_extents_error(cur, error, _RET_IP_);
521 return error;
522}
523
524/*
525 * Merge with the left extent.
526 */
527STATIC int
528xfs_refcount_merge_left_extent(
529 struct xfs_btree_cur *cur,
530 struct xfs_refcount_irec *left,
531 struct xfs_refcount_irec *cleft,
532 xfs_agblock_t *agbno,
533 xfs_extlen_t *aglen)
534{
535 int error;
536 int found_rec;
537
538 trace_xfs_refcount_merge_left_extent(cur, left, cleft);
539
540 ASSERT(left->rc_domain == cleft->rc_domain);
541
542 /* If the extent at agbno (cleft) wasn't synthesized, remove it. */
543 if (cleft->rc_refcount > 1) {
544 error = xfs_refcount_lookup_le(cur, cleft->rc_domain,
545 cleft->rc_startblock, &found_rec);
546 if (error)
547 goto out_error;
548 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
549 xfs_btree_mark_sick(cur);
550 error = -EFSCORRUPTED;
551 goto out_error;
552 }
553
554 error = xfs_refcount_delete(cur, &found_rec);
555 if (error)
556 goto out_error;
557 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
558 xfs_btree_mark_sick(cur);
559 error = -EFSCORRUPTED;
560 goto out_error;
561 }
562 }
563
564 /* Enlarge the left extent. */
565 error = xfs_refcount_lookup_le(cur, left->rc_domain,
566 left->rc_startblock, &found_rec);
567 if (error)
568 goto out_error;
569 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
570 xfs_btree_mark_sick(cur);
571 error = -EFSCORRUPTED;
572 goto out_error;
573 }
574
575 left->rc_blockcount += cleft->rc_blockcount;
576 error = xfs_refcount_update(cur, left);
577 if (error)
578 goto out_error;
579
580 *agbno += cleft->rc_blockcount;
581 *aglen -= cleft->rc_blockcount;
582 return error;
583
584out_error:
585 trace_xfs_refcount_merge_left_extent_error(cur, error, _RET_IP_);
586 return error;
587}
588
589/*
590 * Merge with the right extent.
591 */
592STATIC int
593xfs_refcount_merge_right_extent(
594 struct xfs_btree_cur *cur,
595 struct xfs_refcount_irec *right,
596 struct xfs_refcount_irec *cright,
597 xfs_extlen_t *aglen)
598{
599 int error;
600 int found_rec;
601
602 trace_xfs_refcount_merge_right_extent(cur, cright, right);
603
604 ASSERT(right->rc_domain == cright->rc_domain);
605
606 /*
607 * If the extent ending at agbno+aglen (cright) wasn't synthesized,
608 * remove it.
609 */
610 if (cright->rc_refcount > 1) {
611 error = xfs_refcount_lookup_le(cur, cright->rc_domain,
612 cright->rc_startblock, &found_rec);
613 if (error)
614 goto out_error;
615 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
616 xfs_btree_mark_sick(cur);
617 error = -EFSCORRUPTED;
618 goto out_error;
619 }
620
621 error = xfs_refcount_delete(cur, &found_rec);
622 if (error)
623 goto out_error;
624 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
625 xfs_btree_mark_sick(cur);
626 error = -EFSCORRUPTED;
627 goto out_error;
628 }
629 }
630
631 /* Enlarge the right extent. */
632 error = xfs_refcount_lookup_le(cur, right->rc_domain,
633 right->rc_startblock, &found_rec);
634 if (error)
635 goto out_error;
636 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
637 xfs_btree_mark_sick(cur);
638 error = -EFSCORRUPTED;
639 goto out_error;
640 }
641
642 right->rc_startblock -= cright->rc_blockcount;
643 right->rc_blockcount += cright->rc_blockcount;
644 error = xfs_refcount_update(cur, right);
645 if (error)
646 goto out_error;
647
648 *aglen -= cright->rc_blockcount;
649 return error;
650
651out_error:
652 trace_xfs_refcount_merge_right_extent_error(cur, error, _RET_IP_);
653 return error;
654}
655
656/*
657 * Find the left extent and the one after it (cleft). This function assumes
658 * that we've already split any extent crossing agbno.
659 */
660STATIC int
661xfs_refcount_find_left_extents(
662 struct xfs_btree_cur *cur,
663 struct xfs_refcount_irec *left,
664 struct xfs_refcount_irec *cleft,
665 enum xfs_refc_domain domain,
666 xfs_agblock_t agbno,
667 xfs_extlen_t aglen)
668{
669 struct xfs_refcount_irec tmp;
670 int error;
671 int found_rec;
672
673 left->rc_startblock = cleft->rc_startblock = NULLAGBLOCK;
674 error = xfs_refcount_lookup_le(cur, domain, agbno - 1, &found_rec);
675 if (error)
676 goto out_error;
677 if (!found_rec)
678 return 0;
679
680 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
681 if (error)
682 goto out_error;
683 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
684 xfs_btree_mark_sick(cur);
685 error = -EFSCORRUPTED;
686 goto out_error;
687 }
688
689 if (tmp.rc_domain != domain)
690 return 0;
691 if (xfs_refc_next(&tmp) != agbno)
692 return 0;
693 /* We have a left extent; retrieve (or invent) the next right one */
694 *left = tmp;
695
696 error = xfs_btree_increment(cur, 0, &found_rec);
697 if (error)
698 goto out_error;
699 if (found_rec) {
700 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
701 if (error)
702 goto out_error;
703 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
704 xfs_btree_mark_sick(cur);
705 error = -EFSCORRUPTED;
706 goto out_error;
707 }
708
709 if (tmp.rc_domain != domain)
710 goto not_found;
711
712 /* if tmp starts at the end of our range, just use that */
713 if (tmp.rc_startblock == agbno)
714 *cleft = tmp;
715 else {
716 /*
717 * There's a gap in the refcntbt at the start of the
718 * range we're interested in (refcount == 1) so
719 * synthesize the implied extent and pass it back.
720 * We assume here that the agbno/aglen range was
721 * passed in from a data fork extent mapping and
722 * therefore is allocated to exactly one owner.
723 */
724 cleft->rc_startblock = agbno;
725 cleft->rc_blockcount = min(aglen,
726 tmp.rc_startblock - agbno);
727 cleft->rc_refcount = 1;
728 cleft->rc_domain = domain;
729 }
730 } else {
731not_found:
732 /*
733 * No extents, so pretend that there's one covering the whole
734 * range.
735 */
736 cleft->rc_startblock = agbno;
737 cleft->rc_blockcount = aglen;
738 cleft->rc_refcount = 1;
739 cleft->rc_domain = domain;
740 }
741 trace_xfs_refcount_find_left_extent(cur, left, cleft, agbno);
742 return error;
743
744out_error:
745 trace_xfs_refcount_find_left_extent_error(cur, error, _RET_IP_);
746 return error;
747}
748
749/*
750 * Find the right extent and the one before it (cright). This function
751 * assumes that we've already split any extents crossing agbno + aglen.
752 */
753STATIC int
754xfs_refcount_find_right_extents(
755 struct xfs_btree_cur *cur,
756 struct xfs_refcount_irec *right,
757 struct xfs_refcount_irec *cright,
758 enum xfs_refc_domain domain,
759 xfs_agblock_t agbno,
760 xfs_extlen_t aglen)
761{
762 struct xfs_refcount_irec tmp;
763 int error;
764 int found_rec;
765
766 right->rc_startblock = cright->rc_startblock = NULLAGBLOCK;
767 error = xfs_refcount_lookup_ge(cur, domain, agbno + aglen, &found_rec);
768 if (error)
769 goto out_error;
770 if (!found_rec)
771 return 0;
772
773 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
774 if (error)
775 goto out_error;
776 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
777 xfs_btree_mark_sick(cur);
778 error = -EFSCORRUPTED;
779 goto out_error;
780 }
781
782 if (tmp.rc_domain != domain)
783 return 0;
784 if (tmp.rc_startblock != agbno + aglen)
785 return 0;
786 /* We have a right extent; retrieve (or invent) the next left one */
787 *right = tmp;
788
789 error = xfs_btree_decrement(cur, 0, &found_rec);
790 if (error)
791 goto out_error;
792 if (found_rec) {
793 error = xfs_refcount_get_rec(cur, &tmp, &found_rec);
794 if (error)
795 goto out_error;
796 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
797 xfs_btree_mark_sick(cur);
798 error = -EFSCORRUPTED;
799 goto out_error;
800 }
801
802 if (tmp.rc_domain != domain)
803 goto not_found;
804
805 /* if tmp ends at the end of our range, just use that */
806 if (xfs_refc_next(&tmp) == agbno + aglen)
807 *cright = tmp;
808 else {
809 /*
810 * There's a gap in the refcntbt at the end of the
811 * range we're interested in (refcount == 1) so
812 * create the implied extent and pass it back.
813 * We assume here that the agbno/aglen range was
814 * passed in from a data fork extent mapping and
815 * therefore is allocated to exactly one owner.
816 */
817 cright->rc_startblock = max(agbno, xfs_refc_next(&tmp));
818 cright->rc_blockcount = right->rc_startblock -
819 cright->rc_startblock;
820 cright->rc_refcount = 1;
821 cright->rc_domain = domain;
822 }
823 } else {
824not_found:
825 /*
826 * No extents, so pretend that there's one covering the whole
827 * range.
828 */
829 cright->rc_startblock = agbno;
830 cright->rc_blockcount = aglen;
831 cright->rc_refcount = 1;
832 cright->rc_domain = domain;
833 }
834 trace_xfs_refcount_find_right_extent(cur, cright, right,
835 agbno + aglen);
836 return error;
837
838out_error:
839 trace_xfs_refcount_find_right_extent_error(cur, error, _RET_IP_);
840 return error;
841}
842
843/* Is this extent valid? */
844static inline bool
845xfs_refc_valid(
846 const struct xfs_refcount_irec *rc)
847{
848 return rc->rc_startblock != NULLAGBLOCK;
849}
850
851static inline xfs_nlink_t
852xfs_refc_merge_refcount(
853 const struct xfs_refcount_irec *irec,
854 enum xfs_refc_adjust_op adjust)
855{
856 /* Once a record hits MAXREFCOUNT, it is pinned there forever */
857 if (irec->rc_refcount == MAXREFCOUNT)
858 return MAXREFCOUNT;
859 return irec->rc_refcount + adjust;
860}
861
862static inline bool
863xfs_refc_want_merge_center(
864 const struct xfs_refcount_irec *left,
865 const struct xfs_refcount_irec *cleft,
866 const struct xfs_refcount_irec *cright,
867 const struct xfs_refcount_irec *right,
868 bool cleft_is_cright,
869 enum xfs_refc_adjust_op adjust,
870 unsigned long long *ulenp)
871{
872 unsigned long long ulen = left->rc_blockcount;
873 xfs_nlink_t new_refcount;
874
875 /*
876 * To merge with a center record, both shoulder records must be
877 * adjacent to the record we want to adjust. This is only true if
878 * find_left and find_right made all four records valid.
879 */
880 if (!xfs_refc_valid(left) || !xfs_refc_valid(right) ||
881 !xfs_refc_valid(cleft) || !xfs_refc_valid(cright))
882 return false;
883
884 /* There must only be one record for the entire range. */
885 if (!cleft_is_cright)
886 return false;
887
888 /* The shoulder record refcounts must match the new refcount. */
889 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
890 if (left->rc_refcount != new_refcount)
891 return false;
892 if (right->rc_refcount != new_refcount)
893 return false;
894
895 /*
896 * The new record cannot exceed the max length. ulen is a ULL as the
897 * individual record block counts can be up to (u32 - 1) in length
898 * hence we need to catch u32 addition overflows here.
899 */
900 ulen += cleft->rc_blockcount + right->rc_blockcount;
901 if (ulen >= MAXREFCEXTLEN)
902 return false;
903
904 *ulenp = ulen;
905 return true;
906}
907
908static inline bool
909xfs_refc_want_merge_left(
910 const struct xfs_refcount_irec *left,
911 const struct xfs_refcount_irec *cleft,
912 enum xfs_refc_adjust_op adjust)
913{
914 unsigned long long ulen = left->rc_blockcount;
915 xfs_nlink_t new_refcount;
916
917 /*
918 * For a left merge, the left shoulder record must be adjacent to the
919 * start of the range. If this is true, find_left made left and cleft
920 * contain valid contents.
921 */
922 if (!xfs_refc_valid(left) || !xfs_refc_valid(cleft))
923 return false;
924
925 /* Left shoulder record refcount must match the new refcount. */
926 new_refcount = xfs_refc_merge_refcount(cleft, adjust);
927 if (left->rc_refcount != new_refcount)
928 return false;
929
930 /*
931 * The new record cannot exceed the max length. ulen is a ULL as the
932 * individual record block counts can be up to (u32 - 1) in length
933 * hence we need to catch u32 addition overflows here.
934 */
935 ulen += cleft->rc_blockcount;
936 if (ulen >= MAXREFCEXTLEN)
937 return false;
938
939 return true;
940}
941
942static inline bool
943xfs_refc_want_merge_right(
944 const struct xfs_refcount_irec *cright,
945 const struct xfs_refcount_irec *right,
946 enum xfs_refc_adjust_op adjust)
947{
948 unsigned long long ulen = right->rc_blockcount;
949 xfs_nlink_t new_refcount;
950
951 /*
952 * For a right merge, the right shoulder record must be adjacent to the
953 * end of the range. If this is true, find_right made cright and right
954 * contain valid contents.
955 */
956 if (!xfs_refc_valid(right) || !xfs_refc_valid(cright))
957 return false;
958
959 /* Right shoulder record refcount must match the new refcount. */
960 new_refcount = xfs_refc_merge_refcount(cright, adjust);
961 if (right->rc_refcount != new_refcount)
962 return false;
963
964 /*
965 * The new record cannot exceed the max length. ulen is a ULL as the
966 * individual record block counts can be up to (u32 - 1) in length
967 * hence we need to catch u32 addition overflows here.
968 */
969 ulen += cright->rc_blockcount;
970 if (ulen >= MAXREFCEXTLEN)
971 return false;
972
973 return true;
974}
975
976/*
977 * Try to merge with any extents on the boundaries of the adjustment range.
978 */
979STATIC int
980xfs_refcount_merge_extents(
981 struct xfs_btree_cur *cur,
982 enum xfs_refc_domain domain,
983 xfs_agblock_t *agbno,
984 xfs_extlen_t *aglen,
985 enum xfs_refc_adjust_op adjust,
986 bool *shape_changed)
987{
988 struct xfs_refcount_irec left = {0}, cleft = {0};
989 struct xfs_refcount_irec cright = {0}, right = {0};
990 int error;
991 unsigned long long ulen;
992 bool cequal;
993
994 *shape_changed = false;
995 /*
996 * Find the extent just below agbno [left], just above agbno [cleft],
997 * just below (agbno + aglen) [cright], and just above (agbno + aglen)
998 * [right].
999 */
1000 error = xfs_refcount_find_left_extents(cur, &left, &cleft, domain,
1001 *agbno, *aglen);
1002 if (error)
1003 return error;
1004 error = xfs_refcount_find_right_extents(cur, &right, &cright, domain,
1005 *agbno, *aglen);
1006 if (error)
1007 return error;
1008
1009 /* No left or right extent to merge; exit. */
1010 if (!xfs_refc_valid(&left) && !xfs_refc_valid(&right))
1011 return 0;
1012
1013 cequal = (cleft.rc_startblock == cright.rc_startblock) &&
1014 (cleft.rc_blockcount == cright.rc_blockcount);
1015
1016 /* Try to merge left, cleft, and right. cleft must == cright. */
1017 if (xfs_refc_want_merge_center(&left, &cleft, &cright, &right, cequal,
1018 adjust, &ulen)) {
1019 *shape_changed = true;
1020 return xfs_refcount_merge_center_extents(cur, &left, &cleft,
1021 &right, ulen, aglen);
1022 }
1023
1024 /* Try to merge left and cleft. */
1025 if (xfs_refc_want_merge_left(&left, &cleft, adjust)) {
1026 *shape_changed = true;
1027 error = xfs_refcount_merge_left_extent(cur, &left, &cleft,
1028 agbno, aglen);
1029 if (error)
1030 return error;
1031
1032 /*
1033 * If we just merged left + cleft and cleft == cright,
1034 * we no longer have a cright to merge with right. We're done.
1035 */
1036 if (cequal)
1037 return 0;
1038 }
1039
1040 /* Try to merge cright and right. */
1041 if (xfs_refc_want_merge_right(&cright, &right, adjust)) {
1042 *shape_changed = true;
1043 return xfs_refcount_merge_right_extent(cur, &right, &cright,
1044 aglen);
1045 }
1046
1047 return 0;
1048}
1049
1050/*
1051 * XXX: This is a pretty hand-wavy estimate. The penalty for guessing
1052 * true incorrectly is a shutdown FS; the penalty for guessing false
1053 * incorrectly is more transaction rolls than might be necessary.
1054 * Be conservative here.
1055 */
1056static bool
1057xfs_refcount_still_have_space(
1058 struct xfs_btree_cur *cur)
1059{
1060 unsigned long overhead;
1061
1062 /*
1063 * Worst case estimate: full splits of the free space and rmap btrees
1064 * to handle each of the shape changes to the refcount btree.
1065 */
1066 overhead = xfs_allocfree_block_count(cur->bc_mp,
1067 cur->bc_refc.shape_changes);
1068 overhead += cur->bc_mp->m_refc_maxlevels;
1069 overhead *= cur->bc_mp->m_sb.sb_blocksize;
1070
1071 /*
1072 * Only allow 2 refcount extent updates per transaction if the
1073 * refcount continue update "error" has been injected.
1074 */
1075 if (cur->bc_refc.nr_ops > 2 &&
1076 XFS_TEST_ERROR(false, cur->bc_mp,
1077 XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE))
1078 return false;
1079
1080 if (cur->bc_refc.nr_ops == 0)
1081 return true;
1082 else if (overhead > cur->bc_tp->t_log_res)
1083 return false;
1084 return cur->bc_tp->t_log_res - overhead >
1085 cur->bc_refc.nr_ops * XFS_REFCOUNT_ITEM_OVERHEAD;
1086}
1087
1088/*
1089 * Adjust the refcounts of middle extents. At this point we should have
1090 * split extents that crossed the adjustment range; merged with adjacent
1091 * extents; and updated agbno/aglen to reflect the merges. Therefore,
1092 * all we have to do is update the extents inside [agbno, agbno + aglen].
1093 */
1094STATIC int
1095xfs_refcount_adjust_extents(
1096 struct xfs_btree_cur *cur,
1097 xfs_agblock_t *agbno,
1098 xfs_extlen_t *aglen,
1099 enum xfs_refc_adjust_op adj)
1100{
1101 struct xfs_refcount_irec ext, tmp;
1102 int error;
1103 int found_rec, found_tmp;
1104 xfs_fsblock_t fsbno;
1105
1106 /* Merging did all the work already. */
1107 if (*aglen == 0)
1108 return 0;
1109
1110 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_SHARED, *agbno,
1111 &found_rec);
1112 if (error)
1113 goto out_error;
1114
1115 while (*aglen > 0 && xfs_refcount_still_have_space(cur)) {
1116 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1117 if (error)
1118 goto out_error;
1119 if (!found_rec || ext.rc_domain != XFS_REFC_DOMAIN_SHARED) {
1120 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1121 ext.rc_blockcount = 0;
1122 ext.rc_refcount = 0;
1123 ext.rc_domain = XFS_REFC_DOMAIN_SHARED;
1124 }
1125
1126 /*
1127 * Deal with a hole in the refcount tree; if a file maps to
1128 * these blocks and there's no refcountbt record, pretend that
1129 * there is one with refcount == 1.
1130 */
1131 if (ext.rc_startblock != *agbno) {
1132 tmp.rc_startblock = *agbno;
1133 tmp.rc_blockcount = min(*aglen,
1134 ext.rc_startblock - *agbno);
1135 tmp.rc_refcount = 1 + adj;
1136 tmp.rc_domain = XFS_REFC_DOMAIN_SHARED;
1137
1138 trace_xfs_refcount_modify_extent(cur, &tmp);
1139
1140 /*
1141 * Either cover the hole (increment) or
1142 * delete the range (decrement).
1143 */
1144 cur->bc_refc.nr_ops++;
1145 if (tmp.rc_refcount) {
1146 error = xfs_refcount_insert(cur, &tmp,
1147 &found_tmp);
1148 if (error)
1149 goto out_error;
1150 if (XFS_IS_CORRUPT(cur->bc_mp,
1151 found_tmp != 1)) {
1152 xfs_btree_mark_sick(cur);
1153 error = -EFSCORRUPTED;
1154 goto out_error;
1155 }
1156 } else {
1157 fsbno = xfs_agbno_to_fsb(to_perag(cur->bc_group),
1158 tmp.rc_startblock);
1159 error = xfs_free_extent_later(cur->bc_tp, fsbno,
1160 tmp.rc_blockcount, NULL,
1161 XFS_AG_RESV_NONE, 0);
1162 if (error)
1163 goto out_error;
1164 }
1165
1166 (*agbno) += tmp.rc_blockcount;
1167 (*aglen) -= tmp.rc_blockcount;
1168
1169 /* Stop if there's nothing left to modify */
1170 if (*aglen == 0 || !xfs_refcount_still_have_space(cur))
1171 break;
1172
1173 /* Move the cursor to the start of ext. */
1174 error = xfs_refcount_lookup_ge(cur,
1175 XFS_REFC_DOMAIN_SHARED, *agbno,
1176 &found_rec);
1177 if (error)
1178 goto out_error;
1179 }
1180
1181 /*
1182 * A previous step trimmed agbno/aglen such that the end of the
1183 * range would not be in the middle of the record. If this is
1184 * no longer the case, something is seriously wrong with the
1185 * btree. Make sure we never feed the synthesized record into
1186 * the processing loop below.
1187 */
1188 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount == 0) ||
1189 XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount > *aglen)) {
1190 xfs_btree_mark_sick(cur);
1191 error = -EFSCORRUPTED;
1192 goto out_error;
1193 }
1194
1195 /*
1196 * Adjust the reference count and either update the tree
1197 * (incr) or free the blocks (decr).
1198 */
1199 if (ext.rc_refcount == MAXREFCOUNT)
1200 goto skip;
1201 ext.rc_refcount += adj;
1202 trace_xfs_refcount_modify_extent(cur, &ext);
1203 cur->bc_refc.nr_ops++;
1204 if (ext.rc_refcount > 1) {
1205 error = xfs_refcount_update(cur, &ext);
1206 if (error)
1207 goto out_error;
1208 } else if (ext.rc_refcount == 1) {
1209 error = xfs_refcount_delete(cur, &found_rec);
1210 if (error)
1211 goto out_error;
1212 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1213 xfs_btree_mark_sick(cur);
1214 error = -EFSCORRUPTED;
1215 goto out_error;
1216 }
1217 goto advloop;
1218 } else {
1219 fsbno = xfs_agbno_to_fsb(to_perag(cur->bc_group),
1220 ext.rc_startblock);
1221 error = xfs_free_extent_later(cur->bc_tp, fsbno,
1222 ext.rc_blockcount, NULL,
1223 XFS_AG_RESV_NONE, 0);
1224 if (error)
1225 goto out_error;
1226 }
1227
1228skip:
1229 error = xfs_btree_increment(cur, 0, &found_rec);
1230 if (error)
1231 goto out_error;
1232
1233advloop:
1234 (*agbno) += ext.rc_blockcount;
1235 (*aglen) -= ext.rc_blockcount;
1236 }
1237
1238 return error;
1239out_error:
1240 trace_xfs_refcount_modify_extent_error(cur, error, _RET_IP_);
1241 return error;
1242}
1243
1244/* Adjust the reference count of a range of AG blocks. */
1245STATIC int
1246xfs_refcount_adjust(
1247 struct xfs_btree_cur *cur,
1248 xfs_agblock_t *agbno,
1249 xfs_extlen_t *aglen,
1250 enum xfs_refc_adjust_op adj)
1251{
1252 bool shape_changed;
1253 int shape_changes = 0;
1254 int error;
1255
1256 if (adj == XFS_REFCOUNT_ADJUST_INCREASE)
1257 trace_xfs_refcount_increase(cur, *agbno, *aglen);
1258 else
1259 trace_xfs_refcount_decrease(cur, *agbno, *aglen);
1260
1261 /*
1262 * Ensure that no rcextents cross the boundary of the adjustment range.
1263 */
1264 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1265 *agbno, &shape_changed);
1266 if (error)
1267 goto out_error;
1268 if (shape_changed)
1269 shape_changes++;
1270
1271 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_SHARED,
1272 *agbno + *aglen, &shape_changed);
1273 if (error)
1274 goto out_error;
1275 if (shape_changed)
1276 shape_changes++;
1277
1278 /*
1279 * Try to merge with the left or right extents of the range.
1280 */
1281 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_SHARED,
1282 agbno, aglen, adj, &shape_changed);
1283 if (error)
1284 goto out_error;
1285 if (shape_changed)
1286 shape_changes++;
1287 if (shape_changes)
1288 cur->bc_refc.shape_changes++;
1289
1290 /* Now that we've taken care of the ends, adjust the middle extents */
1291 error = xfs_refcount_adjust_extents(cur, agbno, aglen, adj);
1292 if (error)
1293 goto out_error;
1294
1295 return 0;
1296
1297out_error:
1298 trace_xfs_refcount_adjust_error(cur, error, _RET_IP_);
1299 return error;
1300}
1301
1302/*
1303 * Set up a continuation a deferred refcount operation by updating the intent.
1304 * Checks to make sure we're not going to run off the end of the AG.
1305 */
1306static inline int
1307xfs_refcount_continue_op(
1308 struct xfs_btree_cur *cur,
1309 struct xfs_refcount_intent *ri,
1310 xfs_agblock_t new_agbno)
1311{
1312 struct xfs_mount *mp = cur->bc_mp;
1313 struct xfs_perag *pag = to_perag(cur->bc_group);
1314
1315 if (XFS_IS_CORRUPT(mp, !xfs_verify_agbext(pag, new_agbno,
1316 ri->ri_blockcount))) {
1317 xfs_btree_mark_sick(cur);
1318 return -EFSCORRUPTED;
1319 }
1320
1321 ri->ri_startblock = xfs_agbno_to_fsb(pag, new_agbno);
1322
1323 ASSERT(xfs_verify_fsbext(mp, ri->ri_startblock, ri->ri_blockcount));
1324 ASSERT(pag_agno(pag) == XFS_FSB_TO_AGNO(mp, ri->ri_startblock));
1325
1326 return 0;
1327}
1328
1329/*
1330 * Process one of the deferred refcount operations. We pass back the
1331 * btree cursor to maintain our lock on the btree between calls.
1332 * This saves time and eliminates a buffer deadlock between the
1333 * superblock and the AGF because we'll always grab them in the same
1334 * order.
1335 */
1336int
1337xfs_refcount_finish_one(
1338 struct xfs_trans *tp,
1339 struct xfs_refcount_intent *ri,
1340 struct xfs_btree_cur **pcur)
1341{
1342 struct xfs_mount *mp = tp->t_mountp;
1343 struct xfs_btree_cur *rcur = *pcur;
1344 struct xfs_buf *agbp = NULL;
1345 int error = 0;
1346 xfs_agblock_t bno;
1347 unsigned long nr_ops = 0;
1348 int shape_changes = 0;
1349
1350 bno = XFS_FSB_TO_AGBNO(mp, ri->ri_startblock);
1351
1352 trace_xfs_refcount_deferred(mp, ri);
1353
1354 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REFCOUNT_FINISH_ONE))
1355 return -EIO;
1356
1357 /*
1358 * If we haven't gotten a cursor or the cursor AG doesn't match
1359 * the startblock, get one now.
1360 */
1361 if (rcur != NULL && rcur->bc_group != ri->ri_group) {
1362 nr_ops = rcur->bc_refc.nr_ops;
1363 shape_changes = rcur->bc_refc.shape_changes;
1364 xfs_btree_del_cursor(rcur, 0);
1365 rcur = NULL;
1366 *pcur = NULL;
1367 }
1368 if (rcur == NULL) {
1369 struct xfs_perag *pag = to_perag(ri->ri_group);
1370
1371 error = xfs_alloc_read_agf(pag, tp,
1372 XFS_ALLOC_FLAG_FREEING, &agbp);
1373 if (error)
1374 return error;
1375
1376 *pcur = rcur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1377 rcur->bc_refc.nr_ops = nr_ops;
1378 rcur->bc_refc.shape_changes = shape_changes;
1379 }
1380
1381 switch (ri->ri_type) {
1382 case XFS_REFCOUNT_INCREASE:
1383 error = xfs_refcount_adjust(rcur, &bno, &ri->ri_blockcount,
1384 XFS_REFCOUNT_ADJUST_INCREASE);
1385 if (error)
1386 return error;
1387 if (ri->ri_blockcount > 0)
1388 error = xfs_refcount_continue_op(rcur, ri, bno);
1389 break;
1390 case XFS_REFCOUNT_DECREASE:
1391 error = xfs_refcount_adjust(rcur, &bno, &ri->ri_blockcount,
1392 XFS_REFCOUNT_ADJUST_DECREASE);
1393 if (error)
1394 return error;
1395 if (ri->ri_blockcount > 0)
1396 error = xfs_refcount_continue_op(rcur, ri, bno);
1397 break;
1398 case XFS_REFCOUNT_ALLOC_COW:
1399 error = __xfs_refcount_cow_alloc(rcur, bno, ri->ri_blockcount);
1400 if (error)
1401 return error;
1402 ri->ri_blockcount = 0;
1403 break;
1404 case XFS_REFCOUNT_FREE_COW:
1405 error = __xfs_refcount_cow_free(rcur, bno, ri->ri_blockcount);
1406 if (error)
1407 return error;
1408 ri->ri_blockcount = 0;
1409 break;
1410 default:
1411 ASSERT(0);
1412 return -EFSCORRUPTED;
1413 }
1414 if (!error && ri->ri_blockcount > 0)
1415 trace_xfs_refcount_finish_one_leftover(mp, ri);
1416 return error;
1417}
1418
1419/*
1420 * Record a refcount intent for later processing.
1421 */
1422static void
1423__xfs_refcount_add(
1424 struct xfs_trans *tp,
1425 enum xfs_refcount_intent_type type,
1426 xfs_fsblock_t startblock,
1427 xfs_extlen_t blockcount)
1428{
1429 struct xfs_refcount_intent *ri;
1430
1431 ri = kmem_cache_alloc(xfs_refcount_intent_cache,
1432 GFP_KERNEL | __GFP_NOFAIL);
1433 INIT_LIST_HEAD(&ri->ri_list);
1434 ri->ri_type = type;
1435 ri->ri_startblock = startblock;
1436 ri->ri_blockcount = blockcount;
1437
1438 xfs_refcount_defer_add(tp, ri);
1439}
1440
1441/*
1442 * Increase the reference count of the blocks backing a file's extent.
1443 */
1444void
1445xfs_refcount_increase_extent(
1446 struct xfs_trans *tp,
1447 struct xfs_bmbt_irec *PREV)
1448{
1449 if (!xfs_has_reflink(tp->t_mountp))
1450 return;
1451
1452 __xfs_refcount_add(tp, XFS_REFCOUNT_INCREASE, PREV->br_startblock,
1453 PREV->br_blockcount);
1454}
1455
1456/*
1457 * Decrease the reference count of the blocks backing a file's extent.
1458 */
1459void
1460xfs_refcount_decrease_extent(
1461 struct xfs_trans *tp,
1462 struct xfs_bmbt_irec *PREV)
1463{
1464 if (!xfs_has_reflink(tp->t_mountp))
1465 return;
1466
1467 __xfs_refcount_add(tp, XFS_REFCOUNT_DECREASE, PREV->br_startblock,
1468 PREV->br_blockcount);
1469}
1470
1471/*
1472 * Given an AG extent, find the lowest-numbered run of shared blocks
1473 * within that range and return the range in fbno/flen. If
1474 * find_end_of_shared is set, return the longest contiguous extent of
1475 * shared blocks; if not, just return the first extent we find. If no
1476 * shared blocks are found, fbno and flen will be set to NULLAGBLOCK
1477 * and 0, respectively.
1478 */
1479int
1480xfs_refcount_find_shared(
1481 struct xfs_btree_cur *cur,
1482 xfs_agblock_t agbno,
1483 xfs_extlen_t aglen,
1484 xfs_agblock_t *fbno,
1485 xfs_extlen_t *flen,
1486 bool find_end_of_shared)
1487{
1488 struct xfs_refcount_irec tmp;
1489 int i;
1490 int have;
1491 int error;
1492
1493 trace_xfs_refcount_find_shared(cur, agbno, aglen);
1494
1495 /* By default, skip the whole range */
1496 *fbno = NULLAGBLOCK;
1497 *flen = 0;
1498
1499 /* Try to find a refcount extent that crosses the start */
1500 error = xfs_refcount_lookup_le(cur, XFS_REFC_DOMAIN_SHARED, agbno,
1501 &have);
1502 if (error)
1503 goto out_error;
1504 if (!have) {
1505 /* No left extent, look at the next one */
1506 error = xfs_btree_increment(cur, 0, &have);
1507 if (error)
1508 goto out_error;
1509 if (!have)
1510 goto done;
1511 }
1512 error = xfs_refcount_get_rec(cur, &tmp, &i);
1513 if (error)
1514 goto out_error;
1515 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1516 xfs_btree_mark_sick(cur);
1517 error = -EFSCORRUPTED;
1518 goto out_error;
1519 }
1520 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1521 goto done;
1522
1523 /* If the extent ends before the start, look at the next one */
1524 if (tmp.rc_startblock + tmp.rc_blockcount <= agbno) {
1525 error = xfs_btree_increment(cur, 0, &have);
1526 if (error)
1527 goto out_error;
1528 if (!have)
1529 goto done;
1530 error = xfs_refcount_get_rec(cur, &tmp, &i);
1531 if (error)
1532 goto out_error;
1533 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1534 xfs_btree_mark_sick(cur);
1535 error = -EFSCORRUPTED;
1536 goto out_error;
1537 }
1538 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED)
1539 goto done;
1540 }
1541
1542 /* If the extent starts after the range we want, bail out */
1543 if (tmp.rc_startblock >= agbno + aglen)
1544 goto done;
1545
1546 /* We found the start of a shared extent! */
1547 if (tmp.rc_startblock < agbno) {
1548 tmp.rc_blockcount -= (agbno - tmp.rc_startblock);
1549 tmp.rc_startblock = agbno;
1550 }
1551
1552 *fbno = tmp.rc_startblock;
1553 *flen = min(tmp.rc_blockcount, agbno + aglen - *fbno);
1554 if (!find_end_of_shared)
1555 goto done;
1556
1557 /* Otherwise, find the end of this shared extent */
1558 while (*fbno + *flen < agbno + aglen) {
1559 error = xfs_btree_increment(cur, 0, &have);
1560 if (error)
1561 goto out_error;
1562 if (!have)
1563 break;
1564 error = xfs_refcount_get_rec(cur, &tmp, &i);
1565 if (error)
1566 goto out_error;
1567 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1568 xfs_btree_mark_sick(cur);
1569 error = -EFSCORRUPTED;
1570 goto out_error;
1571 }
1572 if (tmp.rc_domain != XFS_REFC_DOMAIN_SHARED ||
1573 tmp.rc_startblock >= agbno + aglen ||
1574 tmp.rc_startblock != *fbno + *flen)
1575 break;
1576 *flen = min(*flen + tmp.rc_blockcount, agbno + aglen - *fbno);
1577 }
1578
1579done:
1580 trace_xfs_refcount_find_shared_result(cur, *fbno, *flen);
1581
1582out_error:
1583 if (error)
1584 trace_xfs_refcount_find_shared_error(cur, error, _RET_IP_);
1585 return error;
1586}
1587
1588/*
1589 * Recovering CoW Blocks After a Crash
1590 *
1591 * Due to the way that the copy on write mechanism works, there's a window of
1592 * opportunity in which we can lose track of allocated blocks during a crash.
1593 * Because CoW uses delayed allocation in the in-core CoW fork, writeback
1594 * causes blocks to be allocated and stored in the CoW fork. The blocks are
1595 * no longer in the free space btree but are not otherwise recorded anywhere
1596 * until the write completes and the blocks are mapped into the file. A crash
1597 * in between allocation and remapping results in the replacement blocks being
1598 * lost. This situation is exacerbated by the CoW extent size hint because
1599 * allocations can hang around for long time.
1600 *
1601 * However, there is a place where we can record these allocations before they
1602 * become mappings -- the reference count btree. The btree does not record
1603 * extents with refcount == 1, so we can record allocations with a refcount of
1604 * 1. Blocks being used for CoW writeout cannot be shared, so there should be
1605 * no conflict with shared block records. These mappings should be created
1606 * when we allocate blocks to the CoW fork and deleted when they're removed
1607 * from the CoW fork.
1608 *
1609 * Minor nit: records for in-progress CoW allocations and records for shared
1610 * extents must never be merged, to preserve the property that (except for CoW
1611 * allocations) there are no refcount btree entries with refcount == 1. The
1612 * only time this could potentially happen is when unsharing a block that's
1613 * adjacent to CoW allocations, so we must be careful to avoid this.
1614 *
1615 * At mount time we recover lost CoW allocations by searching the refcount
1616 * btree for these refcount == 1 mappings. These represent CoW allocations
1617 * that were in progress at the time the filesystem went down, so we can free
1618 * them to get the space back.
1619 *
1620 * This mechanism is superior to creating EFIs for unmapped CoW extents for
1621 * several reasons -- first, EFIs pin the tail of the log and would have to be
1622 * periodically relogged to avoid filling up the log. Second, CoW completions
1623 * will have to file an EFD and create new EFIs for whatever remains in the
1624 * CoW fork; this partially takes care of (1) but extent-size reservations
1625 * will have to periodically relog even if there's no writeout in progress.
1626 * This can happen if the CoW extent size hint is set, which you really want.
1627 * Third, EFIs cannot currently be automatically relogged into newer
1628 * transactions to advance the log tail. Fourth, stuffing the log full of
1629 * EFIs places an upper bound on the number of CoW allocations that can be
1630 * held filesystem-wide at any given time. Recording them in the refcount
1631 * btree doesn't require us to maintain any state in memory and doesn't pin
1632 * the log.
1633 */
1634/*
1635 * Adjust the refcounts of CoW allocations. These allocations are "magic"
1636 * in that they're not referenced anywhere else in the filesystem, so we
1637 * stash them in the refcount btree with a refcount of 1 until either file
1638 * remapping (or CoW cancellation) happens.
1639 */
1640STATIC int
1641xfs_refcount_adjust_cow_extents(
1642 struct xfs_btree_cur *cur,
1643 xfs_agblock_t agbno,
1644 xfs_extlen_t aglen,
1645 enum xfs_refc_adjust_op adj)
1646{
1647 struct xfs_refcount_irec ext, tmp;
1648 int error;
1649 int found_rec, found_tmp;
1650
1651 if (aglen == 0)
1652 return 0;
1653
1654 /* Find any overlapping refcount records */
1655 error = xfs_refcount_lookup_ge(cur, XFS_REFC_DOMAIN_COW, agbno,
1656 &found_rec);
1657 if (error)
1658 goto out_error;
1659 error = xfs_refcount_get_rec(cur, &ext, &found_rec);
1660 if (error)
1661 goto out_error;
1662 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec &&
1663 ext.rc_domain != XFS_REFC_DOMAIN_COW)) {
1664 xfs_btree_mark_sick(cur);
1665 error = -EFSCORRUPTED;
1666 goto out_error;
1667 }
1668 if (!found_rec) {
1669 ext.rc_startblock = cur->bc_mp->m_sb.sb_agblocks;
1670 ext.rc_blockcount = 0;
1671 ext.rc_refcount = 0;
1672 ext.rc_domain = XFS_REFC_DOMAIN_COW;
1673 }
1674
1675 switch (adj) {
1676 case XFS_REFCOUNT_ADJUST_COW_ALLOC:
1677 /* Adding a CoW reservation, there should be nothing here. */
1678 if (XFS_IS_CORRUPT(cur->bc_mp,
1679 agbno + aglen > ext.rc_startblock)) {
1680 xfs_btree_mark_sick(cur);
1681 error = -EFSCORRUPTED;
1682 goto out_error;
1683 }
1684
1685 tmp.rc_startblock = agbno;
1686 tmp.rc_blockcount = aglen;
1687 tmp.rc_refcount = 1;
1688 tmp.rc_domain = XFS_REFC_DOMAIN_COW;
1689
1690 trace_xfs_refcount_modify_extent(cur, &tmp);
1691
1692 error = xfs_refcount_insert(cur, &tmp,
1693 &found_tmp);
1694 if (error)
1695 goto out_error;
1696 if (XFS_IS_CORRUPT(cur->bc_mp, found_tmp != 1)) {
1697 xfs_btree_mark_sick(cur);
1698 error = -EFSCORRUPTED;
1699 goto out_error;
1700 }
1701 break;
1702 case XFS_REFCOUNT_ADJUST_COW_FREE:
1703 /* Removing a CoW reservation, there should be one extent. */
1704 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_startblock != agbno)) {
1705 xfs_btree_mark_sick(cur);
1706 error = -EFSCORRUPTED;
1707 goto out_error;
1708 }
1709 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_blockcount != aglen)) {
1710 xfs_btree_mark_sick(cur);
1711 error = -EFSCORRUPTED;
1712 goto out_error;
1713 }
1714 if (XFS_IS_CORRUPT(cur->bc_mp, ext.rc_refcount != 1)) {
1715 xfs_btree_mark_sick(cur);
1716 error = -EFSCORRUPTED;
1717 goto out_error;
1718 }
1719
1720 ext.rc_refcount = 0;
1721 trace_xfs_refcount_modify_extent(cur, &ext);
1722 error = xfs_refcount_delete(cur, &found_rec);
1723 if (error)
1724 goto out_error;
1725 if (XFS_IS_CORRUPT(cur->bc_mp, found_rec != 1)) {
1726 xfs_btree_mark_sick(cur);
1727 error = -EFSCORRUPTED;
1728 goto out_error;
1729 }
1730 break;
1731 default:
1732 ASSERT(0);
1733 }
1734
1735 return error;
1736out_error:
1737 trace_xfs_refcount_modify_extent_error(cur, error, _RET_IP_);
1738 return error;
1739}
1740
1741/*
1742 * Add or remove refcount btree entries for CoW reservations.
1743 */
1744STATIC int
1745xfs_refcount_adjust_cow(
1746 struct xfs_btree_cur *cur,
1747 xfs_agblock_t agbno,
1748 xfs_extlen_t aglen,
1749 enum xfs_refc_adjust_op adj)
1750{
1751 bool shape_changed;
1752 int error;
1753
1754 /*
1755 * Ensure that no rcextents cross the boundary of the adjustment range.
1756 */
1757 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1758 agbno, &shape_changed);
1759 if (error)
1760 goto out_error;
1761
1762 error = xfs_refcount_split_extent(cur, XFS_REFC_DOMAIN_COW,
1763 agbno + aglen, &shape_changed);
1764 if (error)
1765 goto out_error;
1766
1767 /*
1768 * Try to merge with the left or right extents of the range.
1769 */
1770 error = xfs_refcount_merge_extents(cur, XFS_REFC_DOMAIN_COW, &agbno,
1771 &aglen, adj, &shape_changed);
1772 if (error)
1773 goto out_error;
1774
1775 /* Now that we've taken care of the ends, adjust the middle extents */
1776 error = xfs_refcount_adjust_cow_extents(cur, agbno, aglen, adj);
1777 if (error)
1778 goto out_error;
1779
1780 return 0;
1781
1782out_error:
1783 trace_xfs_refcount_adjust_cow_error(cur, error, _RET_IP_);
1784 return error;
1785}
1786
1787/*
1788 * Record a CoW allocation in the refcount btree.
1789 */
1790STATIC int
1791__xfs_refcount_cow_alloc(
1792 struct xfs_btree_cur *rcur,
1793 xfs_agblock_t agbno,
1794 xfs_extlen_t aglen)
1795{
1796 trace_xfs_refcount_cow_increase(rcur, agbno, aglen);
1797
1798 /* Add refcount btree reservation */
1799 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1800 XFS_REFCOUNT_ADJUST_COW_ALLOC);
1801}
1802
1803/*
1804 * Remove a CoW allocation from the refcount btree.
1805 */
1806STATIC int
1807__xfs_refcount_cow_free(
1808 struct xfs_btree_cur *rcur,
1809 xfs_agblock_t agbno,
1810 xfs_extlen_t aglen)
1811{
1812 trace_xfs_refcount_cow_decrease(rcur, agbno, aglen);
1813
1814 /* Remove refcount btree reservation */
1815 return xfs_refcount_adjust_cow(rcur, agbno, aglen,
1816 XFS_REFCOUNT_ADJUST_COW_FREE);
1817}
1818
1819/* Record a CoW staging extent in the refcount btree. */
1820void
1821xfs_refcount_alloc_cow_extent(
1822 struct xfs_trans *tp,
1823 xfs_fsblock_t fsb,
1824 xfs_extlen_t len)
1825{
1826 struct xfs_mount *mp = tp->t_mountp;
1827
1828 if (!xfs_has_reflink(mp))
1829 return;
1830
1831 __xfs_refcount_add(tp, XFS_REFCOUNT_ALLOC_COW, fsb, len);
1832
1833 /* Add rmap entry */
1834 xfs_rmap_alloc_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1835 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1836}
1837
1838/* Forget a CoW staging event in the refcount btree. */
1839void
1840xfs_refcount_free_cow_extent(
1841 struct xfs_trans *tp,
1842 xfs_fsblock_t fsb,
1843 xfs_extlen_t len)
1844{
1845 struct xfs_mount *mp = tp->t_mountp;
1846
1847 if (!xfs_has_reflink(mp))
1848 return;
1849
1850 /* Remove rmap entry */
1851 xfs_rmap_free_extent(tp, XFS_FSB_TO_AGNO(mp, fsb),
1852 XFS_FSB_TO_AGBNO(mp, fsb), len, XFS_RMAP_OWN_COW);
1853 __xfs_refcount_add(tp, XFS_REFCOUNT_FREE_COW, fsb, len);
1854}
1855
1856struct xfs_refcount_recovery {
1857 struct list_head rr_list;
1858 struct xfs_refcount_irec rr_rrec;
1859};
1860
1861/* Stuff an extent on the recovery list. */
1862STATIC int
1863xfs_refcount_recover_extent(
1864 struct xfs_btree_cur *cur,
1865 const union xfs_btree_rec *rec,
1866 void *priv)
1867{
1868 struct list_head *debris = priv;
1869 struct xfs_refcount_recovery *rr;
1870
1871 if (XFS_IS_CORRUPT(cur->bc_mp,
1872 be32_to_cpu(rec->refc.rc_refcount) != 1)) {
1873 xfs_btree_mark_sick(cur);
1874 return -EFSCORRUPTED;
1875 }
1876
1877 rr = kmalloc(sizeof(struct xfs_refcount_recovery),
1878 GFP_KERNEL | __GFP_NOFAIL);
1879 INIT_LIST_HEAD(&rr->rr_list);
1880 xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec);
1881
1882 if (xfs_refcount_check_irec(to_perag(cur->bc_group), &rr->rr_rrec) !=
1883 NULL ||
1884 XFS_IS_CORRUPT(cur->bc_mp,
1885 rr->rr_rrec.rc_domain != XFS_REFC_DOMAIN_COW)) {
1886 xfs_btree_mark_sick(cur);
1887 kfree(rr);
1888 return -EFSCORRUPTED;
1889 }
1890
1891 list_add_tail(&rr->rr_list, debris);
1892 return 0;
1893}
1894
1895/* Find and remove leftover CoW reservations. */
1896int
1897xfs_refcount_recover_cow_leftovers(
1898 struct xfs_mount *mp,
1899 struct xfs_perag *pag)
1900{
1901 struct xfs_trans *tp;
1902 struct xfs_btree_cur *cur;
1903 struct xfs_buf *agbp;
1904 struct xfs_refcount_recovery *rr, *n;
1905 struct list_head debris;
1906 union xfs_btree_irec low = {
1907 .rc.rc_domain = XFS_REFC_DOMAIN_COW,
1908 };
1909 union xfs_btree_irec high = {
1910 .rc.rc_domain = XFS_REFC_DOMAIN_COW,
1911 .rc.rc_startblock = -1U,
1912 };
1913 xfs_fsblock_t fsb;
1914 int error;
1915
1916 /* reflink filesystems mustn't have AGs larger than 2^31-1 blocks */
1917 BUILD_BUG_ON(XFS_MAX_CRC_AG_BLOCKS >= XFS_REFC_COWFLAG);
1918 if (mp->m_sb.sb_agblocks > XFS_MAX_CRC_AG_BLOCKS)
1919 return -EOPNOTSUPP;
1920
1921 INIT_LIST_HEAD(&debris);
1922
1923 /*
1924 * In this first part, we use an empty transaction to gather up
1925 * all the leftover CoW extents so that we can subsequently
1926 * delete them. The empty transaction is used to avoid
1927 * a buffer lock deadlock if there happens to be a loop in the
1928 * refcountbt because we're allowed to re-grab a buffer that is
1929 * already attached to our transaction. When we're done
1930 * recording the CoW debris we cancel the (empty) transaction
1931 * and everything goes away cleanly.
1932 */
1933 error = xfs_trans_alloc_empty(mp, &tp);
1934 if (error)
1935 return error;
1936
1937 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
1938 if (error)
1939 goto out_trans;
1940 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, pag);
1941
1942 /* Find all the leftover CoW staging extents. */
1943 error = xfs_btree_query_range(cur, &low, &high,
1944 xfs_refcount_recover_extent, &debris);
1945 xfs_btree_del_cursor(cur, error);
1946 xfs_trans_brelse(tp, agbp);
1947 xfs_trans_cancel(tp);
1948 if (error)
1949 goto out_free;
1950
1951 /* Now iterate the list to free the leftovers */
1952 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1953 /* Set up transaction. */
1954 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1955 if (error)
1956 goto out_free;
1957
1958 /* Free the orphan record */
1959 fsb = xfs_agbno_to_fsb(pag, rr->rr_rrec.rc_startblock);
1960 xfs_refcount_free_cow_extent(tp, fsb,
1961 rr->rr_rrec.rc_blockcount);
1962
1963 /* Free the block. */
1964 error = xfs_free_extent_later(tp, fsb,
1965 rr->rr_rrec.rc_blockcount, NULL,
1966 XFS_AG_RESV_NONE, 0);
1967 if (error)
1968 goto out_trans;
1969
1970 error = xfs_trans_commit(tp);
1971 if (error)
1972 goto out_free;
1973
1974 list_del(&rr->rr_list);
1975 kfree(rr);
1976 }
1977
1978 return error;
1979out_trans:
1980 xfs_trans_cancel(tp);
1981out_free:
1982 /* Free the leftover list */
1983 list_for_each_entry_safe(rr, n, &debris, rr_list) {
1984 list_del(&rr->rr_list);
1985 kfree(rr);
1986 }
1987 return error;
1988}
1989
1990/*
1991 * Scan part of the keyspace of the refcount records and tell us if the area
1992 * has no records, is fully mapped by records, or is partially filled.
1993 */
1994int
1995xfs_refcount_has_records(
1996 struct xfs_btree_cur *cur,
1997 enum xfs_refc_domain domain,
1998 xfs_agblock_t bno,
1999 xfs_extlen_t len,
2000 enum xbtree_recpacking *outcome)
2001{
2002 union xfs_btree_irec low;
2003 union xfs_btree_irec high;
2004
2005 memset(&low, 0, sizeof(low));
2006 low.rc.rc_startblock = bno;
2007 memset(&high, 0xFF, sizeof(high));
2008 high.rc.rc_startblock = bno + len - 1;
2009 low.rc.rc_domain = high.rc.rc_domain = domain;
2010
2011 return xfs_btree_has_records(cur, &low, &high, NULL, outcome);
2012}
2013
2014struct xfs_refcount_query_range_info {
2015 xfs_refcount_query_range_fn fn;
2016 void *priv;
2017};
2018
2019/* Format btree record and pass to our callback. */
2020STATIC int
2021xfs_refcount_query_range_helper(
2022 struct xfs_btree_cur *cur,
2023 const union xfs_btree_rec *rec,
2024 void *priv)
2025{
2026 struct xfs_refcount_query_range_info *query = priv;
2027 struct xfs_refcount_irec irec;
2028 xfs_failaddr_t fa;
2029
2030 xfs_refcount_btrec_to_irec(rec, &irec);
2031 fa = xfs_refcount_check_irec(to_perag(cur->bc_group), &irec);
2032 if (fa)
2033 return xfs_refcount_complain_bad_rec(cur, fa, &irec);
2034
2035 return query->fn(cur, &irec, query->priv);
2036}
2037
2038/* Find all refcount records between two keys. */
2039int
2040xfs_refcount_query_range(
2041 struct xfs_btree_cur *cur,
2042 const struct xfs_refcount_irec *low_rec,
2043 const struct xfs_refcount_irec *high_rec,
2044 xfs_refcount_query_range_fn fn,
2045 void *priv)
2046{
2047 union xfs_btree_irec low_brec = { .rc = *low_rec };
2048 union xfs_btree_irec high_brec = { .rc = *high_rec };
2049 struct xfs_refcount_query_range_info query = { .priv = priv, .fn = fn };
2050
2051 return xfs_btree_query_range(cur, &low_brec, &high_brec,
2052 xfs_refcount_query_range_helper, &query);
2053}
2054
2055int __init
2056xfs_refcount_intent_init_cache(void)
2057{
2058 xfs_refcount_intent_cache = kmem_cache_create("xfs_refc_intent",
2059 sizeof(struct xfs_refcount_intent),
2060 0, 0, NULL);
2061
2062 return xfs_refcount_intent_cache != NULL ? 0 : -ENOMEM;
2063}
2064
2065void
2066xfs_refcount_intent_destroy_cache(void)
2067{
2068 kmem_cache_destroy(xfs_refcount_intent_cache);
2069 xfs_refcount_intent_cache = NULL;
2070}