1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Red Hat, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_trans.h"
14 #include "xfs_alloc.h"
15 #include "xfs_btree.h"
16 #include "xfs_btree_staging.h"
18 #include "xfs_rmap_btree.h"
19 #include "xfs_trace.h"
20 #include "xfs_error.h"
21 #include "xfs_extent_busy.h"
23 #include "xfs_ag_resv.h"
28 * This is a per-ag tree used to track the owner(s) of a given extent. With
29 * reflink it is possible for there to be multiple owners, which is a departure
30 * from classic XFS. Owner records for data extents are inserted when the
31 * extent is mapped and removed when an extent is unmapped. Owner records for
32 * all other block types (i.e. metadata) are inserted when an extent is
33 * allocated and removed when an extent is freed. There can only be one owner
34 * of a metadata extent, usually an inode or some other metadata structure like
37 * The rmap btree is part of the free space management, so blocks for the tree
38 * are sourced from the agfl. Hence we need transaction reservation support for
39 * this tree so that the freelist is always large enough. This also impacts on
40 * the minimum space we need to leave free in the AG.
42 * The tree is ordered by [ag block, owner, offset]. This is a large key size,
43 * but it is the only way to enforce unique keys when a block can be owned by
44 * multiple files at any offset. There's no need to order/search by extent
45 * size for online updating/management of the tree. It is intended that most
46 * reverse lookups will be to find the owner(s) of a particular block, or to
47 * try to recover tree and file data from corrupt primary metadata.
50 static struct xfs_btree_cur *
51 xfs_rmapbt_dup_cursor(
52 struct xfs_btree_cur *cur)
54 return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
55 cur->bc_ag.agbp, cur->bc_ag.pag);
60 struct xfs_btree_cur *cur,
61 const union xfs_btree_ptr *ptr,
64 struct xfs_buf *agbp = cur->bc_ag.agbp;
65 struct xfs_agf *agf = agbp->b_addr;
66 int btnum = cur->bc_btnum;
70 agf->agf_roots[btnum] = ptr->s;
71 be32_add_cpu(&agf->agf_levels[btnum], inc);
72 cur->bc_ag.pag->pagf_levels[btnum] += inc;
74 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
78 xfs_rmapbt_alloc_block(
79 struct xfs_btree_cur *cur,
80 const union xfs_btree_ptr *start,
81 union xfs_btree_ptr *new,
84 struct xfs_buf *agbp = cur->bc_ag.agbp;
85 struct xfs_agf *agf = agbp->b_addr;
86 struct xfs_perag *pag = cur->bc_ag.pag;
90 /* Allocate the new block from the freelist. If we can't, give up. */
91 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_ag.agbp,
96 trace_xfs_rmapbt_alloc_block(cur->bc_mp, pag->pag_agno, bno, 1);
97 if (bno == NULLAGBLOCK) {
102 xfs_extent_busy_reuse(cur->bc_mp, pag, bno, 1, false);
104 new->s = cpu_to_be32(bno);
105 be32_add_cpu(&agf->agf_rmap_blocks, 1);
106 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
108 xfs_ag_resv_rmapbt_alloc(cur->bc_mp, pag->pag_agno);
115 xfs_rmapbt_free_block(
116 struct xfs_btree_cur *cur,
119 struct xfs_buf *agbp = cur->bc_ag.agbp;
120 struct xfs_agf *agf = agbp->b_addr;
121 struct xfs_perag *pag = cur->bc_ag.pag;
125 bno = xfs_daddr_to_agbno(cur->bc_mp, xfs_buf_daddr(bp));
126 trace_xfs_rmapbt_free_block(cur->bc_mp, pag->pag_agno,
128 be32_add_cpu(&agf->agf_rmap_blocks, -1);
129 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
130 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
134 xfs_extent_busy_insert(cur->bc_tp, pag, bno, 1,
135 XFS_EXTENT_BUSY_SKIP_DISCARD);
137 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_RMAPBT, NULL, 1);
142 xfs_rmapbt_get_minrecs(
143 struct xfs_btree_cur *cur,
146 return cur->bc_mp->m_rmap_mnr[level != 0];
150 xfs_rmapbt_get_maxrecs(
151 struct xfs_btree_cur *cur,
154 return cur->bc_mp->m_rmap_mxr[level != 0];
158 xfs_rmapbt_init_key_from_rec(
159 union xfs_btree_key *key,
160 const union xfs_btree_rec *rec)
162 key->rmap.rm_startblock = rec->rmap.rm_startblock;
163 key->rmap.rm_owner = rec->rmap.rm_owner;
164 key->rmap.rm_offset = rec->rmap.rm_offset;
168 * The high key for a reverse mapping record can be computed by shifting
169 * the startblock and offset to the highest value that would still map
170 * to that record. In practice this means that we add blockcount-1 to
171 * the startblock for all records, and if the record is for a data/attr
172 * fork mapping, we add blockcount-1 to the offset too.
175 xfs_rmapbt_init_high_key_from_rec(
176 union xfs_btree_key *key,
177 const union xfs_btree_rec *rec)
182 adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
184 key->rmap.rm_startblock = rec->rmap.rm_startblock;
185 be32_add_cpu(&key->rmap.rm_startblock, adj);
186 key->rmap.rm_owner = rec->rmap.rm_owner;
187 key->rmap.rm_offset = rec->rmap.rm_offset;
188 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
189 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
191 off = be64_to_cpu(key->rmap.rm_offset);
192 off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
193 key->rmap.rm_offset = cpu_to_be64(off);
197 xfs_rmapbt_init_rec_from_cur(
198 struct xfs_btree_cur *cur,
199 union xfs_btree_rec *rec)
201 rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
202 rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
203 rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
204 rec->rmap.rm_offset = cpu_to_be64(
205 xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
209 xfs_rmapbt_init_ptr_from_cur(
210 struct xfs_btree_cur *cur,
211 union xfs_btree_ptr *ptr)
213 struct xfs_agf *agf = cur->bc_ag.agbp->b_addr;
215 ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno));
217 ptr->s = agf->agf_roots[cur->bc_btnum];
222 struct xfs_btree_cur *cur,
223 const union xfs_btree_key *key)
225 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
226 const struct xfs_rmap_key *kp = &key->rmap;
230 d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
234 x = be64_to_cpu(kp->rm_owner);
241 x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
251 xfs_rmapbt_diff_two_keys(
252 struct xfs_btree_cur *cur,
253 const union xfs_btree_key *k1,
254 const union xfs_btree_key *k2)
256 const struct xfs_rmap_key *kp1 = &k1->rmap;
257 const struct xfs_rmap_key *kp2 = &k2->rmap;
261 d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
262 be32_to_cpu(kp2->rm_startblock);
266 x = be64_to_cpu(kp1->rm_owner);
267 y = be64_to_cpu(kp2->rm_owner);
273 x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
274 y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
282 static xfs_failaddr_t
286 struct xfs_mount *mp = bp->b_mount;
287 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
288 struct xfs_perag *pag = bp->b_pag;
293 * magic number and level verification
295 * During growfs operations, we can't verify the exact level or owner as
296 * the perag is not fully initialised and hence not attached to the
297 * buffer. In this case, check against the maximum tree depth.
299 * Similarly, during log recovery we will have a perag structure
300 * attached, but the agf information will not yet have been initialised
301 * from the on disk AGF. Again, we can only check against maximum limits
304 if (!xfs_verify_magic(bp, block->bb_magic))
305 return __this_address;
307 if (!xfs_has_rmapbt(mp))
308 return __this_address;
309 fa = xfs_btree_sblock_v5hdr_verify(bp);
313 level = be16_to_cpu(block->bb_level);
314 if (pag && pag->pagf_init) {
315 if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
316 return __this_address;
317 } else if (level >= mp->m_rmap_maxlevels)
318 return __this_address;
320 return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
324 xfs_rmapbt_read_verify(
329 if (!xfs_btree_sblock_verify_crc(bp))
330 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
332 fa = xfs_rmapbt_verify(bp);
334 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
338 trace_xfs_btree_corrupt(bp, _RET_IP_);
342 xfs_rmapbt_write_verify(
347 fa = xfs_rmapbt_verify(bp);
349 trace_xfs_btree_corrupt(bp, _RET_IP_);
350 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
353 xfs_btree_sblock_calc_crc(bp);
357 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
358 .name = "xfs_rmapbt",
359 .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
360 .verify_read = xfs_rmapbt_read_verify,
361 .verify_write = xfs_rmapbt_write_verify,
362 .verify_struct = xfs_rmapbt_verify,
366 xfs_rmapbt_keys_inorder(
367 struct xfs_btree_cur *cur,
368 const union xfs_btree_key *k1,
369 const union xfs_btree_key *k2)
376 x = be32_to_cpu(k1->rmap.rm_startblock);
377 y = be32_to_cpu(k2->rmap.rm_startblock);
382 a = be64_to_cpu(k1->rmap.rm_owner);
383 b = be64_to_cpu(k2->rmap.rm_owner);
388 a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
389 b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
396 xfs_rmapbt_recs_inorder(
397 struct xfs_btree_cur *cur,
398 const union xfs_btree_rec *r1,
399 const union xfs_btree_rec *r2)
406 x = be32_to_cpu(r1->rmap.rm_startblock);
407 y = be32_to_cpu(r2->rmap.rm_startblock);
412 a = be64_to_cpu(r1->rmap.rm_owner);
413 b = be64_to_cpu(r2->rmap.rm_owner);
418 a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
419 b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
425 static const struct xfs_btree_ops xfs_rmapbt_ops = {
426 .rec_len = sizeof(struct xfs_rmap_rec),
427 .key_len = 2 * sizeof(struct xfs_rmap_key),
429 .dup_cursor = xfs_rmapbt_dup_cursor,
430 .set_root = xfs_rmapbt_set_root,
431 .alloc_block = xfs_rmapbt_alloc_block,
432 .free_block = xfs_rmapbt_free_block,
433 .get_minrecs = xfs_rmapbt_get_minrecs,
434 .get_maxrecs = xfs_rmapbt_get_maxrecs,
435 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
436 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
437 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
438 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
439 .key_diff = xfs_rmapbt_key_diff,
440 .buf_ops = &xfs_rmapbt_buf_ops,
441 .diff_two_keys = xfs_rmapbt_diff_two_keys,
442 .keys_inorder = xfs_rmapbt_keys_inorder,
443 .recs_inorder = xfs_rmapbt_recs_inorder,
446 static struct xfs_btree_cur *
447 xfs_rmapbt_init_common(
448 struct xfs_mount *mp,
449 struct xfs_trans *tp,
450 struct xfs_perag *pag)
452 struct xfs_btree_cur *cur;
454 cur = kmem_cache_zalloc(xfs_btree_cur_zone, GFP_NOFS | __GFP_NOFAIL);
457 /* Overlapping btree; 2 keys per pointer. */
458 cur->bc_btnum = XFS_BTNUM_RMAP;
459 cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
460 cur->bc_blocklog = mp->m_sb.sb_blocklog;
461 cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
462 cur->bc_ops = &xfs_rmapbt_ops;
464 /* take a reference for the cursor */
465 atomic_inc(&pag->pag_ref);
466 cur->bc_ag.pag = pag;
471 /* Create a new reverse mapping btree cursor. */
472 struct xfs_btree_cur *
473 xfs_rmapbt_init_cursor(
474 struct xfs_mount *mp,
475 struct xfs_trans *tp,
476 struct xfs_buf *agbp,
477 struct xfs_perag *pag)
479 struct xfs_agf *agf = agbp->b_addr;
480 struct xfs_btree_cur *cur;
482 cur = xfs_rmapbt_init_common(mp, tp, pag);
483 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
484 cur->bc_ag.agbp = agbp;
488 /* Create a new reverse mapping btree cursor with a fake root for staging. */
489 struct xfs_btree_cur *
490 xfs_rmapbt_stage_cursor(
491 struct xfs_mount *mp,
492 struct xbtree_afakeroot *afake,
493 struct xfs_perag *pag)
495 struct xfs_btree_cur *cur;
497 cur = xfs_rmapbt_init_common(mp, NULL, pag);
498 xfs_btree_stage_afakeroot(cur, afake);
503 * Install a new reverse mapping btree root. Caller is responsible for
504 * invalidating and freeing the old btree blocks.
507 xfs_rmapbt_commit_staged_btree(
508 struct xfs_btree_cur *cur,
509 struct xfs_trans *tp,
510 struct xfs_buf *agbp)
512 struct xfs_agf *agf = agbp->b_addr;
513 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
515 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
517 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(afake->af_root);
518 agf->agf_levels[cur->bc_btnum] = cpu_to_be32(afake->af_levels);
519 agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
520 xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
521 XFS_AGF_RMAP_BLOCKS);
522 xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_rmapbt_ops);
526 * Calculate number of records in an rmap btree block.
533 blocklen -= XFS_RMAP_BLOCK_LEN;
536 return blocklen / sizeof(struct xfs_rmap_rec);
538 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
541 /* Compute the maximum height of an rmap btree. */
543 xfs_rmapbt_compute_maxlevels(
544 struct xfs_mount *mp)
547 * On a non-reflink filesystem, the maximum number of rmap
548 * records is the number of blocks in the AG, hence the max
549 * rmapbt height is log_$maxrecs($agblocks). However, with
550 * reflink each AG block can have up to 2^32 (per the refcount
551 * record format) owners, which means that theoretically we
552 * could face up to 2^64 rmap records.
554 * That effectively means that the max rmapbt height must be
555 * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
556 * blocks to feed the rmapbt long before the rmapbt reaches
557 * maximum height. The reflink code uses ag_resv_critical to
558 * disallow reflinking when less than 10% of the per-AG metadata
559 * block reservation since the fallback is a regular file copy.
561 if (xfs_has_reflink(mp))
562 mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS;
564 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
565 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
568 /* Calculate the refcount btree size for some records. */
570 xfs_rmapbt_calc_size(
571 struct xfs_mount *mp,
572 unsigned long long len)
574 return xfs_btree_calc_size(mp->m_rmap_mnr, len);
578 * Calculate the maximum refcount btree size.
582 struct xfs_mount *mp,
583 xfs_agblock_t agblocks)
585 /* Bail out if we're uninitialized, which can happen in mkfs. */
586 if (mp->m_rmap_mxr[0] == 0)
589 return xfs_rmapbt_calc_size(mp, agblocks);
593 * Figure out how many blocks to reserve and how many are used by this btree.
596 xfs_rmapbt_calc_reserves(
597 struct xfs_mount *mp,
598 struct xfs_trans *tp,
599 struct xfs_perag *pag,
603 struct xfs_buf *agbp;
605 xfs_agblock_t agblocks;
606 xfs_extlen_t tree_len;
609 if (!xfs_has_rmapbt(mp))
612 error = xfs_alloc_read_agf(mp, tp, pag->pag_agno, 0, &agbp);
617 agblocks = be32_to_cpu(agf->agf_length);
618 tree_len = be32_to_cpu(agf->agf_rmap_blocks);
619 xfs_trans_brelse(tp, agbp);
622 * The log is permanently allocated, so the space it occupies will
623 * never be available for the kinds of things that would require btree
624 * expansion. We therefore can pretend the space isn't there.
626 if (mp->m_sb.sb_logstart &&
627 XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == pag->pag_agno)
628 agblocks -= mp->m_sb.sb_logblocks;
630 /* Reserve 1% of the AG or enough for 1 block per record. */
631 *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));