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_rshift(xfs_btree_cur_t *, int, int *);
53 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
54 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
61 * Single level of the xfs_alloc_delete record deletion routine.
62 * Delete record pointed to by cur/level.
63 * Remove the record from its block then rebalance the tree.
64 * Return 0 for error, 1 for done, 2 to go on to the next level.
66 STATIC int /* error */
68 xfs_btree_cur_t *cur, /* btree cursor */
69 int level, /* level removing record from */
70 int *stat) /* fail/done/go-on */
72 xfs_agf_t *agf; /* allocation group freelist header */
73 xfs_alloc_block_t *block; /* btree block record/key lives in */
74 xfs_agblock_t bno; /* btree block number */
75 xfs_buf_t *bp; /* buffer for block */
76 int error; /* error return value */
77 int i; /* loop index */
78 xfs_alloc_key_t key; /* kp points here if block is level 0 */
79 xfs_agblock_t lbno; /* left block's block number */
80 xfs_buf_t *lbp; /* left block's buffer pointer */
81 xfs_alloc_block_t *left; /* left btree block */
82 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
83 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
84 int lrecs=0; /* number of records in left block */
85 xfs_alloc_rec_t *lrp; /* left block record pointer */
86 xfs_mount_t *mp; /* mount structure */
87 int ptr; /* index in btree block for this rec */
88 xfs_agblock_t rbno; /* right block's block number */
89 xfs_buf_t *rbp; /* right block's buffer pointer */
90 xfs_alloc_block_t *right; /* right btree block */
91 xfs_alloc_key_t *rkp; /* right block key pointer */
92 xfs_alloc_ptr_t *rpp; /* right block address pointer */
93 int rrecs=0; /* number of records in right block */
95 xfs_alloc_rec_t *rrp; /* right block record pointer */
96 xfs_btree_cur_t *tcur; /* temporary btree cursor */
99 * Get the index of the entry being deleted, check for nothing there.
101 ptr = cur->bc_ptrs[level];
107 * Get the buffer & block containing the record or key/ptr.
109 bp = cur->bc_bufs[level];
110 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
112 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
116 * Fail if we're off the end of the block.
118 numrecs = be16_to_cpu(block->bb_numrecs);
123 XFS_STATS_INC(xs_abt_delrec);
125 * It's a nonleaf. Excise the key and ptr being deleted, by
126 * sliding the entries past them down one.
127 * Log the changed areas of the block.
130 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
131 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
133 for (i = ptr; i < numrecs; i++) {
134 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
139 memmove(&lkp[ptr - 1], &lkp[ptr],
140 (numrecs - ptr) * sizeof(*lkp));
141 memmove(&lpp[ptr - 1], &lpp[ptr],
142 (numrecs - ptr) * sizeof(*lpp));
143 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
144 xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
148 * It's a leaf. Excise the record being deleted, by sliding the
149 * entries past it down one. Log the changed areas of the block.
152 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
154 memmove(&lrp[ptr - 1], &lrp[ptr],
155 (numrecs - ptr) * sizeof(*lrp));
156 xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
159 * If it's the first record in the block, we'll need a key
160 * structure to pass up to the next level (updkey).
163 key.ar_startblock = lrp->ar_startblock;
164 key.ar_blockcount = lrp->ar_blockcount;
169 * Decrement and log the number of entries in the block.
172 block->bb_numrecs = cpu_to_be16(numrecs);
173 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
175 * See if the longest free extent in the allocation group was
176 * changed by this operation. True if it's the by-size btree, and
177 * this is the leaf level, and there is no right sibling block,
178 * and this was the last record.
180 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
184 cur->bc_btnum == XFS_BTNUM_CNT &&
185 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
187 ASSERT(ptr == numrecs + 1);
189 * There are still records in the block. Grab the size
193 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
194 agf->agf_longest = rrp->ar_blockcount;
197 * No free extents left.
200 agf->agf_longest = 0;
201 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
202 be32_to_cpu(agf->agf_longest);
203 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
207 * Is this the root level? If so, we're almost done.
209 if (level == cur->bc_nlevels - 1) {
211 * If this is the root level,
212 * and there's only one entry left,
213 * and it's NOT the leaf level,
214 * then we can get rid of this level.
216 if (numrecs == 1 && level > 0) {
218 * lpp is still set to the first pointer in the block.
219 * Make it the new root of the btree.
221 bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
222 agf->agf_roots[cur->bc_btnum] = *lpp;
223 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
224 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
226 * Put this buffer/block on the ag's freelist.
228 error = xfs_alloc_put_freelist(cur->bc_tp,
229 cur->bc_private.a.agbp, NULL, bno, 1);
233 * Since blocks move to the free list without the
234 * coordination used in xfs_bmap_finish, we can't allow
235 * block to be available for reallocation and
236 * non-transaction writing (user data) until we know
237 * that the transaction that moved it to the free list
238 * is permanently on disk. We track the blocks by
239 * declaring these blocks as "busy"; the busy list is
240 * maintained on a per-ag basis and each transaction
241 * records which entries should be removed when the
242 * iclog commits to disk. If a busy block is
243 * allocated, the iclog is pushed up to the LSN
244 * that freed the block.
246 xfs_alloc_mark_busy(cur->bc_tp,
247 be32_to_cpu(agf->agf_seqno), bno, 1);
249 xfs_trans_agbtree_delta(cur->bc_tp, -1);
250 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
251 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
253 * Update the cursor so there's one fewer level.
255 xfs_btree_setbuf(cur, level, NULL);
257 } else if (level > 0 &&
258 (error = xfs_btree_decrement(cur, level, &i)))
264 * If we deleted the leftmost entry in the block, update the
265 * key values above us in the tree.
267 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
270 * If the number of records remaining in the block is at least
271 * the minimum, we're done.
273 if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
274 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
280 * Otherwise, we have to move some records around to keep the
281 * tree balanced. Look at the left and right sibling blocks to
282 * see if we can re-balance by moving only one record.
284 rbno = be32_to_cpu(block->bb_rightsib);
285 lbno = be32_to_cpu(block->bb_leftsib);
287 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
289 * Duplicate the cursor so our btree manipulations here won't
290 * disrupt the next level up.
292 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
295 * If there's a right sibling, see if it's ok to shift an entry
298 if (rbno != NULLAGBLOCK) {
300 * Move the temp cursor to the last entry in the next block.
301 * Actually any entry but the first would suffice.
303 i = xfs_btree_lastrec(tcur, level);
304 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
305 if ((error = xfs_btree_increment(tcur, level, &i)))
307 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
308 i = xfs_btree_lastrec(tcur, level);
309 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
311 * Grab a pointer to the block.
313 rbp = tcur->bc_bufs[level];
314 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
316 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
320 * Grab the current block number, for future use.
322 bno = be32_to_cpu(right->bb_leftsib);
324 * If right block is full enough so that removing one entry
325 * won't make it too empty, and left-shifting an entry out
326 * of right to us works, we're done.
328 if (be16_to_cpu(right->bb_numrecs) - 1 >=
329 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
330 if ((error = xfs_alloc_lshift(tcur, level, &i)))
333 ASSERT(be16_to_cpu(block->bb_numrecs) >=
334 XFS_ALLOC_BLOCK_MINRECS(level, cur));
335 xfs_btree_del_cursor(tcur,
338 (error = xfs_btree_decrement(cur, level,
346 * Otherwise, grab the number of records in right for
347 * future reference, and fix up the temp cursor to point
348 * to our block again (last record).
350 rrecs = be16_to_cpu(right->bb_numrecs);
351 if (lbno != NULLAGBLOCK) {
352 i = xfs_btree_firstrec(tcur, level);
353 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
354 if ((error = xfs_btree_decrement(tcur, level, &i)))
356 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
360 * If there's a left sibling, see if it's ok to shift an entry
363 if (lbno != NULLAGBLOCK) {
365 * Move the temp cursor to the first entry in the
368 i = xfs_btree_firstrec(tcur, level);
369 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
370 if ((error = xfs_btree_decrement(tcur, level, &i)))
372 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
373 xfs_btree_firstrec(tcur, level);
375 * Grab a pointer to the block.
377 lbp = tcur->bc_bufs[level];
378 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
380 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
384 * Grab the current block number, for future use.
386 bno = be32_to_cpu(left->bb_rightsib);
388 * If left block is full enough so that removing one entry
389 * won't make it too empty, and right-shifting an entry out
390 * of left to us works, we're done.
392 if (be16_to_cpu(left->bb_numrecs) - 1 >=
393 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
394 if ((error = xfs_alloc_rshift(tcur, level, &i)))
397 ASSERT(be16_to_cpu(block->bb_numrecs) >=
398 XFS_ALLOC_BLOCK_MINRECS(level, cur));
399 xfs_btree_del_cursor(tcur,
408 * Otherwise, grab the number of records in right for
411 lrecs = be16_to_cpu(left->bb_numrecs);
414 * Delete the temp cursor, we're done with it.
416 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
418 * If here, we need to do a join to keep the tree balanced.
420 ASSERT(bno != NULLAGBLOCK);
422 * See if we can join with the left neighbor block.
424 if (lbno != NULLAGBLOCK &&
425 lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
427 * Set "right" to be the starting block,
428 * "left" to be the left neighbor.
432 rrecs = be16_to_cpu(right->bb_numrecs);
434 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
435 cur->bc_private.a.agno, lbno, 0, &lbp,
436 XFS_ALLOC_BTREE_REF)))
438 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
439 lrecs = be16_to_cpu(left->bb_numrecs);
440 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
444 * If that won't work, see if we can join with the right neighbor block.
446 else if (rbno != NULLAGBLOCK &&
447 rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
449 * Set "left" to be the starting block,
450 * "right" to be the right neighbor.
454 lrecs = be16_to_cpu(left->bb_numrecs);
456 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
457 cur->bc_private.a.agno, rbno, 0, &rbp,
458 XFS_ALLOC_BTREE_REF)))
460 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
461 rrecs = be16_to_cpu(right->bb_numrecs);
462 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
466 * Otherwise, we can't fix the imbalance.
467 * Just return. This is probably a logic error, but it's not fatal.
470 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
476 * We're now going to join "left" and "right" by moving all the stuff
477 * in "right" to "left" and deleting "right".
481 * It's a non-leaf. Move keys and pointers.
483 lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
484 lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
485 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
486 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
488 for (i = 0; i < rrecs; i++) {
489 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
493 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
494 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
495 xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
496 xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
499 * It's a leaf. Move records.
501 lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
502 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
503 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
504 xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
507 * If we joined with the left neighbor, set the buffer in the
508 * cursor to the left block, and fix up the index.
511 xfs_btree_setbuf(cur, level, lbp);
512 cur->bc_ptrs[level] += lrecs;
515 * If we joined with the right neighbor and there's a level above
516 * us, increment the cursor at that level.
518 else if (level + 1 < cur->bc_nlevels &&
519 (error = xfs_btree_increment(cur, level + 1, &i)))
522 * Fix up the number of records in the surviving block.
525 left->bb_numrecs = cpu_to_be16(lrecs);
527 * Fix up the right block pointer in the surviving block, and log it.
529 left->bb_rightsib = right->bb_rightsib;
530 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
532 * If there is a right sibling now, make it point to the
535 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
536 xfs_alloc_block_t *rrblock;
539 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
540 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
541 &rrbp, XFS_ALLOC_BTREE_REF)))
543 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
544 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
546 rrblock->bb_leftsib = cpu_to_be32(lbno);
547 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
550 * Free the deleting block by putting it on the freelist.
552 error = xfs_alloc_put_freelist(cur->bc_tp,
553 cur->bc_private.a.agbp, NULL, rbno, 1);
557 * Since blocks move to the free list without the coordination
558 * used in xfs_bmap_finish, we can't allow block to be available
559 * for reallocation and non-transaction writing (user data)
560 * until we know that the transaction that moved it to the free
561 * list is permanently on disk. We track the blocks by declaring
562 * these blocks as "busy"; the busy list is maintained on a
563 * per-ag basis and each transaction records which entries
564 * should be removed when the iclog commits to disk. If a
565 * busy block is allocated, the iclog is pushed up to the
566 * LSN that freed the block.
568 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
569 xfs_trans_agbtree_delta(cur->bc_tp, -1);
572 * Adjust the current level's cursor so that we're left referring
573 * to the right node, after we're done.
574 * If this leaves the ptr value 0 our caller will fix it up.
577 cur->bc_ptrs[level]--;
579 * Return value means the next level up has something to do.
585 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
590 * Insert one record/level. Return information to the caller
591 * allowing the next level up to proceed if necessary.
593 STATIC int /* error */
595 xfs_btree_cur_t *cur, /* btree cursor */
596 int level, /* level to insert record at */
597 xfs_agblock_t *bnop, /* i/o: block number inserted */
598 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
599 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
600 int *stat) /* output: success/failure */
602 xfs_agf_t *agf; /* allocation group freelist header */
603 xfs_alloc_block_t *block; /* btree block record/key lives in */
604 xfs_buf_t *bp; /* buffer for block */
605 int error; /* error return value */
606 int i; /* loop index */
607 xfs_alloc_key_t key; /* key value being inserted */
608 xfs_alloc_key_t *kp; /* pointer to btree keys */
609 xfs_agblock_t nbno; /* block number of allocated block */
610 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
611 xfs_alloc_key_t nkey; /* new key value, from split */
612 xfs_alloc_rec_t nrec; /* new record value, for caller */
614 int optr; /* old ptr value */
615 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
616 int ptr; /* index in btree block for this rec */
617 xfs_alloc_rec_t *rp; /* pointer to btree records */
619 ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
622 * GCC doesn't understand the (arguably complex) control flow in
623 * this function and complains about uninitialized structure fields
626 memset(&nrec, 0, sizeof(nrec));
629 * If we made it to the root level, allocate a new root block
632 if (level >= cur->bc_nlevels) {
633 XFS_STATS_INC(xs_abt_insrec);
634 if ((error = xfs_alloc_newroot(cur, &i)))
641 * Make a key out of the record data to be inserted, and save it.
643 key.ar_startblock = recp->ar_startblock;
644 key.ar_blockcount = recp->ar_blockcount;
645 optr = ptr = cur->bc_ptrs[level];
647 * If we're off the left edge, return failure.
653 XFS_STATS_INC(xs_abt_insrec);
655 * Get pointers to the btree buffer and block.
657 bp = cur->bc_bufs[level];
658 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
659 numrecs = be16_to_cpu(block->bb_numrecs);
661 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
664 * Check that the new entry is being inserted in the right place.
666 if (ptr <= numrecs) {
668 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
669 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
671 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
672 xfs_btree_check_key(cur->bc_btnum, &key, kp);
679 * If the block is full, we can't insert the new entry until we
680 * make the block un-full.
682 if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
684 * First, try shifting an entry to the right neighbor.
686 if ((error = xfs_alloc_rshift(cur, level, &i)))
692 * Next, try shifting an entry to the left neighbor.
695 if ((error = xfs_alloc_lshift(cur, level, &i)))
698 optr = ptr = cur->bc_ptrs[level];
701 * Next, try splitting the current block in
702 * half. If this works we have to re-set our
703 * variables because we could be in a
704 * different block now.
706 if ((error = xfs_alloc_split(cur, level, &nbno,
710 bp = cur->bc_bufs[level];
711 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
714 xfs_btree_check_sblock(cur,
718 ptr = cur->bc_ptrs[level];
719 nrec.ar_startblock = nkey.ar_startblock;
720 nrec.ar_blockcount = nkey.ar_blockcount;
723 * Otherwise the insert fails.
733 * At this point we know there's room for our new entry in the block
736 numrecs = be16_to_cpu(block->bb_numrecs);
739 * It's a non-leaf entry. Make a hole for the new data
740 * in the key and ptr regions of the block.
742 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
743 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
745 for (i = numrecs; i >= ptr; i--) {
746 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
750 memmove(&kp[ptr], &kp[ptr - 1],
751 (numrecs - ptr + 1) * sizeof(*kp));
752 memmove(&pp[ptr], &pp[ptr - 1],
753 (numrecs - ptr + 1) * sizeof(*pp));
755 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
759 * Now stuff the new data in, bump numrecs and log the new data.
762 pp[ptr - 1] = cpu_to_be32(*bnop);
764 block->bb_numrecs = cpu_to_be16(numrecs);
765 xfs_alloc_log_keys(cur, bp, ptr, numrecs);
766 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
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 (numrecs - ptr + 1) * sizeof(*rp));
780 * Now stuff the new record in, bump numrecs
781 * and log the new data.
785 block->bb_numrecs = cpu_to_be16(numrecs);
786 xfs_alloc_log_recs(cur, bp, ptr, numrecs);
789 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
794 * Log the new number of records in the btree header.
796 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
798 * If we inserted at the start of a block, update the parents' keys.
800 if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
803 * Look to see if the longest extent in the allocation group
804 * needs to be updated.
807 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
809 cur->bc_btnum == XFS_BTNUM_CNT &&
810 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
811 be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
813 * If this is a leaf in the by-size btree and there
814 * is no right sibling block and this block is bigger
815 * than the previous longest block, update it.
817 agf->agf_longest = recp->ar_blockcount;
818 cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
819 = be32_to_cpu(recp->ar_blockcount);
820 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
824 * Return the new block number, if any.
825 * If there is one, give back a record value and a cursor too.
828 if (nbno != NULLAGBLOCK) {
837 * Log header fields from a btree block.
841 xfs_trans_t *tp, /* transaction pointer */
842 xfs_buf_t *bp, /* buffer containing btree block */
843 int fields) /* mask of fields: XFS_BB_... */
845 int first; /* first byte offset logged */
846 int last; /* last byte offset logged */
847 static const short offsets[] = { /* table of offsets */
848 offsetof(xfs_alloc_block_t, bb_magic),
849 offsetof(xfs_alloc_block_t, bb_level),
850 offsetof(xfs_alloc_block_t, bb_numrecs),
851 offsetof(xfs_alloc_block_t, bb_leftsib),
852 offsetof(xfs_alloc_block_t, bb_rightsib),
853 sizeof(xfs_alloc_block_t)
856 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
857 xfs_trans_log_buf(tp, bp, first, last);
861 * Log keys from a btree block (nonleaf).
865 xfs_btree_cur_t *cur, /* btree cursor */
866 xfs_buf_t *bp, /* buffer containing btree block */
867 int kfirst, /* index of first key to log */
868 int klast) /* index of last key to log */
870 xfs_alloc_block_t *block; /* btree block to log from */
871 int first; /* first byte offset logged */
872 xfs_alloc_key_t *kp; /* key pointer in btree block */
873 int last; /* last byte offset logged */
875 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
876 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
877 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
878 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
879 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
883 * Log block pointer fields from a btree block (nonleaf).
887 xfs_btree_cur_t *cur, /* btree cursor */
888 xfs_buf_t *bp, /* buffer containing btree block */
889 int pfirst, /* index of first pointer to log */
890 int plast) /* index of last pointer to log */
892 xfs_alloc_block_t *block; /* btree block to log from */
893 int first; /* first byte offset logged */
894 int last; /* last byte offset logged */
895 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
897 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
898 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
899 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
900 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
901 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
905 * Log records from a btree block (leaf).
909 xfs_btree_cur_t *cur, /* btree cursor */
910 xfs_buf_t *bp, /* buffer containing btree block */
911 int rfirst, /* index of first record to log */
912 int rlast) /* index of last record to log */
914 xfs_alloc_block_t *block; /* btree block to log from */
915 int first; /* first byte offset logged */
916 int last; /* last byte offset logged */
917 xfs_alloc_rec_t *rp; /* record pointer for btree block */
920 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
921 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
927 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
928 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
929 ASSERT(be32_to_cpu(p->ar_startblock) +
930 be32_to_cpu(p->ar_blockcount) <=
931 be32_to_cpu(agf->agf_length));
934 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
935 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
936 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
940 * Move 1 record left from cur/level if possible.
941 * Update cur to reflect the new path.
943 STATIC int /* error */
945 xfs_btree_cur_t *cur, /* btree cursor */
946 int level, /* level to shift record on */
947 int *stat) /* success/failure */
949 int error; /* error return value */
951 int i; /* loop index */
953 xfs_alloc_key_t key; /* key value for leaf level upward */
954 xfs_buf_t *lbp; /* buffer for left neighbor block */
955 xfs_alloc_block_t *left; /* left neighbor btree block */
956 int nrec; /* new number of left block entries */
957 xfs_buf_t *rbp; /* buffer for right (current) block */
958 xfs_alloc_block_t *right; /* right (current) btree block */
959 xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
960 xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
961 xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
964 * Set up variables for this block as "right".
966 rbp = cur->bc_bufs[level];
967 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
969 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
973 * If we've got no left sibling then we can't shift an entry left.
975 if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
980 * If the cursor entry is the one that would be moved, don't
981 * do it... it's too complicated.
983 if (cur->bc_ptrs[level] <= 1) {
988 * Set up the left neighbor as "left".
990 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
991 cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
992 0, &lbp, XFS_ALLOC_BTREE_REF)))
994 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
995 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
998 * If it's full, it can't take another entry.
1000 if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1004 nrec = be16_to_cpu(left->bb_numrecs) + 1;
1006 * If non-leaf, copy a key and a ptr to the left block.
1009 xfs_alloc_key_t *lkp; /* key pointer for left block */
1010 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1012 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1013 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1015 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1016 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1017 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1019 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
1023 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1024 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1027 * If leaf, copy a record to the left block.
1030 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1032 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1033 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1035 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1036 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1039 * Bump and log left's numrecs, decrement and log right's numrecs.
1041 be16_add_cpu(&left->bb_numrecs, 1);
1042 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1043 be16_add_cpu(&right->bb_numrecs, -1);
1044 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1046 * Slide the contents of right down one entry.
1050 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1051 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
1056 memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1057 memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1058 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1059 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1061 memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1062 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1063 key.ar_startblock = rrp->ar_startblock;
1064 key.ar_blockcount = rrp->ar_blockcount;
1068 * Update the parent key values of right.
1070 if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
1073 * Slide the cursor value left one.
1075 cur->bc_ptrs[level]--;
1081 * Allocate a new root block, fill it in.
1083 STATIC int /* error */
1085 xfs_btree_cur_t *cur, /* btree cursor */
1086 int *stat) /* success/failure */
1088 int error; /* error return value */
1089 xfs_agblock_t lbno; /* left block number */
1090 xfs_buf_t *lbp; /* left btree buffer */
1091 xfs_alloc_block_t *left; /* left btree block */
1092 xfs_mount_t *mp; /* mount structure */
1093 xfs_agblock_t nbno; /* new block number */
1094 xfs_buf_t *nbp; /* new (root) buffer */
1095 xfs_alloc_block_t *new; /* new (root) btree block */
1096 int nptr; /* new value for key index, 1 or 2 */
1097 xfs_agblock_t rbno; /* right block number */
1098 xfs_buf_t *rbp; /* right btree buffer */
1099 xfs_alloc_block_t *right; /* right btree block */
1103 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1105 * Get a buffer from the freelist blocks, for the new root.
1107 error = xfs_alloc_get_freelist(cur->bc_tp,
1108 cur->bc_private.a.agbp, &nbno, 1);
1112 * None available, we fail.
1114 if (nbno == NULLAGBLOCK) {
1118 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1119 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1121 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1123 * Set the root data in the a.g. freespace structure.
1126 xfs_agf_t *agf; /* a.g. freespace header */
1127 xfs_agnumber_t seqno;
1129 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1130 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
1131 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
1132 seqno = be32_to_cpu(agf->agf_seqno);
1133 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1134 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1135 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1138 * At the previous root level there are now two blocks: the old
1139 * root, and the new block generated when it was split.
1140 * We don't know which one the cursor is pointing at, so we
1141 * set up variables "left" and "right" for each case.
1143 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1144 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1146 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1149 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
1151 * Our block is left, pick up the right block.
1153 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1154 rbno = be32_to_cpu(left->bb_rightsib);
1155 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1156 cur->bc_private.a.agno, rbno, 0, &rbp,
1157 XFS_ALLOC_BTREE_REF)))
1159 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1160 if ((error = xfs_btree_check_sblock(cur, right,
1161 cur->bc_nlevels - 1, rbp)))
1166 * Our block is right, pick up the left block.
1170 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1171 lbno = be32_to_cpu(right->bb_leftsib);
1172 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1173 cur->bc_private.a.agno, lbno, 0, &lbp,
1174 XFS_ALLOC_BTREE_REF)))
1176 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1177 if ((error = xfs_btree_check_sblock(cur, left,
1178 cur->bc_nlevels - 1, lbp)))
1183 * Fill in the new block's btree header and log it.
1185 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1186 new->bb_level = cpu_to_be16(cur->bc_nlevels);
1187 new->bb_numrecs = cpu_to_be16(2);
1188 new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1189 new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1190 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1191 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1193 * Fill in the key data in the new root.
1196 xfs_alloc_key_t *kp; /* btree key pointer */
1198 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1199 if (be16_to_cpu(left->bb_level) > 0) {
1200 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
1201 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
1203 xfs_alloc_rec_t *rp; /* btree record pointer */
1205 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1206 kp[0].ar_startblock = rp->ar_startblock;
1207 kp[0].ar_blockcount = rp->ar_blockcount;
1208 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1209 kp[1].ar_startblock = rp->ar_startblock;
1210 kp[1].ar_blockcount = rp->ar_blockcount;
1213 xfs_alloc_log_keys(cur, nbp, 1, 2);
1215 * Fill in the pointer data in the new root.
1218 xfs_alloc_ptr_t *pp; /* btree address pointer */
1220 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1221 pp[0] = cpu_to_be32(lbno);
1222 pp[1] = cpu_to_be32(rbno);
1224 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1226 * Fix up the cursor.
1228 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1229 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1236 * Move 1 record right from cur/level if possible.
1237 * Update cur to reflect the new path.
1239 STATIC int /* error */
1241 xfs_btree_cur_t *cur, /* btree cursor */
1242 int level, /* level to shift record on */
1243 int *stat) /* success/failure */
1245 int error; /* error return value */
1246 int i; /* loop index */
1247 xfs_alloc_key_t key; /* key value for leaf level upward */
1248 xfs_buf_t *lbp; /* buffer for left (current) block */
1249 xfs_alloc_block_t *left; /* left (current) btree block */
1250 xfs_buf_t *rbp; /* buffer for right neighbor block */
1251 xfs_alloc_block_t *right; /* right neighbor btree block */
1252 xfs_alloc_key_t *rkp; /* key pointer for right block */
1253 xfs_btree_cur_t *tcur; /* temporary cursor */
1256 * Set up variables for this block as "left".
1258 lbp = cur->bc_bufs[level];
1259 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1261 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1265 * If we've got no right sibling then we can't shift an entry right.
1267 if (be32_to_cpu(left->bb_rightsib) == NULLAGBLOCK) {
1272 * If the cursor entry is the one that would be moved, don't
1273 * do it... it's too complicated.
1275 if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
1280 * Set up the right neighbor as "right".
1282 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1283 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib),
1284 0, &rbp, XFS_ALLOC_BTREE_REF)))
1286 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1287 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1290 * If it's full, it can't take another entry.
1292 if (be16_to_cpu(right->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1297 * Make a hole at the start of the right neighbor block, then
1298 * copy the last left block entry to the hole.
1301 xfs_alloc_key_t *lkp; /* key pointer for left block */
1302 xfs_alloc_ptr_t *lpp; /* address pointer for left block */
1303 xfs_alloc_ptr_t *rpp; /* address pointer for right block */
1305 lkp = XFS_ALLOC_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1306 lpp = XFS_ALLOC_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1307 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1308 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1310 for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
1311 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
1315 memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1316 memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1318 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*lpp), level)))
1323 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1324 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1325 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1327 xfs_alloc_rec_t *lrp; /* record pointer for left block */
1328 xfs_alloc_rec_t *rrp; /* record pointer for right block */
1330 lrp = XFS_ALLOC_REC_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
1331 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1332 memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1334 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
1335 key.ar_startblock = rrp->ar_startblock;
1336 key.ar_blockcount = rrp->ar_blockcount;
1338 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1341 * Decrement and log left's numrecs, bump and log right's numrecs.
1343 be16_add_cpu(&left->bb_numrecs, -1);
1344 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1345 be16_add_cpu(&right->bb_numrecs, 1);
1346 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1348 * Using a temporary cursor, update the parent key values of the
1349 * block on the right.
1351 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1353 i = xfs_btree_lastrec(tcur, level);
1354 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1355 if ((error = xfs_btree_increment(tcur, level, &i)) ||
1356 (error = xfs_btree_updkey(tcur, (union xfs_btree_key *)rkp, level + 1)))
1358 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1362 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1367 * Split cur/level block in half.
1368 * Return new block number and its first record (to be inserted into parent).
1370 STATIC int /* error */
1372 xfs_btree_cur_t *cur, /* btree cursor */
1373 int level, /* level to split */
1374 xfs_agblock_t *bnop, /* output: block number allocated */
1375 xfs_alloc_key_t *keyp, /* output: first key of new block */
1376 xfs_btree_cur_t **curp, /* output: new cursor */
1377 int *stat) /* success/failure */
1379 int error; /* error return value */
1380 int i; /* loop index/record number */
1381 xfs_agblock_t lbno; /* left (current) block number */
1382 xfs_buf_t *lbp; /* buffer for left block */
1383 xfs_alloc_block_t *left; /* left (current) btree block */
1384 xfs_agblock_t rbno; /* right (new) block number */
1385 xfs_buf_t *rbp; /* buffer for right block */
1386 xfs_alloc_block_t *right; /* right (new) btree block */
1389 * Allocate the new block from the freelist.
1390 * If we can't do it, we're toast. Give up.
1392 error = xfs_alloc_get_freelist(cur->bc_tp,
1393 cur->bc_private.a.agbp, &rbno, 1);
1396 if (rbno == NULLAGBLOCK) {
1400 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1401 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1404 * Set up the new block as "right".
1406 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1408 * "Left" is the current (according to the cursor) block.
1410 lbp = cur->bc_bufs[level];
1411 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1413 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1417 * Fill in the btree header for the new block.
1419 right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1420 right->bb_level = left->bb_level;
1421 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1423 * Make sure that if there's an odd number of entries now, that
1424 * each new block will have the same number of entries.
1426 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1427 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1428 be16_add_cpu(&right->bb_numrecs, 1);
1429 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1431 * For non-leaf blocks, copy keys and addresses over to the new block.
1434 xfs_alloc_key_t *lkp; /* left btree key pointer */
1435 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1436 xfs_alloc_key_t *rkp; /* right btree key pointer */
1437 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1439 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1440 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1441 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1442 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1444 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1445 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
1449 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1450 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1451 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1452 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1456 * For leaf blocks, copy records over to the new block.
1459 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1460 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1462 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1463 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1464 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1465 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1466 keyp->ar_startblock = rrp->ar_startblock;
1467 keyp->ar_blockcount = rrp->ar_blockcount;
1470 * Find the left block number by looking in the buffer.
1471 * Adjust numrecs, sibling pointers.
1473 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1474 be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1475 right->bb_rightsib = left->bb_rightsib;
1476 left->bb_rightsib = cpu_to_be32(rbno);
1477 right->bb_leftsib = cpu_to_be32(lbno);
1478 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1479 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1481 * If there's a block to the new block's right, make that block
1482 * point back to right instead of to left.
1484 if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
1485 xfs_alloc_block_t *rrblock; /* rr btree block */
1486 xfs_buf_t *rrbp; /* buffer for rrblock */
1488 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1489 cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
1490 &rrbp, XFS_ALLOC_BTREE_REF)))
1492 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1493 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1495 rrblock->bb_leftsib = cpu_to_be32(rbno);
1496 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1499 * If the cursor is really in the right block, move it there.
1500 * If it's just pointing past the last entry in left, then we'll
1501 * insert there, so don't change anything in that case.
1503 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1504 xfs_btree_setbuf(cur, level, rbp);
1505 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1508 * If there are more levels, we'll need another cursor which refers to
1509 * the right block, no matter where this cursor was.
1511 if (level + 1 < cur->bc_nlevels) {
1512 if ((error = xfs_btree_dup_cursor(cur, curp)))
1514 (*curp)->bc_ptrs[level + 1]++;
1522 * Externally visible routines.
1526 * Delete the record pointed to by cur.
1527 * The cursor refers to the place where the record was (could be inserted)
1528 * when the operation returns.
1532 xfs_btree_cur_t *cur, /* btree cursor */
1533 int *stat) /* success/failure */
1535 int error; /* error return value */
1536 int i; /* result code */
1537 int level; /* btree level */
1540 * Go up the tree, starting at leaf level.
1541 * If 2 is returned then a join was done; go to the next level.
1542 * Otherwise we are done.
1544 for (level = 0, i = 2; i == 2; level++) {
1545 if ((error = xfs_alloc_delrec(cur, level, &i)))
1549 for (level = 1; level < cur->bc_nlevels; level++) {
1550 if (cur->bc_ptrs[level] == 0) {
1551 if ((error = xfs_btree_decrement(cur, level, &i)))
1562 * Get the data from the pointed-to record.
1566 xfs_btree_cur_t *cur, /* btree cursor */
1567 xfs_agblock_t *bno, /* output: starting block of extent */
1568 xfs_extlen_t *len, /* output: length of extent */
1569 int *stat) /* output: success/failure */
1571 xfs_alloc_block_t *block; /* btree block */
1573 int error; /* error return value */
1575 int ptr; /* record number */
1577 ptr = cur->bc_ptrs[0];
1578 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1580 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1584 * Off the right end or left end, return failure.
1586 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
1591 * Point to the record and extract its data.
1594 xfs_alloc_rec_t *rec; /* record data */
1596 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1597 *bno = be32_to_cpu(rec->ar_startblock);
1598 *len = be32_to_cpu(rec->ar_blockcount);
1605 * Insert the current record at the point referenced by cur.
1606 * The cursor may be inconsistent on return if splits have been done.
1610 xfs_btree_cur_t *cur, /* btree cursor */
1611 int *stat) /* success/failure */
1613 int error; /* error return value */
1614 int i; /* result value, 0 for failure */
1615 int level; /* current level number in btree */
1616 xfs_agblock_t nbno; /* new block number (split result) */
1617 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1618 xfs_alloc_rec_t nrec; /* record being inserted this level */
1619 xfs_btree_cur_t *pcur; /* previous level's cursor */
1623 nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
1624 nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
1628 * Loop going up the tree, starting at the leaf level.
1629 * Stop when we don't get a split block, that must mean that
1630 * the insert is finished with this level.
1634 * Insert nrec/nbno into this level of the tree.
1635 * Note if we fail, nbno will be null.
1637 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
1640 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1644 * See if the cursor we just used is trash.
1645 * Can't trash the caller's cursor, but otherwise we should
1646 * if ncur is a new cursor or we're about to be done.
1648 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1649 cur->bc_nlevels = pcur->bc_nlevels;
1650 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1653 * If we got a new cursor, switch to it.
1659 } while (nbno != NULLAGBLOCK);
1664 STATIC struct xfs_btree_cur *
1665 xfs_allocbt_dup_cursor(
1666 struct xfs_btree_cur *cur)
1668 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
1669 cur->bc_private.a.agbp, cur->bc_private.a.agno,
1674 * Update the longest extent in the AGF
1677 xfs_allocbt_update_lastrec(
1678 struct xfs_btree_cur *cur,
1679 struct xfs_btree_block *block,
1680 union xfs_btree_rec *rec,
1684 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1685 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
1688 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1691 case LASTREC_UPDATE:
1693 * If this is the last leaf block and it's the last record,
1694 * then update the size of the longest extent in the AG.
1696 if (ptr != xfs_btree_get_numrecs(block))
1698 len = rec->alloc.ar_blockcount;
1705 agf->agf_longest = len;
1706 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
1707 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
1711 xfs_allocbt_get_maxrecs(
1712 struct xfs_btree_cur *cur,
1715 return cur->bc_mp->m_alloc_mxr[level != 0];
1719 xfs_allocbt_init_key_from_rec(
1720 union xfs_btree_key *key,
1721 union xfs_btree_rec *rec)
1723 ASSERT(rec->alloc.ar_startblock != 0);
1725 key->alloc.ar_startblock = rec->alloc.ar_startblock;
1726 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
1730 xfs_allocbt_init_ptr_from_cur(
1731 struct xfs_btree_cur *cur,
1732 union xfs_btree_ptr *ptr)
1734 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1736 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
1737 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
1739 ptr->s = agf->agf_roots[cur->bc_btnum];
1743 xfs_allocbt_key_diff(
1744 struct xfs_btree_cur *cur,
1745 union xfs_btree_key *key)
1747 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
1748 xfs_alloc_key_t *kp = &key->alloc;
1751 if (cur->bc_btnum == XFS_BTNUM_BNO) {
1752 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
1756 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
1760 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
1763 #ifdef XFS_BTREE_TRACE
1764 ktrace_t *xfs_allocbt_trace_buf;
1767 xfs_allocbt_trace_enter(
1768 struct xfs_btree_cur *cur,
1785 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
1786 (void *)func, (void *)s, NULL, (void *)cur,
1787 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1788 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1789 (void *)a8, (void *)a9, (void *)a10);
1793 xfs_allocbt_trace_cursor(
1794 struct xfs_btree_cur *cur,
1799 *s0 = cur->bc_private.a.agno;
1800 *l0 = cur->bc_rec.a.ar_startblock;
1801 *l1 = cur->bc_rec.a.ar_blockcount;
1805 xfs_allocbt_trace_key(
1806 struct xfs_btree_cur *cur,
1807 union xfs_btree_key *key,
1811 *l0 = be32_to_cpu(key->alloc.ar_startblock);
1812 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
1816 xfs_allocbt_trace_record(
1817 struct xfs_btree_cur *cur,
1818 union xfs_btree_rec *rec,
1823 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
1824 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
1827 #endif /* XFS_BTREE_TRACE */
1829 static const struct xfs_btree_ops xfs_allocbt_ops = {
1830 .rec_len = sizeof(xfs_alloc_rec_t),
1831 .key_len = sizeof(xfs_alloc_key_t),
1833 .dup_cursor = xfs_allocbt_dup_cursor,
1834 .update_lastrec = xfs_allocbt_update_lastrec,
1835 .get_maxrecs = xfs_allocbt_get_maxrecs,
1836 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
1837 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
1838 .key_diff = xfs_allocbt_key_diff,
1840 #ifdef XFS_BTREE_TRACE
1841 .trace_enter = xfs_allocbt_trace_enter,
1842 .trace_cursor = xfs_allocbt_trace_cursor,
1843 .trace_key = xfs_allocbt_trace_key,
1844 .trace_record = xfs_allocbt_trace_record,
1849 * Allocate a new allocation btree cursor.
1851 struct xfs_btree_cur * /* new alloc btree cursor */
1852 xfs_allocbt_init_cursor(
1853 struct xfs_mount *mp, /* file system mount point */
1854 struct xfs_trans *tp, /* transaction pointer */
1855 struct xfs_buf *agbp, /* buffer for agf structure */
1856 xfs_agnumber_t agno, /* allocation group number */
1857 xfs_btnum_t btnum) /* btree identifier */
1859 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
1860 struct xfs_btree_cur *cur;
1862 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
1864 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1868 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1869 cur->bc_btnum = btnum;
1870 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1872 cur->bc_ops = &xfs_allocbt_ops;
1873 if (btnum == XFS_BTNUM_CNT)
1874 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
1876 cur->bc_private.a.agbp = agbp;
1877 cur->bc_private.a.agno = agno;