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"
13 #include "xfs_mount.h"
14 #include "xfs_trans.h"
15 #include "xfs_alloc.h"
16 #include "xfs_btree.h"
17 #include "xfs_btree_staging.h"
19 #include "xfs_rmap_btree.h"
20 #include "xfs_trace.h"
21 #include "xfs_error.h"
22 #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
.agno
);
60 struct xfs_btree_cur
*cur
,
61 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
;
67 struct xfs_perag
*pag
= agbp
->b_pag
;
71 agf
->agf_roots
[btnum
] = ptr
->s
;
72 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
73 pag
->pagf_levels
[btnum
] += inc
;
75 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
79 xfs_rmapbt_alloc_block(
80 struct xfs_btree_cur
*cur
,
81 union xfs_btree_ptr
*start
,
82 union xfs_btree_ptr
*new,
85 struct xfs_buf
*agbp
= cur
->bc_ag
.agbp
;
86 struct xfs_agf
*agf
= agbp
->b_addr
;
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
, cur
->bc_ag
.agno
,
98 if (bno
== NULLAGBLOCK
) {
103 xfs_extent_busy_reuse(cur
->bc_mp
, cur
->bc_ag
.agno
, bno
, 1,
106 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
107 new->s
= cpu_to_be32(bno
);
108 be32_add_cpu(&agf
->agf_rmap_blocks
, 1);
109 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_RMAP_BLOCKS
);
111 xfs_ag_resv_rmapbt_alloc(cur
->bc_mp
, cur
->bc_ag
.agno
);
118 xfs_rmapbt_free_block(
119 struct xfs_btree_cur
*cur
,
122 struct xfs_buf
*agbp
= cur
->bc_ag
.agbp
;
123 struct xfs_agf
*agf
= agbp
->b_addr
;
124 struct xfs_perag
*pag
;
128 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
129 trace_xfs_rmapbt_free_block(cur
->bc_mp
, cur
->bc_ag
.agno
,
131 be32_add_cpu(&agf
->agf_rmap_blocks
, -1);
132 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_RMAP_BLOCKS
);
133 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
137 xfs_extent_busy_insert(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1,
138 XFS_EXTENT_BUSY_SKIP_DISCARD
);
139 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
141 pag
= cur
->bc_ag
.agbp
->b_pag
;
142 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_RMAPBT
, NULL
, 1);
147 xfs_rmapbt_get_minrecs(
148 struct xfs_btree_cur
*cur
,
151 return cur
->bc_mp
->m_rmap_mnr
[level
!= 0];
155 xfs_rmapbt_get_maxrecs(
156 struct xfs_btree_cur
*cur
,
159 return cur
->bc_mp
->m_rmap_mxr
[level
!= 0];
163 xfs_rmapbt_init_key_from_rec(
164 union xfs_btree_key
*key
,
165 union xfs_btree_rec
*rec
)
167 key
->rmap
.rm_startblock
= rec
->rmap
.rm_startblock
;
168 key
->rmap
.rm_owner
= rec
->rmap
.rm_owner
;
169 key
->rmap
.rm_offset
= rec
->rmap
.rm_offset
;
173 * The high key for a reverse mapping record can be computed by shifting
174 * the startblock and offset to the highest value that would still map
175 * to that record. In practice this means that we add blockcount-1 to
176 * the startblock for all records, and if the record is for a data/attr
177 * fork mapping, we add blockcount-1 to the offset too.
180 xfs_rmapbt_init_high_key_from_rec(
181 union xfs_btree_key
*key
,
182 union xfs_btree_rec
*rec
)
187 adj
= be32_to_cpu(rec
->rmap
.rm_blockcount
) - 1;
189 key
->rmap
.rm_startblock
= rec
->rmap
.rm_startblock
;
190 be32_add_cpu(&key
->rmap
.rm_startblock
, adj
);
191 key
->rmap
.rm_owner
= rec
->rmap
.rm_owner
;
192 key
->rmap
.rm_offset
= rec
->rmap
.rm_offset
;
193 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec
->rmap
.rm_owner
)) ||
194 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec
->rmap
.rm_offset
)))
196 off
= be64_to_cpu(key
->rmap
.rm_offset
);
197 off
= (XFS_RMAP_OFF(off
) + adj
) | (off
& ~XFS_RMAP_OFF_MASK
);
198 key
->rmap
.rm_offset
= cpu_to_be64(off
);
202 xfs_rmapbt_init_rec_from_cur(
203 struct xfs_btree_cur
*cur
,
204 union xfs_btree_rec
*rec
)
206 rec
->rmap
.rm_startblock
= cpu_to_be32(cur
->bc_rec
.r
.rm_startblock
);
207 rec
->rmap
.rm_blockcount
= cpu_to_be32(cur
->bc_rec
.r
.rm_blockcount
);
208 rec
->rmap
.rm_owner
= cpu_to_be64(cur
->bc_rec
.r
.rm_owner
);
209 rec
->rmap
.rm_offset
= cpu_to_be64(
210 xfs_rmap_irec_offset_pack(&cur
->bc_rec
.r
));
214 xfs_rmapbt_init_ptr_from_cur(
215 struct xfs_btree_cur
*cur
,
216 union xfs_btree_ptr
*ptr
)
218 struct xfs_agf
*agf
= cur
->bc_ag
.agbp
->b_addr
;
220 ASSERT(cur
->bc_ag
.agno
== be32_to_cpu(agf
->agf_seqno
));
222 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
227 struct xfs_btree_cur
*cur
,
228 union xfs_btree_key
*key
)
230 struct xfs_rmap_irec
*rec
= &cur
->bc_rec
.r
;
231 struct xfs_rmap_key
*kp
= &key
->rmap
;
235 d
= (int64_t)be32_to_cpu(kp
->rm_startblock
) - rec
->rm_startblock
;
239 x
= be64_to_cpu(kp
->rm_owner
);
246 x
= XFS_RMAP_OFF(be64_to_cpu(kp
->rm_offset
));
256 xfs_rmapbt_diff_two_keys(
257 struct xfs_btree_cur
*cur
,
258 union xfs_btree_key
*k1
,
259 union xfs_btree_key
*k2
)
261 struct xfs_rmap_key
*kp1
= &k1
->rmap
;
262 struct xfs_rmap_key
*kp2
= &k2
->rmap
;
266 d
= (int64_t)be32_to_cpu(kp1
->rm_startblock
) -
267 be32_to_cpu(kp2
->rm_startblock
);
271 x
= be64_to_cpu(kp1
->rm_owner
);
272 y
= be64_to_cpu(kp2
->rm_owner
);
278 x
= XFS_RMAP_OFF(be64_to_cpu(kp1
->rm_offset
));
279 y
= XFS_RMAP_OFF(be64_to_cpu(kp2
->rm_offset
));
287 static xfs_failaddr_t
291 struct xfs_mount
*mp
= bp
->b_mount
;
292 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
293 struct xfs_perag
*pag
= bp
->b_pag
;
298 * magic number and level verification
300 * During growfs operations, we can't verify the exact level or owner as
301 * the perag is not fully initialised and hence not attached to the
302 * buffer. In this case, check against the maximum tree depth.
304 * Similarly, during log recovery we will have a perag structure
305 * attached, but the agf information will not yet have been initialised
306 * from the on disk AGF. Again, we can only check against maximum limits
309 if (!xfs_verify_magic(bp
, block
->bb_magic
))
310 return __this_address
;
312 if (!xfs_sb_version_hasrmapbt(&mp
->m_sb
))
313 return __this_address
;
314 fa
= xfs_btree_sblock_v5hdr_verify(bp
);
318 level
= be16_to_cpu(block
->bb_level
);
319 if (pag
&& pag
->pagf_init
) {
320 if (level
>= pag
->pagf_levels
[XFS_BTNUM_RMAPi
])
321 return __this_address
;
322 } else if (level
>= mp
->m_rmap_maxlevels
)
323 return __this_address
;
325 return xfs_btree_sblock_verify(bp
, mp
->m_rmap_mxr
[level
!= 0]);
329 xfs_rmapbt_read_verify(
334 if (!xfs_btree_sblock_verify_crc(bp
))
335 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
337 fa
= xfs_rmapbt_verify(bp
);
339 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
343 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
347 xfs_rmapbt_write_verify(
352 fa
= xfs_rmapbt_verify(bp
);
354 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
355 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
358 xfs_btree_sblock_calc_crc(bp
);
362 const struct xfs_buf_ops xfs_rmapbt_buf_ops
= {
363 .name
= "xfs_rmapbt",
364 .magic
= { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC
) },
365 .verify_read
= xfs_rmapbt_read_verify
,
366 .verify_write
= xfs_rmapbt_write_verify
,
367 .verify_struct
= xfs_rmapbt_verify
,
371 xfs_rmapbt_keys_inorder(
372 struct xfs_btree_cur
*cur
,
373 union xfs_btree_key
*k1
,
374 union xfs_btree_key
*k2
)
381 x
= be32_to_cpu(k1
->rmap
.rm_startblock
);
382 y
= be32_to_cpu(k2
->rmap
.rm_startblock
);
387 a
= be64_to_cpu(k1
->rmap
.rm_owner
);
388 b
= be64_to_cpu(k2
->rmap
.rm_owner
);
393 a
= XFS_RMAP_OFF(be64_to_cpu(k1
->rmap
.rm_offset
));
394 b
= XFS_RMAP_OFF(be64_to_cpu(k2
->rmap
.rm_offset
));
401 xfs_rmapbt_recs_inorder(
402 struct xfs_btree_cur
*cur
,
403 union xfs_btree_rec
*r1
,
404 union xfs_btree_rec
*r2
)
411 x
= be32_to_cpu(r1
->rmap
.rm_startblock
);
412 y
= be32_to_cpu(r2
->rmap
.rm_startblock
);
417 a
= be64_to_cpu(r1
->rmap
.rm_owner
);
418 b
= be64_to_cpu(r2
->rmap
.rm_owner
);
423 a
= XFS_RMAP_OFF(be64_to_cpu(r1
->rmap
.rm_offset
));
424 b
= XFS_RMAP_OFF(be64_to_cpu(r2
->rmap
.rm_offset
));
430 static const struct xfs_btree_ops xfs_rmapbt_ops
= {
431 .rec_len
= sizeof(struct xfs_rmap_rec
),
432 .key_len
= 2 * sizeof(struct xfs_rmap_key
),
434 .dup_cursor
= xfs_rmapbt_dup_cursor
,
435 .set_root
= xfs_rmapbt_set_root
,
436 .alloc_block
= xfs_rmapbt_alloc_block
,
437 .free_block
= xfs_rmapbt_free_block
,
438 .get_minrecs
= xfs_rmapbt_get_minrecs
,
439 .get_maxrecs
= xfs_rmapbt_get_maxrecs
,
440 .init_key_from_rec
= xfs_rmapbt_init_key_from_rec
,
441 .init_high_key_from_rec
= xfs_rmapbt_init_high_key_from_rec
,
442 .init_rec_from_cur
= xfs_rmapbt_init_rec_from_cur
,
443 .init_ptr_from_cur
= xfs_rmapbt_init_ptr_from_cur
,
444 .key_diff
= xfs_rmapbt_key_diff
,
445 .buf_ops
= &xfs_rmapbt_buf_ops
,
446 .diff_two_keys
= xfs_rmapbt_diff_two_keys
,
447 .keys_inorder
= xfs_rmapbt_keys_inorder
,
448 .recs_inorder
= xfs_rmapbt_recs_inorder
,
451 static struct xfs_btree_cur
*
452 xfs_rmapbt_init_common(
453 struct xfs_mount
*mp
,
454 struct xfs_trans
*tp
,
457 struct xfs_btree_cur
*cur
;
459 cur
= kmem_cache_zalloc(xfs_btree_cur_zone
, GFP_NOFS
| __GFP_NOFAIL
);
462 /* Overlapping btree; 2 keys per pointer. */
463 cur
->bc_btnum
= XFS_BTNUM_RMAP
;
464 cur
->bc_flags
= XFS_BTREE_CRC_BLOCKS
| XFS_BTREE_OVERLAPPING
;
465 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
466 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_rmap_2
);
467 cur
->bc_ag
.agno
= agno
;
468 cur
->bc_ops
= &xfs_rmapbt_ops
;
473 /* Create a new reverse mapping btree cursor. */
474 struct xfs_btree_cur
*
475 xfs_rmapbt_init_cursor(
476 struct xfs_mount
*mp
,
477 struct xfs_trans
*tp
,
478 struct xfs_buf
*agbp
,
481 struct xfs_agf
*agf
= agbp
->b_addr
;
482 struct xfs_btree_cur
*cur
;
484 cur
= xfs_rmapbt_init_common(mp
, tp
, agno
);
485 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]);
486 cur
->bc_ag
.agbp
= agbp
;
490 /* Create a new reverse mapping btree cursor with a fake root for staging. */
491 struct xfs_btree_cur
*
492 xfs_rmapbt_stage_cursor(
493 struct xfs_mount
*mp
,
494 struct xbtree_afakeroot
*afake
,
497 struct xfs_btree_cur
*cur
;
499 cur
= xfs_rmapbt_init_common(mp
, NULL
, agno
);
500 xfs_btree_stage_afakeroot(cur
, afake
);
505 * Install a new reverse mapping btree root. Caller is responsible for
506 * invalidating and freeing the old btree blocks.
509 xfs_rmapbt_commit_staged_btree(
510 struct xfs_btree_cur
*cur
,
511 struct xfs_trans
*tp
,
512 struct xfs_buf
*agbp
)
514 struct xfs_agf
*agf
= agbp
->b_addr
;
515 struct xbtree_afakeroot
*afake
= cur
->bc_ag
.afake
;
517 ASSERT(cur
->bc_flags
& XFS_BTREE_STAGING
);
519 agf
->agf_roots
[cur
->bc_btnum
] = cpu_to_be32(afake
->af_root
);
520 agf
->agf_levels
[cur
->bc_btnum
] = cpu_to_be32(afake
->af_levels
);
521 agf
->agf_rmap_blocks
= cpu_to_be32(afake
->af_blocks
);
522 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
|
523 XFS_AGF_RMAP_BLOCKS
);
524 xfs_btree_commit_afakeroot(cur
, tp
, agbp
, &xfs_rmapbt_ops
);
528 * Calculate number of records in an rmap btree block.
535 blocklen
-= XFS_RMAP_BLOCK_LEN
;
538 return blocklen
/ sizeof(struct xfs_rmap_rec
);
540 (2 * sizeof(struct xfs_rmap_key
) + sizeof(xfs_rmap_ptr_t
));
543 /* Compute the maximum height of an rmap btree. */
545 xfs_rmapbt_compute_maxlevels(
546 struct xfs_mount
*mp
)
549 * On a non-reflink filesystem, the maximum number of rmap
550 * records is the number of blocks in the AG, hence the max
551 * rmapbt height is log_$maxrecs($agblocks). However, with
552 * reflink each AG block can have up to 2^32 (per the refcount
553 * record format) owners, which means that theoretically we
554 * could face up to 2^64 rmap records.
556 * That effectively means that the max rmapbt height must be
557 * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
558 * blocks to feed the rmapbt long before the rmapbt reaches
559 * maximum height. The reflink code uses ag_resv_critical to
560 * disallow reflinking when less than 10% of the per-AG metadata
561 * block reservation since the fallback is a regular file copy.
563 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
564 mp
->m_rmap_maxlevels
= XFS_BTREE_MAXLEVELS
;
566 mp
->m_rmap_maxlevels
= xfs_btree_compute_maxlevels(
567 mp
->m_rmap_mnr
, mp
->m_sb
.sb_agblocks
);
570 /* Calculate the refcount btree size for some records. */
572 xfs_rmapbt_calc_size(
573 struct xfs_mount
*mp
,
574 unsigned long long len
)
576 return xfs_btree_calc_size(mp
->m_rmap_mnr
, len
);
580 * Calculate the maximum refcount btree size.
584 struct xfs_mount
*mp
,
585 xfs_agblock_t agblocks
)
587 /* Bail out if we're uninitialized, which can happen in mkfs. */
588 if (mp
->m_rmap_mxr
[0] == 0)
591 return xfs_rmapbt_calc_size(mp
, agblocks
);
595 * Figure out how many blocks to reserve and how many are used by this btree.
598 xfs_rmapbt_calc_reserves(
599 struct xfs_mount
*mp
,
600 struct xfs_trans
*tp
,
605 struct xfs_buf
*agbp
;
607 xfs_agblock_t agblocks
;
608 xfs_extlen_t tree_len
;
611 if (!xfs_sb_version_hasrmapbt(&mp
->m_sb
))
614 error
= xfs_alloc_read_agf(mp
, tp
, agno
, 0, &agbp
);
619 agblocks
= be32_to_cpu(agf
->agf_length
);
620 tree_len
= be32_to_cpu(agf
->agf_rmap_blocks
);
621 xfs_trans_brelse(tp
, agbp
);
624 * The log is permanently allocated, so the space it occupies will
625 * never be available for the kinds of things that would require btree
626 * expansion. We therefore can pretend the space isn't there.
628 if (mp
->m_sb
.sb_logstart
&&
629 XFS_FSB_TO_AGNO(mp
, mp
->m_sb
.sb_logstart
) == agno
)
630 agblocks
-= mp
->m_sb
.sb_logblocks
;
632 /* Reserve 1% of the AG or enough for 1 block per record. */
633 *ask
+= max(agblocks
/ 100, xfs_rmapbt_max_size(mp
, agblocks
));