2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_error.h"
42 STATIC
void xfs_inobt_log_block(xfs_trans_t
*, xfs_buf_t
*, int);
43 STATIC
void xfs_inobt_log_keys(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
44 STATIC
void xfs_inobt_log_ptrs(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
45 STATIC
void xfs_inobt_log_recs(xfs_btree_cur_t
*, xfs_buf_t
*, int, int);
46 STATIC
int xfs_inobt_lshift(xfs_btree_cur_t
*, int, int *);
47 STATIC
int xfs_inobt_newroot(xfs_btree_cur_t
*, int *);
48 STATIC
int xfs_inobt_rshift(xfs_btree_cur_t
*, int, int *);
49 STATIC
int xfs_inobt_split(xfs_btree_cur_t
*, int, xfs_agblock_t
*,
50 xfs_inobt_key_t
*, xfs_btree_cur_t
**, int *);
53 * Single level of the xfs_inobt_delete record deletion routine.
54 * Delete record pointed to by cur/level.
55 * Remove the record from its block then rebalance the tree.
56 * Return 0 for error, 1 for done, 2 to go on to the next level.
58 STATIC
int /* error */
60 xfs_btree_cur_t
*cur
, /* btree cursor */
61 int level
, /* level removing record from */
62 int *stat
) /* fail/done/go-on */
64 xfs_buf_t
*agbp
; /* buffer for a.g. inode header */
65 xfs_mount_t
*mp
; /* mount structure */
66 xfs_agi_t
*agi
; /* allocation group inode header */
67 xfs_inobt_block_t
*block
; /* btree block record/key lives in */
68 xfs_agblock_t bno
; /* btree block number */
69 xfs_buf_t
*bp
; /* buffer for block */
70 int error
; /* error return value */
71 int i
; /* loop index */
72 xfs_inobt_key_t key
; /* kp points here if block is level 0 */
73 xfs_inobt_key_t
*kp
= NULL
; /* pointer to btree keys */
74 xfs_agblock_t lbno
; /* left block's block number */
75 xfs_buf_t
*lbp
; /* left block's buffer pointer */
76 xfs_inobt_block_t
*left
; /* left btree block */
77 xfs_inobt_key_t
*lkp
; /* left block key pointer */
78 xfs_inobt_ptr_t
*lpp
; /* left block address pointer */
79 int lrecs
= 0; /* number of records in left block */
80 xfs_inobt_rec_t
*lrp
; /* left block record pointer */
81 xfs_inobt_ptr_t
*pp
= NULL
; /* pointer to btree addresses */
82 int ptr
; /* index in btree block for this rec */
83 xfs_agblock_t rbno
; /* right block's block number */
84 xfs_buf_t
*rbp
; /* right block's buffer pointer */
85 xfs_inobt_block_t
*right
; /* right btree block */
86 xfs_inobt_key_t
*rkp
; /* right block key pointer */
87 xfs_inobt_rec_t
*rp
; /* pointer to btree records */
88 xfs_inobt_ptr_t
*rpp
; /* right block address pointer */
89 int rrecs
= 0; /* number of records in right block */
91 xfs_inobt_rec_t
*rrp
; /* right block record pointer */
92 xfs_btree_cur_t
*tcur
; /* temporary btree cursor */
97 * Get the index of the entry being deleted, check for nothing there.
99 ptr
= cur
->bc_ptrs
[level
];
106 * Get the buffer & block containing the record or key/ptr.
108 bp
= cur
->bc_bufs
[level
];
109 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
111 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
115 * Fail if we're off the end of the block.
118 numrecs
= be16_to_cpu(block
->bb_numrecs
);
124 * It's a nonleaf. Excise the key and ptr being deleted, by
125 * sliding the entries past them down one.
126 * Log the changed areas of the block.
129 kp
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
130 pp
= XFS_INOBT_PTR_ADDR(block
, 1, cur
);
132 for (i
= ptr
; i
< numrecs
; i
++) {
133 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(pp
[i
]), level
)))
138 memmove(&kp
[ptr
- 1], &kp
[ptr
],
139 (numrecs
- ptr
) * sizeof(*kp
));
140 memmove(&pp
[ptr
- 1], &pp
[ptr
],
141 (numrecs
- ptr
) * sizeof(*kp
));
142 xfs_inobt_log_keys(cur
, bp
, ptr
, numrecs
- 1);
143 xfs_inobt_log_ptrs(cur
, bp
, ptr
, numrecs
- 1);
147 * It's a leaf. Excise the record being deleted, by sliding the
148 * entries past it down one. Log the changed areas of the block.
151 rp
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
153 memmove(&rp
[ptr
- 1], &rp
[ptr
],
154 (numrecs
- ptr
) * sizeof(*rp
));
155 xfs_inobt_log_recs(cur
, bp
, ptr
, numrecs
- 1);
158 * If it's the first record in the block, we'll need a key
159 * structure to pass up to the next level (updkey).
162 key
.ir_startino
= rp
->ir_startino
;
167 * Decrement and log the number of entries in the block.
170 block
->bb_numrecs
= cpu_to_be16(numrecs
);
171 xfs_inobt_log_block(cur
->bc_tp
, bp
, XFS_BB_NUMRECS
);
173 * Is this the root level? If so, we're almost done.
175 if (level
== cur
->bc_nlevels
- 1) {
177 * If this is the root level,
178 * and there's only one entry left,
179 * and it's NOT the leaf level,
180 * then we can get rid of this level.
182 if (numrecs
== 1 && level
> 0) {
183 agbp
= cur
->bc_private
.a
.agbp
;
184 agi
= XFS_BUF_TO_AGI(agbp
);
186 * pp is still set to the first pointer in the block.
187 * Make it the new root of the btree.
189 bno
= be32_to_cpu(agi
->agi_root
);
191 be32_add_cpu(&agi
->agi_level
, -1);
195 if ((error
= xfs_free_extent(cur
->bc_tp
,
196 XFS_AGB_TO_FSB(mp
, cur
->bc_private
.a
.agno
, bno
), 1)))
198 xfs_trans_binval(cur
->bc_tp
, bp
);
199 xfs_ialloc_log_agi(cur
->bc_tp
, agbp
,
200 XFS_AGI_ROOT
| XFS_AGI_LEVEL
);
202 * Update the cursor so there's one fewer level.
204 cur
->bc_bufs
[level
] = NULL
;
206 } else if (level
> 0 &&
207 (error
= xfs_btree_decrement(cur
, level
, &i
)))
213 * If we deleted the leftmost entry in the block, update the
214 * key values above us in the tree.
216 if (ptr
== 1 && (error
= xfs_btree_updkey(cur
, (union xfs_btree_key
*)kp
, level
+ 1)))
219 * If the number of records remaining in the block is at least
220 * the minimum, we're done.
222 if (numrecs
>= XFS_INOBT_BLOCK_MINRECS(level
, cur
)) {
224 (error
= xfs_btree_decrement(cur
, level
, &i
)))
230 * Otherwise, we have to move some records around to keep the
231 * tree balanced. Look at the left and right sibling blocks to
232 * see if we can re-balance by moving only one record.
234 rbno
= be32_to_cpu(block
->bb_rightsib
);
235 lbno
= be32_to_cpu(block
->bb_leftsib
);
237 ASSERT(rbno
!= NULLAGBLOCK
|| lbno
!= NULLAGBLOCK
);
239 * Duplicate the cursor so our btree manipulations here won't
240 * disrupt the next level up.
242 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
245 * If there's a right sibling, see if it's ok to shift an entry
248 if (rbno
!= NULLAGBLOCK
) {
250 * Move the temp cursor to the last entry in the next block.
251 * Actually any entry but the first would suffice.
253 i
= xfs_btree_lastrec(tcur
, level
);
254 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
255 if ((error
= xfs_btree_increment(tcur
, level
, &i
)))
257 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
258 i
= xfs_btree_lastrec(tcur
, level
);
259 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
261 * Grab a pointer to the block.
263 rbp
= tcur
->bc_bufs
[level
];
264 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
266 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
270 * Grab the current block number, for future use.
272 bno
= be32_to_cpu(right
->bb_leftsib
);
274 * If right block is full enough so that removing one entry
275 * won't make it too empty, and left-shifting an entry out
276 * of right to us works, we're done.
278 if (be16_to_cpu(right
->bb_numrecs
) - 1 >=
279 XFS_INOBT_BLOCK_MINRECS(level
, cur
)) {
280 if ((error
= xfs_inobt_lshift(tcur
, level
, &i
)))
283 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
284 XFS_INOBT_BLOCK_MINRECS(level
, cur
));
285 xfs_btree_del_cursor(tcur
,
288 (error
= xfs_btree_decrement(cur
, level
,
296 * Otherwise, grab the number of records in right for
297 * future reference, and fix up the temp cursor to point
298 * to our block again (last record).
300 rrecs
= be16_to_cpu(right
->bb_numrecs
);
301 if (lbno
!= NULLAGBLOCK
) {
302 xfs_btree_firstrec(tcur
, level
);
303 if ((error
= xfs_btree_decrement(tcur
, level
, &i
)))
308 * If there's a left sibling, see if it's ok to shift an entry
311 if (lbno
!= NULLAGBLOCK
) {
313 * Move the temp cursor to the first entry in the
316 xfs_btree_firstrec(tcur
, level
);
317 if ((error
= xfs_btree_decrement(tcur
, level
, &i
)))
319 xfs_btree_firstrec(tcur
, level
);
321 * Grab a pointer to the block.
323 lbp
= tcur
->bc_bufs
[level
];
324 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
326 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
330 * Grab the current block number, for future use.
332 bno
= be32_to_cpu(left
->bb_rightsib
);
334 * If left block is full enough so that removing one entry
335 * won't make it too empty, and right-shifting an entry out
336 * of left to us works, we're done.
338 if (be16_to_cpu(left
->bb_numrecs
) - 1 >=
339 XFS_INOBT_BLOCK_MINRECS(level
, cur
)) {
340 if ((error
= xfs_inobt_rshift(tcur
, level
, &i
)))
343 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
344 XFS_INOBT_BLOCK_MINRECS(level
, cur
));
345 xfs_btree_del_cursor(tcur
,
354 * Otherwise, grab the number of records in right for
357 lrecs
= be16_to_cpu(left
->bb_numrecs
);
360 * Delete the temp cursor, we're done with it.
362 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
364 * If here, we need to do a join to keep the tree balanced.
366 ASSERT(bno
!= NULLAGBLOCK
);
368 * See if we can join with the left neighbor block.
370 if (lbno
!= NULLAGBLOCK
&&
371 lrecs
+ numrecs
<= XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
373 * Set "right" to be the starting block,
374 * "left" to be the left neighbor.
378 rrecs
= be16_to_cpu(right
->bb_numrecs
);
380 if ((error
= xfs_btree_read_bufs(mp
, cur
->bc_tp
,
381 cur
->bc_private
.a
.agno
, lbno
, 0, &lbp
,
384 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
385 lrecs
= be16_to_cpu(left
->bb_numrecs
);
386 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
390 * If that won't work, see if we can join with the right neighbor block.
392 else if (rbno
!= NULLAGBLOCK
&&
393 rrecs
+ numrecs
<= XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
395 * Set "left" to be the starting block,
396 * "right" to be the right neighbor.
400 lrecs
= be16_to_cpu(left
->bb_numrecs
);
402 if ((error
= xfs_btree_read_bufs(mp
, cur
->bc_tp
,
403 cur
->bc_private
.a
.agno
, rbno
, 0, &rbp
,
406 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
407 rrecs
= be16_to_cpu(right
->bb_numrecs
);
408 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
412 * Otherwise, we can't fix the imbalance.
413 * Just return. This is probably a logic error, but it's not fatal.
416 if (level
> 0 && (error
= xfs_btree_decrement(cur
, level
, &i
)))
422 * We're now going to join "left" and "right" by moving all the stuff
423 * in "right" to "left" and deleting "right".
427 * It's a non-leaf. Move keys and pointers.
429 lkp
= XFS_INOBT_KEY_ADDR(left
, lrecs
+ 1, cur
);
430 lpp
= XFS_INOBT_PTR_ADDR(left
, lrecs
+ 1, cur
);
431 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
432 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
434 for (i
= 0; i
< rrecs
; i
++) {
435 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
]), level
)))
439 memcpy(lkp
, rkp
, rrecs
* sizeof(*lkp
));
440 memcpy(lpp
, rpp
, rrecs
* sizeof(*lpp
));
441 xfs_inobt_log_keys(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
442 xfs_inobt_log_ptrs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
445 * It's a leaf. Move records.
447 lrp
= XFS_INOBT_REC_ADDR(left
, lrecs
+ 1, cur
);
448 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
449 memcpy(lrp
, rrp
, rrecs
* sizeof(*lrp
));
450 xfs_inobt_log_recs(cur
, lbp
, lrecs
+ 1, lrecs
+ rrecs
);
453 * If we joined with the left neighbor, set the buffer in the
454 * cursor to the left block, and fix up the index.
457 xfs_btree_setbuf(cur
, level
, lbp
);
458 cur
->bc_ptrs
[level
] += lrecs
;
461 * If we joined with the right neighbor and there's a level above
462 * us, increment the cursor at that level.
464 else if (level
+ 1 < cur
->bc_nlevels
&&
465 (error
= xfs_btree_increment(cur
, level
+ 1, &i
)))
468 * Fix up the number of records in the surviving block.
471 left
->bb_numrecs
= cpu_to_be16(lrecs
);
473 * Fix up the right block pointer in the surviving block, and log it.
475 left
->bb_rightsib
= right
->bb_rightsib
;
476 xfs_inobt_log_block(cur
->bc_tp
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
478 * If there is a right sibling now, make it point to the
481 if (be32_to_cpu(left
->bb_rightsib
) != NULLAGBLOCK
) {
482 xfs_inobt_block_t
*rrblock
;
485 if ((error
= xfs_btree_read_bufs(mp
, cur
->bc_tp
,
486 cur
->bc_private
.a
.agno
, be32_to_cpu(left
->bb_rightsib
), 0,
487 &rrbp
, XFS_INO_BTREE_REF
)))
489 rrblock
= XFS_BUF_TO_INOBT_BLOCK(rrbp
);
490 if ((error
= xfs_btree_check_sblock(cur
, rrblock
, level
, rrbp
)))
492 rrblock
->bb_leftsib
= cpu_to_be32(lbno
);
493 xfs_inobt_log_block(cur
->bc_tp
, rrbp
, XFS_BB_LEFTSIB
);
496 * Free the deleting block.
498 if ((error
= xfs_free_extent(cur
->bc_tp
, XFS_AGB_TO_FSB(mp
,
499 cur
->bc_private
.a
.agno
, rbno
), 1)))
501 xfs_trans_binval(cur
->bc_tp
, rbp
);
503 * Readjust the ptr at this level if it's not a leaf, since it's
504 * still pointing at the deletion point, which makes the cursor
505 * inconsistent. If this makes the ptr 0, the caller fixes it up.
506 * We can't use decrement because it would change the next level up.
509 cur
->bc_ptrs
[level
]--;
511 * Return value means the next level up has something to do.
517 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
522 * Insert one record/level. Return information to the caller
523 * allowing the next level up to proceed if necessary.
525 STATIC
int /* error */
527 xfs_btree_cur_t
*cur
, /* btree cursor */
528 int level
, /* level to insert record at */
529 xfs_agblock_t
*bnop
, /* i/o: block number inserted */
530 xfs_inobt_rec_t
*recp
, /* i/o: record data inserted */
531 xfs_btree_cur_t
**curp
, /* output: new cursor replacing cur */
532 int *stat
) /* success/failure */
534 xfs_inobt_block_t
*block
; /* btree block record/key lives in */
535 xfs_buf_t
*bp
; /* buffer for block */
536 int error
; /* error return value */
537 int i
; /* loop index */
538 xfs_inobt_key_t key
; /* key value being inserted */
539 xfs_inobt_key_t
*kp
=NULL
; /* pointer to btree keys */
540 xfs_agblock_t nbno
; /* block number of allocated block */
541 xfs_btree_cur_t
*ncur
; /* new cursor to be used at next lvl */
542 xfs_inobt_key_t nkey
; /* new key value, from split */
543 xfs_inobt_rec_t nrec
; /* new record value, for caller */
545 int optr
; /* old ptr value */
546 xfs_inobt_ptr_t
*pp
; /* pointer to btree addresses */
547 int ptr
; /* index in btree block for this rec */
548 xfs_inobt_rec_t
*rp
=NULL
; /* pointer to btree records */
551 * GCC doesn't understand the (arguably complex) control flow in
552 * this function and complains about uninitialized structure fields
555 memset(&nrec
, 0, sizeof(nrec
));
558 * If we made it to the root level, allocate a new root block
561 if (level
>= cur
->bc_nlevels
) {
562 error
= xfs_inobt_newroot(cur
, &i
);
568 * Make a key out of the record data to be inserted, and save it.
570 key
.ir_startino
= recp
->ir_startino
;
571 optr
= ptr
= cur
->bc_ptrs
[level
];
573 * If we're off the left edge, return failure.
580 * Get pointers to the btree buffer and block.
582 bp
= cur
->bc_bufs
[level
];
583 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
584 numrecs
= be16_to_cpu(block
->bb_numrecs
);
586 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
589 * Check that the new entry is being inserted in the right place.
591 if (ptr
<= numrecs
) {
593 rp
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
594 xfs_btree_check_rec(cur
->bc_btnum
, recp
, rp
);
596 kp
= XFS_INOBT_KEY_ADDR(block
, ptr
, cur
);
597 xfs_btree_check_key(cur
->bc_btnum
, &key
, kp
);
604 * If the block is full, we can't insert the new entry until we
605 * make the block un-full.
607 if (numrecs
== XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
609 * First, try shifting an entry to the right neighbor.
611 if ((error
= xfs_inobt_rshift(cur
, level
, &i
)))
617 * Next, try shifting an entry to the left neighbor.
620 if ((error
= xfs_inobt_lshift(cur
, level
, &i
)))
623 optr
= ptr
= cur
->bc_ptrs
[level
];
626 * Next, try splitting the current block
627 * in half. If this works we have to
628 * re-set our variables because
629 * we could be in a different block now.
631 if ((error
= xfs_inobt_split(cur
, level
, &nbno
,
635 bp
= cur
->bc_bufs
[level
];
636 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
638 if ((error
= xfs_btree_check_sblock(cur
,
642 ptr
= cur
->bc_ptrs
[level
];
643 nrec
.ir_startino
= nkey
.ir_startino
;
646 * Otherwise the insert fails.
655 * At this point we know there's room for our new entry in the block
658 numrecs
= be16_to_cpu(block
->bb_numrecs
);
661 * It's a non-leaf entry. Make a hole for the new data
662 * in the key and ptr regions of the block.
664 kp
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
665 pp
= XFS_INOBT_PTR_ADDR(block
, 1, cur
);
667 for (i
= numrecs
; i
>= ptr
; i
--) {
668 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(pp
[i
- 1]), level
)))
672 memmove(&kp
[ptr
], &kp
[ptr
- 1],
673 (numrecs
- ptr
+ 1) * sizeof(*kp
));
674 memmove(&pp
[ptr
], &pp
[ptr
- 1],
675 (numrecs
- ptr
+ 1) * sizeof(*pp
));
677 * Now stuff the new data in, bump numrecs and log the new data.
680 if ((error
= xfs_btree_check_sptr(cur
, *bnop
, level
)))
684 pp
[ptr
- 1] = cpu_to_be32(*bnop
);
686 block
->bb_numrecs
= cpu_to_be16(numrecs
);
687 xfs_inobt_log_keys(cur
, bp
, ptr
, numrecs
);
688 xfs_inobt_log_ptrs(cur
, bp
, ptr
, numrecs
);
691 * It's a leaf entry. Make a hole for the new record.
693 rp
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
694 memmove(&rp
[ptr
], &rp
[ptr
- 1],
695 (numrecs
- ptr
+ 1) * sizeof(*rp
));
697 * Now stuff the new record in, bump numrecs
698 * and log the new data.
702 block
->bb_numrecs
= cpu_to_be16(numrecs
);
703 xfs_inobt_log_recs(cur
, bp
, ptr
, numrecs
);
706 * Log the new number of records in the btree header.
708 xfs_inobt_log_block(cur
->bc_tp
, bp
, XFS_BB_NUMRECS
);
711 * Check that the key/record is in the right place, now.
715 xfs_btree_check_rec(cur
->bc_btnum
, rp
+ ptr
- 1,
718 xfs_btree_check_key(cur
->bc_btnum
, kp
+ ptr
- 1,
723 * If we inserted at the start of a block, update the parents' keys.
725 if (optr
== 1 && (error
= xfs_btree_updkey(cur
, (union xfs_btree_key
*)&key
, level
+ 1)))
728 * Return the new block number, if any.
729 * If there is one, give back a record value and a cursor too.
732 if (nbno
!= NULLAGBLOCK
) {
741 * Log header fields from a btree block.
745 xfs_trans_t
*tp
, /* transaction pointer */
746 xfs_buf_t
*bp
, /* buffer containing btree block */
747 int fields
) /* mask of fields: XFS_BB_... */
749 int first
; /* first byte offset logged */
750 int last
; /* last byte offset logged */
751 static const short offsets
[] = { /* table of offsets */
752 offsetof(xfs_inobt_block_t
, bb_magic
),
753 offsetof(xfs_inobt_block_t
, bb_level
),
754 offsetof(xfs_inobt_block_t
, bb_numrecs
),
755 offsetof(xfs_inobt_block_t
, bb_leftsib
),
756 offsetof(xfs_inobt_block_t
, bb_rightsib
),
757 sizeof(xfs_inobt_block_t
)
760 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
, &last
);
761 xfs_trans_log_buf(tp
, bp
, first
, last
);
765 * Log keys from a btree block (nonleaf).
769 xfs_btree_cur_t
*cur
, /* btree cursor */
770 xfs_buf_t
*bp
, /* buffer containing btree block */
771 int kfirst
, /* index of first key to log */
772 int klast
) /* index of last key to log */
774 xfs_inobt_block_t
*block
; /* btree block to log from */
775 int first
; /* first byte offset logged */
776 xfs_inobt_key_t
*kp
; /* key pointer in btree block */
777 int last
; /* last byte offset logged */
779 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
780 kp
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
781 first
= (int)((xfs_caddr_t
)&kp
[kfirst
- 1] - (xfs_caddr_t
)block
);
782 last
= (int)(((xfs_caddr_t
)&kp
[klast
] - 1) - (xfs_caddr_t
)block
);
783 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
787 * Log block pointer fields from a btree block (nonleaf).
791 xfs_btree_cur_t
*cur
, /* btree cursor */
792 xfs_buf_t
*bp
, /* buffer containing btree block */
793 int pfirst
, /* index of first pointer to log */
794 int plast
) /* index of last pointer to log */
796 xfs_inobt_block_t
*block
; /* btree block to log from */
797 int first
; /* first byte offset logged */
798 int last
; /* last byte offset logged */
799 xfs_inobt_ptr_t
*pp
; /* block-pointer pointer in btree blk */
801 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
802 pp
= XFS_INOBT_PTR_ADDR(block
, 1, cur
);
803 first
= (int)((xfs_caddr_t
)&pp
[pfirst
- 1] - (xfs_caddr_t
)block
);
804 last
= (int)(((xfs_caddr_t
)&pp
[plast
] - 1) - (xfs_caddr_t
)block
);
805 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
809 * Log records from a btree block (leaf).
813 xfs_btree_cur_t
*cur
, /* btree cursor */
814 xfs_buf_t
*bp
, /* buffer containing btree block */
815 int rfirst
, /* index of first record to log */
816 int rlast
) /* index of last record 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_rec_t
*rp
; /* record pointer for btree block */
823 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
824 rp
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
825 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
826 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
827 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
831 * Move 1 record left from cur/level if possible.
832 * Update cur to reflect the new path.
834 STATIC
int /* error */
836 xfs_btree_cur_t
*cur
, /* btree cursor */
837 int level
, /* level to shift record on */
838 int *stat
) /* success/failure */
840 int error
; /* error return value */
842 int i
; /* loop index */
844 xfs_inobt_key_t key
; /* key value for leaf level upward */
845 xfs_buf_t
*lbp
; /* buffer for left neighbor block */
846 xfs_inobt_block_t
*left
; /* left neighbor btree block */
847 xfs_inobt_key_t
*lkp
=NULL
; /* key pointer for left block */
848 xfs_inobt_ptr_t
*lpp
; /* address pointer for left block */
849 xfs_inobt_rec_t
*lrp
=NULL
; /* record pointer for left block */
850 int nrec
; /* new number of left block entries */
851 xfs_buf_t
*rbp
; /* buffer for right (current) block */
852 xfs_inobt_block_t
*right
; /* right (current) btree block */
853 xfs_inobt_key_t
*rkp
=NULL
; /* key pointer for right block */
854 xfs_inobt_ptr_t
*rpp
=NULL
; /* address pointer for right block */
855 xfs_inobt_rec_t
*rrp
=NULL
; /* record pointer for right block */
858 * Set up variables for this block as "right".
860 rbp
= cur
->bc_bufs
[level
];
861 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
863 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
867 * If we've got no left sibling then we can't shift an entry left.
869 if (be32_to_cpu(right
->bb_leftsib
) == NULLAGBLOCK
) {
874 * If the cursor entry is the one that would be moved, don't
875 * do it... it's too complicated.
877 if (cur
->bc_ptrs
[level
] <= 1) {
882 * Set up the left neighbor as "left".
884 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
885 cur
->bc_private
.a
.agno
, be32_to_cpu(right
->bb_leftsib
),
886 0, &lbp
, XFS_INO_BTREE_REF
)))
888 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
889 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
892 * If it's full, it can't take another entry.
894 if (be16_to_cpu(left
->bb_numrecs
) == XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
898 nrec
= be16_to_cpu(left
->bb_numrecs
) + 1;
900 * If non-leaf, copy a key and a ptr to the left block.
903 lkp
= XFS_INOBT_KEY_ADDR(left
, nrec
, cur
);
904 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
906 xfs_inobt_log_keys(cur
, lbp
, nrec
, nrec
);
907 lpp
= XFS_INOBT_PTR_ADDR(left
, nrec
, cur
);
908 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
910 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(*rpp
), level
)))
914 xfs_inobt_log_ptrs(cur
, lbp
, nrec
, nrec
);
917 * If leaf, copy a record to the left block.
920 lrp
= XFS_INOBT_REC_ADDR(left
, nrec
, cur
);
921 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
923 xfs_inobt_log_recs(cur
, lbp
, nrec
, nrec
);
926 * Bump and log left's numrecs, decrement and log right's numrecs.
928 be16_add_cpu(&left
->bb_numrecs
, 1);
929 xfs_inobt_log_block(cur
->bc_tp
, lbp
, XFS_BB_NUMRECS
);
932 xfs_btree_check_key(cur
->bc_btnum
, lkp
- 1, lkp
);
934 xfs_btree_check_rec(cur
->bc_btnum
, lrp
- 1, lrp
);
936 be16_add_cpu(&right
->bb_numrecs
, -1);
937 xfs_inobt_log_block(cur
->bc_tp
, rbp
, XFS_BB_NUMRECS
);
939 * Slide the contents of right down one entry.
943 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
944 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
+ 1]),
949 memmove(rkp
, rkp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
950 memmove(rpp
, rpp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
951 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
952 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
954 memmove(rrp
, rrp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
955 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
956 key
.ir_startino
= rrp
->ir_startino
;
960 * Update the parent key values of right.
962 if ((error
= xfs_btree_updkey(cur
, (union xfs_btree_key
*)rkp
, level
+ 1)))
965 * Slide the cursor value left one.
967 cur
->bc_ptrs
[level
]--;
973 * Allocate a new root block, fill it in.
975 STATIC
int /* error */
977 xfs_btree_cur_t
*cur
, /* btree cursor */
978 int *stat
) /* success/failure */
980 xfs_agi_t
*agi
; /* a.g. inode header */
981 xfs_alloc_arg_t args
; /* allocation argument structure */
982 xfs_inobt_block_t
*block
; /* one half of the old root block */
983 xfs_buf_t
*bp
; /* buffer containing block */
984 int error
; /* error return value */
985 xfs_inobt_key_t
*kp
; /* btree key pointer */
986 xfs_agblock_t lbno
; /* left block number */
987 xfs_buf_t
*lbp
; /* left buffer pointer */
988 xfs_inobt_block_t
*left
; /* left btree block */
989 xfs_buf_t
*nbp
; /* new (root) buffer */
990 xfs_inobt_block_t
*new; /* new (root) btree block */
991 int nptr
; /* new value for key index, 1 or 2 */
992 xfs_inobt_ptr_t
*pp
; /* btree address pointer */
993 xfs_agblock_t rbno
; /* right block number */
994 xfs_buf_t
*rbp
; /* right buffer pointer */
995 xfs_inobt_block_t
*right
; /* right btree block */
996 xfs_inobt_rec_t
*rp
; /* btree record pointer */
998 ASSERT(cur
->bc_nlevels
< XFS_IN_MAXLEVELS(cur
->bc_mp
));
1001 * Get a block & a buffer.
1003 agi
= XFS_BUF_TO_AGI(cur
->bc_private
.a
.agbp
);
1004 args
.tp
= cur
->bc_tp
;
1005 args
.mp
= cur
->bc_mp
;
1006 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, cur
->bc_private
.a
.agno
,
1007 be32_to_cpu(agi
->agi_root
));
1008 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.wasdel
=
1009 args
.isfl
= args
.userdata
= args
.minalignslop
= 0;
1010 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1011 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1012 if ((error
= xfs_alloc_vextent(&args
)))
1015 * None available, we fail.
1017 if (args
.fsbno
== NULLFSBLOCK
) {
1021 ASSERT(args
.len
== 1);
1022 nbp
= xfs_btree_get_bufs(args
.mp
, args
.tp
, args
.agno
, args
.agbno
, 0);
1023 new = XFS_BUF_TO_INOBT_BLOCK(nbp
);
1025 * Set the root data in the a.g. inode structure.
1027 agi
->agi_root
= cpu_to_be32(args
.agbno
);
1028 be32_add_cpu(&agi
->agi_level
, 1);
1029 xfs_ialloc_log_agi(args
.tp
, cur
->bc_private
.a
.agbp
,
1030 XFS_AGI_ROOT
| XFS_AGI_LEVEL
);
1032 * At the previous root level there are now two blocks: the old
1033 * root, and the new block generated when it was split.
1034 * We don't know which one the cursor is pointing at, so we
1035 * set up variables "left" and "right" for each case.
1037 bp
= cur
->bc_bufs
[cur
->bc_nlevels
- 1];
1038 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1040 if ((error
= xfs_btree_check_sblock(cur
, block
, cur
->bc_nlevels
- 1, bp
)))
1043 if (be32_to_cpu(block
->bb_rightsib
) != NULLAGBLOCK
) {
1045 * Our block is left, pick up the right block.
1048 lbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(lbp
));
1050 rbno
= be32_to_cpu(left
->bb_rightsib
);
1051 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
1052 rbno
, 0, &rbp
, XFS_INO_BTREE_REF
)))
1055 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
1056 if ((error
= xfs_btree_check_sblock(cur
, right
,
1057 cur
->bc_nlevels
- 1, rbp
)))
1062 * Our block is right, pick up the left block.
1065 rbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(rbp
));
1067 lbno
= be32_to_cpu(right
->bb_leftsib
);
1068 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
1069 lbno
, 0, &lbp
, XFS_INO_BTREE_REF
)))
1072 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
1073 if ((error
= xfs_btree_check_sblock(cur
, left
,
1074 cur
->bc_nlevels
- 1, lbp
)))
1079 * Fill in the new block's btree header and log it.
1081 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
1082 new->bb_level
= cpu_to_be16(cur
->bc_nlevels
);
1083 new->bb_numrecs
= cpu_to_be16(2);
1084 new->bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
1085 new->bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
1086 xfs_inobt_log_block(args
.tp
, nbp
, XFS_BB_ALL_BITS
);
1087 ASSERT(lbno
!= NULLAGBLOCK
&& rbno
!= NULLAGBLOCK
);
1089 * Fill in the key data in the new root.
1091 kp
= XFS_INOBT_KEY_ADDR(new, 1, cur
);
1092 if (be16_to_cpu(left
->bb_level
) > 0) {
1093 kp
[0] = *XFS_INOBT_KEY_ADDR(left
, 1, cur
);
1094 kp
[1] = *XFS_INOBT_KEY_ADDR(right
, 1, cur
);
1096 rp
= XFS_INOBT_REC_ADDR(left
, 1, cur
);
1097 kp
[0].ir_startino
= rp
->ir_startino
;
1098 rp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
1099 kp
[1].ir_startino
= rp
->ir_startino
;
1101 xfs_inobt_log_keys(cur
, nbp
, 1, 2);
1103 * Fill in the pointer data in the new root.
1105 pp
= XFS_INOBT_PTR_ADDR(new, 1, cur
);
1106 pp
[0] = cpu_to_be32(lbno
);
1107 pp
[1] = cpu_to_be32(rbno
);
1108 xfs_inobt_log_ptrs(cur
, nbp
, 1, 2);
1110 * Fix up the cursor.
1112 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
1113 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
1120 * Move 1 record right from cur/level if possible.
1121 * Update cur to reflect the new path.
1123 STATIC
int /* error */
1125 xfs_btree_cur_t
*cur
, /* btree cursor */
1126 int level
, /* level to shift record on */
1127 int *stat
) /* success/failure */
1129 int error
; /* error return value */
1130 int i
; /* loop index */
1131 xfs_inobt_key_t key
; /* key value for leaf level upward */
1132 xfs_buf_t
*lbp
; /* buffer for left (current) block */
1133 xfs_inobt_block_t
*left
; /* left (current) btree block */
1134 xfs_inobt_key_t
*lkp
; /* key pointer for left block */
1135 xfs_inobt_ptr_t
*lpp
; /* address pointer for left block */
1136 xfs_inobt_rec_t
*lrp
; /* record pointer for left block */
1137 xfs_buf_t
*rbp
; /* buffer for right neighbor block */
1138 xfs_inobt_block_t
*right
; /* right neighbor btree block */
1139 xfs_inobt_key_t
*rkp
; /* key pointer for right block */
1140 xfs_inobt_ptr_t
*rpp
; /* address pointer for right block */
1141 xfs_inobt_rec_t
*rrp
=NULL
; /* record pointer for right block */
1142 xfs_btree_cur_t
*tcur
; /* temporary cursor */
1145 * Set up variables for this block as "left".
1147 lbp
= cur
->bc_bufs
[level
];
1148 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
1150 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
1154 * If we've got no right sibling then we can't shift an entry right.
1156 if (be32_to_cpu(left
->bb_rightsib
) == NULLAGBLOCK
) {
1161 * If the cursor entry is the one that would be moved, don't
1162 * do it... it's too complicated.
1164 if (cur
->bc_ptrs
[level
] >= be16_to_cpu(left
->bb_numrecs
)) {
1169 * Set up the right neighbor as "right".
1171 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
1172 cur
->bc_private
.a
.agno
, be32_to_cpu(left
->bb_rightsib
),
1173 0, &rbp
, XFS_INO_BTREE_REF
)))
1175 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
1176 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
1179 * If it's full, it can't take another entry.
1181 if (be16_to_cpu(right
->bb_numrecs
) == XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
1186 * Make a hole at the start of the right neighbor block, then
1187 * copy the last left block entry to the hole.
1190 lkp
= XFS_INOBT_KEY_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1191 lpp
= XFS_INOBT_PTR_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1192 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
1193 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
1195 for (i
= be16_to_cpu(right
->bb_numrecs
) - 1; i
>= 0; i
--) {
1196 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
]), level
)))
1200 memmove(rkp
+ 1, rkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1201 memmove(rpp
+ 1, rpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1203 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(*lpp
), level
)))
1208 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1209 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1211 lrp
= XFS_INOBT_REC_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
1212 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
1213 memmove(rrp
+ 1, rrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1215 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
1216 key
.ir_startino
= rrp
->ir_startino
;
1220 * Decrement and log left's numrecs, bump and log right's numrecs.
1222 be16_add_cpu(&left
->bb_numrecs
, -1);
1223 xfs_inobt_log_block(cur
->bc_tp
, lbp
, XFS_BB_NUMRECS
);
1224 be16_add_cpu(&right
->bb_numrecs
, 1);
1227 xfs_btree_check_key(cur
->bc_btnum
, rkp
, rkp
+ 1);
1229 xfs_btree_check_rec(cur
->bc_btnum
, rrp
, rrp
+ 1);
1231 xfs_inobt_log_block(cur
->bc_tp
, rbp
, XFS_BB_NUMRECS
);
1233 * Using a temporary cursor, update the parent key values of the
1234 * block on the right.
1236 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
1238 xfs_btree_lastrec(tcur
, level
);
1239 if ((error
= xfs_btree_increment(tcur
, level
, &i
)) ||
1240 (error
= xfs_btree_updkey(tcur
, (union xfs_btree_key
*)rkp
, level
+ 1))) {
1241 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1244 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1250 * Split cur/level block in half.
1251 * Return new block number and its first record (to be inserted into parent).
1253 STATIC
int /* error */
1255 xfs_btree_cur_t
*cur
, /* btree cursor */
1256 int level
, /* level to split */
1257 xfs_agblock_t
*bnop
, /* output: block number allocated */
1258 xfs_inobt_key_t
*keyp
, /* output: first key of new block */
1259 xfs_btree_cur_t
**curp
, /* output: new cursor */
1260 int *stat
) /* success/failure */
1262 xfs_alloc_arg_t args
; /* allocation argument structure */
1263 int error
; /* error return value */
1264 int i
; /* loop index/record number */
1265 xfs_agblock_t lbno
; /* left (current) block number */
1266 xfs_buf_t
*lbp
; /* buffer for left block */
1267 xfs_inobt_block_t
*left
; /* left (current) btree block */
1268 xfs_inobt_key_t
*lkp
; /* left btree key pointer */
1269 xfs_inobt_ptr_t
*lpp
; /* left btree address pointer */
1270 xfs_inobt_rec_t
*lrp
; /* left btree record pointer */
1271 xfs_buf_t
*rbp
; /* buffer for right block */
1272 xfs_inobt_block_t
*right
; /* right (new) btree block */
1273 xfs_inobt_key_t
*rkp
; /* right btree key pointer */
1274 xfs_inobt_ptr_t
*rpp
; /* right btree address pointer */
1275 xfs_inobt_rec_t
*rrp
; /* right btree record pointer */
1278 * Set up left block (current one).
1280 lbp
= cur
->bc_bufs
[level
];
1281 args
.tp
= cur
->bc_tp
;
1282 args
.mp
= cur
->bc_mp
;
1283 lbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(lbp
));
1285 * Allocate the new block.
1286 * If we can't do it, we're toast. Give up.
1288 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, cur
->bc_private
.a
.agno
, lbno
);
1289 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.wasdel
=
1290 args
.isfl
= args
.userdata
= args
.minalignslop
= 0;
1291 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1292 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1293 if ((error
= xfs_alloc_vextent(&args
)))
1295 if (args
.fsbno
== NULLFSBLOCK
) {
1299 ASSERT(args
.len
== 1);
1300 rbp
= xfs_btree_get_bufs(args
.mp
, args
.tp
, args
.agno
, args
.agbno
, 0);
1302 * Set up the new block as "right".
1304 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
1306 * "Left" is the current (according to the cursor) block.
1308 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
1310 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
1314 * Fill in the btree header for the new block.
1316 right
->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
1317 right
->bb_level
= left
->bb_level
;
1318 right
->bb_numrecs
= cpu_to_be16(be16_to_cpu(left
->bb_numrecs
) / 2);
1320 * Make sure that if there's an odd number of entries now, that
1321 * each new block will have the same number of entries.
1323 if ((be16_to_cpu(left
->bb_numrecs
) & 1) &&
1324 cur
->bc_ptrs
[level
] <= be16_to_cpu(right
->bb_numrecs
) + 1)
1325 be16_add_cpu(&right
->bb_numrecs
, 1);
1326 i
= be16_to_cpu(left
->bb_numrecs
) - be16_to_cpu(right
->bb_numrecs
) + 1;
1328 * For non-leaf blocks, copy keys and addresses over to the new block.
1331 lkp
= XFS_INOBT_KEY_ADDR(left
, i
, cur
);
1332 lpp
= XFS_INOBT_PTR_ADDR(left
, i
, cur
);
1333 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
1334 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
1336 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
1337 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(lpp
[i
]), level
)))
1341 memcpy(rkp
, lkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1342 memcpy(rpp
, lpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1343 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1344 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1348 * For leaf blocks, copy records over to the new block.
1351 lrp
= XFS_INOBT_REC_ADDR(left
, i
, cur
);
1352 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
1353 memcpy(rrp
, lrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1354 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1355 keyp
->ir_startino
= rrp
->ir_startino
;
1358 * Find the left block number by looking in the buffer.
1359 * Adjust numrecs, sibling pointers.
1361 be16_add_cpu(&left
->bb_numrecs
, -(be16_to_cpu(right
->bb_numrecs
)));
1362 right
->bb_rightsib
= left
->bb_rightsib
;
1363 left
->bb_rightsib
= cpu_to_be32(args
.agbno
);
1364 right
->bb_leftsib
= cpu_to_be32(lbno
);
1365 xfs_inobt_log_block(args
.tp
, rbp
, XFS_BB_ALL_BITS
);
1366 xfs_inobt_log_block(args
.tp
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1368 * If there's a block to the new block's right, make that block
1369 * point back to right instead of to left.
1371 if (be32_to_cpu(right
->bb_rightsib
) != NULLAGBLOCK
) {
1372 xfs_inobt_block_t
*rrblock
; /* rr btree block */
1373 xfs_buf_t
*rrbp
; /* buffer for rrblock */
1375 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
1376 be32_to_cpu(right
->bb_rightsib
), 0, &rrbp
,
1377 XFS_INO_BTREE_REF
)))
1379 rrblock
= XFS_BUF_TO_INOBT_BLOCK(rrbp
);
1380 if ((error
= xfs_btree_check_sblock(cur
, rrblock
, level
, rrbp
)))
1382 rrblock
->bb_leftsib
= cpu_to_be32(args
.agbno
);
1383 xfs_inobt_log_block(args
.tp
, rrbp
, XFS_BB_LEFTSIB
);
1386 * If the cursor is really in the right block, move it there.
1387 * If it's just pointing past the last entry in left, then we'll
1388 * insert there, so don't change anything in that case.
1390 if (cur
->bc_ptrs
[level
] > be16_to_cpu(left
->bb_numrecs
) + 1) {
1391 xfs_btree_setbuf(cur
, level
, rbp
);
1392 cur
->bc_ptrs
[level
] -= be16_to_cpu(left
->bb_numrecs
);
1395 * If there are more levels, we'll need another cursor which refers
1396 * the right block, no matter where this cursor was.
1398 if (level
+ 1 < cur
->bc_nlevels
) {
1399 if ((error
= xfs_btree_dup_cursor(cur
, curp
)))
1401 (*curp
)->bc_ptrs
[level
+ 1]++;
1409 * Externally visible routines.
1413 * Delete the record pointed to by cur.
1414 * The cursor refers to the place where the record was (could be inserted)
1415 * when the operation returns.
1419 xfs_btree_cur_t
*cur
, /* btree cursor */
1420 int *stat
) /* success/failure */
1423 int i
; /* result code */
1424 int level
; /* btree level */
1427 * Go up the tree, starting at leaf level.
1428 * If 2 is returned then a join was done; go to the next level.
1429 * Otherwise we are done.
1431 for (level
= 0, i
= 2; i
== 2; level
++) {
1432 if ((error
= xfs_inobt_delrec(cur
, level
, &i
)))
1436 for (level
= 1; level
< cur
->bc_nlevels
; level
++) {
1437 if (cur
->bc_ptrs
[level
] == 0) {
1438 if ((error
= xfs_btree_decrement(cur
, level
, &i
)))
1450 * Get the data from the pointed-to record.
1454 xfs_btree_cur_t
*cur
, /* btree cursor */
1455 xfs_agino_t
*ino
, /* output: starting inode of chunk */
1456 __int32_t
*fcnt
, /* output: number of free inodes */
1457 xfs_inofree_t
*free
, /* output: free inode mask */
1458 int *stat
) /* output: success/failure */
1460 xfs_inobt_block_t
*block
; /* btree block */
1461 xfs_buf_t
*bp
; /* buffer containing btree block */
1463 int error
; /* error return value */
1465 int ptr
; /* record number */
1466 xfs_inobt_rec_t
*rec
; /* record data */
1468 bp
= cur
->bc_bufs
[0];
1469 ptr
= cur
->bc_ptrs
[0];
1470 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1472 if ((error
= xfs_btree_check_sblock(cur
, block
, 0, bp
)))
1476 * Off the right end or left end, return failure.
1478 if (ptr
> be16_to_cpu(block
->bb_numrecs
) || ptr
<= 0) {
1483 * Point to the record and extract its data.
1485 rec
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
1486 *ino
= be32_to_cpu(rec
->ir_startino
);
1487 *fcnt
= be32_to_cpu(rec
->ir_freecount
);
1488 *free
= be64_to_cpu(rec
->ir_free
);
1494 * Insert the current record at the point referenced by cur.
1495 * The cursor may be inconsistent on return if splits have been done.
1499 xfs_btree_cur_t
*cur
, /* btree cursor */
1500 int *stat
) /* success/failure */
1502 int error
; /* error return value */
1503 int i
; /* result value, 0 for failure */
1504 int level
; /* current level number in btree */
1505 xfs_agblock_t nbno
; /* new block number (split result) */
1506 xfs_btree_cur_t
*ncur
; /* new cursor (split result) */
1507 xfs_inobt_rec_t nrec
; /* record being inserted this level */
1508 xfs_btree_cur_t
*pcur
; /* previous level's cursor */
1512 nrec
.ir_startino
= cpu_to_be32(cur
->bc_rec
.i
.ir_startino
);
1513 nrec
.ir_freecount
= cpu_to_be32(cur
->bc_rec
.i
.ir_freecount
);
1514 nrec
.ir_free
= cpu_to_be64(cur
->bc_rec
.i
.ir_free
);
1518 * Loop going up the tree, starting at the leaf level.
1519 * Stop when we don't get a split block, that must mean that
1520 * the insert is finished with this level.
1524 * Insert nrec/nbno into this level of the tree.
1525 * Note if we fail, nbno will be null.
1527 if ((error
= xfs_inobt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
1530 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
1534 * See if the cursor we just used is trash.
1535 * Can't trash the caller's cursor, but otherwise we should
1536 * if ncur is a new cursor or we're about to be done.
1538 if (pcur
!= cur
&& (ncur
|| nbno
== NULLAGBLOCK
)) {
1539 cur
->bc_nlevels
= pcur
->bc_nlevels
;
1540 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
1543 * If we got a new cursor, switch to it.
1549 } while (nbno
!= NULLAGBLOCK
);
1555 * Update the record referred to by cur, to the value given
1556 * by [ino, fcnt, free].
1557 * This either works (return 0) or gets an EFSCORRUPTED error.
1561 xfs_btree_cur_t
*cur
, /* btree cursor */
1562 xfs_agino_t ino
, /* starting inode of chunk */
1563 __int32_t fcnt
, /* free inode count */
1564 xfs_inofree_t free
) /* free inode mask */
1566 xfs_inobt_block_t
*block
; /* btree block to update */
1567 xfs_buf_t
*bp
; /* buffer containing btree block */
1568 int error
; /* error return value */
1569 int ptr
; /* current record number (updating) */
1570 xfs_inobt_rec_t
*rp
; /* pointer to updated record */
1573 * Pick up the current block.
1575 bp
= cur
->bc_bufs
[0];
1576 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1578 if ((error
= xfs_btree_check_sblock(cur
, block
, 0, bp
)))
1582 * Get the address of the rec to be updated.
1584 ptr
= cur
->bc_ptrs
[0];
1585 rp
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
1587 * Fill in the new contents and log them.
1589 rp
->ir_startino
= cpu_to_be32(ino
);
1590 rp
->ir_freecount
= cpu_to_be32(fcnt
);
1591 rp
->ir_free
= cpu_to_be64(free
);
1592 xfs_inobt_log_recs(cur
, bp
, ptr
, ptr
);
1594 * Updating first record in leaf. Pass new key value up to our parent.
1597 xfs_inobt_key_t key
; /* key containing [ino] */
1599 key
.ir_startino
= cpu_to_be32(ino
);
1600 if ((error
= xfs_btree_updkey(cur
, (union xfs_btree_key
*)&key
, 1)))
1606 STATIC
struct xfs_btree_cur
*
1607 xfs_inobt_dup_cursor(
1608 struct xfs_btree_cur
*cur
)
1610 return xfs_inobt_init_cursor(cur
->bc_mp
, cur
->bc_tp
,
1611 cur
->bc_private
.a
.agbp
, cur
->bc_private
.a
.agno
);
1615 xfs_inobt_get_maxrecs(
1616 struct xfs_btree_cur
*cur
,
1619 return cur
->bc_mp
->m_inobt_mxr
[level
!= 0];
1623 xfs_inobt_init_key_from_rec(
1624 union xfs_btree_key
*key
,
1625 union xfs_btree_rec
*rec
)
1627 key
->inobt
.ir_startino
= rec
->inobt
.ir_startino
;
1631 * intial value of ptr for lookup
1634 xfs_inobt_init_ptr_from_cur(
1635 struct xfs_btree_cur
*cur
,
1636 union xfs_btree_ptr
*ptr
)
1638 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(cur
->bc_private
.a
.agbp
);
1640 ASSERT(cur
->bc_private
.a
.agno
== be32_to_cpu(agi
->agi_seqno
));
1642 ptr
->s
= agi
->agi_root
;
1647 struct xfs_btree_cur
*cur
,
1648 union xfs_btree_key
*key
)
1650 return (__int64_t
)be32_to_cpu(key
->inobt
.ir_startino
) -
1651 cur
->bc_rec
.i
.ir_startino
;
1654 #ifdef XFS_BTREE_TRACE
1655 ktrace_t
*xfs_inobt_trace_buf
;
1658 xfs_inobt_trace_enter(
1659 struct xfs_btree_cur
*cur
,
1676 ktrace_enter(xfs_inobt_trace_buf
, (void *)(__psint_t
)type
,
1677 (void *)func
, (void *)s
, NULL
, (void *)cur
,
1678 (void *)a0
, (void *)a1
, (void *)a2
, (void *)a3
,
1679 (void *)a4
, (void *)a5
, (void *)a6
, (void *)a7
,
1680 (void *)a8
, (void *)a9
, (void *)a10
);
1684 xfs_inobt_trace_cursor(
1685 struct xfs_btree_cur
*cur
,
1690 *s0
= cur
->bc_private
.a
.agno
;
1691 *l0
= cur
->bc_rec
.i
.ir_startino
;
1692 *l1
= cur
->bc_rec
.i
.ir_free
;
1696 xfs_inobt_trace_key(
1697 struct xfs_btree_cur
*cur
,
1698 union xfs_btree_key
*key
,
1702 *l0
= be32_to_cpu(key
->inobt
.ir_startino
);
1707 xfs_inobt_trace_record(
1708 struct xfs_btree_cur
*cur
,
1709 union xfs_btree_rec
*rec
,
1714 *l0
= be32_to_cpu(rec
->inobt
.ir_startino
);
1715 *l1
= be32_to_cpu(rec
->inobt
.ir_freecount
);
1716 *l2
= be64_to_cpu(rec
->inobt
.ir_free
);
1718 #endif /* XFS_BTREE_TRACE */
1720 static const struct xfs_btree_ops xfs_inobt_ops
= {
1721 .rec_len
= sizeof(xfs_inobt_rec_t
),
1722 .key_len
= sizeof(xfs_inobt_key_t
),
1724 .dup_cursor
= xfs_inobt_dup_cursor
,
1725 .get_maxrecs
= xfs_inobt_get_maxrecs
,
1726 .init_key_from_rec
= xfs_inobt_init_key_from_rec
,
1727 .init_ptr_from_cur
= xfs_inobt_init_ptr_from_cur
,
1728 .key_diff
= xfs_inobt_key_diff
,
1730 #ifdef XFS_BTREE_TRACE
1731 .trace_enter
= xfs_inobt_trace_enter
,
1732 .trace_cursor
= xfs_inobt_trace_cursor
,
1733 .trace_key
= xfs_inobt_trace_key
,
1734 .trace_record
= xfs_inobt_trace_record
,
1739 * Allocate a new inode btree cursor.
1741 struct xfs_btree_cur
* /* new inode btree cursor */
1742 xfs_inobt_init_cursor(
1743 struct xfs_mount
*mp
, /* file system mount point */
1744 struct xfs_trans
*tp
, /* transaction pointer */
1745 struct xfs_buf
*agbp
, /* buffer for agi structure */
1746 xfs_agnumber_t agno
) /* allocation group number */
1748 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1749 struct xfs_btree_cur
*cur
;
1751 cur
= kmem_zone_zalloc(xfs_btree_cur_zone
, KM_SLEEP
);
1755 cur
->bc_nlevels
= be32_to_cpu(agi
->agi_level
);
1756 cur
->bc_btnum
= XFS_BTNUM_INO
;
1757 cur
->bc_blocklog
= mp
->m_sb
.sb_blocklog
;
1759 cur
->bc_ops
= &xfs_inobt_ops
;
1761 cur
->bc_private
.a
.agbp
= agbp
;
1762 cur
->bc_private
.a
.agno
= agno
;