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
;
42 agf
->agf_roots
[btnum
] = ptr
->s
;
43 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
44 cur
->bc_mp
->m_perag
[seqno
].pagf_levels
[btnum
] += inc
;
46 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
50 xfs_allocbt_alloc_block(
51 struct xfs_btree_cur
*cur
,
52 union xfs_btree_ptr
*start
,
53 union xfs_btree_ptr
*new,
60 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
62 /* Allocate the new block from the freelist. If we can't, give up. */
63 error
= xfs_alloc_get_freelist(cur
->bc_tp
, cur
->bc_private
.a
.agbp
,
66 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
70 if (bno
== NULLAGBLOCK
) {
71 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
76 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
77 new->s
= cpu_to_be32(bno
);
79 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
85 xfs_allocbt_free_block(
86 struct xfs_btree_cur
*cur
,
89 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
90 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
94 bno
= XFS_DADDR_TO_AGBNO(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
95 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
100 * Since blocks move to the free list without the coordination used in
101 * xfs_bmap_finish, we can't allow block to be available for
102 * reallocation and non-transaction writing (user data) until we know
103 * that the transaction that moved it to the free list is permanently
104 * on disk. We track the blocks by declaring these blocks as "busy";
105 * the busy list is maintained on a per-ag basis and each transaction
106 * records which entries should be removed when the iclog commits to
107 * disk. If a busy block is allocated, the iclog is pushed up to the
108 * LSN that freed the block.
110 xfs_alloc_mark_busy(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1);
111 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
116 * Update the longest extent in the AGF
119 xfs_allocbt_update_lastrec(
120 struct xfs_btree_cur
*cur
,
121 struct xfs_btree_block
*block
,
122 union xfs_btree_rec
*rec
,
126 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
127 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
131 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
136 * If this is the last leaf block and it's the last record,
137 * then update the size of the longest extent in the AG.
139 if (ptr
!= xfs_btree_get_numrecs(block
))
141 len
= rec
->alloc
.ar_blockcount
;
144 if (be32_to_cpu(rec
->alloc
.ar_blockcount
) <=
145 be32_to_cpu(agf
->agf_longest
))
147 len
= rec
->alloc
.ar_blockcount
;
150 numrecs
= xfs_btree_get_numrecs(block
);
153 ASSERT(ptr
== numrecs
+ 1);
156 xfs_alloc_rec_t
*rrp
;
158 rrp
= XFS_ALLOC_REC_ADDR(cur
->bc_mp
, block
, numrecs
);
159 len
= rrp
->ar_blockcount
;
170 agf
->agf_longest
= len
;
171 cur
->bc_mp
->m_perag
[seqno
].pagf_longest
= be32_to_cpu(len
);
172 xfs_alloc_log_agf(cur
->bc_tp
, cur
->bc_private
.a
.agbp
, XFS_AGF_LONGEST
);
176 xfs_allocbt_get_minrecs(
177 struct xfs_btree_cur
*cur
,
180 return cur
->bc_mp
->m_alloc_mnr
[level
!= 0];
184 xfs_allocbt_get_maxrecs(
185 struct xfs_btree_cur
*cur
,
188 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
192 xfs_allocbt_init_key_from_rec(
193 union xfs_btree_key
*key
,
194 union xfs_btree_rec
*rec
)
196 ASSERT(rec
->alloc
.ar_startblock
!= 0);
198 key
->alloc
.ar_startblock
= rec
->alloc
.ar_startblock
;
199 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
203 xfs_allocbt_init_rec_from_key(
204 union xfs_btree_key
*key
,
205 union xfs_btree_rec
*rec
)
207 ASSERT(key
->alloc
.ar_startblock
!= 0);
209 rec
->alloc
.ar_startblock
= key
->alloc
.ar_startblock
;
210 rec
->alloc
.ar_blockcount
= key
->alloc
.ar_blockcount
;
214 xfs_allocbt_init_rec_from_cur(
215 struct xfs_btree_cur
*cur
,
216 union xfs_btree_rec
*rec
)
218 ASSERT(cur
->bc_rec
.a
.ar_startblock
!= 0);
220 rec
->alloc
.ar_startblock
= cpu_to_be32(cur
->bc_rec
.a
.ar_startblock
);
221 rec
->alloc
.ar_blockcount
= cpu_to_be32(cur
->bc_rec
.a
.ar_blockcount
);
225 xfs_allocbt_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
];
238 xfs_allocbt_key_diff(
239 struct xfs_btree_cur
*cur
,
240 union xfs_btree_key
*key
)
242 xfs_alloc_rec_incore_t
*rec
= &cur
->bc_rec
.a
;
243 xfs_alloc_key_t
*kp
= &key
->alloc
;
246 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
247 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) -
251 diff
= (__int64_t
)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
255 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
259 xfs_allocbt_kill_root(
260 struct xfs_btree_cur
*cur
,
263 union xfs_btree_ptr
*newroot
)
267 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
268 XFS_BTREE_STATS_INC(cur
, killroot
);
271 * Update the root pointer, decreasing the level by 1 and then
274 xfs_allocbt_set_root(cur
, newroot
, -1);
275 error
= xfs_allocbt_free_block(cur
, bp
);
277 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
281 XFS_BTREE_STATS_INC(cur
, free
);
283 xfs_btree_setbuf(cur
, level
, NULL
);
286 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
292 xfs_allocbt_keys_inorder(
293 struct xfs_btree_cur
*cur
,
294 union xfs_btree_key
*k1
,
295 union xfs_btree_key
*k2
)
297 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
298 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
299 be32_to_cpu(k2
->alloc
.ar_startblock
);
301 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
302 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
303 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
304 be32_to_cpu(k1
->alloc
.ar_startblock
) <
305 be32_to_cpu(k2
->alloc
.ar_startblock
));
310 xfs_allocbt_recs_inorder(
311 struct xfs_btree_cur
*cur
,
312 union xfs_btree_rec
*r1
,
313 union xfs_btree_rec
*r2
)
315 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
316 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
317 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
318 be32_to_cpu(r2
->alloc
.ar_startblock
);
320 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
321 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
322 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
323 be32_to_cpu(r1
->alloc
.ar_startblock
) <
324 be32_to_cpu(r2
->alloc
.ar_startblock
));
329 #ifdef XFS_BTREE_TRACE
330 ktrace_t
*xfs_allocbt_trace_buf
;
333 xfs_allocbt_trace_enter(
334 struct xfs_btree_cur
*cur
,
351 ktrace_enter(xfs_allocbt_trace_buf
, (void *)(__psint_t
)type
,
352 (void *)func
, (void *)s
, NULL
, (void *)cur
,
353 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
354 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
355 (void *)a8
, (void *)a9
, (void *)a10
);
359 xfs_allocbt_trace_cursor(
360 struct xfs_btree_cur
*cur
,
365 *s0
= cur
->bc_private
.a
.agno
;
366 *l0
= cur
->bc_rec
.a
.ar_startblock
;
367 *l1
= cur
->bc_rec
.a
.ar_blockcount
;
371 xfs_allocbt_trace_key(
372 struct xfs_btree_cur
*cur
,
373 union xfs_btree_key
*key
,
377 *l0
= be32_to_cpu(key
->alloc
.ar_startblock
);
378 *l1
= be32_to_cpu(key
->alloc
.ar_blockcount
);
382 xfs_allocbt_trace_record(
383 struct xfs_btree_cur
*cur
,
384 union xfs_btree_rec
*rec
,
389 *l0
= be32_to_cpu(rec
->alloc
.ar_startblock
);
390 *l1
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
393 #endif /* XFS_BTREE_TRACE */
395 static const struct xfs_btree_ops xfs_allocbt_ops
= {
396 .rec_len
= sizeof(xfs_alloc_rec_t
),
397 .key_len
= sizeof(xfs_alloc_key_t
),
399 .dup_cursor
= xfs_allocbt_dup_cursor
,
400 .set_root
= xfs_allocbt_set_root
,
401 .kill_root
= xfs_allocbt_kill_root
,
402 .alloc_block
= xfs_allocbt_alloc_block
,
403 .free_block
= xfs_allocbt_free_block
,
404 .update_lastrec
= xfs_allocbt_update_lastrec
,
405 .get_minrecs
= xfs_allocbt_get_minrecs
,
406 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
407 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
408 .init_rec_from_key
= xfs_allocbt_init_rec_from_key
,
409 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
410 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
411 .key_diff
= xfs_allocbt_key_diff
,
414 .keys_inorder
= xfs_allocbt_keys_inorder
,
415 .recs_inorder
= xfs_allocbt_recs_inorder
,
418 #ifdef XFS_BTREE_TRACE
419 .trace_enter
= xfs_allocbt_trace_enter
,
420 .trace_cursor
= xfs_allocbt_trace_cursor
,
421 .trace_key
= xfs_allocbt_trace_key
,
422 .trace_record
= xfs_allocbt_trace_record
,
427 * Allocate a new allocation btree cursor.
429 struct xfs_btree_cur
* /* new alloc btree cursor */
430 xfs_allocbt_init_cursor(
431 struct xfs_mount
*mp
, /* file system mount point */
432 struct xfs_trans
*tp
, /* transaction pointer */
433 struct xfs_buf
*agbp
, /* buffer for agf structure */
434 xfs_agnumber_t agno
, /* allocation group number */
435 xfs_btnum_t btnum
) /* btree identifier */
437 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
438 struct xfs_btree_cur
*cur
;
440 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
442 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
446 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[btnum
]);
447 cur
->bc_btnum
= btnum
;
448 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
450 cur
->bc_ops
= &xfs_allocbt_ops
;
451 if (btnum
== XFS_BTNUM_CNT
)
452 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
454 cur
->bc_private
.a
.agbp
= agbp
;
455 cur
->bc_private
.a
.agno
= agno
;
461 * Calculate number of records in an alloc btree block.
465 struct xfs_mount
*mp
,
469 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
472 return blocklen
/ sizeof(xfs_alloc_rec_t
);
473 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));