Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_ialloc_btree.c
blob2d4daecec9905956b65d9589f64782f4323c864e
1 /*
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:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
37 #include "xfs_inum.h"
38 #include "xfs_log.h"
39 #include "xfs_trans.h"
40 #include "xfs_sb.h"
41 #include "xfs_ag.h"
42 #include "xfs_dir.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_btree.h"
49 #include "xfs_ialloc.h"
50 #include "xfs_alloc.h"
51 #include "xfs_error.h"
54 * Inode allocation management for XFS.
58 * Prototypes for internal functions.
61 STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
62 STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
63 STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
64 STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
65 STATIC int xfs_inobt_lshift(xfs_btree_cur_t *, int, int *);
66 STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);
67 STATIC int xfs_inobt_rshift(xfs_btree_cur_t *, int, int *);
68 STATIC int xfs_inobt_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
69 xfs_inobt_key_t *, xfs_btree_cur_t **, int *);
70 STATIC int xfs_inobt_updkey(xfs_btree_cur_t *, xfs_inobt_key_t *, int);
73 * Internal functions.
77 * Single level of the xfs_inobt_delete record deletion routine.
78 * Delete record pointed to by cur/level.
79 * Remove the record from its block then rebalance the tree.
80 * Return 0 for error, 1 for done, 2 to go on to the next level.
82 STATIC int /* error */
83 xfs_inobt_delrec(
84 xfs_btree_cur_t *cur, /* btree cursor */
85 int level, /* level removing record from */
86 int *stat) /* fail/done/go-on */
88 xfs_buf_t *agbp; /* buffer for a.g. inode header */
89 xfs_mount_t *mp; /* mount structure */
90 xfs_agi_t *agi; /* allocation group inode header */
91 xfs_inobt_block_t *block; /* btree block record/key lives in */
92 xfs_agblock_t bno; /* btree block number */
93 xfs_buf_t *bp; /* buffer for block */
94 int error; /* error return value */
95 int i; /* loop index */
96 xfs_inobt_key_t key; /* kp points here if block is level 0 */
97 xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
98 xfs_agblock_t lbno; /* left block's block number */
99 xfs_buf_t *lbp; /* left block's buffer pointer */
100 xfs_inobt_block_t *left; /* left btree block */
101 xfs_inobt_key_t *lkp; /* left block key pointer */
102 xfs_inobt_ptr_t *lpp; /* left block address pointer */
103 int lrecs = 0; /* number of records in left block */
104 xfs_inobt_rec_t *lrp; /* left block record pointer */
105 xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
106 int ptr; /* index in btree block for this rec */
107 xfs_agblock_t rbno; /* right block's block number */
108 xfs_buf_t *rbp; /* right block's buffer pointer */
109 xfs_inobt_block_t *right; /* right btree block */
110 xfs_inobt_key_t *rkp; /* right block key pointer */
111 xfs_inobt_rec_t *rp; /* pointer to btree records */
112 xfs_inobt_ptr_t *rpp; /* right block address pointer */
113 int rrecs = 0; /* number of records in right block */
114 int numrecs;
115 xfs_inobt_rec_t *rrp; /* right block record pointer */
116 xfs_btree_cur_t *tcur; /* temporary btree cursor */
118 mp = cur->bc_mp;
121 * Get the index of the entry being deleted, check for nothing there.
123 ptr = cur->bc_ptrs[level];
124 if (ptr == 0) {
125 *stat = 0;
126 return 0;
130 * Get the buffer & block containing the record or key/ptr.
132 bp = cur->bc_bufs[level];
133 block = XFS_BUF_TO_INOBT_BLOCK(bp);
134 #ifdef DEBUG
135 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
136 return error;
137 #endif
139 * Fail if we're off the end of the block.
142 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
143 if (ptr > numrecs) {
144 *stat = 0;
145 return 0;
148 * It's a nonleaf. Excise the key and ptr being deleted, by
149 * sliding the entries past them down one.
150 * Log the changed areas of the block.
152 if (level > 0) {
153 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
154 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
155 #ifdef DEBUG
156 for (i = ptr; i < numrecs; i++) {
157 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i], ARCH_CONVERT), level)))
158 return error;
160 #endif
161 if (ptr < numrecs) {
162 memmove(&kp[ptr - 1], &kp[ptr],
163 (numrecs - ptr) * sizeof(*kp));
164 memmove(&pp[ptr - 1], &pp[ptr],
165 (numrecs - ptr) * sizeof(*kp));
166 xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
167 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
171 * It's a leaf. Excise the record being deleted, by sliding the
172 * entries past it down one. Log the changed areas of the block.
174 else {
175 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
176 if (ptr < numrecs) {
177 memmove(&rp[ptr - 1], &rp[ptr],
178 (numrecs - ptr) * sizeof(*rp));
179 xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
182 * If it's the first record in the block, we'll need a key
183 * structure to pass up to the next level (updkey).
185 if (ptr == 1) {
186 key.ir_startino = rp->ir_startino;
187 kp = &key;
191 * Decrement and log the number of entries in the block.
193 numrecs--;
194 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
195 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
197 * Is this the root level? If so, we're almost done.
199 if (level == cur->bc_nlevels - 1) {
201 * If this is the root level,
202 * and there's only one entry left,
203 * and it's NOT the leaf level,
204 * then we can get rid of this level.
206 if (numrecs == 1 && level > 0) {
207 agbp = cur->bc_private.i.agbp;
208 agi = XFS_BUF_TO_AGI(agbp);
210 * pp is still set to the first pointer in the block.
211 * Make it the new root of the btree.
213 bno = INT_GET(agi->agi_root, ARCH_CONVERT);
214 agi->agi_root = *pp;
215 INT_MOD(agi->agi_level, ARCH_CONVERT, -1);
217 * Free the block.
219 if ((error = xfs_free_extent(cur->bc_tp,
220 XFS_AGB_TO_FSB(mp, cur->bc_private.i.agno, bno), 1)))
221 return error;
222 xfs_trans_binval(cur->bc_tp, bp);
223 xfs_ialloc_log_agi(cur->bc_tp, agbp,
224 XFS_AGI_ROOT | XFS_AGI_LEVEL);
226 * Update the cursor so there's one fewer level.
228 cur->bc_bufs[level] = NULL;
229 cur->bc_nlevels--;
230 } else if (level > 0 &&
231 (error = xfs_inobt_decrement(cur, level, &i)))
232 return error;
233 *stat = 1;
234 return 0;
237 * If we deleted the leftmost entry in the block, update the
238 * key values above us in the tree.
240 if (ptr == 1 && (error = xfs_inobt_updkey(cur, kp, level + 1)))
241 return error;
243 * If the number of records remaining in the block is at least
244 * the minimum, we're done.
246 if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
247 if (level > 0 &&
248 (error = xfs_inobt_decrement(cur, level, &i)))
249 return error;
250 *stat = 1;
251 return 0;
254 * Otherwise, we have to move some records around to keep the
255 * tree balanced. Look at the left and right sibling blocks to
256 * see if we can re-balance by moving only one record.
258 rbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
259 lbno = INT_GET(block->bb_leftsib, ARCH_CONVERT);
260 bno = NULLAGBLOCK;
261 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
263 * Duplicate the cursor so our btree manipulations here won't
264 * disrupt the next level up.
266 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
267 return error;
269 * If there's a right sibling, see if it's ok to shift an entry
270 * out of it.
272 if (rbno != NULLAGBLOCK) {
274 * Move the temp cursor to the last entry in the next block.
275 * Actually any entry but the first would suffice.
277 i = xfs_btree_lastrec(tcur, level);
278 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
279 if ((error = xfs_inobt_increment(tcur, level, &i)))
280 goto error0;
281 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
282 i = xfs_btree_lastrec(tcur, level);
283 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
285 * Grab a pointer to the block.
287 rbp = tcur->bc_bufs[level];
288 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
289 #ifdef DEBUG
290 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
291 goto error0;
292 #endif
294 * Grab the current block number, for future use.
296 bno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
298 * If right block is full enough so that removing one entry
299 * won't make it too empty, and left-shifting an entry out
300 * of right to us works, we're done.
302 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1 >=
303 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
304 if ((error = xfs_inobt_lshift(tcur, level, &i)))
305 goto error0;
306 if (i) {
307 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
308 XFS_INOBT_BLOCK_MINRECS(level, cur));
309 xfs_btree_del_cursor(tcur,
310 XFS_BTREE_NOERROR);
311 if (level > 0 &&
312 (error = xfs_inobt_decrement(cur, level,
313 &i)))
314 return error;
315 *stat = 1;
316 return 0;
320 * Otherwise, grab the number of records in right for
321 * future reference, and fix up the temp cursor to point
322 * to our block again (last record).
324 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
325 if (lbno != NULLAGBLOCK) {
326 xfs_btree_firstrec(tcur, level);
327 if ((error = xfs_inobt_decrement(tcur, level, &i)))
328 goto error0;
332 * If there's a left sibling, see if it's ok to shift an entry
333 * out of it.
335 if (lbno != NULLAGBLOCK) {
337 * Move the temp cursor to the first entry in the
338 * previous block.
340 xfs_btree_firstrec(tcur, level);
341 if ((error = xfs_inobt_decrement(tcur, level, &i)))
342 goto error0;
343 xfs_btree_firstrec(tcur, level);
345 * Grab a pointer to the block.
347 lbp = tcur->bc_bufs[level];
348 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
349 #ifdef DEBUG
350 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
351 goto error0;
352 #endif
354 * Grab the current block number, for future use.
356 bno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
358 * If left block is full enough so that removing one entry
359 * won't make it too empty, and right-shifting an entry out
360 * of left to us works, we're done.
362 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) - 1 >=
363 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
364 if ((error = xfs_inobt_rshift(tcur, level, &i)))
365 goto error0;
366 if (i) {
367 ASSERT(INT_GET(block->bb_numrecs, ARCH_CONVERT) >=
368 XFS_INOBT_BLOCK_MINRECS(level, cur));
369 xfs_btree_del_cursor(tcur,
370 XFS_BTREE_NOERROR);
371 if (level == 0)
372 cur->bc_ptrs[0]++;
373 *stat = 1;
374 return 0;
378 * Otherwise, grab the number of records in right for
379 * future reference.
381 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
384 * Delete the temp cursor, we're done with it.
386 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
388 * If here, we need to do a join to keep the tree balanced.
390 ASSERT(bno != NULLAGBLOCK);
392 * See if we can join with the left neighbor block.
394 if (lbno != NULLAGBLOCK &&
395 lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
397 * Set "right" to be the starting block,
398 * "left" to be the left neighbor.
400 rbno = bno;
401 right = block;
402 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
403 rbp = bp;
404 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
405 cur->bc_private.i.agno, lbno, 0, &lbp,
406 XFS_INO_BTREE_REF)))
407 return error;
408 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
409 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
410 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
411 return error;
414 * If that won't work, see if we can join with the right neighbor block.
416 else if (rbno != NULLAGBLOCK &&
417 rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
419 * Set "left" to be the starting block,
420 * "right" to be the right neighbor.
422 lbno = bno;
423 left = block;
424 lrecs = INT_GET(left->bb_numrecs, ARCH_CONVERT);
425 lbp = bp;
426 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
427 cur->bc_private.i.agno, rbno, 0, &rbp,
428 XFS_INO_BTREE_REF)))
429 return error;
430 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
431 rrecs = INT_GET(right->bb_numrecs, ARCH_CONVERT);
432 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
433 return error;
436 * Otherwise, we can't fix the imbalance.
437 * Just return. This is probably a logic error, but it's not fatal.
439 else {
440 if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i)))
441 return error;
442 *stat = 1;
443 return 0;
446 * We're now going to join "left" and "right" by moving all the stuff
447 * in "right" to "left" and deleting "right".
449 if (level > 0) {
451 * It's a non-leaf. Move keys and pointers.
453 lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
454 lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
455 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
456 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
457 #ifdef DEBUG
458 for (i = 0; i < rrecs; i++) {
459 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
460 return error;
462 #endif
463 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
464 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
465 xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
466 xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
467 } else {
469 * It's a leaf. Move records.
471 lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
472 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
473 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
474 xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
477 * If we joined with the left neighbor, set the buffer in the
478 * cursor to the left block, and fix up the index.
480 if (bp != lbp) {
481 xfs_btree_setbuf(cur, level, lbp);
482 cur->bc_ptrs[level] += lrecs;
485 * If we joined with the right neighbor and there's a level above
486 * us, increment the cursor at that level.
488 else if (level + 1 < cur->bc_nlevels &&
489 (error = xfs_alloc_increment(cur, level + 1, &i)))
490 return error;
492 * Fix up the number of records in the surviving block.
494 lrecs += rrecs;
495 INT_SET(left->bb_numrecs, ARCH_CONVERT, lrecs);
497 * Fix up the right block pointer in the surviving block, and log it.
499 left->bb_rightsib = right->bb_rightsib;
500 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
502 * If there is a right sibling now, make it point to the
503 * remaining block.
505 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
506 xfs_inobt_block_t *rrblock;
507 xfs_buf_t *rrbp;
509 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
510 cur->bc_private.i.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0,
511 &rrbp, XFS_INO_BTREE_REF)))
512 return error;
513 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
514 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
515 return error;
516 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, lbno);
517 xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
520 * Free the deleting block.
522 if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
523 cur->bc_private.i.agno, rbno), 1)))
524 return error;
525 xfs_trans_binval(cur->bc_tp, rbp);
527 * Readjust the ptr at this level if it's not a leaf, since it's
528 * still pointing at the deletion point, which makes the cursor
529 * inconsistent. If this makes the ptr 0, the caller fixes it up.
530 * We can't use decrement because it would change the next level up.
532 if (level > 0)
533 cur->bc_ptrs[level]--;
535 * Return value means the next level up has something to do.
537 *stat = 2;
538 return 0;
540 error0:
541 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
542 return error;
546 * Insert one record/level. Return information to the caller
547 * allowing the next level up to proceed if necessary.
549 STATIC int /* error */
550 xfs_inobt_insrec(
551 xfs_btree_cur_t *cur, /* btree cursor */
552 int level, /* level to insert record at */
553 xfs_agblock_t *bnop, /* i/o: block number inserted */
554 xfs_inobt_rec_t *recp, /* i/o: record data inserted */
555 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
556 int *stat) /* success/failure */
558 xfs_inobt_block_t *block; /* btree block record/key lives in */
559 xfs_buf_t *bp; /* buffer for block */
560 int error; /* error return value */
561 int i; /* loop index */
562 xfs_inobt_key_t key; /* key value being inserted */
563 xfs_inobt_key_t *kp=NULL; /* pointer to btree keys */
564 xfs_agblock_t nbno; /* block number of allocated block */
565 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
566 xfs_inobt_key_t nkey; /* new key value, from split */
567 xfs_inobt_rec_t nrec; /* new record value, for caller */
568 int numrecs;
569 int optr; /* old ptr value */
570 xfs_inobt_ptr_t *pp; /* pointer to btree addresses */
571 int ptr; /* index in btree block for this rec */
572 xfs_inobt_rec_t *rp=NULL; /* pointer to btree records */
575 * If we made it to the root level, allocate a new root block
576 * and we're done.
578 if (level >= cur->bc_nlevels) {
579 error = xfs_inobt_newroot(cur, &i);
580 *bnop = NULLAGBLOCK;
581 *stat = i;
582 return error;
585 * Make a key out of the record data to be inserted, and save it.
587 key.ir_startino = recp->ir_startino; /* INT_: direct copy */
588 optr = ptr = cur->bc_ptrs[level];
590 * If we're off the left edge, return failure.
592 if (ptr == 0) {
593 *stat = 0;
594 return 0;
597 * Get pointers to the btree buffer and block.
599 bp = cur->bc_bufs[level];
600 block = XFS_BUF_TO_INOBT_BLOCK(bp);
601 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
602 #ifdef DEBUG
603 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
604 return error;
606 * Check that the new entry is being inserted in the right place.
608 if (ptr <= numrecs) {
609 if (level == 0) {
610 rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
611 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
612 } else {
613 kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
614 xfs_btree_check_key(cur->bc_btnum, &key, kp);
617 #endif
618 nbno = NULLAGBLOCK;
619 ncur = (xfs_btree_cur_t *)0;
621 * If the block is full, we can't insert the new entry until we
622 * make the block un-full.
624 if (numrecs == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
626 * First, try shifting an entry to the right neighbor.
628 if ((error = xfs_inobt_rshift(cur, level, &i)))
629 return error;
630 if (i) {
631 /* nothing */
634 * Next, try shifting an entry to the left neighbor.
636 else {
637 if ((error = xfs_inobt_lshift(cur, level, &i)))
638 return error;
639 if (i) {
640 optr = ptr = cur->bc_ptrs[level];
641 } else {
643 * Next, try splitting the current block
644 * in half. If this works we have to
645 * re-set our variables because
646 * we could be in a different block now.
648 if ((error = xfs_inobt_split(cur, level, &nbno,
649 &nkey, &ncur, &i)))
650 return error;
651 if (i) {
652 bp = cur->bc_bufs[level];
653 block = XFS_BUF_TO_INOBT_BLOCK(bp);
654 #ifdef DEBUG
655 if ((error = xfs_btree_check_sblock(cur,
656 block, level, bp)))
657 return error;
658 #endif
659 ptr = cur->bc_ptrs[level];
660 nrec.ir_startino = nkey.ir_startino; /* INT_: direct copy */
661 } else {
663 * Otherwise the insert fails.
665 *stat = 0;
666 return 0;
672 * At this point we know there's room for our new entry in the block
673 * we're pointing at.
675 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
676 if (level > 0) {
678 * It's a non-leaf entry. Make a hole for the new data
679 * in the key and ptr regions of the block.
681 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
682 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
683 #ifdef DEBUG
684 for (i = numrecs; i >= ptr; i--) {
685 if ((error = xfs_btree_check_sptr(cur, INT_GET(pp[i - 1], ARCH_CONVERT), level)))
686 return error;
688 #endif
689 memmove(&kp[ptr], &kp[ptr - 1],
690 (numrecs - ptr + 1) * sizeof(*kp));
691 memmove(&pp[ptr], &pp[ptr - 1],
692 (numrecs - ptr + 1) * sizeof(*pp));
694 * Now stuff the new data in, bump numrecs and log the new data.
696 #ifdef DEBUG
697 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
698 return error;
699 #endif
700 kp[ptr - 1] = key; /* INT_: struct copy */
701 INT_SET(pp[ptr - 1], ARCH_CONVERT, *bnop);
702 numrecs++;
703 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
704 xfs_inobt_log_keys(cur, bp, ptr, numrecs);
705 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs);
706 } else {
708 * It's a leaf entry. Make a hole for the new record.
710 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
711 memmove(&rp[ptr], &rp[ptr - 1],
712 (numrecs - ptr + 1) * sizeof(*rp));
714 * Now stuff the new record in, bump numrecs
715 * and log the new data.
717 rp[ptr - 1] = *recp; /* INT_: struct copy */
718 numrecs++;
719 INT_SET(block->bb_numrecs, ARCH_CONVERT, numrecs);
720 xfs_inobt_log_recs(cur, bp, ptr, numrecs);
723 * Log the new number of records in the btree header.
725 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
726 #ifdef DEBUG
728 * Check that the key/record is in the right place, now.
730 if (ptr < numrecs) {
731 if (level == 0)
732 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
733 rp + ptr);
734 else
735 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
736 kp + ptr);
738 #endif
740 * If we inserted at the start of a block, update the parents' keys.
742 if (optr == 1 && (error = xfs_inobt_updkey(cur, &key, level + 1)))
743 return error;
745 * Return the new block number, if any.
746 * If there is one, give back a record value and a cursor too.
748 *bnop = nbno;
749 if (nbno != NULLAGBLOCK) {
750 *recp = nrec; /* INT_: struct copy */
751 *curp = ncur;
753 *stat = 1;
754 return 0;
758 * Log header fields from a btree block.
760 STATIC void
761 xfs_inobt_log_block(
762 xfs_trans_t *tp, /* transaction pointer */
763 xfs_buf_t *bp, /* buffer containing btree block */
764 int fields) /* mask of fields: XFS_BB_... */
766 int first; /* first byte offset logged */
767 int last; /* last byte offset logged */
768 static const short offsets[] = { /* table of offsets */
769 offsetof(xfs_inobt_block_t, bb_magic),
770 offsetof(xfs_inobt_block_t, bb_level),
771 offsetof(xfs_inobt_block_t, bb_numrecs),
772 offsetof(xfs_inobt_block_t, bb_leftsib),
773 offsetof(xfs_inobt_block_t, bb_rightsib),
774 sizeof(xfs_inobt_block_t)
777 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
778 xfs_trans_log_buf(tp, bp, first, last);
782 * Log keys from a btree block (nonleaf).
784 STATIC void
785 xfs_inobt_log_keys(
786 xfs_btree_cur_t *cur, /* btree cursor */
787 xfs_buf_t *bp, /* buffer containing btree block */
788 int kfirst, /* index of first key to log */
789 int klast) /* index of last key to log */
791 xfs_inobt_block_t *block; /* btree block to log from */
792 int first; /* first byte offset logged */
793 xfs_inobt_key_t *kp; /* key pointer in btree block */
794 int last; /* last byte offset logged */
796 block = XFS_BUF_TO_INOBT_BLOCK(bp);
797 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
798 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
799 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
800 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
804 * Log block pointer fields from a btree block (nonleaf).
806 STATIC void
807 xfs_inobt_log_ptrs(
808 xfs_btree_cur_t *cur, /* btree cursor */
809 xfs_buf_t *bp, /* buffer containing btree block */
810 int pfirst, /* index of first pointer to log */
811 int plast) /* index of last pointer to log */
813 xfs_inobt_block_t *block; /* btree block to log from */
814 int first; /* first byte offset logged */
815 int last; /* last byte offset logged */
816 xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
818 block = XFS_BUF_TO_INOBT_BLOCK(bp);
819 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
820 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
821 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
822 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
826 * Log records from a btree block (leaf).
828 STATIC void
829 xfs_inobt_log_recs(
830 xfs_btree_cur_t *cur, /* btree cursor */
831 xfs_buf_t *bp, /* buffer containing btree block */
832 int rfirst, /* index of first record to log */
833 int rlast) /* index of last record to log */
835 xfs_inobt_block_t *block; /* btree block to log from */
836 int first; /* first byte offset logged */
837 int last; /* last byte offset logged */
838 xfs_inobt_rec_t *rp; /* record pointer for btree block */
840 block = XFS_BUF_TO_INOBT_BLOCK(bp);
841 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
842 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
843 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
844 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
848 * Lookup the record. The cursor is made to point to it, based on dir.
849 * Return 0 if can't find any such record, 1 for success.
851 STATIC int /* error */
852 xfs_inobt_lookup(
853 xfs_btree_cur_t *cur, /* btree cursor */
854 xfs_lookup_t dir, /* <=, ==, or >= */
855 int *stat) /* success/failure */
857 xfs_agblock_t agbno; /* a.g. relative btree block number */
858 xfs_agnumber_t agno; /* allocation group number */
859 xfs_inobt_block_t *block=NULL; /* current btree block */
860 __int64_t diff; /* difference for the current key */
861 int error; /* error return value */
862 int keyno=0; /* current key number */
863 int level; /* level in the btree */
864 xfs_mount_t *mp; /* file system mount point */
867 * Get the allocation group header, and the root block number.
869 mp = cur->bc_mp;
871 xfs_agi_t *agi; /* a.g. inode header */
873 agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
874 agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
875 agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
878 * Iterate over each level in the btree, starting at the root.
879 * For each level above the leaves, find the key we need, based
880 * on the lookup record, then follow the corresponding block
881 * pointer down to the next level.
883 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
884 xfs_buf_t *bp; /* buffer pointer for btree block */
885 xfs_daddr_t d; /* disk address of btree block */
888 * Get the disk address we're looking for.
890 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
892 * If the old buffer at this level is for a different block,
893 * throw it away, otherwise just use it.
895 bp = cur->bc_bufs[level];
896 if (bp && XFS_BUF_ADDR(bp) != d)
897 bp = (xfs_buf_t *)0;
898 if (!bp) {
900 * Need to get a new buffer. Read it, then
901 * set it in the cursor, releasing the old one.
903 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
904 agno, agbno, 0, &bp, XFS_INO_BTREE_REF)))
905 return error;
906 xfs_btree_setbuf(cur, level, bp);
908 * Point to the btree block, now that we have the buffer
910 block = XFS_BUF_TO_INOBT_BLOCK(bp);
911 if ((error = xfs_btree_check_sblock(cur, block, level,
912 bp)))
913 return error;
914 } else
915 block = XFS_BUF_TO_INOBT_BLOCK(bp);
917 * If we already had a key match at a higher level, we know
918 * we need to use the first entry in this block.
920 if (diff == 0)
921 keyno = 1;
923 * Otherwise we need to search this block. Do a binary search.
925 else {
926 int high; /* high entry number */
927 xfs_inobt_key_t *kkbase=NULL;/* base of keys in block */
928 xfs_inobt_rec_t *krbase=NULL;/* base of records in block */
929 int low; /* low entry number */
932 * Get a pointer to keys or records.
934 if (level > 0)
935 kkbase = XFS_INOBT_KEY_ADDR(block, 1, cur);
936 else
937 krbase = XFS_INOBT_REC_ADDR(block, 1, cur);
939 * Set low and high entry numbers, 1-based.
941 low = 1;
942 if (!(high = INT_GET(block->bb_numrecs, ARCH_CONVERT))) {
944 * If the block is empty, the tree must
945 * be an empty leaf.
947 ASSERT(level == 0 && cur->bc_nlevels == 1);
948 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
949 *stat = 0;
950 return 0;
953 * Binary search the block.
955 while (low <= high) {
956 xfs_agino_t startino; /* key value */
959 * keyno is average of low and high.
961 keyno = (low + high) >> 1;
963 * Get startino.
965 if (level > 0) {
966 xfs_inobt_key_t *kkp;
968 kkp = kkbase + keyno - 1;
969 startino = INT_GET(kkp->ir_startino, ARCH_CONVERT);
970 } else {
971 xfs_inobt_rec_t *krp;
973 krp = krbase + keyno - 1;
974 startino = INT_GET(krp->ir_startino, ARCH_CONVERT);
977 * Compute difference to get next direction.
979 diff = (__int64_t)
980 startino - cur->bc_rec.i.ir_startino;
982 * Less than, move right.
984 if (diff < 0)
985 low = keyno + 1;
987 * Greater than, move left.
989 else if (diff > 0)
990 high = keyno - 1;
992 * Equal, we're done.
994 else
995 break;
999 * If there are more levels, set up for the next level
1000 * by getting the block number and filling in the cursor.
1002 if (level > 0) {
1004 * If we moved left, need the previous key number,
1005 * unless there isn't one.
1007 if (diff > 0 && --keyno < 1)
1008 keyno = 1;
1009 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, keyno, cur), ARCH_CONVERT);
1010 #ifdef DEBUG
1011 if ((error = xfs_btree_check_sptr(cur, agbno, level)))
1012 return error;
1013 #endif
1014 cur->bc_ptrs[level] = keyno;
1018 * Done with the search.
1019 * See if we need to adjust the results.
1021 if (dir != XFS_LOOKUP_LE && diff < 0) {
1022 keyno++;
1024 * If ge search and we went off the end of the block, but it's
1025 * not the last block, we're in the wrong block.
1027 if (dir == XFS_LOOKUP_GE &&
1028 keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT) &&
1029 INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1030 int i;
1032 cur->bc_ptrs[0] = keyno;
1033 if ((error = xfs_inobt_increment(cur, 0, &i)))
1034 return error;
1035 ASSERT(i == 1);
1036 *stat = 1;
1037 return 0;
1040 else if (dir == XFS_LOOKUP_LE && diff > 0)
1041 keyno--;
1042 cur->bc_ptrs[0] = keyno;
1044 * Return if we succeeded or not.
1046 if (keyno == 0 || keyno > INT_GET(block->bb_numrecs, ARCH_CONVERT))
1047 *stat = 0;
1048 else
1049 *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
1050 return 0;
1054 * Move 1 record left from cur/level if possible.
1055 * Update cur to reflect the new path.
1057 STATIC int /* error */
1058 xfs_inobt_lshift(
1059 xfs_btree_cur_t *cur, /* btree cursor */
1060 int level, /* level to shift record on */
1061 int *stat) /* success/failure */
1063 int error; /* error return value */
1064 #ifdef DEBUG
1065 int i; /* loop index */
1066 #endif
1067 xfs_inobt_key_t key; /* key value for leaf level upward */
1068 xfs_buf_t *lbp; /* buffer for left neighbor block */
1069 xfs_inobt_block_t *left; /* left neighbor btree block */
1070 xfs_inobt_key_t *lkp=NULL; /* key pointer for left block */
1071 xfs_inobt_ptr_t *lpp; /* address pointer for left block */
1072 xfs_inobt_rec_t *lrp=NULL; /* record pointer for left block */
1073 int nrec; /* new number of left block entries */
1074 xfs_buf_t *rbp; /* buffer for right (current) block */
1075 xfs_inobt_block_t *right; /* right (current) btree block */
1076 xfs_inobt_key_t *rkp=NULL; /* key pointer for right block */
1077 xfs_inobt_ptr_t *rpp=NULL; /* address pointer for right block */
1078 xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
1081 * Set up variables for this block as "right".
1083 rbp = cur->bc_bufs[level];
1084 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1085 #ifdef DEBUG
1086 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1087 return error;
1088 #endif
1090 * If we've got no left sibling then we can't shift an entry left.
1092 if (INT_GET(right->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1093 *stat = 0;
1094 return 0;
1097 * If the cursor entry is the one that would be moved, don't
1098 * do it... it's too complicated.
1100 if (cur->bc_ptrs[level] <= 1) {
1101 *stat = 0;
1102 return 0;
1105 * Set up the left neighbor as "left".
1107 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1108 cur->bc_private.i.agno, INT_GET(right->bb_leftsib, ARCH_CONVERT), 0, &lbp,
1109 XFS_INO_BTREE_REF)))
1110 return error;
1111 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1112 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1113 return error;
1115 * If it's full, it can't take another entry.
1117 if (INT_GET(left->bb_numrecs, ARCH_CONVERT) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
1118 *stat = 0;
1119 return 0;
1121 nrec = INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1;
1123 * If non-leaf, copy a key and a ptr to the left block.
1125 if (level > 0) {
1126 lkp = XFS_INOBT_KEY_ADDR(left, nrec, cur);
1127 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1128 *lkp = *rkp;
1129 xfs_inobt_log_keys(cur, lbp, nrec, nrec);
1130 lpp = XFS_INOBT_PTR_ADDR(left, nrec, cur);
1131 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1132 #ifdef DEBUG
1133 if ((error = xfs_btree_check_sptr(cur, INT_GET(*rpp, ARCH_CONVERT), level)))
1134 return error;
1135 #endif
1136 *lpp = *rpp; /* INT_: no-change copy */
1137 xfs_inobt_log_ptrs(cur, lbp, nrec, nrec);
1140 * If leaf, copy a record to the left block.
1142 else {
1143 lrp = XFS_INOBT_REC_ADDR(left, nrec, cur);
1144 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1145 *lrp = *rrp;
1146 xfs_inobt_log_recs(cur, lbp, nrec, nrec);
1149 * Bump and log left's numrecs, decrement and log right's numrecs.
1151 INT_MOD(left->bb_numrecs, ARCH_CONVERT, +1);
1152 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1153 #ifdef DEBUG
1154 if (level > 0)
1155 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1156 else
1157 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1158 #endif
1159 INT_MOD(right->bb_numrecs, ARCH_CONVERT, -1);
1160 xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1162 * Slide the contents of right down one entry.
1164 if (level > 0) {
1165 #ifdef DEBUG
1166 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1167 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i + 1], ARCH_CONVERT),
1168 level)))
1169 return error;
1171 #endif
1172 memmove(rkp, rkp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1173 memmove(rpp, rpp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1174 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1175 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1176 } else {
1177 memmove(rrp, rrp + 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1178 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1179 key.ir_startino = rrp->ir_startino; /* INT_: direct copy */
1180 rkp = &key;
1183 * Update the parent key values of right.
1185 if ((error = xfs_inobt_updkey(cur, rkp, level + 1)))
1186 return error;
1188 * Slide the cursor value left one.
1190 cur->bc_ptrs[level]--;
1191 *stat = 1;
1192 return 0;
1196 * Allocate a new root block, fill it in.
1198 STATIC int /* error */
1199 xfs_inobt_newroot(
1200 xfs_btree_cur_t *cur, /* btree cursor */
1201 int *stat) /* success/failure */
1203 xfs_agi_t *agi; /* a.g. inode header */
1204 xfs_alloc_arg_t args; /* allocation argument structure */
1205 xfs_inobt_block_t *block; /* one half of the old root block */
1206 xfs_buf_t *bp; /* buffer containing block */
1207 int error; /* error return value */
1208 xfs_inobt_key_t *kp; /* btree key pointer */
1209 xfs_agblock_t lbno; /* left block number */
1210 xfs_buf_t *lbp; /* left buffer pointer */
1211 xfs_inobt_block_t *left; /* left btree block */
1212 xfs_buf_t *nbp; /* new (root) buffer */
1213 xfs_inobt_block_t *new; /* new (root) btree block */
1214 int nptr; /* new value for key index, 1 or 2 */
1215 xfs_inobt_ptr_t *pp; /* btree address pointer */
1216 xfs_agblock_t rbno; /* right block number */
1217 xfs_buf_t *rbp; /* right buffer pointer */
1218 xfs_inobt_block_t *right; /* right btree block */
1219 xfs_inobt_rec_t *rp; /* btree record pointer */
1221 ASSERT(cur->bc_nlevels < XFS_IN_MAXLEVELS(cur->bc_mp));
1224 * Get a block & a buffer.
1226 agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
1227 args.tp = cur->bc_tp;
1228 args.mp = cur->bc_mp;
1229 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno,
1230 INT_GET(agi->agi_root, ARCH_CONVERT));
1231 args.mod = args.minleft = args.alignment = args.total = args.wasdel =
1232 args.isfl = args.userdata = args.minalignslop = 0;
1233 args.minlen = args.maxlen = args.prod = 1;
1234 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1235 if ((error = xfs_alloc_vextent(&args)))
1236 return error;
1238 * None available, we fail.
1240 if (args.fsbno == NULLFSBLOCK) {
1241 *stat = 0;
1242 return 0;
1244 ASSERT(args.len == 1);
1245 nbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
1246 new = XFS_BUF_TO_INOBT_BLOCK(nbp);
1248 * Set the root data in the a.g. inode structure.
1250 INT_SET(agi->agi_root, ARCH_CONVERT, args.agbno);
1251 INT_MOD(agi->agi_level, ARCH_CONVERT, 1);
1252 xfs_ialloc_log_agi(args.tp, cur->bc_private.i.agbp,
1253 XFS_AGI_ROOT | XFS_AGI_LEVEL);
1255 * At the previous root level there are now two blocks: the old
1256 * root, and the new block generated when it was split.
1257 * We don't know which one the cursor is pointing at, so we
1258 * set up variables "left" and "right" for each case.
1260 bp = cur->bc_bufs[cur->bc_nlevels - 1];
1261 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1262 #ifdef DEBUG
1263 if ((error = xfs_btree_check_sblock(cur, block, cur->bc_nlevels - 1, bp)))
1264 return error;
1265 #endif
1266 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1268 * Our block is left, pick up the right block.
1270 lbp = bp;
1271 lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
1272 left = block;
1273 rbno = INT_GET(left->bb_rightsib, ARCH_CONVERT);
1274 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1275 rbno, 0, &rbp, XFS_INO_BTREE_REF)))
1276 return error;
1277 bp = rbp;
1278 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1279 if ((error = xfs_btree_check_sblock(cur, right,
1280 cur->bc_nlevels - 1, rbp)))
1281 return error;
1282 nptr = 1;
1283 } else {
1285 * Our block is right, pick up the left block.
1287 rbp = bp;
1288 rbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(rbp));
1289 right = block;
1290 lbno = INT_GET(right->bb_leftsib, ARCH_CONVERT);
1291 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1292 lbno, 0, &lbp, XFS_INO_BTREE_REF)))
1293 return error;
1294 bp = lbp;
1295 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1296 if ((error = xfs_btree_check_sblock(cur, left,
1297 cur->bc_nlevels - 1, lbp)))
1298 return error;
1299 nptr = 2;
1302 * Fill in the new block's btree header and log it.
1304 INT_SET(new->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1305 INT_SET(new->bb_level, ARCH_CONVERT, (__uint16_t)cur->bc_nlevels);
1306 INT_SET(new->bb_numrecs, ARCH_CONVERT, 2);
1307 INT_SET(new->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
1308 INT_SET(new->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
1309 xfs_inobt_log_block(args.tp, nbp, XFS_BB_ALL_BITS);
1310 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1312 * Fill in the key data in the new root.
1314 kp = XFS_INOBT_KEY_ADDR(new, 1, cur);
1315 if (INT_GET(left->bb_level, ARCH_CONVERT) > 0) {
1316 kp[0] = *XFS_INOBT_KEY_ADDR(left, 1, cur); /* INT_: struct copy */
1317 kp[1] = *XFS_INOBT_KEY_ADDR(right, 1, cur); /* INT_: struct copy */
1318 } else {
1319 rp = XFS_INOBT_REC_ADDR(left, 1, cur);
1320 INT_COPY(kp[0].ir_startino, rp->ir_startino, ARCH_CONVERT);
1321 rp = XFS_INOBT_REC_ADDR(right, 1, cur);
1322 INT_COPY(kp[1].ir_startino, rp->ir_startino, ARCH_CONVERT);
1324 xfs_inobt_log_keys(cur, nbp, 1, 2);
1326 * Fill in the pointer data in the new root.
1328 pp = XFS_INOBT_PTR_ADDR(new, 1, cur);
1329 INT_SET(pp[0], ARCH_CONVERT, lbno);
1330 INT_SET(pp[1], ARCH_CONVERT, rbno);
1331 xfs_inobt_log_ptrs(cur, nbp, 1, 2);
1333 * Fix up the cursor.
1335 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1336 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1337 cur->bc_nlevels++;
1338 *stat = 1;
1339 return 0;
1343 * Move 1 record right from cur/level if possible.
1344 * Update cur to reflect the new path.
1346 STATIC int /* error */
1347 xfs_inobt_rshift(
1348 xfs_btree_cur_t *cur, /* btree cursor */
1349 int level, /* level to shift record on */
1350 int *stat) /* success/failure */
1352 int error; /* error return value */
1353 int i; /* loop index */
1354 xfs_inobt_key_t key; /* key value for leaf level upward */
1355 xfs_buf_t *lbp; /* buffer for left (current) block */
1356 xfs_inobt_block_t *left; /* left (current) btree block */
1357 xfs_inobt_key_t *lkp; /* key pointer for left block */
1358 xfs_inobt_ptr_t *lpp; /* address pointer for left block */
1359 xfs_inobt_rec_t *lrp; /* record pointer for left block */
1360 xfs_buf_t *rbp; /* buffer for right neighbor block */
1361 xfs_inobt_block_t *right; /* right neighbor btree block */
1362 xfs_inobt_key_t *rkp; /* key pointer for right block */
1363 xfs_inobt_ptr_t *rpp; /* address pointer for right block */
1364 xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
1365 xfs_btree_cur_t *tcur; /* temporary cursor */
1368 * Set up variables for this block as "left".
1370 lbp = cur->bc_bufs[level];
1371 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1372 #ifdef DEBUG
1373 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1374 return error;
1375 #endif
1377 * If we've got no right sibling then we can't shift an entry right.
1379 if (INT_GET(left->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1380 *stat = 0;
1381 return 0;
1384 * If the cursor entry is the one that would be moved, don't
1385 * do it... it's too complicated.
1387 if (cur->bc_ptrs[level] >= INT_GET(left->bb_numrecs, ARCH_CONVERT)) {
1388 *stat = 0;
1389 return 0;
1392 * Set up the right neighbor as "right".
1394 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1395 cur->bc_private.i.agno, INT_GET(left->bb_rightsib, ARCH_CONVERT), 0, &rbp,
1396 XFS_INO_BTREE_REF)))
1397 return error;
1398 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1399 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
1400 return error;
1402 * If it's full, it can't take another entry.
1404 if (INT_GET(right->bb_numrecs, ARCH_CONVERT) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
1405 *stat = 0;
1406 return 0;
1409 * Make a hole at the start of the right neighbor block, then
1410 * copy the last left block entry to the hole.
1412 if (level > 0) {
1413 lkp = XFS_INOBT_KEY_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1414 lpp = XFS_INOBT_PTR_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1415 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1416 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1417 #ifdef DEBUG
1418 for (i = INT_GET(right->bb_numrecs, ARCH_CONVERT) - 1; i >= 0; i--) {
1419 if ((error = xfs_btree_check_sptr(cur, INT_GET(rpp[i], ARCH_CONVERT), level)))
1420 return error;
1422 #endif
1423 memmove(rkp + 1, rkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1424 memmove(rpp + 1, rpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1425 #ifdef DEBUG
1426 if ((error = xfs_btree_check_sptr(cur, INT_GET(*lpp, ARCH_CONVERT), level)))
1427 return error;
1428 #endif
1429 *rkp = *lkp; /* INT_: no change copy */
1430 *rpp = *lpp; /* INT_: no change copy */
1431 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1432 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1433 } else {
1434 lrp = XFS_INOBT_REC_ADDR(left, INT_GET(left->bb_numrecs, ARCH_CONVERT), cur);
1435 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1436 memmove(rrp + 1, rrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1437 *rrp = *lrp;
1438 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1);
1439 key.ir_startino = rrp->ir_startino; /* INT_: direct copy */
1440 rkp = &key;
1443 * Decrement and log left's numrecs, bump and log right's numrecs.
1445 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -1);
1446 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1447 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1448 #ifdef DEBUG
1449 if (level > 0)
1450 xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
1451 else
1452 xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
1453 #endif
1454 xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1456 * Using a temporary cursor, update the parent key values of the
1457 * block on the right.
1459 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
1460 return error;
1461 xfs_btree_lastrec(tcur, level);
1462 if ((error = xfs_inobt_increment(tcur, level, &i)) ||
1463 (error = xfs_inobt_updkey(tcur, rkp, level + 1))) {
1464 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1465 return error;
1467 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1468 *stat = 1;
1469 return 0;
1473 * Split cur/level block in half.
1474 * Return new block number and its first record (to be inserted into parent).
1476 STATIC int /* error */
1477 xfs_inobt_split(
1478 xfs_btree_cur_t *cur, /* btree cursor */
1479 int level, /* level to split */
1480 xfs_agblock_t *bnop, /* output: block number allocated */
1481 xfs_inobt_key_t *keyp, /* output: first key of new block */
1482 xfs_btree_cur_t **curp, /* output: new cursor */
1483 int *stat) /* success/failure */
1485 xfs_alloc_arg_t args; /* allocation argument structure */
1486 int error; /* error return value */
1487 int i; /* loop index/record number */
1488 xfs_agblock_t lbno; /* left (current) block number */
1489 xfs_buf_t *lbp; /* buffer for left block */
1490 xfs_inobt_block_t *left; /* left (current) btree block */
1491 xfs_inobt_key_t *lkp; /* left btree key pointer */
1492 xfs_inobt_ptr_t *lpp; /* left btree address pointer */
1493 xfs_inobt_rec_t *lrp; /* left btree record pointer */
1494 xfs_buf_t *rbp; /* buffer for right block */
1495 xfs_inobt_block_t *right; /* right (new) btree block */
1496 xfs_inobt_key_t *rkp; /* right btree key pointer */
1497 xfs_inobt_ptr_t *rpp; /* right btree address pointer */
1498 xfs_inobt_rec_t *rrp; /* right btree record pointer */
1501 * Set up left block (current one).
1503 lbp = cur->bc_bufs[level];
1504 args.tp = cur->bc_tp;
1505 args.mp = cur->bc_mp;
1506 lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
1508 * Allocate the new block.
1509 * If we can't do it, we're toast. Give up.
1511 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno, lbno);
1512 args.mod = args.minleft = args.alignment = args.total = args.wasdel =
1513 args.isfl = args.userdata = args.minalignslop = 0;
1514 args.minlen = args.maxlen = args.prod = 1;
1515 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1516 if ((error = xfs_alloc_vextent(&args)))
1517 return error;
1518 if (args.fsbno == NULLFSBLOCK) {
1519 *stat = 0;
1520 return 0;
1522 ASSERT(args.len == 1);
1523 rbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
1525 * Set up the new block as "right".
1527 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
1529 * "Left" is the current (according to the cursor) block.
1531 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
1532 #ifdef DEBUG
1533 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1534 return error;
1535 #endif
1537 * Fill in the btree header for the new block.
1539 INT_SET(right->bb_magic, ARCH_CONVERT, xfs_magics[cur->bc_btnum]);
1540 right->bb_level = left->bb_level; /* INT_: direct copy */
1541 INT_SET(right->bb_numrecs, ARCH_CONVERT, (__uint16_t)(INT_GET(left->bb_numrecs, ARCH_CONVERT) / 2));
1543 * Make sure that if there's an odd number of entries now, that
1544 * each new block will have the same number of entries.
1546 if ((INT_GET(left->bb_numrecs, ARCH_CONVERT) & 1) &&
1547 cur->bc_ptrs[level] <= INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1)
1548 INT_MOD(right->bb_numrecs, ARCH_CONVERT, +1);
1549 i = INT_GET(left->bb_numrecs, ARCH_CONVERT) - INT_GET(right->bb_numrecs, ARCH_CONVERT) + 1;
1551 * For non-leaf blocks, copy keys and addresses over to the new block.
1553 if (level > 0) {
1554 lkp = XFS_INOBT_KEY_ADDR(left, i, cur);
1555 lpp = XFS_INOBT_PTR_ADDR(left, i, cur);
1556 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
1557 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
1558 #ifdef DEBUG
1559 for (i = 0; i < INT_GET(right->bb_numrecs, ARCH_CONVERT); i++) {
1560 if ((error = xfs_btree_check_sptr(cur, INT_GET(lpp[i], ARCH_CONVERT), level)))
1561 return error;
1563 #endif
1564 memcpy(rkp, lkp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rkp));
1565 memcpy(rpp, lpp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rpp));
1566 xfs_inobt_log_keys(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1567 xfs_inobt_log_ptrs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1568 *keyp = *rkp;
1571 * For leaf blocks, copy records over to the new block.
1573 else {
1574 lrp = XFS_INOBT_REC_ADDR(left, i, cur);
1575 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
1576 memcpy(rrp, lrp, INT_GET(right->bb_numrecs, ARCH_CONVERT) * sizeof(*rrp));
1577 xfs_inobt_log_recs(cur, rbp, 1, INT_GET(right->bb_numrecs, ARCH_CONVERT));
1578 keyp->ir_startino = rrp->ir_startino; /* INT_: direct copy */
1581 * Find the left block number by looking in the buffer.
1582 * Adjust numrecs, sibling pointers.
1584 INT_MOD(left->bb_numrecs, ARCH_CONVERT, -(INT_GET(right->bb_numrecs, ARCH_CONVERT)));
1585 right->bb_rightsib = left->bb_rightsib; /* INT_: direct copy */
1586 INT_SET(left->bb_rightsib, ARCH_CONVERT, args.agbno);
1587 INT_SET(right->bb_leftsib, ARCH_CONVERT, lbno);
1588 xfs_inobt_log_block(args.tp, rbp, XFS_BB_ALL_BITS);
1589 xfs_inobt_log_block(args.tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1591 * If there's a block to the new block's right, make that block
1592 * point back to right instead of to left.
1594 if (INT_GET(right->bb_rightsib, ARCH_CONVERT) != NULLAGBLOCK) {
1595 xfs_inobt_block_t *rrblock; /* rr btree block */
1596 xfs_buf_t *rrbp; /* buffer for rrblock */
1598 if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
1599 INT_GET(right->bb_rightsib, ARCH_CONVERT), 0, &rrbp,
1600 XFS_INO_BTREE_REF)))
1601 return error;
1602 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
1603 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1604 return error;
1605 INT_SET(rrblock->bb_leftsib, ARCH_CONVERT, args.agbno);
1606 xfs_inobt_log_block(args.tp, rrbp, XFS_BB_LEFTSIB);
1609 * If the cursor is really in the right block, move it there.
1610 * If it's just pointing past the last entry in left, then we'll
1611 * insert there, so don't change anything in that case.
1613 if (cur->bc_ptrs[level] > INT_GET(left->bb_numrecs, ARCH_CONVERT) + 1) {
1614 xfs_btree_setbuf(cur, level, rbp);
1615 cur->bc_ptrs[level] -= INT_GET(left->bb_numrecs, ARCH_CONVERT);
1618 * If there are more levels, we'll need another cursor which refers
1619 * the right block, no matter where this cursor was.
1621 if (level + 1 < cur->bc_nlevels) {
1622 if ((error = xfs_btree_dup_cursor(cur, curp)))
1623 return error;
1624 (*curp)->bc_ptrs[level + 1]++;
1626 *bnop = args.agbno;
1627 *stat = 1;
1628 return 0;
1632 * Update keys at all levels from here to the root along the cursor's path.
1634 STATIC int /* error */
1635 xfs_inobt_updkey(
1636 xfs_btree_cur_t *cur, /* btree cursor */
1637 xfs_inobt_key_t *keyp, /* new key value to update to */
1638 int level) /* starting level for update */
1640 int ptr; /* index of key in block */
1643 * Go up the tree from this level toward the root.
1644 * At each level, update the key value to the value input.
1645 * Stop when we reach a level where the cursor isn't pointing
1646 * at the first entry in the block.
1648 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1649 xfs_buf_t *bp; /* buffer for block */
1650 xfs_inobt_block_t *block; /* btree block */
1651 #ifdef DEBUG
1652 int error; /* error return value */
1653 #endif
1654 xfs_inobt_key_t *kp; /* ptr to btree block keys */
1656 bp = cur->bc_bufs[level];
1657 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1658 #ifdef DEBUG
1659 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1660 return error;
1661 #endif
1662 ptr = cur->bc_ptrs[level];
1663 kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
1664 *kp = *keyp;
1665 xfs_inobt_log_keys(cur, bp, ptr, ptr);
1667 return 0;
1671 * Externally visible routines.
1675 * Decrement cursor by one record at the level.
1676 * For nonzero levels the leaf-ward information is untouched.
1678 int /* error */
1679 xfs_inobt_decrement(
1680 xfs_btree_cur_t *cur, /* btree cursor */
1681 int level, /* level in btree, 0 is leaf */
1682 int *stat) /* success/failure */
1684 xfs_inobt_block_t *block; /* btree block */
1685 int error;
1686 int lev; /* btree level */
1688 ASSERT(level < cur->bc_nlevels);
1690 * Read-ahead to the left at this level.
1692 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1694 * Decrement the ptr at this level. If we're still in the block
1695 * then we're done.
1697 if (--cur->bc_ptrs[level] > 0) {
1698 *stat = 1;
1699 return 0;
1702 * Get a pointer to the btree block.
1704 block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[level]);
1705 #ifdef DEBUG
1706 if ((error = xfs_btree_check_sblock(cur, block, level,
1707 cur->bc_bufs[level])))
1708 return error;
1709 #endif
1711 * If we just went off the left edge of the tree, return failure.
1713 if (INT_GET(block->bb_leftsib, ARCH_CONVERT) == NULLAGBLOCK) {
1714 *stat = 0;
1715 return 0;
1718 * March up the tree decrementing pointers.
1719 * Stop when we don't go off the left edge of a block.
1721 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1722 if (--cur->bc_ptrs[lev] > 0)
1723 break;
1725 * Read-ahead the left block, we're going to read it
1726 * in the next loop.
1728 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1731 * If we went off the root then we are seriously confused.
1733 ASSERT(lev < cur->bc_nlevels);
1735 * Now walk back down the tree, fixing up the cursor's buffer
1736 * pointers and key numbers.
1738 for (block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
1739 xfs_agblock_t agbno; /* block number of btree block */
1740 xfs_buf_t *bp; /* buffer containing btree block */
1742 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1743 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1744 cur->bc_private.i.agno, agbno, 0, &bp,
1745 XFS_INO_BTREE_REF)))
1746 return error;
1747 lev--;
1748 xfs_btree_setbuf(cur, lev, bp);
1749 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1750 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1751 return error;
1752 cur->bc_ptrs[lev] = INT_GET(block->bb_numrecs, ARCH_CONVERT);
1754 *stat = 1;
1755 return 0;
1759 * Delete the record pointed to by cur.
1760 * The cursor refers to the place where the record was (could be inserted)
1761 * when the operation returns.
1763 int /* error */
1764 xfs_inobt_delete(
1765 xfs_btree_cur_t *cur, /* btree cursor */
1766 int *stat) /* success/failure */
1768 int error;
1769 int i; /* result code */
1770 int level; /* btree level */
1773 * Go up the tree, starting at leaf level.
1774 * If 2 is returned then a join was done; go to the next level.
1775 * Otherwise we are done.
1777 for (level = 0, i = 2; i == 2; level++) {
1778 if ((error = xfs_inobt_delrec(cur, level, &i)))
1779 return error;
1781 if (i == 0) {
1782 for (level = 1; level < cur->bc_nlevels; level++) {
1783 if (cur->bc_ptrs[level] == 0) {
1784 if ((error = xfs_inobt_decrement(cur, level, &i)))
1785 return error;
1786 break;
1790 *stat = i;
1791 return 0;
1796 * Get the data from the pointed-to record.
1798 int /* error */
1799 xfs_inobt_get_rec(
1800 xfs_btree_cur_t *cur, /* btree cursor */
1801 xfs_agino_t *ino, /* output: starting inode of chunk */
1802 __int32_t *fcnt, /* output: number of free inodes */
1803 xfs_inofree_t *free, /* output: free inode mask */
1804 int *stat) /* output: success/failure */
1806 xfs_inobt_block_t *block; /* btree block */
1807 xfs_buf_t *bp; /* buffer containing btree block */
1808 #ifdef DEBUG
1809 int error; /* error return value */
1810 #endif
1811 int ptr; /* record number */
1812 xfs_inobt_rec_t *rec; /* record data */
1814 bp = cur->bc_bufs[0];
1815 ptr = cur->bc_ptrs[0];
1816 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1817 #ifdef DEBUG
1818 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
1819 return error;
1820 #endif
1822 * Off the right end or left end, return failure.
1824 if (ptr > INT_GET(block->bb_numrecs, ARCH_CONVERT) || ptr <= 0) {
1825 *stat = 0;
1826 return 0;
1829 * Point to the record and extract its data.
1831 rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
1832 *ino = INT_GET(rec->ir_startino, ARCH_CONVERT);
1833 *fcnt = INT_GET(rec->ir_freecount, ARCH_CONVERT);
1834 *free = INT_GET(rec->ir_free, ARCH_CONVERT);
1835 *stat = 1;
1836 return 0;
1840 * Increment cursor by one record at the level.
1841 * For nonzero levels the leaf-ward information is untouched.
1843 int /* error */
1844 xfs_inobt_increment(
1845 xfs_btree_cur_t *cur, /* btree cursor */
1846 int level, /* level in btree, 0 is leaf */
1847 int *stat) /* success/failure */
1849 xfs_inobt_block_t *block; /* btree block */
1850 xfs_buf_t *bp; /* buffer containing btree block */
1851 int error; /* error return value */
1852 int lev; /* btree level */
1854 ASSERT(level < cur->bc_nlevels);
1856 * Read-ahead to the right at this level.
1858 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1860 * Get a pointer to the btree block.
1862 bp = cur->bc_bufs[level];
1863 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1864 #ifdef DEBUG
1865 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
1866 return error;
1867 #endif
1869 * Increment the ptr at this level. If we're still in the block
1870 * then we're done.
1872 if (++cur->bc_ptrs[level] <= INT_GET(block->bb_numrecs, ARCH_CONVERT)) {
1873 *stat = 1;
1874 return 0;
1877 * If we just went off the right edge of the tree, return failure.
1879 if (INT_GET(block->bb_rightsib, ARCH_CONVERT) == NULLAGBLOCK) {
1880 *stat = 0;
1881 return 0;
1884 * March up the tree incrementing pointers.
1885 * Stop when we don't go off the right edge of a block.
1887 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1888 bp = cur->bc_bufs[lev];
1889 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1890 #ifdef DEBUG
1891 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1892 return error;
1893 #endif
1894 if (++cur->bc_ptrs[lev] <= INT_GET(block->bb_numrecs, ARCH_CONVERT))
1895 break;
1897 * Read-ahead the right block, we're going to read it
1898 * in the next loop.
1900 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1903 * If we went off the root then we are seriously confused.
1905 ASSERT(lev < cur->bc_nlevels);
1907 * Now walk back down the tree, fixing up the cursor's buffer
1908 * pointers and key numbers.
1910 for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_INOBT_BLOCK(bp);
1911 lev > level; ) {
1912 xfs_agblock_t agbno; /* block number of btree block */
1914 agbno = INT_GET(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur), ARCH_CONVERT);
1915 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1916 cur->bc_private.i.agno, agbno, 0, &bp,
1917 XFS_INO_BTREE_REF)))
1918 return error;
1919 lev--;
1920 xfs_btree_setbuf(cur, lev, bp);
1921 block = XFS_BUF_TO_INOBT_BLOCK(bp);
1922 if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
1923 return error;
1924 cur->bc_ptrs[lev] = 1;
1926 *stat = 1;
1927 return 0;
1931 * Insert the current record at the point referenced by cur.
1932 * The cursor may be inconsistent on return if splits have been done.
1934 int /* error */
1935 xfs_inobt_insert(
1936 xfs_btree_cur_t *cur, /* btree cursor */
1937 int *stat) /* success/failure */
1939 int error; /* error return value */
1940 int i; /* result value, 0 for failure */
1941 int level; /* current level number in btree */
1942 xfs_agblock_t nbno; /* new block number (split result) */
1943 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1944 xfs_inobt_rec_t nrec; /* record being inserted this level */
1945 xfs_btree_cur_t *pcur; /* previous level's cursor */
1947 level = 0;
1948 nbno = NULLAGBLOCK;
1949 INT_SET(nrec.ir_startino, ARCH_CONVERT, cur->bc_rec.i.ir_startino);
1950 INT_SET(nrec.ir_freecount, ARCH_CONVERT, cur->bc_rec.i.ir_freecount);
1951 INT_SET(nrec.ir_free, ARCH_CONVERT, cur->bc_rec.i.ir_free);
1952 ncur = (xfs_btree_cur_t *)0;
1953 pcur = cur;
1955 * Loop going up the tree, starting at the leaf level.
1956 * Stop when we don't get a split block, that must mean that
1957 * the insert is finished with this level.
1959 do {
1961 * Insert nrec/nbno into this level of the tree.
1962 * Note if we fail, nbno will be null.
1964 if ((error = xfs_inobt_insrec(pcur, level++, &nbno, &nrec, &ncur,
1965 &i))) {
1966 if (pcur != cur)
1967 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1968 return error;
1971 * See if the cursor we just used is trash.
1972 * Can't trash the caller's cursor, but otherwise we should
1973 * if ncur is a new cursor or we're about to be done.
1975 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1976 cur->bc_nlevels = pcur->bc_nlevels;
1977 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1980 * If we got a new cursor, switch to it.
1982 if (ncur) {
1983 pcur = ncur;
1984 ncur = (xfs_btree_cur_t *)0;
1986 } while (nbno != NULLAGBLOCK);
1987 *stat = i;
1988 return 0;
1992 * Lookup the record equal to ino in the btree given by cur.
1994 int /* error */
1995 xfs_inobt_lookup_eq(
1996 xfs_btree_cur_t *cur, /* btree cursor */
1997 xfs_agino_t ino, /* starting inode of chunk */
1998 __int32_t fcnt, /* free inode count */
1999 xfs_inofree_t free, /* free inode mask */
2000 int *stat) /* success/failure */
2002 cur->bc_rec.i.ir_startino = ino;
2003 cur->bc_rec.i.ir_freecount = fcnt;
2004 cur->bc_rec.i.ir_free = free;
2005 return xfs_inobt_lookup(cur, XFS_LOOKUP_EQ, stat);
2009 * Lookup the first record greater than or equal to ino
2010 * in the btree given by cur.
2012 int /* error */
2013 xfs_inobt_lookup_ge(
2014 xfs_btree_cur_t *cur, /* btree cursor */
2015 xfs_agino_t ino, /* starting inode of chunk */
2016 __int32_t fcnt, /* free inode count */
2017 xfs_inofree_t free, /* free inode mask */
2018 int *stat) /* success/failure */
2020 cur->bc_rec.i.ir_startino = ino;
2021 cur->bc_rec.i.ir_freecount = fcnt;
2022 cur->bc_rec.i.ir_free = free;
2023 return xfs_inobt_lookup(cur, XFS_LOOKUP_GE, stat);
2027 * Lookup the first record less than or equal to ino
2028 * in the btree given by cur.
2030 int /* error */
2031 xfs_inobt_lookup_le(
2032 xfs_btree_cur_t *cur, /* btree cursor */
2033 xfs_agino_t ino, /* starting inode of chunk */
2034 __int32_t fcnt, /* free inode count */
2035 xfs_inofree_t free, /* free inode mask */
2036 int *stat) /* success/failure */
2038 cur->bc_rec.i.ir_startino = ino;
2039 cur->bc_rec.i.ir_freecount = fcnt;
2040 cur->bc_rec.i.ir_free = free;
2041 return xfs_inobt_lookup(cur, XFS_LOOKUP_LE, stat);
2045 * Update the record referred to by cur, to the value given
2046 * by [ino, fcnt, free].
2047 * This either works (return 0) or gets an EFSCORRUPTED error.
2049 int /* error */
2050 xfs_inobt_update(
2051 xfs_btree_cur_t *cur, /* btree cursor */
2052 xfs_agino_t ino, /* starting inode of chunk */
2053 __int32_t fcnt, /* free inode count */
2054 xfs_inofree_t free) /* free inode mask */
2056 xfs_inobt_block_t *block; /* btree block to update */
2057 xfs_buf_t *bp; /* buffer containing btree block */
2058 int error; /* error return value */
2059 int ptr; /* current record number (updating) */
2060 xfs_inobt_rec_t *rp; /* pointer to updated record */
2063 * Pick up the current block.
2065 bp = cur->bc_bufs[0];
2066 block = XFS_BUF_TO_INOBT_BLOCK(bp);
2067 #ifdef DEBUG
2068 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
2069 return error;
2070 #endif
2072 * Get the address of the rec to be updated.
2074 ptr = cur->bc_ptrs[0];
2075 rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
2077 * Fill in the new contents and log them.
2079 INT_SET(rp->ir_startino, ARCH_CONVERT, ino);
2080 INT_SET(rp->ir_freecount, ARCH_CONVERT, fcnt);
2081 INT_SET(rp->ir_free, ARCH_CONVERT, free);
2082 xfs_inobt_log_recs(cur, bp, ptr, ptr);
2084 * Updating first record in leaf. Pass new key value up to our parent.
2086 if (ptr == 1) {
2087 xfs_inobt_key_t key; /* key containing [ino] */
2089 INT_SET(key.ir_startino, ARCH_CONVERT, ino);
2090 if ((error = xfs_inobt_updkey(cur, &key, 1)))
2091 return error;
2093 return 0;