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"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_inode.h"
17 #include "xfs_trans.h"
18 #include "xfs_alloc.h"
19 #include "xfs_btree.h"
21 #include "xfs_rmap_btree.h"
22 #include "xfs_trace.h"
23 #include "xfs_cksum.h"
24 #include "xfs_error.h"
25 #include "xfs_extent_busy.h"
26 #include "xfs_ag_resv.h"
31 * This is a per-ag tree used to track the owner(s) of a given extent. With
32 * reflink it is possible for there to be multiple owners, which is a departure
33 * from classic XFS. Owner records for data extents are inserted when the
34 * extent is mapped and removed when an extent is unmapped. Owner records for
35 * all other block types (i.e. metadata) are inserted when an extent is
36 * allocated and removed when an extent is freed. There can only be one owner
37 * of a metadata extent, usually an inode or some other metadata structure like
40 * The rmap btree is part of the free space management, so blocks for the tree
41 * are sourced from the agfl. Hence we need transaction reservation support for
42 * this tree so that the freelist is always large enough. This also impacts on
43 * the minimum space we need to leave free in the AG.
45 * The tree is ordered by [ag block, owner, offset]. This is a large key size,
46 * but it is the only way to enforce unique keys when a block can be owned by
47 * multiple files at any offset. There's no need to order/search by extent
48 * size for online updating/management of the tree. It is intended that most
49 * reverse lookups will be to find the owner(s) of a particular block, or to
50 * try to recover tree and file data from corrupt primary metadata.
53 static struct xfs_btree_cur
*
54 xfs_rmapbt_dup_cursor(
55 struct xfs_btree_cur
*cur
)
57 return xfs_rmapbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
58 cur
->bc_private
.a
.agbp
, cur
->bc_private
.a
.agno
);
63 struct xfs_btree_cur
*cur
,
64 union xfs_btree_ptr
*ptr
,
67 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
68 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
69 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
70 int btnum
= cur
->bc_btnum
;
71 struct xfs_perag
*pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
75 agf
->agf_roots
[btnum
] = ptr
->s
;
76 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
77 pag
->pagf_levels
[btnum
] += inc
;
80 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
84 xfs_rmapbt_alloc_block(
85 struct xfs_btree_cur
*cur
,
86 union xfs_btree_ptr
*start
,
87 union xfs_btree_ptr
*new,
90 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
91 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
95 /* Allocate the new block from the freelist. If we can't, give up. */
96 error
= xfs_alloc_get_freelist(cur
->bc_tp
, cur
->bc_private
.a
.agbp
,
101 trace_xfs_rmapbt_alloc_block(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
103 if (bno
== NULLAGBLOCK
) {
108 xfs_extent_busy_reuse(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
, 1,
111 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
112 new->s
= cpu_to_be32(bno
);
113 be32_add_cpu(&agf
->agf_rmap_blocks
, 1);
114 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_RMAP_BLOCKS
);
116 xfs_ag_resv_rmapbt_alloc(cur
->bc_mp
, cur
->bc_private
.a
.agno
);
123 xfs_rmapbt_free_block(
124 struct xfs_btree_cur
*cur
,
127 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
128 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
132 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
133 trace_xfs_rmapbt_free_block(cur
->bc_mp
, cur
->bc_private
.a
.agno
,
135 be32_add_cpu(&agf
->agf_rmap_blocks
, -1);
136 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_RMAP_BLOCKS
);
137 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
141 xfs_extent_busy_insert(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1,
142 XFS_EXTENT_BUSY_SKIP_DISCARD
);
143 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
145 xfs_ag_resv_rmapbt_free(cur
->bc_mp
, cur
->bc_private
.a
.agno
);
151 xfs_rmapbt_get_minrecs(
152 struct xfs_btree_cur
*cur
,
155 return cur
->bc_mp
->m_rmap_mnr
[level
!= 0];
159 xfs_rmapbt_get_maxrecs(
160 struct xfs_btree_cur
*cur
,
163 return cur
->bc_mp
->m_rmap_mxr
[level
!= 0];
167 xfs_rmapbt_init_key_from_rec(
168 union xfs_btree_key
*key
,
169 union xfs_btree_rec
*rec
)
171 key
->rmap
.rm_startblock
= rec
->rmap
.rm_startblock
;
172 key
->rmap
.rm_owner
= rec
->rmap
.rm_owner
;
173 key
->rmap
.rm_offset
= rec
->rmap
.rm_offset
;
177 * The high key for a reverse mapping record can be computed by shifting
178 * the startblock and offset to the highest value that would still map
179 * to that record. In practice this means that we add blockcount-1 to
180 * the startblock for all records, and if the record is for a data/attr
181 * fork mapping, we add blockcount-1 to the offset too.
184 xfs_rmapbt_init_high_key_from_rec(
185 union xfs_btree_key
*key
,
186 union xfs_btree_rec
*rec
)
191 adj
= be32_to_cpu(rec
->rmap
.rm_blockcount
) - 1;
193 key
->rmap
.rm_startblock
= rec
->rmap
.rm_startblock
;
194 be32_add_cpu(&key
->rmap
.rm_startblock
, adj
);
195 key
->rmap
.rm_owner
= rec
->rmap
.rm_owner
;
196 key
->rmap
.rm_offset
= rec
->rmap
.rm_offset
;
197 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec
->rmap
.rm_owner
)) ||
198 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec
->rmap
.rm_offset
)))
200 off
= be64_to_cpu(key
->rmap
.rm_offset
);
201 off
= (XFS_RMAP_OFF(off
) + adj
) | (off
& ~XFS_RMAP_OFF_MASK
);
202 key
->rmap
.rm_offset
= cpu_to_be64(off
);
206 xfs_rmapbt_init_rec_from_cur(
207 struct xfs_btree_cur
*cur
,
208 union xfs_btree_rec
*rec
)
210 rec
->rmap
.rm_startblock
= cpu_to_be32(cur
->bc_rec
.r
.rm_startblock
);
211 rec
->rmap
.rm_blockcount
= cpu_to_be32(cur
->bc_rec
.r
.rm_blockcount
);
212 rec
->rmap
.rm_owner
= cpu_to_be64(cur
->bc_rec
.r
.rm_owner
);
213 rec
->rmap
.rm_offset
= cpu_to_be64(
214 xfs_rmap_irec_offset_pack(&cur
->bc_rec
.r
));
218 xfs_rmapbt_init_ptr_from_cur(
219 struct xfs_btree_cur
*cur
,
220 union xfs_btree_ptr
*ptr
)
222 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
224 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agf
->agf_seqno
));
226 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
231 struct xfs_btree_cur
*cur
,
232 union xfs_btree_key
*key
)
234 struct xfs_rmap_irec
*rec
= &cur
->bc_rec
.r
;
235 struct xfs_rmap_key
*kp
= &key
->rmap
;
239 d
= (int64_t)be32_to_cpu(kp
->rm_startblock
) - rec
->rm_startblock
;
243 x
= be64_to_cpu(kp
->rm_owner
);
250 x
= XFS_RMAP_OFF(be64_to_cpu(kp
->rm_offset
));
260 xfs_rmapbt_diff_two_keys(
261 struct xfs_btree_cur
*cur
,
262 union xfs_btree_key
*k1
,
263 union xfs_btree_key
*k2
)
265 struct xfs_rmap_key
*kp1
= &k1
->rmap
;
266 struct xfs_rmap_key
*kp2
= &k2
->rmap
;
270 d
= (int64_t)be32_to_cpu(kp1
->rm_startblock
) -
271 be32_to_cpu(kp2
->rm_startblock
);
275 x
= be64_to_cpu(kp1
->rm_owner
);
276 y
= be64_to_cpu(kp2
->rm_owner
);
282 x
= XFS_RMAP_OFF(be64_to_cpu(kp1
->rm_offset
));
283 y
= XFS_RMAP_OFF(be64_to_cpu(kp2
->rm_offset
));
291 static xfs_failaddr_t
295 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
296 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
297 struct xfs_perag
*pag
= bp
->b_pag
;
302 * magic number and level verification
304 * During growfs operations, we can't verify the exact level or owner as
305 * the perag is not fully initialised and hence not attached to the
306 * buffer. In this case, check against the maximum tree depth.
308 * Similarly, during log recovery we will have a perag structure
309 * attached, but the agf information will not yet have been initialised
310 * from the on disk AGF. Again, we can only check against maximum limits
313 if (block
->bb_magic
!= cpu_to_be32(XFS_RMAP_CRC_MAGIC
))
314 return __this_address
;
316 if (!xfs_sb_version_hasrmapbt(&mp
->m_sb
))
317 return __this_address
;
318 fa
= xfs_btree_sblock_v5hdr_verify(bp
);
322 level
= be16_to_cpu(block
->bb_level
);
323 if (pag
&& pag
->pagf_init
) {
324 if (level
>= pag
->pagf_levels
[XFS_BTNUM_RMAPi
])
325 return __this_address
;
326 } else if (level
>= mp
->m_rmap_maxlevels
)
327 return __this_address
;
329 return xfs_btree_sblock_verify(bp
, mp
->m_rmap_mxr
[level
!= 0]);
333 xfs_rmapbt_read_verify(
338 if (!xfs_btree_sblock_verify_crc(bp
))
339 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
341 fa
= xfs_rmapbt_verify(bp
);
343 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
347 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
351 xfs_rmapbt_write_verify(
356 fa
= xfs_rmapbt_verify(bp
);
358 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
359 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
362 xfs_btree_sblock_calc_crc(bp
);
366 const struct xfs_buf_ops xfs_rmapbt_buf_ops
= {
367 .name
= "xfs_rmapbt",
368 .verify_read
= xfs_rmapbt_read_verify
,
369 .verify_write
= xfs_rmapbt_write_verify
,
370 .verify_struct
= xfs_rmapbt_verify
,
374 xfs_rmapbt_keys_inorder(
375 struct xfs_btree_cur
*cur
,
376 union xfs_btree_key
*k1
,
377 union xfs_btree_key
*k2
)
384 x
= be32_to_cpu(k1
->rmap
.rm_startblock
);
385 y
= be32_to_cpu(k2
->rmap
.rm_startblock
);
390 a
= be64_to_cpu(k1
->rmap
.rm_owner
);
391 b
= be64_to_cpu(k2
->rmap
.rm_owner
);
396 a
= XFS_RMAP_OFF(be64_to_cpu(k1
->rmap
.rm_offset
));
397 b
= XFS_RMAP_OFF(be64_to_cpu(k2
->rmap
.rm_offset
));
404 xfs_rmapbt_recs_inorder(
405 struct xfs_btree_cur
*cur
,
406 union xfs_btree_rec
*r1
,
407 union xfs_btree_rec
*r2
)
414 x
= be32_to_cpu(r1
->rmap
.rm_startblock
);
415 y
= be32_to_cpu(r2
->rmap
.rm_startblock
);
420 a
= be64_to_cpu(r1
->rmap
.rm_owner
);
421 b
= be64_to_cpu(r2
->rmap
.rm_owner
);
426 a
= XFS_RMAP_OFF(be64_to_cpu(r1
->rmap
.rm_offset
));
427 b
= XFS_RMAP_OFF(be64_to_cpu(r2
->rmap
.rm_offset
));
433 static const struct xfs_btree_ops xfs_rmapbt_ops
= {
434 .rec_len
= sizeof(struct xfs_rmap_rec
),
435 .key_len
= 2 * sizeof(struct xfs_rmap_key
),
437 .dup_cursor
= xfs_rmapbt_dup_cursor
,
438 .set_root
= xfs_rmapbt_set_root
,
439 .alloc_block
= xfs_rmapbt_alloc_block
,
440 .free_block
= xfs_rmapbt_free_block
,
441 .get_minrecs
= xfs_rmapbt_get_minrecs
,
442 .get_maxrecs
= xfs_rmapbt_get_maxrecs
,
443 .init_key_from_rec
= xfs_rmapbt_init_key_from_rec
,
444 .init_high_key_from_rec
= xfs_rmapbt_init_high_key_from_rec
,
445 .init_rec_from_cur
= xfs_rmapbt_init_rec_from_cur
,
446 .init_ptr_from_cur
= xfs_rmapbt_init_ptr_from_cur
,
447 .key_diff
= xfs_rmapbt_key_diff
,
448 .buf_ops
= &xfs_rmapbt_buf_ops
,
449 .diff_two_keys
= xfs_rmapbt_diff_two_keys
,
450 .keys_inorder
= xfs_rmapbt_keys_inorder
,
451 .recs_inorder
= xfs_rmapbt_recs_inorder
,
455 * Allocate a new allocation btree cursor.
457 struct xfs_btree_cur
*
458 xfs_rmapbt_init_cursor(
459 struct xfs_mount
*mp
,
460 struct xfs_trans
*tp
,
461 struct xfs_buf
*agbp
,
464 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
465 struct xfs_btree_cur
*cur
;
467 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_NOFS
);
470 /* Overlapping btree; 2 keys per pointer. */
471 cur
->bc_btnum
= XFS_BTNUM_RMAP
;
472 cur
->bc_flags
= XFS_BTREE_CRC_BLOCKS
| XFS_BTREE_OVERLAPPING
;
473 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
474 cur
->bc_ops
= &xfs_rmapbt_ops
;
475 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]);
476 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_rmap_2
);
478 cur
->bc_private
.a
.agbp
= agbp
;
479 cur
->bc_private
.a
.agno
= agno
;
485 * Calculate number of records in an rmap btree block.
492 blocklen
-= XFS_RMAP_BLOCK_LEN
;
495 return blocklen
/ sizeof(struct xfs_rmap_rec
);
497 (2 * sizeof(struct xfs_rmap_key
) + sizeof(xfs_rmap_ptr_t
));
500 /* Compute the maximum height of an rmap btree. */
502 xfs_rmapbt_compute_maxlevels(
503 struct xfs_mount
*mp
)
506 * On a non-reflink filesystem, the maximum number of rmap
507 * records is the number of blocks in the AG, hence the max
508 * rmapbt height is log_$maxrecs($agblocks). However, with
509 * reflink each AG block can have up to 2^32 (per the refcount
510 * record format) owners, which means that theoretically we
511 * could face up to 2^64 rmap records.
513 * That effectively means that the max rmapbt height must be
514 * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
515 * blocks to feed the rmapbt long before the rmapbt reaches
516 * maximum height. The reflink code uses ag_resv_critical to
517 * disallow reflinking when less than 10% of the per-AG metadata
518 * block reservation since the fallback is a regular file copy.
520 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
521 mp
->m_rmap_maxlevels
= XFS_BTREE_MAXLEVELS
;
523 mp
->m_rmap_maxlevels
= xfs_btree_compute_maxlevels(
524 mp
->m_rmap_mnr
, mp
->m_sb
.sb_agblocks
);
527 /* Calculate the refcount btree size for some records. */
529 xfs_rmapbt_calc_size(
530 struct xfs_mount
*mp
,
531 unsigned long long len
)
533 return xfs_btree_calc_size(mp
->m_rmap_mnr
, len
);
537 * Calculate the maximum refcount btree size.
541 struct xfs_mount
*mp
,
542 xfs_agblock_t agblocks
)
544 /* Bail out if we're uninitialized, which can happen in mkfs. */
545 if (mp
->m_rmap_mxr
[0] == 0)
548 return xfs_rmapbt_calc_size(mp
, agblocks
);
552 * Figure out how many blocks to reserve and how many are used by this btree.
555 xfs_rmapbt_calc_reserves(
556 struct xfs_mount
*mp
,
557 struct xfs_trans
*tp
,
562 struct xfs_buf
*agbp
;
564 xfs_agblock_t agblocks
;
565 xfs_extlen_t tree_len
;
568 if (!xfs_sb_version_hasrmapbt(&mp
->m_sb
))
571 error
= xfs_alloc_read_agf(mp
, tp
, agno
, 0, &agbp
);
575 agf
= XFS_BUF_TO_AGF(agbp
);
576 agblocks
= be32_to_cpu(agf
->agf_length
);
577 tree_len
= be32_to_cpu(agf
->agf_rmap_blocks
);
578 xfs_trans_brelse(tp
, agbp
);
580 /* Reserve 1% of the AG or enough for 1 block per record. */
581 *ask
+= max(agblocks
/ 100, xfs_rmapbt_max_size(mp
, agblocks
));