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"
44 STATIC struct xfs_btree_cur *
45 xfs_allocbt_dup_cursor(
46 struct xfs_btree_cur *cur)
48 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
49 cur->bc_private.a.agbp, cur->bc_private.a.agno,
55 struct xfs_btree_cur *cur,
56 union xfs_btree_ptr *ptr,
59 struct xfs_buf *agbp = cur->bc_private.a.agbp;
60 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
61 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
62 int btnum = cur->bc_btnum;
66 agf->agf_roots[btnum] = ptr->s;
67 be32_add_cpu(&agf->agf_levels[btnum], inc);
68 cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
70 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
74 xfs_allocbt_alloc_block(
75 struct xfs_btree_cur *cur,
76 union xfs_btree_ptr *start,
77 union xfs_btree_ptr *new,
84 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
86 /* Allocate the new block from the freelist. If we can't, give up. */
87 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
90 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
94 if (bno == NULLAGBLOCK) {
95 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
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_mark_busy(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);
155 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
160 * If this is the last leaf block and it's the last record,
161 * then update the size of the longest extent in the AG.
163 if (ptr != xfs_btree_get_numrecs(block))
165 len = rec->alloc.ar_blockcount;
168 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
169 be32_to_cpu(agf->agf_longest))
171 len = rec->alloc.ar_blockcount;
174 numrecs = xfs_btree_get_numrecs(block);
177 ASSERT(ptr == numrecs + 1);
180 xfs_alloc_rec_t *rrp;
182 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
183 len = rrp->ar_blockcount;
194 agf->agf_longest = len;
195 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
196 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
200 xfs_allocbt_get_minrecs(
201 struct xfs_btree_cur *cur,
204 return cur->bc_mp->m_alloc_mnr[level != 0];
208 xfs_allocbt_get_maxrecs(
209 struct xfs_btree_cur *cur,
212 return cur->bc_mp->m_alloc_mxr[level != 0];
216 xfs_allocbt_init_key_from_rec(
217 union xfs_btree_key *key,
218 union xfs_btree_rec *rec)
220 ASSERT(rec->alloc.ar_startblock != 0);
222 key->alloc.ar_startblock = rec->alloc.ar_startblock;
223 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
227 xfs_allocbt_init_rec_from_key(
228 union xfs_btree_key *key,
229 union xfs_btree_rec *rec)
231 ASSERT(key->alloc.ar_startblock != 0);
233 rec->alloc.ar_startblock = key->alloc.ar_startblock;
234 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
238 xfs_allocbt_init_rec_from_cur(
239 struct xfs_btree_cur *cur,
240 union xfs_btree_rec *rec)
242 ASSERT(cur->bc_rec.a.ar_startblock != 0);
244 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
245 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
249 xfs_allocbt_init_ptr_from_cur(
250 struct xfs_btree_cur *cur,
251 union xfs_btree_ptr *ptr)
253 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
255 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
256 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
258 ptr->s = agf->agf_roots[cur->bc_btnum];
262 xfs_allocbt_key_diff(
263 struct xfs_btree_cur *cur,
264 union xfs_btree_key *key)
266 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
267 xfs_alloc_key_t *kp = &key->alloc;
270 if (cur->bc_btnum == XFS_BTNUM_BNO) {
271 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
275 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
279 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
283 xfs_allocbt_kill_root(
284 struct xfs_btree_cur *cur,
287 union xfs_btree_ptr *newroot)
291 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
292 XFS_BTREE_STATS_INC(cur, killroot);
295 * Update the root pointer, decreasing the level by 1 and then
298 xfs_allocbt_set_root(cur, newroot, -1);
299 error = xfs_allocbt_free_block(cur, bp);
301 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
305 XFS_BTREE_STATS_INC(cur, free);
307 xfs_btree_setbuf(cur, level, NULL);
310 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
314 #ifdef XFS_BTREE_TRACE
315 ktrace_t *xfs_allocbt_trace_buf;
318 xfs_allocbt_trace_enter(
319 struct xfs_btree_cur *cur,
336 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
337 (void *)func, (void *)s, NULL, (void *)cur,
338 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
339 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
340 (void *)a8, (void *)a9, (void *)a10);
344 xfs_allocbt_trace_cursor(
345 struct xfs_btree_cur *cur,
350 *s0 = cur->bc_private.a.agno;
351 *l0 = cur->bc_rec.a.ar_startblock;
352 *l1 = cur->bc_rec.a.ar_blockcount;
356 xfs_allocbt_trace_key(
357 struct xfs_btree_cur *cur,
358 union xfs_btree_key *key,
362 *l0 = be32_to_cpu(key->alloc.ar_startblock);
363 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
367 xfs_allocbt_trace_record(
368 struct xfs_btree_cur *cur,
369 union xfs_btree_rec *rec,
374 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
375 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
378 #endif /* XFS_BTREE_TRACE */
380 static const struct xfs_btree_ops xfs_allocbt_ops = {
381 .rec_len = sizeof(xfs_alloc_rec_t),
382 .key_len = sizeof(xfs_alloc_key_t),
384 .dup_cursor = xfs_allocbt_dup_cursor,
385 .set_root = xfs_allocbt_set_root,
386 .kill_root = xfs_allocbt_kill_root,
387 .alloc_block = xfs_allocbt_alloc_block,
388 .free_block = xfs_allocbt_free_block,
389 .update_lastrec = xfs_allocbt_update_lastrec,
390 .get_minrecs = xfs_allocbt_get_minrecs,
391 .get_maxrecs = xfs_allocbt_get_maxrecs,
392 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
393 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
394 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
395 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
396 .key_diff = xfs_allocbt_key_diff,
398 #ifdef XFS_BTREE_TRACE
399 .trace_enter = xfs_allocbt_trace_enter,
400 .trace_cursor = xfs_allocbt_trace_cursor,
401 .trace_key = xfs_allocbt_trace_key,
402 .trace_record = xfs_allocbt_trace_record,
407 * Allocate a new allocation btree cursor.
409 struct xfs_btree_cur * /* new alloc btree cursor */
410 xfs_allocbt_init_cursor(
411 struct xfs_mount *mp, /* file system mount point */
412 struct xfs_trans *tp, /* transaction pointer */
413 struct xfs_buf *agbp, /* buffer for agf structure */
414 xfs_agnumber_t agno, /* allocation group number */
415 xfs_btnum_t btnum) /* btree identifier */
417 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
418 struct xfs_btree_cur *cur;
420 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
422 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
426 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
427 cur->bc_btnum = btnum;
428 cur->bc_blocklog = mp->m_sb.sb_blocklog;
430 cur->bc_ops = &xfs_allocbt_ops;
431 if (btnum == XFS_BTNUM_CNT)
432 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
434 cur->bc_private.a.agbp = agbp;
435 cur->bc_private.a.agno = agno;