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