1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, 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"
13 #include "xfs_mount.h"
14 #include "xfs_btree.h"
15 #include "xfs_alloc_btree.h"
16 #include "xfs_alloc.h"
17 #include "xfs_trace.h"
18 #include "xfs_cksum.h"
19 #include "xfs_trans.h"
22 STATIC
struct xfs_btree_cur
*
23 xfs_allocbt_dup_cursor(
24 struct xfs_btree_cur
*cur
)
26 return xfs_allocbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
27 cur
->bc_private
.a
.agbp
, cur
->bc_private
.a
.agno
,
33 struct xfs_btree_cur
*cur
,
34 union xfs_btree_ptr
*ptr
,
37 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
38 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
39 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
40 int btnum
= cur
->bc_btnum
;
41 struct xfs_perag
*pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
45 agf
->agf_roots
[btnum
] = ptr
->s
;
46 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
47 pag
->pagf_levels
[btnum
] += inc
;
50 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
54 xfs_allocbt_alloc_block(
55 struct xfs_btree_cur
*cur
,
56 union xfs_btree_ptr
*start
,
57 union xfs_btree_ptr
*new,
63 /* Allocate the new block from the freelist. If we can't, give up. */
64 error
= xfs_alloc_get_freelist(cur
->bc_tp
, cur
->bc_private
.a
.agbp
,
69 if (bno
== NULLAGBLOCK
) {
74 xfs_extent_busy_reuse(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
, 1, false);
76 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
77 new->s
= cpu_to_be32(bno
);
84 xfs_allocbt_free_block(
85 struct xfs_btree_cur
*cur
,
88 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
89 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
93 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
94 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
98 xfs_extent_busy_insert(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1,
99 XFS_EXTENT_BUSY_SKIP_DISCARD
);
100 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
105 * Update the longest extent in the AGF
108 xfs_allocbt_update_lastrec(
109 struct xfs_btree_cur
*cur
,
110 struct xfs_btree_block
*block
,
111 union xfs_btree_rec
*rec
,
115 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
116 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
117 struct xfs_perag
*pag
;
121 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
126 * If this is the last leaf block and it's the last record,
127 * then update the size of the longest extent in the AG.
129 if (ptr
!= xfs_btree_get_numrecs(block
))
131 len
= rec
->alloc
.ar_blockcount
;
134 if (be32_to_cpu(rec
->alloc
.ar_blockcount
) <=
135 be32_to_cpu(agf
->agf_longest
))
137 len
= rec
->alloc
.ar_blockcount
;
140 numrecs
= xfs_btree_get_numrecs(block
);
143 ASSERT(ptr
== numrecs
+ 1);
146 xfs_alloc_rec_t
*rrp
;
148 rrp
= XFS_ALLOC_REC_ADDR(cur
->bc_mp
, block
, numrecs
);
149 len
= rrp
->ar_blockcount
;
160 agf
->agf_longest
= len
;
161 pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
162 pag
->pagf_longest
= be32_to_cpu(len
);
164 xfs_alloc_log_agf(cur
->bc_tp
, cur
->bc_private
.a
.agbp
, XFS_AGF_LONGEST
);
168 xfs_allocbt_get_minrecs(
169 struct xfs_btree_cur
*cur
,
172 return cur
->bc_mp
->m_alloc_mnr
[level
!= 0];
176 xfs_allocbt_get_maxrecs(
177 struct xfs_btree_cur
*cur
,
180 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
184 xfs_allocbt_init_key_from_rec(
185 union xfs_btree_key
*key
,
186 union xfs_btree_rec
*rec
)
188 key
->alloc
.ar_startblock
= rec
->alloc
.ar_startblock
;
189 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
193 xfs_bnobt_init_high_key_from_rec(
194 union xfs_btree_key
*key
,
195 union xfs_btree_rec
*rec
)
199 x
= be32_to_cpu(rec
->alloc
.ar_startblock
);
200 x
+= be32_to_cpu(rec
->alloc
.ar_blockcount
) - 1;
201 key
->alloc
.ar_startblock
= cpu_to_be32(x
);
202 key
->alloc
.ar_blockcount
= 0;
206 xfs_cntbt_init_high_key_from_rec(
207 union xfs_btree_key
*key
,
208 union xfs_btree_rec
*rec
)
210 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
211 key
->alloc
.ar_startblock
= 0;
215 xfs_allocbt_init_rec_from_cur(
216 struct xfs_btree_cur
*cur
,
217 union xfs_btree_rec
*rec
)
219 rec
->alloc
.ar_startblock
= cpu_to_be32(cur
->bc_rec
.a
.ar_startblock
);
220 rec
->alloc
.ar_blockcount
= cpu_to_be32(cur
->bc_rec
.a
.ar_blockcount
);
224 xfs_allocbt_init_ptr_from_cur(
225 struct xfs_btree_cur
*cur
,
226 union xfs_btree_ptr
*ptr
)
228 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
230 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agf
->agf_seqno
));
232 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
237 struct xfs_btree_cur
*cur
,
238 union xfs_btree_key
*key
)
240 xfs_alloc_rec_incore_t
*rec
= &cur
->bc_rec
.a
;
241 xfs_alloc_key_t
*kp
= &key
->alloc
;
243 return (int64_t)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
248 struct xfs_btree_cur
*cur
,
249 union xfs_btree_key
*key
)
251 xfs_alloc_rec_incore_t
*rec
= &cur
->bc_rec
.a
;
252 xfs_alloc_key_t
*kp
= &key
->alloc
;
255 diff
= (int64_t)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
259 return (int64_t)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
263 xfs_bnobt_diff_two_keys(
264 struct xfs_btree_cur
*cur
,
265 union xfs_btree_key
*k1
,
266 union xfs_btree_key
*k2
)
268 return (int64_t)be32_to_cpu(k1
->alloc
.ar_startblock
) -
269 be32_to_cpu(k2
->alloc
.ar_startblock
);
273 xfs_cntbt_diff_two_keys(
274 struct xfs_btree_cur
*cur
,
275 union xfs_btree_key
*k1
,
276 union xfs_btree_key
*k2
)
280 diff
= be32_to_cpu(k1
->alloc
.ar_blockcount
) -
281 be32_to_cpu(k2
->alloc
.ar_blockcount
);
285 return be32_to_cpu(k1
->alloc
.ar_startblock
) -
286 be32_to_cpu(k2
->alloc
.ar_startblock
);
289 static xfs_failaddr_t
293 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
294 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
295 struct xfs_perag
*pag
= bp
->b_pag
;
300 * magic number and level verification
302 * During growfs operations, we can't verify the exact level or owner as
303 * the perag is not fully initialised and hence not attached to the
304 * buffer. In this case, check against the maximum tree depth.
306 * Similarly, during log recovery we will have a perag structure
307 * attached, but the agf information will not yet have been initialised
308 * from the on disk AGF. Again, we can only check against maximum limits
311 level
= be16_to_cpu(block
->bb_level
);
312 switch (block
->bb_magic
) {
313 case cpu_to_be32(XFS_ABTB_CRC_MAGIC
):
314 fa
= xfs_btree_sblock_v5hdr_verify(bp
);
318 case cpu_to_be32(XFS_ABTB_MAGIC
):
319 if (pag
&& pag
->pagf_init
) {
320 if (level
>= pag
->pagf_levels
[XFS_BTNUM_BNOi
])
321 return __this_address
;
322 } else if (level
>= mp
->m_ag_maxlevels
)
323 return __this_address
;
325 case cpu_to_be32(XFS_ABTC_CRC_MAGIC
):
326 fa
= xfs_btree_sblock_v5hdr_verify(bp
);
330 case cpu_to_be32(XFS_ABTC_MAGIC
):
331 if (pag
&& pag
->pagf_init
) {
332 if (level
>= pag
->pagf_levels
[XFS_BTNUM_CNTi
])
333 return __this_address
;
334 } else if (level
>= mp
->m_ag_maxlevels
)
335 return __this_address
;
338 return __this_address
;
341 return xfs_btree_sblock_verify(bp
, mp
->m_alloc_mxr
[level
!= 0]);
345 xfs_allocbt_read_verify(
350 if (!xfs_btree_sblock_verify_crc(bp
))
351 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
353 fa
= xfs_allocbt_verify(bp
);
355 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
359 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
363 xfs_allocbt_write_verify(
368 fa
= xfs_allocbt_verify(bp
);
370 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
371 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
374 xfs_btree_sblock_calc_crc(bp
);
378 const struct xfs_buf_ops xfs_allocbt_buf_ops
= {
379 .name
= "xfs_allocbt",
380 .verify_read
= xfs_allocbt_read_verify
,
381 .verify_write
= xfs_allocbt_write_verify
,
382 .verify_struct
= xfs_allocbt_verify
,
387 xfs_bnobt_keys_inorder(
388 struct xfs_btree_cur
*cur
,
389 union xfs_btree_key
*k1
,
390 union xfs_btree_key
*k2
)
392 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
393 be32_to_cpu(k2
->alloc
.ar_startblock
);
397 xfs_bnobt_recs_inorder(
398 struct xfs_btree_cur
*cur
,
399 union xfs_btree_rec
*r1
,
400 union xfs_btree_rec
*r2
)
402 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
403 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
404 be32_to_cpu(r2
->alloc
.ar_startblock
);
408 xfs_cntbt_keys_inorder(
409 struct xfs_btree_cur
*cur
,
410 union xfs_btree_key
*k1
,
411 union xfs_btree_key
*k2
)
413 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
414 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
415 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
416 be32_to_cpu(k1
->alloc
.ar_startblock
) <
417 be32_to_cpu(k2
->alloc
.ar_startblock
));
421 xfs_cntbt_recs_inorder(
422 struct xfs_btree_cur
*cur
,
423 union xfs_btree_rec
*r1
,
424 union xfs_btree_rec
*r2
)
426 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
427 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
428 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
429 be32_to_cpu(r1
->alloc
.ar_startblock
) <
430 be32_to_cpu(r2
->alloc
.ar_startblock
));
433 static const struct xfs_btree_ops xfs_bnobt_ops
= {
434 .rec_len
= sizeof(xfs_alloc_rec_t
),
435 .key_len
= sizeof(xfs_alloc_key_t
),
437 .dup_cursor
= xfs_allocbt_dup_cursor
,
438 .set_root
= xfs_allocbt_set_root
,
439 .alloc_block
= xfs_allocbt_alloc_block
,
440 .free_block
= xfs_allocbt_free_block
,
441 .update_lastrec
= xfs_allocbt_update_lastrec
,
442 .get_minrecs
= xfs_allocbt_get_minrecs
,
443 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
444 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
445 .init_high_key_from_rec
= xfs_bnobt_init_high_key_from_rec
,
446 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
447 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
448 .key_diff
= xfs_bnobt_key_diff
,
449 .buf_ops
= &xfs_allocbt_buf_ops
,
450 .diff_two_keys
= xfs_bnobt_diff_two_keys
,
451 .keys_inorder
= xfs_bnobt_keys_inorder
,
452 .recs_inorder
= xfs_bnobt_recs_inorder
,
455 static const struct xfs_btree_ops xfs_cntbt_ops
= {
456 .rec_len
= sizeof(xfs_alloc_rec_t
),
457 .key_len
= sizeof(xfs_alloc_key_t
),
459 .dup_cursor
= xfs_allocbt_dup_cursor
,
460 .set_root
= xfs_allocbt_set_root
,
461 .alloc_block
= xfs_allocbt_alloc_block
,
462 .free_block
= xfs_allocbt_free_block
,
463 .update_lastrec
= xfs_allocbt_update_lastrec
,
464 .get_minrecs
= xfs_allocbt_get_minrecs
,
465 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
466 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
467 .init_high_key_from_rec
= xfs_cntbt_init_high_key_from_rec
,
468 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
469 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
470 .key_diff
= xfs_cntbt_key_diff
,
471 .buf_ops
= &xfs_allocbt_buf_ops
,
472 .diff_two_keys
= xfs_cntbt_diff_two_keys
,
473 .keys_inorder
= xfs_cntbt_keys_inorder
,
474 .recs_inorder
= xfs_cntbt_recs_inorder
,
478 * Allocate a new allocation btree cursor.
480 struct xfs_btree_cur
* /* new alloc btree cursor */
481 xfs_allocbt_init_cursor(
482 struct xfs_mount
*mp
, /* file system mount point */
483 struct xfs_trans
*tp
, /* transaction pointer */
484 struct xfs_buf
*agbp
, /* buffer for agf structure */
485 xfs_agnumber_t agno
, /* allocation group number */
486 xfs_btnum_t btnum
) /* btree identifier */
488 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
489 struct xfs_btree_cur
*cur
;
491 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
493 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_NOFS
);
497 cur
->bc_btnum
= btnum
;
498 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
500 if (btnum
== XFS_BTNUM_CNT
) {
501 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_abtc_2
);
502 cur
->bc_ops
= &xfs_cntbt_ops
;
503 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
504 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
506 cur
->bc_statoff
= XFS_STATS_CALC_INDEX(xs_abtb_2
);
507 cur
->bc_ops
= &xfs_bnobt_ops
;
508 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
511 cur
->bc_private
.a
.agbp
= agbp
;
512 cur
->bc_private
.a
.agno
= agno
;
514 if (xfs_sb_version_hascrc(&mp
->m_sb
))
515 cur
->bc_flags
|= XFS_BTREE_CRC_BLOCKS
;
521 * Calculate number of records in an alloc btree block.
525 struct xfs_mount
*mp
,
529 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
532 return blocklen
/ sizeof(xfs_alloc_rec_t
);
533 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));
536 /* Calculate the freespace btree size for some records. */
538 xfs_allocbt_calc_size(
539 struct xfs_mount
*mp
,
540 unsigned long long len
)
542 return xfs_btree_calc_size(mp
->m_alloc_mnr
, len
);