1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Red Hat, Inc.
6 #include "libxfs_priv.h"
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"
21 #include "xfs_ag_resv.h"
23 static struct kmem_cache
*xfs_rmapbt_cur_cache
;
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(pag
, 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(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_roots
[cur
->bc_btnum
];
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
)
276 const struct xfs_rmap_key
*kp1
= &k1
->rmap
;
277 const struct xfs_rmap_key
*kp2
= &k2
->rmap
;
281 d
= (int64_t)be32_to_cpu(kp1
->rm_startblock
) -
282 be32_to_cpu(kp2
->rm_startblock
);
286 x
= be64_to_cpu(kp1
->rm_owner
);
287 y
= be64_to_cpu(kp2
->rm_owner
);
293 x
= offset_keymask(be64_to_cpu(kp1
->rm_offset
));
294 y
= offset_keymask(be64_to_cpu(kp2
->rm_offset
));
302 static xfs_failaddr_t
306 struct xfs_mount
*mp
= bp
->b_mount
;
307 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
308 struct xfs_perag
*pag
= bp
->b_pag
;
313 * magic number and level verification
315 * During growfs operations, we can't verify the exact level or owner as
316 * the perag is not fully initialised and hence not attached to the
317 * buffer. In this case, check against the maximum tree depth.
319 * Similarly, during log recovery we will have a perag structure
320 * attached, but the agf information will not yet have been initialised
321 * from the on disk AGF. Again, we can only check against maximum limits
324 if (!xfs_verify_magic(bp
, block
->bb_magic
))
325 return __this_address
;
327 if (!xfs_has_rmapbt(mp
))
328 return __this_address
;
329 fa
= xfs_btree_sblock_v5hdr_verify(bp
);
333 level
= be16_to_cpu(block
->bb_level
);
334 if (pag
&& xfs_perag_initialised_agf(pag
)) {
335 if (level
>= pag
->pagf_levels
[XFS_BTNUM_RMAPi
])
336 return __this_address
;
337 } else if (level
>= mp
->m_rmap_maxlevels
)
338 return __this_address
;
340 return xfs_btree_sblock_verify(bp
, mp
->m_rmap_mxr
[level
!= 0]);
344 xfs_rmapbt_read_verify(
349 if (!xfs_btree_sblock_verify_crc(bp
))
350 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
352 fa
= xfs_rmapbt_verify(bp
);
354 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
358 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
362 xfs_rmapbt_write_verify(
367 fa
= xfs_rmapbt_verify(bp
);
369 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
370 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
373 xfs_btree_sblock_calc_crc(bp
);
377 const struct xfs_buf_ops xfs_rmapbt_buf_ops
= {
378 .name
= "xfs_rmapbt",
379 .magic
= { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC
) },
380 .verify_read
= xfs_rmapbt_read_verify
,
381 .verify_write
= xfs_rmapbt_write_verify
,
382 .verify_struct
= xfs_rmapbt_verify
,
386 xfs_rmapbt_keys_inorder(
387 struct xfs_btree_cur
*cur
,
388 const union xfs_btree_key
*k1
,
389 const union xfs_btree_key
*k2
)
396 x
= be32_to_cpu(k1
->rmap
.rm_startblock
);
397 y
= be32_to_cpu(k2
->rmap
.rm_startblock
);
402 a
= be64_to_cpu(k1
->rmap
.rm_owner
);
403 b
= be64_to_cpu(k2
->rmap
.rm_owner
);
408 a
= offset_keymask(be64_to_cpu(k1
->rmap
.rm_offset
));
409 b
= offset_keymask(be64_to_cpu(k2
->rmap
.rm_offset
));
416 xfs_rmapbt_recs_inorder(
417 struct xfs_btree_cur
*cur
,
418 const union xfs_btree_rec
*r1
,
419 const union xfs_btree_rec
*r2
)
426 x
= be32_to_cpu(r1
->rmap
.rm_startblock
);
427 y
= be32_to_cpu(r2
->rmap
.rm_startblock
);
432 a
= be64_to_cpu(r1
->rmap
.rm_owner
);
433 b
= be64_to_cpu(r2
->rmap
.rm_owner
);
438 a
= offset_keymask(be64_to_cpu(r1
->rmap
.rm_offset
));
439 b
= offset_keymask(be64_to_cpu(r2
->rmap
.rm_offset
));
445 static const struct xfs_btree_ops xfs_rmapbt_ops
= {
446 .rec_len
= sizeof(struct xfs_rmap_rec
),
447 .key_len
= 2 * sizeof(struct xfs_rmap_key
),
449 .dup_cursor
= xfs_rmapbt_dup_cursor
,
450 .set_root
= xfs_rmapbt_set_root
,
451 .alloc_block
= xfs_rmapbt_alloc_block
,
452 .free_block
= xfs_rmapbt_free_block
,
453 .get_minrecs
= xfs_rmapbt_get_minrecs
,
454 .get_maxrecs
= xfs_rmapbt_get_maxrecs
,
455 .init_key_from_rec
= xfs_rmapbt_init_key_from_rec
,
456 .init_high_key_from_rec
= xfs_rmapbt_init_high_key_from_rec
,
457 .init_rec_from_cur
= xfs_rmapbt_init_rec_from_cur
,
458 .init_ptr_from_cur
= xfs_rmapbt_init_ptr_from_cur
,
459 .key_diff
= xfs_rmapbt_key_diff
,
460 .buf_ops
= &xfs_rmapbt_buf_ops
,
461 .diff_two_keys
= xfs_rmapbt_diff_two_keys
,
462 .keys_inorder
= xfs_rmapbt_keys_inorder
,
463 .recs_inorder
= xfs_rmapbt_recs_inorder
,
466 static struct xfs_btree_cur
*
467 xfs_rmapbt_init_common(
468 struct xfs_mount
*mp
,
469 struct xfs_trans
*tp
,
470 struct xfs_perag
*pag
)
472 struct xfs_btree_cur
*cur
;
474 /* Overlapping btree; 2 keys per pointer. */
475 cur
= xfs_btree_alloc_cursor(mp
, tp
, XFS_BTNUM_RMAP
,
476 mp
->m_rmap_maxlevels
, xfs_rmapbt_cur_cache
);
477 cur
->bc_flags
= XFS_BTREE_CRC_BLOCKS
| XFS_BTREE_OVERLAPPING
;
478 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_rmap_2
);
479 cur
->bc_ops
= &xfs_rmapbt_ops
;
481 cur
->bc_ag
.pag
= xfs_perag_hold(pag
);
485 /* Create a new reverse mapping btree cursor. */
486 struct xfs_btree_cur
*
487 xfs_rmapbt_init_cursor(
488 struct xfs_mount
*mp
,
489 struct xfs_trans
*tp
,
490 struct xfs_buf
*agbp
,
491 struct xfs_perag
*pag
)
493 struct xfs_agf
*agf
= agbp
->b_addr
;
494 struct xfs_btree_cur
*cur
;
496 cur
= xfs_rmapbt_init_common(mp
, tp
, pag
);
497 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]);
498 cur
->bc_ag
.agbp
= agbp
;
502 /* Create a new reverse mapping btree cursor with a fake root for staging. */
503 struct xfs_btree_cur
*
504 xfs_rmapbt_stage_cursor(
505 struct xfs_mount
*mp
,
506 struct xbtree_afakeroot
*afake
,
507 struct xfs_perag
*pag
)
509 struct xfs_btree_cur
*cur
;
511 cur
= xfs_rmapbt_init_common(mp
, NULL
, pag
);
512 xfs_btree_stage_afakeroot(cur
, afake
);
517 * Install a new reverse mapping btree root. Caller is responsible for
518 * invalidating and freeing the old btree blocks.
521 xfs_rmapbt_commit_staged_btree(
522 struct xfs_btree_cur
*cur
,
523 struct xfs_trans
*tp
,
524 struct xfs_buf
*agbp
)
526 struct xfs_agf
*agf
= agbp
->b_addr
;
527 struct xbtree_afakeroot
*afake
= cur
->bc_ag
.afake
;
529 ASSERT(cur
->bc_flags
& XFS_BTREE_STAGING
);
531 agf
->agf_roots
[cur
->bc_btnum
] = cpu_to_be32(afake
->af_root
);
532 agf
->agf_levels
[cur
->bc_btnum
] = cpu_to_be32(afake
->af_levels
);
533 agf
->agf_rmap_blocks
= cpu_to_be32(afake
->af_blocks
);
534 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
|
535 XFS_AGF_RMAP_BLOCKS
);
536 xfs_btree_commit_afakeroot(cur
, tp
, agbp
, &xfs_rmapbt_ops
);
539 /* Calculate number of records in a reverse mapping btree block. */
540 static inline unsigned int
541 xfs_rmapbt_block_maxrecs(
542 unsigned int blocklen
,
546 return blocklen
/ sizeof(struct xfs_rmap_rec
);
548 (2 * sizeof(struct xfs_rmap_key
) + sizeof(xfs_rmap_ptr_t
));
552 * Calculate number of records in an rmap btree block.
559 blocklen
-= XFS_RMAP_BLOCK_LEN
;
560 return xfs_rmapbt_block_maxrecs(blocklen
, leaf
);
563 /* Compute the max possible height for reverse mapping btrees. */
565 xfs_rmapbt_maxlevels_ondisk(void)
567 unsigned int minrecs
[2];
568 unsigned int blocklen
;
570 blocklen
= XFS_MIN_CRC_BLOCKSIZE
- XFS_BTREE_SBLOCK_CRC_LEN
;
572 minrecs
[0] = xfs_rmapbt_block_maxrecs(blocklen
, true) / 2;
573 minrecs
[1] = xfs_rmapbt_block_maxrecs(blocklen
, false) / 2;
576 * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
578 * On a reflink filesystem, each AG block can have up to 2^32 (per the
579 * refcount record format) owners, which means that theoretically we
580 * could face up to 2^64 rmap records. However, we're likely to run
581 * out of blocks in the AG long before that happens, which means that
582 * we must compute the max height based on what the btree will look
583 * like if it consumes almost all the blocks in the AG due to maximal
586 return xfs_btree_space_to_height(minrecs
, XFS_MAX_CRC_AG_BLOCKS
);
589 /* Compute the maximum height of an rmap btree. */
591 xfs_rmapbt_compute_maxlevels(
592 struct xfs_mount
*mp
)
594 if (!xfs_has_rmapbt(mp
)) {
595 mp
->m_rmap_maxlevels
= 0;
599 if (xfs_has_reflink(mp
)) {
601 * Compute the asymptotic maxlevels for an rmap btree on a
602 * filesystem that supports reflink.
604 * On a reflink filesystem, each AG block can have up to 2^32
605 * (per the refcount record format) owners, which means that
606 * theoretically we could face up to 2^64 rmap records.
607 * However, we're likely to run out of blocks in the AG long
608 * before that happens, which means that we must compute the
609 * max height based on what the btree will look like if it
610 * consumes almost all the blocks in the AG due to maximal
613 mp
->m_rmap_maxlevels
= xfs_btree_space_to_height(mp
->m_rmap_mnr
,
614 mp
->m_sb
.sb_agblocks
);
617 * If there's no block sharing, compute the maximum rmapbt
618 * height assuming one rmap record per AG block.
620 mp
->m_rmap_maxlevels
= xfs_btree_compute_maxlevels(
621 mp
->m_rmap_mnr
, mp
->m_sb
.sb_agblocks
);
623 ASSERT(mp
->m_rmap_maxlevels
<= xfs_rmapbt_maxlevels_ondisk());
626 /* Calculate the refcount btree size for some records. */
628 xfs_rmapbt_calc_size(
629 struct xfs_mount
*mp
,
630 unsigned long long len
)
632 return xfs_btree_calc_size(mp
->m_rmap_mnr
, len
);
636 * Calculate the maximum refcount btree size.
640 struct xfs_mount
*mp
,
641 xfs_agblock_t agblocks
)
643 /* Bail out if we're uninitialized, which can happen in mkfs. */
644 if (mp
->m_rmap_mxr
[0] == 0)
647 return xfs_rmapbt_calc_size(mp
, agblocks
);
651 * Figure out how many blocks to reserve and how many are used by this btree.
654 xfs_rmapbt_calc_reserves(
655 struct xfs_mount
*mp
,
656 struct xfs_trans
*tp
,
657 struct xfs_perag
*pag
,
661 struct xfs_buf
*agbp
;
663 xfs_agblock_t agblocks
;
664 xfs_extlen_t tree_len
;
667 if (!xfs_has_rmapbt(mp
))
670 error
= xfs_alloc_read_agf(pag
, tp
, 0, &agbp
);
675 agblocks
= be32_to_cpu(agf
->agf_length
);
676 tree_len
= be32_to_cpu(agf
->agf_rmap_blocks
);
677 xfs_trans_brelse(tp
, agbp
);
680 * The log is permanently allocated, so the space it occupies will
681 * never be available for the kinds of things that would require btree
682 * expansion. We therefore can pretend the space isn't there.
684 if (xfs_ag_contains_log(mp
, pag
->pag_agno
))
685 agblocks
-= mp
->m_sb
.sb_logblocks
;
687 /* Reserve 1% of the AG or enough for 1 block per record. */
688 *ask
+= max(agblocks
/ 100, xfs_rmapbt_max_size(mp
, agblocks
));
695 xfs_rmapbt_init_cur_cache(void)
697 xfs_rmapbt_cur_cache
= kmem_cache_create("xfs_rmapbt_cur",
698 xfs_btree_cur_sizeof(xfs_rmapbt_maxlevels_ondisk()),
701 if (!xfs_rmapbt_cur_cache
)
707 xfs_rmapbt_destroy_cur_cache(void)
709 kmem_cache_destroy(xfs_rmapbt_cur_cache
);
710 xfs_rmapbt_cur_cache
= NULL
;