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