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,
61 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
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
,
67 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
71 if (bno
== NULLAGBLOCK
) {
72 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
77 xfs_extent_busy_reuse(cur
->bc_mp
, cur
->bc_private
.a
.agno
, bno
, 1, false);
79 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
80 new->s
= cpu_to_be32(bno
);
82 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
88 xfs_allocbt_free_block(
89 struct xfs_btree_cur
*cur
,
92 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
93 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(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 * Similarly, during log recovery we will have a perag structure
273 * attached, but the agf information will not yet have been initialised
274 * from the on disk AGF. Again, we can only check against maximum limits
277 level
= be16_to_cpu(block
->bb_level
);
278 switch (block
->bb_magic
) {
279 case cpu_to_be32(XFS_ABTB_CRC_MAGIC
):
280 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
282 if (!uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
))
284 if (block
->bb_u
.s
.bb_blkno
!= cpu_to_be64(bp
->b_bn
))
287 be32_to_cpu(block
->bb_u
.s
.bb_owner
) != pag
->pag_agno
)
290 case cpu_to_be32(XFS_ABTB_MAGIC
):
291 if (pag
&& pag
->pagf_init
) {
292 if (level
>= pag
->pagf_levels
[XFS_BTNUM_BNOi
])
294 } else if (level
>= mp
->m_ag_maxlevels
)
297 case cpu_to_be32(XFS_ABTC_CRC_MAGIC
):
298 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
300 if (!uuid_equal(&block
->bb_u
.s
.bb_uuid
, &mp
->m_sb
.sb_uuid
))
302 if (block
->bb_u
.s
.bb_blkno
!= cpu_to_be64(bp
->b_bn
))
305 be32_to_cpu(block
->bb_u
.s
.bb_owner
) != pag
->pag_agno
)
308 case cpu_to_be32(XFS_ABTC_MAGIC
):
309 if (pag
&& pag
->pagf_init
) {
310 if (level
>= pag
->pagf_levels
[XFS_BTNUM_CNTi
])
312 } else if (level
>= mp
->m_ag_maxlevels
)
319 /* numrecs verification */
320 if (be16_to_cpu(block
->bb_numrecs
) > mp
->m_alloc_mxr
[level
!= 0])
323 /* sibling pointer verification */
324 if (!block
->bb_u
.s
.bb_leftsib
||
325 (be32_to_cpu(block
->bb_u
.s
.bb_leftsib
) >= mp
->m_sb
.sb_agblocks
&&
326 block
->bb_u
.s
.bb_leftsib
!= cpu_to_be32(NULLAGBLOCK
)))
328 if (!block
->bb_u
.s
.bb_rightsib
||
329 (be32_to_cpu(block
->bb_u
.s
.bb_rightsib
) >= mp
->m_sb
.sb_agblocks
&&
330 block
->bb_u
.s
.bb_rightsib
!= cpu_to_be32(NULLAGBLOCK
)))
337 xfs_allocbt_read_verify(
340 if (!xfs_btree_sblock_verify_crc(bp
))
341 xfs_buf_ioerror(bp
, EFSBADCRC
);
342 else if (!xfs_allocbt_verify(bp
))
343 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
346 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
347 xfs_verifier_error(bp
);
352 xfs_allocbt_write_verify(
355 if (!xfs_allocbt_verify(bp
)) {
356 trace_xfs_btree_corrupt(bp
, _RET_IP_
);
357 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
358 xfs_verifier_error(bp
);
361 xfs_btree_sblock_calc_crc(bp
);
365 const struct xfs_buf_ops xfs_allocbt_buf_ops
= {
366 .verify_read
= xfs_allocbt_read_verify
,
367 .verify_write
= xfs_allocbt_write_verify
,
371 #if defined(DEBUG) || defined(XFS_WARN)
373 xfs_allocbt_keys_inorder(
374 struct xfs_btree_cur
*cur
,
375 union xfs_btree_key
*k1
,
376 union xfs_btree_key
*k2
)
378 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
379 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
380 be32_to_cpu(k2
->alloc
.ar_startblock
);
382 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
383 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
384 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
385 be32_to_cpu(k1
->alloc
.ar_startblock
) <
386 be32_to_cpu(k2
->alloc
.ar_startblock
));
391 xfs_allocbt_recs_inorder(
392 struct xfs_btree_cur
*cur
,
393 union xfs_btree_rec
*r1
,
394 union xfs_btree_rec
*r2
)
396 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
397 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
398 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
399 be32_to_cpu(r2
->alloc
.ar_startblock
);
401 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
402 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
403 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
404 be32_to_cpu(r1
->alloc
.ar_startblock
) <
405 be32_to_cpu(r2
->alloc
.ar_startblock
));
410 static const struct xfs_btree_ops xfs_allocbt_ops
= {
411 .rec_len
= sizeof(xfs_alloc_rec_t
),
412 .key_len
= sizeof(xfs_alloc_key_t
),
414 .dup_cursor
= xfs_allocbt_dup_cursor
,
415 .set_root
= xfs_allocbt_set_root
,
416 .alloc_block
= xfs_allocbt_alloc_block
,
417 .free_block
= xfs_allocbt_free_block
,
418 .update_lastrec
= xfs_allocbt_update_lastrec
,
419 .get_minrecs
= xfs_allocbt_get_minrecs
,
420 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
421 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
422 .init_rec_from_key
= xfs_allocbt_init_rec_from_key
,
423 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
424 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
425 .key_diff
= xfs_allocbt_key_diff
,
426 .buf_ops
= &xfs_allocbt_buf_ops
,
427 #if defined(DEBUG) || defined(XFS_WARN)
428 .keys_inorder
= xfs_allocbt_keys_inorder
,
429 .recs_inorder
= xfs_allocbt_recs_inorder
,
434 * Allocate a new allocation btree cursor.
436 struct xfs_btree_cur
* /* new alloc btree cursor */
437 xfs_allocbt_init_cursor(
438 struct xfs_mount
*mp
, /* file system mount point */
439 struct xfs_trans
*tp
, /* transaction pointer */
440 struct xfs_buf
*agbp
, /* buffer for agf structure */
441 xfs_agnumber_t agno
, /* allocation group number */
442 xfs_btnum_t btnum
) /* btree identifier */
444 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
445 struct xfs_btree_cur
*cur
;
447 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
449 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
453 cur
->bc_btnum
= btnum
;
454 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
455 cur
->bc_ops
= &xfs_allocbt_ops
;
457 if (btnum
== XFS_BTNUM_CNT
) {
458 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
459 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
461 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
464 cur
->bc_private
.a
.agbp
= agbp
;
465 cur
->bc_private
.a
.agno
= agno
;
467 if (xfs_sb_version_hascrc(&mp
->m_sb
))
468 cur
->bc_flags
|= XFS_BTREE_CRC_BLOCKS
;
474 * Calculate number of records in an alloc btree block.
478 struct xfs_mount
*mp
,
482 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
485 return blocklen
/ sizeof(xfs_alloc_rec_t
);
486 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));