2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * Free space allocation for XFS.
38 #include "xfs_macros.h"
39 #include "xfs_types.h"
42 #include "xfs_trans.h"
46 #include "xfs_dmapi.h"
47 #include "xfs_mount.h"
48 #include "xfs_alloc_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_bmap_btree.h"
51 #include "xfs_btree.h"
52 #include "xfs_ialloc.h"
53 #include "xfs_alloc.h"
54 #include "xfs_error.h"
57 * Prototypes for internal functions.
60 STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
61 STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
62 STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
63 STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
64 STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
65 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
66 STATIC int xfs_alloc_rshift(xfs_btree_cur_t *, int, int *);
67 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
68 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
69 STATIC int xfs_alloc_updkey(xfs_btree_cur_t *, xfs_alloc_key_t *, int);
76 * Single level of the xfs_alloc_delete record deletion routine.
77 * Delete record pointed to by cur/level.
78 * Remove the record from its block then rebalance the tree.
79 * Return 0 for error, 1 for done, 2 to go on to the next level.
81 STATIC int /* error */
83 xfs_btree_cur_t *cur, /* btree cursor */
84 int level, /* level removing record from */
85 int *stat) /* fail/done/go-on */
87 xfs_agf_t *agf; /* allocation group freelist header */
88 xfs_alloc_block_t *block; /* btree block record/key lives in */
89 xfs_agblock_t bno; /* btree block number */
90 xfs_buf_t *bp; /* buffer for block */
91 int error; /* error return value */
92 int i; /* loop index */
93 xfs_alloc_key_t key; /* kp points here if block is level 0 */
94 xfs_agblock_t lbno; /* left block's block number */
95 xfs_buf_t *lbp; /* left block's buffer pointer */
96 xfs_alloc_block_t *left; /* left btree block */
97 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
98 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
99 int lrecs=0; /* number of records in left block */
100 xfs_alloc_rec_t *lrp; /* left block record pointer */
101 xfs_mount_t *mp; /* mount structure */
102 int ptr; /* index in btree block for this rec */
103 xfs_agblock_t rbno; /* right block's block number */
104 xfs_buf_t *rbp; /* right block's buffer pointer */
105 xfs_alloc_block_t *right; /* right btree block */
106 xfs_alloc_key_t *rkp; /* right block key pointer */
107 xfs_alloc_ptr_t *rpp; /* right block address pointer */
108 int rrecs=0; /* number of records in right block */
109 xfs_alloc_rec_t *rrp; /* right block record pointer */
110 xfs_btree_cur_t *tcur; /* temporary btree cursor */
113 * Get the index of the entry being deleted, check for nothing there.
115 ptr = cur->bc_ptrs[level];
121 * Get the buffer & block containing the record or key/ptr.
123 bp = cur->bc_bufs[level];
124 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
126 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
130 * Fail if we're off the end of the block.
132 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
136 XFS_STATS_INC(xs_abt_delrec);
138 * It's a nonleaf. Excise the key and ptr being deleted, by
139 * sliding the entries past them down one.
140 * Log the changed areas of the block.
143 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
144 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
146 for (i = ptr; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++) {
147 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
151 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
152 memmove(&lkp[ptr - 1], &lkp[ptr],
153 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lkp)); /* INT_: mem copy */
154 memmove(&lpp[ptr - 1], &lpp[ptr],
155 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lpp)); /* INT_: mem copy */
156 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
157 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
161 * It's a leaf. Excise the record being deleted, by sliding the
162 * entries past it down one. Log the changed areas of the block.
165 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
166 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
167 memmove(&lrp[ptr - 1], &lrp[ptr],
168 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lrp));
169 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
172 * If it's the first record in the block, we'll need a key
173 * structure to pass up to the next level (updkey).
176 key.ar_startblock = lrp->ar_startblock; /* INT_: direct copy */
177 key.ar_blockcount = lrp->ar_blockcount; /* INT_: direct copy */
182 * Decrement and log the number of entries in the block.
184 INT_MOD(block->bb_numrecs, ARCH_CONVERT, -1);
185 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
187 * See if the longest free extent in the allocation group was
188 * changed by this operation. True if it's the by-size btree, and
189 * this is the leaf level, and there is no right sibling block,
190 * and this was the last record.
192 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
196 cur->bc_btnum == XFS_BTNUM_CNT &&
197 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
198 ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
199 ASSERT(ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT) + 1);
201 * There are still records in the block. Grab the size
204 if (INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
205 rrp = XFS_ALLOC_REC_ADDR(block, INT_GET(block->bb_numrecs, ARCH_CONVERT), cur);
206 INT_COPY(agf->agf_longest, rrp->ar_blockcount, ARCH_CONVERT);
209 * No free extents left.
212 agf->agf_longest = 0;
213 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest =
214 INT_GET(agf->agf_longest, ARCH_CONVERT);
215 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
219 * Is this the root level? If so, we're almost done.
221 if (level == cur->bc_nlevels - 1) {
223 * If this is the root level,
224 * and there's only one entry left,
225 * and it's NOT the leaf level,
226 * then we can get rid of this level.
228 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == 1 && level > 0) {
230 * lpp is still set to the first pointer in the block.
231 * Make it the new root of the btree.
233 bno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
234 INT_COPY(agf->agf_roots[cur->bc_btnum], *lpp, ARCH_CONVERT);
235 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, -1);
236 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_levels[cur->bc_btnum]--;
238 * Put this buffer/block on the ag's freelist.
240 if ((error = xfs_alloc_put_freelist(cur->bc_tp,
241 cur->bc_private.a.agbp, NULL, bno)))
244 * Since blocks move to the free list without the
245 * coordination used in xfs_bmap_finish, we can't allow
246 * block to be available for reallocation and
247 * non-transaction writing (user data) until we know
248 * that the transaction that moved it to the free list
249 * is permanently on disk. We track the blocks by
250 * declaring these blocks as "busy"; the busy list is
251 * maintained on a per-ag basis and each transaction
252 * records which entries should be removed when the
253 * iclog commits to disk. If a busy block is
254 * allocated, the iclog is pushed up to the LSN
255 * that freed the block.
257 xfs_alloc_mark_busy(cur->bc_tp,
258 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
260 xfs_trans_agbtree_delta(cur->bc_tp, -1);
261 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
262 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
264 * Update the cursor so there's one fewer level.
266 xfs_btree_setbuf(cur, level, NULL);
268 } else if (level > 0 &&
269 (error = xfs_alloc_decrement(cur, level, &i)))
275 * If we deleted the leftmost entry in the block, update the
276 * key values above us in the tree.
278 if (ptr == 1 && (error = xfs_alloc_updkey(cur, lkp, level + 1)))
281 * If the number of records remaining in the block is at least
282 * the minimum, we're done.
284 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
285 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
291 * Otherwise, we have to move some records around to keep the
292 * tree balanced. Look at the left and right sibling blocks to
293 * see if we can re-balance by moving only one record.
295 rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
296 lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT);
298 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
300 * Duplicate the cursor so our btree manipulations here won't
301 * disrupt the next level up.
303 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
306 * If there's a right sibling, see if it's ok to shift an entry
309 if (rbno != NULLAGBLOCK) {
311 * Move the temp cursor to the last entry in the next block.
312 * Actually any entry but the first would suffice.
314 i = xfs_btree_lastrec(tcur, level);
315 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
316 if ((error = xfs_alloc_increment(tcur, level, &i)))
318 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
319 i = xfs_btree_lastrec(tcur, level);
320 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
322 * Grab a pointer to the block.
324 rbp = tcur->bc_bufs[level];
325 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
327 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
331 * Grab the current block number, for future use.
333 bno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
335 * If right block is full enough so that removing one entry
336 * won't make it too empty, and left-shifting an entry out
337 * of right to us works, we're done.
339 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >=
340 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
341 if ((error = xfs_alloc_lshift(tcur, level, &i)))
344 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
345 XFS_ALLOC_BLOCK_MINRECS(level, cur));
346 xfs_btree_del_cursor(tcur,
349 (error = xfs_alloc_decrement(cur, level,
357 * Otherwise, grab the number of records in right for
358 * future reference, and fix up the temp cursor to point
359 * to our block again (last record).
361 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
362 if (lbno != NULLAGBLOCK) {
363 i = xfs_btree_firstrec(tcur, level);
364 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
365 if ((error = xfs_alloc_decrement(tcur, level, &i)))
367 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
371 * If there's a left sibling, see if it's ok to shift an entry
374 if (lbno != NULLAGBLOCK) {
376 * Move the temp cursor to the first entry in the
379 i = xfs_btree_firstrec(tcur, level);
380 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
381 if ((error = xfs_alloc_decrement(tcur, level, &i)))
383 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
384 xfs_btree_firstrec(tcur, level);
386 * Grab a pointer to the block.
388 lbp = tcur->bc_bufs[level];
389 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
391 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
395 * Grab the current block number, for future use.
397 bno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
399 * If left block is full enough so that removing one entry
400 * won't make it too empty, and right-shifting an entry out
401 * of left to us works, we're done.
403 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >=
404 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
405 if ((error = xfs_alloc_rshift(tcur, level, &i)))
408 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
409 XFS_ALLOC_BLOCK_MINRECS(level, cur));
410 xfs_btree_del_cursor(tcur,
419 * Otherwise, grab the number of records in right for
422 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
425 * Delete the temp cursor, we're done with it.
427 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
429 * If here, we need to do a join to keep the tree balanced.
431 ASSERT(bno != NULLAGBLOCK);
433 * See if we can join with the left neighbor block.
435 if (lbno != NULLAGBLOCK &&
436 lrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
438 * Set "right" to be the starting block,
439 * "left" to be the left neighbor.
444 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
445 cur->bc_private.a.agno, lbno, 0, &lbp,
446 XFS_ALLOC_BTREE_REF)))
448 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
449 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
453 * If that won't work, see if we can join with the right neighbor block.
455 else if (rbno != NULLAGBLOCK &&
456 rrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <=
457 XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
459 * Set "left" to be the starting block,
460 * "right" to be the right neighbor.
465 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
466 cur->bc_private.a.agno, rbno, 0, &rbp,
467 XFS_ALLOC_BTREE_REF)))
469 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
470 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
474 * Otherwise, we can't fix the imbalance.
475 * Just return. This is probably a logic error, but it's not fatal.
478 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
484 * We're now going to join "left" and "right" by moving all the stuff
485 * in "right" to "left" and deleting "right".
489 * It's a non-leaf. Move keys and pointers.
491 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
492 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
493 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
494 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
496 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
497 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
501 memcpy(lkp, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lkp)); /* INT_: structure copy */
502 memcpy(lpp, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lpp)); /* INT_: structure copy */
503 xfs_alloc_log_keys(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
504 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
505 xfs_alloc_log_ptrs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
506 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
509 * It's a leaf. Move records.
511 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
512 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
513 memcpy(lrp, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lrp));
514 xfs_alloc_log_recs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
515 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
518 * If we joined with the left neighbor, set the buffer in the
519 * cursor to the left block, and fix up the index.
522 xfs_btree_setbuf(cur, level, lbp);
523 cur->bc_ptrs[level] += INT_GET(left->bb_numrecs, ARCH_CONVERT);
526 * If we joined with the right neighbor and there's a level above
527 * us, increment the cursor at that level.
529 else if (level + 1 < cur->bc_nlevels &&
530 (error = xfs_alloc_increment(cur, level + 1, &i)))
533 * Fix up the number of records in the surviving block.
535 INT_MOD(left->bb_numrecs, ARCH_CONVERT, INT_GET(right->bb_numrecs, ARCH_CONVERT));
537 * Fix up the right block pointer in the surviving block, and log it.
539 left->bb_rightsib = right->bb_rightsib; /* INT_: direct copy */
540 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
542 * If there is a right sibling now, make it point to the
545 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
546 xfs_alloc_block_t *rrblock;
549 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
550 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0,
551 &rrbp, XFS_ALLOC_BTREE_REF)))
553 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
554 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
556 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, lbno);
557 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
560 * Free the deleting block by putting it on the freelist.
562 if ((error = xfs_alloc_put_freelist(cur->bc_tp, cur->bc_private.a.agbp,
566 * Since blocks move to the free list without the coordination
567 * used in xfs_bmap_finish, we can't allow block to be available
568 * for reallocation and non-transaction writing (user data)
569 * until we know that the transaction that moved it to the free
570 * list is permanently on disk. We track the blocks by declaring
571 * these blocks as "busy"; the busy list is maintained on a
572 * per-ag basis and each transaction records which entries
573 * should be removed when the iclog commits to disk. If a
574 * busy block is allocated, the iclog is pushed up to the
575 * LSN that freed the block.
577 xfs_alloc_mark_busy(cur->bc_tp,
578 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
580 xfs_trans_agbtree_delta(cur->bc_tp, -1);
582 * Adjust the current level's cursor so that we're left referring
583 * to the right node, after we're done.
584 * If this leaves the ptr value 0 our caller will fix it up.
587 cur->bc_ptrs[level]--;
589 * Return value means the next level up has something to do.
595 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
600 * Insert one record/level. Return information to the caller
601 * allowing the next level up to proceed if necessary.
603 STATIC int /* error */
605 xfs_btree_cur_t *cur, /* btree cursor */
606 int level, /* level to insert record at */
607 xfs_agblock_t *bnop, /* i/o: block number inserted */
608 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
609 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
610 int *stat) /* output: success/failure */
612 xfs_agf_t *agf; /* allocation group freelist header */
613 xfs_alloc_block_t *block; /* btree block record/key lives in */
614 xfs_buf_t *bp; /* buffer for block */
615 int error; /* error return value */
616 int i; /* loop index */
617 xfs_alloc_key_t key; /* key value being inserted */
618 xfs_alloc_key_t *kp; /* pointer to btree keys */
619 xfs_agblock_t nbno; /* block number of allocated block */
620 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
621 xfs_alloc_key_t nkey; /* new key value, from split */
622 xfs_alloc_rec_t nrec; /* new record value, for caller */
623 int optr; /* old ptr value */
624 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
625 int ptr; /* index in btree block for this rec */
626 xfs_alloc_rec_t *rp; /* pointer to btree records */
628 ASSERT(INT_GET(recp->ar_blockcount, ARCH_CONVERT) > 0);
630 * If we made it to the root level, allocate a new root block
633 if (level >= cur->bc_nlevels) {
634 XFS_STATS_INC(xs_abt_insrec);
635 if ((error = xfs_alloc_newroot(cur, &i)))
642 * Make a key out of the record data to be inserted, and save it.
644 key.ar_startblock = recp->ar_startblock; /* INT_: direct copy */
645 key.ar_blockcount = recp->ar_blockcount; /* INT_: direct copy */
646 optr = ptr = cur->bc_ptrs[level];
648 * If we're off the left edge, return failure.
654 XFS_STATS_INC(xs_abt_insrec);
656 * Get pointers to the btree buffer and block.
658 bp = cur->bc_bufs[level];
659 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
661 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
664 * Check that the new entry is being inserted in the right place.
666 if (ptr <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
668 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
669 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
671 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
672 xfs_btree_check_key(cur->bc_btnum, &key, kp);
677 ncur = (xfs_btree_cur_t *)0;
679 * If the block is full, we can't insert the new entry until we
680 * make the block un-full.
682 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
684 * First, try shifting an entry to the right neighbor.
686 if ((error = xfs_alloc_rshift(cur, level, &i)))
692 * Next, try shifting an entry to the left neighbor.
695 if ((error = xfs_alloc_lshift(cur, level, &i)))
698 optr = ptr = cur->bc_ptrs[level];
701 * Next, try splitting the current block in
702 * half. If this works we have to re-set our
703 * variables because we could be in a
704 * different block now.
706 if ((error = xfs_alloc_split(cur, level, &nbno,
710 bp = cur->bc_bufs[level];
711 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
714 xfs_btree_check_sblock(cur,
718 ptr = cur->bc_ptrs[level];
719 nrec.ar_startblock = nkey.ar_startblock; /* INT_: direct copy */
720 nrec.ar_blockcount = nkey.ar_blockcount; /* INT_: direct copy */
723 * Otherwise the insert fails.
733 * At this point we know there's room for our new entry in the block
738 * It's a non-leaf entry. Make a hole for the new data
739 * in the key and ptr regions of the block.
741 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
742 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
744 for (i = INT_GET(block->bb_numrecs, ARCH_CONVERT); i >= ptr; i--) {
745 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
749 memmove(&kp[ptr], &kp[ptr - 1],
750 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*kp)); /* INT_: copy */
751 memmove(&pp[ptr], &pp[ptr - 1],
752 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*pp)); /* INT_: copy */
754 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
758 * Now stuff the new data in, bump numrecs and log the new data.
761 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
762 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
763 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
764 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
766 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
767 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
772 * It's a leaf entry. Make a hole for the new record.
774 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
775 memmove(&rp[ptr], &rp[ptr - 1],
776 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*rp));
778 * Now stuff the new record in, bump numrecs
779 * and log the new data.
781 rp[ptr - 1] = *recp; /* INT_: struct copy */
782 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
783 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
785 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
786 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
791 * Log the new number of records in the btree header.
793 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
795 * If we inserted at the start of a block, update the parents' keys.
797 if (optr == 1 && (error = xfs_alloc_updkey(cur, &key, level + 1)))
800 * Look to see if the longest extent in the allocation group
801 * needs to be updated.
804 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
806 cur->bc_btnum == XFS_BTNUM_CNT &&
807 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
808 INT_GET(recp->ar_blockcount, ARCH_CONVERT) > INT_GET(agf->agf_longest, ARCH_CONVERT)) {
810 * If this is a leaf in the by-size btree and there
811 * is no right sibling block and this block is bigger
812 * than the previous longest block, update it.
814 INT_COPY(agf->agf_longest, recp->ar_blockcount, ARCH_CONVERT);
815 cur->bc_mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest
816 = INT_GET(recp->ar_blockcount, ARCH_CONVERT);
817 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
821 * Return the new block number, if any.
822 * If there is one, give back a record value and a cursor too.
825 if (nbno != NULLAGBLOCK) {
826 *recp = nrec; /* INT_: struct copy */
827 *curp = ncur; /* INT_: struct copy */
834 * Log header fields from a btree block.
838 xfs_trans_t *tp, /* transaction pointer */
839 xfs_buf_t *bp, /* buffer containing btree block */
840 int fields) /* mask of fields: XFS_BB_... */
842 int first; /* first byte offset logged */
843 int last; /* last byte offset logged */
844 static const short offsets[] = { /* table of offsets */
845 offsetof(xfs_alloc_block_t, bb_magic),
846 offsetof(xfs_alloc_block_t, bb_level),
847 offsetof(xfs_alloc_block_t, bb_numrecs),
848 offsetof(xfs_alloc_block_t, bb_leftsib),
849 offsetof(xfs_alloc_block_t, bb_rightsib),
850 sizeof(xfs_alloc_block_t)
853 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
854 xfs_trans_log_buf(tp, bp, first, last);
858 * Log keys from a btree block (nonleaf).
862 xfs_btree_cur_t *cur, /* btree cursor */
863 xfs_buf_t *bp, /* buffer containing btree block */
864 int kfirst, /* index of first key to log */
865 int klast) /* index of last key to log */
867 xfs_alloc_block_t *block; /* btree block to log from */
868 int first; /* first byte offset logged */
869 xfs_alloc_key_t *kp; /* key pointer in btree block */
870 int last; /* last byte offset logged */
872 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
873 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
874 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
875 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
876 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
880 * Log block pointer fields from a btree block (nonleaf).
884 xfs_btree_cur_t *cur, /* btree cursor */
885 xfs_buf_t *bp, /* buffer containing btree block */
886 int pfirst, /* index of first pointer to log */
887 int plast) /* index of last pointer to log */
889 xfs_alloc_block_t *block; /* btree block to log from */
890 int first; /* first byte offset logged */
891 int last; /* last byte offset logged */
892 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
894 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
895 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
896 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
897 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
898 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
902 * Log records from a btree block (leaf).
906 xfs_btree_cur_t *cur, /* btree cursor */
907 xfs_buf_t *bp, /* buffer containing btree block */
908 int rfirst, /* index of first record to log */
909 int rlast) /* index of last record to log */
911 xfs_alloc_block_t *block; /* btree block to log from */
912 int first; /* first byte offset logged */
913 int last; /* last byte offset logged */
914 xfs_alloc_rec_t *rp; /* record pointer for btree block */
917 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
918 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
924 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
925 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
926 ASSERT(INT_GET(p->ar_startblock, ARCH_CONVERT) + INT_GET(p->ar_blockcount, ARCH_CONVERT) <=
927 INT_GET(agf->agf_length, ARCH_CONVERT));
930 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
931 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
932 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
936 * Lookup the record. The cursor is made to point to it, based on dir.
937 * Return 0 if can't find any such record, 1 for success.
939 STATIC int /* error */
941 xfs_btree_cur_t *cur, /* btree cursor */
942 xfs_lookup_t dir, /* <=, ==, or >= */
943 int *stat) /* success/failure */
945 xfs_agblock_t agbno; /* a.g. relative btree block number */
946 xfs_agnumber_t agno; /* allocation group number */
947 xfs_alloc_block_t *block=NULL; /* current btree block */
948 int diff; /* difference for the current key */
949 int error; /* error return value */
950 int keyno=0; /* current key number */
951 int level; /* level in the btree */
952 xfs_mount_t *mp; /* file system mount point */
954 XFS_STATS_INC(xs_abt_lookup);
956 * Get the allocation group header, and the root block number.
961 xfs_agf_t *agf; /* a.g. freespace header */
963 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
964 agno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
965 agbno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
968 * Iterate over each level in the btree, starting at the root.
969 * For each level above the leaves, find the key we need, based
970 * on the lookup record, then follow the corresponding block
971 * pointer down to the next level.
973 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
974 xfs_buf_t *bp; /* buffer pointer for btree block */
975 xfs_daddr_t d; /* disk address of btree block */
978 * Get the disk address we're looking for.
980 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
982 * If the old buffer at this level is for a different block,
983 * throw it away, otherwise just use it.
985 bp = cur->bc_bufs[level];
986 if (bp && XFS_BUF_ADDR(bp) != d)
990 * Need to get a new buffer. Read it, then
991 * set it in the cursor, releasing the old one.
993 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, agno,
994 agbno, 0, &bp, XFS_ALLOC_BTREE_REF)))
996 xfs_btree_setbuf(cur, level, bp);
998 * Point to the btree block, now that we have the buffer
1000 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1001 if ((error = xfs_btree_check_sblock(cur, block, level,
1005 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1007 * If we already had a key match at a higher level, we know
1008 * we need to use the first entry in this block.
1013 * Otherwise we need to search this block. Do a binary search.
1016 int high; /* high entry number */
1017 xfs_alloc_key_t *kkbase=NULL;/* base of keys in block */
1018 xfs_alloc_rec_t *krbase=NULL;/* base of records in block */
1019 int low; /* low entry number */
1022 * Get a pointer to keys or records.
1025 kkbase = XFS_ALLOC_KEY_ADDR(block, 1, cur);
1027 krbase = XFS_ALLOC_REC_ADDR(block, 1, cur);
1029 * Set low and high entry numbers, 1-based.
1032 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
1034 * If the block is empty, the tree must
1037 ASSERT(level == 0 && cur->bc_nlevels == 1);
1038 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1043 * Binary search the block.
1045 while (low <= high) {
1046 xfs_extlen_t blockcount; /* key value */
1047 xfs_agblock_t startblock; /* key value */
1049 XFS_STATS_INC(xs_abt_compare);
1051 * keyno is average of low and high.
1053 keyno = (low + high) >> 1;
1055 * Get startblock & blockcount.
1058 xfs_alloc_key_t *kkp;
1060 kkp = kkbase + keyno - 1;
1061 startblock = INT_GET(kkp->ar_startblock, ARCH_CONVERT);
1062 blockcount = INT_GET(kkp->ar_blockcount, ARCH_CONVERT);
1064 xfs_alloc_rec_t *krp;
1066 krp = krbase + keyno - 1;
1067 startblock = INT_GET(krp->ar_startblock, ARCH_CONVERT);
1068 blockcount = INT_GET(krp->ar_blockcount, ARCH_CONVERT);
1071 * Compute difference to get next direction.
1073 if (cur->bc_btnum == XFS_BTNUM_BNO)
1074 diff = (int)startblock -
1075 (int)cur->bc_rec.a.ar_startblock;
1076 else if (!(diff = (int)blockcount -
1077 (int)cur->bc_rec.a.ar_blockcount))
1078 diff = (int)startblock -
1079 (int)cur->bc_rec.a.ar_startblock;
1081 * Less than, move right.
1086 * Greater than, move left.
1091 * Equal, we're done.
1098 * If there are more levels, set up for the next level
1099 * by getting the block number and filling in the cursor.
1103 * If we moved left, need the previous key number,
1104 * unless there isn't one.
1106 if (diff > 0 && --keyno < 1)
1108 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1110 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1113 cur->bc_ptrs[level] = keyno;
1117 * Done with the search.
1118 * See if we need to adjust the results.
1120 if (dir != XFS_LOOKUP_LE && diff < 0) {
1123 * If ge search and we went off the end of the block, but it's
1124 * not the last block, we're in the wrong block.
1126 if (dir == XFS_LOOKUP_GE &&
1127 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1128 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1131 cur->bc_ptrs[0] = keyno;
1132 if ((error = xfs_alloc_increment(cur, 0, &i)))
1134 XFS_WANT_CORRUPTED_RETURN(i == 1);
1139 else if (dir == XFS_LOOKUP_LE && diff > 0)
1141 cur->bc_ptrs[0] = keyno;
1143 * Return if we succeeded or not.
1145 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1148 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1153 * Move 1 record left from cur/level if possible.
1154 * Update cur to reflect the new path.
1156 STATIC int /* error */
1158 xfs_btree_cur_t *cur, /* btree cursor */
1159 int level, /* level to shift record on */
1160 int *stat) /* success/failure */
1162 int error; /* error return value */
1164 int i; /* loop index */
1166 xfs_alloc_key_t key; /* key value for leaf level upward */
1167 xfs_buf_t *lbp; /* buffer for left neighbor block */
1168 xfs_alloc_block_t *left; /* left neighbor btree block */
1169 int nrec; /* new number of left block entries */
1170 xfs_buf_t *rbp; /* buffer for right (current) block */
1171 xfs_alloc_block_t *right; /* right (current) btree block */
1172 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
1173 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
1174 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
1177 * Set up variables for this block as "right".
1179 rbp = cur->bc_bufs[level];
1180 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1182 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1186 * If we've got no left sibling then we can't shift an entry left.
1188 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1193 * If the cursor entry is the one that would be moved, don't
1194 * do it... it's too complicated.
1196 if (cur->bc_ptrs[level] <= 1) {
1201 * Set up the left neighbor as "left".
1203 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1204 cur->bc_private.a.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1205 XFS_ALLOC_BTREE_REF)))
1207 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1208 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1211 * If it's full, it can't take another entry.
1213 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1217 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1219 * If non-leaf, copy a key and a ptr to the left block.
1222 xfs_alloc_key_t *lkp; /* key pointer for left block */
1223 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1225 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1226 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1228 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1229 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1230 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1232 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1235 *lpp = *rpp; /* INT_: copy */
1236 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1237 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1240 * If leaf, copy a record to the left block.
1243 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1245 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1246 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1248 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1249 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1252 * Bump and log left's numrecs, decrement and log right's numrecs.
1254 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1255 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1256 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1257 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1259 * Slide the contents of right down one entry.
1263 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1264 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1269 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1270 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1271 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1272 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1274 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1275 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1276 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1277 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1281 * Update the parent key values of right.
1283 if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
1286 * Slide the cursor value left one.
1288 cur->bc_ptrs[level]--;
1294 * Allocate a new root block, fill it in.
1296 STATIC int /* error */
1298 xfs_btree_cur_t *cur, /* btree cursor */
1299 int *stat) /* success/failure */
1301 int error; /* error return value */
1302 xfs_agblock_t lbno; /* left block number */
1303 xfs_buf_t *lbp; /* left btree buffer */
1304 xfs_alloc_block_t *left; /* left btree block */
1305 xfs_mount_t *mp; /* mount structure */
1306 xfs_agblock_t nbno; /* new block number */
1307 xfs_buf_t *nbp; /* new (root) buffer */
1308 xfs_alloc_block_t *new; /* new (root) btree block */
1309 int nptr; /* new value for key index, 1 or 2 */
1310 xfs_agblock_t rbno; /* right block number */
1311 xfs_buf_t *rbp; /* right btree buffer */
1312 xfs_alloc_block_t *right; /* right btree block */
1316 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1318 * Get a buffer from the freelist blocks, for the new root.
1320 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1324 * None available, we fail.
1326 if (nbno == NULLAGBLOCK) {
1330 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1331 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1333 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1335 * Set the root data in the a.g. freespace structure.
1338 xfs_agf_t *agf; /* a.g. freespace header */
1339 xfs_agnumber_t seqno;
1341 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1342 INT_SET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT, nbno);
1343 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, 1);
1344 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
1345 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1346 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1347 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1350 * At the previous root level there are now two blocks: the old
1351 * root, and the new block generated when it was split.
1352 * We don't know which one the cursor is pointing at, so we
1353 * set up variables "left" and "right" for each case.
1355 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1356 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1358 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1361 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1363 * Our block is left, pick up the right block.
1365 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1366 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1367 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1368 cur->bc_private.a.agno, rbno, 0, &rbp,
1369 XFS_ALLOC_BTREE_REF)))
1371 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1372 if ((error = xfs_btree_check_sblock(cur, right,
1373 cur->bc_nlevels - 1, rbp)))
1378 * Our block is right, pick up the left block.
1382 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1383 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1384 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1385 cur->bc_private.a.agno, lbno, 0, &lbp,
1386 XFS_ALLOC_BTREE_REF)))
1388 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1389 if ((error = xfs_btree_check_sblock(cur, left,
1390 cur->bc_nlevels - 1, lbp)))
1395 * Fill in the new block's btree header and log it.
1397 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1398 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1399 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1400 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1401 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1402 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1403 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1405 * Fill in the key data in the new root.
1408 xfs_alloc_key_t *kp; /* btree key pointer */
1410 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1411 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1412 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); /* INT_: structure copy */
1413 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);/* INT_: structure copy */
1415 xfs_alloc_rec_t *rp; /* btree record pointer */
1417 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1418 kp[0].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1419 kp[0].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1420 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1421 kp[1].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1422 kp[1].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1425 xfs_alloc_log_keys(cur, nbp, 1, 2);
1427 * Fill in the pointer data in the new root.
1430 xfs_alloc_ptr_t *pp; /* btree address pointer */
1432 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1433 INT_SET(pp[0], ARCH_CONVERT, lbno);
1434 INT_SET(pp[1], ARCH_CONVERT, rbno);
1436 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1438 * Fix up the cursor.
1440 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1441 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1448 * Move 1 record right from cur/level if possible.
1449 * Update cur to reflect the new path.
1451 STATIC int /* error */
1453 xfs_btree_cur_t *cur, /* btree cursor */
1454 int level, /* level to shift record on */
1455 int *stat) /* success/failure */
1457 int error; /* error return value */
1458 int i; /* loop index */
1459 xfs_alloc_key_t key; /* key value for leaf level upward */
1460 xfs_buf_t *lbp; /* buffer for left (current) block */
1461 xfs_alloc_block_t *left; /* left (current) btree block */
1462 xfs_buf_t *rbp; /* buffer for right neighbor block */
1463 xfs_alloc_block_t *right; /* right neighbor btree block */
1464 xfs_alloc_key_t *rkp; /* key pointer for right block */
1465 xfs_btree_cur_t *tcur; /* temporary cursor */
1468 * Set up variables for this block as "left".
1470 lbp = cur->bc_bufs[level];
1471 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1473 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1477 * If we've got no right sibling then we can't shift an entry right.
1479 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1484 * If the cursor entry is the one that would be moved, don't
1485 * do it... it's too complicated.
1487 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1492 * Set up the right neighbor as "right".
1494 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1495 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1496 XFS_ALLOC_BTREE_REF)))
1498 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1499 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1502 * If it's full, it can't take another entry.
1504 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1509 * Make a hole at the start of the right neighbor block, then
1510 * copy the last left block entry to the hole.
1513 xfs_alloc_key_t *lkp; /* key pointer for left block */
1514 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1515 xfs_alloc_ptr_t *rpp; /* address pointer for right block */
1517 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1518 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1519 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1520 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1522 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1523 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1527 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1528 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1530 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1533 *rkp = *lkp; /* INT_: copy */
1534 *rpp = *lpp; /* INT_: copy */
1535 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1536 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1537 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1539 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1540 xfs_alloc_rec_t *rrp; /* record pointer for right block */
1542 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1543 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1544 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1546 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1547 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1548 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1550 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1553 * Decrement and log left's numrecs, bump and log right's numrecs.
1555 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1556 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1557 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1558 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1560 * Using a temporary cursor, update the parent key values of the
1561 * block on the right.
1563 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1565 i = xfs_btree_lastrec(tcur, level);
1566 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1567 if ((error = xfs_alloc_increment(tcur, level, &i)) ||
1568 (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
1570 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1574 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1579 * Split cur/level block in half.
1580 * Return new block number and its first record (to be inserted into parent).
1582 STATIC int /* error */
1584 xfs_btree_cur_t *cur, /* btree cursor */
1585 int level, /* level to split */
1586 xfs_agblock_t *bnop, /* output: block number allocated */
1587 xfs_alloc_key_t *keyp, /* output: first key of new block */
1588 xfs_btree_cur_t **curp, /* output: new cursor */
1589 int *stat) /* success/failure */
1591 int error; /* error return value */
1592 int i; /* loop index/record number */
1593 xfs_agblock_t lbno; /* left (current) block number */
1594 xfs_buf_t *lbp; /* buffer for left block */
1595 xfs_alloc_block_t *left; /* left (current) btree block */
1596 xfs_agblock_t rbno; /* right (new) block number */
1597 xfs_buf_t *rbp; /* buffer for right block */
1598 xfs_alloc_block_t *right; /* right (new) btree block */
1601 * Allocate the new block from the freelist.
1602 * If we can't do it, we're toast. Give up.
1604 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1607 if (rbno == NULLAGBLOCK) {
1611 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1612 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1615 * Set up the new block as "right".
1617 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1619 * "Left" is the current (according to the cursor) block.
1621 lbp = cur->bc_bufs[level];
1622 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1624 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1628 * Fill in the btree header for the new block.
1630 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1631 right->bb_level = left->bb_level; /* INT_: direct copy */
1632 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1634 * Make sure that if there's an odd number of entries now, that
1635 * each new block will have the same number of entries.
1637 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1638 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1639 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1640 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1642 * For non-leaf blocks, copy keys and addresses over to the new block.
1645 xfs_alloc_key_t *lkp; /* left btree key pointer */
1646 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1647 xfs_alloc_key_t *rkp; /* right btree key pointer */
1648 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1650 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1651 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1652 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1653 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1655 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1656 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1660 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp)); /* INT_: copy */
1661 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp)); /* INT_: copy */
1662 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1663 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1667 * For leaf blocks, copy records over to the new block.
1670 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1671 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1673 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1674 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1675 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1676 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1677 keyp->ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1678 keyp->ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1681 * Find the left block number by looking in the buffer.
1682 * Adjust numrecs, sibling pointers.
1684 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1685 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1686 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1687 INT_SET(left->bb_rightsib, ARCH_CONVERT, rbno);
1688 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1689 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1690 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1692 * If there's a block to the new block's right, make that block
1693 * point back to right instead of to left.
1695 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1696 xfs_alloc_block_t *rrblock; /* rr btree block */
1697 xfs_buf_t *rrbp; /* buffer for rrblock */
1699 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1700 cur->bc_private.a.agno, INT_GET(right->bb_rightsib, ARCH_CONVERT), 0,
1701 &rrbp, XFS_ALLOC_BTREE_REF)))
1703 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1704 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1706 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, rbno);
1707 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1710 * If the cursor is really in the right block, move it there.
1711 * If it's just pointing past the last entry in left, then we'll
1712 * insert there, so don't change anything in that case.
1714 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1715 xfs_btree_setbuf(cur, level, rbp);
1716 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1719 * If there are more levels, we'll need another cursor which refers to
1720 * the right block, no matter where this cursor was.
1722 if (level + 1 < cur->bc_nlevels) {
1723 if ((error = xfs_btree_dup_cursor(cur, curp)))
1725 (*curp)->bc_ptrs[level + 1]++;
1733 * Update keys at all levels from here to the root along the cursor's path.
1735 STATIC int /* error */
1737 xfs_btree_cur_t *cur, /* btree cursor */
1738 xfs_alloc_key_t *keyp, /* new key value to update to */
1739 int level) /* starting level for update */
1741 int ptr; /* index of key in block */
1744 * Go up the tree from this level toward the root.
1745 * At each level, update the key value to the value input.
1746 * Stop when we reach a level where the cursor isn't pointing
1747 * at the first entry in the block.
1749 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1750 xfs_alloc_block_t *block; /* btree block */
1751 xfs_buf_t *bp; /* buffer for block */
1753 int error; /* error return value */
1755 xfs_alloc_key_t *kp; /* ptr to btree block keys */
1757 bp = cur->bc_bufs[level];
1758 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1760 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1763 ptr = cur->bc_ptrs[level];
1764 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
1766 xfs_alloc_log_keys(cur, bp, ptr, ptr);
1772 * Externally visible routines.
1776 * Decrement cursor by one record at the level.
1777 * For nonzero levels the leaf-ward information is untouched.
1780 xfs_alloc_decrement(
1781 xfs_btree_cur_t *cur, /* btree cursor */
1782 int level, /* level in btree, 0 is leaf */
1783 int *stat) /* success/failure */
1785 xfs_alloc_block_t *block; /* btree block */
1786 int error; /* error return value */
1787 int lev; /* btree level */
1789 ASSERT(level < cur->bc_nlevels);
1791 * Read-ahead to the left at this level.
1793 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1795 * Decrement the ptr at this level. If we're still in the block
1798 if (--cur->bc_ptrs[level] > 0) {
1803 * Get a pointer to the btree block.
1805 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[level]);
1807 if ((error = xfs_btree_check_sblock(cur, block, level,
1808 cur->bc_bufs[level])))
1812 * If we just went off the left edge of the tree, return failure.
1814 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1819 * March up the tree decrementing pointers.
1820 * Stop when we don't go off the left edge of a block.
1822 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1823 if (--cur->bc_ptrs[lev] > 0)
1826 * Read-ahead the left block, we're going to read it
1829 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1832 * If we went off the root then we are seriously confused.
1834 ASSERT(lev < cur->bc_nlevels);
1836 * Now walk back down the tree, fixing up the cursor's buffer
1837 * pointers and key numbers.
1839 for (block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1840 xfs_agblock_t agbno; /* block number of btree block */
1841 xfs_buf_t *bp; /* buffer pointer for block */
1843 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1844 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1845 cur->bc_private.a.agno, agbno, 0, &bp,
1846 XFS_ALLOC_BTREE_REF)))
1849 xfs_btree_setbuf(cur, lev, bp);
1850 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1851 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1853 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1860 * Delete the record pointed to by cur.
1861 * The cursor refers to the place where the record was (could be inserted)
1862 * when the operation returns.
1866 xfs_btree_cur_t *cur, /* btree cursor */
1867 int *stat) /* success/failure */
1869 int error; /* error return value */
1870 int i; /* result code */
1871 int level; /* btree level */
1874 * Go up the tree, starting at leaf level.
1875 * If 2 is returned then a join was done; go to the next level.
1876 * Otherwise we are done.
1878 for (level = 0, i = 2; i == 2; level++) {
1879 if ((error = xfs_alloc_delrec(cur, level, &i)))
1883 for (level = 1; level < cur->bc_nlevels; level++) {
1884 if (cur->bc_ptrs[level] == 0) {
1885 if ((error = xfs_alloc_decrement(cur, level, &i)))
1896 * Get the data from the pointed-to record.
1900 xfs_btree_cur_t *cur, /* btree cursor */
1901 xfs_agblock_t *bno, /* output: starting block of extent */
1902 xfs_extlen_t *len, /* output: length of extent */
1903 int *stat) /* output: success/failure */
1905 xfs_alloc_block_t *block; /* btree block */
1907 int error; /* error return value */
1909 int ptr; /* record number */
1911 ptr = cur->bc_ptrs[0];
1912 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1914 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1918 * Off the right end or left end, return failure.
1920 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1925 * Point to the record and extract its data.
1928 xfs_alloc_rec_t *rec; /* record data */
1930 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1931 *bno = INT_GET(rec->ar_startblock, ARCH_CONVERT);
1932 *len = INT_GET(rec->ar_blockcount, ARCH_CONVERT);
1939 * Increment cursor by one record at the level.
1940 * For nonzero levels the leaf-ward information is untouched.
1943 xfs_alloc_increment(
1944 xfs_btree_cur_t *cur, /* btree cursor */
1945 int level, /* level in btree, 0 is leaf */
1946 int *stat) /* success/failure */
1948 xfs_alloc_block_t *block; /* btree block */
1949 xfs_buf_t *bp; /* tree block buffer */
1950 int error; /* error return value */
1951 int lev; /* btree level */
1953 ASSERT(level < cur->bc_nlevels);
1955 * Read-ahead to the right at this level.
1957 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1959 * Get a pointer to the btree block.
1961 bp = cur->bc_bufs[level];
1962 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1964 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1968 * Increment the ptr at this level. If we're still in the block
1971 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1976 * If we just went off the right edge of the tree, return failure.
1978 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1983 * March up the tree incrementing pointers.
1984 * Stop when we don't go off the right edge of a block.
1986 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1987 bp = cur->bc_bufs[lev];
1988 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1990 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1993 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1996 * Read-ahead the right block, we're going to read it
1999 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2002 * If we went off the root then we are seriously confused.
2004 ASSERT(lev < cur->bc_nlevels);
2006 * Now walk back down the tree, fixing up the cursor's buffer
2007 * pointers and key numbers.
2009 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2011 xfs_agblock_t agbno; /* block number of btree block */
2013 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
2014 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
2015 cur->bc_private.a.agno, agbno, 0, &bp,
2016 XFS_ALLOC_BTREE_REF)))
2019 xfs_btree_setbuf(cur, lev, bp);
2020 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2021 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
2023 cur->bc_ptrs[lev] = 1;
2030 * Insert the current record at the point referenced by cur.
2031 * The cursor may be inconsistent on return if splits have been done.
2035 xfs_btree_cur_t *cur, /* btree cursor */
2036 int *stat) /* success/failure */
2038 int error; /* error return value */
2039 int i; /* result value, 0 for failure */
2040 int level; /* current level number in btree */
2041 xfs_agblock_t nbno; /* new block number (split result) */
2042 xfs_btree_cur_t *ncur; /* new cursor (split result) */
2043 xfs_alloc_rec_t nrec; /* record being inserted this level */
2044 xfs_btree_cur_t *pcur; /* previous level's cursor */
2048 INT_SET(nrec.ar_startblock, ARCH_CONVERT, cur->bc_rec.a.ar_startblock);
2049 INT_SET(nrec.ar_blockcount, ARCH_CONVERT, cur->bc_rec.a.ar_blockcount);
2050 ncur = (xfs_btree_cur_t *)0;
2053 * Loop going up the tree, starting at the leaf level.
2054 * Stop when we don't get a split block, that must mean that
2055 * the insert is finished with this level.
2059 * Insert nrec/nbno into this level of the tree.
2060 * Note if we fail, nbno will be null.
2062 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
2065 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2069 * See if the cursor we just used is trash.
2070 * Can't trash the caller's cursor, but otherwise we should
2071 * if ncur is a new cursor or we're about to be done.
2073 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
2074 cur->bc_nlevels = pcur->bc_nlevels;
2075 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2078 * If we got a new cursor, switch to it.
2082 ncur = (xfs_btree_cur_t *)0;
2084 } while (nbno != NULLAGBLOCK);
2090 * Lookup the record equal to [bno, len] in the btree given by cur.
2093 xfs_alloc_lookup_eq(
2094 xfs_btree_cur_t *cur, /* btree cursor */
2095 xfs_agblock_t bno, /* starting block of extent */
2096 xfs_extlen_t len, /* length of extent */
2097 int *stat) /* success/failure */
2099 cur->bc_rec.a.ar_startblock = bno;
2100 cur->bc_rec.a.ar_blockcount = len;
2101 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, stat);
2105 * Lookup the first record greater than or equal to [bno, len]
2106 * in the btree given by cur.
2109 xfs_alloc_lookup_ge(
2110 xfs_btree_cur_t *cur, /* btree cursor */
2111 xfs_agblock_t bno, /* starting block of extent */
2112 xfs_extlen_t len, /* length of extent */
2113 int *stat) /* success/failure */
2115 cur->bc_rec.a.ar_startblock = bno;
2116 cur->bc_rec.a.ar_blockcount = len;
2117 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, stat);
2121 * Lookup the first record less than or equal to [bno, len]
2122 * in the btree given by cur.
2125 xfs_alloc_lookup_le(
2126 xfs_btree_cur_t *cur, /* btree cursor */
2127 xfs_agblock_t bno, /* starting block of extent */
2128 xfs_extlen_t len, /* length of extent */
2129 int *stat) /* success/failure */
2131 cur->bc_rec.a.ar_startblock = bno;
2132 cur->bc_rec.a.ar_blockcount = len;
2133 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, stat);
2137 * Update the record referred to by cur, to the value given by [bno, len].
2138 * This either works (return 0) or gets an EFSCORRUPTED error.
2142 xfs_btree_cur_t *cur, /* btree cursor */
2143 xfs_agblock_t bno, /* starting block of extent */
2144 xfs_extlen_t len) /* length of extent */
2146 xfs_alloc_block_t *block; /* btree block to update */
2147 int error; /* error return value */
2148 int ptr; /* current record number (updating) */
2152 * Pick up the a.g. freelist struct and the current block.
2154 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
2156 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
2160 * Get the address of the rec to be updated.
2162 ptr = cur->bc_ptrs[0];
2164 xfs_alloc_rec_t *rp; /* pointer to updated record */
2166 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
2168 * Fill in the new contents and log them.
2170 INT_SET(rp->ar_startblock, ARCH_CONVERT, bno);
2171 INT_SET(rp->ar_blockcount, ARCH_CONVERT, len);
2172 xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
2175 * If it's the by-size btree and it's the last leaf block and
2176 * it's the last record... then update the size of the longest
2177 * extent in the a.g., which we cache in the a.g. freelist header.
2179 if (cur->bc_btnum == XFS_BTNUM_CNT &&
2180 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
2181 ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
2182 xfs_agf_t *agf; /* a.g. freespace header */
2183 xfs_agnumber_t seqno;
2185 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
2186 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
2187 cur->bc_mp->m_perag[seqno].pagf_longest = len;
2188 INT_SET(agf->agf_longest, ARCH_CONVERT, len);
2189 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
2193 * Updating first record in leaf. Pass new key value up to our parent.
2196 xfs_alloc_key_t key; /* key containing [bno, len] */
2198 INT_SET(key.ar_startblock, ARCH_CONVERT, bno);
2199 INT_SET(key.ar_blockcount, ARCH_CONVERT, len);
2200 if ((error = xfs_alloc_updkey(cur, &key, 1)))