2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_alloc.h"
56 #include "xfs_error.h"
59 * Inode allocation management for XFS.
63 * Prototypes for internal functions.
66 STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
67 STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
68 STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
69 STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
70 STATIC int xfs_inobt_lshift(xfs_btree_cur_t *, int, int *);
71 STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);
72 STATIC int xfs_inobt_rshift(xfs_btree_cur_t *, int, int *);
73 STATIC int xfs_inobt_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
74 xfs_inobt_key_t *, xfs_btree_cur_t **, int *);
75 STATIC int xfs_inobt_updkey(xfs_btree_cur_t *, xfs_inobt_key_t *, int);
82 * Single level of the xfs_inobt_delete record deletion routine.
83 * Delete record pointed to by cur/level.
84 * Remove the record from its block then rebalance the tree.
85 * Return 0 for error, 1 for done, 2 to go on to the next level.
87 STATIC int /* error */
89 xfs_btree_cur_t *cur, /* btree cursor */
90 int level, /* level removing record from */
91 int *stat) /* fail/done/go-on */
93 xfs_buf_t *agbp; /* buffer for a.g. inode header */
94 xfs_mount_t *mp; /* mount structure */
95 xfs_agi_t *agi; /* allocation group inode header */
96 xfs_inobt_block_t *block; /* btree block record/key lives in */
97 xfs_agblock_t bno; /* btree block number */
98 xfs_buf_t *bp; /* buffer for block */
99 int error; /* error return value */
100 int i; /* loop index */
101 xfs_inobt_key_t key; /* kp points here if block is level 0 */
102 xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
103 xfs_agblock_t lbno; /* left block's block number */
104 xfs_buf_t *lbp; /* left block's buffer pointer */
105 xfs_inobt_block_t *left; /* left btree block */
106 xfs_inobt_key_t *lkp; /* left block key pointer */
107 xfs_inobt_ptr_t *lpp; /* left block address pointer */
108 int lrecs = 0; /* number of records in left block */
109 xfs_inobt_rec_t *lrp; /* left block record pointer */
110 xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
111 int ptr; /* index in btree block for this rec */
112 xfs_agblock_t rbno; /* right block's block number */
113 xfs_buf_t *rbp; /* right block's buffer pointer */
114 xfs_inobt_block_t *right; /* right btree block */
115 xfs_inobt_key_t *rkp; /* right block key pointer */
116 xfs_inobt_rec_t *rp; /* pointer to btree records */
117 xfs_inobt_ptr_t *rpp; /* right block address pointer */
118 int rrecs = 0; /* number of records in right block */
120 xfs_inobt_rec_t *rrp; /* right block record pointer */
121 xfs_btree_cur_t *tcur; /* temporary btree cursor */
126 * Get the index of the entry being deleted, check for nothing there.
128 ptr = cur->bc_ptrs[level];
135 * Get the buffer & block containing the record or key/ptr.
137 bp = cur->bc_bufs[level];
138 block = XFS_BUF_TO_INOBT_BLOCK(bp);
140 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
144 * Fail if we're off the end of the block.
147 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
153 * It's a nonleaf. Excise the key and ptr being deleted, by
154 * sliding the entries past them down one.
155 * Log the changed areas of the block.
158 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
159 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
161 for (i = ptr; i < numrecs; i++) {
162 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i], ARCH_CONVERT), level)))
167 memmove(&kp[ptr - 1], &kp[ptr],
168 (numrecs - ptr) * sizeof(*kp));
169 memmove(&pp[ptr - 1], &pp[ptr],
170 (numrecs - ptr) * sizeof(*kp));
171 xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
172 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
176 * It's a leaf. Excise the record being deleted, by sliding the
177 * entries past it down one. Log the changed areas of the block.
180 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
182 memmove(&rp[ptr - 1], &rp[ptr],
183 (numrecs - ptr) * sizeof(*rp));
184 xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
187 * If it's the first record in the block, we'll need a key
188 * structure to pass up to the next level (updkey).
191 key.ir_startino = rp->ir_startino;
196 * Decrement and log the number of entries in the block.
199 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
200 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
202 * Is this the root level? If so, we're almost done.
204 if (level == cur->bc_nlevels - 1) {
206 * If this is the root level,
207 * and there's only one entry left,
208 * and it's NOT the leaf level,
209 * then we can get rid of this level.
211 if (numrecs == 1 && level > 0) {
212 agbp = cur->bc_private.i.agbp;
213 agi = XFS_BUF_TO_AGI(agbp);
215 * pp is still set to the first pointer in the block.
216 * Make it the new root of the btree.
218 bno = INT_GET(agi->agi_root, ARCH_CONVERT);
220 INT_MOD(agi->agi_level, ARCH_CONVERT, -1);
224 if ((error = xfs_free_extent(cur->bc_tp,
225 XFS_AGB_TO_FSB(mp, cur->bc_private.i.agno, bno), 1)))
227 xfs_trans_binval(cur->bc_tp, bp);
228 xfs_ialloc_log_agi(cur->bc_tp, agbp,
229 XFS_AGI_ROOT | XFS_AGI_LEVEL);
231 * Update the cursor so there's one fewer level.
233 cur->bc_bufs[level] = NULL;
235 } else if (level > 0 &&
236 (error = xfs_inobt_decrement(cur, level, &i)))
242 * If we deleted the leftmost entry in the block, update the
243 * key values above us in the tree.
245 if (ptr == 1 && (error = xfs_inobt_updkey(cur, kp, level + 1)))
248 * If the number of records remaining in the block is at least
249 * the minimum, we're done.
251 if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
253 (error = xfs_inobt_decrement(cur, level, &i)))
259 * Otherwise, we have to move some records around to keep the
260 * tree balanced. Look at the left and right sibling blocks to
261 * see if we can re-balance by moving only one record.
263 rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
264 lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT);
266 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
268 * Duplicate the cursor so our btree manipulations here won't
269 * disrupt the next level up.
271 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
274 * If there's a right sibling, see if it's ok to shift an entry
277 if (rbno != NULLAGBLOCK) {
279 * Move the temp cursor to the last entry in the next block.
280 * Actually any entry but the first would suffice.
282 i = xfs_btree_lastrec(tcur, level);
283 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
284 if ((error = xfs_inobt_increment(tcur, level, &i)))
286 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
287 i = xfs_btree_lastrec(tcur, level);
288 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
290 * Grab a pointer to the block.
292 rbp = tcur->bc_bufs[level];
293 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
295 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
299 * Grab the current block number, for future use.
301 bno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
303 * If right block is full enough so that removing one entry
304 * won't make it too empty, and left-shifting an entry out
305 * of right to us works, we're done.
307 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >=
308 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
309 if ((error = xfs_inobt_lshift(tcur, level, &i)))
312 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
313 XFS_INOBT_BLOCK_MINRECS(level, cur));
314 xfs_btree_del_cursor(tcur,
317 (error = xfs_inobt_decrement(cur, level,
325 * Otherwise, grab the number of records in right for
326 * future reference, and fix up the temp cursor to point
327 * to our block again (last record).
329 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
330 if (lbno != NULLAGBLOCK) {
331 xfs_btree_firstrec(tcur, level);
332 if ((error = xfs_inobt_decrement(tcur, level, &i)))
337 * If there's a left sibling, see if it's ok to shift an entry
340 if (lbno != NULLAGBLOCK) {
342 * Move the temp cursor to the first entry in the
345 xfs_btree_firstrec(tcur, level);
346 if ((error = xfs_inobt_decrement(tcur, level, &i)))
348 xfs_btree_firstrec(tcur, level);
350 * Grab a pointer to the block.
352 lbp = tcur->bc_bufs[level];
353 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
355 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
359 * Grab the current block number, for future use.
361 bno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
363 * If left block is full enough so that removing one entry
364 * won't make it too empty, and right-shifting an entry out
365 * of left to us works, we're done.
367 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >=
368 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
369 if ((error = xfs_inobt_rshift(tcur, level, &i)))
372 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
373 XFS_INOBT_BLOCK_MINRECS(level, cur));
374 xfs_btree_del_cursor(tcur,
383 * Otherwise, grab the number of records in right for
386 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
389 * Delete the temp cursor, we're done with it.
391 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
393 * If here, we need to do a join to keep the tree balanced.
395 ASSERT(bno != NULLAGBLOCK);
397 * See if we can join with the left neighbor block.
399 if (lbno != NULLAGBLOCK &&
400 lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
402 * Set "right" to be the starting block,
403 * "left" to be the left neighbor.
407 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
409 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
410 cur->bc_private.i.agno, lbno, 0, &lbp,
413 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
414 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
415 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
419 * If that won't work, see if we can join with the right neighbor block.
421 else if (rbno != NULLAGBLOCK &&
422 rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
424 * Set "left" to be the starting block,
425 * "right" to be the right neighbor.
429 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
431 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
432 cur->bc_private.i.agno, rbno, 0, &rbp,
435 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
436 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
437 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
441 * Otherwise, we can't fix the imbalance.
442 * Just return. This is probably a logic error, but it's not fatal.
445 if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i)))
451 * We're now going to join "left" and "right" by moving all the stuff
452 * in "right" to "left" and deleting "right".
456 * It's a non-leaf. Move keys and pointers.
458 lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
459 lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
460 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
461 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
463 for (i = 0; i < rrecs; i++) {
464 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
468 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
469 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
470 xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
471 xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
474 * It's a leaf. Move records.
476 lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
477 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
478 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
479 xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
482 * If we joined with the left neighbor, set the buffer in the
483 * cursor to the left block, and fix up the index.
486 xfs_btree_setbuf(cur, level, lbp);
487 cur->bc_ptrs[level] += lrecs;
490 * If we joined with the right neighbor and there's a level above
491 * us, increment the cursor at that level.
493 else if (level + 1 < cur->bc_nlevels &&
494 (error = xfs_alloc_increment(cur, level + 1, &i)))
497 * Fix up the number of records in the surviving block.
500 INT_SET(left->bb_numrecs, ARCH_CONVERT, lrecs);
502 * Fix up the right block pointer in the surviving block, and log it.
504 left->bb_rightsib = right->bb_rightsib;
505 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
507 * If there is a right sibling now, make it point to the
510 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
511 xfs_inobt_block_t *rrblock;
514 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
515 cur->bc_private.i.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0,
516 &rrbp, XFS_INO_BTREE_REF)))
518 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
519 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
521 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, lbno);
522 xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
525 * Free the deleting block.
527 if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
528 cur->bc_private.i.agno, rbno), 1)))
530 xfs_trans_binval(cur->bc_tp, rbp);
532 * Readjust the ptr at this level if it's not a leaf, since it's
533 * still pointing at the deletion point, which makes the cursor
534 * inconsistent. If this makes the ptr 0, the caller fixes it up.
535 * We can't use decrement because it would change the next level up.
538 cur->bc_ptrs[level]--;
540 * Return value means the next level up has something to do.
546 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
551 * Insert one record/level. Return information to the caller
552 * allowing the next level up to proceed if necessary.
554 STATIC int /* error */
556 xfs_btree_cur_t *cur, /* btree cursor */
557 int level, /* level to insert record at */
558 xfs_agblock_t *bnop, /* i/o: block number inserted */
559 xfs_inobt_rec_t *recp, /* i/o: record data inserted */
560 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
561 int *stat) /* success/failure */
563 xfs_inobt_block_t *block; /* btree block record/key lives in */
564 xfs_buf_t *bp; /* buffer for block */
565 int error; /* error return value */
566 int i; /* loop index */
567 xfs_inobt_key_t key; /* key value being inserted */
568 xfs_inobt_key_t *kp=NULL; /* pointer to btree keys */
569 xfs_agblock_t nbno; /* block number of allocated block */
570 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
571 xfs_inobt_key_t nkey; /* new key value, from split */
572 xfs_inobt_rec_t nrec; /* new record value, for caller */
574 int optr; /* old ptr value */
575 xfs_inobt_ptr_t *pp; /* pointer to btree addresses */
576 int ptr; /* index in btree block for this rec */
577 xfs_inobt_rec_t *rp=NULL; /* pointer to btree records */
580 * If we made it to the root level, allocate a new root block
583 if (level >= cur->bc_nlevels) {
584 error = xfs_inobt_newroot(cur, &i);
590 * Make a key out of the record data to be inserted, and save it.
592 key.ir_startino = recp->ir_startino; /* INT_: direct copy */
593 optr = ptr = cur->bc_ptrs[level];
595 * If we're off the left edge, return failure.
602 * Get pointers to the btree buffer and block.
604 bp = cur->bc_bufs[level];
605 block = XFS_BUF_TO_INOBT_BLOCK(bp);
606 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
608 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
611 * Check that the new entry is being inserted in the right place.
613 if (ptr <= numrecs) {
615 rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
616 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
618 kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
619 xfs_btree_check_key(cur->bc_btnum, &key, kp);
624 ncur = (xfs_btree_cur_t *)0;
626 * If the block is full, we can't insert the new entry until we
627 * make the block un-full.
629 if (numrecs == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
631 * First, try shifting an entry to the right neighbor.
633 if ((error = xfs_inobt_rshift(cur, level, &i)))
639 * Next, try shifting an entry to the left neighbor.
642 if ((error = xfs_inobt_lshift(cur, level, &i)))
645 optr = ptr = cur->bc_ptrs[level];
648 * Next, try splitting the current block
649 * in half. If this works we have to
650 * re-set our variables because
651 * we could be in a different block now.
653 if ((error = xfs_inobt_split(cur, level, &nbno,
657 bp = cur->bc_bufs[level];
658 block = XFS_BUF_TO_INOBT_BLOCK(bp);
660 if ((error = xfs_btree_check_sblock(cur,
664 ptr = cur->bc_ptrs[level];
665 nrec.ir_startino = nkey.ir_startino; /* INT_: direct copy */
668 * Otherwise the insert fails.
677 * At this point we know there's room for our new entry in the block
680 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
683 * It's a non-leaf entry. Make a hole for the new data
684 * in the key and ptr regions of the block.
686 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
687 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
689 for (i = numrecs; i >= ptr; i--) {
690 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
694 memmove(&kp[ptr], &kp[ptr - 1],
695 (numrecs - ptr + 1) * sizeof(*kp));
696 memmove(&pp[ptr], &pp[ptr - 1],
697 (numrecs - ptr + 1) * sizeof(*pp));
699 * Now stuff the new data in, bump numrecs and log the new data.
702 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
705 kp[ptr - 1] = key; /* INT_: struct copy */
706 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
708 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
709 xfs_inobt_log_keys(cur, bp, ptr, numrecs);
710 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs);
713 * It's a leaf entry. Make a hole for the new record.
715 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
716 memmove(&rp[ptr], &rp[ptr - 1],
717 (numrecs - ptr + 1) * sizeof(*rp));
719 * Now stuff the new record in, bump numrecs
720 * and log the new data.
722 rp[ptr - 1] = *recp; /* INT_: struct copy */
724 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
725 xfs_inobt_log_recs(cur, bp, ptr, numrecs);
728 * Log the new number of records in the btree header.
730 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
733 * Check that the key/record is in the right place, now.
737 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
740 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
745 * If we inserted at the start of a block, update the parents' keys.
747 if (optr == 1 && (error = xfs_inobt_updkey(cur, &key, level + 1)))
750 * Return the new block number, if any.
751 * If there is one, give back a record value and a cursor too.
754 if (nbno != NULLAGBLOCK) {
755 *recp = nrec; /* INT_: struct copy */
763 * Log header fields from a btree block.
767 xfs_trans_t *tp, /* transaction pointer */
768 xfs_buf_t *bp, /* buffer containing btree block */
769 int fields) /* mask of fields: XFS_BB_... */
771 int first; /* first byte offset logged */
772 int last; /* last byte offset logged */
773 static const short offsets[] = { /* table of offsets */
774 offsetof(xfs_inobt_block_t, bb_magic),
775 offsetof(xfs_inobt_block_t, bb_level),
776 offsetof(xfs_inobt_block_t, bb_numrecs),
777 offsetof(xfs_inobt_block_t, bb_leftsib),
778 offsetof(xfs_inobt_block_t, bb_rightsib),
779 sizeof(xfs_inobt_block_t)
782 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
783 xfs_trans_log_buf(tp, bp, first, last);
787 * Log keys from a btree block (nonleaf).
791 xfs_btree_cur_t *cur, /* btree cursor */
792 xfs_buf_t *bp, /* buffer containing btree block */
793 int kfirst, /* index of first key to log */
794 int klast) /* index of last key to log */
796 xfs_inobt_block_t *block; /* btree block to log from */
797 int first; /* first byte offset logged */
798 xfs_inobt_key_t *kp; /* key pointer in btree block */
799 int last; /* last byte offset logged */
801 block = XFS_BUF_TO_INOBT_BLOCK(bp);
802 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
803 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
804 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
805 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
809 * Log block pointer fields from a btree block (nonleaf).
813 xfs_btree_cur_t *cur, /* btree cursor */
814 xfs_buf_t *bp, /* buffer containing btree block */
815 int pfirst, /* index of first pointer to log */
816 int plast) /* index of last pointer to log */
818 xfs_inobt_block_t *block; /* btree block to log from */
819 int first; /* first byte offset logged */
820 int last; /* last byte offset logged */
821 xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
823 block = XFS_BUF_TO_INOBT_BLOCK(bp);
824 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
825 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
826 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
827 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
831 * Log records from a btree block (leaf).
835 xfs_btree_cur_t *cur, /* btree cursor */
836 xfs_buf_t *bp, /* buffer containing btree block */
837 int rfirst, /* index of first record to log */
838 int rlast) /* index of last record to log */
840 xfs_inobt_block_t *block; /* btree block to log from */
841 int first; /* first byte offset logged */
842 int last; /* last byte offset logged */
843 xfs_inobt_rec_t *rp; /* record pointer for btree block */
845 block = XFS_BUF_TO_INOBT_BLOCK(bp);
846 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
847 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
848 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
849 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
853 * Lookup the record. The cursor is made to point to it, based on dir.
854 * Return 0 if can't find any such record, 1 for success.
856 STATIC int /* error */
858 xfs_btree_cur_t *cur, /* btree cursor */
859 xfs_lookup_t dir, /* <=, ==, or >= */
860 int *stat) /* success/failure */
862 xfs_agblock_t agbno; /* a.g. relative btree block number */
863 xfs_agnumber_t agno; /* allocation group number */
864 xfs_inobt_block_t *block=NULL; /* current btree block */
865 __int64_t diff; /* difference for the current key */
866 int error; /* error return value */
867 int keyno=0; /* current key number */
868 int level; /* level in the btree */
869 xfs_mount_t *mp; /* file system mount point */
872 * Get the allocation group header, and the root block number.
876 xfs_agi_t *agi; /* a.g. inode header */
878 agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
879 agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
880 agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
883 * Iterate over each level in the btree, starting at the root.
884 * For each level above the leaves, find the key we need, based
885 * on the lookup record, then follow the corresponding block
886 * pointer down to the next level.
888 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
889 xfs_buf_t *bp; /* buffer pointer for btree block */
890 xfs_daddr_t d; /* disk address of btree block */
893 * Get the disk address we're looking for.
895 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
897 * If the old buffer at this level is for a different block,
898 * throw it away, otherwise just use it.
900 bp = cur->bc_bufs[level];
901 if (bp && XFS_BUF_ADDR(bp) != d)
905 * Need to get a new buffer. Read it, then
906 * set it in the cursor, releasing the old one.
908 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
909 agno, agbno, 0, &bp, XFS_INO_BTREE_REF)))
911 xfs_btree_setbuf(cur, level, bp);
913 * Point to the btree block, now that we have the buffer
915 block = XFS_BUF_TO_INOBT_BLOCK(bp);
916 if ((error = xfs_btree_check_sblock(cur, block, level,
920 block = XFS_BUF_TO_INOBT_BLOCK(bp);
922 * If we already had a key match at a higher level, we know
923 * we need to use the first entry in this block.
928 * Otherwise we need to search this block. Do a binary search.
931 int high; /* high entry number */
932 xfs_inobt_key_t *kkbase=NULL;/* base of keys in block */
933 xfs_inobt_rec_t *krbase=NULL;/* base of records in block */
934 int low; /* low entry number */
937 * Get a pointer to keys or records.
940 kkbase = XFS_INOBT_KEY_ADDR(block, 1, cur);
942 krbase = XFS_INOBT_REC_ADDR(block, 1, cur);
944 * Set low and high entry numbers, 1-based.
947 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
949 * If the block is empty, the tree must
952 ASSERT(level == 0 && cur->bc_nlevels == 1);
953 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
958 * Binary search the block.
960 while (low <= high) {
961 xfs_agino_t startino; /* key value */
964 * keyno is average of low and high.
966 keyno = (low + high) >> 1;
971 xfs_inobt_key_t *kkp;
973 kkp = kkbase + keyno - 1;
974 startino = INT_GET(kkp->ir_startino, ARCH_CONVERT);
976 xfs_inobt_rec_t *krp;
978 krp = krbase + keyno - 1;
979 startino = INT_GET(krp->ir_startino, ARCH_CONVERT);
982 * Compute difference to get next direction.
985 startino - cur->bc_rec.i.ir_startino;
987 * Less than, move right.
992 * Greater than, move left.
1004 * If there are more levels, set up for the next level
1005 * by getting the block number and filling in the cursor.
1009 * If we moved left, need the previous key number,
1010 * unless there isn't one.
1012 if (diff > 0 && --keyno < 1)
1014 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1016 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1019 cur->bc_ptrs[level] = keyno;
1023 * Done with the search.
1024 * See if we need to adjust the results.
1026 if (dir != XFS_LOOKUP_LE && diff < 0) {
1029 * If ge search and we went off the end of the block, but it's
1030 * not the last block, we're in the wrong block.
1032 if (dir == XFS_LOOKUP_GE &&
1033 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1034 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1037 cur->bc_ptrs[0] = keyno;
1038 if ((error = xfs_inobt_increment(cur, 0, &i)))
1045 else if (dir == XFS_LOOKUP_LE && diff > 0)
1047 cur->bc_ptrs[0] = keyno;
1049 * Return if we succeeded or not.
1051 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1054 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1059 * Move 1 record left from cur/level if possible.
1060 * Update cur to reflect the new path.
1062 STATIC int /* error */
1064 xfs_btree_cur_t *cur, /* btree cursor */
1065 int level, /* level to shift record on */
1066 int *stat) /* success/failure */
1068 int error; /* error return value */
1070 int i; /* loop index */
1072 xfs_inobt_key_t key; /* key value for leaf level upward */
1073 xfs_buf_t *lbp; /* buffer for left neighbor block */
1074 xfs_inobt_block_t *left; /* left neighbor btree block */
1075 xfs_inobt_key_t *lkp=NULL; /* key pointer for left block */
1076 xfs_inobt_ptr_t *lpp; /* address pointer for left block */
1077 xfs_inobt_rec_t *lrp=NULL; /* record pointer for left block */
1078 int nrec; /* new number of left block entries */
1079 xfs_buf_t *rbp; /* buffer for right (current) block */
1080 xfs_inobt_block_t *right; /* right (current) btree block */
1081 xfs_inobt_key_t *rkp=NULL; /* key pointer for right block */
1082 xfs_inobt_ptr_t *rpp=NULL; /* address pointer for right block */
1083 xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
1086 * Set up variables for this block as "right".
1088 rbp = cur->bc_bufs[level];
1089 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1091 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1095 * If we've got no left sibling then we can't shift an entry left.
1097 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1102 * If the cursor entry is the one that would be moved, don't
1103 * do it... it's too complicated.
1105 if (cur->bc_ptrs[level] <= 1) {
1110 * Set up the left neighbor as "left".
1112 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1113 cur->bc_private.i.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1114 XFS_INO_BTREE_REF)))
1116 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1117 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1120 * If it's full, it can't take another entry.
1122 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
1126 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1128 * If non-leaf, copy a key and a ptr to the left block.
1131 lkp = XFS_INOBT_KEY_ADDR(left, nrec, cur);
1132 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1134 xfs_inobt_log_keys(cur, lbp, nrec, nrec);
1135 lpp = XFS_INOBT_PTR_ADDR(left, nrec, cur);
1136 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1138 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1141 *lpp = *rpp; /* INT_: no-change copy */
1142 xfs_inobt_log_ptrs(cur, lbp, nrec, nrec);
1145 * If leaf, copy a record to the left block.
1148 lrp = XFS_INOBT_REC_ADDR(left, nrec, cur);
1149 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1151 xfs_inobt_log_recs(cur, lbp, nrec, nrec);
1154 * Bump and log left's numrecs, decrement and log right's numrecs.
1156 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1157 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1160 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1162 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1164 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1165 xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1167 * Slide the contents of right down one entry.
1171 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1172 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1177 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1178 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1179 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1180 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1182 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1183 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1184 key.ir_startino = rrp->ir_startino; /* INT_: direct copy */
1188 * Update the parent key values of right.
1190 if ((error = xfs_inobt_updkey(cur, rkp, level + 1)))
1193 * Slide the cursor value left one.
1195 cur->bc_ptrs[level]--;
1201 * Allocate a new root block, fill it in.
1203 STATIC int /* error */
1205 xfs_btree_cur_t *cur, /* btree cursor */
1206 int *stat) /* success/failure */
1208 xfs_agi_t *agi; /* a.g. inode header */
1209 xfs_alloc_arg_t args; /* allocation argument structure */
1210 xfs_inobt_block_t *block; /* one half of the old root block */
1211 xfs_buf_t *bp; /* buffer containing block */
1212 int error; /* error return value */
1213 xfs_inobt_key_t *kp; /* btree key pointer */
1214 xfs_agblock_t lbno; /* left block number */
1215 xfs_buf_t *lbp; /* left buffer pointer */
1216 xfs_inobt_block_t *left; /* left btree block */
1217 xfs_buf_t *nbp; /* new (root) buffer */
1218 xfs_inobt_block_t *new; /* new (root) btree block */
1219 int nptr; /* new value for key index, 1 or 2 */
1220 xfs_inobt_ptr_t *pp; /* btree address pointer */
1221 xfs_agblock_t rbno; /* right block number */
1222 xfs_buf_t *rbp; /* right buffer pointer */
1223 xfs_inobt_block_t *right; /* right btree block */
1224 xfs_inobt_rec_t *rp; /* btree record pointer */
1226 ASSERT(cur->bc_nlevels < XFS_IN_MAXLEVELS(cur->bc_mp));
1229 * Get a block & a buffer.
1231 agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
1232 args.tp = cur->bc_tp;
1233 args.mp = cur->bc_mp;
1234 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno,
1235 INT_GET(agi->agi_root, ARCH_CONVERT));
1236 args.mod = args.minleft = args.alignment = args.total = args.wasdel =
1237 args.isfl = args.userdata = args.minalignslop = 0;
1238 args.minlen = args.maxlen = args.prod = 1;
1239 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1240 if ((error = xfs_alloc_vextent(&args)))
1243 * None available, we fail.
1245 if (args.fsbno == NULLFSBLOCK) {
1249 ASSERT(args.len == 1);
1250 nbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
1251 new = XFS_BUF_TO_INOBT_BLOCK(nbp);
1253 * Set the root data in the a.g. inode structure.
1255 INT_SET(agi->agi_root, ARCH_CONVERT, args.agbno);
1256 INT_MOD(agi->agi_level, ARCH_CONVERT, 1);
1257 xfs_ialloc_log_agi(args.tp, cur->bc_private.i.agbp,
1258 XFS_AGI_ROOT | XFS_AGI_LEVEL);
1260 * At the previous root level there are now two blocks: the old
1261 * root, and the new block generated when it was split.
1262 * We don't know which one the cursor is pointing at, so we
1263 * set up variables "left" and "right" for each case.
1265 bp = cur->bc_bufs[cur->bc_nlevels - 1];
1266 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1268 if ((error = xfs_btree_check_sblock(cur, block, cur->bc_nlevels - 1, bp)))
1271 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1273 * Our block is left, pick up the right block.
1276 lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
1278 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1279 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1280 rbno, 0, &rbp, XFS_INO_BTREE_REF)))
1283 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1284 if ((error = xfs_btree_check_sblock(cur, right,
1285 cur->bc_nlevels - 1, rbp)))
1290 * Our block is right, pick up the left block.
1293 rbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(rbp));
1295 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1296 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1297 lbno, 0, &lbp, XFS_INO_BTREE_REF)))
1300 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1301 if ((error = xfs_btree_check_sblock(cur, left,
1302 cur->bc_nlevels - 1, lbp)))
1307 * Fill in the new block's btree header and log it.
1309 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1310 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1311 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1312 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1313 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1314 xfs_inobt_log_block(args.tp, nbp, XFS_BB_ALL_BITS);
1315 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1317 * Fill in the key data in the new root.
1319 kp = XFS_INOBT_KEY_ADDR(new, 1, cur);
1320 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1321 kp[0] = *XFS_INOBT_KEY_ADDR(left, 1, cur); /* INT_: struct copy */
1322 kp[1] = *XFS_INOBT_KEY_ADDR(right, 1, cur); /* INT_: struct copy */
1324 rp = XFS_INOBT_REC_ADDR(left, 1, cur);
1325 INT_COPY(kp[0].ir_startino, rp->ir_startino, ARCH_CONVERT);
1326 rp = XFS_INOBT_REC_ADDR(right, 1, cur);
1327 INT_COPY(kp[1].ir_startino, rp->ir_startino, ARCH_CONVERT);
1329 xfs_inobt_log_keys(cur, nbp, 1, 2);
1331 * Fill in the pointer data in the new root.
1333 pp = XFS_INOBT_PTR_ADDR(new, 1, cur);
1334 INT_SET(pp[0], ARCH_CONVERT, lbno);
1335 INT_SET(pp[1], ARCH_CONVERT, rbno);
1336 xfs_inobt_log_ptrs(cur, nbp, 1, 2);
1338 * Fix up the cursor.
1340 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1341 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1348 * Move 1 record right from cur/level if possible.
1349 * Update cur to reflect the new path.
1351 STATIC int /* error */
1353 xfs_btree_cur_t *cur, /* btree cursor */
1354 int level, /* level to shift record on */
1355 int *stat) /* success/failure */
1357 int error; /* error return value */
1358 int i; /* loop index */
1359 xfs_inobt_key_t key; /* key value for leaf level upward */
1360 xfs_buf_t *lbp; /* buffer for left (current) block */
1361 xfs_inobt_block_t *left; /* left (current) btree block */
1362 xfs_inobt_key_t *lkp; /* key pointer for left block */
1363 xfs_inobt_ptr_t *lpp; /* address pointer for left block */
1364 xfs_inobt_rec_t *lrp; /* record pointer for left block */
1365 xfs_buf_t *rbp; /* buffer for right neighbor block */
1366 xfs_inobt_block_t *right; /* right neighbor btree block */
1367 xfs_inobt_key_t *rkp; /* key pointer for right block */
1368 xfs_inobt_ptr_t *rpp; /* address pointer for right block */
1369 xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
1370 xfs_btree_cur_t *tcur; /* temporary cursor */
1373 * Set up variables for this block as "left".
1375 lbp = cur->bc_bufs[level];
1376 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1378 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1382 * If we've got no right sibling then we can't shift an entry right.
1384 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1389 * If the cursor entry is the one that would be moved, don't
1390 * do it... it's too complicated.
1392 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1397 * Set up the right neighbor as "right".
1399 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1400 cur->bc_private.i.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1401 XFS_INO_BTREE_REF)))
1403 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1404 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1407 * If it's full, it can't take another entry.
1409 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
1414 * Make a hole at the start of the right neighbor block, then
1415 * copy the last left block entry to the hole.
1418 lkp = XFS_INOBT_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1419 lpp = XFS_INOBT_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1420 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1421 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1423 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1424 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1428 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1429 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1431 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1434 *rkp = *lkp; /* INT_: no change copy */
1435 *rpp = *lpp; /* INT_: no change copy */
1436 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1437 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1439 lrp = XFS_INOBT_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1440 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1441 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1443 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1444 key.ir_startino = rrp->ir_startino; /* INT_: direct copy */
1448 * Decrement and log left's numrecs, bump and log right's numrecs.
1450 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1451 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1452 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1455 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1457 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1459 xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1461 * Using a temporary cursor, update the parent key values of the
1462 * block on the right.
1464 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1466 xfs_btree_lastrec(tcur, level);
1467 if ((error = xfs_inobt_increment(tcur, level, &i)) ||
1468 (error = xfs_inobt_updkey(tcur, rkp, level + 1))) {
1469 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1472 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1478 * Split cur/level block in half.
1479 * Return new block number and its first record (to be inserted into parent).
1481 STATIC int /* error */
1483 xfs_btree_cur_t *cur, /* btree cursor */
1484 int level, /* level to split */
1485 xfs_agblock_t *bnop, /* output: block number allocated */
1486 xfs_inobt_key_t *keyp, /* output: first key of new block */
1487 xfs_btree_cur_t **curp, /* output: new cursor */
1488 int *stat) /* success/failure */
1490 xfs_alloc_arg_t args; /* allocation argument structure */
1491 int error; /* error return value */
1492 int i; /* loop index/record number */
1493 xfs_agblock_t lbno; /* left (current) block number */
1494 xfs_buf_t *lbp; /* buffer for left block */
1495 xfs_inobt_block_t *left; /* left (current) btree block */
1496 xfs_inobt_key_t *lkp; /* left btree key pointer */
1497 xfs_inobt_ptr_t *lpp; /* left btree address pointer */
1498 xfs_inobt_rec_t *lrp; /* left btree record pointer */
1499 xfs_buf_t *rbp; /* buffer for right block */
1500 xfs_inobt_block_t *right; /* right (new) btree block */
1501 xfs_inobt_key_t *rkp; /* right btree key pointer */
1502 xfs_inobt_ptr_t *rpp; /* right btree address pointer */
1503 xfs_inobt_rec_t *rrp; /* right btree record pointer */
1506 * Set up left block (current one).
1508 lbp = cur->bc_bufs[level];
1509 args.tp = cur->bc_tp;
1510 args.mp = cur->bc_mp;
1511 lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
1513 * Allocate the new block.
1514 * If we can't do it, we're toast. Give up.
1516 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno, lbno);
1517 args.mod = args.minleft = args.alignment = args.total = args.wasdel =
1518 args.isfl = args.userdata = args.minalignslop = 0;
1519 args.minlen = args.maxlen = args.prod = 1;
1520 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1521 if ((error = xfs_alloc_vextent(&args)))
1523 if (args.fsbno == NULLFSBLOCK) {
1527 ASSERT(args.len == 1);
1528 rbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
1530 * Set up the new block as "right".
1532 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1534 * "Left" is the current (according to the cursor) block.
1536 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1538 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1542 * Fill in the btree header for the new block.
1544 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1545 right->bb_level = left->bb_level; /* INT_: direct copy */
1546 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1548 * Make sure that if there's an odd number of entries now, that
1549 * each new block will have the same number of entries.
1551 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1552 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1553 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1554 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1556 * For non-leaf blocks, copy keys and addresses over to the new block.
1559 lkp = XFS_INOBT_KEY_ADDR(left, i, cur);
1560 lpp = XFS_INOBT_PTR_ADDR(left, i, cur);
1561 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1562 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1564 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1565 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1569 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1570 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1571 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1572 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1576 * For leaf blocks, copy records over to the new block.
1579 lrp = XFS_INOBT_REC_ADDR(left, i, cur);
1580 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1581 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1582 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1583 keyp->ir_startino = rrp->ir_startino; /* INT_: direct copy */
1586 * Find the left block number by looking in the buffer.
1587 * Adjust numrecs, sibling pointers.
1589 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1590 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1591 INT_SET(left->bb_rightsib, ARCH_CONVERT, args.agbno);
1592 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1593 xfs_inobt_log_block(args.tp, rbp, XFS_BB_ALL_BITS);
1594 xfs_inobt_log_block(args.tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1596 * If there's a block to the new block's right, make that block
1597 * point back to right instead of to left.
1599 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1600 xfs_inobt_block_t *rrblock; /* rr btree block */
1601 xfs_buf_t *rrbp; /* buffer for rrblock */
1603 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1604 INT_GET(right->bb_rightsib, ARCH_CONVERT), 0, &rrbp,
1605 XFS_INO_BTREE_REF)))
1607 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
1608 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1610 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, args.agbno);
1611 xfs_inobt_log_block(args.tp, rrbp, XFS_BB_LEFTSIB);
1614 * If the cursor is really in the right block, move it there.
1615 * If it's just pointing past the last entry in left, then we'll
1616 * insert there, so don't change anything in that case.
1618 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1619 xfs_btree_setbuf(cur, level, rbp);
1620 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1623 * If there are more levels, we'll need another cursor which refers
1624 * the right block, no matter where this cursor was.
1626 if (level + 1 < cur->bc_nlevels) {
1627 if ((error = xfs_btree_dup_cursor(cur, curp)))
1629 (*curp)->bc_ptrs[level + 1]++;
1637 * Update keys at all levels from here to the root along the cursor's path.
1639 STATIC int /* error */
1641 xfs_btree_cur_t *cur, /* btree cursor */
1642 xfs_inobt_key_t *keyp, /* new key value to update to */
1643 int level) /* starting level for update */
1645 int ptr; /* index of key in block */
1648 * Go up the tree from this level toward the root.
1649 * At each level, update the key value to the value input.
1650 * Stop when we reach a level where the cursor isn't pointing
1651 * at the first entry in the block.
1653 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1654 xfs_buf_t *bp; /* buffer for block */
1655 xfs_inobt_block_t *block; /* btree block */
1657 int error; /* error return value */
1659 xfs_inobt_key_t *kp; /* ptr to btree block keys */
1661 bp = cur->bc_bufs[level];
1662 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1664 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1667 ptr = cur->bc_ptrs[level];
1668 kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
1670 xfs_inobt_log_keys(cur, bp, ptr, ptr);
1676 * Externally visible routines.
1680 * Decrement cursor by one record at the level.
1681 * For nonzero levels the leaf-ward information is untouched.
1684 xfs_inobt_decrement(
1685 xfs_btree_cur_t *cur, /* btree cursor */
1686 int level, /* level in btree, 0 is leaf */
1687 int *stat) /* success/failure */
1689 xfs_inobt_block_t *block; /* btree block */
1691 int lev; /* btree level */
1693 ASSERT(level < cur->bc_nlevels);
1695 * Read-ahead to the left at this level.
1697 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1699 * Decrement the ptr at this level. If we're still in the block
1702 if (--cur->bc_ptrs[level] > 0) {
1707 * Get a pointer to the btree block.
1709 block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[level]);
1711 if ((error = xfs_btree_check_sblock(cur, block, level,
1712 cur->bc_bufs[level])))
1716 * If we just went off the left edge of the tree, return failure.
1718 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1723 * March up the tree decrementing pointers.
1724 * Stop when we don't go off the left edge of a block.
1726 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1727 if (--cur->bc_ptrs[lev] > 0)
1730 * Read-ahead the left block, we're going to read it
1733 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1736 * If we went off the root then we are seriously confused.
1738 ASSERT(lev < cur->bc_nlevels);
1740 * Now walk back down the tree, fixing up the cursor's buffer
1741 * pointers and key numbers.
1743 for (block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1744 xfs_agblock_t agbno; /* block number of btree block */
1745 xfs_buf_t *bp; /* buffer containing btree block */
1747 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1748 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1749 cur->bc_private.i.agno, agbno, 0, &bp,
1750 XFS_INO_BTREE_REF)))
1753 xfs_btree_setbuf(cur, lev, bp);
1754 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1755 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1757 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1764 * Delete the record pointed to by cur.
1765 * The cursor refers to the place where the record was (could be inserted)
1766 * when the operation returns.
1770 xfs_btree_cur_t *cur, /* btree cursor */
1771 int *stat) /* success/failure */
1774 int i; /* result code */
1775 int level; /* btree level */
1778 * Go up the tree, starting at leaf level.
1779 * If 2 is returned then a join was done; go to the next level.
1780 * Otherwise we are done.
1782 for (level = 0, i = 2; i == 2; level++) {
1783 if ((error = xfs_inobt_delrec(cur, level, &i)))
1787 for (level = 1; level < cur->bc_nlevels; level++) {
1788 if (cur->bc_ptrs[level] == 0) {
1789 if ((error = xfs_inobt_decrement(cur, level, &i)))
1801 * Get the data from the pointed-to record.
1805 xfs_btree_cur_t *cur, /* btree cursor */
1806 xfs_agino_t *ino, /* output: starting inode of chunk */
1807 __int32_t *fcnt, /* output: number of free inodes */
1808 xfs_inofree_t *free, /* output: free inode mask */
1809 int *stat) /* output: success/failure */
1811 xfs_inobt_block_t *block; /* btree block */
1812 xfs_buf_t *bp; /* buffer containing btree block */
1814 int error; /* error return value */
1816 int ptr; /* record number */
1817 xfs_inobt_rec_t *rec; /* record data */
1819 bp = cur->bc_bufs[0];
1820 ptr = cur->bc_ptrs[0];
1821 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1823 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
1827 * Off the right end or left end, return failure.
1829 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1834 * Point to the record and extract its data.
1836 rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
1837 *ino = INT_GET(rec->ir_startino, ARCH_CONVERT);
1838 *fcnt = INT_GET(rec->ir_freecount, ARCH_CONVERT);
1839 *free = INT_GET(rec->ir_free, ARCH_CONVERT);
1845 * Increment cursor by one record at the level.
1846 * For nonzero levels the leaf-ward information is untouched.
1849 xfs_inobt_increment(
1850 xfs_btree_cur_t *cur, /* btree cursor */
1851 int level, /* level in btree, 0 is leaf */
1852 int *stat) /* success/failure */
1854 xfs_inobt_block_t *block; /* btree block */
1855 xfs_buf_t *bp; /* buffer containing btree block */
1856 int error; /* error return value */
1857 int lev; /* btree level */
1859 ASSERT(level < cur->bc_nlevels);
1861 * Read-ahead to the right at this level.
1863 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1865 * Get a pointer to the btree block.
1867 bp = cur->bc_bufs[level];
1868 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1870 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1874 * Increment the ptr at this level. If we're still in the block
1877 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1882 * If we just went off the right edge of the tree, return failure.
1884 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1889 * March up the tree incrementing pointers.
1890 * Stop when we don't go off the right edge of a block.
1892 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1893 bp = cur->bc_bufs[lev];
1894 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1896 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1899 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1902 * Read-ahead the right block, we're going to read it
1905 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1908 * If we went off the root then we are seriously confused.
1910 ASSERT(lev < cur->bc_nlevels);
1912 * Now walk back down the tree, fixing up the cursor's buffer
1913 * pointers and key numbers.
1915 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_INOBT_BLOCK(bp);
1917 xfs_agblock_t agbno; /* block number of btree block */
1919 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1920 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1921 cur->bc_private.i.agno, agbno, 0, &bp,
1922 XFS_INO_BTREE_REF)))
1925 xfs_btree_setbuf(cur, lev, bp);
1926 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1927 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1929 cur->bc_ptrs[lev] = 1;
1936 * Insert the current record at the point referenced by cur.
1937 * The cursor may be inconsistent on return if splits have been done.
1941 xfs_btree_cur_t *cur, /* btree cursor */
1942 int *stat) /* success/failure */
1944 int error; /* error return value */
1945 int i; /* result value, 0 for failure */
1946 int level; /* current level number in btree */
1947 xfs_agblock_t nbno; /* new block number (split result) */
1948 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1949 xfs_inobt_rec_t nrec; /* record being inserted this level */
1950 xfs_btree_cur_t *pcur; /* previous level's cursor */
1954 INT_SET(nrec.ir_startino, ARCH_CONVERT, cur->bc_rec.i.ir_startino);
1955 INT_SET(nrec.ir_freecount, ARCH_CONVERT, cur->bc_rec.i.ir_freecount);
1956 INT_SET(nrec.ir_free, ARCH_CONVERT, cur->bc_rec.i.ir_free);
1957 ncur = (xfs_btree_cur_t *)0;
1960 * Loop going up the tree, starting at the leaf level.
1961 * Stop when we don't get a split block, that must mean that
1962 * the insert is finished with this level.
1966 * Insert nrec/nbno into this level of the tree.
1967 * Note if we fail, nbno will be null.
1969 if ((error = xfs_inobt_insrec(pcur, level++, &nbno, &nrec, &ncur,
1972 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1976 * See if the cursor we just used is trash.
1977 * Can't trash the caller's cursor, but otherwise we should
1978 * if ncur is a new cursor or we're about to be done.
1980 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1981 cur->bc_nlevels = pcur->bc_nlevels;
1982 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1985 * If we got a new cursor, switch to it.
1989 ncur = (xfs_btree_cur_t *)0;
1991 } while (nbno != NULLAGBLOCK);
1997 * Lookup the record equal to ino in the btree given by cur.
2000 xfs_inobt_lookup_eq(
2001 xfs_btree_cur_t *cur, /* btree cursor */
2002 xfs_agino_t ino, /* starting inode of chunk */
2003 __int32_t fcnt, /* free inode count */
2004 xfs_inofree_t free, /* free inode mask */
2005 int *stat) /* success/failure */
2007 cur->bc_rec.i.ir_startino = ino;
2008 cur->bc_rec.i.ir_freecount = fcnt;
2009 cur->bc_rec.i.ir_free = free;
2010 return xfs_inobt_lookup(cur, XFS_LOOKUP_EQ, stat);
2014 * Lookup the first record greater than or equal to ino
2015 * in the btree given by cur.
2018 xfs_inobt_lookup_ge(
2019 xfs_btree_cur_t *cur, /* btree cursor */
2020 xfs_agino_t ino, /* starting inode of chunk */
2021 __int32_t fcnt, /* free inode count */
2022 xfs_inofree_t free, /* free inode mask */
2023 int *stat) /* success/failure */
2025 cur->bc_rec.i.ir_startino = ino;
2026 cur->bc_rec.i.ir_freecount = fcnt;
2027 cur->bc_rec.i.ir_free = free;
2028 return xfs_inobt_lookup(cur, XFS_LOOKUP_GE, stat);
2032 * Lookup the first record less than or equal to ino
2033 * in the btree given by cur.
2036 xfs_inobt_lookup_le(
2037 xfs_btree_cur_t *cur, /* btree cursor */
2038 xfs_agino_t ino, /* starting inode of chunk */
2039 __int32_t fcnt, /* free inode count */
2040 xfs_inofree_t free, /* free inode mask */
2041 int *stat) /* success/failure */
2043 cur->bc_rec.i.ir_startino = ino;
2044 cur->bc_rec.i.ir_freecount = fcnt;
2045 cur->bc_rec.i.ir_free = free;
2046 return xfs_inobt_lookup(cur, XFS_LOOKUP_LE, stat);
2050 * Update the record referred to by cur, to the value given
2051 * by [ino, fcnt, free].
2052 * This either works (return 0) or gets an EFSCORRUPTED error.
2056 xfs_btree_cur_t *cur, /* btree cursor */
2057 xfs_agino_t ino, /* starting inode of chunk */
2058 __int32_t fcnt, /* free inode count */
2059 xfs_inofree_t free) /* free inode mask */
2061 xfs_inobt_block_t *block; /* btree block to update */
2062 xfs_buf_t *bp; /* buffer containing btree block */
2063 int error; /* error return value */
2064 int ptr; /* current record number (updating) */
2065 xfs_inobt_rec_t *rp; /* pointer to updated record */
2068 * Pick up the current block.
2070 bp = cur->bc_bufs[0];
2071 block = XFS_BUF_TO_INOBT_BLOCK(bp);
2073 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
2077 * Get the address of the rec to be updated.
2079 ptr = cur->bc_ptrs[0];
2080 rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
2082 * Fill in the new contents and log them.
2084 INT_SET(rp->ir_startino, ARCH_CONVERT, ino);
2085 INT_SET(rp->ir_freecount, ARCH_CONVERT, fcnt);
2086 INT_SET(rp->ir_free, ARCH_CONVERT, free);
2087 xfs_inobt_log_recs(cur, bp, ptr, ptr);
2089 * Updating first record in leaf. Pass new key value up to our parent.
2092 xfs_inobt_key_t key; /* key containing [ino] */
2094 INT_SET(key.ir_startino, ARCH_CONVERT, ino);
2095 if ((error = xfs_inobt_updkey(cur, &key, 1)))