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"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_alloc.h"
42 #include "xfs_error.h"
45 * Prototypes for internal functions.
48 STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
49 STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
50 STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
51 STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
52 STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
53 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
54 STATIC int xfs_alloc_rshift(xfs_btree_cur_t *, int, int *);
55 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
56 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
57 STATIC int xfs_alloc_updkey(xfs_btree_cur_t *, xfs_alloc_key_t *, int);
64 * Single level of the xfs_alloc_delete record deletion routine.
65 * Delete record pointed to by cur/level.
66 * Remove the record from its block then rebalance the tree.
67 * Return 0 for error, 1 for done, 2 to go on to the next level.
69 STATIC int /* error */
71 xfs_btree_cur_t *cur, /* btree cursor */
72 int level, /* level removing record from */
73 int *stat) /* fail/done/go-on */
75 xfs_agf_t *agf; /* allocation group freelist header */
76 xfs_alloc_block_t *block; /* btree block record/key lives in */
77 xfs_agblock_t bno; /* btree block number */
78 xfs_buf_t *bp; /* buffer for block */
79 int error; /* error return value */
80 int i; /* loop index */
81 xfs_alloc_key_t key; /* kp points here if block is level 0 */
82 xfs_agblock_t lbno; /* left block's block number */
83 xfs_buf_t *lbp; /* left block's buffer pointer */
84 xfs_alloc_block_t *left; /* left btree block */
85 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
86 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
87 int lrecs=0; /* number of records in left block */
88 xfs_alloc_rec_t *lrp; /* left block record pointer */
89 xfs_mount_t *mp; /* mount structure */
90 int ptr; /* index in btree block for this rec */
91 xfs_agblock_t rbno; /* right block's block number */
92 xfs_buf_t *rbp; /* right block's buffer pointer */
93 xfs_alloc_block_t *right; /* right btree block */
94 xfs_alloc_key_t *rkp; /* right block key pointer */
95 xfs_alloc_ptr_t *rpp; /* right block address pointer */
96 int rrecs=0; /* number of records in right block */
97 xfs_alloc_rec_t *rrp; /* right block record pointer */
98 xfs_btree_cur_t *tcur; /* temporary btree cursor */
101 * Get the index of the entry being deleted, check for nothing there.
103 ptr = cur->bc_ptrs[level];
109 * Get the buffer & block containing the record or key/ptr.
111 bp = cur->bc_bufs[level];
112 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
114 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
118 * Fail if we're off the end of the block.
120 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
124 XFS_STATS_INC(xs_abt_delrec);
126 * It's a nonleaf. Excise the key and ptr being deleted, by
127 * sliding the entries past them down one.
128 * Log the changed areas of the block.
131 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
132 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
134 for (i = ptr; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++) {
135 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
139 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
140 memmove(&lkp[ptr - 1], &lkp[ptr],
141 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lkp)); /* INT_: mem copy */
142 memmove(&lpp[ptr - 1], &lpp[ptr],
143 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lpp)); /* INT_: mem copy */
144 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
145 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
149 * It's a leaf. Excise the record being deleted, by sliding the
150 * entries past it down one. Log the changed areas of the block.
153 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
154 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
155 memmove(&lrp[ptr - 1], &lrp[ptr],
156 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lrp));
157 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
160 * If it's the first record in the block, we'll need a key
161 * structure to pass up to the next level (updkey).
164 key.ar_startblock = lrp->ar_startblock; /* INT_: direct copy */
165 key.ar_blockcount = lrp->ar_blockcount; /* INT_: direct copy */
170 * Decrement and log the number of entries in the block.
172 INT_MOD(block->bb_numrecs, ARCH_CONVERT, -1);
173 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
175 * See if the longest free extent in the allocation group was
176 * changed by this operation. True if it's the by-size btree, and
177 * this is the leaf level, and there is no right sibling block,
178 * and this was the last record.
180 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
184 cur->bc_btnum == XFS_BTNUM_CNT &&
185 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
186 ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
187 ASSERT(ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT) + 1);
189 * There are still records in the block. Grab the size
192 if (INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
193 rrp = XFS_ALLOC_REC_ADDR(block, INT_GET(block->bb_numrecs, ARCH_CONVERT), cur);
194 INT_COPY(agf->agf_longest, rrp->ar_blockcount, ARCH_CONVERT);
197 * No free extents left.
200 agf->agf_longest = 0;
201 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest =
202 INT_GET(agf->agf_longest, ARCH_CONVERT);
203 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
207 * Is this the root level? If so, we're almost done.
209 if (level == cur->bc_nlevels - 1) {
211 * If this is the root level,
212 * and there's only one entry left,
213 * and it's NOT the leaf level,
214 * then we can get rid of this level.
216 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == 1 && level > 0) {
218 * lpp is still set to the first pointer in the block.
219 * Make it the new root of the btree.
221 bno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
222 INT_COPY(agf->agf_roots[cur->bc_btnum], *lpp, ARCH_CONVERT);
223 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, -1);
224 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_levels[cur->bc_btnum]--;
226 * Put this buffer/block on the ag's freelist.
228 if ((error = xfs_alloc_put_freelist(cur->bc_tp,
229 cur->bc_private.a.agbp, NULL, bno)))
232 * Since blocks move to the free list without the
233 * coordination used in xfs_bmap_finish, we can't allow
234 * block to be available for reallocation and
235 * non-transaction writing (user data) until we know
236 * that the transaction that moved it to the free list
237 * is permanently on disk. We track the blocks by
238 * declaring these blocks as "busy"; the busy list is
239 * maintained on a per-ag basis and each transaction
240 * records which entries should be removed when the
241 * iclog commits to disk. If a busy block is
242 * allocated, the iclog is pushed up to the LSN
243 * that freed the block.
245 xfs_alloc_mark_busy(cur->bc_tp,
246 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
248 xfs_trans_agbtree_delta(cur->bc_tp, -1);
249 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
250 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
252 * Update the cursor so there's one fewer level.
254 xfs_btree_setbuf(cur, level, NULL);
256 } else if (level > 0 &&
257 (error = xfs_alloc_decrement(cur, level, &i)))
263 * If we deleted the leftmost entry in the block, update the
264 * key values above us in the tree.
266 if (ptr == 1 && (error = xfs_alloc_updkey(cur, lkp, level + 1)))
269 * If the number of records remaining in the block is at least
270 * the minimum, we're done.
272 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
273 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
279 * Otherwise, we have to move some records around to keep the
280 * tree balanced. Look at the left and right sibling blocks to
281 * see if we can re-balance by moving only one record.
283 rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
284 lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT);
286 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
288 * Duplicate the cursor so our btree manipulations here won't
289 * disrupt the next level up.
291 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
294 * If there's a right sibling, see if it's ok to shift an entry
297 if (rbno != NULLAGBLOCK) {
299 * Move the temp cursor to the last entry in the next block.
300 * Actually any entry but the first would suffice.
302 i = xfs_btree_lastrec(tcur, level);
303 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
304 if ((error = xfs_alloc_increment(tcur, level, &i)))
306 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
307 i = xfs_btree_lastrec(tcur, level);
308 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
310 * Grab a pointer to the block.
312 rbp = tcur->bc_bufs[level];
313 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
315 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
319 * Grab the current block number, for future use.
321 bno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
323 * If right block is full enough so that removing one entry
324 * won't make it too empty, and left-shifting an entry out
325 * of right to us works, we're done.
327 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >=
328 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
329 if ((error = xfs_alloc_lshift(tcur, level, &i)))
332 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
333 XFS_ALLOC_BLOCK_MINRECS(level, cur));
334 xfs_btree_del_cursor(tcur,
337 (error = xfs_alloc_decrement(cur, level,
345 * Otherwise, grab the number of records in right for
346 * future reference, and fix up the temp cursor to point
347 * to our block again (last record).
349 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
350 if (lbno != NULLAGBLOCK) {
351 i = xfs_btree_firstrec(tcur, level);
352 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
353 if ((error = xfs_alloc_decrement(tcur, level, &i)))
355 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
359 * If there's a left sibling, see if it's ok to shift an entry
362 if (lbno != NULLAGBLOCK) {
364 * Move the temp cursor to the first entry in the
367 i = xfs_btree_firstrec(tcur, level);
368 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
369 if ((error = xfs_alloc_decrement(tcur, level, &i)))
371 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
372 xfs_btree_firstrec(tcur, level);
374 * Grab a pointer to the block.
376 lbp = tcur->bc_bufs[level];
377 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
379 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
383 * Grab the current block number, for future use.
385 bno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
387 * If left block is full enough so that removing one entry
388 * won't make it too empty, and right-shifting an entry out
389 * of left to us works, we're done.
391 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >=
392 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
393 if ((error = xfs_alloc_rshift(tcur, level, &i)))
396 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
397 XFS_ALLOC_BLOCK_MINRECS(level, cur));
398 xfs_btree_del_cursor(tcur,
407 * Otherwise, grab the number of records in right for
410 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
413 * Delete the temp cursor, we're done with it.
415 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
417 * If here, we need to do a join to keep the tree balanced.
419 ASSERT(bno != NULLAGBLOCK);
421 * See if we can join with the left neighbor block.
423 if (lbno != NULLAGBLOCK &&
424 lrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
426 * Set "right" to be the starting block,
427 * "left" to be the left neighbor.
432 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
433 cur->bc_private.a.agno, lbno, 0, &lbp,
434 XFS_ALLOC_BTREE_REF)))
436 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
437 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
441 * If that won't work, see if we can join with the right neighbor block.
443 else if (rbno != NULLAGBLOCK &&
444 rrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <=
445 XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
447 * Set "left" to be the starting block,
448 * "right" to be the right neighbor.
453 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
454 cur->bc_private.a.agno, rbno, 0, &rbp,
455 XFS_ALLOC_BTREE_REF)))
457 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
458 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
462 * Otherwise, we can't fix the imbalance.
463 * Just return. This is probably a logic error, but it's not fatal.
466 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
472 * We're now going to join "left" and "right" by moving all the stuff
473 * in "right" to "left" and deleting "right".
477 * It's a non-leaf. Move keys and pointers.
479 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
480 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
481 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
482 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
484 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
485 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
489 memcpy(lkp, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lkp)); /* INT_: structure copy */
490 memcpy(lpp, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lpp)); /* INT_: structure copy */
491 xfs_alloc_log_keys(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
492 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
493 xfs_alloc_log_ptrs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
494 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
497 * It's a leaf. Move records.
499 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
500 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
501 memcpy(lrp, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lrp));
502 xfs_alloc_log_recs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
503 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
506 * If we joined with the left neighbor, set the buffer in the
507 * cursor to the left block, and fix up the index.
510 xfs_btree_setbuf(cur, level, lbp);
511 cur->bc_ptrs[level] += INT_GET(left->bb_numrecs, ARCH_CONVERT);
514 * If we joined with the right neighbor and there's a level above
515 * us, increment the cursor at that level.
517 else if (level + 1 < cur->bc_nlevels &&
518 (error = xfs_alloc_increment(cur, level + 1, &i)))
521 * Fix up the number of records in the surviving block.
523 INT_MOD(left->bb_numrecs, ARCH_CONVERT, INT_GET(right->bb_numrecs, ARCH_CONVERT));
525 * Fix up the right block pointer in the surviving block, and log it.
527 left->bb_rightsib = right->bb_rightsib; /* INT_: direct copy */
528 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
530 * If there is a right sibling now, make it point to the
533 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
534 xfs_alloc_block_t *rrblock;
537 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
538 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0,
539 &rrbp, XFS_ALLOC_BTREE_REF)))
541 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
542 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
544 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, lbno);
545 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
548 * Free the deleting block by putting it on the freelist.
550 if ((error = xfs_alloc_put_freelist(cur->bc_tp, cur->bc_private.a.agbp,
554 * Since blocks move to the free list without the coordination
555 * used in xfs_bmap_finish, we can't allow block to be available
556 * for reallocation and non-transaction writing (user data)
557 * until we know that the transaction that moved it to the free
558 * list is permanently on disk. We track the blocks by declaring
559 * these blocks as "busy"; the busy list is maintained on a
560 * per-ag basis and each transaction records which entries
561 * should be removed when the iclog commits to disk. If a
562 * busy block is allocated, the iclog is pushed up to the
563 * LSN that freed the block.
565 xfs_alloc_mark_busy(cur->bc_tp,
566 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
568 xfs_trans_agbtree_delta(cur->bc_tp, -1);
570 * Adjust the current level's cursor so that we're left referring
571 * to the right node, after we're done.
572 * If this leaves the ptr value 0 our caller will fix it up.
575 cur->bc_ptrs[level]--;
577 * Return value means the next level up has something to do.
583 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
588 * Insert one record/level. Return information to the caller
589 * allowing the next level up to proceed if necessary.
591 STATIC int /* error */
593 xfs_btree_cur_t *cur, /* btree cursor */
594 int level, /* level to insert record at */
595 xfs_agblock_t *bnop, /* i/o: block number inserted */
596 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
597 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
598 int *stat) /* output: success/failure */
600 xfs_agf_t *agf; /* allocation group freelist header */
601 xfs_alloc_block_t *block; /* btree block record/key lives in */
602 xfs_buf_t *bp; /* buffer for block */
603 int error; /* error return value */
604 int i; /* loop index */
605 xfs_alloc_key_t key; /* key value being inserted */
606 xfs_alloc_key_t *kp; /* pointer to btree keys */
607 xfs_agblock_t nbno; /* block number of allocated block */
608 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
609 xfs_alloc_key_t nkey; /* new key value, from split */
610 xfs_alloc_rec_t nrec; /* new record value, for caller */
611 int optr; /* old ptr value */
612 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
613 int ptr; /* index in btree block for this rec */
614 xfs_alloc_rec_t *rp; /* pointer to btree records */
616 ASSERT(INT_GET(recp->ar_blockcount, ARCH_CONVERT) > 0);
619 * GCC doesn't understand the (arguably complex) control flow in
620 * this function and complains about uninitialized structure fields
623 memset(&nrec, 0, sizeof(nrec));
626 * If we made it to the root level, allocate a new root block
629 if (level >= cur->bc_nlevels) {
630 XFS_STATS_INC(xs_abt_insrec);
631 if ((error = xfs_alloc_newroot(cur, &i)))
638 * Make a key out of the record data to be inserted, and save it.
640 key.ar_startblock = recp->ar_startblock; /* INT_: direct copy */
641 key.ar_blockcount = recp->ar_blockcount; /* INT_: direct copy */
642 optr = ptr = cur->bc_ptrs[level];
644 * If we're off the left edge, return failure.
650 XFS_STATS_INC(xs_abt_insrec);
652 * Get pointers to the btree buffer and block.
654 bp = cur->bc_bufs[level];
655 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
657 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
660 * Check that the new entry is being inserted in the right place.
662 if (ptr <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
664 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
665 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
667 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
668 xfs_btree_check_key(cur->bc_btnum, &key, kp);
673 ncur = (xfs_btree_cur_t *)0;
675 * If the block is full, we can't insert the new entry until we
676 * make the block un-full.
678 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
680 * First, try shifting an entry to the right neighbor.
682 if ((error = xfs_alloc_rshift(cur, level, &i)))
688 * Next, try shifting an entry to the left neighbor.
691 if ((error = xfs_alloc_lshift(cur, level, &i)))
694 optr = ptr = cur->bc_ptrs[level];
697 * Next, try splitting the current block in
698 * half. If this works we have to re-set our
699 * variables because we could be in a
700 * different block now.
702 if ((error = xfs_alloc_split(cur, level, &nbno,
706 bp = cur->bc_bufs[level];
707 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
710 xfs_btree_check_sblock(cur,
714 ptr = cur->bc_ptrs[level];
715 nrec.ar_startblock = nkey.ar_startblock; /* INT_: direct copy */
716 nrec.ar_blockcount = nkey.ar_blockcount; /* INT_: direct copy */
719 * Otherwise the insert fails.
729 * At this point we know there's room for our new entry in the block
734 * It's a non-leaf entry. Make a hole for the new data
735 * in the key and ptr regions of the block.
737 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
738 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
740 for (i = INT_GET(block->bb_numrecs, ARCH_CONVERT); i >= ptr; i--) {
741 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
745 memmove(&kp[ptr], &kp[ptr - 1],
746 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*kp)); /* INT_: copy */
747 memmove(&pp[ptr], &pp[ptr - 1],
748 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*pp)); /* INT_: copy */
750 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
754 * Now stuff the new data in, bump numrecs and log the new data.
757 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
758 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
759 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
760 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
762 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
763 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
768 * It's a leaf entry. Make a hole for the new record.
770 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
771 memmove(&rp[ptr], &rp[ptr - 1],
772 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*rp));
774 * Now stuff the new record in, bump numrecs
775 * and log the new data.
777 rp[ptr - 1] = *recp; /* INT_: struct copy */
778 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
779 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
781 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
782 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
787 * Log the new number of records in the btree header.
789 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
791 * If we inserted at the start of a block, update the parents' keys.
793 if (optr == 1 && (error = xfs_alloc_updkey(cur, &key, level + 1)))
796 * Look to see if the longest extent in the allocation group
797 * needs to be updated.
800 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
802 cur->bc_btnum == XFS_BTNUM_CNT &&
803 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
804 INT_GET(recp->ar_blockcount, ARCH_CONVERT) > INT_GET(agf->agf_longest, ARCH_CONVERT)) {
806 * If this is a leaf in the by-size btree and there
807 * is no right sibling block and this block is bigger
808 * than the previous longest block, update it.
810 INT_COPY(agf->agf_longest, recp->ar_blockcount, ARCH_CONVERT);
811 cur->bc_mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest
812 = INT_GET(recp->ar_blockcount, ARCH_CONVERT);
813 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
817 * Return the new block number, if any.
818 * If there is one, give back a record value and a cursor too.
821 if (nbno != NULLAGBLOCK) {
822 *recp = nrec; /* INT_: struct copy */
823 *curp = ncur; /* INT_: struct copy */
830 * Log header fields from a btree block.
834 xfs_trans_t *tp, /* transaction pointer */
835 xfs_buf_t *bp, /* buffer containing btree block */
836 int fields) /* mask of fields: XFS_BB_... */
838 int first; /* first byte offset logged */
839 int last; /* last byte offset logged */
840 static const short offsets[] = { /* table of offsets */
841 offsetof(xfs_alloc_block_t, bb_magic),
842 offsetof(xfs_alloc_block_t, bb_level),
843 offsetof(xfs_alloc_block_t, bb_numrecs),
844 offsetof(xfs_alloc_block_t, bb_leftsib),
845 offsetof(xfs_alloc_block_t, bb_rightsib),
846 sizeof(xfs_alloc_block_t)
849 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
850 xfs_trans_log_buf(tp, bp, first, last);
854 * Log keys from a btree block (nonleaf).
858 xfs_btree_cur_t *cur, /* btree cursor */
859 xfs_buf_t *bp, /* buffer containing btree block */
860 int kfirst, /* index of first key to log */
861 int klast) /* index of last key to log */
863 xfs_alloc_block_t *block; /* btree block to log from */
864 int first; /* first byte offset logged */
865 xfs_alloc_key_t *kp; /* key pointer in btree block */
866 int last; /* last byte offset logged */
868 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
869 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
870 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
871 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
872 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
876 * Log block pointer fields from a btree block (nonleaf).
880 xfs_btree_cur_t *cur, /* btree cursor */
881 xfs_buf_t *bp, /* buffer containing btree block */
882 int pfirst, /* index of first pointer to log */
883 int plast) /* index of last pointer to log */
885 xfs_alloc_block_t *block; /* btree block to log from */
886 int first; /* first byte offset logged */
887 int last; /* last byte offset logged */
888 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
890 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
891 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
892 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
893 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
894 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
898 * Log records from a btree block (leaf).
902 xfs_btree_cur_t *cur, /* btree cursor */
903 xfs_buf_t *bp, /* buffer containing btree block */
904 int rfirst, /* index of first record to log */
905 int rlast) /* index of last record to log */
907 xfs_alloc_block_t *block; /* btree block to log from */
908 int first; /* first byte offset logged */
909 int last; /* last byte offset logged */
910 xfs_alloc_rec_t *rp; /* record pointer for btree block */
913 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
914 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
920 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
921 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
922 ASSERT(INT_GET(p->ar_startblock, ARCH_CONVERT) + INT_GET(p->ar_blockcount, ARCH_CONVERT) <=
923 INT_GET(agf->agf_length, ARCH_CONVERT));
926 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
927 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
928 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
932 * Lookup the record. The cursor is made to point to it, based on dir.
933 * Return 0 if can't find any such record, 1 for success.
935 STATIC int /* error */
937 xfs_btree_cur_t *cur, /* btree cursor */
938 xfs_lookup_t dir, /* <=, ==, or >= */
939 int *stat) /* success/failure */
941 xfs_agblock_t agbno; /* a.g. relative btree block number */
942 xfs_agnumber_t agno; /* allocation group number */
943 xfs_alloc_block_t *block=NULL; /* current btree block */
944 int diff; /* difference for the current key */
945 int error; /* error return value */
946 int keyno=0; /* current key number */
947 int level; /* level in the btree */
948 xfs_mount_t *mp; /* file system mount point */
950 XFS_STATS_INC(xs_abt_lookup);
952 * Get the allocation group header, and the root block number.
957 xfs_agf_t *agf; /* a.g. freespace header */
959 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
960 agno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
961 agbno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
964 * Iterate over each level in the btree, starting at the root.
965 * For each level above the leaves, find the key we need, based
966 * on the lookup record, then follow the corresponding block
967 * pointer down to the next level.
969 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
970 xfs_buf_t *bp; /* buffer pointer for btree block */
971 xfs_daddr_t d; /* disk address of btree block */
974 * Get the disk address we're looking for.
976 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
978 * If the old buffer at this level is for a different block,
979 * throw it away, otherwise just use it.
981 bp = cur->bc_bufs[level];
982 if (bp && XFS_BUF_ADDR(bp) != d)
986 * Need to get a new buffer. Read it, then
987 * set it in the cursor, releasing the old one.
989 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, agno,
990 agbno, 0, &bp, XFS_ALLOC_BTREE_REF)))
992 xfs_btree_setbuf(cur, level, bp);
994 * Point to the btree block, now that we have the buffer
996 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
997 if ((error = xfs_btree_check_sblock(cur, block, level,
1001 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1003 * If we already had a key match at a higher level, we know
1004 * we need to use the first entry in this block.
1009 * Otherwise we need to search this block. Do a binary search.
1012 int high; /* high entry number */
1013 xfs_alloc_key_t *kkbase=NULL;/* base of keys in block */
1014 xfs_alloc_rec_t *krbase=NULL;/* base of records in block */
1015 int low; /* low entry number */
1018 * Get a pointer to keys or records.
1021 kkbase = XFS_ALLOC_KEY_ADDR(block, 1, cur);
1023 krbase = XFS_ALLOC_REC_ADDR(block, 1, cur);
1025 * Set low and high entry numbers, 1-based.
1028 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
1030 * If the block is empty, the tree must
1033 ASSERT(level == 0 && cur->bc_nlevels == 1);
1034 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1039 * Binary search the block.
1041 while (low <= high) {
1042 xfs_extlen_t blockcount; /* key value */
1043 xfs_agblock_t startblock; /* key value */
1045 XFS_STATS_INC(xs_abt_compare);
1047 * keyno is average of low and high.
1049 keyno = (low + high) >> 1;
1051 * Get startblock & blockcount.
1054 xfs_alloc_key_t *kkp;
1056 kkp = kkbase + keyno - 1;
1057 startblock = INT_GET(kkp->ar_startblock, ARCH_CONVERT);
1058 blockcount = INT_GET(kkp->ar_blockcount, ARCH_CONVERT);
1060 xfs_alloc_rec_t *krp;
1062 krp = krbase + keyno - 1;
1063 startblock = INT_GET(krp->ar_startblock, ARCH_CONVERT);
1064 blockcount = INT_GET(krp->ar_blockcount, ARCH_CONVERT);
1067 * Compute difference to get next direction.
1069 if (cur->bc_btnum == XFS_BTNUM_BNO)
1070 diff = (int)startblock -
1071 (int)cur->bc_rec.a.ar_startblock;
1072 else if (!(diff = (int)blockcount -
1073 (int)cur->bc_rec.a.ar_blockcount))
1074 diff = (int)startblock -
1075 (int)cur->bc_rec.a.ar_startblock;
1077 * Less than, move right.
1082 * Greater than, move left.
1087 * Equal, we're done.
1094 * If there are more levels, set up for the next level
1095 * by getting the block number and filling in the cursor.
1099 * If we moved left, need the previous key number,
1100 * unless there isn't one.
1102 if (diff > 0 && --keyno < 1)
1104 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1106 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1109 cur->bc_ptrs[level] = keyno;
1113 * Done with the search.
1114 * See if we need to adjust the results.
1116 if (dir != XFS_LOOKUP_LE && diff < 0) {
1119 * If ge search and we went off the end of the block, but it's
1120 * not the last block, we're in the wrong block.
1122 if (dir == XFS_LOOKUP_GE &&
1123 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1124 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1127 cur->bc_ptrs[0] = keyno;
1128 if ((error = xfs_alloc_increment(cur, 0, &i)))
1130 XFS_WANT_CORRUPTED_RETURN(i == 1);
1135 else if (dir == XFS_LOOKUP_LE && diff > 0)
1137 cur->bc_ptrs[0] = keyno;
1139 * Return if we succeeded or not.
1141 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1144 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1149 * Move 1 record left from cur/level if possible.
1150 * Update cur to reflect the new path.
1152 STATIC int /* error */
1154 xfs_btree_cur_t *cur, /* btree cursor */
1155 int level, /* level to shift record on */
1156 int *stat) /* success/failure */
1158 int error; /* error return value */
1160 int i; /* loop index */
1162 xfs_alloc_key_t key; /* key value for leaf level upward */
1163 xfs_buf_t *lbp; /* buffer for left neighbor block */
1164 xfs_alloc_block_t *left; /* left neighbor btree block */
1165 int nrec; /* new number of left block entries */
1166 xfs_buf_t *rbp; /* buffer for right (current) block */
1167 xfs_alloc_block_t *right; /* right (current) btree block */
1168 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
1169 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
1170 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
1173 * Set up variables for this block as "right".
1175 rbp = cur->bc_bufs[level];
1176 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1178 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1182 * If we've got no left sibling then we can't shift an entry left.
1184 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1189 * If the cursor entry is the one that would be moved, don't
1190 * do it... it's too complicated.
1192 if (cur->bc_ptrs[level] <= 1) {
1197 * Set up the left neighbor as "left".
1199 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1200 cur->bc_private.a.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1201 XFS_ALLOC_BTREE_REF)))
1203 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1204 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1207 * If it's full, it can't take another entry.
1209 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1213 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1215 * If non-leaf, copy a key and a ptr to the left block.
1218 xfs_alloc_key_t *lkp; /* key pointer for left block */
1219 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1221 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1222 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1224 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1225 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1226 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1228 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1231 *lpp = *rpp; /* INT_: copy */
1232 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1233 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1236 * If leaf, copy a record to the left block.
1239 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1241 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1242 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1244 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1245 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1248 * Bump and log left's numrecs, decrement and log right's numrecs.
1250 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1251 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1252 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1253 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1255 * Slide the contents of right down one entry.
1259 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1260 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1265 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1266 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1267 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1268 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1270 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1271 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1272 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1273 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1277 * Update the parent key values of right.
1279 if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
1282 * Slide the cursor value left one.
1284 cur->bc_ptrs[level]--;
1290 * Allocate a new root block, fill it in.
1292 STATIC int /* error */
1294 xfs_btree_cur_t *cur, /* btree cursor */
1295 int *stat) /* success/failure */
1297 int error; /* error return value */
1298 xfs_agblock_t lbno; /* left block number */
1299 xfs_buf_t *lbp; /* left btree buffer */
1300 xfs_alloc_block_t *left; /* left btree block */
1301 xfs_mount_t *mp; /* mount structure */
1302 xfs_agblock_t nbno; /* new block number */
1303 xfs_buf_t *nbp; /* new (root) buffer */
1304 xfs_alloc_block_t *new; /* new (root) btree block */
1305 int nptr; /* new value for key index, 1 or 2 */
1306 xfs_agblock_t rbno; /* right block number */
1307 xfs_buf_t *rbp; /* right btree buffer */
1308 xfs_alloc_block_t *right; /* right btree block */
1312 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1314 * Get a buffer from the freelist blocks, for the new root.
1316 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1320 * None available, we fail.
1322 if (nbno == NULLAGBLOCK) {
1326 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1327 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1329 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1331 * Set the root data in the a.g. freespace structure.
1334 xfs_agf_t *agf; /* a.g. freespace header */
1335 xfs_agnumber_t seqno;
1337 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1338 INT_SET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT, nbno);
1339 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, 1);
1340 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
1341 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1342 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1343 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1346 * At the previous root level there are now two blocks: the old
1347 * root, and the new block generated when it was split.
1348 * We don't know which one the cursor is pointing at, so we
1349 * set up variables "left" and "right" for each case.
1351 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1352 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1354 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1357 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1359 * Our block is left, pick up the right block.
1361 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1362 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1363 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1364 cur->bc_private.a.agno, rbno, 0, &rbp,
1365 XFS_ALLOC_BTREE_REF)))
1367 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1368 if ((error = xfs_btree_check_sblock(cur, right,
1369 cur->bc_nlevels - 1, rbp)))
1374 * Our block is right, pick up the left block.
1378 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1379 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1380 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1381 cur->bc_private.a.agno, lbno, 0, &lbp,
1382 XFS_ALLOC_BTREE_REF)))
1384 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1385 if ((error = xfs_btree_check_sblock(cur, left,
1386 cur->bc_nlevels - 1, lbp)))
1391 * Fill in the new block's btree header and log it.
1393 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1394 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1395 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1396 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1397 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1398 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1399 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1401 * Fill in the key data in the new root.
1404 xfs_alloc_key_t *kp; /* btree key pointer */
1406 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1407 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1408 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); /* INT_: structure copy */
1409 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);/* INT_: structure copy */
1411 xfs_alloc_rec_t *rp; /* btree record pointer */
1413 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1414 kp[0].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1415 kp[0].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1416 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1417 kp[1].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1418 kp[1].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1421 xfs_alloc_log_keys(cur, nbp, 1, 2);
1423 * Fill in the pointer data in the new root.
1426 xfs_alloc_ptr_t *pp; /* btree address pointer */
1428 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1429 INT_SET(pp[0], ARCH_CONVERT, lbno);
1430 INT_SET(pp[1], ARCH_CONVERT, rbno);
1432 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1434 * Fix up the cursor.
1436 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1437 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1444 * Move 1 record right from cur/level if possible.
1445 * Update cur to reflect the new path.
1447 STATIC int /* error */
1449 xfs_btree_cur_t *cur, /* btree cursor */
1450 int level, /* level to shift record on */
1451 int *stat) /* success/failure */
1453 int error; /* error return value */
1454 int i; /* loop index */
1455 xfs_alloc_key_t key; /* key value for leaf level upward */
1456 xfs_buf_t *lbp; /* buffer for left (current) block */
1457 xfs_alloc_block_t *left; /* left (current) btree block */
1458 xfs_buf_t *rbp; /* buffer for right neighbor block */
1459 xfs_alloc_block_t *right; /* right neighbor btree block */
1460 xfs_alloc_key_t *rkp; /* key pointer for right block */
1461 xfs_btree_cur_t *tcur; /* temporary cursor */
1464 * Set up variables for this block as "left".
1466 lbp = cur->bc_bufs[level];
1467 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1469 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1473 * If we've got no right sibling then we can't shift an entry right.
1475 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1480 * If the cursor entry is the one that would be moved, don't
1481 * do it... it's too complicated.
1483 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1488 * Set up the right neighbor as "right".
1490 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1491 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1492 XFS_ALLOC_BTREE_REF)))
1494 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1495 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1498 * If it's full, it can't take another entry.
1500 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1505 * Make a hole at the start of the right neighbor block, then
1506 * copy the last left block entry to the hole.
1509 xfs_alloc_key_t *lkp; /* key pointer for left block */
1510 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1511 xfs_alloc_ptr_t *rpp; /* address pointer for right block */
1513 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1514 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1515 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1516 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1518 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1519 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1523 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1524 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1526 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1529 *rkp = *lkp; /* INT_: copy */
1530 *rpp = *lpp; /* INT_: copy */
1531 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1532 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1533 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1535 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1536 xfs_alloc_rec_t *rrp; /* record pointer for right block */
1538 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1539 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1540 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1542 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1543 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1544 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1546 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1549 * Decrement and log left's numrecs, bump and log right's numrecs.
1551 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1552 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1553 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1554 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1556 * Using a temporary cursor, update the parent key values of the
1557 * block on the right.
1559 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1561 i = xfs_btree_lastrec(tcur, level);
1562 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1563 if ((error = xfs_alloc_increment(tcur, level, &i)) ||
1564 (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
1566 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1570 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1575 * Split cur/level block in half.
1576 * Return new block number and its first record (to be inserted into parent).
1578 STATIC int /* error */
1580 xfs_btree_cur_t *cur, /* btree cursor */
1581 int level, /* level to split */
1582 xfs_agblock_t *bnop, /* output: block number allocated */
1583 xfs_alloc_key_t *keyp, /* output: first key of new block */
1584 xfs_btree_cur_t **curp, /* output: new cursor */
1585 int *stat) /* success/failure */
1587 int error; /* error return value */
1588 int i; /* loop index/record number */
1589 xfs_agblock_t lbno; /* left (current) block number */
1590 xfs_buf_t *lbp; /* buffer for left block */
1591 xfs_alloc_block_t *left; /* left (current) btree block */
1592 xfs_agblock_t rbno; /* right (new) block number */
1593 xfs_buf_t *rbp; /* buffer for right block */
1594 xfs_alloc_block_t *right; /* right (new) btree block */
1597 * Allocate the new block from the freelist.
1598 * If we can't do it, we're toast. Give up.
1600 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1603 if (rbno == NULLAGBLOCK) {
1607 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1608 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1611 * Set up the new block as "right".
1613 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1615 * "Left" is the current (according to the cursor) block.
1617 lbp = cur->bc_bufs[level];
1618 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1620 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1624 * Fill in the btree header for the new block.
1626 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1627 right->bb_level = left->bb_level; /* INT_: direct copy */
1628 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1630 * Make sure that if there's an odd number of entries now, that
1631 * each new block will have the same number of entries.
1633 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1634 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1635 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1636 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1638 * For non-leaf blocks, copy keys and addresses over to the new block.
1641 xfs_alloc_key_t *lkp; /* left btree key pointer */
1642 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1643 xfs_alloc_key_t *rkp; /* right btree key pointer */
1644 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1646 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1647 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1648 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1649 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1651 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1652 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1656 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp)); /* INT_: copy */
1657 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp)); /* INT_: copy */
1658 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1659 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1663 * For leaf blocks, copy records over to the new block.
1666 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1667 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1669 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1670 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1671 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1672 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1673 keyp->ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1674 keyp->ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1677 * Find the left block number by looking in the buffer.
1678 * Adjust numrecs, sibling pointers.
1680 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1681 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1682 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1683 INT_SET(left->bb_rightsib, ARCH_CONVERT, rbno);
1684 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1685 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1686 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1688 * If there's a block to the new block's right, make that block
1689 * point back to right instead of to left.
1691 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1692 xfs_alloc_block_t *rrblock; /* rr btree block */
1693 xfs_buf_t *rrbp; /* buffer for rrblock */
1695 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1696 cur->bc_private.a.agno, INT_GET(right->bb_rightsib, ARCH_CONVERT), 0,
1697 &rrbp, XFS_ALLOC_BTREE_REF)))
1699 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1700 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1702 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, rbno);
1703 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1706 * If the cursor is really in the right block, move it there.
1707 * If it's just pointing past the last entry in left, then we'll
1708 * insert there, so don't change anything in that case.
1710 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1711 xfs_btree_setbuf(cur, level, rbp);
1712 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1715 * If there are more levels, we'll need another cursor which refers to
1716 * the right block, no matter where this cursor was.
1718 if (level + 1 < cur->bc_nlevels) {
1719 if ((error = xfs_btree_dup_cursor(cur, curp)))
1721 (*curp)->bc_ptrs[level + 1]++;
1729 * Update keys at all levels from here to the root along the cursor's path.
1731 STATIC int /* error */
1733 xfs_btree_cur_t *cur, /* btree cursor */
1734 xfs_alloc_key_t *keyp, /* new key value to update to */
1735 int level) /* starting level for update */
1737 int ptr; /* index of key in block */
1740 * Go up the tree from this level toward the root.
1741 * At each level, update the key value to the value input.
1742 * Stop when we reach a level where the cursor isn't pointing
1743 * at the first entry in the block.
1745 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1746 xfs_alloc_block_t *block; /* btree block */
1747 xfs_buf_t *bp; /* buffer for block */
1749 int error; /* error return value */
1751 xfs_alloc_key_t *kp; /* ptr to btree block keys */
1753 bp = cur->bc_bufs[level];
1754 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1756 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1759 ptr = cur->bc_ptrs[level];
1760 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
1762 xfs_alloc_log_keys(cur, bp, ptr, ptr);
1768 * Externally visible routines.
1772 * Decrement cursor by one record at the level.
1773 * For nonzero levels the leaf-ward information is untouched.
1776 xfs_alloc_decrement(
1777 xfs_btree_cur_t *cur, /* btree cursor */
1778 int level, /* level in btree, 0 is leaf */
1779 int *stat) /* success/failure */
1781 xfs_alloc_block_t *block; /* btree block */
1782 int error; /* error return value */
1783 int lev; /* btree level */
1785 ASSERT(level < cur->bc_nlevels);
1787 * Read-ahead to the left at this level.
1789 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1791 * Decrement the ptr at this level. If we're still in the block
1794 if (--cur->bc_ptrs[level] > 0) {
1799 * Get a pointer to the btree block.
1801 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[level]);
1803 if ((error = xfs_btree_check_sblock(cur, block, level,
1804 cur->bc_bufs[level])))
1808 * If we just went off the left edge of the tree, return failure.
1810 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1815 * March up the tree decrementing pointers.
1816 * Stop when we don't go off the left edge of a block.
1818 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1819 if (--cur->bc_ptrs[lev] > 0)
1822 * Read-ahead the left block, we're going to read it
1825 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1828 * If we went off the root then we are seriously confused.
1830 ASSERT(lev < cur->bc_nlevels);
1832 * Now walk back down the tree, fixing up the cursor's buffer
1833 * pointers and key numbers.
1835 for (block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1836 xfs_agblock_t agbno; /* block number of btree block */
1837 xfs_buf_t *bp; /* buffer pointer for block */
1839 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1840 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1841 cur->bc_private.a.agno, agbno, 0, &bp,
1842 XFS_ALLOC_BTREE_REF)))
1845 xfs_btree_setbuf(cur, lev, bp);
1846 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1847 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1849 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1856 * Delete the record pointed to by cur.
1857 * The cursor refers to the place where the record was (could be inserted)
1858 * when the operation returns.
1862 xfs_btree_cur_t *cur, /* btree cursor */
1863 int *stat) /* success/failure */
1865 int error; /* error return value */
1866 int i; /* result code */
1867 int level; /* btree level */
1870 * Go up the tree, starting at leaf level.
1871 * If 2 is returned then a join was done; go to the next level.
1872 * Otherwise we are done.
1874 for (level = 0, i = 2; i == 2; level++) {
1875 if ((error = xfs_alloc_delrec(cur, level, &i)))
1879 for (level = 1; level < cur->bc_nlevels; level++) {
1880 if (cur->bc_ptrs[level] == 0) {
1881 if ((error = xfs_alloc_decrement(cur, level, &i)))
1892 * Get the data from the pointed-to record.
1896 xfs_btree_cur_t *cur, /* btree cursor */
1897 xfs_agblock_t *bno, /* output: starting block of extent */
1898 xfs_extlen_t *len, /* output: length of extent */
1899 int *stat) /* output: success/failure */
1901 xfs_alloc_block_t *block; /* btree block */
1903 int error; /* error return value */
1905 int ptr; /* record number */
1907 ptr = cur->bc_ptrs[0];
1908 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1910 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1914 * Off the right end or left end, return failure.
1916 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1921 * Point to the record and extract its data.
1924 xfs_alloc_rec_t *rec; /* record data */
1926 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1927 *bno = INT_GET(rec->ar_startblock, ARCH_CONVERT);
1928 *len = INT_GET(rec->ar_blockcount, ARCH_CONVERT);
1935 * Increment cursor by one record at the level.
1936 * For nonzero levels the leaf-ward information is untouched.
1939 xfs_alloc_increment(
1940 xfs_btree_cur_t *cur, /* btree cursor */
1941 int level, /* level in btree, 0 is leaf */
1942 int *stat) /* success/failure */
1944 xfs_alloc_block_t *block; /* btree block */
1945 xfs_buf_t *bp; /* tree block buffer */
1946 int error; /* error return value */
1947 int lev; /* btree level */
1949 ASSERT(level < cur->bc_nlevels);
1951 * Read-ahead to the right at this level.
1953 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1955 * Get a pointer to the btree block.
1957 bp = cur->bc_bufs[level];
1958 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1960 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1964 * Increment the ptr at this level. If we're still in the block
1967 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1972 * If we just went off the right edge of the tree, return failure.
1974 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1979 * March up the tree incrementing pointers.
1980 * Stop when we don't go off the right edge of a block.
1982 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1983 bp = cur->bc_bufs[lev];
1984 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1986 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1989 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1992 * Read-ahead the right block, we're going to read it
1995 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1998 * If we went off the root then we are seriously confused.
2000 ASSERT(lev < cur->bc_nlevels);
2002 * Now walk back down the tree, fixing up the cursor's buffer
2003 * pointers and key numbers.
2005 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2007 xfs_agblock_t agbno; /* block number of btree block */
2009 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
2010 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
2011 cur->bc_private.a.agno, agbno, 0, &bp,
2012 XFS_ALLOC_BTREE_REF)))
2015 xfs_btree_setbuf(cur, lev, bp);
2016 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2017 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
2019 cur->bc_ptrs[lev] = 1;
2026 * Insert the current record at the point referenced by cur.
2027 * The cursor may be inconsistent on return if splits have been done.
2031 xfs_btree_cur_t *cur, /* btree cursor */
2032 int *stat) /* success/failure */
2034 int error; /* error return value */
2035 int i; /* result value, 0 for failure */
2036 int level; /* current level number in btree */
2037 xfs_agblock_t nbno; /* new block number (split result) */
2038 xfs_btree_cur_t *ncur; /* new cursor (split result) */
2039 xfs_alloc_rec_t nrec; /* record being inserted this level */
2040 xfs_btree_cur_t *pcur; /* previous level's cursor */
2044 INT_SET(nrec.ar_startblock, ARCH_CONVERT, cur->bc_rec.a.ar_startblock);
2045 INT_SET(nrec.ar_blockcount, ARCH_CONVERT, cur->bc_rec.a.ar_blockcount);
2046 ncur = (xfs_btree_cur_t *)0;
2049 * Loop going up the tree, starting at the leaf level.
2050 * Stop when we don't get a split block, that must mean that
2051 * the insert is finished with this level.
2055 * Insert nrec/nbno into this level of the tree.
2056 * Note if we fail, nbno will be null.
2058 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
2061 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2065 * See if the cursor we just used is trash.
2066 * Can't trash the caller's cursor, but otherwise we should
2067 * if ncur is a new cursor or we're about to be done.
2069 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
2070 cur->bc_nlevels = pcur->bc_nlevels;
2071 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2074 * If we got a new cursor, switch to it.
2078 ncur = (xfs_btree_cur_t *)0;
2080 } while (nbno != NULLAGBLOCK);
2086 * Lookup the record equal to [bno, len] in the btree given by cur.
2089 xfs_alloc_lookup_eq(
2090 xfs_btree_cur_t *cur, /* btree cursor */
2091 xfs_agblock_t bno, /* starting block of extent */
2092 xfs_extlen_t len, /* length of extent */
2093 int *stat) /* success/failure */
2095 cur->bc_rec.a.ar_startblock = bno;
2096 cur->bc_rec.a.ar_blockcount = len;
2097 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, stat);
2101 * Lookup the first record greater than or equal to [bno, len]
2102 * in the btree given by cur.
2105 xfs_alloc_lookup_ge(
2106 xfs_btree_cur_t *cur, /* btree cursor */
2107 xfs_agblock_t bno, /* starting block of extent */
2108 xfs_extlen_t len, /* length of extent */
2109 int *stat) /* success/failure */
2111 cur->bc_rec.a.ar_startblock = bno;
2112 cur->bc_rec.a.ar_blockcount = len;
2113 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, stat);
2117 * Lookup the first record less than or equal to [bno, len]
2118 * in the btree given by cur.
2121 xfs_alloc_lookup_le(
2122 xfs_btree_cur_t *cur, /* btree cursor */
2123 xfs_agblock_t bno, /* starting block of extent */
2124 xfs_extlen_t len, /* length of extent */
2125 int *stat) /* success/failure */
2127 cur->bc_rec.a.ar_startblock = bno;
2128 cur->bc_rec.a.ar_blockcount = len;
2129 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, stat);
2133 * Update the record referred to by cur, to the value given by [bno, len].
2134 * This either works (return 0) or gets an EFSCORRUPTED error.
2138 xfs_btree_cur_t *cur, /* btree cursor */
2139 xfs_agblock_t bno, /* starting block of extent */
2140 xfs_extlen_t len) /* length of extent */
2142 xfs_alloc_block_t *block; /* btree block to update */
2143 int error; /* error return value */
2144 int ptr; /* current record number (updating) */
2148 * Pick up the a.g. freelist struct and the current block.
2150 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
2152 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
2156 * Get the address of the rec to be updated.
2158 ptr = cur->bc_ptrs[0];
2160 xfs_alloc_rec_t *rp; /* pointer to updated record */
2162 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
2164 * Fill in the new contents and log them.
2166 INT_SET(rp->ar_startblock, ARCH_CONVERT, bno);
2167 INT_SET(rp->ar_blockcount, ARCH_CONVERT, len);
2168 xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
2171 * If it's the by-size btree and it's the last leaf block and
2172 * it's the last record... then update the size of the longest
2173 * extent in the a.g., which we cache in the a.g. freelist header.
2175 if (cur->bc_btnum == XFS_BTNUM_CNT &&
2176 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
2177 ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
2178 xfs_agf_t *agf; /* a.g. freespace header */
2179 xfs_agnumber_t seqno;
2181 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
2182 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
2183 cur->bc_mp->m_perag[seqno].pagf_longest = len;
2184 INT_SET(agf->agf_longest, ARCH_CONVERT, len);
2185 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
2189 * Updating first record in leaf. Pass new key value up to our parent.
2192 xfs_alloc_key_t key; /* key containing [bno, len] */
2194 INT_SET(key.ar_startblock, ARCH_CONVERT, bno);
2195 INT_SET(key.ar_blockcount, ARCH_CONVERT, len);
2196 if ((error = xfs_alloc_updkey(cur, &key, 1)))