2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_alloc.h"
56 #include "xfs_error.h"
59 * Prototypes for internal functions.
62 STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
63 STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
64 STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
65 STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
66 STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
67 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
68 STATIC int xfs_alloc_rshift(xfs_btree_cur_t *, int, int *);
69 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
70 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
71 STATIC int xfs_alloc_updkey(xfs_btree_cur_t *, xfs_alloc_key_t *, int);
78 * Single level of the xfs_alloc_delete record deletion routine.
79 * Delete record pointed to by cur/level.
80 * Remove the record from its block then rebalance the tree.
81 * Return 0 for error, 1 for done, 2 to go on to the next level.
83 STATIC int /* error */
85 xfs_btree_cur_t *cur, /* btree cursor */
86 int level, /* level removing record from */
87 int *stat) /* fail/done/go-on */
89 xfs_agf_t *agf; /* allocation group freelist header */
90 xfs_alloc_block_t *block; /* btree block record/key lives in */
91 xfs_agblock_t bno; /* btree block number */
92 xfs_buf_t *bp; /* buffer for block */
93 int error; /* error return value */
94 int i; /* loop index */
95 xfs_alloc_key_t key; /* kp points here if block is level 0 */
96 xfs_agblock_t lbno; /* left block's block number */
97 xfs_buf_t *lbp; /* left block's buffer pointer */
98 xfs_alloc_block_t *left; /* left btree block */
99 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
100 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
101 int lrecs=0; /* number of records in left block */
102 xfs_alloc_rec_t *lrp; /* left block record pointer */
103 xfs_mount_t *mp; /* mount structure */
104 int ptr; /* index in btree block for this rec */
105 xfs_agblock_t rbno; /* right block's block number */
106 xfs_buf_t *rbp; /* right block's buffer pointer */
107 xfs_alloc_block_t *right; /* right btree block */
108 xfs_alloc_key_t *rkp; /* right block key pointer */
109 xfs_alloc_ptr_t *rpp; /* right block address pointer */
110 int rrecs=0; /* number of records in right block */
111 xfs_alloc_rec_t *rrp; /* right block record pointer */
112 xfs_btree_cur_t *tcur; /* temporary btree cursor */
115 * Get the index of the entry being deleted, check for nothing there.
117 ptr = cur->bc_ptrs[level];
123 * Get the buffer & block containing the record or key/ptr.
125 bp = cur->bc_bufs[level];
126 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
128 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
132 * Fail if we're off the end of the block.
134 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
138 XFS_STATS_INC(xs_abt_delrec);
140 * It's a nonleaf. Excise the key and ptr being deleted, by
141 * sliding the entries past them down one.
142 * Log the changed areas of the block.
145 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
146 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
148 for (i = ptr; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++) {
149 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
153 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
154 memmove(&lkp[ptr - 1], &lkp[ptr],
155 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lkp)); /* INT_: mem copy */
156 memmove(&lpp[ptr - 1], &lpp[ptr],
157 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lpp)); /* INT_: mem copy */
158 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
159 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
163 * It's a leaf. Excise the record being deleted, by sliding the
164 * entries past it down one. Log the changed areas of the block.
167 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
168 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
169 memmove(&lrp[ptr - 1], &lrp[ptr],
170 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr) * sizeof(*lrp));
171 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT) - 1);
174 * If it's the first record in the block, we'll need a key
175 * structure to pass up to the next level (updkey).
178 key.ar_startblock = lrp->ar_startblock; /* INT_: direct copy */
179 key.ar_blockcount = lrp->ar_blockcount; /* INT_: direct copy */
184 * Decrement and log the number of entries in the block.
186 INT_MOD(block->bb_numrecs, ARCH_CONVERT, -1);
187 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
189 * See if the longest free extent in the allocation group was
190 * changed by this operation. True if it's the by-size btree, and
191 * this is the leaf level, and there is no right sibling block,
192 * and this was the last record.
194 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
198 cur->bc_btnum == XFS_BTNUM_CNT &&
199 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
200 ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
201 ASSERT(ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT) + 1);
203 * There are still records in the block. Grab the size
206 if (INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
207 rrp = XFS_ALLOC_REC_ADDR(block, INT_GET(block->bb_numrecs, ARCH_CONVERT), cur);
208 INT_COPY(agf->agf_longest, rrp->ar_blockcount, ARCH_CONVERT);
211 * No free extents left.
214 agf->agf_longest = 0;
215 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest =
216 INT_GET(agf->agf_longest, ARCH_CONVERT);
217 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
221 * Is this the root level? If so, we're almost done.
223 if (level == cur->bc_nlevels - 1) {
225 * If this is the root level,
226 * and there's only one entry left,
227 * and it's NOT the leaf level,
228 * then we can get rid of this level.
230 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == 1 && level > 0) {
232 * lpp is still set to the first pointer in the block.
233 * Make it the new root of the btree.
235 bno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
236 INT_COPY(agf->agf_roots[cur->bc_btnum], *lpp, ARCH_CONVERT);
237 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, -1);
238 mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_levels[cur->bc_btnum]--;
240 * Put this buffer/block on the ag's freelist.
242 if ((error = xfs_alloc_put_freelist(cur->bc_tp,
243 cur->bc_private.a.agbp, NULL, bno)))
246 * Since blocks move to the free list without the
247 * coordination used in xfs_bmap_finish, we can't allow
248 * block to be available for reallocation and
249 * non-transaction writing (user data) until we know
250 * that the transaction that moved it to the free list
251 * is permanently on disk. We track the blocks by
252 * declaring these blocks as "busy"; the busy list is
253 * maintained on a per-ag basis and each transaction
254 * records which entries should be removed when the
255 * iclog commits to disk. If a busy block is
256 * allocated, the iclog is pushed up to the LSN
257 * that freed the block.
259 xfs_alloc_mark_busy(cur->bc_tp,
260 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
262 xfs_trans_agbtree_delta(cur->bc_tp, -1);
263 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
264 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
266 * Update the cursor so there's one fewer level.
268 xfs_btree_setbuf(cur, level, NULL);
270 } else if (level > 0 &&
271 (error = xfs_alloc_decrement(cur, level, &i)))
277 * If we deleted the leftmost entry in the block, update the
278 * key values above us in the tree.
280 if (ptr == 1 && (error = xfs_alloc_updkey(cur, lkp, level + 1)))
283 * If the number of records remaining in the block is at least
284 * the minimum, we're done.
286 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
287 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
293 * Otherwise, we have to move some records around to keep the
294 * tree balanced. Look at the left and right sibling blocks to
295 * see if we can re-balance by moving only one record.
297 rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
298 lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT);
300 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
302 * Duplicate the cursor so our btree manipulations here won't
303 * disrupt the next level up.
305 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
308 * If there's a right sibling, see if it's ok to shift an entry
311 if (rbno != NULLAGBLOCK) {
313 * Move the temp cursor to the last entry in the next block.
314 * Actually any entry but the first would suffice.
316 i = xfs_btree_lastrec(tcur, level);
317 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
318 if ((error = xfs_alloc_increment(tcur, level, &i)))
320 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
321 i = xfs_btree_lastrec(tcur, level);
322 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
324 * Grab a pointer to the block.
326 rbp = tcur->bc_bufs[level];
327 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
329 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
333 * Grab the current block number, for future use.
335 bno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
337 * If right block is full enough so that removing one entry
338 * won't make it too empty, and left-shifting an entry out
339 * of right to us works, we're done.
341 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >=
342 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
343 if ((error = xfs_alloc_lshift(tcur, level, &i)))
346 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
347 XFS_ALLOC_BLOCK_MINRECS(level, cur));
348 xfs_btree_del_cursor(tcur,
351 (error = xfs_alloc_decrement(cur, level,
359 * Otherwise, grab the number of records in right for
360 * future reference, and fix up the temp cursor to point
361 * to our block again (last record).
363 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
364 if (lbno != NULLAGBLOCK) {
365 i = xfs_btree_firstrec(tcur, level);
366 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
367 if ((error = xfs_alloc_decrement(tcur, level, &i)))
369 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
373 * If there's a left sibling, see if it's ok to shift an entry
376 if (lbno != NULLAGBLOCK) {
378 * Move the temp cursor to the first entry in the
381 i = xfs_btree_firstrec(tcur, level);
382 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
383 if ((error = xfs_alloc_decrement(tcur, level, &i)))
385 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
386 xfs_btree_firstrec(tcur, level);
388 * Grab a pointer to the block.
390 lbp = tcur->bc_bufs[level];
391 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
393 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
397 * Grab the current block number, for future use.
399 bno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
401 * If left block is full enough so that removing one entry
402 * won't make it too empty, and right-shifting an entry out
403 * of left to us works, we're done.
405 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >=
406 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
407 if ((error = xfs_alloc_rshift(tcur, level, &i)))
410 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
411 XFS_ALLOC_BLOCK_MINRECS(level, cur));
412 xfs_btree_del_cursor(tcur,
421 * Otherwise, grab the number of records in right for
424 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
427 * Delete the temp cursor, we're done with it.
429 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
431 * If here, we need to do a join to keep the tree balanced.
433 ASSERT(bno != NULLAGBLOCK);
435 * See if we can join with the left neighbor block.
437 if (lbno != NULLAGBLOCK &&
438 lrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
440 * Set "right" to be the starting block,
441 * "left" to be the left neighbor.
446 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
447 cur->bc_private.a.agno, lbno, 0, &lbp,
448 XFS_ALLOC_BTREE_REF)))
450 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
451 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
455 * If that won't work, see if we can join with the right neighbor block.
457 else if (rbno != NULLAGBLOCK &&
458 rrecs + INT_GET(block->bb_numrecs, ARCH_CONVERT) <=
459 XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
461 * Set "left" to be the starting block,
462 * "right" to be the right neighbor.
467 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
468 cur->bc_private.a.agno, rbno, 0, &rbp,
469 XFS_ALLOC_BTREE_REF)))
471 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
472 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
476 * Otherwise, we can't fix the imbalance.
477 * Just return. This is probably a logic error, but it's not fatal.
480 if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
486 * We're now going to join "left" and "right" by moving all the stuff
487 * in "right" to "left" and deleting "right".
491 * It's a non-leaf. Move keys and pointers.
493 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
494 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
495 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
496 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
498 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
499 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
503 memcpy(lkp, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lkp)); /* INT_: structure copy */
504 memcpy(lpp, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lpp)); /* INT_: structure copy */
505 xfs_alloc_log_keys(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
506 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
507 xfs_alloc_log_ptrs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
508 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
511 * It's a leaf. Move records.
513 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1, cur);
514 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
515 memcpy(lrp, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*lrp));
516 xfs_alloc_log_recs(cur, lbp, INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1,
517 INT_GET(left->bb_numrecs, ARCH_CONVERT) + INT_GET(right->bb_numrecs, ARCH_CONVERT));
520 * If we joined with the left neighbor, set the buffer in the
521 * cursor to the left block, and fix up the index.
524 xfs_btree_setbuf(cur, level, lbp);
525 cur->bc_ptrs[level] += INT_GET(left->bb_numrecs, ARCH_CONVERT);
528 * If we joined with the right neighbor and there's a level above
529 * us, increment the cursor at that level.
531 else if (level + 1 < cur->bc_nlevels &&
532 (error = xfs_alloc_increment(cur, level + 1, &i)))
535 * Fix up the number of records in the surviving block.
537 INT_MOD(left->bb_numrecs, ARCH_CONVERT, INT_GET(right->bb_numrecs, ARCH_CONVERT));
539 * Fix up the right block pointer in the surviving block, and log it.
541 left->bb_rightsib = right->bb_rightsib; /* INT_: direct copy */
542 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
544 * If there is a right sibling now, make it point to the
547 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
548 xfs_alloc_block_t *rrblock;
551 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
552 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0,
553 &rrbp, XFS_ALLOC_BTREE_REF)))
555 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
556 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
558 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, lbno);
559 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
562 * Free the deleting block by putting it on the freelist.
564 if ((error = xfs_alloc_put_freelist(cur->bc_tp, cur->bc_private.a.agbp,
568 * Since blocks move to the free list without the coordination
569 * used in xfs_bmap_finish, we can't allow block to be available
570 * for reallocation and non-transaction writing (user data)
571 * until we know that the transaction that moved it to the free
572 * list is permanently on disk. We track the blocks by declaring
573 * these blocks as "busy"; the busy list is maintained on a
574 * per-ag basis and each transaction records which entries
575 * should be removed when the iclog commits to disk. If a
576 * busy block is allocated, the iclog is pushed up to the
577 * LSN that freed the block.
579 xfs_alloc_mark_busy(cur->bc_tp,
580 INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
582 xfs_trans_agbtree_delta(cur->bc_tp, -1);
584 * Adjust the current level's cursor so that we're left referring
585 * to the right node, after we're done.
586 * If this leaves the ptr value 0 our caller will fix it up.
589 cur->bc_ptrs[level]--;
591 * Return value means the next level up has something to do.
597 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
602 * Insert one record/level. Return information to the caller
603 * allowing the next level up to proceed if necessary.
605 STATIC int /* error */
607 xfs_btree_cur_t *cur, /* btree cursor */
608 int level, /* level to insert record at */
609 xfs_agblock_t *bnop, /* i/o: block number inserted */
610 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
611 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
612 int *stat) /* output: success/failure */
614 xfs_agf_t *agf; /* allocation group freelist header */
615 xfs_alloc_block_t *block; /* btree block record/key lives in */
616 xfs_buf_t *bp; /* buffer for block */
617 int error; /* error return value */
618 int i; /* loop index */
619 xfs_alloc_key_t key; /* key value being inserted */
620 xfs_alloc_key_t *kp; /* pointer to btree keys */
621 xfs_agblock_t nbno; /* block number of allocated block */
622 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
623 xfs_alloc_key_t nkey; /* new key value, from split */
624 xfs_alloc_rec_t nrec; /* new record value, for caller */
625 int optr; /* old ptr value */
626 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
627 int ptr; /* index in btree block for this rec */
628 xfs_alloc_rec_t *rp; /* pointer to btree records */
630 ASSERT(INT_GET(recp->ar_blockcount, ARCH_CONVERT) > 0);
632 * If we made it to the root level, allocate a new root block
635 if (level >= cur->bc_nlevels) {
636 XFS_STATS_INC(xs_abt_insrec);
637 if ((error = xfs_alloc_newroot(cur, &i)))
644 * Make a key out of the record data to be inserted, and save it.
646 key.ar_startblock = recp->ar_startblock; /* INT_: direct copy */
647 key.ar_blockcount = recp->ar_blockcount; /* INT_: direct copy */
648 optr = ptr = cur->bc_ptrs[level];
650 * If we're off the left edge, return failure.
656 XFS_STATS_INC(xs_abt_insrec);
658 * Get pointers to the btree buffer and block.
660 bp = cur->bc_bufs[level];
661 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
663 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
666 * Check that the new entry is being inserted in the right place.
668 if (ptr <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
670 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
671 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
673 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
674 xfs_btree_check_key(cur->bc_btnum, &key, kp);
679 ncur = (xfs_btree_cur_t *)0;
681 * If the block is full, we can't insert the new entry until we
682 * make the block un-full.
684 if (INT_GET(block->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
686 * First, try shifting an entry to the right neighbor.
688 if ((error = xfs_alloc_rshift(cur, level, &i)))
694 * Next, try shifting an entry to the left neighbor.
697 if ((error = xfs_alloc_lshift(cur, level, &i)))
700 optr = ptr = cur->bc_ptrs[level];
703 * Next, try splitting the current block in
704 * half. If this works we have to re-set our
705 * variables because we could be in a
706 * different block now.
708 if ((error = xfs_alloc_split(cur, level, &nbno,
712 bp = cur->bc_bufs[level];
713 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
716 xfs_btree_check_sblock(cur,
720 ptr = cur->bc_ptrs[level];
721 nrec.ar_startblock = nkey.ar_startblock; /* INT_: direct copy */
722 nrec.ar_blockcount = nkey.ar_blockcount; /* INT_: direct copy */
725 * Otherwise the insert fails.
735 * At this point we know there's room for our new entry in the block
740 * It's a non-leaf entry. Make a hole for the new data
741 * in the key and ptr regions of the block.
743 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
744 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
746 for (i = INT_GET(block->bb_numrecs, ARCH_CONVERT); i >= ptr; i--) {
747 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
751 memmove(&kp[ptr], &kp[ptr - 1],
752 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*kp)); /* INT_: copy */
753 memmove(&pp[ptr], &pp[ptr - 1],
754 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*pp)); /* INT_: copy */
756 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
760 * Now stuff the new data in, bump numrecs and log the new data.
763 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
764 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
765 xfs_alloc_log_keys(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
766 xfs_alloc_log_ptrs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
768 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
769 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
774 * It's a leaf entry. Make a hole for the new record.
776 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
777 memmove(&rp[ptr], &rp[ptr - 1],
778 (INT_GET(block->bb_numrecs, ARCH_CONVERT) - ptr + 1) * sizeof(*rp));
780 * Now stuff the new record in, bump numrecs
781 * and log the new data.
783 rp[ptr - 1] = *recp; /* INT_: struct copy */
784 INT_MOD(block->bb_numrecs, ARCH_CONVERT, +1);
785 xfs_alloc_log_recs(cur, bp, ptr, INT_GET(block->bb_numrecs, ARCH_CONVERT));
787 if (ptr < INT_GET(block->bb_numrecs, ARCH_CONVERT))
788 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
793 * Log the new number of records in the btree header.
795 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
797 * If we inserted at the start of a block, update the parents' keys.
799 if (optr == 1 && (error = xfs_alloc_updkey(cur, &key, level + 1)))
802 * Look to see if the longest extent in the allocation group
803 * needs to be updated.
806 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
808 cur->bc_btnum == XFS_BTNUM_CNT &&
809 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
810 INT_GET(recp->ar_blockcount, ARCH_CONVERT) > INT_GET(agf->agf_longest, ARCH_CONVERT)) {
812 * If this is a leaf in the by-size btree and there
813 * is no right sibling block and this block is bigger
814 * than the previous longest block, update it.
816 INT_COPY(agf->agf_longest, recp->ar_blockcount, ARCH_CONVERT);
817 cur->bc_mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)].pagf_longest
818 = INT_GET(recp->ar_blockcount, ARCH_CONVERT);
819 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
823 * Return the new block number, if any.
824 * If there is one, give back a record value and a cursor too.
827 if (nbno != NULLAGBLOCK) {
828 *recp = nrec; /* INT_: struct copy */
829 *curp = ncur; /* INT_: struct copy */
836 * Log header fields from a btree block.
840 xfs_trans_t *tp, /* transaction pointer */
841 xfs_buf_t *bp, /* buffer containing btree block */
842 int fields) /* mask of fields: XFS_BB_... */
844 int first; /* first byte offset logged */
845 int last; /* last byte offset logged */
846 static const short offsets[] = { /* table of offsets */
847 offsetof(xfs_alloc_block_t, bb_magic),
848 offsetof(xfs_alloc_block_t, bb_level),
849 offsetof(xfs_alloc_block_t, bb_numrecs),
850 offsetof(xfs_alloc_block_t, bb_leftsib),
851 offsetof(xfs_alloc_block_t, bb_rightsib),
852 sizeof(xfs_alloc_block_t)
855 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
856 xfs_trans_log_buf(tp, bp, first, last);
860 * Log keys from a btree block (nonleaf).
864 xfs_btree_cur_t *cur, /* btree cursor */
865 xfs_buf_t *bp, /* buffer containing btree block */
866 int kfirst, /* index of first key to log */
867 int klast) /* index of last key to log */
869 xfs_alloc_block_t *block; /* btree block to log from */
870 int first; /* first byte offset logged */
871 xfs_alloc_key_t *kp; /* key pointer in btree block */
872 int last; /* last byte offset logged */
874 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
875 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
876 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
877 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
878 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
882 * Log block pointer fields from a btree block (nonleaf).
886 xfs_btree_cur_t *cur, /* btree cursor */
887 xfs_buf_t *bp, /* buffer containing btree block */
888 int pfirst, /* index of first pointer to log */
889 int plast) /* index of last pointer to log */
891 xfs_alloc_block_t *block; /* btree block to log from */
892 int first; /* first byte offset logged */
893 int last; /* last byte offset logged */
894 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
896 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
897 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
898 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
899 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
900 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
904 * Log records from a btree block (leaf).
908 xfs_btree_cur_t *cur, /* btree cursor */
909 xfs_buf_t *bp, /* buffer containing btree block */
910 int rfirst, /* index of first record to log */
911 int rlast) /* index of last record to log */
913 xfs_alloc_block_t *block; /* btree block to log from */
914 int first; /* first byte offset logged */
915 int last; /* last byte offset logged */
916 xfs_alloc_rec_t *rp; /* record pointer for btree block */
919 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
920 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
926 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
927 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
928 ASSERT(INT_GET(p->ar_startblock, ARCH_CONVERT) + INT_GET(p->ar_blockcount, ARCH_CONVERT) <=
929 INT_GET(agf->agf_length, ARCH_CONVERT));
932 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
933 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
934 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
938 * Lookup the record. The cursor is made to point to it, based on dir.
939 * Return 0 if can't find any such record, 1 for success.
941 STATIC int /* error */
943 xfs_btree_cur_t *cur, /* btree cursor */
944 xfs_lookup_t dir, /* <=, ==, or >= */
945 int *stat) /* success/failure */
947 xfs_agblock_t agbno; /* a.g. relative btree block number */
948 xfs_agnumber_t agno; /* allocation group number */
949 xfs_alloc_block_t *block=NULL; /* current btree block */
950 int diff; /* difference for the current key */
951 int error; /* error return value */
952 int keyno=0; /* current key number */
953 int level; /* level in the btree */
954 xfs_mount_t *mp; /* file system mount point */
956 XFS_STATS_INC(xs_abt_lookup);
958 * Get the allocation group header, and the root block number.
963 xfs_agf_t *agf; /* a.g. freespace header */
965 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
966 agno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
967 agbno = INT_GET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT);
970 * Iterate over each level in the btree, starting at the root.
971 * For each level above the leaves, find the key we need, based
972 * on the lookup record, then follow the corresponding block
973 * pointer down to the next level.
975 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
976 xfs_buf_t *bp; /* buffer pointer for btree block */
977 xfs_daddr_t d; /* disk address of btree block */
980 * Get the disk address we're looking for.
982 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
984 * If the old buffer at this level is for a different block,
985 * throw it away, otherwise just use it.
987 bp = cur->bc_bufs[level];
988 if (bp && XFS_BUF_ADDR(bp) != d)
992 * Need to get a new buffer. Read it, then
993 * set it in the cursor, releasing the old one.
995 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, agno,
996 agbno, 0, &bp, XFS_ALLOC_BTREE_REF)))
998 xfs_btree_setbuf(cur, level, bp);
1000 * Point to the btree block, now that we have the buffer
1002 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1003 if ((error = xfs_btree_check_sblock(cur, block, level,
1007 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1009 * If we already had a key match at a higher level, we know
1010 * we need to use the first entry in this block.
1015 * Otherwise we need to search this block. Do a binary search.
1018 int high; /* high entry number */
1019 xfs_alloc_key_t *kkbase=NULL;/* base of keys in block */
1020 xfs_alloc_rec_t *krbase=NULL;/* base of records in block */
1021 int low; /* low entry number */
1024 * Get a pointer to keys or records.
1027 kkbase = XFS_ALLOC_KEY_ADDR(block, 1, cur);
1029 krbase = XFS_ALLOC_REC_ADDR(block, 1, cur);
1031 * Set low and high entry numbers, 1-based.
1034 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
1036 * If the block is empty, the tree must
1039 ASSERT(level == 0 && cur->bc_nlevels == 1);
1040 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1045 * Binary search the block.
1047 while (low <= high) {
1048 xfs_extlen_t blockcount; /* key value */
1049 xfs_agblock_t startblock; /* key value */
1051 XFS_STATS_INC(xs_abt_compare);
1053 * keyno is average of low and high.
1055 keyno = (low + high) >> 1;
1057 * Get startblock & blockcount.
1060 xfs_alloc_key_t *kkp;
1062 kkp = kkbase + keyno - 1;
1063 startblock = INT_GET(kkp->ar_startblock, ARCH_CONVERT);
1064 blockcount = INT_GET(kkp->ar_blockcount, ARCH_CONVERT);
1066 xfs_alloc_rec_t *krp;
1068 krp = krbase + keyno - 1;
1069 startblock = INT_GET(krp->ar_startblock, ARCH_CONVERT);
1070 blockcount = INT_GET(krp->ar_blockcount, ARCH_CONVERT);
1073 * Compute difference to get next direction.
1075 if (cur->bc_btnum == XFS_BTNUM_BNO)
1076 diff = (int)startblock -
1077 (int)cur->bc_rec.a.ar_startblock;
1078 else if (!(diff = (int)blockcount -
1079 (int)cur->bc_rec.a.ar_blockcount))
1080 diff = (int)startblock -
1081 (int)cur->bc_rec.a.ar_startblock;
1083 * Less than, move right.
1088 * Greater than, move left.
1093 * Equal, we're done.
1100 * If there are more levels, set up for the next level
1101 * by getting the block number and filling in the cursor.
1105 * If we moved left, need the previous key number,
1106 * unless there isn't one.
1108 if (diff > 0 && --keyno < 1)
1110 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1112 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1115 cur->bc_ptrs[level] = keyno;
1119 * Done with the search.
1120 * See if we need to adjust the results.
1122 if (dir != XFS_LOOKUP_LE && diff < 0) {
1125 * If ge search and we went off the end of the block, but it's
1126 * not the last block, we're in the wrong block.
1128 if (dir == XFS_LOOKUP_GE &&
1129 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1130 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1133 cur->bc_ptrs[0] = keyno;
1134 if ((error = xfs_alloc_increment(cur, 0, &i)))
1136 XFS_WANT_CORRUPTED_RETURN(i == 1);
1141 else if (dir == XFS_LOOKUP_LE && diff > 0)
1143 cur->bc_ptrs[0] = keyno;
1145 * Return if we succeeded or not.
1147 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1150 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1155 * Move 1 record left from cur/level if possible.
1156 * Update cur to reflect the new path.
1158 STATIC int /* error */
1160 xfs_btree_cur_t *cur, /* btree cursor */
1161 int level, /* level to shift record on */
1162 int *stat) /* success/failure */
1164 int error; /* error return value */
1166 int i; /* loop index */
1168 xfs_alloc_key_t key; /* key value for leaf level upward */
1169 xfs_buf_t *lbp; /* buffer for left neighbor block */
1170 xfs_alloc_block_t *left; /* left neighbor btree block */
1171 int nrec; /* new number of left block entries */
1172 xfs_buf_t *rbp; /* buffer for right (current) block */
1173 xfs_alloc_block_t *right; /* right (current) btree block */
1174 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
1175 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
1176 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
1179 * Set up variables for this block as "right".
1181 rbp = cur->bc_bufs[level];
1182 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1184 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1188 * If we've got no left sibling then we can't shift an entry left.
1190 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1195 * If the cursor entry is the one that would be moved, don't
1196 * do it... it's too complicated.
1198 if (cur->bc_ptrs[level] <= 1) {
1203 * Set up the left neighbor as "left".
1205 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1206 cur->bc_private.a.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1207 XFS_ALLOC_BTREE_REF)))
1209 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1210 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1213 * If it's full, it can't take another entry.
1215 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1219 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1221 * If non-leaf, copy a key and a ptr to the left block.
1224 xfs_alloc_key_t *lkp; /* key pointer for left block */
1225 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1227 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1228 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1230 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1231 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1232 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1234 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1237 *lpp = *rpp; /* INT_: copy */
1238 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1239 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1242 * If leaf, copy a record to the left block.
1245 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1247 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1248 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1250 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1251 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1254 * Bump and log left's numrecs, decrement and log right's numrecs.
1256 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1257 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1258 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1259 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1261 * Slide the contents of right down one entry.
1265 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1266 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1271 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1272 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1273 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1274 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1276 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1277 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1278 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1279 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1283 * Update the parent key values of right.
1285 if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
1288 * Slide the cursor value left one.
1290 cur->bc_ptrs[level]--;
1296 * Allocate a new root block, fill it in.
1298 STATIC int /* error */
1300 xfs_btree_cur_t *cur, /* btree cursor */
1301 int *stat) /* success/failure */
1303 int error; /* error return value */
1304 xfs_agblock_t lbno; /* left block number */
1305 xfs_buf_t *lbp; /* left btree buffer */
1306 xfs_alloc_block_t *left; /* left btree block */
1307 xfs_mount_t *mp; /* mount structure */
1308 xfs_agblock_t nbno; /* new block number */
1309 xfs_buf_t *nbp; /* new (root) buffer */
1310 xfs_alloc_block_t *new; /* new (root) btree block */
1311 int nptr; /* new value for key index, 1 or 2 */
1312 xfs_agblock_t rbno; /* right block number */
1313 xfs_buf_t *rbp; /* right btree buffer */
1314 xfs_alloc_block_t *right; /* right btree block */
1318 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1320 * Get a buffer from the freelist blocks, for the new root.
1322 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1326 * None available, we fail.
1328 if (nbno == NULLAGBLOCK) {
1332 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1333 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1335 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1337 * Set the root data in the a.g. freespace structure.
1340 xfs_agf_t *agf; /* a.g. freespace header */
1341 xfs_agnumber_t seqno;
1343 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1344 INT_SET(agf->agf_roots[cur->bc_btnum], ARCH_CONVERT, nbno);
1345 INT_MOD(agf->agf_levels[cur->bc_btnum], ARCH_CONVERT, 1);
1346 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
1347 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1348 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1349 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1352 * At the previous root level there are now two blocks: the old
1353 * root, and the new block generated when it was split.
1354 * We don't know which one the cursor is pointing at, so we
1355 * set up variables "left" and "right" for each case.
1357 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1358 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1360 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1363 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1365 * Our block is left, pick up the right block.
1367 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1368 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1369 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1370 cur->bc_private.a.agno, rbno, 0, &rbp,
1371 XFS_ALLOC_BTREE_REF)))
1373 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1374 if ((error = xfs_btree_check_sblock(cur, right,
1375 cur->bc_nlevels - 1, rbp)))
1380 * Our block is right, pick up the left block.
1384 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1385 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1386 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1387 cur->bc_private.a.agno, lbno, 0, &lbp,
1388 XFS_ALLOC_BTREE_REF)))
1390 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1391 if ((error = xfs_btree_check_sblock(cur, left,
1392 cur->bc_nlevels - 1, lbp)))
1397 * Fill in the new block's btree header and log it.
1399 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1400 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1401 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1402 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1403 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1404 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1405 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1407 * Fill in the key data in the new root.
1410 xfs_alloc_key_t *kp; /* btree key pointer */
1412 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1413 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1414 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); /* INT_: structure copy */
1415 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);/* INT_: structure copy */
1417 xfs_alloc_rec_t *rp; /* btree record pointer */
1419 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1420 kp[0].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1421 kp[0].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1422 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1423 kp[1].ar_startblock = rp->ar_startblock; /* INT_: direct copy */
1424 kp[1].ar_blockcount = rp->ar_blockcount; /* INT_: direct copy */
1427 xfs_alloc_log_keys(cur, nbp, 1, 2);
1429 * Fill in the pointer data in the new root.
1432 xfs_alloc_ptr_t *pp; /* btree address pointer */
1434 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1435 INT_SET(pp[0], ARCH_CONVERT, lbno);
1436 INT_SET(pp[1], ARCH_CONVERT, rbno);
1438 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1440 * Fix up the cursor.
1442 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1443 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1450 * Move 1 record right from cur/level if possible.
1451 * Update cur to reflect the new path.
1453 STATIC int /* error */
1455 xfs_btree_cur_t *cur, /* btree cursor */
1456 int level, /* level to shift record on */
1457 int *stat) /* success/failure */
1459 int error; /* error return value */
1460 int i; /* loop index */
1461 xfs_alloc_key_t key; /* key value for leaf level upward */
1462 xfs_buf_t *lbp; /* buffer for left (current) block */
1463 xfs_alloc_block_t *left; /* left (current) btree block */
1464 xfs_buf_t *rbp; /* buffer for right neighbor block */
1465 xfs_alloc_block_t *right; /* right neighbor btree block */
1466 xfs_alloc_key_t *rkp; /* key pointer for right block */
1467 xfs_btree_cur_t *tcur; /* temporary cursor */
1470 * Set up variables for this block as "left".
1472 lbp = cur->bc_bufs[level];
1473 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1475 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1479 * If we've got no right sibling then we can't shift an entry right.
1481 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1486 * If the cursor entry is the one that would be moved, don't
1487 * do it... it's too complicated.
1489 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1494 * Set up the right neighbor as "right".
1496 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1497 cur->bc_private.a.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1498 XFS_ALLOC_BTREE_REF)))
1500 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1501 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1504 * If it's full, it can't take another entry.
1506 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1511 * Make a hole at the start of the right neighbor block, then
1512 * copy the last left block entry to the hole.
1515 xfs_alloc_key_t *lkp; /* key pointer for left block */
1516 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1517 xfs_alloc_ptr_t *rpp; /* address pointer for right block */
1519 lkp = XFS_ALLOC_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1520 lpp = XFS_ALLOC_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1521 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1522 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1524 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1525 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1529 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1530 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1532 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1535 *rkp = *lkp; /* INT_: copy */
1536 *rpp = *lpp; /* INT_: copy */
1537 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1538 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1539 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1541 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1542 xfs_alloc_rec_t *rrp; /* record pointer for right block */
1544 lrp = XFS_ALLOC_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1545 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1546 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1548 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1549 key.ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1550 key.ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1552 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1555 * Decrement and log left's numrecs, bump and log right's numrecs.
1557 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1558 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1559 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1560 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1562 * Using a temporary cursor, update the parent key values of the
1563 * block on the right.
1565 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1567 i = xfs_btree_lastrec(tcur, level);
1568 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1569 if ((error = xfs_alloc_increment(tcur, level, &i)) ||
1570 (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
1572 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1576 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1581 * Split cur/level block in half.
1582 * Return new block number and its first record (to be inserted into parent).
1584 STATIC int /* error */
1586 xfs_btree_cur_t *cur, /* btree cursor */
1587 int level, /* level to split */
1588 xfs_agblock_t *bnop, /* output: block number allocated */
1589 xfs_alloc_key_t *keyp, /* output: first key of new block */
1590 xfs_btree_cur_t **curp, /* output: new cursor */
1591 int *stat) /* success/failure */
1593 int error; /* error return value */
1594 int i; /* loop index/record number */
1595 xfs_agblock_t lbno; /* left (current) block number */
1596 xfs_buf_t *lbp; /* buffer for left block */
1597 xfs_alloc_block_t *left; /* left (current) btree block */
1598 xfs_agblock_t rbno; /* right (new) block number */
1599 xfs_buf_t *rbp; /* buffer for right block */
1600 xfs_alloc_block_t *right; /* right (new) btree block */
1603 * Allocate the new block from the freelist.
1604 * If we can't do it, we're toast. Give up.
1606 if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
1609 if (rbno == NULLAGBLOCK) {
1613 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1614 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1617 * Set up the new block as "right".
1619 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1621 * "Left" is the current (according to the cursor) block.
1623 lbp = cur->bc_bufs[level];
1624 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1626 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1630 * Fill in the btree header for the new block.
1632 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1633 right->bb_level = left->bb_level; /* INT_: direct copy */
1634 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1636 * Make sure that if there's an odd number of entries now, that
1637 * each new block will have the same number of entries.
1639 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1640 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1641 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1642 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1644 * For non-leaf blocks, copy keys and addresses over to the new block.
1647 xfs_alloc_key_t *lkp; /* left btree key pointer */
1648 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1649 xfs_alloc_key_t *rkp; /* right btree key pointer */
1650 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1652 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1653 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1654 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1655 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1657 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1658 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1662 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp)); /* INT_: copy */
1663 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp)); /* INT_: copy */
1664 xfs_alloc_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1665 xfs_alloc_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1669 * For leaf blocks, copy records over to the new block.
1672 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1673 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1675 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1676 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1677 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1678 xfs_alloc_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1679 keyp->ar_startblock = rrp->ar_startblock; /* INT_: direct copy */
1680 keyp->ar_blockcount = rrp->ar_blockcount; /* INT_: direct copy */
1683 * Find the left block number by looking in the buffer.
1684 * Adjust numrecs, sibling pointers.
1686 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1687 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1688 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1689 INT_SET(left->bb_rightsib, ARCH_CONVERT, rbno);
1690 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1691 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1692 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1694 * If there's a block to the new block's right, make that block
1695 * point back to right instead of to left.
1697 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1698 xfs_alloc_block_t *rrblock; /* rr btree block */
1699 xfs_buf_t *rrbp; /* buffer for rrblock */
1701 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1702 cur->bc_private.a.agno, INT_GET(right->bb_rightsib, ARCH_CONVERT), 0,
1703 &rrbp, XFS_ALLOC_BTREE_REF)))
1705 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1706 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1708 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, rbno);
1709 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1712 * If the cursor is really in the right block, move it there.
1713 * If it's just pointing past the last entry in left, then we'll
1714 * insert there, so don't change anything in that case.
1716 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1717 xfs_btree_setbuf(cur, level, rbp);
1718 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1721 * If there are more levels, we'll need another cursor which refers to
1722 * the right block, no matter where this cursor was.
1724 if (level + 1 < cur->bc_nlevels) {
1725 if ((error = xfs_btree_dup_cursor(cur, curp)))
1727 (*curp)->bc_ptrs[level + 1]++;
1735 * Update keys at all levels from here to the root along the cursor's path.
1737 STATIC int /* error */
1739 xfs_btree_cur_t *cur, /* btree cursor */
1740 xfs_alloc_key_t *keyp, /* new key value to update to */
1741 int level) /* starting level for update */
1743 int ptr; /* index of key in block */
1746 * Go up the tree from this level toward the root.
1747 * At each level, update the key value to the value input.
1748 * Stop when we reach a level where the cursor isn't pointing
1749 * at the first entry in the block.
1751 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1752 xfs_alloc_block_t *block; /* btree block */
1753 xfs_buf_t *bp; /* buffer for block */
1755 int error; /* error return value */
1757 xfs_alloc_key_t *kp; /* ptr to btree block keys */
1759 bp = cur->bc_bufs[level];
1760 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1762 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1765 ptr = cur->bc_ptrs[level];
1766 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
1768 xfs_alloc_log_keys(cur, bp, ptr, ptr);
1774 * Externally visible routines.
1778 * Decrement cursor by one record at the level.
1779 * For nonzero levels the leaf-ward information is untouched.
1782 xfs_alloc_decrement(
1783 xfs_btree_cur_t *cur, /* btree cursor */
1784 int level, /* level in btree, 0 is leaf */
1785 int *stat) /* success/failure */
1787 xfs_alloc_block_t *block; /* btree block */
1788 int error; /* error return value */
1789 int lev; /* btree level */
1791 ASSERT(level < cur->bc_nlevels);
1793 * Read-ahead to the left at this level.
1795 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1797 * Decrement the ptr at this level. If we're still in the block
1800 if (--cur->bc_ptrs[level] > 0) {
1805 * Get a pointer to the btree block.
1807 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[level]);
1809 if ((error = xfs_btree_check_sblock(cur, block, level,
1810 cur->bc_bufs[level])))
1814 * If we just went off the left edge of the tree, return failure.
1816 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1821 * March up the tree decrementing pointers.
1822 * Stop when we don't go off the left edge of a block.
1824 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1825 if (--cur->bc_ptrs[lev] > 0)
1828 * Read-ahead the left block, we're going to read it
1831 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1834 * If we went off the root then we are seriously confused.
1836 ASSERT(lev < cur->bc_nlevels);
1838 * Now walk back down the tree, fixing up the cursor's buffer
1839 * pointers and key numbers.
1841 for (block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1842 xfs_agblock_t agbno; /* block number of btree block */
1843 xfs_buf_t *bp; /* buffer pointer for block */
1845 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1846 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1847 cur->bc_private.a.agno, agbno, 0, &bp,
1848 XFS_ALLOC_BTREE_REF)))
1851 xfs_btree_setbuf(cur, lev, bp);
1852 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1853 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1855 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1862 * Delete the record pointed to by cur.
1863 * The cursor refers to the place where the record was (could be inserted)
1864 * when the operation returns.
1868 xfs_btree_cur_t *cur, /* btree cursor */
1869 int *stat) /* success/failure */
1871 int error; /* error return value */
1872 int i; /* result code */
1873 int level; /* btree level */
1876 * Go up the tree, starting at leaf level.
1877 * If 2 is returned then a join was done; go to the next level.
1878 * Otherwise we are done.
1880 for (level = 0, i = 2; i == 2; level++) {
1881 if ((error = xfs_alloc_delrec(cur, level, &i)))
1885 for (level = 1; level < cur->bc_nlevels; level++) {
1886 if (cur->bc_ptrs[level] == 0) {
1887 if ((error = xfs_alloc_decrement(cur, level, &i)))
1898 * Get the data from the pointed-to record.
1902 xfs_btree_cur_t *cur, /* btree cursor */
1903 xfs_agblock_t *bno, /* output: starting block of extent */
1904 xfs_extlen_t *len, /* output: length of extent */
1905 int *stat) /* output: success/failure */
1907 xfs_alloc_block_t *block; /* btree block */
1909 int error; /* error return value */
1911 int ptr; /* record number */
1913 ptr = cur->bc_ptrs[0];
1914 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1916 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1920 * Off the right end or left end, return failure.
1922 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1927 * Point to the record and extract its data.
1930 xfs_alloc_rec_t *rec; /* record data */
1932 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1933 *bno = INT_GET(rec->ar_startblock, ARCH_CONVERT);
1934 *len = INT_GET(rec->ar_blockcount, ARCH_CONVERT);
1941 * Increment cursor by one record at the level.
1942 * For nonzero levels the leaf-ward information is untouched.
1945 xfs_alloc_increment(
1946 xfs_btree_cur_t *cur, /* btree cursor */
1947 int level, /* level in btree, 0 is leaf */
1948 int *stat) /* success/failure */
1950 xfs_alloc_block_t *block; /* btree block */
1951 xfs_buf_t *bp; /* tree block buffer */
1952 int error; /* error return value */
1953 int lev; /* btree level */
1955 ASSERT(level < cur->bc_nlevels);
1957 * Read-ahead to the right at this level.
1959 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1961 * Get a pointer to the btree block.
1963 bp = cur->bc_bufs[level];
1964 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1966 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1970 * Increment the ptr at this level. If we're still in the block
1973 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1978 * If we just went off the right edge of the tree, return failure.
1980 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1985 * March up the tree incrementing pointers.
1986 * Stop when we don't go off the right edge of a block.
1988 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1989 bp = cur->bc_bufs[lev];
1990 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
1992 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1995 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1998 * Read-ahead the right block, we're going to read it
2001 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
2004 * If we went off the root then we are seriously confused.
2006 ASSERT(lev < cur->bc_nlevels);
2008 * Now walk back down the tree, fixing up the cursor's buffer
2009 * pointers and key numbers.
2011 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2013 xfs_agblock_t agbno; /* block number of btree block */
2015 agbno = INT_GET(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
2016 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
2017 cur->bc_private.a.agno, agbno, 0, &bp,
2018 XFS_ALLOC_BTREE_REF)))
2021 xfs_btree_setbuf(cur, lev, bp);
2022 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
2023 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
2025 cur->bc_ptrs[lev] = 1;
2032 * Insert the current record at the point referenced by cur.
2033 * The cursor may be inconsistent on return if splits have been done.
2037 xfs_btree_cur_t *cur, /* btree cursor */
2038 int *stat) /* success/failure */
2040 int error; /* error return value */
2041 int i; /* result value, 0 for failure */
2042 int level; /* current level number in btree */
2043 xfs_agblock_t nbno; /* new block number (split result) */
2044 xfs_btree_cur_t *ncur; /* new cursor (split result) */
2045 xfs_alloc_rec_t nrec; /* record being inserted this level */
2046 xfs_btree_cur_t *pcur; /* previous level's cursor */
2050 INT_SET(nrec.ar_startblock, ARCH_CONVERT, cur->bc_rec.a.ar_startblock);
2051 INT_SET(nrec.ar_blockcount, ARCH_CONVERT, cur->bc_rec.a.ar_blockcount);
2052 ncur = (xfs_btree_cur_t *)0;
2055 * Loop going up the tree, starting at the leaf level.
2056 * Stop when we don't get a split block, that must mean that
2057 * the insert is finished with this level.
2061 * Insert nrec/nbno into this level of the tree.
2062 * Note if we fail, nbno will be null.
2064 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
2067 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
2071 * See if the cursor we just used is trash.
2072 * Can't trash the caller's cursor, but otherwise we should
2073 * if ncur is a new cursor or we're about to be done.
2075 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
2076 cur->bc_nlevels = pcur->bc_nlevels;
2077 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
2080 * If we got a new cursor, switch to it.
2084 ncur = (xfs_btree_cur_t *)0;
2086 } while (nbno != NULLAGBLOCK);
2092 * Lookup the record equal to [bno, len] in the btree given by cur.
2095 xfs_alloc_lookup_eq(
2096 xfs_btree_cur_t *cur, /* btree cursor */
2097 xfs_agblock_t bno, /* starting block of extent */
2098 xfs_extlen_t len, /* length of extent */
2099 int *stat) /* success/failure */
2101 cur->bc_rec.a.ar_startblock = bno;
2102 cur->bc_rec.a.ar_blockcount = len;
2103 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, stat);
2107 * Lookup the first record greater than or equal to [bno, len]
2108 * in the btree given by cur.
2111 xfs_alloc_lookup_ge(
2112 xfs_btree_cur_t *cur, /* btree cursor */
2113 xfs_agblock_t bno, /* starting block of extent */
2114 xfs_extlen_t len, /* length of extent */
2115 int *stat) /* success/failure */
2117 cur->bc_rec.a.ar_startblock = bno;
2118 cur->bc_rec.a.ar_blockcount = len;
2119 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, stat);
2123 * Lookup the first record less than or equal to [bno, len]
2124 * in the btree given by cur.
2127 xfs_alloc_lookup_le(
2128 xfs_btree_cur_t *cur, /* btree cursor */
2129 xfs_agblock_t bno, /* starting block of extent */
2130 xfs_extlen_t len, /* length of extent */
2131 int *stat) /* success/failure */
2133 cur->bc_rec.a.ar_startblock = bno;
2134 cur->bc_rec.a.ar_blockcount = len;
2135 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, stat);
2139 * Update the record referred to by cur, to the value given by [bno, len].
2140 * This either works (return 0) or gets an EFSCORRUPTED error.
2144 xfs_btree_cur_t *cur, /* btree cursor */
2145 xfs_agblock_t bno, /* starting block of extent */
2146 xfs_extlen_t len) /* length of extent */
2148 xfs_alloc_block_t *block; /* btree block to update */
2149 int error; /* error return value */
2150 int ptr; /* current record number (updating) */
2154 * Pick up the a.g. freelist struct and the current block.
2156 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
2158 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
2162 * Get the address of the rec to be updated.
2164 ptr = cur->bc_ptrs[0];
2166 xfs_alloc_rec_t *rp; /* pointer to updated record */
2168 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
2170 * Fill in the new contents and log them.
2172 INT_SET(rp->ar_startblock, ARCH_CONVERT, bno);
2173 INT_SET(rp->ar_blockcount, ARCH_CONVERT, len);
2174 xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
2177 * If it's the by-size btree and it's the last leaf block and
2178 * it's the last record... then update the size of the longest
2179 * extent in the a.g., which we cache in the a.g. freelist header.
2181 if (cur->bc_btnum == XFS_BTNUM_CNT &&
2182 INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK &&
2183 ptr == INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
2184 xfs_agf_t *agf; /* a.g. freespace header */
2185 xfs_agnumber_t seqno;
2187 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
2188 seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
2189 cur->bc_mp->m_perag[seqno].pagf_longest = len;
2190 INT_SET(agf->agf_longest, ARCH_CONVERT, len);
2191 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
2195 * Updating first record in leaf. Pass new key value up to our parent.
2198 xfs_alloc_key_t key; /* key containing [bno, len] */
2200 INT_SET(key.ar_startblock, ARCH_CONVERT, bno);
2201 INT_SET(key.ar_blockcount, ARCH_CONVERT, len);
2202 if ((error = xfs_alloc_updkey(cur, &key, 1)))