2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_error.h"
43 * Prototypes for internal functions.
46 STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
47 STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
48 STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
49 STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
50 STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
51 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
52 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
53 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
60 * Single level of the xfs_alloc_delete record deletion routine.
61 * Delete record pointed to by cur/level.
62 * Remove the record from its block then rebalance the tree.
63 * Return 0 for error, 1 for done, 2 to go on to the next level.
65 STATIC int /* error */
67 xfs_btree_cur_t *cur, /* btree cursor */
68 int level, /* level removing record from */
69 int *stat) /* fail/done/go-on */
71 xfs_agf_t *agf; /* allocation group freelist header */
72 xfs_alloc_block_t *block; /* btree block record/key lives in */
73 xfs_agblock_t bno; /* btree block number */
74 xfs_buf_t *bp; /* buffer for block */
75 int error; /* error return value */
76 int i; /* loop index */
77 xfs_alloc_key_t key; /* kp points here if block is level 0 */
78 xfs_agblock_t lbno; /* left block's block number */
79 xfs_buf_t *lbp; /* left block's buffer pointer */
80 xfs_alloc_block_t *left; /* left btree block */
81 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
82 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
83 int lrecs=0; /* number of records in left block */
84 xfs_alloc_rec_t *lrp; /* left block record pointer */
85 xfs_mount_t *mp; /* mount structure */
86 int ptr; /* index in btree block for this rec */
87 xfs_agblock_t rbno; /* right block's block number */
88 xfs_buf_t *rbp; /* right block's buffer pointer */
89 xfs_alloc_block_t *right; /* right btree block */
90 xfs_alloc_key_t *rkp; /* right block key pointer */
91 xfs_alloc_ptr_t *rpp; /* right block address pointer */
92 int rrecs=0; /* number of records in right block */
94 xfs_alloc_rec_t *rrp; /* right block record pointer */
95 xfs_btree_cur_t *tcur; /* temporary btree cursor */
98 * Get the index of the entry being deleted, check for nothing there.
100 ptr = cur->bc_ptrs[level];
106 * Get the buffer & block containing the record or key/ptr.
108 bp = cur->bc_bufs[level];
109 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
111 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
115 * Fail if we're off the end of the block.
117 numrecs = be16_to_cpu(block->bb_numrecs);
122 XFS_STATS_INC(xs_abt_delrec);
124 * It's a nonleaf. Excise the key and ptr being deleted, by
125 * sliding the entries past them down one.
126 * Log the changed areas of the block.
129 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
130 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
132 for (i = ptr; i < numrecs; i++) {
133 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
138 memmove(&lkp[ptr - 1], &lkp[ptr],
139 (numrecs - ptr) * sizeof(*lkp));
140 memmove(&lpp[ptr - 1], &lpp[ptr],
141 (numrecs - ptr) * sizeof(*lpp));
142 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
143 xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
147 * It's a leaf. Excise the record being deleted, by sliding the
148 * entries past it down one. Log the changed areas of the block.
151 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
153 memmove(&lrp[ptr - 1], &lrp[ptr],
154 (numrecs - ptr) * sizeof(*lrp));
155 xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
158 * If it's the first record in the block, we'll need a key
159 * structure to pass up to the next level (updkey).
162 key.ar_startblock = lrp->ar_startblock;
163 key.ar_blockcount = lrp->ar_blockcount;
168 * Decrement and log the number of entries in the block.
171 block->bb_numrecs = cpu_to_be16(numrecs);
172 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
174 * See if the longest free extent in the allocation group was
175 * changed by this operation. True if it's the by-size btree, and
176 * this is the leaf level, and there is no right sibling block,
177 * and this was the last record.
179 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
183 cur->bc_btnum == XFS_BTNUM_CNT &&
184 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
186 ASSERT(ptr == numrecs + 1);
188 * There are still records in the block. Grab the size
192 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
193 agf->agf_longest = rrp->ar_blockcount;
196 * No free extents left.
199 agf->agf_longest = 0;
200 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
201 be32_to_cpu(agf->agf_longest);
202 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
206 * Is this the root level? If so, we're almost done.
208 if (level == cur->bc_nlevels - 1) {
210 * If this is the root level,
211 * and there's only one entry left,
212 * and it's NOT the leaf level,
213 * then we can get rid of this level.
215 if (numrecs == 1 && level > 0) {
217 * lpp is still set to the first pointer in the block.
218 * Make it the new root of the btree.
220 bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
221 agf->agf_roots[cur->bc_btnum] = *lpp;
222 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
223 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
225 * Put this buffer/block on the ag's freelist.
227 error = xfs_alloc_put_freelist(cur->bc_tp,
228 cur->bc_private.a.agbp, NULL, bno, 1);
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 be32_to_cpu(agf->agf_seqno), 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_btree_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_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
269 * If the number of records remaining in the block is at least
270 * the minimum, we're done.
272 if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
273 if (level > 0 && (error = xfs_btree_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 = be32_to_cpu(block->bb_rightsib);
284 lbno = be32_to_cpu(block->bb_leftsib);
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_btree_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 = be32_to_cpu(right->bb_leftsib);
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 (be16_to_cpu(right->bb_numrecs) - 1 >=
328 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
329 if ((error = xfs_alloc_lshift(tcur, level, &i)))
332 ASSERT(be16_to_cpu(block->bb_numrecs) >=
333 XFS_ALLOC_BLOCK_MINRECS(level, cur));
334 xfs_btree_del_cursor(tcur,
337 (error = xfs_btree_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 = be16_to_cpu(right->bb_numrecs);
350 if (lbno != NULLAGBLOCK) {
351 i = xfs_btree_firstrec(tcur, level);
352 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
353 if ((error = xfs_btree_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_btree_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 = be32_to_cpu(left->bb_rightsib);
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 (be16_to_cpu(left->bb_numrecs) - 1 >=
392 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
393 if ((error = xfs_btree_rshift(tcur, level, &i)))
396 ASSERT(be16_to_cpu(block->bb_numrecs) >=
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 = be16_to_cpu(left->bb_numrecs);
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 + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
426 * Set "right" to be the starting block,
427 * "left" to be the left neighbor.
431 rrecs = be16_to_cpu(right->bb_numrecs);
433 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
434 cur->bc_private.a.agno, lbno, 0, &lbp,
435 XFS_ALLOC_BTREE_REF)))
437 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
438 lrecs = be16_to_cpu(left->bb_numrecs);
439 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
443 * If that won't work, see if we can join with the right neighbor block.
445 else if (rbno != NULLAGBLOCK &&
446 rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
448 * Set "left" to be the starting block,
449 * "right" to be the right neighbor.
453 lrecs = be16_to_cpu(left->bb_numrecs);
455 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
456 cur->bc_private.a.agno, rbno, 0, &rbp,
457 XFS_ALLOC_BTREE_REF)))
459 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
460 rrecs = be16_to_cpu(right->bb_numrecs);
461 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
465 * Otherwise, we can't fix the imbalance.
466 * Just return. This is probably a logic error, but it's not fatal.
469 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
475 * We're now going to join "left" and "right" by moving all the stuff
476 * in "right" to "left" and deleting "right".
480 * It's a non-leaf. Move keys and pointers.
482 lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
483 lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
484 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
485 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
487 for (i = 0; i < rrecs; i++) {
488 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
492 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
493 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
494 xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
495 xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
498 * It's a leaf. Move records.
500 lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
501 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
502 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
503 xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
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] += lrecs;
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_btree_increment(cur, level + 1, &i)))
521 * Fix up the number of records in the surviving block.
524 left->bb_numrecs = cpu_to_be16(lrecs);
526 * Fix up the right block pointer in the surviving block, and log it.
528 left->bb_rightsib = right->bb_rightsib;
529 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
531 * If there is a right sibling now, make it point to the
534 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
535 xfs_alloc_block_t *rrblock;
538 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
539 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
540 &rrbp, XFS_ALLOC_BTREE_REF)))
542 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
543 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
545 rrblock->bb_leftsib = cpu_to_be32(lbno);
546 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
549 * Free the deleting block by putting it on the freelist.
551 error = xfs_alloc_put_freelist(cur->bc_tp,
552 cur->bc_private.a.agbp, NULL, rbno, 1);
556 * Since blocks move to the free list without the coordination
557 * used in xfs_bmap_finish, we can't allow block to be available
558 * for reallocation and non-transaction writing (user data)
559 * until we know that the transaction that moved it to the free
560 * list is permanently on disk. We track the blocks by declaring
561 * these blocks as "busy"; the busy list is maintained on a
562 * per-ag basis and each transaction records which entries
563 * should be removed when the iclog commits to disk. If a
564 * busy block is allocated, the iclog is pushed up to the
565 * LSN that freed the block.
567 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
568 xfs_trans_agbtree_delta(cur->bc_tp, -1);
571 * Adjust the current level's cursor so that we're left referring
572 * to the right node, after we're done.
573 * If this leaves the ptr value 0 our caller will fix it up.
576 cur->bc_ptrs[level]--;
578 * Return value means the next level up has something to do.
584 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
589 * Insert one record/level. Return information to the caller
590 * allowing the next level up to proceed if necessary.
592 STATIC int /* error */
594 xfs_btree_cur_t *cur, /* btree cursor */
595 int level, /* level to insert record at */
596 xfs_agblock_t *bnop, /* i/o: block number inserted */
597 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
598 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
599 int *stat) /* output: success/failure */
601 xfs_agf_t *agf; /* allocation group freelist header */
602 xfs_alloc_block_t *block; /* btree block record/key lives in */
603 xfs_buf_t *bp; /* buffer for block */
604 int error; /* error return value */
605 int i; /* loop index */
606 xfs_alloc_key_t key; /* key value being inserted */
607 xfs_alloc_key_t *kp; /* pointer to btree keys */
608 xfs_agblock_t nbno; /* block number of allocated block */
609 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
610 xfs_alloc_key_t nkey; /* new key value, from split */
611 xfs_alloc_rec_t nrec; /* new record value, for caller */
613 int optr; /* old ptr value */
614 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
615 int ptr; /* index in btree block for this rec */
616 xfs_alloc_rec_t *rp; /* pointer to btree records */
618 ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
621 * GCC doesn't understand the (arguably complex) control flow in
622 * this function and complains about uninitialized structure fields
625 memset(&nrec, 0, sizeof(nrec));
628 * If we made it to the root level, allocate a new root block
631 if (level >= cur->bc_nlevels) {
632 XFS_STATS_INC(xs_abt_insrec);
633 if ((error = xfs_alloc_newroot(cur, &i)))
640 * Make a key out of the record data to be inserted, and save it.
642 key.ar_startblock = recp->ar_startblock;
643 key.ar_blockcount = recp->ar_blockcount;
644 optr = ptr = cur->bc_ptrs[level];
646 * If we're off the left edge, return failure.
652 XFS_STATS_INC(xs_abt_insrec);
654 * Get pointers to the btree buffer and block.
656 bp = cur->bc_bufs[level];
657 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
658 numrecs = be16_to_cpu(block->bb_numrecs);
660 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
663 * Check that the new entry is being inserted in the right place.
665 if (ptr <= numrecs) {
667 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
668 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
670 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
671 xfs_btree_check_key(cur->bc_btnum, &key, kp);
678 * If the block is full, we can't insert the new entry until we
679 * make the block un-full.
681 if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
683 * First, try shifting an entry to the right neighbor.
685 if ((error = xfs_btree_rshift(cur, level, &i)))
691 * Next, try shifting an entry to the left neighbor.
694 if ((error = xfs_alloc_lshift(cur, level, &i)))
697 optr = ptr = cur->bc_ptrs[level];
700 * Next, try splitting the current block in
701 * half. If this works we have to re-set our
702 * variables because we could be in a
703 * different block now.
705 if ((error = xfs_alloc_split(cur, level, &nbno,
709 bp = cur->bc_bufs[level];
710 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
713 xfs_btree_check_sblock(cur,
717 ptr = cur->bc_ptrs[level];
718 nrec.ar_startblock = nkey.ar_startblock;
719 nrec.ar_blockcount = nkey.ar_blockcount;
722 * Otherwise the insert fails.
732 * At this point we know there's room for our new entry in the block
735 numrecs = be16_to_cpu(block->bb_numrecs);
738 * It's a non-leaf entry. Make a hole for the new data
739 * in the key and ptr regions of the block.
741 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
742 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
744 for (i = numrecs; i >= ptr; i--) {
745 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
749 memmove(&kp[ptr], &kp[ptr - 1],
750 (numrecs - ptr + 1) * sizeof(*kp));
751 memmove(&pp[ptr], &pp[ptr - 1],
752 (numrecs - ptr + 1) * sizeof(*pp));
754 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
758 * Now stuff the new data in, bump numrecs and log the new data.
761 pp[ptr - 1] = cpu_to_be32(*bnop);
763 block->bb_numrecs = cpu_to_be16(numrecs);
764 xfs_alloc_log_keys(cur, bp, ptr, numrecs);
765 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
768 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
773 * It's a leaf entry. Make a hole for the new record.
775 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
776 memmove(&rp[ptr], &rp[ptr - 1],
777 (numrecs - ptr + 1) * sizeof(*rp));
779 * Now stuff the new record in, bump numrecs
780 * and log the new data.
784 block->bb_numrecs = cpu_to_be16(numrecs);
785 xfs_alloc_log_recs(cur, bp, ptr, numrecs);
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_btree_updkey(cur, (union xfs_btree_key *)&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 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
810 be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
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 agf->agf_longest = recp->ar_blockcount;
817 cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
818 = be32_to_cpu(recp->ar_blockcount);
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) {
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(be32_to_cpu(p->ar_startblock) +
929 be32_to_cpu(p->ar_blockcount) <=
930 be32_to_cpu(agf->agf_length));
933 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
934 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
935 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
939 * Move 1 record left from cur/level if possible.
940 * Update cur to reflect the new path.
942 STATIC int /* error */
944 xfs_btree_cur_t *cur, /* btree cursor */
945 int level, /* level to shift record on */
946 int *stat) /* success/failure */
948 int error; /* error return value */
950 int i; /* loop index */
952 xfs_alloc_key_t key; /* key value for leaf level upward */
953 xfs_buf_t *lbp; /* buffer for left neighbor block */
954 xfs_alloc_block_t *left; /* left neighbor btree block */
955 int nrec; /* new number of left block entries */
956 xfs_buf_t *rbp; /* buffer for right (current) block */
957 xfs_alloc_block_t *right; /* right (current) btree block */
958 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
959 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
960 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
963 * Set up variables for this block as "right".
965 rbp = cur->bc_bufs[level];
966 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
968 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
972 * If we've got no left sibling then we can't shift an entry left.
974 if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
979 * If the cursor entry is the one that would be moved, don't
980 * do it... it's too complicated.
982 if (cur->bc_ptrs[level] <= 1) {
987 * Set up the left neighbor as "left".
989 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
990 cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
991 0, &lbp, XFS_ALLOC_BTREE_REF)))
993 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
994 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
997 * If it's full, it can't take another entry.
999 if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1003 nrec = be16_to_cpu(left->bb_numrecs) + 1;
1005 * If non-leaf, copy a key and a ptr to the left block.
1008 xfs_alloc_key_t *lkp; /* key pointer for left block */
1009 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1011 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1012 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1014 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1015 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1016 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1018 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
1022 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1023 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1026 * If leaf, copy a record to the left block.
1029 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1031 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1032 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1034 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1035 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1038 * Bump and log left's numrecs, decrement and log right's numrecs.
1040 be16_add_cpu(&left->bb_numrecs, 1);
1041 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1042 be16_add_cpu(&right->bb_numrecs, -1);
1043 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1045 * Slide the contents of right down one entry.
1049 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1050 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
1055 memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1056 memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1057 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1058 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1060 memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1061 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1062 key.ar_startblock = rrp->ar_startblock;
1063 key.ar_blockcount = rrp->ar_blockcount;
1067 * Update the parent key values of right.
1069 if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
1072 * Slide the cursor value left one.
1074 cur->bc_ptrs[level]--;
1080 * Allocate a new root block, fill it in.
1082 STATIC int /* error */
1084 xfs_btree_cur_t *cur, /* btree cursor */
1085 int *stat) /* success/failure */
1087 int error; /* error return value */
1088 xfs_agblock_t lbno; /* left block number */
1089 xfs_buf_t *lbp; /* left btree buffer */
1090 xfs_alloc_block_t *left; /* left btree block */
1091 xfs_mount_t *mp; /* mount structure */
1092 xfs_agblock_t nbno; /* new block number */
1093 xfs_buf_t *nbp; /* new (root) buffer */
1094 xfs_alloc_block_t *new; /* new (root) btree block */
1095 int nptr; /* new value for key index, 1 or 2 */
1096 xfs_agblock_t rbno; /* right block number */
1097 xfs_buf_t *rbp; /* right btree buffer */
1098 xfs_alloc_block_t *right; /* right btree block */
1102 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1104 * Get a buffer from the freelist blocks, for the new root.
1106 error = xfs_alloc_get_freelist(cur->bc_tp,
1107 cur->bc_private.a.agbp, &nbno, 1);
1111 * None available, we fail.
1113 if (nbno == NULLAGBLOCK) {
1117 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1118 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1120 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1122 * Set the root data in the a.g. freespace structure.
1125 xfs_agf_t *agf; /* a.g. freespace header */
1126 xfs_agnumber_t seqno;
1128 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1129 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
1130 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
1131 seqno = be32_to_cpu(agf->agf_seqno);
1132 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1133 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1134 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1137 * At the previous root level there are now two blocks: the old
1138 * root, and the new block generated when it was split.
1139 * We don't know which one the cursor is pointing at, so we
1140 * set up variables "left" and "right" for each case.
1142 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1143 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1145 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1148 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
1150 * Our block is left, pick up the right block.
1152 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1153 rbno = be32_to_cpu(left->bb_rightsib);
1154 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1155 cur->bc_private.a.agno, rbno, 0, &rbp,
1156 XFS_ALLOC_BTREE_REF)))
1158 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1159 if ((error = xfs_btree_check_sblock(cur, right,
1160 cur->bc_nlevels - 1, rbp)))
1165 * Our block is right, pick up the left block.
1169 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1170 lbno = be32_to_cpu(right->bb_leftsib);
1171 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1172 cur->bc_private.a.agno, lbno, 0, &lbp,
1173 XFS_ALLOC_BTREE_REF)))
1175 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1176 if ((error = xfs_btree_check_sblock(cur, left,
1177 cur->bc_nlevels - 1, lbp)))
1182 * Fill in the new block's btree header and log it.
1184 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1185 new->bb_level = cpu_to_be16(cur->bc_nlevels);
1186 new->bb_numrecs = cpu_to_be16(2);
1187 new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1188 new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1189 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1190 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1192 * Fill in the key data in the new root.
1195 xfs_alloc_key_t *kp; /* btree key pointer */
1197 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1198 if (be16_to_cpu(left->bb_level) > 0) {
1199 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
1200 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
1202 xfs_alloc_rec_t *rp; /* btree record pointer */
1204 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1205 kp[0].ar_startblock = rp->ar_startblock;
1206 kp[0].ar_blockcount = rp->ar_blockcount;
1207 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1208 kp[1].ar_startblock = rp->ar_startblock;
1209 kp[1].ar_blockcount = rp->ar_blockcount;
1212 xfs_alloc_log_keys(cur, nbp, 1, 2);
1214 * Fill in the pointer data in the new root.
1217 xfs_alloc_ptr_t *pp; /* btree address pointer */
1219 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1220 pp[0] = cpu_to_be32(lbno);
1221 pp[1] = cpu_to_be32(rbno);
1223 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1225 * Fix up the cursor.
1227 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1228 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1235 * Split cur/level block in half.
1236 * Return new block number and its first record (to be inserted into parent).
1238 STATIC int /* error */
1240 xfs_btree_cur_t *cur, /* btree cursor */
1241 int level, /* level to split */
1242 xfs_agblock_t *bnop, /* output: block number allocated */
1243 xfs_alloc_key_t *keyp, /* output: first key of new block */
1244 xfs_btree_cur_t **curp, /* output: new cursor */
1245 int *stat) /* success/failure */
1247 int error; /* error return value */
1248 int i; /* loop index/record number */
1249 xfs_agblock_t lbno; /* left (current) block number */
1250 xfs_buf_t *lbp; /* buffer for left block */
1251 xfs_alloc_block_t *left; /* left (current) btree block */
1252 xfs_agblock_t rbno; /* right (new) block number */
1253 xfs_buf_t *rbp; /* buffer for right block */
1254 xfs_alloc_block_t *right; /* right (new) btree block */
1257 * Allocate the new block from the freelist.
1258 * If we can't do it, we're toast. Give up.
1260 error = xfs_alloc_get_freelist(cur->bc_tp,
1261 cur->bc_private.a.agbp, &rbno, 1);
1264 if (rbno == NULLAGBLOCK) {
1268 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1269 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1272 * Set up the new block as "right".
1274 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1276 * "Left" is the current (according to the cursor) block.
1278 lbp = cur->bc_bufs[level];
1279 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1281 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1285 * Fill in the btree header for the new block.
1287 right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1288 right->bb_level = left->bb_level;
1289 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1291 * Make sure that if there's an odd number of entries now, that
1292 * each new block will have the same number of entries.
1294 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1295 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1296 be16_add_cpu(&right->bb_numrecs, 1);
1297 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1299 * For non-leaf blocks, copy keys and addresses over to the new block.
1302 xfs_alloc_key_t *lkp; /* left btree key pointer */
1303 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1304 xfs_alloc_key_t *rkp; /* right btree key pointer */
1305 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1307 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1308 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1309 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1310 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1312 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1313 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
1317 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1318 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1319 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1320 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1324 * For leaf blocks, copy records over to the new block.
1327 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1328 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1330 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1331 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1332 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1333 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1334 keyp->ar_startblock = rrp->ar_startblock;
1335 keyp->ar_blockcount = rrp->ar_blockcount;
1338 * Find the left block number by looking in the buffer.
1339 * Adjust numrecs, sibling pointers.
1341 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1342 be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1343 right->bb_rightsib = left->bb_rightsib;
1344 left->bb_rightsib = cpu_to_be32(rbno);
1345 right->bb_leftsib = cpu_to_be32(lbno);
1346 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1347 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1349 * If there's a block to the new block's right, make that block
1350 * point back to right instead of to left.
1352 if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
1353 xfs_alloc_block_t *rrblock; /* rr btree block */
1354 xfs_buf_t *rrbp; /* buffer for rrblock */
1356 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1357 cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
1358 &rrbp, XFS_ALLOC_BTREE_REF)))
1360 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1361 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1363 rrblock->bb_leftsib = cpu_to_be32(rbno);
1364 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1367 * If the cursor is really in the right block, move it there.
1368 * If it's just pointing past the last entry in left, then we'll
1369 * insert there, so don't change anything in that case.
1371 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1372 xfs_btree_setbuf(cur, level, rbp);
1373 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1376 * If there are more levels, we'll need another cursor which refers to
1377 * the right block, no matter where this cursor was.
1379 if (level + 1 < cur->bc_nlevels) {
1380 if ((error = xfs_btree_dup_cursor(cur, curp)))
1382 (*curp)->bc_ptrs[level + 1]++;
1390 * Externally visible routines.
1394 * Delete the record pointed to by cur.
1395 * The cursor refers to the place where the record was (could be inserted)
1396 * when the operation returns.
1400 xfs_btree_cur_t *cur, /* btree cursor */
1401 int *stat) /* success/failure */
1403 int error; /* error return value */
1404 int i; /* result code */
1405 int level; /* btree level */
1408 * Go up the tree, starting at leaf level.
1409 * If 2 is returned then a join was done; go to the next level.
1410 * Otherwise we are done.
1412 for (level = 0, i = 2; i == 2; level++) {
1413 if ((error = xfs_alloc_delrec(cur, level, &i)))
1417 for (level = 1; level < cur->bc_nlevels; level++) {
1418 if (cur->bc_ptrs[level] == 0) {
1419 if ((error = xfs_btree_decrement(cur, level, &i)))
1430 * Get the data from the pointed-to record.
1434 xfs_btree_cur_t *cur, /* btree cursor */
1435 xfs_agblock_t *bno, /* output: starting block of extent */
1436 xfs_extlen_t *len, /* output: length of extent */
1437 int *stat) /* output: success/failure */
1439 xfs_alloc_block_t *block; /* btree block */
1441 int error; /* error return value */
1443 int ptr; /* record number */
1445 ptr = cur->bc_ptrs[0];
1446 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1448 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1452 * Off the right end or left end, return failure.
1454 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
1459 * Point to the record and extract its data.
1462 xfs_alloc_rec_t *rec; /* record data */
1464 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1465 *bno = be32_to_cpu(rec->ar_startblock);
1466 *len = be32_to_cpu(rec->ar_blockcount);
1473 * Insert the current record at the point referenced by cur.
1474 * The cursor may be inconsistent on return if splits have been done.
1478 xfs_btree_cur_t *cur, /* btree cursor */
1479 int *stat) /* success/failure */
1481 int error; /* error return value */
1482 int i; /* result value, 0 for failure */
1483 int level; /* current level number in btree */
1484 xfs_agblock_t nbno; /* new block number (split result) */
1485 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1486 xfs_alloc_rec_t nrec; /* record being inserted this level */
1487 xfs_btree_cur_t *pcur; /* previous level's cursor */
1491 nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
1492 nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
1496 * Loop going up the tree, starting at the leaf level.
1497 * Stop when we don't get a split block, that must mean that
1498 * the insert is finished with this level.
1502 * Insert nrec/nbno into this level of the tree.
1503 * Note if we fail, nbno will be null.
1505 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
1508 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1512 * See if the cursor we just used is trash.
1513 * Can't trash the caller's cursor, but otherwise we should
1514 * if ncur is a new cursor or we're about to be done.
1516 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1517 cur->bc_nlevels = pcur->bc_nlevels;
1518 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1521 * If we got a new cursor, switch to it.
1527 } while (nbno != NULLAGBLOCK);
1532 STATIC struct xfs_btree_cur *
1533 xfs_allocbt_dup_cursor(
1534 struct xfs_btree_cur *cur)
1536 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
1537 cur->bc_private.a.agbp, cur->bc_private.a.agno,
1542 * Update the longest extent in the AGF
1545 xfs_allocbt_update_lastrec(
1546 struct xfs_btree_cur *cur,
1547 struct xfs_btree_block *block,
1548 union xfs_btree_rec *rec,
1552 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1553 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
1556 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1559 case LASTREC_UPDATE:
1561 * If this is the last leaf block and it's the last record,
1562 * then update the size of the longest extent in the AG.
1564 if (ptr != xfs_btree_get_numrecs(block))
1566 len = rec->alloc.ar_blockcount;
1573 agf->agf_longest = len;
1574 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
1575 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
1579 xfs_allocbt_get_maxrecs(
1580 struct xfs_btree_cur *cur,
1583 return cur->bc_mp->m_alloc_mxr[level != 0];
1587 xfs_allocbt_init_key_from_rec(
1588 union xfs_btree_key *key,
1589 union xfs_btree_rec *rec)
1591 ASSERT(rec->alloc.ar_startblock != 0);
1593 key->alloc.ar_startblock = rec->alloc.ar_startblock;
1594 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
1598 xfs_allocbt_init_ptr_from_cur(
1599 struct xfs_btree_cur *cur,
1600 union xfs_btree_ptr *ptr)
1602 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1604 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
1605 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
1607 ptr->s = agf->agf_roots[cur->bc_btnum];
1611 xfs_allocbt_key_diff(
1612 struct xfs_btree_cur *cur,
1613 union xfs_btree_key *key)
1615 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
1616 xfs_alloc_key_t *kp = &key->alloc;
1619 if (cur->bc_btnum == XFS_BTNUM_BNO) {
1620 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
1624 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
1628 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
1631 #ifdef XFS_BTREE_TRACE
1632 ktrace_t *xfs_allocbt_trace_buf;
1635 xfs_allocbt_trace_enter(
1636 struct xfs_btree_cur *cur,
1653 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
1654 (void *)func, (void *)s, NULL, (void *)cur,
1655 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1656 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1657 (void *)a8, (void *)a9, (void *)a10);
1661 xfs_allocbt_trace_cursor(
1662 struct xfs_btree_cur *cur,
1667 *s0 = cur->bc_private.a.agno;
1668 *l0 = cur->bc_rec.a.ar_startblock;
1669 *l1 = cur->bc_rec.a.ar_blockcount;
1673 xfs_allocbt_trace_key(
1674 struct xfs_btree_cur *cur,
1675 union xfs_btree_key *key,
1679 *l0 = be32_to_cpu(key->alloc.ar_startblock);
1680 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
1684 xfs_allocbt_trace_record(
1685 struct xfs_btree_cur *cur,
1686 union xfs_btree_rec *rec,
1691 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
1692 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
1695 #endif /* XFS_BTREE_TRACE */
1697 static const struct xfs_btree_ops xfs_allocbt_ops = {
1698 .rec_len = sizeof(xfs_alloc_rec_t),
1699 .key_len = sizeof(xfs_alloc_key_t),
1701 .dup_cursor = xfs_allocbt_dup_cursor,
1702 .update_lastrec = xfs_allocbt_update_lastrec,
1703 .get_maxrecs = xfs_allocbt_get_maxrecs,
1704 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
1705 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
1706 .key_diff = xfs_allocbt_key_diff,
1708 #ifdef XFS_BTREE_TRACE
1709 .trace_enter = xfs_allocbt_trace_enter,
1710 .trace_cursor = xfs_allocbt_trace_cursor,
1711 .trace_key = xfs_allocbt_trace_key,
1712 .trace_record = xfs_allocbt_trace_record,
1717 * Allocate a new allocation btree cursor.
1719 struct xfs_btree_cur * /* new alloc btree cursor */
1720 xfs_allocbt_init_cursor(
1721 struct xfs_mount *mp, /* file system mount point */
1722 struct xfs_trans *tp, /* transaction pointer */
1723 struct xfs_buf *agbp, /* buffer for agf structure */
1724 xfs_agnumber_t agno, /* allocation group number */
1725 xfs_btnum_t btnum) /* btree identifier */
1727 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
1728 struct xfs_btree_cur *cur;
1730 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
1732 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1736 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1737 cur->bc_btnum = btnum;
1738 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1740 cur->bc_ops = &xfs_allocbt_ops;
1741 if (btnum == XFS_BTNUM_CNT)
1742 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
1744 cur->bc_private.a.agbp = agbp;
1745 cur->bc_private.a.agno = agno;