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_health.h"
20 #include "xfs_trace.h"
21 #include "xfs_error.h"
22 #include "xfs_extent_busy.h"
24 #include "xfs_ag_resv.h"
25 #include "xfs_buf_mem.h"
26 #include "xfs_btree_mem.h"
28 static struct kmem_cache *xfs_rmapbt_cur_cache;
33 * This is a per-ag tree used to track the owner(s) of a given extent. With
34 * reflink it is possible for there to be multiple owners, which is a departure
35 * from classic XFS. Owner records for data extents are inserted when the
36 * extent is mapped and removed when an extent is unmapped. Owner records for
37 * all other block types (i.e. metadata) are inserted when an extent is
38 * allocated and removed when an extent is freed. There can only be one owner
39 * of a metadata extent, usually an inode or some other metadata structure like
42 * The rmap btree is part of the free space management, so blocks for the tree
43 * are sourced from the agfl. Hence we need transaction reservation support for
44 * this tree so that the freelist is always large enough. This also impacts on
45 * the minimum space we need to leave free in the AG.
47 * The tree is ordered by [ag block, owner, offset]. This is a large key size,
48 * but it is the only way to enforce unique keys when a block can be owned by
49 * multiple files at any offset. There's no need to order/search by extent
50 * size for online updating/management of the tree. It is intended that most
51 * reverse lookups will be to find the owner(s) of a particular block, or to
52 * try to recover tree and file data from corrupt primary metadata.
55 static struct xfs_btree_cur *
56 xfs_rmapbt_dup_cursor(
57 struct xfs_btree_cur *cur)
59 return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
60 cur->bc_ag.agbp, cur->bc_ag.pag);
65 struct xfs_btree_cur *cur,
66 const union xfs_btree_ptr *ptr,
69 struct xfs_buf *agbp = cur->bc_ag.agbp;
70 struct xfs_agf *agf = agbp->b_addr;
74 agf->agf_rmap_root = ptr->s;
75 be32_add_cpu(&agf->agf_rmap_level, inc);
76 cur->bc_ag.pag->pagf_rmap_level += inc;
78 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
82 xfs_rmapbt_alloc_block(
83 struct xfs_btree_cur *cur,
84 const union xfs_btree_ptr *start,
85 union xfs_btree_ptr *new,
88 struct xfs_buf *agbp = cur->bc_ag.agbp;
89 struct xfs_agf *agf = agbp->b_addr;
90 struct xfs_perag *pag = cur->bc_ag.pag;
94 /* Allocate the new block from the freelist. If we can't, give up. */
95 error = xfs_alloc_get_freelist(pag, cur->bc_tp, cur->bc_ag.agbp,
99 if (bno == NULLAGBLOCK) {
104 xfs_extent_busy_reuse(cur->bc_mp, pag, bno, 1, false);
106 new->s = cpu_to_be32(bno);
107 be32_add_cpu(&agf->agf_rmap_blocks, 1);
108 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
110 xfs_ag_resv_rmapbt_alloc(cur->bc_mp, pag->pag_agno);
117 xfs_rmapbt_free_block(
118 struct xfs_btree_cur *cur,
121 struct xfs_buf *agbp = cur->bc_ag.agbp;
122 struct xfs_agf *agf = agbp->b_addr;
123 struct xfs_perag *pag = cur->bc_ag.pag;
127 bno = xfs_daddr_to_agbno(cur->bc_mp, xfs_buf_daddr(bp));
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(pag, 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 * Convert the ondisk record's offset field into the ondisk key's offset field.
159 * Fork and bmbt are significant parts of the rmap record key, but written
160 * status is merely a record attribute.
162 static inline __be64 ondisk_rec_offset_to_key(const union xfs_btree_rec *rec)
164 return rec->rmap.rm_offset & ~cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
168 xfs_rmapbt_init_key_from_rec(
169 union xfs_btree_key *key,
170 const union xfs_btree_rec *rec)
172 key->rmap.rm_startblock = rec->rmap.rm_startblock;
173 key->rmap.rm_owner = rec->rmap.rm_owner;
174 key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
178 * The high key for a reverse mapping record can be computed by shifting
179 * the startblock and offset to the highest value that would still map
180 * to that record. In practice this means that we add blockcount-1 to
181 * the startblock for all records, and if the record is for a data/attr
182 * fork mapping, we add blockcount-1 to the offset too.
185 xfs_rmapbt_init_high_key_from_rec(
186 union xfs_btree_key *key,
187 const union xfs_btree_rec *rec)
192 adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
194 key->rmap.rm_startblock = rec->rmap.rm_startblock;
195 be32_add_cpu(&key->rmap.rm_startblock, adj);
196 key->rmap.rm_owner = rec->rmap.rm_owner;
197 key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
198 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
199 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
201 off = be64_to_cpu(key->rmap.rm_offset);
202 off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
203 key->rmap.rm_offset = cpu_to_be64(off);
207 xfs_rmapbt_init_rec_from_cur(
208 struct xfs_btree_cur *cur,
209 union xfs_btree_rec *rec)
211 rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
212 rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
213 rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
214 rec->rmap.rm_offset = cpu_to_be64(
215 xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
219 xfs_rmapbt_init_ptr_from_cur(
220 struct xfs_btree_cur *cur,
221 union xfs_btree_ptr *ptr)
223 struct xfs_agf *agf = cur->bc_ag.agbp->b_addr;
225 ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agf->agf_seqno));
227 ptr->s = agf->agf_rmap_root;
231 * Mask the appropriate parts of the ondisk key field for a key comparison.
232 * Fork and bmbt are significant parts of the rmap record key, but written
233 * status is merely a record attribute.
235 static inline uint64_t offset_keymask(uint64_t offset)
237 return offset & ~XFS_RMAP_OFF_UNWRITTEN;
242 struct xfs_btree_cur *cur,
243 const union xfs_btree_key *key)
245 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
246 const struct xfs_rmap_key *kp = &key->rmap;
250 d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
254 x = be64_to_cpu(kp->rm_owner);
261 x = offset_keymask(be64_to_cpu(kp->rm_offset));
262 y = offset_keymask(xfs_rmap_irec_offset_pack(rec));
271 xfs_rmapbt_diff_two_keys(
272 struct xfs_btree_cur *cur,
273 const union xfs_btree_key *k1,
274 const union xfs_btree_key *k2,
275 const union xfs_btree_key *mask)
277 const struct xfs_rmap_key *kp1 = &k1->rmap;
278 const struct xfs_rmap_key *kp2 = &k2->rmap;
282 /* Doesn't make sense to mask off the physical space part */
283 ASSERT(!mask || mask->rmap.rm_startblock);
285 d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
286 be32_to_cpu(kp2->rm_startblock);
290 if (!mask || mask->rmap.rm_owner) {
291 x = be64_to_cpu(kp1->rm_owner);
292 y = be64_to_cpu(kp2->rm_owner);
299 if (!mask || mask->rmap.rm_offset) {
300 /* Doesn't make sense to allow offset but not owner */
301 ASSERT(!mask || mask->rmap.rm_owner);
303 x = offset_keymask(be64_to_cpu(kp1->rm_offset));
304 y = offset_keymask(be64_to_cpu(kp2->rm_offset));
314 static xfs_failaddr_t
318 struct xfs_mount *mp = bp->b_mount;
319 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
320 struct xfs_perag *pag = bp->b_pag;
325 * magic number and level verification
327 * During growfs operations, we can't verify the exact level or owner as
328 * the perag is not fully initialised and hence not attached to the
329 * buffer. In this case, check against the maximum tree depth.
331 * Similarly, during log recovery we will have a perag structure
332 * attached, but the agf information will not yet have been initialised
333 * from the on disk AGF. Again, we can only check against maximum limits
336 if (!xfs_verify_magic(bp, block->bb_magic))
337 return __this_address;
339 if (!xfs_has_rmapbt(mp))
340 return __this_address;
341 fa = xfs_btree_agblock_v5hdr_verify(bp);
345 level = be16_to_cpu(block->bb_level);
346 if (pag && xfs_perag_initialised_agf(pag)) {
347 unsigned int maxlevel = pag->pagf_rmap_level;
349 #ifdef CONFIG_XFS_ONLINE_REPAIR
351 * Online repair could be rewriting the free space btrees, so
352 * we'll validate against the larger of either tree while this
355 maxlevel = max_t(unsigned int, maxlevel,
356 pag->pagf_repair_rmap_level);
358 if (level >= maxlevel)
359 return __this_address;
360 } else if (level >= mp->m_rmap_maxlevels)
361 return __this_address;
363 return xfs_btree_agblock_verify(bp, mp->m_rmap_mxr[level != 0]);
367 xfs_rmapbt_read_verify(
372 if (!xfs_btree_agblock_verify_crc(bp))
373 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
375 fa = xfs_rmapbt_verify(bp);
377 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
381 trace_xfs_btree_corrupt(bp, _RET_IP_);
385 xfs_rmapbt_write_verify(
390 fa = xfs_rmapbt_verify(bp);
392 trace_xfs_btree_corrupt(bp, _RET_IP_);
393 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
396 xfs_btree_agblock_calc_crc(bp);
400 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
401 .name = "xfs_rmapbt",
402 .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
403 .verify_read = xfs_rmapbt_read_verify,
404 .verify_write = xfs_rmapbt_write_verify,
405 .verify_struct = xfs_rmapbt_verify,
409 xfs_rmapbt_keys_inorder(
410 struct xfs_btree_cur *cur,
411 const union xfs_btree_key *k1,
412 const union xfs_btree_key *k2)
419 x = be32_to_cpu(k1->rmap.rm_startblock);
420 y = be32_to_cpu(k2->rmap.rm_startblock);
425 a = be64_to_cpu(k1->rmap.rm_owner);
426 b = be64_to_cpu(k2->rmap.rm_owner);
431 a = offset_keymask(be64_to_cpu(k1->rmap.rm_offset));
432 b = offset_keymask(be64_to_cpu(k2->rmap.rm_offset));
439 xfs_rmapbt_recs_inorder(
440 struct xfs_btree_cur *cur,
441 const union xfs_btree_rec *r1,
442 const union xfs_btree_rec *r2)
449 x = be32_to_cpu(r1->rmap.rm_startblock);
450 y = be32_to_cpu(r2->rmap.rm_startblock);
455 a = be64_to_cpu(r1->rmap.rm_owner);
456 b = be64_to_cpu(r2->rmap.rm_owner);
461 a = offset_keymask(be64_to_cpu(r1->rmap.rm_offset));
462 b = offset_keymask(be64_to_cpu(r2->rmap.rm_offset));
468 STATIC enum xbtree_key_contig
469 xfs_rmapbt_keys_contiguous(
470 struct xfs_btree_cur *cur,
471 const union xfs_btree_key *key1,
472 const union xfs_btree_key *key2,
473 const union xfs_btree_key *mask)
475 ASSERT(!mask || mask->rmap.rm_startblock);
478 * We only support checking contiguity of the physical space component.
479 * If any callers ever need more specificity than that, they'll have to
482 ASSERT(!mask || (!mask->rmap.rm_owner && !mask->rmap.rm_offset));
484 return xbtree_key_contig(be32_to_cpu(key1->rmap.rm_startblock),
485 be32_to_cpu(key2->rmap.rm_startblock));
488 const struct xfs_btree_ops xfs_rmapbt_ops = {
490 .type = XFS_BTREE_TYPE_AG,
491 .geom_flags = XFS_BTGEO_OVERLAPPING,
493 .rec_len = sizeof(struct xfs_rmap_rec),
494 /* Overlapping btree; 2 keys per pointer. */
495 .key_len = 2 * sizeof(struct xfs_rmap_key),
496 .ptr_len = XFS_BTREE_SHORT_PTR_LEN,
498 .lru_refs = XFS_RMAP_BTREE_REF,
499 .statoff = XFS_STATS_CALC_INDEX(xs_rmap_2),
500 .sick_mask = XFS_SICK_AG_RMAPBT,
502 .dup_cursor = xfs_rmapbt_dup_cursor,
503 .set_root = xfs_rmapbt_set_root,
504 .alloc_block = xfs_rmapbt_alloc_block,
505 .free_block = xfs_rmapbt_free_block,
506 .get_minrecs = xfs_rmapbt_get_minrecs,
507 .get_maxrecs = xfs_rmapbt_get_maxrecs,
508 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
509 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
510 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
511 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
512 .key_diff = xfs_rmapbt_key_diff,
513 .buf_ops = &xfs_rmapbt_buf_ops,
514 .diff_two_keys = xfs_rmapbt_diff_two_keys,
515 .keys_inorder = xfs_rmapbt_keys_inorder,
516 .recs_inorder = xfs_rmapbt_recs_inorder,
517 .keys_contiguous = xfs_rmapbt_keys_contiguous,
521 * Create a new reverse mapping btree cursor.
523 * For staging cursors tp and agbp are NULL.
525 struct xfs_btree_cur *
526 xfs_rmapbt_init_cursor(
527 struct xfs_mount *mp,
528 struct xfs_trans *tp,
529 struct xfs_buf *agbp,
530 struct xfs_perag *pag)
532 struct xfs_btree_cur *cur;
534 cur = xfs_btree_alloc_cursor(mp, tp, &xfs_rmapbt_ops,
535 mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
536 cur->bc_ag.pag = xfs_perag_hold(pag);
537 cur->bc_ag.agbp = agbp;
539 struct xfs_agf *agf = agbp->b_addr;
541 cur->bc_nlevels = be32_to_cpu(agf->agf_rmap_level);
546 #ifdef CONFIG_XFS_BTREE_IN_MEM
547 static inline unsigned int
548 xfs_rmapbt_mem_block_maxrecs(
549 unsigned int blocklen,
553 return blocklen / sizeof(struct xfs_rmap_rec);
555 (2 * sizeof(struct xfs_rmap_key) + sizeof(__be64));
559 * Validate an in-memory rmap btree block. Callers are allowed to generate an
560 * in-memory btree even if the ondisk feature is not enabled.
562 static xfs_failaddr_t
563 xfs_rmapbt_mem_verify(
566 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
569 unsigned int maxrecs;
571 if (!xfs_verify_magic(bp, block->bb_magic))
572 return __this_address;
574 fa = xfs_btree_fsblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
578 level = be16_to_cpu(block->bb_level);
579 if (level >= xfs_rmapbt_maxlevels_ondisk())
580 return __this_address;
582 maxrecs = xfs_rmapbt_mem_block_maxrecs(
583 XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN, level == 0);
584 return xfs_btree_memblock_verify(bp, maxrecs);
588 xfs_rmapbt_mem_rw_verify(
591 xfs_failaddr_t fa = xfs_rmapbt_mem_verify(bp);
594 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
597 /* skip crc checks on in-memory btrees to save time */
598 static const struct xfs_buf_ops xfs_rmapbt_mem_buf_ops = {
599 .name = "xfs_rmapbt_mem",
600 .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
601 .verify_read = xfs_rmapbt_mem_rw_verify,
602 .verify_write = xfs_rmapbt_mem_rw_verify,
603 .verify_struct = xfs_rmapbt_mem_verify,
606 const struct xfs_btree_ops xfs_rmapbt_mem_ops = {
608 .type = XFS_BTREE_TYPE_MEM,
609 .geom_flags = XFS_BTGEO_OVERLAPPING,
611 .rec_len = sizeof(struct xfs_rmap_rec),
612 /* Overlapping btree; 2 keys per pointer. */
613 .key_len = 2 * sizeof(struct xfs_rmap_key),
614 .ptr_len = XFS_BTREE_LONG_PTR_LEN,
616 .lru_refs = XFS_RMAP_BTREE_REF,
617 .statoff = XFS_STATS_CALC_INDEX(xs_rmap_mem_2),
619 .dup_cursor = xfbtree_dup_cursor,
620 .set_root = xfbtree_set_root,
621 .alloc_block = xfbtree_alloc_block,
622 .free_block = xfbtree_free_block,
623 .get_minrecs = xfbtree_get_minrecs,
624 .get_maxrecs = xfbtree_get_maxrecs,
625 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
626 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
627 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
628 .init_ptr_from_cur = xfbtree_init_ptr_from_cur,
629 .key_diff = xfs_rmapbt_key_diff,
630 .buf_ops = &xfs_rmapbt_mem_buf_ops,
631 .diff_two_keys = xfs_rmapbt_diff_two_keys,
632 .keys_inorder = xfs_rmapbt_keys_inorder,
633 .recs_inorder = xfs_rmapbt_recs_inorder,
634 .keys_contiguous = xfs_rmapbt_keys_contiguous,
637 /* Create a cursor for an in-memory btree. */
638 struct xfs_btree_cur *
639 xfs_rmapbt_mem_cursor(
640 struct xfs_perag *pag,
641 struct xfs_trans *tp,
642 struct xfbtree *xfbt)
644 struct xfs_btree_cur *cur;
645 struct xfs_mount *mp = pag->pag_mount;
647 cur = xfs_btree_alloc_cursor(mp, tp, &xfs_rmapbt_mem_ops,
648 xfs_rmapbt_maxlevels_ondisk(), xfs_rmapbt_cur_cache);
649 cur->bc_mem.xfbtree = xfbt;
650 cur->bc_nlevels = xfbt->nlevels;
652 cur->bc_mem.pag = xfs_perag_hold(pag);
656 /* Create an in-memory rmap btree. */
659 struct xfs_mount *mp,
660 struct xfbtree *xfbt,
661 struct xfs_buftarg *btp,
665 return xfbtree_init(mp, xfbt, btp, &xfs_rmapbt_mem_ops);
668 /* Compute the max possible height for reverse mapping btrees in memory. */
670 xfs_rmapbt_mem_maxlevels(void)
672 unsigned int minrecs[2];
673 unsigned int blocklen;
675 blocklen = XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN;
677 minrecs[0] = xfs_rmapbt_mem_block_maxrecs(blocklen, true) / 2;
678 minrecs[1] = xfs_rmapbt_mem_block_maxrecs(blocklen, false) / 2;
681 * How tall can an in-memory rmap btree become if we filled the entire
682 * AG with rmap records?
684 return xfs_btree_compute_maxlevels(minrecs,
685 XFS_MAX_AG_BYTES / sizeof(struct xfs_rmap_rec));
688 # define xfs_rmapbt_mem_maxlevels() (0)
689 #endif /* CONFIG_XFS_BTREE_IN_MEM */
692 * Install a new reverse mapping btree root. Caller is responsible for
693 * invalidating and freeing the old btree blocks.
696 xfs_rmapbt_commit_staged_btree(
697 struct xfs_btree_cur *cur,
698 struct xfs_trans *tp,
699 struct xfs_buf *agbp)
701 struct xfs_agf *agf = agbp->b_addr;
702 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
704 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
706 agf->agf_rmap_root = cpu_to_be32(afake->af_root);
707 agf->agf_rmap_level = cpu_to_be32(afake->af_levels);
708 agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
709 xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
710 XFS_AGF_RMAP_BLOCKS);
711 xfs_btree_commit_afakeroot(cur, tp, agbp);
714 /* Calculate number of records in a reverse mapping btree block. */
715 static inline unsigned int
716 xfs_rmapbt_block_maxrecs(
717 unsigned int blocklen,
721 return blocklen / sizeof(struct xfs_rmap_rec);
723 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
727 * Calculate number of records in an rmap btree block.
734 blocklen -= XFS_RMAP_BLOCK_LEN;
735 return xfs_rmapbt_block_maxrecs(blocklen, leaf);
738 /* Compute the max possible height for reverse mapping btrees. */
740 xfs_rmapbt_maxlevels_ondisk(void)
742 unsigned int minrecs[2];
743 unsigned int blocklen;
745 blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
747 minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
748 minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
751 * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
753 * On a reflink filesystem, each AG block can have up to 2^32 (per the
754 * refcount record format) owners, which means that theoretically we
755 * could face up to 2^64 rmap records. However, we're likely to run
756 * out of blocks in the AG long before that happens, which means that
757 * we must compute the max height based on what the btree will look
758 * like if it consumes almost all the blocks in the AG due to maximal
761 return max(xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS),
762 xfs_rmapbt_mem_maxlevels());
765 /* Compute the maximum height of an rmap btree. */
767 xfs_rmapbt_compute_maxlevels(
768 struct xfs_mount *mp)
770 if (!xfs_has_rmapbt(mp)) {
771 mp->m_rmap_maxlevels = 0;
775 if (xfs_has_reflink(mp)) {
777 * Compute the asymptotic maxlevels for an rmap btree on a
778 * filesystem that supports reflink.
780 * On a reflink filesystem, each AG block can have up to 2^32
781 * (per the refcount record format) owners, which means that
782 * theoretically we could face up to 2^64 rmap records.
783 * However, we're likely to run out of blocks in the AG long
784 * before that happens, which means that we must compute the
785 * max height based on what the btree will look like if it
786 * consumes almost all the blocks in the AG due to maximal
789 mp->m_rmap_maxlevels = xfs_btree_space_to_height(mp->m_rmap_mnr,
790 mp->m_sb.sb_agblocks);
793 * If there's no block sharing, compute the maximum rmapbt
794 * height assuming one rmap record per AG block.
796 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
797 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
799 ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
802 /* Calculate the refcount btree size for some records. */
804 xfs_rmapbt_calc_size(
805 struct xfs_mount *mp,
806 unsigned long long len)
808 return xfs_btree_calc_size(mp->m_rmap_mnr, len);
812 * Calculate the maximum refcount btree size.
816 struct xfs_mount *mp,
817 xfs_agblock_t agblocks)
819 /* Bail out if we're uninitialized, which can happen in mkfs. */
820 if (mp->m_rmap_mxr[0] == 0)
823 return xfs_rmapbt_calc_size(mp, agblocks);
827 * Figure out how many blocks to reserve and how many are used by this btree.
830 xfs_rmapbt_calc_reserves(
831 struct xfs_mount *mp,
832 struct xfs_trans *tp,
833 struct xfs_perag *pag,
837 struct xfs_buf *agbp;
839 xfs_agblock_t agblocks;
840 xfs_extlen_t tree_len;
843 if (!xfs_has_rmapbt(mp))
846 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
851 agblocks = be32_to_cpu(agf->agf_length);
852 tree_len = be32_to_cpu(agf->agf_rmap_blocks);
853 xfs_trans_brelse(tp, agbp);
856 * The log is permanently allocated, so the space it occupies will
857 * never be available for the kinds of things that would require btree
858 * expansion. We therefore can pretend the space isn't there.
860 if (xfs_ag_contains_log(mp, pag->pag_agno))
861 agblocks -= mp->m_sb.sb_logblocks;
863 /* Reserve 1% of the AG or enough for 1 block per record. */
864 *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
871 xfs_rmapbt_init_cur_cache(void)
873 xfs_rmapbt_cur_cache = kmem_cache_create("xfs_rmapbt_cur",
874 xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
877 if (!xfs_rmapbt_cur_cache)
883 xfs_rmapbt_destroy_cur_cache(void)
885 kmem_cache_destroy(xfs_rmapbt_cur_cache);
886 xfs_rmapbt_cur_cache = NULL;