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 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_btree_trace.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_alloc.h"
41 #include "xfs_error.h"
42 #include "xfs_trace.h"
45 STATIC
struct xfs_btree_cur
*
46 xfs_allocbt_dup_cursor(
47 struct xfs_btree_cur
*cur
)
49 return xfs_allocbt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
50 cur
->bc_private
.a
.agbp
, cur
->bc_private
.a
.agno
,
56 struct xfs_btree_cur
*cur
,
57 union xfs_btree_ptr
*ptr
,
60 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
61 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
62 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
63 int btnum
= cur
->bc_btnum
;
64 struct xfs_perag
*pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
68 agf
->agf_roots
[btnum
] = ptr
->s
;
69 be32_add_cpu(&agf
->agf_levels
[btnum
], inc
);
70 pag
->pagf_levels
[btnum
] += inc
;
73 xfs_alloc_log_agf(cur
->bc_tp
, agbp
, XFS_AGF_ROOTS
| XFS_AGF_LEVELS
);
77 xfs_allocbt_alloc_block(
78 struct xfs_btree_cur
*cur
,
79 union xfs_btree_ptr
*start
,
80 union xfs_btree_ptr
*new,
87 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
89 /* Allocate the new block from the freelist. If we can't, give up. */
90 error
= xfs_alloc_get_freelist(cur
->bc_tp
, cur
->bc_private
.a
.agbp
,
93 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
97 if (bno
== NULLAGBLOCK
) {
98 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
103 xfs_trans_agbtree_delta(cur
->bc_tp
, 1);
104 new->s
= cpu_to_be32(bno
);
106 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
112 xfs_allocbt_free_block(
113 struct xfs_btree_cur
*cur
,
116 struct xfs_buf
*agbp
= cur
->bc_private
.a
.agbp
;
117 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
121 bno
= xfs_daddr_to_agbno(cur
->bc_mp
, XFS_BUF_ADDR(bp
));
122 error
= xfs_alloc_put_freelist(cur
->bc_tp
, agbp
, NULL
, bno
, 1);
127 * Since blocks move to the free list without the coordination used in
128 * xfs_bmap_finish, we can't allow block to be available for
129 * reallocation and non-transaction writing (user data) until we know
130 * that the transaction that moved it to the free list is permanently
131 * on disk. We track the blocks by declaring these blocks as "busy";
132 * the busy list is maintained on a per-ag basis and each transaction
133 * records which entries should be removed when the iclog commits to
134 * disk. If a busy block is allocated, the iclog is pushed up to the
135 * LSN that freed the block.
137 xfs_alloc_mark_busy(cur
->bc_tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1);
138 xfs_trans_agbtree_delta(cur
->bc_tp
, -1);
143 * Update the longest extent in the AGF
146 xfs_allocbt_update_lastrec(
147 struct xfs_btree_cur
*cur
,
148 struct xfs_btree_block
*block
,
149 union xfs_btree_rec
*rec
,
153 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
154 xfs_agnumber_t seqno
= be32_to_cpu(agf
->agf_seqno
);
155 struct xfs_perag
*pag
;
159 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
164 * If this is the last leaf block and it's the last record,
165 * then update the size of the longest extent in the AG.
167 if (ptr
!= xfs_btree_get_numrecs(block
))
169 len
= rec
->alloc
.ar_blockcount
;
172 if (be32_to_cpu(rec
->alloc
.ar_blockcount
) <=
173 be32_to_cpu(agf
->agf_longest
))
175 len
= rec
->alloc
.ar_blockcount
;
178 numrecs
= xfs_btree_get_numrecs(block
);
181 ASSERT(ptr
== numrecs
+ 1);
184 xfs_alloc_rec_t
*rrp
;
186 rrp
= XFS_ALLOC_REC_ADDR(cur
->bc_mp
, block
, numrecs
);
187 len
= rrp
->ar_blockcount
;
198 agf
->agf_longest
= len
;
199 pag
= xfs_perag_get(cur
->bc_mp
, seqno
);
200 pag
->pagf_longest
= be32_to_cpu(len
);
202 xfs_alloc_log_agf(cur
->bc_tp
, cur
->bc_private
.a
.agbp
, XFS_AGF_LONGEST
);
206 xfs_allocbt_get_minrecs(
207 struct xfs_btree_cur
*cur
,
210 return cur
->bc_mp
->m_alloc_mnr
[level
!= 0];
214 xfs_allocbt_get_maxrecs(
215 struct xfs_btree_cur
*cur
,
218 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
222 xfs_allocbt_init_key_from_rec(
223 union xfs_btree_key
*key
,
224 union xfs_btree_rec
*rec
)
226 ASSERT(rec
->alloc
.ar_startblock
!= 0);
228 key
->alloc
.ar_startblock
= rec
->alloc
.ar_startblock
;
229 key
->alloc
.ar_blockcount
= rec
->alloc
.ar_blockcount
;
233 xfs_allocbt_init_rec_from_key(
234 union xfs_btree_key
*key
,
235 union xfs_btree_rec
*rec
)
237 ASSERT(key
->alloc
.ar_startblock
!= 0);
239 rec
->alloc
.ar_startblock
= key
->alloc
.ar_startblock
;
240 rec
->alloc
.ar_blockcount
= key
->alloc
.ar_blockcount
;
244 xfs_allocbt_init_rec_from_cur(
245 struct xfs_btree_cur
*cur
,
246 union xfs_btree_rec
*rec
)
248 ASSERT(cur
->bc_rec
.a
.ar_startblock
!= 0);
250 rec
->alloc
.ar_startblock
= cpu_to_be32(cur
->bc_rec
.a
.ar_startblock
);
251 rec
->alloc
.ar_blockcount
= cpu_to_be32(cur
->bc_rec
.a
.ar_blockcount
);
255 xfs_allocbt_init_ptr_from_cur(
256 struct xfs_btree_cur
*cur
,
257 union xfs_btree_ptr
*ptr
)
259 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(cur
->bc_private
.a
.agbp
);
261 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agf
->agf_seqno
));
262 ASSERT(agf
->agf_roots
[cur
->bc_btnum
] != 0);
264 ptr
->s
= agf
->agf_roots
[cur
->bc_btnum
];
268 xfs_allocbt_key_diff(
269 struct xfs_btree_cur
*cur
,
270 union xfs_btree_key
*key
)
272 xfs_alloc_rec_incore_t
*rec
= &cur
->bc_rec
.a
;
273 xfs_alloc_key_t
*kp
= &key
->alloc
;
276 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
277 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) -
281 diff
= (__int64_t
)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
285 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) - rec
->ar_startblock
;
289 xfs_allocbt_kill_root(
290 struct xfs_btree_cur
*cur
,
293 union xfs_btree_ptr
*newroot
)
297 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ENTRY
);
298 XFS_BTREE_STATS_INC(cur
, killroot
);
301 * Update the root pointer, decreasing the level by 1 and then
304 xfs_allocbt_set_root(cur
, newroot
, -1);
305 error
= xfs_allocbt_free_block(cur
, bp
);
307 XFS_BTREE_TRACE_CURSOR(cur
, XBT_ERROR
);
311 XFS_BTREE_STATS_INC(cur
, free
);
313 xfs_btree_setbuf(cur
, level
, NULL
);
316 XFS_BTREE_TRACE_CURSOR(cur
, XBT_EXIT
);
322 xfs_allocbt_keys_inorder(
323 struct xfs_btree_cur
*cur
,
324 union xfs_btree_key
*k1
,
325 union xfs_btree_key
*k2
)
327 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
328 return be32_to_cpu(k1
->alloc
.ar_startblock
) <
329 be32_to_cpu(k2
->alloc
.ar_startblock
);
331 return be32_to_cpu(k1
->alloc
.ar_blockcount
) <
332 be32_to_cpu(k2
->alloc
.ar_blockcount
) ||
333 (k1
->alloc
.ar_blockcount
== k2
->alloc
.ar_blockcount
&&
334 be32_to_cpu(k1
->alloc
.ar_startblock
) <
335 be32_to_cpu(k2
->alloc
.ar_startblock
));
340 xfs_allocbt_recs_inorder(
341 struct xfs_btree_cur
*cur
,
342 union xfs_btree_rec
*r1
,
343 union xfs_btree_rec
*r2
)
345 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
346 return be32_to_cpu(r1
->alloc
.ar_startblock
) +
347 be32_to_cpu(r1
->alloc
.ar_blockcount
) <=
348 be32_to_cpu(r2
->alloc
.ar_startblock
);
350 return be32_to_cpu(r1
->alloc
.ar_blockcount
) <
351 be32_to_cpu(r2
->alloc
.ar_blockcount
) ||
352 (r1
->alloc
.ar_blockcount
== r2
->alloc
.ar_blockcount
&&
353 be32_to_cpu(r1
->alloc
.ar_startblock
) <
354 be32_to_cpu(r2
->alloc
.ar_startblock
));
359 #ifdef XFS_BTREE_TRACE
360 ktrace_t
*xfs_allocbt_trace_buf
;
363 xfs_allocbt_trace_enter(
364 struct xfs_btree_cur
*cur
,
381 ktrace_enter(xfs_allocbt_trace_buf
, (void *)(__psint_t
)type
,
382 (void *)func
, (void *)s
, NULL
, (void *)cur
,
383 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
384 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
385 (void *)a8
, (void *)a9
, (void *)a10
);
389 xfs_allocbt_trace_cursor(
390 struct xfs_btree_cur
*cur
,
395 *s0
= cur
->bc_private
.a
.agno
;
396 *l0
= cur
->bc_rec
.a
.ar_startblock
;
397 *l1
= cur
->bc_rec
.a
.ar_blockcount
;
401 xfs_allocbt_trace_key(
402 struct xfs_btree_cur
*cur
,
403 union xfs_btree_key
*key
,
407 *l0
= be32_to_cpu(key
->alloc
.ar_startblock
);
408 *l1
= be32_to_cpu(key
->alloc
.ar_blockcount
);
412 xfs_allocbt_trace_record(
413 struct xfs_btree_cur
*cur
,
414 union xfs_btree_rec
*rec
,
419 *l0
= be32_to_cpu(rec
->alloc
.ar_startblock
);
420 *l1
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
423 #endif /* XFS_BTREE_TRACE */
425 static const struct xfs_btree_ops xfs_allocbt_ops
= {
426 .rec_len
= sizeof(xfs_alloc_rec_t
),
427 .key_len
= sizeof(xfs_alloc_key_t
),
429 .dup_cursor
= xfs_allocbt_dup_cursor
,
430 .set_root
= xfs_allocbt_set_root
,
431 .kill_root
= xfs_allocbt_kill_root
,
432 .alloc_block
= xfs_allocbt_alloc_block
,
433 .free_block
= xfs_allocbt_free_block
,
434 .update_lastrec
= xfs_allocbt_update_lastrec
,
435 .get_minrecs
= xfs_allocbt_get_minrecs
,
436 .get_maxrecs
= xfs_allocbt_get_maxrecs
,
437 .init_key_from_rec
= xfs_allocbt_init_key_from_rec
,
438 .init_rec_from_key
= xfs_allocbt_init_rec_from_key
,
439 .init_rec_from_cur
= xfs_allocbt_init_rec_from_cur
,
440 .init_ptr_from_cur
= xfs_allocbt_init_ptr_from_cur
,
441 .key_diff
= xfs_allocbt_key_diff
,
444 .keys_inorder
= xfs_allocbt_keys_inorder
,
445 .recs_inorder
= xfs_allocbt_recs_inorder
,
448 #ifdef XFS_BTREE_TRACE
449 .trace_enter
= xfs_allocbt_trace_enter
,
450 .trace_cursor
= xfs_allocbt_trace_cursor
,
451 .trace_key
= xfs_allocbt_trace_key
,
452 .trace_record
= xfs_allocbt_trace_record
,
457 * Allocate a new allocation btree cursor.
459 struct xfs_btree_cur
* /* new alloc btree cursor */
460 xfs_allocbt_init_cursor(
461 struct xfs_mount
*mp
, /* file system mount point */
462 struct xfs_trans
*tp
, /* transaction pointer */
463 struct xfs_buf
*agbp
, /* buffer for agf structure */
464 xfs_agnumber_t agno
, /* allocation group number */
465 xfs_btnum_t btnum
) /* btree identifier */
467 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
468 struct xfs_btree_cur
*cur
;
470 ASSERT(btnum
== XFS_BTNUM_BNO
|| btnum
== XFS_BTNUM_CNT
);
472 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
476 cur
->bc_nlevels
= be32_to_cpu(agf
->agf_levels
[btnum
]);
477 cur
->bc_btnum
= btnum
;
478 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
480 cur
->bc_ops
= &xfs_allocbt_ops
;
481 if (btnum
== XFS_BTNUM_CNT
)
482 cur
->bc_flags
= XFS_BTREE_LASTREC_UPDATE
;
484 cur
->bc_private
.a
.agbp
= agbp
;
485 cur
->bc_private
.a
.agno
= agno
;
491 * Calculate number of records in an alloc btree block.
495 struct xfs_mount
*mp
,
499 blocklen
-= XFS_ALLOC_BLOCK_LEN(mp
);
502 return blocklen
/ sizeof(xfs_alloc_rec_t
);
503 return blocklen
/ (sizeof(xfs_alloc_key_t
) + sizeof(xfs_alloc_ptr_t
));