2 * Copyright (c) 2000-2001,2005 Silicon Graphics, 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 STATIC
struct xfs_btree_cur
*
21 xfs_allocbt_dup_cursor(
22 struct xfs_btree_cur
*cur
)
24 return xfs_allocbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
25 cur
->bc_private
.a
.agbp
, cur
->bc_private
.a
.agno
,
31 struct xfs_btree_cur
*cur
,
32 union xfs_btree_ptr
*ptr
,
35 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
36 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
37 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
38 int btnum
= cur
->bc_btnum
;
39 struct xfs_perag
*pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
43 agf
->agf_roots
[btnum
] = ptr
->s
;
44 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
45 pag
->pagf_levels
[btnum
] += inc
;
48 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
52 xfs_allocbt_alloc_block(
53 struct xfs_btree_cur
*cur
,
54 union xfs_btree_ptr
*start
,
55 union xfs_btree_ptr
*new,
62 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
64 /* Allocate the new block from the freelist. If we can't, give up. */
65 error
= xfs_alloc_get_freelist(cur
->bc_tp
, cur
->bc_private
.a
.agbp
,
68 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
72 if (bno
== NULLAGBLOCK
) {
73 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
78 xfs_extent_busy_reuse(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
, 1, false);
80 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
81 new->s
= cpu_to_be32(bno
);
83 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
89 xfs_allocbt_free_block(
90 struct xfs_btree_cur
*cur
,
93 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
97 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
98 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
102 xfs_extent_busy_insert(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1,
103 XFS_EXTENT_BUSY_SKIP_DISCARD
);
104 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
106 xfs_trans_binval(cur
->bc_tp
, bp
);
111 * Update the longest extent in the AGF
114 xfs_allocbt_update_lastrec(
115 struct xfs_btree_cur
*cur
,
116 struct xfs_btree_block
*block
,
117 union xfs_btree_rec
*rec
,
121 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
122 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
123 struct xfs_perag
*pag
;
127 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
132 * If this is the last leaf block and it's the last record,
133 * then update the size of the longest extent in the AG.
135 if (ptr
!= xfs_btree_get_numrecs(block
))
137 len
= rec
->alloc
.ar_blockcount
;
140 if (be32_to_cpu(rec
->alloc
.ar_blockcount
) <=
141 be32_to_cpu(agf
->agf_longest
))
143 len
= rec
->alloc
.ar_blockcount
;
146 numrecs
= xfs_btree_get_numrecs(block
);
149 ASSERT(ptr
== numrecs
+ 1);
152 xfs_alloc_rec_t
*rrp
;
154 rrp
= XFS_ALLOC_REC_ADDR(cur
->bc_mp
, block
, numrecs
);
155 len
= rrp
->ar_blockcount
;
166 agf
->agf_longest
= len
;
167 pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
168 pag
->pagf_longest
= be32_to_cpu(len
);
170 xfs_alloc_log_agf(cur
->bc_tp
, cur
->bc_private
.a
.agbp
, XFS_AGF_LONGEST
);
174 xfs_allocbt_get_minrecs(
175 struct xfs_btree_cur
*cur
,
178 return cur
->bc_mp
->m_alloc_mnr
[level
!= 0];
182 xfs_allocbt_get_maxrecs(
183 struct xfs_btree_cur
*cur
,
186 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
190 xfs_allocbt_init_key_from_rec(
191 union xfs_btree_key
*key
,
192 union xfs_btree_rec
*rec
)
194 ASSERT(rec
->alloc
.ar_startblock
!= 0);
196 key
->alloc
.ar_startblock
= rec
->alloc
.ar_startblock
;
197 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
201 xfs_allocbt_init_rec_from_key(
202 union xfs_btree_key
*key
,
203 union xfs_btree_rec
*rec
)
205 ASSERT(key
->alloc
.ar_startblock
!= 0);
207 rec
->alloc
.ar_startblock
= key
->alloc
.ar_startblock
;
208 rec
->alloc
.ar_blockcount
= key
->alloc
.ar_blockcount
;
212 xfs_allocbt_init_rec_from_cur(
213 struct xfs_btree_cur
*cur
,
214 union xfs_btree_rec
*rec
)
216 ASSERT(cur
->bc_rec
.a
.ar_startblock
!= 0);
218 rec
->alloc
.ar_startblock
= cpu_to_be32(cur
->bc_rec
.a
.ar_startblock
);
219 rec
->alloc
.ar_blockcount
= cpu_to_be32(cur
->bc_rec
.a
.ar_blockcount
);
223 xfs_allocbt_init_ptr_from_cur(
224 struct xfs_btree_cur
*cur
,
225 union xfs_btree_ptr
*ptr
)
227 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
229 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agf
->agf_seqno
));
230 ASSERT(agf
->agf_roots
[cur
->bc_btnum
] != 0);
232 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
236 xfs_allocbt_key_diff(
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
;
244 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
245 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) -
249 diff
= (__int64_t
)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
253 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
260 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
261 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
262 struct xfs_perag
*pag
= bp
->b_pag
;
266 * magic number and level verification
268 * During growfs operations, we can't verify the exact level or owner as
269 * the perag is not fully initialised and hence not attached to the
270 * buffer. In this case, check against the maximum tree depth.
272 level
= be16_to_cpu(block
->bb_level
);
273 switch (cpu_to_be32(block
->bb_magic
)) {
274 case XFS_ABTB_CRC_MAGIC
:
275 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
277 if (!uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
))
279 if (block
->bb_u
.s
.bb_blkno
!= cpu_to_be64(bp
->b_bn
))
282 be32_to_cpu(block
->bb_u
.s
.bb_owner
) != pag
->pag_agno
)
287 if (level
>= pag
->pagf_levels
[XFS_BTNUM_BNOi
])
289 } else if (level
>= mp
->m_ag_maxlevels
)
292 case XFS_ABTC_CRC_MAGIC
:
293 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
295 if (!uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
))
297 if (block
->bb_u
.s
.bb_blkno
!= cpu_to_be64(bp
->b_bn
))
300 be32_to_cpu(block
->bb_u
.s
.bb_owner
) != pag
->pag_agno
)
305 if (level
>= pag
->pagf_levels
[XFS_BTNUM_CNTi
])
307 } else if (level
>= mp
->m_ag_maxlevels
)
314 /* numrecs verification */
315 if (be16_to_cpu(block
->bb_numrecs
) > mp
->m_alloc_mxr
[level
!= 0])
318 /* sibling pointer verification */
319 if (!block
->bb_u
.s
.bb_leftsib
||
320 (be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) >= mp
->m_sb
.sb_agblocks
&&
321 block
->bb_u
.s
.bb_leftsib
!= cpu_to_be32(NULLAGBLOCK
)))
323 if (!block
->bb_u
.s
.bb_rightsib
||
324 (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) >= mp
->m_sb
.sb_agblocks
&&
325 block
->bb_u
.s
.bb_rightsib
!= cpu_to_be32(NULLAGBLOCK
)))
332 xfs_allocbt_read_verify(
335 if (!(xfs_btree_sblock_verify_crc(bp
) &&
336 xfs_allocbt_verify(bp
))) {
337 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
338 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
,
339 bp
->b_target
->bt_mount
, bp
->b_addr
);
340 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
345 xfs_allocbt_write_verify(
348 if (!xfs_allocbt_verify(bp
)) {
349 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
350 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
,
351 bp
->b_target
->bt_mount
, bp
->b_addr
);
352 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
354 xfs_btree_sblock_calc_crc(bp
);
358 const struct xfs_buf_ops xfs_allocbt_buf_ops
= {
359 .verify_read
= xfs_allocbt_read_verify
,
360 .verify_write
= xfs_allocbt_write_verify
,
366 xfs_allocbt_keys_inorder(
367 struct xfs_btree_cur
*cur
,
368 union xfs_btree_key
*k1
,
369 union xfs_btree_key
*k2
)
371 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
372 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
373 be32_to_cpu(k2
->alloc
.ar_startblock
);
375 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
376 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
377 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
378 be32_to_cpu(k1
->alloc
.ar_startblock
) <
379 be32_to_cpu(k2
->alloc
.ar_startblock
));
384 xfs_allocbt_recs_inorder(
385 struct xfs_btree_cur
*cur
,
386 union xfs_btree_rec
*r1
,
387 union xfs_btree_rec
*r2
)
389 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
390 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
391 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
392 be32_to_cpu(r2
->alloc
.ar_startblock
);
394 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
395 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
396 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
397 be32_to_cpu(r1
->alloc
.ar_startblock
) <
398 be32_to_cpu(r2
->alloc
.ar_startblock
));
403 #ifdef XFS_BTREE_TRACE
404 ktrace_t
*xfs_allocbt_trace_buf
;
407 xfs_allocbt_trace_enter(
408 struct xfs_btree_cur
*cur
,
425 ktrace_enter(xfs_allocbt_trace_buf
, (void *)(__psint_t
)type
,
426 (void *)func
, (void *)s
, NULL
, (void *)cur
,
427 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
428 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
429 (void *)a8
, (void *)a9
, (void *)a10
);
433 xfs_allocbt_trace_cursor(
434 struct xfs_btree_cur
*cur
,
439 *s0
= cur
->bc_private
.a
.agno
;
440 *l0
= cur
->bc_rec
.a
.ar_startblock
;
441 *l1
= cur
->bc_rec
.a
.ar_blockcount
;
445 xfs_allocbt_trace_key(
446 struct xfs_btree_cur
*cur
,
447 union xfs_btree_key
*key
,
451 *l0
= be32_to_cpu(key
->alloc
.ar_startblock
);
452 *l1
= be32_to_cpu(key
->alloc
.ar_blockcount
);
456 xfs_allocbt_trace_record(
457 struct xfs_btree_cur
*cur
,
458 union xfs_btree_rec
*rec
,
463 *l0
= be32_to_cpu(rec
->alloc
.ar_startblock
);
464 *l1
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
467 #endif /* XFS_BTREE_TRACE */
469 static const struct xfs_btree_ops xfs_allocbt_ops
= {
470 .rec_len
= sizeof(xfs_alloc_rec_t
),
471 .key_len
= sizeof(xfs_alloc_key_t
),
473 .dup_cursor
= xfs_allocbt_dup_cursor
,
474 .set_root
= xfs_allocbt_set_root
,
475 .alloc_block
= xfs_allocbt_alloc_block
,
476 .free_block
= xfs_allocbt_free_block
,
477 .update_lastrec
= xfs_allocbt_update_lastrec
,
478 .get_minrecs
= xfs_allocbt_get_minrecs
,
479 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
480 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
481 .init_rec_from_key
= xfs_allocbt_init_rec_from_key
,
482 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
483 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
484 .key_diff
= xfs_allocbt_key_diff
,
485 .buf_ops
= &xfs_allocbt_buf_ops
,
487 .keys_inorder
= xfs_allocbt_keys_inorder
,
488 .recs_inorder
= xfs_allocbt_recs_inorder
,
491 #ifdef XFS_BTREE_TRACE
492 .trace_enter
= xfs_allocbt_trace_enter
,
493 .trace_cursor
= xfs_allocbt_trace_cursor
,
494 .trace_key
= xfs_allocbt_trace_key
,
495 .trace_record
= xfs_allocbt_trace_record
,
500 * Allocate a new allocation btree cursor.
502 struct xfs_btree_cur
* /* new alloc btree cursor */
503 xfs_allocbt_init_cursor(
504 struct xfs_mount
*mp
, /* file system mount point */
505 struct xfs_trans
*tp
, /* transaction pointer */
506 struct xfs_buf
*agbp
, /* buffer for agf structure */
507 xfs_agnumber_t agno
, /* allocation group number */
508 xfs_btnum_t btnum
) /* btree identifier */
510 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
511 struct xfs_btree_cur
*cur
;
513 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
515 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
519 cur
->bc_btnum
= btnum
;
520 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
521 cur
->bc_ops
= &xfs_allocbt_ops
;
523 if (btnum
== XFS_BTNUM_CNT
) {
524 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
525 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
527 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
530 cur
->bc_private
.a
.agbp
= agbp
;
531 cur
->bc_private
.a
.agno
= agno
;
533 if (xfs_sb_version_hascrc(&mp
->m_sb
))
534 cur
->bc_flags
|= XFS_BTREE_CRC_BLOCKS
;
540 * Calculate number of records in an alloc btree block.
544 struct xfs_mount
*mp
,
548 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
551 return blocklen
/ sizeof(xfs_alloc_rec_t
);
552 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));