2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_btree_trace.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_alloc.h"
41 #include "xfs_error.h"
43 STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
44 STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
45 STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
46 STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
49 * Single level of the xfs_inobt_delete record deletion routine.
50 * Delete record pointed to by cur/level.
51 * Remove the record from its block then rebalance the tree.
52 * Return 0 for error, 1 for done, 2 to go on to the next level.
54 STATIC int /* error */
56 xfs_btree_cur_t *cur, /* btree cursor */
57 int level, /* level removing record from */
58 int *stat) /* fail/done/go-on */
60 xfs_buf_t *agbp; /* buffer for a.g. inode header */
61 xfs_mount_t *mp; /* mount structure */
62 xfs_agi_t *agi; /* allocation group inode header */
63 xfs_inobt_block_t *block; /* btree block record/key lives in */
64 xfs_agblock_t bno; /* btree block number */
65 xfs_buf_t *bp; /* buffer for block */
66 int error; /* error return value */
67 int i; /* loop index */
68 xfs_inobt_key_t key; /* kp points here if block is level 0 */
69 xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
70 xfs_agblock_t lbno; /* left block's block number */
71 xfs_buf_t *lbp; /* left block's buffer pointer */
72 xfs_inobt_block_t *left; /* left btree block */
73 xfs_inobt_key_t *lkp; /* left block key pointer */
74 xfs_inobt_ptr_t *lpp; /* left block address pointer */
75 int lrecs = 0; /* number of records in left block */
76 xfs_inobt_rec_t *lrp; /* left block record pointer */
77 xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
78 int ptr; /* index in btree block for this rec */
79 xfs_agblock_t rbno; /* right block's block number */
80 xfs_buf_t *rbp; /* right block's buffer pointer */
81 xfs_inobt_block_t *right; /* right btree block */
82 xfs_inobt_key_t *rkp; /* right block key pointer */
83 xfs_inobt_rec_t *rp; /* pointer to btree records */
84 xfs_inobt_ptr_t *rpp; /* right block address pointer */
85 int rrecs = 0; /* number of records in right block */
87 xfs_inobt_rec_t *rrp; /* right block record pointer */
88 xfs_btree_cur_t *tcur; /* temporary btree cursor */
93 * Get the index of the entry being deleted, check for nothing there.
95 ptr = cur->bc_ptrs[level];
102 * Get the buffer & block containing the record or key/ptr.
104 bp = cur->bc_bufs[level];
105 block = XFS_BUF_TO_INOBT_BLOCK(bp);
107 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
111 * Fail if we're off the end of the block.
114 numrecs = be16_to_cpu(block->bb_numrecs);
120 * It's a nonleaf. Excise the key and ptr being deleted, by
121 * sliding the entries past them down one.
122 * Log the changed areas of the block.
125 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
126 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
128 for (i = ptr; i < numrecs; i++) {
129 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i]), level)))
134 memmove(&kp[ptr - 1], &kp[ptr],
135 (numrecs - ptr) * sizeof(*kp));
136 memmove(&pp[ptr - 1], &pp[ptr],
137 (numrecs - ptr) * sizeof(*kp));
138 xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
139 xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
143 * It's a leaf. Excise the record being deleted, by sliding the
144 * entries past it down one. Log the changed areas of the block.
147 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
149 memmove(&rp[ptr - 1], &rp[ptr],
150 (numrecs - ptr) * sizeof(*rp));
151 xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
154 * If it's the first record in the block, we'll need a key
155 * structure to pass up to the next level (updkey).
158 key.ir_startino = rp->ir_startino;
163 * Decrement and log the number of entries in the block.
166 block->bb_numrecs = cpu_to_be16(numrecs);
167 xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
169 * Is this the root level? If so, we're almost done.
171 if (level == cur->bc_nlevels - 1) {
173 * If this is the root level,
174 * and there's only one entry left,
175 * and it's NOT the leaf level,
176 * then we can get rid of this level.
178 if (numrecs == 1 && level > 0) {
179 agbp = cur->bc_private.a.agbp;
180 agi = XFS_BUF_TO_AGI(agbp);
182 * pp is still set to the first pointer in the block.
183 * Make it the new root of the btree.
185 bno = be32_to_cpu(agi->agi_root);
187 be32_add_cpu(&agi->agi_level, -1);
191 if ((error = xfs_free_extent(cur->bc_tp,
192 XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, bno), 1)))
194 xfs_trans_binval(cur->bc_tp, bp);
195 xfs_ialloc_log_agi(cur->bc_tp, agbp,
196 XFS_AGI_ROOT | XFS_AGI_LEVEL);
198 * Update the cursor so there's one fewer level.
200 cur->bc_bufs[level] = NULL;
202 } else if (level > 0 &&
203 (error = xfs_btree_decrement(cur, level, &i)))
209 * If we deleted the leftmost entry in the block, update the
210 * key values above us in the tree.
212 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1)))
215 * If the number of records remaining in the block is at least
216 * the minimum, we're done.
218 if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
220 (error = xfs_btree_decrement(cur, level, &i)))
226 * Otherwise, we have to move some records around to keep the
227 * tree balanced. Look at the left and right sibling blocks to
228 * see if we can re-balance by moving only one record.
230 rbno = be32_to_cpu(block->bb_rightsib);
231 lbno = be32_to_cpu(block->bb_leftsib);
233 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
235 * Duplicate the cursor so our btree manipulations here won't
236 * disrupt the next level up.
238 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
241 * If there's a right sibling, see if it's ok to shift an entry
244 if (rbno != NULLAGBLOCK) {
246 * Move the temp cursor to the last entry in the next block.
247 * Actually any entry but the first would suffice.
249 i = xfs_btree_lastrec(tcur, level);
250 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
251 if ((error = xfs_btree_increment(tcur, level, &i)))
253 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
254 i = xfs_btree_lastrec(tcur, level);
255 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
257 * Grab a pointer to the block.
259 rbp = tcur->bc_bufs[level];
260 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
262 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
266 * Grab the current block number, for future use.
268 bno = be32_to_cpu(right->bb_leftsib);
270 * If right block is full enough so that removing one entry
271 * won't make it too empty, and left-shifting an entry out
272 * of right to us works, we're done.
274 if (be16_to_cpu(right->bb_numrecs) - 1 >=
275 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
276 if ((error = xfs_btree_lshift(tcur, level, &i)))
279 ASSERT(be16_to_cpu(block->bb_numrecs) >=
280 XFS_INOBT_BLOCK_MINRECS(level, cur));
281 xfs_btree_del_cursor(tcur,
284 (error = xfs_btree_decrement(cur, level,
292 * Otherwise, grab the number of records in right for
293 * future reference, and fix up the temp cursor to point
294 * to our block again (last record).
296 rrecs = be16_to_cpu(right->bb_numrecs);
297 if (lbno != NULLAGBLOCK) {
298 xfs_btree_firstrec(tcur, level);
299 if ((error = xfs_btree_decrement(tcur, level, &i)))
304 * If there's a left sibling, see if it's ok to shift an entry
307 if (lbno != NULLAGBLOCK) {
309 * Move the temp cursor to the first entry in the
312 xfs_btree_firstrec(tcur, level);
313 if ((error = xfs_btree_decrement(tcur, level, &i)))
315 xfs_btree_firstrec(tcur, level);
317 * Grab a pointer to the block.
319 lbp = tcur->bc_bufs[level];
320 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
322 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
326 * Grab the current block number, for future use.
328 bno = be32_to_cpu(left->bb_rightsib);
330 * If left block is full enough so that removing one entry
331 * won't make it too empty, and right-shifting an entry out
332 * of left to us works, we're done.
334 if (be16_to_cpu(left->bb_numrecs) - 1 >=
335 XFS_INOBT_BLOCK_MINRECS(level, cur)) {
336 if ((error = xfs_btree_rshift(tcur, level, &i)))
339 ASSERT(be16_to_cpu(block->bb_numrecs) >=
340 XFS_INOBT_BLOCK_MINRECS(level, cur));
341 xfs_btree_del_cursor(tcur,
350 * Otherwise, grab the number of records in right for
353 lrecs = be16_to_cpu(left->bb_numrecs);
356 * Delete the temp cursor, we're done with it.
358 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
360 * If here, we need to do a join to keep the tree balanced.
362 ASSERT(bno != NULLAGBLOCK);
364 * See if we can join with the left neighbor block.
366 if (lbno != NULLAGBLOCK &&
367 lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
369 * Set "right" to be the starting block,
370 * "left" to be the left neighbor.
374 rrecs = be16_to_cpu(right->bb_numrecs);
376 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
377 cur->bc_private.a.agno, lbno, 0, &lbp,
380 left = XFS_BUF_TO_INOBT_BLOCK(lbp);
381 lrecs = be16_to_cpu(left->bb_numrecs);
382 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
386 * If that won't work, see if we can join with the right neighbor block.
388 else if (rbno != NULLAGBLOCK &&
389 rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
391 * Set "left" to be the starting block,
392 * "right" to be the right neighbor.
396 lrecs = be16_to_cpu(left->bb_numrecs);
398 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
399 cur->bc_private.a.agno, rbno, 0, &rbp,
402 right = XFS_BUF_TO_INOBT_BLOCK(rbp);
403 rrecs = be16_to_cpu(right->bb_numrecs);
404 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
408 * Otherwise, we can't fix the imbalance.
409 * Just return. This is probably a logic error, but it's not fatal.
412 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
418 * We're now going to join "left" and "right" by moving all the stuff
419 * in "right" to "left" and deleting "right".
423 * It's a non-leaf. Move keys and pointers.
425 lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
426 lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
427 rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
428 rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
430 for (i = 0; i < rrecs; i++) {
431 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
435 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
436 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
437 xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
438 xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
441 * It's a leaf. Move records.
443 lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
444 rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
445 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
446 xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
449 * If we joined with the left neighbor, set the buffer in the
450 * cursor to the left block, and fix up the index.
453 xfs_btree_setbuf(cur, level, lbp);
454 cur->bc_ptrs[level] += lrecs;
457 * If we joined with the right neighbor and there's a level above
458 * us, increment the cursor at that level.
460 else if (level + 1 < cur->bc_nlevels &&
461 (error = xfs_btree_increment(cur, level + 1, &i)))
464 * Fix up the number of records in the surviving block.
467 left->bb_numrecs = cpu_to_be16(lrecs);
469 * Fix up the right block pointer in the surviving block, and log it.
471 left->bb_rightsib = right->bb_rightsib;
472 xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
474 * If there is a right sibling now, make it point to the
477 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
478 xfs_inobt_block_t *rrblock;
481 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
482 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
483 &rrbp, XFS_INO_BTREE_REF)))
485 rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
486 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
488 rrblock->bb_leftsib = cpu_to_be32(lbno);
489 xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
492 * Free the deleting block.
494 if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
495 cur->bc_private.a.agno, rbno), 1)))
497 xfs_trans_binval(cur->bc_tp, rbp);
499 * Readjust the ptr at this level if it's not a leaf, since it's
500 * still pointing at the deletion point, which makes the cursor
501 * inconsistent. If this makes the ptr 0, the caller fixes it up.
502 * We can't use decrement because it would change the next level up.
505 cur->bc_ptrs[level]--;
507 * Return value means the next level up has something to do.
513 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
518 * Log header fields from a btree block.
522 xfs_trans_t *tp, /* transaction pointer */
523 xfs_buf_t *bp, /* buffer containing btree block */
524 int fields) /* mask of fields: XFS_BB_... */
526 int first; /* first byte offset logged */
527 int last; /* last byte offset logged */
528 static const short offsets[] = { /* table of offsets */
529 offsetof(xfs_inobt_block_t, bb_magic),
530 offsetof(xfs_inobt_block_t, bb_level),
531 offsetof(xfs_inobt_block_t, bb_numrecs),
532 offsetof(xfs_inobt_block_t, bb_leftsib),
533 offsetof(xfs_inobt_block_t, bb_rightsib),
534 sizeof(xfs_inobt_block_t)
537 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
538 xfs_trans_log_buf(tp, bp, first, last);
542 * Log keys from a btree block (nonleaf).
546 xfs_btree_cur_t *cur, /* btree cursor */
547 xfs_buf_t *bp, /* buffer containing btree block */
548 int kfirst, /* index of first key to log */
549 int klast) /* index of last key to log */
551 xfs_inobt_block_t *block; /* btree block to log from */
552 int first; /* first byte offset logged */
553 xfs_inobt_key_t *kp; /* key pointer in btree block */
554 int last; /* last byte offset logged */
556 block = XFS_BUF_TO_INOBT_BLOCK(bp);
557 kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
558 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
559 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
560 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
564 * Log block pointer fields from a btree block (nonleaf).
568 xfs_btree_cur_t *cur, /* btree cursor */
569 xfs_buf_t *bp, /* buffer containing btree block */
570 int pfirst, /* index of first pointer to log */
571 int plast) /* index of last pointer to log */
573 xfs_inobt_block_t *block; /* btree block to log from */
574 int first; /* first byte offset logged */
575 int last; /* last byte offset logged */
576 xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
578 block = XFS_BUF_TO_INOBT_BLOCK(bp);
579 pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
580 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
581 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
582 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
586 * Log records from a btree block (leaf).
590 xfs_btree_cur_t *cur, /* btree cursor */
591 xfs_buf_t *bp, /* buffer containing btree block */
592 int rfirst, /* index of first record to log */
593 int rlast) /* index of last record to log */
595 xfs_inobt_block_t *block; /* btree block to log from */
596 int first; /* first byte offset logged */
597 int last; /* last byte offset logged */
598 xfs_inobt_rec_t *rp; /* record pointer for btree block */
600 block = XFS_BUF_TO_INOBT_BLOCK(bp);
601 rp = XFS_INOBT_REC_ADDR(block, 1, cur);
602 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
603 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
604 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
609 * Externally visible routines.
613 * Delete the record pointed to by cur.
614 * The cursor refers to the place where the record was (could be inserted)
615 * when the operation returns.
619 xfs_btree_cur_t *cur, /* btree cursor */
620 int *stat) /* success/failure */
623 int i; /* result code */
624 int level; /* btree level */
627 * Go up the tree, starting at leaf level.
628 * If 2 is returned then a join was done; go to the next level.
629 * Otherwise we are done.
631 for (level = 0, i = 2; i == 2; level++) {
632 if ((error = xfs_inobt_delrec(cur, level, &i)))
636 for (level = 1; level < cur->bc_nlevels; level++) {
637 if (cur->bc_ptrs[level] == 0) {
638 if ((error = xfs_btree_decrement(cur, level, &i)))
650 * Get the data from the pointed-to record.
654 xfs_btree_cur_t *cur, /* btree cursor */
655 xfs_agino_t *ino, /* output: starting inode of chunk */
656 __int32_t *fcnt, /* output: number of free inodes */
657 xfs_inofree_t *free, /* output: free inode mask */
658 int *stat) /* output: success/failure */
660 xfs_inobt_block_t *block; /* btree block */
661 xfs_buf_t *bp; /* buffer containing btree block */
663 int error; /* error return value */
665 int ptr; /* record number */
666 xfs_inobt_rec_t *rec; /* record data */
668 bp = cur->bc_bufs[0];
669 ptr = cur->bc_ptrs[0];
670 block = XFS_BUF_TO_INOBT_BLOCK(bp);
672 if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
676 * Off the right end or left end, return failure.
678 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
683 * Point to the record and extract its data.
685 rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
686 *ino = be32_to_cpu(rec->ir_startino);
687 *fcnt = be32_to_cpu(rec->ir_freecount);
688 *free = be64_to_cpu(rec->ir_free);
694 STATIC struct xfs_btree_cur *
695 xfs_inobt_dup_cursor(
696 struct xfs_btree_cur *cur)
698 return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
699 cur->bc_private.a.agbp, cur->bc_private.a.agno);
704 struct xfs_btree_cur *cur,
705 union xfs_btree_ptr *nptr,
706 int inc) /* level change */
708 struct xfs_buf *agbp = cur->bc_private.a.agbp;
709 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
711 agi->agi_root = nptr->s;
712 be32_add_cpu(&agi->agi_level, inc);
713 xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
717 xfs_inobt_alloc_block(
718 struct xfs_btree_cur *cur,
719 union xfs_btree_ptr *start,
720 union xfs_btree_ptr *new,
724 xfs_alloc_arg_t args; /* block allocation args */
725 int error; /* error return value */
726 xfs_agblock_t sbno = be32_to_cpu(start->s);
728 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
730 memset(&args, 0, sizeof(args));
731 args.tp = cur->bc_tp;
732 args.mp = cur->bc_mp;
733 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
737 args.type = XFS_ALLOCTYPE_NEAR_BNO;
739 error = xfs_alloc_vextent(&args);
741 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
744 if (args.fsbno == NULLFSBLOCK) {
745 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
749 ASSERT(args.len == 1);
750 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
752 new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
759 xfs_inobt_get_maxrecs(
760 struct xfs_btree_cur *cur,
763 return cur->bc_mp->m_inobt_mxr[level != 0];
767 xfs_inobt_init_key_from_rec(
768 union xfs_btree_key *key,
769 union xfs_btree_rec *rec)
771 key->inobt.ir_startino = rec->inobt.ir_startino;
775 xfs_inobt_init_rec_from_key(
776 union xfs_btree_key *key,
777 union xfs_btree_rec *rec)
779 rec->inobt.ir_startino = key->inobt.ir_startino;
783 xfs_inobt_init_rec_from_cur(
784 struct xfs_btree_cur *cur,
785 union xfs_btree_rec *rec)
787 rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
788 rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
789 rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
793 * intial value of ptr for lookup
796 xfs_inobt_init_ptr_from_cur(
797 struct xfs_btree_cur *cur,
798 union xfs_btree_ptr *ptr)
800 struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
802 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
804 ptr->s = agi->agi_root;
809 struct xfs_btree_cur *cur,
810 union xfs_btree_key *key)
812 return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
813 cur->bc_rec.i.ir_startino;
816 #ifdef XFS_BTREE_TRACE
817 ktrace_t *xfs_inobt_trace_buf;
820 xfs_inobt_trace_enter(
821 struct xfs_btree_cur *cur,
838 ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
839 (void *)func, (void *)s, NULL, (void *)cur,
840 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
841 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
842 (void *)a8, (void *)a9, (void *)a10);
846 xfs_inobt_trace_cursor(
847 struct xfs_btree_cur *cur,
852 *s0 = cur->bc_private.a.agno;
853 *l0 = cur->bc_rec.i.ir_startino;
854 *l1 = cur->bc_rec.i.ir_free;
859 struct xfs_btree_cur *cur,
860 union xfs_btree_key *key,
864 *l0 = be32_to_cpu(key->inobt.ir_startino);
869 xfs_inobt_trace_record(
870 struct xfs_btree_cur *cur,
871 union xfs_btree_rec *rec,
876 *l0 = be32_to_cpu(rec->inobt.ir_startino);
877 *l1 = be32_to_cpu(rec->inobt.ir_freecount);
878 *l2 = be64_to_cpu(rec->inobt.ir_free);
880 #endif /* XFS_BTREE_TRACE */
882 static const struct xfs_btree_ops xfs_inobt_ops = {
883 .rec_len = sizeof(xfs_inobt_rec_t),
884 .key_len = sizeof(xfs_inobt_key_t),
886 .dup_cursor = xfs_inobt_dup_cursor,
887 .set_root = xfs_inobt_set_root,
888 .alloc_block = xfs_inobt_alloc_block,
889 .get_maxrecs = xfs_inobt_get_maxrecs,
890 .init_key_from_rec = xfs_inobt_init_key_from_rec,
891 .init_rec_from_key = xfs_inobt_init_rec_from_key,
892 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
893 .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
894 .key_diff = xfs_inobt_key_diff,
896 #ifdef XFS_BTREE_TRACE
897 .trace_enter = xfs_inobt_trace_enter,
898 .trace_cursor = xfs_inobt_trace_cursor,
899 .trace_key = xfs_inobt_trace_key,
900 .trace_record = xfs_inobt_trace_record,
905 * Allocate a new inode btree cursor.
907 struct xfs_btree_cur * /* new inode btree cursor */
908 xfs_inobt_init_cursor(
909 struct xfs_mount *mp, /* file system mount point */
910 struct xfs_trans *tp, /* transaction pointer */
911 struct xfs_buf *agbp, /* buffer for agi structure */
912 xfs_agnumber_t agno) /* allocation group number */
914 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
915 struct xfs_btree_cur *cur;
917 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
921 cur->bc_nlevels = be32_to_cpu(agi->agi_level);
922 cur->bc_btnum = XFS_BTNUM_INO;
923 cur->bc_blocklog = mp->m_sb.sb_blocklog;
925 cur->bc_ops = &xfs_inobt_ops;
927 cur->bc_private.a.agbp = agbp;
928 cur->bc_private.a.agno = agno;