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, to_perag(cur->bc_group));
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;
71 struct xfs_perag *pag = to_perag(cur->bc_group);
75 agf->agf_rmap_root = ptr->s;
76 be32_add_cpu(&agf->agf_rmap_level, inc);
77 pag->pagf_rmap_level += inc;
79 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
83 xfs_rmapbt_alloc_block(
84 struct xfs_btree_cur *cur,
85 const union xfs_btree_ptr *start,
86 union xfs_btree_ptr *new,
89 struct xfs_buf *agbp = cur->bc_ag.agbp;
90 struct xfs_agf *agf = agbp->b_addr;
91 struct xfs_perag *pag = to_perag(cur->bc_group);
92 struct xfs_alloc_arg args = { .len = 1 };
96 /* Allocate the new block from the freelist. If we can't, give up. */
97 error = xfs_alloc_get_freelist(pag, cur->bc_tp, cur->bc_ag.agbp,
101 if (bno == NULLAGBLOCK) {
106 xfs_extent_busy_reuse(pag_group(pag), bno, 1, false);
108 new->s = cpu_to_be32(bno);
109 be32_add_cpu(&agf->agf_rmap_blocks, 1);
110 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
113 * Since rmapbt blocks are sourced from the AGFL, they are allocated one
114 * at a time and the reservation updates don't require a transaction.
116 xfs_ag_resv_alloc_extent(pag, XFS_AG_RESV_RMAPBT, &args);
123 xfs_rmapbt_free_block(
124 struct xfs_btree_cur *cur,
127 struct xfs_buf *agbp = cur->bc_ag.agbp;
128 struct xfs_agf *agf = agbp->b_addr;
129 struct xfs_perag *pag = to_perag(cur->bc_group);
133 bno = xfs_daddr_to_agbno(cur->bc_mp, xfs_buf_daddr(bp));
134 be32_add_cpu(&agf->agf_rmap_blocks, -1);
135 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
136 error = xfs_alloc_put_freelist(pag, cur->bc_tp, agbp, NULL, bno, 1);
140 xfs_extent_busy_insert(cur->bc_tp, pag_group(pag), bno, 1,
141 XFS_EXTENT_BUSY_SKIP_DISCARD);
143 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_RMAPBT, NULL, 1);
148 xfs_rmapbt_get_minrecs(
149 struct xfs_btree_cur *cur,
152 return cur->bc_mp->m_rmap_mnr[level != 0];
156 xfs_rmapbt_get_maxrecs(
157 struct xfs_btree_cur *cur,
160 return cur->bc_mp->m_rmap_mxr[level != 0];
164 * Convert the ondisk record's offset field into the ondisk key's offset field.
165 * Fork and bmbt are significant parts of the rmap record key, but written
166 * status is merely a record attribute.
168 static inline __be64 ondisk_rec_offset_to_key(const union xfs_btree_rec *rec)
170 return rec->rmap.rm_offset & ~cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
174 xfs_rmapbt_init_key_from_rec(
175 union xfs_btree_key *key,
176 const union xfs_btree_rec *rec)
178 key->rmap.rm_startblock = rec->rmap.rm_startblock;
179 key->rmap.rm_owner = rec->rmap.rm_owner;
180 key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
184 * The high key for a reverse mapping record can be computed by shifting
185 * the startblock and offset to the highest value that would still map
186 * to that record. In practice this means that we add blockcount-1 to
187 * the startblock for all records, and if the record is for a data/attr
188 * fork mapping, we add blockcount-1 to the offset too.
191 xfs_rmapbt_init_high_key_from_rec(
192 union xfs_btree_key *key,
193 const union xfs_btree_rec *rec)
198 adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
200 key->rmap.rm_startblock = rec->rmap.rm_startblock;
201 be32_add_cpu(&key->rmap.rm_startblock, adj);
202 key->rmap.rm_owner = rec->rmap.rm_owner;
203 key->rmap.rm_offset = ondisk_rec_offset_to_key(rec);
204 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
205 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
207 off = be64_to_cpu(key->rmap.rm_offset);
208 off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
209 key->rmap.rm_offset = cpu_to_be64(off);
213 xfs_rmapbt_init_rec_from_cur(
214 struct xfs_btree_cur *cur,
215 union xfs_btree_rec *rec)
217 rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
218 rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
219 rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
220 rec->rmap.rm_offset = cpu_to_be64(
221 xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
225 xfs_rmapbt_init_ptr_from_cur(
226 struct xfs_btree_cur *cur,
227 union xfs_btree_ptr *ptr)
229 struct xfs_agf *agf = cur->bc_ag.agbp->b_addr;
231 ASSERT(cur->bc_group->xg_gno == be32_to_cpu(agf->agf_seqno));
233 ptr->s = agf->agf_rmap_root;
237 * Mask the appropriate parts of the ondisk key field for a key comparison.
238 * Fork and bmbt are significant parts of the rmap record key, but written
239 * status is merely a record attribute.
241 static inline uint64_t offset_keymask(uint64_t offset)
243 return offset & ~XFS_RMAP_OFF_UNWRITTEN;
248 struct xfs_btree_cur *cur,
249 const union xfs_btree_key *key)
251 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
252 const struct xfs_rmap_key *kp = &key->rmap;
256 d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
260 x = be64_to_cpu(kp->rm_owner);
267 x = offset_keymask(be64_to_cpu(kp->rm_offset));
268 y = offset_keymask(xfs_rmap_irec_offset_pack(rec));
277 xfs_rmapbt_diff_two_keys(
278 struct xfs_btree_cur *cur,
279 const union xfs_btree_key *k1,
280 const union xfs_btree_key *k2,
281 const union xfs_btree_key *mask)
283 const struct xfs_rmap_key *kp1 = &k1->rmap;
284 const struct xfs_rmap_key *kp2 = &k2->rmap;
288 /* Doesn't make sense to mask off the physical space part */
289 ASSERT(!mask || mask->rmap.rm_startblock);
291 d = (int64_t)be32_to_cpu(kp1->rm_startblock) -
292 be32_to_cpu(kp2->rm_startblock);
296 if (!mask || mask->rmap.rm_owner) {
297 x = be64_to_cpu(kp1->rm_owner);
298 y = be64_to_cpu(kp2->rm_owner);
305 if (!mask || mask->rmap.rm_offset) {
306 /* Doesn't make sense to allow offset but not owner */
307 ASSERT(!mask || mask->rmap.rm_owner);
309 x = offset_keymask(be64_to_cpu(kp1->rm_offset));
310 y = offset_keymask(be64_to_cpu(kp2->rm_offset));
320 static xfs_failaddr_t
324 struct xfs_mount *mp = bp->b_mount;
325 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
326 struct xfs_perag *pag = bp->b_pag;
331 * magic number and level verification
333 * During growfs operations, we can't verify the exact level or owner as
334 * the perag is not fully initialised and hence not attached to the
335 * buffer. In this case, check against the maximum tree depth.
337 * Similarly, during log recovery we will have a perag structure
338 * attached, but the agf information will not yet have been initialised
339 * from the on disk AGF. Again, we can only check against maximum limits
342 if (!xfs_verify_magic(bp, block->bb_magic))
343 return __this_address;
345 if (!xfs_has_rmapbt(mp))
346 return __this_address;
347 fa = xfs_btree_agblock_v5hdr_verify(bp);
351 level = be16_to_cpu(block->bb_level);
352 if (pag && xfs_perag_initialised_agf(pag)) {
353 unsigned int maxlevel = pag->pagf_rmap_level;
355 #ifdef CONFIG_XFS_ONLINE_REPAIR
357 * Online repair could be rewriting the free space btrees, so
358 * we'll validate against the larger of either tree while this
361 maxlevel = max_t(unsigned int, maxlevel,
362 pag->pagf_repair_rmap_level);
364 if (level >= maxlevel)
365 return __this_address;
366 } else if (level >= mp->m_rmap_maxlevels)
367 return __this_address;
369 return xfs_btree_agblock_verify(bp, mp->m_rmap_mxr[level != 0]);
373 xfs_rmapbt_read_verify(
378 if (!xfs_btree_agblock_verify_crc(bp))
379 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
381 fa = xfs_rmapbt_verify(bp);
383 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
387 trace_xfs_btree_corrupt(bp, _RET_IP_);
391 xfs_rmapbt_write_verify(
396 fa = xfs_rmapbt_verify(bp);
398 trace_xfs_btree_corrupt(bp, _RET_IP_);
399 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
402 xfs_btree_agblock_calc_crc(bp);
406 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
407 .name = "xfs_rmapbt",
408 .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
409 .verify_read = xfs_rmapbt_read_verify,
410 .verify_write = xfs_rmapbt_write_verify,
411 .verify_struct = xfs_rmapbt_verify,
415 xfs_rmapbt_keys_inorder(
416 struct xfs_btree_cur *cur,
417 const union xfs_btree_key *k1,
418 const union xfs_btree_key *k2)
425 x = be32_to_cpu(k1->rmap.rm_startblock);
426 y = be32_to_cpu(k2->rmap.rm_startblock);
431 a = be64_to_cpu(k1->rmap.rm_owner);
432 b = be64_to_cpu(k2->rmap.rm_owner);
437 a = offset_keymask(be64_to_cpu(k1->rmap.rm_offset));
438 b = offset_keymask(be64_to_cpu(k2->rmap.rm_offset));
445 xfs_rmapbt_recs_inorder(
446 struct xfs_btree_cur *cur,
447 const union xfs_btree_rec *r1,
448 const union xfs_btree_rec *r2)
455 x = be32_to_cpu(r1->rmap.rm_startblock);
456 y = be32_to_cpu(r2->rmap.rm_startblock);
461 a = be64_to_cpu(r1->rmap.rm_owner);
462 b = be64_to_cpu(r2->rmap.rm_owner);
467 a = offset_keymask(be64_to_cpu(r1->rmap.rm_offset));
468 b = offset_keymask(be64_to_cpu(r2->rmap.rm_offset));
474 STATIC enum xbtree_key_contig
475 xfs_rmapbt_keys_contiguous(
476 struct xfs_btree_cur *cur,
477 const union xfs_btree_key *key1,
478 const union xfs_btree_key *key2,
479 const union xfs_btree_key *mask)
481 ASSERT(!mask || mask->rmap.rm_startblock);
484 * We only support checking contiguity of the physical space component.
485 * If any callers ever need more specificity than that, they'll have to
488 ASSERT(!mask || (!mask->rmap.rm_owner && !mask->rmap.rm_offset));
490 return xbtree_key_contig(be32_to_cpu(key1->rmap.rm_startblock),
491 be32_to_cpu(key2->rmap.rm_startblock));
494 const struct xfs_btree_ops xfs_rmapbt_ops = {
496 .type = XFS_BTREE_TYPE_AG,
497 .geom_flags = XFS_BTGEO_OVERLAPPING,
499 .rec_len = sizeof(struct xfs_rmap_rec),
500 /* Overlapping btree; 2 keys per pointer. */
501 .key_len = 2 * sizeof(struct xfs_rmap_key),
502 .ptr_len = XFS_BTREE_SHORT_PTR_LEN,
504 .lru_refs = XFS_RMAP_BTREE_REF,
505 .statoff = XFS_STATS_CALC_INDEX(xs_rmap_2),
506 .sick_mask = XFS_SICK_AG_RMAPBT,
508 .dup_cursor = xfs_rmapbt_dup_cursor,
509 .set_root = xfs_rmapbt_set_root,
510 .alloc_block = xfs_rmapbt_alloc_block,
511 .free_block = xfs_rmapbt_free_block,
512 .get_minrecs = xfs_rmapbt_get_minrecs,
513 .get_maxrecs = xfs_rmapbt_get_maxrecs,
514 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
515 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
516 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
517 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
518 .key_diff = xfs_rmapbt_key_diff,
519 .buf_ops = &xfs_rmapbt_buf_ops,
520 .diff_two_keys = xfs_rmapbt_diff_two_keys,
521 .keys_inorder = xfs_rmapbt_keys_inorder,
522 .recs_inorder = xfs_rmapbt_recs_inorder,
523 .keys_contiguous = xfs_rmapbt_keys_contiguous,
527 * Create a new reverse mapping btree cursor.
529 * For staging cursors tp and agbp are NULL.
531 struct xfs_btree_cur *
532 xfs_rmapbt_init_cursor(
533 struct xfs_mount *mp,
534 struct xfs_trans *tp,
535 struct xfs_buf *agbp,
536 struct xfs_perag *pag)
538 struct xfs_btree_cur *cur;
540 cur = xfs_btree_alloc_cursor(mp, tp, &xfs_rmapbt_ops,
541 mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
542 cur->bc_group = xfs_group_hold(pag_group(pag));
543 cur->bc_ag.agbp = agbp;
545 struct xfs_agf *agf = agbp->b_addr;
547 cur->bc_nlevels = be32_to_cpu(agf->agf_rmap_level);
552 #ifdef CONFIG_XFS_BTREE_IN_MEM
553 static inline unsigned int
554 xfs_rmapbt_mem_block_maxrecs(
555 unsigned int blocklen,
559 return blocklen / sizeof(struct xfs_rmap_rec);
561 (2 * sizeof(struct xfs_rmap_key) + sizeof(__be64));
565 * Validate an in-memory rmap btree block. Callers are allowed to generate an
566 * in-memory btree even if the ondisk feature is not enabled.
568 static xfs_failaddr_t
569 xfs_rmapbt_mem_verify(
572 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
575 unsigned int maxrecs;
577 if (!xfs_verify_magic(bp, block->bb_magic))
578 return __this_address;
580 fa = xfs_btree_fsblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
584 level = be16_to_cpu(block->bb_level);
585 if (level >= xfs_rmapbt_maxlevels_ondisk())
586 return __this_address;
588 maxrecs = xfs_rmapbt_mem_block_maxrecs(
589 XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN, level == 0);
590 return xfs_btree_memblock_verify(bp, maxrecs);
594 xfs_rmapbt_mem_rw_verify(
597 xfs_failaddr_t fa = xfs_rmapbt_mem_verify(bp);
600 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
603 /* skip crc checks on in-memory btrees to save time */
604 static const struct xfs_buf_ops xfs_rmapbt_mem_buf_ops = {
605 .name = "xfs_rmapbt_mem",
606 .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
607 .verify_read = xfs_rmapbt_mem_rw_verify,
608 .verify_write = xfs_rmapbt_mem_rw_verify,
609 .verify_struct = xfs_rmapbt_mem_verify,
612 const struct xfs_btree_ops xfs_rmapbt_mem_ops = {
614 .type = XFS_BTREE_TYPE_MEM,
615 .geom_flags = XFS_BTGEO_OVERLAPPING,
617 .rec_len = sizeof(struct xfs_rmap_rec),
618 /* Overlapping btree; 2 keys per pointer. */
619 .key_len = 2 * sizeof(struct xfs_rmap_key),
620 .ptr_len = XFS_BTREE_LONG_PTR_LEN,
622 .lru_refs = XFS_RMAP_BTREE_REF,
623 .statoff = XFS_STATS_CALC_INDEX(xs_rmap_mem_2),
625 .dup_cursor = xfbtree_dup_cursor,
626 .set_root = xfbtree_set_root,
627 .alloc_block = xfbtree_alloc_block,
628 .free_block = xfbtree_free_block,
629 .get_minrecs = xfbtree_get_minrecs,
630 .get_maxrecs = xfbtree_get_maxrecs,
631 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
632 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
633 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
634 .init_ptr_from_cur = xfbtree_init_ptr_from_cur,
635 .key_diff = xfs_rmapbt_key_diff,
636 .buf_ops = &xfs_rmapbt_mem_buf_ops,
637 .diff_two_keys = xfs_rmapbt_diff_two_keys,
638 .keys_inorder = xfs_rmapbt_keys_inorder,
639 .recs_inorder = xfs_rmapbt_recs_inorder,
640 .keys_contiguous = xfs_rmapbt_keys_contiguous,
643 /* Create a cursor for an in-memory btree. */
644 struct xfs_btree_cur *
645 xfs_rmapbt_mem_cursor(
646 struct xfs_perag *pag,
647 struct xfs_trans *tp,
648 struct xfbtree *xfbt)
650 struct xfs_btree_cur *cur;
652 cur = xfs_btree_alloc_cursor(pag_mount(pag), tp, &xfs_rmapbt_mem_ops,
653 xfs_rmapbt_maxlevels_ondisk(), xfs_rmapbt_cur_cache);
654 cur->bc_mem.xfbtree = xfbt;
655 cur->bc_nlevels = xfbt->nlevels;
657 cur->bc_group = xfs_group_hold(pag_group(pag));
661 /* Create an in-memory rmap btree. */
664 struct xfs_mount *mp,
665 struct xfbtree *xfbt,
666 struct xfs_buftarg *btp,
670 return xfbtree_init(mp, xfbt, btp, &xfs_rmapbt_mem_ops);
673 /* Compute the max possible height for reverse mapping btrees in memory. */
675 xfs_rmapbt_mem_maxlevels(void)
677 unsigned int minrecs[2];
678 unsigned int blocklen;
680 blocklen = XFBNO_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN;
682 minrecs[0] = xfs_rmapbt_mem_block_maxrecs(blocklen, true) / 2;
683 minrecs[1] = xfs_rmapbt_mem_block_maxrecs(blocklen, false) / 2;
686 * How tall can an in-memory rmap btree become if we filled the entire
687 * AG with rmap records?
689 return xfs_btree_compute_maxlevels(minrecs,
690 XFS_MAX_AG_BYTES / sizeof(struct xfs_rmap_rec));
693 # define xfs_rmapbt_mem_maxlevels() (0)
694 #endif /* CONFIG_XFS_BTREE_IN_MEM */
697 * Install a new reverse mapping btree root. Caller is responsible for
698 * invalidating and freeing the old btree blocks.
701 xfs_rmapbt_commit_staged_btree(
702 struct xfs_btree_cur *cur,
703 struct xfs_trans *tp,
704 struct xfs_buf *agbp)
706 struct xfs_agf *agf = agbp->b_addr;
707 struct xbtree_afakeroot *afake = cur->bc_ag.afake;
709 ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
711 agf->agf_rmap_root = cpu_to_be32(afake->af_root);
712 agf->agf_rmap_level = cpu_to_be32(afake->af_levels);
713 agf->agf_rmap_blocks = cpu_to_be32(afake->af_blocks);
714 xfs_alloc_log_agf(tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS |
715 XFS_AGF_RMAP_BLOCKS);
716 xfs_btree_commit_afakeroot(cur, tp, agbp);
719 /* Calculate number of records in a reverse mapping btree block. */
720 static inline unsigned int
721 xfs_rmapbt_block_maxrecs(
722 unsigned int blocklen,
726 return blocklen / sizeof(struct xfs_rmap_rec);
728 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
732 * Calculate number of records in an rmap btree block.
736 struct xfs_mount *mp,
737 unsigned int blocklen,
740 blocklen -= XFS_RMAP_BLOCK_LEN;
741 return xfs_rmapbt_block_maxrecs(blocklen, leaf);
744 /* Compute the max possible height for reverse mapping btrees. */
746 xfs_rmapbt_maxlevels_ondisk(void)
748 unsigned int minrecs[2];
749 unsigned int blocklen;
751 blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
753 minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
754 minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
757 * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
759 * On a reflink filesystem, each AG block can have up to 2^32 (per the
760 * refcount record format) owners, which means that theoretically we
761 * could face up to 2^64 rmap records. However, we're likely to run
762 * out of blocks in the AG long before that happens, which means that
763 * we must compute the max height based on what the btree will look
764 * like if it consumes almost all the blocks in the AG due to maximal
767 return max(xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS),
768 xfs_rmapbt_mem_maxlevels());
771 /* Compute the maximum height of an rmap btree. */
773 xfs_rmapbt_compute_maxlevels(
774 struct xfs_mount *mp)
776 if (!xfs_has_rmapbt(mp)) {
777 mp->m_rmap_maxlevels = 0;
781 if (xfs_has_reflink(mp)) {
783 * Compute the asymptotic maxlevels for an rmap btree on a
784 * filesystem that supports reflink.
786 * On a reflink filesystem, each AG block can have up to 2^32
787 * (per the refcount record format) owners, which means that
788 * theoretically we could face up to 2^64 rmap records.
789 * However, we're likely to run out of blocks in the AG long
790 * before that happens, which means that we must compute the
791 * max height based on what the btree will look like if it
792 * consumes almost all the blocks in the AG due to maximal
795 mp->m_rmap_maxlevels = xfs_btree_space_to_height(mp->m_rmap_mnr,
796 mp->m_sb.sb_agblocks);
799 * If there's no block sharing, compute the maximum rmapbt
800 * height assuming one rmap record per AG block.
802 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
803 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
805 ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
808 /* Calculate the refcount btree size for some records. */
810 xfs_rmapbt_calc_size(
811 struct xfs_mount *mp,
812 unsigned long long len)
814 return xfs_btree_calc_size(mp->m_rmap_mnr, len);
818 * Calculate the maximum refcount btree size.
822 struct xfs_mount *mp,
823 xfs_agblock_t agblocks)
825 /* Bail out if we're uninitialized, which can happen in mkfs. */
826 if (mp->m_rmap_mxr[0] == 0)
829 return xfs_rmapbt_calc_size(mp, agblocks);
833 * Figure out how many blocks to reserve and how many are used by this btree.
836 xfs_rmapbt_calc_reserves(
837 struct xfs_mount *mp,
838 struct xfs_trans *tp,
839 struct xfs_perag *pag,
843 struct xfs_buf *agbp;
845 xfs_agblock_t agblocks;
846 xfs_extlen_t tree_len;
849 if (!xfs_has_rmapbt(mp))
852 error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
857 agblocks = be32_to_cpu(agf->agf_length);
858 tree_len = be32_to_cpu(agf->agf_rmap_blocks);
859 xfs_trans_brelse(tp, agbp);
862 * The log is permanently allocated, so the space it occupies will
863 * never be available for the kinds of things that would require btree
864 * expansion. We therefore can pretend the space isn't there.
866 if (xfs_ag_contains_log(mp, pag_agno(pag)))
867 agblocks -= mp->m_sb.sb_logblocks;
869 /* Reserve 1% of the AG or enough for 1 block per record. */
870 *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks));
877 xfs_rmapbt_init_cur_cache(void)
879 xfs_rmapbt_cur_cache = kmem_cache_create("xfs_rmapbt_cur",
880 xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
883 if (!xfs_rmapbt_cur_cache)
889 xfs_rmapbt_destroy_cur_cache(void)
891 kmem_cache_destroy(xfs_rmapbt_cur_cache);
892 xfs_rmapbt_cur_cache = NULL;