2 * Copyright (c) 2014 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_alloc.h"
31 #include "xfs_btree.h"
33 #include "xfs_rmap_btree.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
36 #include "xfs_error.h"
37 #include "xfs_extent_busy.h"
42 * This is a per-ag tree used to track the owner(s) of a given extent. With
43 * reflink it is possible for there to be multiple owners, which is a departure
44 * from classic XFS. Owner records for data extents are inserted when the
45 * extent is mapped and removed when an extent is unmapped. Owner records for
46 * all other block types (i.e. metadata) are inserted when an extent is
47 * allocated and removed when an extent is freed. There can only be one owner
48 * of a metadata extent, usually an inode or some other metadata structure like
51 * The rmap btree is part of the free space management, so blocks for the tree
52 * are sourced from the agfl. Hence we need transaction reservation support for
53 * this tree so that the freelist is always large enough. This also impacts on
54 * the minimum space we need to leave free in the AG.
56 * The tree is ordered by [ag block, owner, offset]. This is a large key size,
57 * but it is the only way to enforce unique keys when a block can be owned by
58 * multiple files at any offset. There's no need to order/search by extent
59 * size for online updating/management of the tree. It is intended that most
60 * reverse lookups will be to find the owner(s) of a particular block, or to
61 * try to recover tree and file data from corrupt primary metadata.
64 static struct xfs_btree_cur *
65 xfs_rmapbt_dup_cursor(
66 struct xfs_btree_cur *cur)
68 return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
69 cur->bc_private.a.agbp, cur->bc_private.a.agno);
74 struct xfs_btree_cur *cur,
75 union xfs_btree_ptr *ptr,
78 struct xfs_buf *agbp = cur->bc_private.a.agbp;
79 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
80 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
81 int btnum = cur->bc_btnum;
82 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
86 agf->agf_roots[btnum] = ptr->s;
87 be32_add_cpu(&agf->agf_levels[btnum], inc);
88 pag->pagf_levels[btnum] += inc;
91 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
95 xfs_rmapbt_alloc_block(
96 struct xfs_btree_cur *cur,
97 union xfs_btree_ptr *start,
98 union xfs_btree_ptr *new,
104 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
106 /* Allocate the new block from the freelist. If we can't, give up. */
107 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
110 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
114 trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
116 if (bno == NULLAGBLOCK) {
117 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
122 xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
125 xfs_trans_agbtree_delta(cur->bc_tp, 1);
126 new->s = cpu_to_be32(bno);
128 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
134 xfs_rmapbt_free_block(
135 struct xfs_btree_cur *cur,
138 struct xfs_buf *agbp = cur->bc_private.a.agbp;
139 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
143 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
144 trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
146 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
150 xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
151 XFS_EXTENT_BUSY_SKIP_DISCARD);
152 xfs_trans_agbtree_delta(cur->bc_tp, -1);
158 xfs_rmapbt_get_minrecs(
159 struct xfs_btree_cur *cur,
162 return cur->bc_mp->m_rmap_mnr[level != 0];
166 xfs_rmapbt_get_maxrecs(
167 struct xfs_btree_cur *cur,
170 return cur->bc_mp->m_rmap_mxr[level != 0];
174 xfs_rmapbt_init_key_from_rec(
175 union xfs_btree_key *key,
176 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 = rec->rmap.rm_offset;
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 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 = rec->rmap.rm_offset;
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 = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
231 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
232 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
234 ptr->s = agf->agf_roots[cur->bc_btnum];
239 struct xfs_btree_cur *cur,
240 union xfs_btree_key *key)
242 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
243 struct xfs_rmap_key *kp = &key->rmap;
247 d = (__int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
251 x = be64_to_cpu(kp->rm_owner);
258 x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
268 xfs_rmapbt_diff_two_keys(
269 struct xfs_btree_cur *cur,
270 union xfs_btree_key *k1,
271 union xfs_btree_key *k2)
273 struct xfs_rmap_key *kp1 = &k1->rmap;
274 struct xfs_rmap_key *kp2 = &k2->rmap;
278 d = (__int64_t)be32_to_cpu(kp1->rm_startblock) -
279 be32_to_cpu(kp2->rm_startblock);
283 x = be64_to_cpu(kp1->rm_owner);
284 y = be64_to_cpu(kp2->rm_owner);
290 x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
291 y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
303 struct xfs_mount *mp = bp->b_target->bt_mount;
304 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
305 struct xfs_perag *pag = bp->b_pag;
309 * magic number and level verification
311 * During growfs operations, we can't verify the exact level or owner as
312 * the perag is not fully initialised and hence not attached to the
313 * buffer. In this case, check against the maximum tree depth.
315 * Similarly, during log recovery we will have a perag structure
316 * attached, but the agf information will not yet have been initialised
317 * from the on disk AGF. Again, we can only check against maximum limits
320 if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
323 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
325 if (!xfs_btree_sblock_v5hdr_verify(bp))
328 level = be16_to_cpu(block->bb_level);
329 if (pag && pag->pagf_init) {
330 if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
332 } else if (level >= mp->m_rmap_maxlevels)
335 return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
339 xfs_rmapbt_read_verify(
342 if (!xfs_btree_sblock_verify_crc(bp))
343 xfs_buf_ioerror(bp, -EFSBADCRC);
344 else if (!xfs_rmapbt_verify(bp))
345 xfs_buf_ioerror(bp, -EFSCORRUPTED);
348 trace_xfs_btree_corrupt(bp, _RET_IP_);
349 xfs_verifier_error(bp);
354 xfs_rmapbt_write_verify(
357 if (!xfs_rmapbt_verify(bp)) {
358 trace_xfs_btree_corrupt(bp, _RET_IP_);
359 xfs_buf_ioerror(bp, -EFSCORRUPTED);
360 xfs_verifier_error(bp);
363 xfs_btree_sblock_calc_crc(bp);
367 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
368 .name = "xfs_rmapbt",
369 .verify_read = xfs_rmapbt_read_verify,
370 .verify_write = xfs_rmapbt_write_verify,
373 #if defined(DEBUG) || defined(XFS_WARN)
375 xfs_rmapbt_keys_inorder(
376 struct xfs_btree_cur *cur,
377 union xfs_btree_key *k1,
378 union xfs_btree_key *k2)
385 x = be32_to_cpu(k1->rmap.rm_startblock);
386 y = be32_to_cpu(k2->rmap.rm_startblock);
391 a = be64_to_cpu(k1->rmap.rm_owner);
392 b = be64_to_cpu(k2->rmap.rm_owner);
397 a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
398 b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
405 xfs_rmapbt_recs_inorder(
406 struct xfs_btree_cur *cur,
407 union xfs_btree_rec *r1,
408 union xfs_btree_rec *r2)
415 x = be32_to_cpu(r1->rmap.rm_startblock);
416 y = be32_to_cpu(r2->rmap.rm_startblock);
421 a = be64_to_cpu(r1->rmap.rm_owner);
422 b = be64_to_cpu(r2->rmap.rm_owner);
427 a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
428 b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
435 static const struct xfs_btree_ops xfs_rmapbt_ops = {
436 .rec_len = sizeof(struct xfs_rmap_rec),
437 .key_len = 2 * sizeof(struct xfs_rmap_key),
439 .dup_cursor = xfs_rmapbt_dup_cursor,
440 .set_root = xfs_rmapbt_set_root,
441 .alloc_block = xfs_rmapbt_alloc_block,
442 .free_block = xfs_rmapbt_free_block,
443 .get_minrecs = xfs_rmapbt_get_minrecs,
444 .get_maxrecs = xfs_rmapbt_get_maxrecs,
445 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
446 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
447 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
448 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
449 .key_diff = xfs_rmapbt_key_diff,
450 .buf_ops = &xfs_rmapbt_buf_ops,
451 .diff_two_keys = xfs_rmapbt_diff_two_keys,
452 #if defined(DEBUG) || defined(XFS_WARN)
453 .keys_inorder = xfs_rmapbt_keys_inorder,
454 .recs_inorder = xfs_rmapbt_recs_inorder,
459 * Allocate a new allocation btree cursor.
461 struct xfs_btree_cur *
462 xfs_rmapbt_init_cursor(
463 struct xfs_mount *mp,
464 struct xfs_trans *tp,
465 struct xfs_buf *agbp,
468 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
469 struct xfs_btree_cur *cur;
471 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
474 /* Overlapping btree; 2 keys per pointer. */
475 cur->bc_btnum = XFS_BTNUM_RMAP;
476 cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
477 cur->bc_blocklog = mp->m_sb.sb_blocklog;
478 cur->bc_ops = &xfs_rmapbt_ops;
479 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
481 cur->bc_private.a.agbp = agbp;
482 cur->bc_private.a.agno = agno;
488 * Calculate number of records in an rmap btree block.
492 struct xfs_mount *mp,
496 blocklen -= XFS_RMAP_BLOCK_LEN;
499 return blocklen / sizeof(struct xfs_rmap_rec);
501 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
504 /* Compute the maximum height of an rmap btree. */
506 xfs_rmapbt_compute_maxlevels(
507 struct xfs_mount *mp)
509 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
510 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);