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_trans_agbtree_delta(cur
->bc_tp
, 1);
79 new->s
= cpu_to_be32(bno
);
81 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
87 xfs_allocbt_free_block(
88 struct xfs_btree_cur
*cur
,
91 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
92 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
96 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
97 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
102 * Since blocks move to the free list without the coordination used in
103 * xfs_bmap_finish, we can't allow block to be available for
104 * reallocation and non-transaction writing (user data) until we know
105 * that the transaction that moved it to the free list is permanently
106 * on disk. We track the blocks by declaring these blocks as "busy";
107 * the busy list is maintained on a per-ag basis and each transaction
108 * records which entries should be removed when the iclog commits to
109 * disk. If a busy block is allocated, the iclog is pushed up to the
110 * LSN that freed the block.
112 xfs_alloc_busy_insert(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1);
113 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
118 * Update the longest extent in the AGF
121 xfs_allocbt_update_lastrec(
122 struct xfs_btree_cur
*cur
,
123 struct xfs_btree_block
*block
,
124 union xfs_btree_rec
*rec
,
128 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
129 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
130 struct xfs_perag
*pag
;
134 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
139 * If this is the last leaf block and it's the last record,
140 * then update the size of the longest extent in the AG.
142 if (ptr
!= xfs_btree_get_numrecs(block
))
144 len
= rec
->alloc
.ar_blockcount
;
147 if (be32_to_cpu(rec
->alloc
.ar_blockcount
) <=
148 be32_to_cpu(agf
->agf_longest
))
150 len
= rec
->alloc
.ar_blockcount
;
153 numrecs
= xfs_btree_get_numrecs(block
);
156 ASSERT(ptr
== numrecs
+ 1);
159 xfs_alloc_rec_t
*rrp
;
161 rrp
= XFS_ALLOC_REC_ADDR(cur
->bc_mp
, block
, numrecs
);
162 len
= rrp
->ar_blockcount
;
173 agf
->agf_longest
= len
;
174 pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
175 pag
->pagf_longest
= be32_to_cpu(len
);
177 xfs_alloc_log_agf(cur
->bc_tp
, cur
->bc_private
.a
.agbp
, XFS_AGF_LONGEST
);
181 xfs_allocbt_get_minrecs(
182 struct xfs_btree_cur
*cur
,
185 return cur
->bc_mp
->m_alloc_mnr
[level
!= 0];
189 xfs_allocbt_get_maxrecs(
190 struct xfs_btree_cur
*cur
,
193 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
197 xfs_allocbt_init_key_from_rec(
198 union xfs_btree_key
*key
,
199 union xfs_btree_rec
*rec
)
201 ASSERT(rec
->alloc
.ar_startblock
!= 0);
203 key
->alloc
.ar_startblock
= rec
->alloc
.ar_startblock
;
204 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
208 xfs_allocbt_init_rec_from_key(
209 union xfs_btree_key
*key
,
210 union xfs_btree_rec
*rec
)
212 ASSERT(key
->alloc
.ar_startblock
!= 0);
214 rec
->alloc
.ar_startblock
= key
->alloc
.ar_startblock
;
215 rec
->alloc
.ar_blockcount
= key
->alloc
.ar_blockcount
;
219 xfs_allocbt_init_rec_from_cur(
220 struct xfs_btree_cur
*cur
,
221 union xfs_btree_rec
*rec
)
223 ASSERT(cur
->bc_rec
.a
.ar_startblock
!= 0);
225 rec
->alloc
.ar_startblock
= cpu_to_be32(cur
->bc_rec
.a
.ar_startblock
);
226 rec
->alloc
.ar_blockcount
= cpu_to_be32(cur
->bc_rec
.a
.ar_blockcount
);
230 xfs_allocbt_init_ptr_from_cur(
231 struct xfs_btree_cur
*cur
,
232 union xfs_btree_ptr
*ptr
)
234 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
236 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agf
->agf_seqno
));
237 ASSERT(agf
->agf_roots
[cur
->bc_btnum
] != 0);
239 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
243 xfs_allocbt_key_diff(
244 struct xfs_btree_cur
*cur
,
245 union xfs_btree_key
*key
)
247 xfs_alloc_rec_incore_t
*rec
= &cur
->bc_rec
.a
;
248 xfs_alloc_key_t
*kp
= &key
->alloc
;
251 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
252 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) -
256 diff
= (__int64_t
)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
260 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
265 xfs_allocbt_keys_inorder(
266 struct xfs_btree_cur
*cur
,
267 union xfs_btree_key
*k1
,
268 union xfs_btree_key
*k2
)
270 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
271 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
272 be32_to_cpu(k2
->alloc
.ar_startblock
);
274 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
275 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
276 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
277 be32_to_cpu(k1
->alloc
.ar_startblock
) <
278 be32_to_cpu(k2
->alloc
.ar_startblock
));
283 xfs_allocbt_recs_inorder(
284 struct xfs_btree_cur
*cur
,
285 union xfs_btree_rec
*r1
,
286 union xfs_btree_rec
*r2
)
288 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
289 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
290 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
291 be32_to_cpu(r2
->alloc
.ar_startblock
);
293 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
294 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
295 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
296 be32_to_cpu(r1
->alloc
.ar_startblock
) <
297 be32_to_cpu(r2
->alloc
.ar_startblock
));
302 #ifdef XFS_BTREE_TRACE
303 ktrace_t
*xfs_allocbt_trace_buf
;
306 xfs_allocbt_trace_enter(
307 struct xfs_btree_cur
*cur
,
324 ktrace_enter(xfs_allocbt_trace_buf
, (void *)(__psint_t
)type
,
325 (void *)func
, (void *)s
, NULL
, (void *)cur
,
326 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
327 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
328 (void *)a8
, (void *)a9
, (void *)a10
);
332 xfs_allocbt_trace_cursor(
333 struct xfs_btree_cur
*cur
,
338 *s0
= cur
->bc_private
.a
.agno
;
339 *l0
= cur
->bc_rec
.a
.ar_startblock
;
340 *l1
= cur
->bc_rec
.a
.ar_blockcount
;
344 xfs_allocbt_trace_key(
345 struct xfs_btree_cur
*cur
,
346 union xfs_btree_key
*key
,
350 *l0
= be32_to_cpu(key
->alloc
.ar_startblock
);
351 *l1
= be32_to_cpu(key
->alloc
.ar_blockcount
);
355 xfs_allocbt_trace_record(
356 struct xfs_btree_cur
*cur
,
357 union xfs_btree_rec
*rec
,
362 *l0
= be32_to_cpu(rec
->alloc
.ar_startblock
);
363 *l1
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
366 #endif /* XFS_BTREE_TRACE */
368 static const struct xfs_btree_ops xfs_allocbt_ops
= {
369 .rec_len
= sizeof(xfs_alloc_rec_t
),
370 .key_len
= sizeof(xfs_alloc_key_t
),
372 .dup_cursor
= xfs_allocbt_dup_cursor
,
373 .set_root
= xfs_allocbt_set_root
,
374 .alloc_block
= xfs_allocbt_alloc_block
,
375 .free_block
= xfs_allocbt_free_block
,
376 .update_lastrec
= xfs_allocbt_update_lastrec
,
377 .get_minrecs
= xfs_allocbt_get_minrecs
,
378 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
379 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
380 .init_rec_from_key
= xfs_allocbt_init_rec_from_key
,
381 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
382 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
383 .key_diff
= xfs_allocbt_key_diff
,
386 .keys_inorder
= xfs_allocbt_keys_inorder
,
387 .recs_inorder
= xfs_allocbt_recs_inorder
,
390 #ifdef XFS_BTREE_TRACE
391 .trace_enter
= xfs_allocbt_trace_enter
,
392 .trace_cursor
= xfs_allocbt_trace_cursor
,
393 .trace_key
= xfs_allocbt_trace_key
,
394 .trace_record
= xfs_allocbt_trace_record
,
399 * Allocate a new allocation btree cursor.
401 struct xfs_btree_cur
* /* new alloc btree cursor */
402 xfs_allocbt_init_cursor(
403 struct xfs_mount
*mp
, /* file system mount point */
404 struct xfs_trans
*tp
, /* transaction pointer */
405 struct xfs_buf
*agbp
, /* buffer for agf structure */
406 xfs_agnumber_t agno
, /* allocation group number */
407 xfs_btnum_t btnum
) /* btree identifier */
409 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
410 struct xfs_btree_cur
*cur
;
412 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
414 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
418 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[btnum
]);
419 cur
->bc_btnum
= btnum
;
420 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
422 cur
->bc_ops
= &xfs_allocbt_ops
;
423 if (btnum
== XFS_BTNUM_CNT
)
424 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
426 cur
->bc_private
.a
.agbp
= agbp
;
427 cur
->bc_private
.a
.agno
= agno
;
433 * Calculate number of records in an alloc btree block.
437 struct xfs_mount
*mp
,
441 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
444 return blocklen
/ sizeof(xfs_alloc_rec_t
);
445 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));