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);
618 * If we made it to the root level, allocate a new root block
621 if (level >= cur->bc_nlevels) {
622 XFS_STATS_INC(xs_abt_insrec);
623 if ((error = xfs_alloc_newroot(cur, &i)))
630 * Make a key out of the record data to be inserted, and save it.
632 key.ar_startblock = recp->ar_startblock; /* INT_: direct copy */
633 key.ar_blockcount = recp->ar_blockcount; /* INT_: direct copy */
634 optr = ptr = cur->bc_ptrs[level];
636 * If we're off the left edge, return failure.
642 XFS_STATS_INC(xs_abt_insrec);
644 * Get pointers to the btree buffer and block.
646 bp = cur->bc_bufs[level];
647 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
649 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
652 * Check that the new entry is being inserted in the right place.
654 if (ptr <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
656 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
657 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
659 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
660 xfs_btree_check_key(cur->bc_btnum, &key, kp);
665 ncur = (xfs_btree_cur_t *)0;
667 * If the block is full, we can't insert the new entry until we
668 * make the block un-full.
670 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
672 * First, try shifting an entry to the right neighbor.
674 if ((error = xfs_alloc_rshift(cur, level, &i)))
680 * Next, try shifting an entry to the left neighbor.
683 if ((error = xfs_alloc_lshift(cur, level, &i)))
686 optr = ptr = cur->bc_ptrs[level];
689 * Next, try splitting the current block in
690 * half. If this works we have to re-set our
691 * variables because we could be in a
692 * different block now.
694 if ((error = xfs_alloc_split(cur, level, &nbno,
698 bp = cur->bc_bufs[level];
699 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
702 xfs_btree_check_sblock(cur,
706 ptr = cur->bc_ptrs[level];
707 nrec.ar_startblock = nkey.ar_startblock; /* INT_: direct copy */
708 nrec.ar_blockcount = nkey.ar_blockcount; /* INT_: direct copy */
711 * Otherwise the insert fails.
721 * At this point we know there's room for our new entry in the block
726 * It's a non-leaf entry. Make a hole for the new data
727 * in the key and ptr regions of the block.
729 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
730 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
732 for (i = INT_GET(block->bb_numrecs, ARCH_CONVERT); i >= ptr; i--) {
733 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
737 memmove(&kp[ptr], &kp[ptr - 1],
738 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*kp)); /* INT_: copy */
739 memmove(&pp[ptr], &pp[ptr - 1],
740 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*pp)); /* INT_: copy */
742 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
746 * Now stuff the new data in, bump numrecs and log the new data.
749 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
750 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
751 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
752 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
754 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
755 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
760 * It's a leaf entry. Make a hole for the new record.
762 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
763 memmove(&rp[ptr], &rp[ptr - 1],
764 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*rp));
766 * Now stuff the new record in, bump numrecs
767 * and log the new data.
769 rp[ptr - 1] = *recp; /* INT_: struct copy */
770 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
771 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
773 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
774 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
779 * Log the new number of records in the btree header.
781 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
783 * If we inserted at the start of a block, update the parents' keys.
785 if (optr == 1 && (error = xfs_alloc_updkey(cur, &key, level + 1)))
788 * Look to see if the longest extent in the allocation group
789 * needs to be updated.
792 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
794 cur->bc_btnum == XFS_BTNUM_CNT &&
795 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
796 INT_GET(recp->ar_blockcount, ARCH_CONVERT) > INT_GET(agf->agf_longest, ARCH_CONVERT)) {
798 * If this is a leaf in the by-size btree and there
799 * is no right sibling block and this block is bigger
800 * than the previous longest block, update it.
802 INT_COPY(agf->agf_longest, recp->ar_blockcount, ARCH_CONVERT);
803 cur->bc_mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest
804 = INT_GET(recp->ar_blockcount, ARCH_CONVERT);
805 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
809 * Return the new block number, if any.
810 * If there is one, give back a record value and a cursor too.
813 if (nbno != NULLAGBLOCK) {
814 *recp = nrec; /* INT_: struct copy */
815 *curp = ncur; /* INT_: struct copy */
822 * Log header fields from a btree block.
826 xfs_trans_t *tp, /* transaction pointer */
827 xfs_buf_t *bp, /* buffer containing btree block */
828 int fields) /* mask of fields: XFS_BB_... */
830 int first; /* first byte offset logged */
831 int last; /* last byte offset logged */
832 static const short offsets[] = { /* table of offsets */
833 offsetof(xfs_alloc_block_t, bb_magic),
834 offsetof(xfs_alloc_block_t, bb_level),
835 offsetof(xfs_alloc_block_t, bb_numrecs),
836 offsetof(xfs_alloc_block_t, bb_leftsib),
837 offsetof(xfs_alloc_block_t, bb_rightsib),
838 sizeof(xfs_alloc_block_t)
841 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
842 xfs_trans_log_buf(tp, bp, first, last);
846 * Log keys from a btree block (nonleaf).
850 xfs_btree_cur_t *cur, /* btree cursor */
851 xfs_buf_t *bp, /* buffer containing btree block */
852 int kfirst, /* index of first key to log */
853 int klast) /* index of last key to log */
855 xfs_alloc_block_t *block; /* btree block to log from */
856 int first; /* first byte offset logged */
857 xfs_alloc_key_t *kp; /* key pointer in btree block */
858 int last; /* last byte offset logged */
860 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
861 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
862 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
863 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
864 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
868 * Log block pointer fields from a btree block (nonleaf).
872 xfs_btree_cur_t *cur, /* btree cursor */
873 xfs_buf_t *bp, /* buffer containing btree block */
874 int pfirst, /* index of first pointer to log */
875 int plast) /* index of last pointer to log */
877 xfs_alloc_block_t *block; /* btree block to log from */
878 int first; /* first byte offset logged */
879 int last; /* last byte offset logged */
880 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
882 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
883 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
884 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
885 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
886 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
890 * Log records from a btree block (leaf).
894 xfs_btree_cur_t *cur, /* btree cursor */
895 xfs_buf_t *bp, /* buffer containing btree block */
896 int rfirst, /* index of first record to log */
897 int rlast) /* index of last record to log */
899 xfs_alloc_block_t *block; /* btree block to log from */
900 int first; /* first byte offset logged */
901 int last; /* last byte offset logged */
902 xfs_alloc_rec_t *rp; /* record pointer for btree block */
905 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
906 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
912 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
913 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
914 ASSERT(INT_GET(p->ar_startblock, ARCH_CONVERT) + INT_GET(p->ar_blockcount, ARCH_CONVERT) <=
915 INT_GET(agf->agf_length, ARCH_CONVERT));
918 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
919 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
920 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
924 * Lookup the record. The cursor is made to point to it, based on dir.
925 * Return 0 if can't find any such record, 1 for success.
927 STATIC int /* error */
929 xfs_btree_cur_t *cur, /* btree cursor */
930 xfs_lookup_t dir, /* <=, ==, or >= */
931 int *stat) /* success/failure */
933 xfs_agblock_t agbno; /* a.g. relative btree block number */
934 xfs_agnumber_t agno; /* allocation group number */
935 xfs_alloc_block_t *block=NULL; /* current btree block */
936 int diff; /* difference for the current key */
937 int error; /* error return value */
938 int keyno=0; /* current key number */
939 int level; /* level in the btree */
940 xfs_mount_t *mp; /* file system mount point */
942 XFS_STATS_INC(xs_abt_lookup);
944 * Get the allocation group header, and the root block number.
949 xfs_agf_t *agf; /* a.g. freespace header */
951 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
952 agno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
953 agbno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
956 * Iterate over each level in the btree, starting at the root.
957 * For each level above the leaves, find the key we need, based
958 * on the lookup record, then follow the corresponding block
959 * pointer down to the next level.
961 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
962 xfs_buf_t *bp; /* buffer pointer for btree block */
963 xfs_daddr_t d; /* disk address of btree block */
966 * Get the disk address we're looking for.
968 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
970 * If the old buffer at this level is for a different block,
971 * throw it away, otherwise just use it.
973 bp = cur->bc_bufs[level];
974 if (bp && XFS_BUF_ADDR(bp) != d)
978 * Need to get a new buffer. Read it, then
979 * set it in the cursor, releasing the old one.
981 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, agno,
982 agbno, 0, &bp, XFS_ALLOC_BTREE_REF)))
984 xfs_btree_setbuf(cur, level, bp);
986 * Point to the btree block, now that we have the buffer
988 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
989 if ((error = xfs_btree_check_sblock(cur, block, level,
993 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
995 * If we already had a key match at a higher level, we know
996 * we need to use the first entry in this block.
1001 * Otherwise we need to search this block. Do a binary search.
1004 int high; /* high entry number */
1005 xfs_alloc_key_t *kkbase=NULL;/* base of keys in block */
1006 xfs_alloc_rec_t *krbase=NULL;/* base of records in block */
1007 int low; /* low entry number */
1010 * Get a pointer to keys or records.
1013 kkbase = XFS_ALLOC_KEY_ADDR(block, 1, cur);
1015 krbase = XFS_ALLOC_REC_ADDR(block, 1, cur);
1017 * Set low and high entry numbers, 1-based.
1020 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
1022 * If the block is empty, the tree must
1025 ASSERT(level == 0 && cur->bc_nlevels == 1);
1026 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1031 * Binary search the block.
1033 while (low <= high) {
1034 xfs_extlen_t blockcount; /* key value */
1035 xfs_agblock_t startblock; /* key value */
1037 XFS_STATS_INC(xs_abt_compare);
1039 * keyno is average of low and high.
1041 keyno = (low + high) >> 1;
1043 * Get startblock & blockcount.
1046 xfs_alloc_key_t *kkp;
1048 kkp = kkbase + keyno - 1;
1049 startblock = INT_GET(kkp->ar_startblock, ARCH_CONVERT);
1050 blockcount = INT_GET(kkp->ar_blockcount, ARCH_CONVERT);
1052 xfs_alloc_rec_t *krp;
1054 krp = krbase + keyno - 1;
1055 startblock = INT_GET(krp->ar_startblock, ARCH_CONVERT);
1056 blockcount = INT_GET(krp->ar_blockcount, ARCH_CONVERT);
1059 * Compute difference to get next direction.
1061 if (cur->bc_btnum == XFS_BTNUM_BNO)
1062 diff = (int)startblock -
1063 (int)cur->bc_rec.a.ar_startblock;
1064 else if (!(diff = (int)blockcount -
1065 (int)cur->bc_rec.a.ar_blockcount))
1066 diff = (int)startblock -
1067 (int)cur->bc_rec.a.ar_startblock;
1069 * Less than, move right.
1074 * Greater than, move left.
1079 * Equal, we're done.
1086 * If there are more levels, set up for the next level
1087 * by getting the block number and filling in the cursor.
1091 * If we moved left, need the previous key number,
1092 * unless there isn't one.
1094 if (diff > 0 && --keyno < 1)
1096 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1098 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1101 cur->bc_ptrs[level] = keyno;
1105 * Done with the search.
1106 * See if we need to adjust the results.
1108 if (dir != XFS_LOOKUP_LE && diff < 0) {
1111 * If ge search and we went off the end of the block, but it's
1112 * not the last block, we're in the wrong block.
1114 if (dir == XFS_LOOKUP_GE &&
1115 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1116 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1119 cur->bc_ptrs[0] = keyno;
1120 if ((error = xfs_alloc_increment(cur, 0, &i)))
1122 XFS_WANT_CORRUPTED_RETURN(i == 1);
1127 else if (dir == XFS_LOOKUP_LE && diff > 0)
1129 cur->bc_ptrs[0] = keyno;
1131 * Return if we succeeded or not.
1133 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1136 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1141 * Move 1 record left from cur/level if possible.
1142 * Update cur to reflect the new path.
1144 STATIC int /* error */
1146 xfs_btree_cur_t *cur, /* btree cursor */
1147 int level, /* level to shift record on */
1148 int *stat) /* success/failure */
1150 int error; /* error return value */
1152 int i; /* loop index */
1154 xfs_alloc_key_t key; /* key value for leaf level upward */
1155 xfs_buf_t *lbp; /* buffer for left neighbor block */
1156 xfs_alloc_block_t *left; /* left neighbor btree block */
1157 int nrec; /* new number of left block entries */
1158 xfs_buf_t *rbp; /* buffer for right (current) block */
1159 xfs_alloc_block_t *right; /* right (current) btree block */
1160 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
1161 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
1162 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
1165 * Set up variables for this block as "right".
1167 rbp = cur->bc_bufs[level];
1168 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1170 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1174 * If we've got no left sibling then we can't shift an entry left.
1176 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1181 * If the cursor entry is the one that would be moved, don't
1182 * do it... it's too complicated.
1184 if (cur->bc_ptrs[level] <= 1) {
1189 * Set up the left neighbor as "left".
1191 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1192 cur->bc_private.a.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1193 XFS_ALLOC_BTREE_REF)))
1195 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1196 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1199 * If it's full, it can't take another entry.
1201 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1205 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1207 * If non-leaf, copy a key and a ptr to the left block.
1210 xfs_alloc_key_t *lkp; /* key pointer for left block */
1211 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1213 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1214 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1216 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1217 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1218 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1220 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1223 *lpp = *rpp; /* INT_: copy */
1224 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1225 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1228 * If leaf, copy a record to the left block.
1231 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1233 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1234 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1236 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1237 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1240 * Bump and log left's numrecs, decrement and log right's numrecs.
1242 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1243 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1244 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1245 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1247 * Slide the contents of right down one entry.
1251 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1252 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1257 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1258 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1259 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1260 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1262 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1263 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1264 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1265 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1269 * Update the parent key values of right.
1271 if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
1274 * Slide the cursor value left one.
1276 cur->bc_ptrs[level]--;
1282 * Allocate a new root block, fill it in.
1284 STATIC int /* error */
1286 xfs_btree_cur_t *cur, /* btree cursor */
1287 int *stat) /* success/failure */
1289 int error; /* error return value */
1290 xfs_agblock_t lbno; /* left block number */
1291 xfs_buf_t *lbp; /* left btree buffer */
1292 xfs_alloc_block_t *left; /* left btree block */
1293 xfs_mount_t *mp; /* mount structure */
1294 xfs_agblock_t nbno; /* new block number */
1295 xfs_buf_t *nbp; /* new (root) buffer */
1296 xfs_alloc_block_t *new; /* new (root) btree block */
1297 int nptr; /* new value for key index, 1 or 2 */
1298 xfs_agblock_t rbno; /* right block number */
1299 xfs_buf_t *rbp; /* right btree buffer */
1300 xfs_alloc_block_t *right; /* right btree block */
1304 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1306 * Get a buffer from the freelist blocks, for the new root.
1308 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1312 * None available, we fail.
1314 if (nbno == NULLAGBLOCK) {
1318 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1319 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1321 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1323 * Set the root data in the a.g. freespace structure.
1326 xfs_agf_t *agf; /* a.g. freespace header */
1327 xfs_agnumber_t seqno;
1329 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1330 INT_SET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT, nbno);
1331 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, 1);
1332 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
1333 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1334 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1335 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1338 * At the previous root level there are now two blocks: the old
1339 * root, and the new block generated when it was split.
1340 * We don't know which one the cursor is pointing at, so we
1341 * set up variables "left" and "right" for each case.
1343 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1344 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1346 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1349 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1351 * Our block is left, pick up the right block.
1353 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1354 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1355 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1356 cur->bc_private.a.agno, rbno, 0, &rbp,
1357 XFS_ALLOC_BTREE_REF)))
1359 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1360 if ((error = xfs_btree_check_sblock(cur, right,
1361 cur->bc_nlevels - 1, rbp)))
1366 * Our block is right, pick up the left block.
1370 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1371 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1372 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1373 cur->bc_private.a.agno, lbno, 0, &lbp,
1374 XFS_ALLOC_BTREE_REF)))
1376 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1377 if ((error = xfs_btree_check_sblock(cur, left,
1378 cur->bc_nlevels - 1, lbp)))
1383 * Fill in the new block's btree header and log it.
1385 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1386 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1387 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1388 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1389 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1390 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1391 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1393 * Fill in the key data in the new root.
1396 xfs_alloc_key_t *kp; /* btree key pointer */
1398 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1399 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1400 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); /* INT_: structure copy */
1401 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);/* INT_: structure copy */
1403 xfs_alloc_rec_t *rp; /* btree record pointer */
1405 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1406 kp[0].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1407 kp[0].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1408 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1409 kp[1].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1410 kp[1].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1413 xfs_alloc_log_keys(cur, nbp, 1, 2);
1415 * Fill in the pointer data in the new root.
1418 xfs_alloc_ptr_t *pp; /* btree address pointer */
1420 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1421 INT_SET(pp[0], ARCH_CONVERT, lbno);
1422 INT_SET(pp[1], ARCH_CONVERT, rbno);
1424 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1426 * Fix up the cursor.
1428 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1429 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1436 * Move 1 record right from cur/level if possible.
1437 * Update cur to reflect the new path.
1439 STATIC int /* error */
1441 xfs_btree_cur_t *cur, /* btree cursor */
1442 int level, /* level to shift record on */
1443 int *stat) /* success/failure */
1445 int error; /* error return value */
1446 int i; /* loop index */
1447 xfs_alloc_key_t key; /* key value for leaf level upward */
1448 xfs_buf_t *lbp; /* buffer for left (current) block */
1449 xfs_alloc_block_t *left; /* left (current) btree block */
1450 xfs_buf_t *rbp; /* buffer for right neighbor block */
1451 xfs_alloc_block_t *right; /* right neighbor btree block */
1452 xfs_alloc_key_t *rkp; /* key pointer for right block */
1453 xfs_btree_cur_t *tcur; /* temporary cursor */
1456 * Set up variables for this block as "left".
1458 lbp = cur->bc_bufs[level];
1459 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1461 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1465 * If we've got no right sibling then we can't shift an entry right.
1467 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1472 * If the cursor entry is the one that would be moved, don't
1473 * do it... it's too complicated.
1475 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1480 * Set up the right neighbor as "right".
1482 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1483 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1484 XFS_ALLOC_BTREE_REF)))
1486 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1487 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1490 * If it's full, it can't take another entry.
1492 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1497 * Make a hole at the start of the right neighbor block, then
1498 * copy the last left block entry to the hole.
1501 xfs_alloc_key_t *lkp; /* key pointer for left block */
1502 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1503 xfs_alloc_ptr_t *rpp; /* address pointer for right block */
1505 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1506 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1507 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1508 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1510 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1511 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1515 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1516 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1518 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1521 *rkp = *lkp; /* INT_: copy */
1522 *rpp = *lpp; /* INT_: copy */
1523 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1524 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1525 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1527 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1528 xfs_alloc_rec_t *rrp; /* record pointer for right block */
1530 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1531 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1532 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1534 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1535 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1536 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1538 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1541 * Decrement and log left's numrecs, bump and log right's numrecs.
1543 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1544 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1545 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1546 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1548 * Using a temporary cursor, update the parent key values of the
1549 * block on the right.
1551 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1553 i = xfs_btree_lastrec(tcur, level);
1554 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1555 if ((error = xfs_alloc_increment(tcur, level, &i)) ||
1556 (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
1558 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1562 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1567 * Split cur/level block in half.
1568 * Return new block number and its first record (to be inserted into parent).
1570 STATIC int /* error */
1572 xfs_btree_cur_t *cur, /* btree cursor */
1573 int level, /* level to split */
1574 xfs_agblock_t *bnop, /* output: block number allocated */
1575 xfs_alloc_key_t *keyp, /* output: first key of new block */
1576 xfs_btree_cur_t **curp, /* output: new cursor */
1577 int *stat) /* success/failure */
1579 int error; /* error return value */
1580 int i; /* loop index/record number */
1581 xfs_agblock_t lbno; /* left (current) block number */
1582 xfs_buf_t *lbp; /* buffer for left block */
1583 xfs_alloc_block_t *left; /* left (current) btree block */
1584 xfs_agblock_t rbno; /* right (new) block number */
1585 xfs_buf_t *rbp; /* buffer for right block */
1586 xfs_alloc_block_t *right; /* right (new) btree block */
1589 * Allocate the new block from the freelist.
1590 * If we can't do it, we're toast. Give up.
1592 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1595 if (rbno == NULLAGBLOCK) {
1599 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1600 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1603 * Set up the new block as "right".
1605 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1607 * "Left" is the current (according to the cursor) block.
1609 lbp = cur->bc_bufs[level];
1610 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1612 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1616 * Fill in the btree header for the new block.
1618 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1619 right->bb_level = left->bb_level; /* INT_: direct copy */
1620 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1622 * Make sure that if there's an odd number of entries now, that
1623 * each new block will have the same number of entries.
1625 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1626 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1627 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1628 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1630 * For non-leaf blocks, copy keys and addresses over to the new block.
1633 xfs_alloc_key_t *lkp; /* left btree key pointer */
1634 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1635 xfs_alloc_key_t *rkp; /* right btree key pointer */
1636 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1638 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1639 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1640 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1641 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1643 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1644 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1648 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp)); /* INT_: copy */
1649 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp)); /* INT_: copy */
1650 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1651 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1655 * For leaf blocks, copy records over to the new block.
1658 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1659 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1661 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1662 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1663 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1664 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1665 keyp->ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1666 keyp->ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1669 * Find the left block number by looking in the buffer.
1670 * Adjust numrecs, sibling pointers.
1672 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1673 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1674 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1675 INT_SET(left->bb_rightsib, ARCH_CONVERT, rbno);
1676 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1677 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1678 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1680 * If there's a block to the new block's right, make that block
1681 * point back to right instead of to left.
1683 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1684 xfs_alloc_block_t *rrblock; /* rr btree block */
1685 xfs_buf_t *rrbp; /* buffer for rrblock */
1687 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1688 cur->bc_private.a.agno, INT_GET(right->bb_rightsib, ARCH_CONVERT), 0,
1689 &rrbp, XFS_ALLOC_BTREE_REF)))
1691 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1692 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1694 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, rbno);
1695 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1698 * If the cursor is really in the right block, move it there.
1699 * If it's just pointing past the last entry in left, then we'll
1700 * insert there, so don't change anything in that case.
1702 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1703 xfs_btree_setbuf(cur, level, rbp);
1704 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1707 * If there are more levels, we'll need another cursor which refers to
1708 * the right block, no matter where this cursor was.
1710 if (level + 1 < cur->bc_nlevels) {
1711 if ((error = xfs_btree_dup_cursor(cur, curp)))
1713 (*curp)->bc_ptrs[level + 1]++;
1721 * Update keys at all levels from here to the root along the cursor's path.
1723 STATIC int /* error */
1725 xfs_btree_cur_t *cur, /* btree cursor */
1726 xfs_alloc_key_t *keyp, /* new key value to update to */
1727 int level) /* starting level for update */
1729 int ptr; /* index of key in block */
1732 * Go up the tree from this level toward the root.
1733 * At each level, update the key value to the value input.
1734 * Stop when we reach a level where the cursor isn't pointing
1735 * at the first entry in the block.
1737 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1738 xfs_alloc_block_t *block; /* btree block */
1739 xfs_buf_t *bp; /* buffer for block */
1741 int error; /* error return value */
1743 xfs_alloc_key_t *kp; /* ptr to btree block keys */
1745 bp = cur->bc_bufs[level];
1746 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1748 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1751 ptr = cur->bc_ptrs[level];
1752 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
1754 xfs_alloc_log_keys(cur, bp, ptr, ptr);
1760 * Externally visible routines.
1764 * Decrement cursor by one record at the level.
1765 * For nonzero levels the leaf-ward information is untouched.
1768 xfs_alloc_decrement(
1769 xfs_btree_cur_t *cur, /* btree cursor */
1770 int level, /* level in btree, 0 is leaf */
1771 int *stat) /* success/failure */
1773 xfs_alloc_block_t *block; /* btree block */
1774 int error; /* error return value */
1775 int lev; /* btree level */
1777 ASSERT(level < cur->bc_nlevels);
1779 * Read-ahead to the left at this level.
1781 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1783 * Decrement the ptr at this level. If we're still in the block
1786 if (--cur->bc_ptrs[level] > 0) {
1791 * Get a pointer to the btree block.
1793 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[level]);
1795 if ((error = xfs_btree_check_sblock(cur, block, level,
1796 cur->bc_bufs[level])))
1800 * If we just went off the left edge of the tree, return failure.
1802 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1807 * March up the tree decrementing pointers.
1808 * Stop when we don't go off the left edge of a block.
1810 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1811 if (--cur->bc_ptrs[lev] > 0)
1814 * Read-ahead the left block, we're going to read it
1817 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1820 * If we went off the root then we are seriously confused.
1822 ASSERT(lev < cur->bc_nlevels);
1824 * Now walk back down the tree, fixing up the cursor's buffer
1825 * pointers and key numbers.
1827 for (block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1828 xfs_agblock_t agbno; /* block number of btree block */
1829 xfs_buf_t *bp; /* buffer pointer for block */
1831 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1832 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1833 cur->bc_private.a.agno, agbno, 0, &bp,
1834 XFS_ALLOC_BTREE_REF)))
1837 xfs_btree_setbuf(cur, lev, bp);
1838 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1839 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1841 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1848 * Delete the record pointed to by cur.
1849 * The cursor refers to the place where the record was (could be inserted)
1850 * when the operation returns.
1854 xfs_btree_cur_t *cur, /* btree cursor */
1855 int *stat) /* success/failure */
1857 int error; /* error return value */
1858 int i; /* result code */
1859 int level; /* btree level */
1862 * Go up the tree, starting at leaf level.
1863 * If 2 is returned then a join was done; go to the next level.
1864 * Otherwise we are done.
1866 for (level = 0, i = 2; i == 2; level++) {
1867 if ((error = xfs_alloc_delrec(cur, level, &i)))
1871 for (level = 1; level < cur->bc_nlevels; level++) {
1872 if (cur->bc_ptrs[level] == 0) {
1873 if ((error = xfs_alloc_decrement(cur, level, &i)))
1884 * Get the data from the pointed-to record.
1888 xfs_btree_cur_t *cur, /* btree cursor */
1889 xfs_agblock_t *bno, /* output: starting block of extent */
1890 xfs_extlen_t *len, /* output: length of extent */
1891 int *stat) /* output: success/failure */
1893 xfs_alloc_block_t *block; /* btree block */
1895 int error; /* error return value */
1897 int ptr; /* record number */
1899 ptr = cur->bc_ptrs[0];
1900 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1902 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1906 * Off the right end or left end, return failure.
1908 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1913 * Point to the record and extract its data.
1916 xfs_alloc_rec_t *rec; /* record data */
1918 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1919 *bno = INT_GET(rec->ar_startblock, ARCH_CONVERT);
1920 *len = INT_GET(rec->ar_blockcount, ARCH_CONVERT);
1927 * Increment cursor by one record at the level.
1928 * For nonzero levels the leaf-ward information is untouched.
1931 xfs_alloc_increment(
1932 xfs_btree_cur_t *cur, /* btree cursor */
1933 int level, /* level in btree, 0 is leaf */
1934 int *stat) /* success/failure */
1936 xfs_alloc_block_t *block; /* btree block */
1937 xfs_buf_t *bp; /* tree block buffer */
1938 int error; /* error return value */
1939 int lev; /* btree level */
1941 ASSERT(level < cur->bc_nlevels);
1943 * Read-ahead to the right at this level.
1945 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1947 * Get a pointer to the btree block.
1949 bp = cur->bc_bufs[level];
1950 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1952 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1956 * Increment the ptr at this level. If we're still in the block
1959 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1964 * If we just went off the right edge of the tree, return failure.
1966 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1971 * March up the tree incrementing pointers.
1972 * Stop when we don't go off the right edge of a block.
1974 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1975 bp = cur->bc_bufs[lev];
1976 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1978 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1981 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1984 * Read-ahead the right block, we're going to read it
1987 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1990 * If we went off the root then we are seriously confused.
1992 ASSERT(lev < cur->bc_nlevels);
1994 * Now walk back down the tree, fixing up the cursor's buffer
1995 * pointers and key numbers.
1997 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1999 xfs_agblock_t agbno; /* block number of btree block */
2001 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
2002 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
2003 cur->bc_private.a.agno, agbno, 0, &bp,
2004 XFS_ALLOC_BTREE_REF)))
2007 xfs_btree_setbuf(cur, lev, bp);
2008 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2009 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
2011 cur->bc_ptrs[lev] = 1;
2018 * Insert the current record at the point referenced by cur.
2019 * The cursor may be inconsistent on return if splits have been done.
2023 xfs_btree_cur_t *cur, /* btree cursor */
2024 int *stat) /* success/failure */
2026 int error; /* error return value */
2027 int i; /* result value, 0 for failure */
2028 int level; /* current level number in btree */
2029 xfs_agblock_t nbno; /* new block number (split result) */
2030 xfs_btree_cur_t *ncur; /* new cursor (split result) */
2031 xfs_alloc_rec_t nrec; /* record being inserted this level */
2032 xfs_btree_cur_t *pcur; /* previous level's cursor */
2036 INT_SET(nrec.ar_startblock, ARCH_CONVERT, cur->bc_rec.a.ar_startblock);
2037 INT_SET(nrec.ar_blockcount, ARCH_CONVERT, cur->bc_rec.a.ar_blockcount);
2038 ncur = (xfs_btree_cur_t *)0;
2041 * Loop going up the tree, starting at the leaf level.
2042 * Stop when we don't get a split block, that must mean that
2043 * the insert is finished with this level.
2047 * Insert nrec/nbno into this level of the tree.
2048 * Note if we fail, nbno will be null.
2050 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
2053 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2057 * See if the cursor we just used is trash.
2058 * Can't trash the caller's cursor, but otherwise we should
2059 * if ncur is a new cursor or we're about to be done.
2061 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
2062 cur->bc_nlevels = pcur->bc_nlevels;
2063 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2066 * If we got a new cursor, switch to it.
2070 ncur = (xfs_btree_cur_t *)0;
2072 } while (nbno != NULLAGBLOCK);
2078 * Lookup the record equal to [bno, len] in the btree given by cur.
2081 xfs_alloc_lookup_eq(
2082 xfs_btree_cur_t *cur, /* btree cursor */
2083 xfs_agblock_t bno, /* starting block of extent */
2084 xfs_extlen_t len, /* length of extent */
2085 int *stat) /* success/failure */
2087 cur->bc_rec.a.ar_startblock = bno;
2088 cur->bc_rec.a.ar_blockcount = len;
2089 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, stat);
2093 * Lookup the first record greater than or equal to [bno, len]
2094 * in the btree given by cur.
2097 xfs_alloc_lookup_ge(
2098 xfs_btree_cur_t *cur, /* btree cursor */
2099 xfs_agblock_t bno, /* starting block of extent */
2100 xfs_extlen_t len, /* length of extent */
2101 int *stat) /* success/failure */
2103 cur->bc_rec.a.ar_startblock = bno;
2104 cur->bc_rec.a.ar_blockcount = len;
2105 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, stat);
2109 * Lookup the first record less than or equal to [bno, len]
2110 * in the btree given by cur.
2113 xfs_alloc_lookup_le(
2114 xfs_btree_cur_t *cur, /* btree cursor */
2115 xfs_agblock_t bno, /* starting block of extent */
2116 xfs_extlen_t len, /* length of extent */
2117 int *stat) /* success/failure */
2119 cur->bc_rec.a.ar_startblock = bno;
2120 cur->bc_rec.a.ar_blockcount = len;
2121 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, stat);
2125 * Update the record referred to by cur, to the value given by [bno, len].
2126 * This either works (return 0) or gets an EFSCORRUPTED error.
2130 xfs_btree_cur_t *cur, /* btree cursor */
2131 xfs_agblock_t bno, /* starting block of extent */
2132 xfs_extlen_t len) /* length of extent */
2134 xfs_alloc_block_t *block; /* btree block to update */
2135 int error; /* error return value */
2136 int ptr; /* current record number (updating) */
2140 * Pick up the a.g. freelist struct and the current block.
2142 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
2144 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
2148 * Get the address of the rec to be updated.
2150 ptr = cur->bc_ptrs[0];
2152 xfs_alloc_rec_t *rp; /* pointer to updated record */
2154 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
2156 * Fill in the new contents and log them.
2158 INT_SET(rp->ar_startblock, ARCH_CONVERT, bno);
2159 INT_SET(rp->ar_blockcount, ARCH_CONVERT, len);
2160 xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
2163 * If it's the by-size btree and it's the last leaf block and
2164 * it's the last record... then update the size of the longest
2165 * extent in the a.g., which we cache in the a.g. freelist header.
2167 if (cur->bc_btnum == XFS_BTNUM_CNT &&
2168 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
2169 ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
2170 xfs_agf_t *agf; /* a.g. freespace header */
2171 xfs_agnumber_t seqno;
2173 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
2174 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
2175 cur->bc_mp->m_perag[seqno].pagf_longest = len;
2176 INT_SET(agf->agf_longest, ARCH_CONVERT, len);
2177 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
2181 * Updating first record in leaf. Pass new key value up to our parent.
2184 xfs_alloc_key_t key; /* key containing [bno, len] */
2186 INT_SET(key.ar_startblock, ARCH_CONVERT, bno);
2187 INT_SET(key.ar_blockcount, ARCH_CONVERT, len);
2188 if ((error = xfs_alloc_updkey(cur, &key, 1)))