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"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_btree.h"
34 #include "xfs_btree_trace.h"
35 #include "xfs_alloc.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
40 STATIC struct xfs_btree_cur *
41 xfs_allocbt_dup_cursor(
42 struct xfs_btree_cur *cur)
44 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
45 cur->bc_private.a.agbp, cur->bc_private.a.agno,
51 struct xfs_btree_cur *cur,
52 union xfs_btree_ptr *ptr,
55 struct xfs_buf *agbp = cur->bc_private.a.agbp;
56 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
57 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
58 int btnum = cur->bc_btnum;
59 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
63 agf->agf_roots[btnum] = ptr->s;
64 be32_add_cpu(&agf->agf_levels[btnum], inc);
65 pag->pagf_levels[btnum] += inc;
68 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
72 xfs_allocbt_alloc_block(
73 struct xfs_btree_cur *cur,
74 union xfs_btree_ptr *start,
75 union xfs_btree_ptr *new,
82 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
84 /* Allocate the new block from the freelist. If we can't, give up. */
85 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
88 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
92 if (bno == NULLAGBLOCK) {
93 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
97 if (xfs_alloc_busy_search(cur->bc_mp, cur->bc_private.a.agno, bno, 1))
98 xfs_trans_set_sync(cur->bc_tp);
100 xfs_trans_agbtree_delta(cur->bc_tp, 1);
101 new->s = cpu_to_be32(bno);
103 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
109 xfs_allocbt_free_block(
110 struct xfs_btree_cur *cur,
113 struct xfs_buf *agbp = cur->bc_private.a.agbp;
114 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
118 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
119 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
124 * Since blocks move to the free list without the coordination used in
125 * xfs_bmap_finish, we can't allow block to be available for
126 * reallocation and non-transaction writing (user data) until we know
127 * that the transaction that moved it to the free list is permanently
128 * on disk. We track the blocks by declaring these blocks as "busy";
129 * the busy list is maintained on a per-ag basis and each transaction
130 * records which entries should be removed when the iclog commits to
131 * disk. If a busy block is allocated, the iclog is pushed up to the
132 * LSN that freed the block.
134 xfs_alloc_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
135 xfs_trans_agbtree_delta(cur->bc_tp, -1);
140 * Update the longest extent in the AGF
143 xfs_allocbt_update_lastrec(
144 struct xfs_btree_cur *cur,
145 struct xfs_btree_block *block,
146 union xfs_btree_rec *rec,
150 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
151 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
152 struct xfs_perag *pag;
156 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
161 * If this is the last leaf block and it's the last record,
162 * then update the size of the longest extent in the AG.
164 if (ptr != xfs_btree_get_numrecs(block))
166 len = rec->alloc.ar_blockcount;
169 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
170 be32_to_cpu(agf->agf_longest))
172 len = rec->alloc.ar_blockcount;
175 numrecs = xfs_btree_get_numrecs(block);
178 ASSERT(ptr == numrecs + 1);
181 xfs_alloc_rec_t *rrp;
183 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
184 len = rrp->ar_blockcount;
195 agf->agf_longest = len;
196 pag = xfs_perag_get(cur->bc_mp, seqno);
197 pag->pagf_longest = be32_to_cpu(len);
199 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
203 xfs_allocbt_get_minrecs(
204 struct xfs_btree_cur *cur,
207 return cur->bc_mp->m_alloc_mnr[level != 0];
211 xfs_allocbt_get_maxrecs(
212 struct xfs_btree_cur *cur,
215 return cur->bc_mp->m_alloc_mxr[level != 0];
219 xfs_allocbt_init_key_from_rec(
220 union xfs_btree_key *key,
221 union xfs_btree_rec *rec)
223 ASSERT(rec->alloc.ar_startblock != 0);
225 key->alloc.ar_startblock = rec->alloc.ar_startblock;
226 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
230 xfs_allocbt_init_rec_from_key(
231 union xfs_btree_key *key,
232 union xfs_btree_rec *rec)
234 ASSERT(key->alloc.ar_startblock != 0);
236 rec->alloc.ar_startblock = key->alloc.ar_startblock;
237 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
241 xfs_allocbt_init_rec_from_cur(
242 struct xfs_btree_cur *cur,
243 union xfs_btree_rec *rec)
245 ASSERT(cur->bc_rec.a.ar_startblock != 0);
247 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
248 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
252 xfs_allocbt_init_ptr_from_cur(
253 struct xfs_btree_cur *cur,
254 union xfs_btree_ptr *ptr)
256 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
258 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
259 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
261 ptr->s = agf->agf_roots[cur->bc_btnum];
265 xfs_allocbt_key_diff(
266 struct xfs_btree_cur *cur,
267 union xfs_btree_key *key)
269 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
270 xfs_alloc_key_t *kp = &key->alloc;
273 if (cur->bc_btnum == XFS_BTNUM_BNO) {
274 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
278 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
282 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
287 xfs_allocbt_keys_inorder(
288 struct xfs_btree_cur *cur,
289 union xfs_btree_key *k1,
290 union xfs_btree_key *k2)
292 if (cur->bc_btnum == XFS_BTNUM_BNO) {
293 return be32_to_cpu(k1->alloc.ar_startblock) <
294 be32_to_cpu(k2->alloc.ar_startblock);
296 return be32_to_cpu(k1->alloc.ar_blockcount) <
297 be32_to_cpu(k2->alloc.ar_blockcount) ||
298 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
299 be32_to_cpu(k1->alloc.ar_startblock) <
300 be32_to_cpu(k2->alloc.ar_startblock));
305 xfs_allocbt_recs_inorder(
306 struct xfs_btree_cur *cur,
307 union xfs_btree_rec *r1,
308 union xfs_btree_rec *r2)
310 if (cur->bc_btnum == XFS_BTNUM_BNO) {
311 return be32_to_cpu(r1->alloc.ar_startblock) +
312 be32_to_cpu(r1->alloc.ar_blockcount) <=
313 be32_to_cpu(r2->alloc.ar_startblock);
315 return be32_to_cpu(r1->alloc.ar_blockcount) <
316 be32_to_cpu(r2->alloc.ar_blockcount) ||
317 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
318 be32_to_cpu(r1->alloc.ar_startblock) <
319 be32_to_cpu(r2->alloc.ar_startblock));
324 #ifdef XFS_BTREE_TRACE
325 ktrace_t *xfs_allocbt_trace_buf;
328 xfs_allocbt_trace_enter(
329 struct xfs_btree_cur *cur,
346 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
347 (void *)func, (void *)s, NULL, (void *)cur,
348 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
349 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
350 (void *)a8, (void *)a9, (void *)a10);
354 xfs_allocbt_trace_cursor(
355 struct xfs_btree_cur *cur,
360 *s0 = cur->bc_private.a.agno;
361 *l0 = cur->bc_rec.a.ar_startblock;
362 *l1 = cur->bc_rec.a.ar_blockcount;
366 xfs_allocbt_trace_key(
367 struct xfs_btree_cur *cur,
368 union xfs_btree_key *key,
372 *l0 = be32_to_cpu(key->alloc.ar_startblock);
373 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
377 xfs_allocbt_trace_record(
378 struct xfs_btree_cur *cur,
379 union xfs_btree_rec *rec,
384 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
385 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
388 #endif /* XFS_BTREE_TRACE */
390 static const struct xfs_btree_ops xfs_allocbt_ops = {
391 .rec_len = sizeof(xfs_alloc_rec_t),
392 .key_len = sizeof(xfs_alloc_key_t),
394 .dup_cursor = xfs_allocbt_dup_cursor,
395 .set_root = xfs_allocbt_set_root,
396 .alloc_block = xfs_allocbt_alloc_block,
397 .free_block = xfs_allocbt_free_block,
398 .update_lastrec = xfs_allocbt_update_lastrec,
399 .get_minrecs = xfs_allocbt_get_minrecs,
400 .get_maxrecs = xfs_allocbt_get_maxrecs,
401 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
402 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
403 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
404 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
405 .key_diff = xfs_allocbt_key_diff,
408 .keys_inorder = xfs_allocbt_keys_inorder,
409 .recs_inorder = xfs_allocbt_recs_inorder,
412 #ifdef XFS_BTREE_TRACE
413 .trace_enter = xfs_allocbt_trace_enter,
414 .trace_cursor = xfs_allocbt_trace_cursor,
415 .trace_key = xfs_allocbt_trace_key,
416 .trace_record = xfs_allocbt_trace_record,
421 * Allocate a new allocation btree cursor.
423 struct xfs_btree_cur * /* new alloc btree cursor */
424 xfs_allocbt_init_cursor(
425 struct xfs_mount *mp, /* file system mount point */
426 struct xfs_trans *tp, /* transaction pointer */
427 struct xfs_buf *agbp, /* buffer for agf structure */
428 xfs_agnumber_t agno, /* allocation group number */
429 xfs_btnum_t btnum) /* btree identifier */
431 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
432 struct xfs_btree_cur *cur;
434 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
436 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
440 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
441 cur->bc_btnum = btnum;
442 cur->bc_blocklog = mp->m_sb.sb_blocklog;
444 cur->bc_ops = &xfs_allocbt_ops;
445 if (btnum == XFS_BTNUM_CNT)
446 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
448 cur->bc_private.a.agbp = agbp;
449 cur->bc_private.a.agno = agno;
455 * Calculate number of records in an alloc btree block.
459 struct xfs_mount *mp,
463 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
466 return blocklen / sizeof(xfs_alloc_rec_t);
467 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));