]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_ialloc_btree.c
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 * Inode allocation management for XFS.
26 * Insert one record/level. Return information to the caller
27 * allowing the next level up to proceed if necessary.
29 STATIC
int /* error */
31 xfs_btree_cur_t
*cur
, /* btree cursor */
32 int level
, /* level to insert record at */
33 xfs_agblock_t
*bnop
, /* i/o: block number inserted */
34 xfs_inobt_rec_t
*recp
, /* i/o: record data inserted */
35 xfs_btree_cur_t
**curp
, /* output: new cursor replacing cur */
36 int *stat
) /* success/failure */
38 xfs_inobt_block_t
*block
; /* btree block record/key lives in */
39 xfs_buf_t
*bp
; /* buffer for block */
40 int error
; /* error return value */
41 int i
; /* loop index */
42 xfs_inobt_key_t key
; /* key value being inserted */
43 xfs_inobt_key_t
*kp
=NULL
; /* pointer to btree keys */
44 xfs_agblock_t nbno
; /* block number of allocated block */
45 xfs_btree_cur_t
*ncur
; /* new cursor to be used at next lvl */
46 xfs_inobt_key_t nkey
; /* new key value, from split */
47 xfs_inobt_rec_t nrec
; /* new record value, for caller */
49 int optr
; /* old ptr value */
50 xfs_inobt_ptr_t
*pp
; /* pointer to btree addresses */
51 int ptr
; /* index in btree block for this rec */
52 xfs_inobt_rec_t
*rp
=NULL
; /* pointer to btree records */
55 * GCC doesn't understand the (arguably complex) control flow in
56 * this function and complains about uninitialized structure fields
59 memset(&nrec
, 0, sizeof(nrec
));
62 * If we made it to the root level, allocate a new root block
65 if (level
>= cur
->bc_nlevels
) {
66 error
= xfs_inobt_newroot(cur
, &i
);
72 * Make a key out of the record data to be inserted, and save it.
74 key
.ir_startino
= recp
->ir_startino
; /* INT_: direct copy */
75 optr
= ptr
= cur
->bc_ptrs
[level
];
77 * If we're off the left edge, return failure.
84 * Get pointers to the btree buffer and block.
86 bp
= cur
->bc_bufs
[level
];
87 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
88 numrecs
= be16_to_cpu(block
->bb_numrecs
);
90 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
93 * Check that the new entry is being inserted in the right place.
97 rp
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
98 xfs_btree_check_rec(cur
->bc_btnum
, recp
, rp
);
100 kp
= XFS_INOBT_KEY_ADDR(block
, ptr
, cur
);
101 xfs_btree_check_key(cur
->bc_btnum
, &key
, kp
);
106 ncur
= (xfs_btree_cur_t
*)0;
108 * If the block is full, we can't insert the new entry until we
109 * make the block un-full.
111 if (numrecs
== XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
113 * First, try shifting an entry to the right neighbor.
115 if ((error
= xfs_inobt_rshift(cur
, level
, &i
)))
121 * Next, try shifting an entry to the left neighbor.
124 if ((error
= xfs_inobt_lshift(cur
, level
, &i
)))
127 optr
= ptr
= cur
->bc_ptrs
[level
];
130 * Next, try splitting the current block
131 * in half. If this works we have to
132 * re-set our variables because
133 * we could be in a different block now.
135 if ((error
= xfs_inobt_split(cur
, level
, &nbno
,
139 bp
= cur
->bc_bufs
[level
];
140 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
142 if ((error
= xfs_btree_check_sblock(cur
,
146 ptr
= cur
->bc_ptrs
[level
];
147 nrec
.ir_startino
= nkey
.ir_startino
; /* INT_: direct copy */
150 * Otherwise the insert fails.
159 * At this point we know there's room for our new entry in the block
162 numrecs
= be16_to_cpu(block
->bb_numrecs
);
165 * It's a non-leaf entry. Make a hole for the new data
166 * in the key and ptr regions of the block.
168 kp
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
169 pp
= XFS_INOBT_PTR_ADDR(block
, 1, cur
);
171 for (i
= numrecs
; i
>= ptr
; i
--) {
172 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(pp
[i
- 1]), level
)))
176 memmove(&kp
[ptr
], &kp
[ptr
- 1],
177 (numrecs
- ptr
+ 1) * sizeof(*kp
));
178 memmove(&pp
[ptr
], &pp
[ptr
- 1],
179 (numrecs
- ptr
+ 1) * sizeof(*pp
));
181 * Now stuff the new data in, bump numrecs and log the new data.
184 if ((error
= xfs_btree_check_sptr(cur
, *bnop
, level
)))
187 kp
[ptr
- 1] = key
; /* INT_: struct copy */
188 pp
[ptr
- 1] = cpu_to_be32(*bnop
);
190 block
->bb_numrecs
= cpu_to_be16(numrecs
);
191 xfs_inobt_log_keys(cur
, bp
, ptr
, numrecs
);
192 xfs_inobt_log_ptrs(cur
, bp
, ptr
, numrecs
);
195 * It's a leaf entry. Make a hole for the new record.
197 rp
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
198 memmove(&rp
[ptr
], &rp
[ptr
- 1],
199 (numrecs
- ptr
+ 1) * sizeof(*rp
));
201 * Now stuff the new record in, bump numrecs
202 * and log the new data.
204 rp
[ptr
- 1] = *recp
; /* INT_: struct copy */
206 block
->bb_numrecs
= cpu_to_be16(numrecs
);
207 xfs_inobt_log_recs(cur
, bp
, ptr
, numrecs
);
210 * Log the new number of records in the btree header.
212 xfs_inobt_log_block(cur
->bc_tp
, bp
, XFS_BB_NUMRECS
);
215 * Check that the key/record is in the right place, now.
219 xfs_btree_check_rec(cur
->bc_btnum
, rp
+ ptr
- 1,
222 xfs_btree_check_key(cur
->bc_btnum
, kp
+ ptr
- 1,
227 * If we inserted at the start of a block, update the parents' keys.
229 if (optr
== 1 && (error
= xfs_inobt_updkey(cur
, &key
, level
+ 1)))
232 * Return the new block number, if any.
233 * If there is one, give back a record value and a cursor too.
236 if (nbno
!= NULLAGBLOCK
) {
237 *recp
= nrec
; /* INT_: struct copy */
245 * Log header fields from a btree block.
249 xfs_trans_t
*tp
, /* transaction pointer */
250 xfs_buf_t
*bp
, /* buffer containing btree block */
251 int fields
) /* mask of fields: XFS_BB_... */
253 int first
; /* first byte offset logged */
254 int last
; /* last byte offset logged */
255 static const short offsets
[] = { /* table of offsets */
256 offsetof(xfs_inobt_block_t
, bb_magic
),
257 offsetof(xfs_inobt_block_t
, bb_level
),
258 offsetof(xfs_inobt_block_t
, bb_numrecs
),
259 offsetof(xfs_inobt_block_t
, bb_leftsib
),
260 offsetof(xfs_inobt_block_t
, bb_rightsib
),
261 sizeof(xfs_inobt_block_t
)
264 xfs_btree_offsets(fields
, offsets
, XFS_BB_NUM_BITS
, &first
, &last
);
265 xfs_trans_log_buf(tp
, bp
, first
, last
);
269 * Log keys from a btree block (nonleaf).
273 xfs_btree_cur_t
*cur
, /* btree cursor */
274 xfs_buf_t
*bp
, /* buffer containing btree block */
275 int kfirst
, /* index of first key to log */
276 int klast
) /* index of last key to log */
278 xfs_inobt_block_t
*block
; /* btree block to log from */
279 int first
; /* first byte offset logged */
280 xfs_inobt_key_t
*kp
; /* key pointer in btree block */
281 int last
; /* last byte offset logged */
283 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
284 kp
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
285 first
= (int)((xfs_caddr_t
)&kp
[kfirst
- 1] - (xfs_caddr_t
)block
);
286 last
= (int)(((xfs_caddr_t
)&kp
[klast
] - 1) - (xfs_caddr_t
)block
);
287 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
291 * Log block pointer fields from a btree block (nonleaf).
295 xfs_btree_cur_t
*cur
, /* btree cursor */
296 xfs_buf_t
*bp
, /* buffer containing btree block */
297 int pfirst
, /* index of first pointer to log */
298 int plast
) /* index of last pointer to log */
300 xfs_inobt_block_t
*block
; /* btree block to log from */
301 int first
; /* first byte offset logged */
302 int last
; /* last byte offset logged */
303 xfs_inobt_ptr_t
*pp
; /* block-pointer pointer in btree blk */
305 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
306 pp
= XFS_INOBT_PTR_ADDR(block
, 1, cur
);
307 first
= (int)((xfs_caddr_t
)&pp
[pfirst
- 1] - (xfs_caddr_t
)block
);
308 last
= (int)(((xfs_caddr_t
)&pp
[plast
] - 1) - (xfs_caddr_t
)block
);
309 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
313 * Log records from a btree block (leaf).
317 xfs_btree_cur_t
*cur
, /* btree cursor */
318 xfs_buf_t
*bp
, /* buffer containing btree block */
319 int rfirst
, /* index of first record to log */
320 int rlast
) /* index of last record to log */
322 xfs_inobt_block_t
*block
; /* btree block to log from */
323 int first
; /* first byte offset logged */
324 int last
; /* last byte offset logged */
325 xfs_inobt_rec_t
*rp
; /* record pointer for btree block */
327 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
328 rp
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
329 first
= (int)((xfs_caddr_t
)&rp
[rfirst
- 1] - (xfs_caddr_t
)block
);
330 last
= (int)(((xfs_caddr_t
)&rp
[rlast
] - 1) - (xfs_caddr_t
)block
);
331 xfs_trans_log_buf(cur
->bc_tp
, bp
, first
, last
);
335 * Lookup the record. The cursor is made to point to it, based on dir.
336 * Return 0 if can't find any such record, 1 for success.
338 STATIC
int /* error */
340 xfs_btree_cur_t
*cur
, /* btree cursor */
341 xfs_lookup_t dir
, /* <=, ==, or >= */
342 int *stat
) /* success/failure */
344 xfs_agblock_t agbno
; /* a.g. relative btree block number */
345 xfs_agnumber_t agno
; /* allocation group number */
346 xfs_inobt_block_t
*block
=NULL
; /* current btree block */
347 __int64_t diff
; /* difference for the current key */
348 int error
; /* error return value */
349 int keyno
=0; /* current key number */
350 int level
; /* level in the btree */
351 xfs_mount_t
*mp
; /* file system mount point */
354 * Get the allocation group header, and the root block number.
358 xfs_agi_t
*agi
; /* a.g. inode header */
360 agi
= XFS_BUF_TO_AGI(cur
->bc_private
.i
.agbp
);
361 agno
= be32_to_cpu(agi
->agi_seqno
);
362 agbno
= be32_to_cpu(agi
->agi_root
);
365 * Iterate over each level in the btree, starting at the root.
366 * For each level above the leaves, find the key we need, based
367 * on the lookup record, then follow the corresponding block
368 * pointer down to the next level.
370 for (level
= cur
->bc_nlevels
- 1, diff
= 1; level
>= 0; level
--) {
371 xfs_buf_t
*bp
; /* buffer pointer for btree block */
372 xfs_daddr_t d
; /* disk address of btree block */
375 * Get the disk address we're looking for.
377 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
379 * If the old buffer at this level is for a different block,
380 * throw it away, otherwise just use it.
382 bp
= cur
->bc_bufs
[level
];
383 if (bp
&& XFS_BUF_ADDR(bp
) != d
)
387 * Need to get a new buffer. Read it, then
388 * set it in the cursor, releasing the old one.
390 if ((error
= xfs_btree_read_bufs(mp
, cur
->bc_tp
,
391 agno
, agbno
, 0, &bp
, XFS_INO_BTREE_REF
)))
393 xfs_btree_setbuf(cur
, level
, bp
);
395 * Point to the btree block, now that we have the buffer
397 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
398 if ((error
= xfs_btree_check_sblock(cur
, block
, level
,
402 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
404 * If we already had a key match at a higher level, we know
405 * we need to use the first entry in this block.
410 * Otherwise we need to search this block. Do a binary search.
413 int high
; /* high entry number */
414 xfs_inobt_key_t
*kkbase
=NULL
;/* base of keys in block */
415 xfs_inobt_rec_t
*krbase
=NULL
;/* base of records in block */
416 int low
; /* low entry number */
419 * Get a pointer to keys or records.
422 kkbase
= XFS_INOBT_KEY_ADDR(block
, 1, cur
);
424 krbase
= XFS_INOBT_REC_ADDR(block
, 1, cur
);
426 * Set low and high entry numbers, 1-based.
429 if (!(high
= be16_to_cpu(block
->bb_numrecs
))) {
431 * If the block is empty, the tree must
434 ASSERT(level
== 0 && cur
->bc_nlevels
== 1);
435 cur
->bc_ptrs
[0] = dir
!= XFS_LOOKUP_LE
;
440 * Binary search the block.
442 while (low
<= high
) {
443 xfs_agino_t startino
; /* key value */
446 * keyno is average of low and high.
448 keyno
= (low
+ high
) >> 1;
453 xfs_inobt_key_t
*kkp
;
455 kkp
= kkbase
+ keyno
- 1;
456 startino
= INT_GET(kkp
->ir_startino
, ARCH_CONVERT
);
458 xfs_inobt_rec_t
*krp
;
460 krp
= krbase
+ keyno
- 1;
461 startino
= INT_GET(krp
->ir_startino
, ARCH_CONVERT
);
464 * Compute difference to get next direction.
467 startino
- cur
->bc_rec
.i
.ir_startino
;
469 * Less than, move right.
474 * Greater than, move left.
486 * If there are more levels, set up for the next level
487 * by getting the block number and filling in the cursor.
491 * If we moved left, need the previous key number,
492 * unless there isn't one.
494 if (diff
> 0 && --keyno
< 1)
496 agbno
= be32_to_cpu(*XFS_INOBT_PTR_ADDR(block
, keyno
, cur
));
498 if ((error
= xfs_btree_check_sptr(cur
, agbno
, level
)))
501 cur
->bc_ptrs
[level
] = keyno
;
505 * Done with the search.
506 * See if we need to adjust the results.
508 if (dir
!= XFS_LOOKUP_LE
&& diff
< 0) {
511 * If ge search and we went off the end of the block, but it's
512 * not the last block, we're in the wrong block.
514 if (dir
== XFS_LOOKUP_GE
&&
515 keyno
> be16_to_cpu(block
->bb_numrecs
) &&
516 be32_to_cpu(block
->bb_rightsib
) != NULLAGBLOCK
) {
519 cur
->bc_ptrs
[0] = keyno
;
520 if ((error
= xfs_inobt_increment(cur
, 0, &i
)))
527 else if (dir
== XFS_LOOKUP_LE
&& diff
> 0)
529 cur
->bc_ptrs
[0] = keyno
;
531 * Return if we succeeded or not.
533 if (keyno
== 0 || keyno
> be16_to_cpu(block
->bb_numrecs
))
536 *stat
= ((dir
!= XFS_LOOKUP_EQ
) || (diff
== 0));
541 * Move 1 record left from cur/level if possible.
542 * Update cur to reflect the new path.
544 STATIC
int /* error */
546 xfs_btree_cur_t
*cur
, /* btree cursor */
547 int level
, /* level to shift record on */
548 int *stat
) /* success/failure */
550 int error
; /* error return value */
552 int i
; /* loop index */
554 xfs_inobt_key_t key
; /* key value for leaf level upward */
555 xfs_buf_t
*lbp
; /* buffer for left neighbor block */
556 xfs_inobt_block_t
*left
; /* left neighbor btree block */
557 xfs_inobt_key_t
*lkp
=NULL
; /* key pointer for left block */
558 xfs_inobt_ptr_t
*lpp
; /* address pointer for left block */
559 xfs_inobt_rec_t
*lrp
=NULL
; /* record pointer for left block */
560 int nrec
; /* new number of left block entries */
561 xfs_buf_t
*rbp
; /* buffer for right (current) block */
562 xfs_inobt_block_t
*right
; /* right (current) btree block */
563 xfs_inobt_key_t
*rkp
=NULL
; /* key pointer for right block */
564 xfs_inobt_ptr_t
*rpp
=NULL
; /* address pointer for right block */
565 xfs_inobt_rec_t
*rrp
=NULL
; /* record pointer for right block */
568 * Set up variables for this block as "right".
570 rbp
= cur
->bc_bufs
[level
];
571 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
573 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
577 * If we've got no left sibling then we can't shift an entry left.
579 if (be32_to_cpu(right
->bb_leftsib
) == NULLAGBLOCK
) {
584 * If the cursor entry is the one that would be moved, don't
585 * do it... it's too complicated.
587 if (cur
->bc_ptrs
[level
] <= 1) {
592 * Set up the left neighbor as "left".
594 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
595 cur
->bc_private
.i
.agno
, be32_to_cpu(right
->bb_leftsib
),
596 0, &lbp
, XFS_INO_BTREE_REF
)))
598 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
599 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
602 * If it's full, it can't take another entry.
604 if (be16_to_cpu(left
->bb_numrecs
) == XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
608 nrec
= be16_to_cpu(left
->bb_numrecs
) + 1;
610 * If non-leaf, copy a key and a ptr to the left block.
613 lkp
= XFS_INOBT_KEY_ADDR(left
, nrec
, cur
);
614 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
616 xfs_inobt_log_keys(cur
, lbp
, nrec
, nrec
);
617 lpp
= XFS_INOBT_PTR_ADDR(left
, nrec
, cur
);
618 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
620 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(*rpp
), level
)))
623 *lpp
= *rpp
; /* INT_: no-change copy */
624 xfs_inobt_log_ptrs(cur
, lbp
, nrec
, nrec
);
627 * If leaf, copy a record to the left block.
630 lrp
= XFS_INOBT_REC_ADDR(left
, nrec
, cur
);
631 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
633 xfs_inobt_log_recs(cur
, lbp
, nrec
, nrec
);
636 * Bump and log left's numrecs, decrement and log right's numrecs.
638 be16_add(&left
->bb_numrecs
, 1);
639 xfs_inobt_log_block(cur
->bc_tp
, lbp
, XFS_BB_NUMRECS
);
642 xfs_btree_check_key(cur
->bc_btnum
, lkp
- 1, lkp
);
644 xfs_btree_check_rec(cur
->bc_btnum
, lrp
- 1, lrp
);
646 be16_add(&right
->bb_numrecs
, -1);
647 xfs_inobt_log_block(cur
->bc_tp
, rbp
, XFS_BB_NUMRECS
);
649 * Slide the contents of right down one entry.
653 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
654 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
+ 1]),
659 memmove(rkp
, rkp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
660 memmove(rpp
, rpp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
661 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
662 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
664 memmove(rrp
, rrp
+ 1, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
665 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
666 key
.ir_startino
= rrp
->ir_startino
; /* INT_: direct copy */
670 * Update the parent key values of right.
672 if ((error
= xfs_inobt_updkey(cur
, rkp
, level
+ 1)))
675 * Slide the cursor value left one.
677 cur
->bc_ptrs
[level
]--;
683 * Allocate a new root block, fill it in.
685 STATIC
int /* error */
687 xfs_btree_cur_t
*cur
, /* btree cursor */
688 int *stat
) /* success/failure */
690 xfs_agi_t
*agi
; /* a.g. inode header */
691 xfs_alloc_arg_t args
; /* allocation argument structure */
692 xfs_inobt_block_t
*block
; /* one half of the old root block */
693 xfs_buf_t
*bp
; /* buffer containing block */
694 int error
; /* error return value */
695 xfs_inobt_key_t
*kp
; /* btree key pointer */
696 xfs_agblock_t lbno
; /* left block number */
697 xfs_buf_t
*lbp
; /* left buffer pointer */
698 xfs_inobt_block_t
*left
; /* left btree block */
699 xfs_buf_t
*nbp
; /* new (root) buffer */
700 xfs_inobt_block_t
*new; /* new (root) btree block */
701 int nptr
; /* new value for key index, 1 or 2 */
702 xfs_inobt_ptr_t
*pp
; /* btree address pointer */
703 xfs_agblock_t rbno
; /* right block number */
704 xfs_buf_t
*rbp
; /* right buffer pointer */
705 xfs_inobt_block_t
*right
; /* right btree block */
706 xfs_inobt_rec_t
*rp
; /* btree record pointer */
708 ASSERT(cur
->bc_nlevels
< XFS_IN_MAXLEVELS(cur
->bc_mp
));
711 * Get a block & a buffer.
713 agi
= XFS_BUF_TO_AGI(cur
->bc_private
.i
.agbp
);
714 args
.tp
= cur
->bc_tp
;
715 args
.mp
= cur
->bc_mp
;
716 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, cur
->bc_private
.i
.agno
,
717 be32_to_cpu(agi
->agi_root
));
718 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.wasdel
=
719 args
.isfl
= args
.userdata
= args
.minalignslop
= 0;
720 args
.minlen
= args
.maxlen
= args
.prod
= 1;
721 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
722 if ((error
= xfs_alloc_vextent(&args
)))
725 * None available, we fail.
727 if (args
.fsbno
== NULLFSBLOCK
) {
731 ASSERT(args
.len
== 1);
732 nbp
= xfs_btree_get_bufs(args
.mp
, args
.tp
, args
.agno
, args
.agbno
, 0);
733 new = XFS_BUF_TO_INOBT_BLOCK(nbp
);
735 * Set the root data in the a.g. inode structure.
737 agi
->agi_root
= cpu_to_be32(args
.agbno
);
738 be32_add(&agi
->agi_level
, 1);
739 xfs_ialloc_log_agi(args
.tp
, cur
->bc_private
.i
.agbp
,
740 XFS_AGI_ROOT
| XFS_AGI_LEVEL
);
742 * At the previous root level there are now two blocks: the old
743 * root, and the new block generated when it was split.
744 * We don't know which one the cursor is pointing at, so we
745 * set up variables "left" and "right" for each case.
747 bp
= cur
->bc_bufs
[cur
->bc_nlevels
- 1];
748 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
750 if ((error
= xfs_btree_check_sblock(cur
, block
, cur
->bc_nlevels
- 1, bp
)))
753 if (be32_to_cpu(block
->bb_rightsib
) != NULLAGBLOCK
) {
755 * Our block is left, pick up the right block.
758 lbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(lbp
));
760 rbno
= be32_to_cpu(left
->bb_rightsib
);
761 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
762 rbno
, 0, &rbp
, XFS_INO_BTREE_REF
)))
765 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
766 if ((error
= xfs_btree_check_sblock(cur
, right
,
767 cur
->bc_nlevels
- 1, rbp
)))
772 * Our block is right, pick up the left block.
775 rbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(rbp
));
777 lbno
= be32_to_cpu(right
->bb_leftsib
);
778 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
779 lbno
, 0, &lbp
, XFS_INO_BTREE_REF
)))
782 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
783 if ((error
= xfs_btree_check_sblock(cur
, left
,
784 cur
->bc_nlevels
- 1, lbp
)))
789 * Fill in the new block's btree header and log it.
791 new->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
792 new->bb_level
= cpu_to_be16(cur
->bc_nlevels
);
793 new->bb_numrecs
= cpu_to_be16(2);
794 new->bb_leftsib
= cpu_to_be32(NULLAGBLOCK
);
795 new->bb_rightsib
= cpu_to_be32(NULLAGBLOCK
);
796 xfs_inobt_log_block(args
.tp
, nbp
, XFS_BB_ALL_BITS
);
797 ASSERT(lbno
!= NULLAGBLOCK
&& rbno
!= NULLAGBLOCK
);
799 * Fill in the key data in the new root.
801 kp
= XFS_INOBT_KEY_ADDR(new, 1, cur
);
802 if (be16_to_cpu(left
->bb_level
) > 0) {
803 kp
[0] = *XFS_INOBT_KEY_ADDR(left
, 1, cur
); /* INT_: struct copy */
804 kp
[1] = *XFS_INOBT_KEY_ADDR(right
, 1, cur
); /* INT_: struct copy */
806 rp
= XFS_INOBT_REC_ADDR(left
, 1, cur
);
807 INT_COPY(kp
[0].ir_startino
, rp
->ir_startino
, ARCH_CONVERT
);
808 rp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
809 INT_COPY(kp
[1].ir_startino
, rp
->ir_startino
, ARCH_CONVERT
);
811 xfs_inobt_log_keys(cur
, nbp
, 1, 2);
813 * Fill in the pointer data in the new root.
815 pp
= XFS_INOBT_PTR_ADDR(new, 1, cur
);
816 pp
[0] = cpu_to_be32(lbno
);
817 pp
[1] = cpu_to_be32(rbno
);
818 xfs_inobt_log_ptrs(cur
, nbp
, 1, 2);
822 xfs_btree_setbuf(cur
, cur
->bc_nlevels
, nbp
);
823 cur
->bc_ptrs
[cur
->bc_nlevels
] = nptr
;
830 * Move 1 record right from cur/level if possible.
831 * Update cur to reflect the new path.
833 STATIC
int /* error */
835 xfs_btree_cur_t
*cur
, /* btree cursor */
836 int level
, /* level to shift record on */
837 int *stat
) /* success/failure */
839 int error
; /* error return value */
840 int i
; /* loop index */
841 xfs_inobt_key_t key
; /* key value for leaf level upward */
842 xfs_buf_t
*lbp
; /* buffer for left (current) block */
843 xfs_inobt_block_t
*left
; /* left (current) btree block */
844 xfs_inobt_key_t
*lkp
; /* key pointer for left block */
845 xfs_inobt_ptr_t
*lpp
; /* address pointer for left block */
846 xfs_inobt_rec_t
*lrp
; /* record pointer for left block */
847 xfs_buf_t
*rbp
; /* buffer for right neighbor block */
848 xfs_inobt_block_t
*right
; /* right neighbor btree block */
849 xfs_inobt_key_t
*rkp
; /* key pointer for right block */
850 xfs_inobt_ptr_t
*rpp
; /* address pointer for right block */
851 xfs_inobt_rec_t
*rrp
=NULL
; /* record pointer for right block */
852 xfs_btree_cur_t
*tcur
; /* temporary cursor */
855 * Set up variables for this block as "left".
857 lbp
= cur
->bc_bufs
[level
];
858 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
860 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
864 * If we've got no right sibling then we can't shift an entry right.
866 if (be32_to_cpu(left
->bb_rightsib
) == NULLAGBLOCK
) {
871 * If the cursor entry is the one that would be moved, don't
872 * do it... it's too complicated.
874 if (cur
->bc_ptrs
[level
] >= be16_to_cpu(left
->bb_numrecs
)) {
879 * Set up the right neighbor as "right".
881 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
882 cur
->bc_private
.i
.agno
, be32_to_cpu(left
->bb_rightsib
),
883 0, &rbp
, XFS_INO_BTREE_REF
)))
885 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
886 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
889 * If it's full, it can't take another entry.
891 if (be16_to_cpu(right
->bb_numrecs
) == XFS_INOBT_BLOCK_MAXRECS(level
, cur
)) {
896 * Make a hole at the start of the right neighbor block, then
897 * copy the last left block entry to the hole.
900 lkp
= XFS_INOBT_KEY_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
901 lpp
= XFS_INOBT_PTR_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
902 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
903 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
905 for (i
= be16_to_cpu(right
->bb_numrecs
) - 1; i
>= 0; i
--) {
906 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
]), level
)))
910 memmove(rkp
+ 1, rkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
911 memmove(rpp
+ 1, rpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
913 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(*lpp
), level
)))
916 *rkp
= *lkp
; /* INT_: no change copy */
917 *rpp
= *lpp
; /* INT_: no change copy */
918 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
919 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
921 lrp
= XFS_INOBT_REC_ADDR(left
, be16_to_cpu(left
->bb_numrecs
), cur
);
922 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
923 memmove(rrp
+ 1, rrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
925 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
) + 1);
926 key
.ir_startino
= rrp
->ir_startino
; /* INT_: direct copy */
930 * Decrement and log left's numrecs, bump and log right's numrecs.
932 be16_add(&left
->bb_numrecs
, -1);
933 xfs_inobt_log_block(cur
->bc_tp
, lbp
, XFS_BB_NUMRECS
);
934 be16_add(&right
->bb_numrecs
, 1);
937 xfs_btree_check_key(cur
->bc_btnum
, rkp
, rkp
+ 1);
939 xfs_btree_check_rec(cur
->bc_btnum
, rrp
, rrp
+ 1);
941 xfs_inobt_log_block(cur
->bc_tp
, rbp
, XFS_BB_NUMRECS
);
943 * Using a temporary cursor, update the parent key values of the
944 * block on the right.
946 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
948 xfs_btree_lastrec(tcur
, level
);
949 if ((error
= xfs_inobt_increment(tcur
, level
, &i
)) ||
950 (error
= xfs_inobt_updkey(tcur
, rkp
, level
+ 1))) {
951 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
954 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
960 * Split cur/level block in half.
961 * Return new block number and its first record (to be inserted into parent).
963 STATIC
int /* error */
965 xfs_btree_cur_t
*cur
, /* btree cursor */
966 int level
, /* level to split */
967 xfs_agblock_t
*bnop
, /* output: block number allocated */
968 xfs_inobt_key_t
*keyp
, /* output: first key of new block */
969 xfs_btree_cur_t
**curp
, /* output: new cursor */
970 int *stat
) /* success/failure */
972 xfs_alloc_arg_t args
; /* allocation argument structure */
973 int error
; /* error return value */
974 int i
; /* loop index/record number */
975 xfs_agblock_t lbno
; /* left (current) block number */
976 xfs_buf_t
*lbp
; /* buffer for left block */
977 xfs_inobt_block_t
*left
; /* left (current) btree block */
978 xfs_inobt_key_t
*lkp
; /* left btree key pointer */
979 xfs_inobt_ptr_t
*lpp
; /* left btree address pointer */
980 xfs_inobt_rec_t
*lrp
; /* left btree record pointer */
981 xfs_buf_t
*rbp
; /* buffer for right block */
982 xfs_inobt_block_t
*right
; /* right (new) btree block */
983 xfs_inobt_key_t
*rkp
; /* right btree key pointer */
984 xfs_inobt_ptr_t
*rpp
; /* right btree address pointer */
985 xfs_inobt_rec_t
*rrp
; /* right btree record pointer */
988 * Set up left block (current one).
990 lbp
= cur
->bc_bufs
[level
];
991 args
.tp
= cur
->bc_tp
;
992 args
.mp
= cur
->bc_mp
;
993 lbno
= XFS_DADDR_TO_AGBNO(args
.mp
, XFS_BUF_ADDR(lbp
));
995 * Allocate the new block.
996 * If we can't do it, we're toast. Give up.
998 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, cur
->bc_private
.i
.agno
, lbno
);
999 args
.mod
= args
.minleft
= args
.alignment
= args
.total
= args
.wasdel
=
1000 args
.isfl
= args
.userdata
= args
.minalignslop
= 0;
1001 args
.minlen
= args
.maxlen
= args
.prod
= 1;
1002 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
1003 if ((error
= xfs_alloc_vextent(&args
)))
1005 if (args
.fsbno
== NULLFSBLOCK
) {
1009 ASSERT(args
.len
== 1);
1010 rbp
= xfs_btree_get_bufs(args
.mp
, args
.tp
, args
.agno
, args
.agbno
, 0);
1012 * Set up the new block as "right".
1014 right
= XFS_BUF_TO_INOBT_BLOCK(rbp
);
1016 * "Left" is the current (according to the cursor) block.
1018 left
= XFS_BUF_TO_INOBT_BLOCK(lbp
);
1020 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
1024 * Fill in the btree header for the new block.
1026 right
->bb_magic
= cpu_to_be32(xfs_magics
[cur
->bc_btnum
]);
1027 right
->bb_level
= left
->bb_level
;
1028 right
->bb_numrecs
= cpu_to_be16(be16_to_cpu(left
->bb_numrecs
) / 2);
1030 * Make sure that if there's an odd number of entries now, that
1031 * each new block will have the same number of entries.
1033 if ((be16_to_cpu(left
->bb_numrecs
) & 1) &&
1034 cur
->bc_ptrs
[level
] <= be16_to_cpu(right
->bb_numrecs
) + 1)
1035 be16_add(&right
->bb_numrecs
, 1);
1036 i
= be16_to_cpu(left
->bb_numrecs
) - be16_to_cpu(right
->bb_numrecs
) + 1;
1038 * For non-leaf blocks, copy keys and addresses over to the new block.
1041 lkp
= XFS_INOBT_KEY_ADDR(left
, i
, cur
);
1042 lpp
= XFS_INOBT_PTR_ADDR(left
, i
, cur
);
1043 rkp
= XFS_INOBT_KEY_ADDR(right
, 1, cur
);
1044 rpp
= XFS_INOBT_PTR_ADDR(right
, 1, cur
);
1046 for (i
= 0; i
< be16_to_cpu(right
->bb_numrecs
); i
++) {
1047 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(lpp
[i
]), level
)))
1051 memcpy(rkp
, lkp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rkp
));
1052 memcpy(rpp
, lpp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rpp
));
1053 xfs_inobt_log_keys(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1054 xfs_inobt_log_ptrs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1058 * For leaf blocks, copy records over to the new block.
1061 lrp
= XFS_INOBT_REC_ADDR(left
, i
, cur
);
1062 rrp
= XFS_INOBT_REC_ADDR(right
, 1, cur
);
1063 memcpy(rrp
, lrp
, be16_to_cpu(right
->bb_numrecs
) * sizeof(*rrp
));
1064 xfs_inobt_log_recs(cur
, rbp
, 1, be16_to_cpu(right
->bb_numrecs
));
1065 keyp
->ir_startino
= rrp
->ir_startino
; /* INT_: direct copy */
1068 * Find the left block number by looking in the buffer.
1069 * Adjust numrecs, sibling pointers.
1071 be16_add(&left
->bb_numrecs
, -(be16_to_cpu(right
->bb_numrecs
)));
1072 right
->bb_rightsib
= left
->bb_rightsib
;
1073 left
->bb_rightsib
= cpu_to_be32(args
.agbno
);
1074 right
->bb_leftsib
= cpu_to_be32(lbno
);
1075 xfs_inobt_log_block(args
.tp
, rbp
, XFS_BB_ALL_BITS
);
1076 xfs_inobt_log_block(args
.tp
, lbp
, XFS_BB_NUMRECS
| XFS_BB_RIGHTSIB
);
1078 * If there's a block to the new block's right, make that block
1079 * point back to right instead of to left.
1081 if (be32_to_cpu(right
->bb_rightsib
) != NULLAGBLOCK
) {
1082 xfs_inobt_block_t
*rrblock
; /* rr btree block */
1083 xfs_buf_t
*rrbp
; /* buffer for rrblock */
1085 if ((error
= xfs_btree_read_bufs(args
.mp
, args
.tp
, args
.agno
,
1086 be32_to_cpu(right
->bb_rightsib
), 0, &rrbp
,
1087 XFS_INO_BTREE_REF
)))
1089 rrblock
= XFS_BUF_TO_INOBT_BLOCK(rrbp
);
1090 if ((error
= xfs_btree_check_sblock(cur
, rrblock
, level
, rrbp
)))
1092 rrblock
->bb_leftsib
= cpu_to_be32(args
.agbno
);
1093 xfs_inobt_log_block(args
.tp
, rrbp
, XFS_BB_LEFTSIB
);
1096 * If the cursor is really in the right block, move it there.
1097 * If it's just pointing past the last entry in left, then we'll
1098 * insert there, so don't change anything in that case.
1100 if (cur
->bc_ptrs
[level
] > be16_to_cpu(left
->bb_numrecs
) + 1) {
1101 xfs_btree_setbuf(cur
, level
, rbp
);
1102 cur
->bc_ptrs
[level
] -= be16_to_cpu(left
->bb_numrecs
);
1105 * If there are more levels, we'll need another cursor which refers
1106 * the right block, no matter where this cursor was.
1108 if (level
+ 1 < cur
->bc_nlevels
) {
1109 if ((error
= xfs_btree_dup_cursor(cur
, curp
)))
1111 (*curp
)->bc_ptrs
[level
+ 1]++;
1119 * Update keys at all levels from here to the root along the cursor's path.
1121 STATIC
int /* error */
1123 xfs_btree_cur_t
*cur
, /* btree cursor */
1124 xfs_inobt_key_t
*keyp
, /* new key value to update to */
1125 int level
) /* starting level for update */
1127 int ptr
; /* index of key in block */
1130 * Go up the tree from this level toward the root.
1131 * At each level, update the key value to the value input.
1132 * Stop when we reach a level where the cursor isn't pointing
1133 * at the first entry in the block.
1135 for (ptr
= 1; ptr
== 1 && level
< cur
->bc_nlevels
; level
++) {
1136 xfs_buf_t
*bp
; /* buffer for block */
1137 xfs_inobt_block_t
*block
; /* btree block */
1139 int error
; /* error return value */
1141 xfs_inobt_key_t
*kp
; /* ptr to btree block keys */
1143 bp
= cur
->bc_bufs
[level
];
1144 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1146 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
1149 ptr
= cur
->bc_ptrs
[level
];
1150 kp
= XFS_INOBT_KEY_ADDR(block
, ptr
, cur
);
1152 xfs_inobt_log_keys(cur
, bp
, ptr
, ptr
);
1158 * Externally visible routines.
1162 * Decrement cursor by one record at the level.
1163 * For nonzero levels the leaf-ward information is untouched.
1166 xfs_inobt_decrement(
1167 xfs_btree_cur_t
*cur
, /* btree cursor */
1168 int level
, /* level in btree, 0 is leaf */
1169 int *stat
) /* success/failure */
1171 xfs_inobt_block_t
*block
; /* btree block */
1173 int lev
; /* btree level */
1175 ASSERT(level
< cur
->bc_nlevels
);
1177 * Read-ahead to the left at this level.
1179 xfs_btree_readahead(cur
, level
, XFS_BTCUR_LEFTRA
);
1181 * Decrement the ptr at this level. If we're still in the block
1184 if (--cur
->bc_ptrs
[level
] > 0) {
1189 * Get a pointer to the btree block.
1191 block
= XFS_BUF_TO_INOBT_BLOCK(cur
->bc_bufs
[level
]);
1193 if ((error
= xfs_btree_check_sblock(cur
, block
, level
,
1194 cur
->bc_bufs
[level
])))
1198 * If we just went off the left edge of the tree, return failure.
1200 if (be32_to_cpu(block
->bb_leftsib
) == NULLAGBLOCK
) {
1205 * March up the tree decrementing pointers.
1206 * Stop when we don't go off the left edge of a block.
1208 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1209 if (--cur
->bc_ptrs
[lev
] > 0)
1212 * Read-ahead the left block, we're going to read it
1215 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_LEFTRA
);
1218 * If we went off the root then we are seriously confused.
1220 ASSERT(lev
< cur
->bc_nlevels
);
1222 * Now walk back down the tree, fixing up the cursor's buffer
1223 * pointers and key numbers.
1225 for (block
= XFS_BUF_TO_INOBT_BLOCK(cur
->bc_bufs
[lev
]); lev
> level
; ) {
1226 xfs_agblock_t agbno
; /* block number of btree block */
1227 xfs_buf_t
*bp
; /* buffer containing btree block */
1229 agbno
= be32_to_cpu(*XFS_INOBT_PTR_ADDR(block
, cur
->bc_ptrs
[lev
], cur
));
1230 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
1231 cur
->bc_private
.i
.agno
, agbno
, 0, &bp
,
1232 XFS_INO_BTREE_REF
)))
1235 xfs_btree_setbuf(cur
, lev
, bp
);
1236 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1237 if ((error
= xfs_btree_check_sblock(cur
, block
, lev
, bp
)))
1239 cur
->bc_ptrs
[lev
] = be16_to_cpu(block
->bb_numrecs
);
1246 * Get the data from the pointed-to record.
1250 xfs_btree_cur_t
*cur
, /* btree cursor */
1251 xfs_agino_t
*ino
, /* output: starting inode of chunk */
1252 __int32_t
*fcnt
, /* output: number of free inodes */
1253 xfs_inofree_t
*free
, /* output: free inode mask */
1254 int *stat
) /* output: success/failure */
1256 xfs_inobt_block_t
*block
; /* btree block */
1257 xfs_buf_t
*bp
; /* buffer containing btree block */
1259 int error
; /* error return value */
1261 int ptr
; /* record number */
1262 xfs_inobt_rec_t
*rec
; /* record data */
1264 bp
= cur
->bc_bufs
[0];
1265 ptr
= cur
->bc_ptrs
[0];
1266 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1268 if ((error
= xfs_btree_check_sblock(cur
, block
, 0, bp
)))
1272 * Off the right end or left end, return failure.
1274 if (ptr
> be16_to_cpu(block
->bb_numrecs
) || ptr
<= 0) {
1279 * Point to the record and extract its data.
1281 rec
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
1282 *ino
= INT_GET(rec
->ir_startino
, ARCH_CONVERT
);
1283 *fcnt
= INT_GET(rec
->ir_freecount
, ARCH_CONVERT
);
1284 *free
= INT_GET(rec
->ir_free
, ARCH_CONVERT
);
1290 * Increment cursor by one record at the level.
1291 * For nonzero levels the leaf-ward information is untouched.
1294 xfs_inobt_increment(
1295 xfs_btree_cur_t
*cur
, /* btree cursor */
1296 int level
, /* level in btree, 0 is leaf */
1297 int *stat
) /* success/failure */
1299 xfs_inobt_block_t
*block
; /* btree block */
1300 xfs_buf_t
*bp
; /* buffer containing btree block */
1301 int error
; /* error return value */
1302 int lev
; /* btree level */
1304 ASSERT(level
< cur
->bc_nlevels
);
1306 * Read-ahead to the right at this level.
1308 xfs_btree_readahead(cur
, level
, XFS_BTCUR_RIGHTRA
);
1310 * Get a pointer to the btree block.
1312 bp
= cur
->bc_bufs
[level
];
1313 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1315 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
1319 * Increment the ptr at this level. If we're still in the block
1322 if (++cur
->bc_ptrs
[level
] <= be16_to_cpu(block
->bb_numrecs
)) {
1327 * If we just went off the right edge of the tree, return failure.
1329 if (be32_to_cpu(block
->bb_rightsib
) == NULLAGBLOCK
) {
1334 * March up the tree incrementing pointers.
1335 * Stop when we don't go off the right edge of a block.
1337 for (lev
= level
+ 1; lev
< cur
->bc_nlevels
; lev
++) {
1338 bp
= cur
->bc_bufs
[lev
];
1339 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1341 if ((error
= xfs_btree_check_sblock(cur
, block
, lev
, bp
)))
1344 if (++cur
->bc_ptrs
[lev
] <= be16_to_cpu(block
->bb_numrecs
))
1347 * Read-ahead the right block, we're going to read it
1350 xfs_btree_readahead(cur
, lev
, XFS_BTCUR_RIGHTRA
);
1353 * If we went off the root then we are seriously confused.
1355 ASSERT(lev
< cur
->bc_nlevels
);
1357 * Now walk back down the tree, fixing up the cursor's buffer
1358 * pointers and key numbers.
1360 for (bp
= cur
->bc_bufs
[lev
], block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1362 xfs_agblock_t agbno
; /* block number of btree block */
1364 agbno
= be32_to_cpu(*XFS_INOBT_PTR_ADDR(block
, cur
->bc_ptrs
[lev
], cur
));
1365 if ((error
= xfs_btree_read_bufs(cur
->bc_mp
, cur
->bc_tp
,
1366 cur
->bc_private
.i
.agno
, agbno
, 0, &bp
,
1367 XFS_INO_BTREE_REF
)))
1370 xfs_btree_setbuf(cur
, lev
, bp
);
1371 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1372 if ((error
= xfs_btree_check_sblock(cur
, block
, lev
, bp
)))
1374 cur
->bc_ptrs
[lev
] = 1;
1381 * Insert the current record at the point referenced by cur.
1382 * The cursor may be inconsistent on return if splits have been done.
1386 xfs_btree_cur_t
*cur
, /* btree cursor */
1387 int *stat
) /* success/failure */
1389 int error
; /* error return value */
1390 int i
; /* result value, 0 for failure */
1391 int level
; /* current level number in btree */
1392 xfs_agblock_t nbno
; /* new block number (split result) */
1393 xfs_btree_cur_t
*ncur
; /* new cursor (split result) */
1394 xfs_inobt_rec_t nrec
; /* record being inserted this level */
1395 xfs_btree_cur_t
*pcur
; /* previous level's cursor */
1399 INT_SET(nrec
.ir_startino
, ARCH_CONVERT
, cur
->bc_rec
.i
.ir_startino
);
1400 INT_SET(nrec
.ir_freecount
, ARCH_CONVERT
, cur
->bc_rec
.i
.ir_freecount
);
1401 INT_SET(nrec
.ir_free
, ARCH_CONVERT
, cur
->bc_rec
.i
.ir_free
);
1402 ncur
= (xfs_btree_cur_t
*)0;
1405 * Loop going up the tree, starting at the leaf level.
1406 * Stop when we don't get a split block, that must mean that
1407 * the insert is finished with this level.
1411 * Insert nrec/nbno into this level of the tree.
1412 * Note if we fail, nbno will be null.
1414 if ((error
= xfs_inobt_insrec(pcur
, level
++, &nbno
, &nrec
, &ncur
,
1417 xfs_btree_del_cursor(pcur
, XFS_BTREE_ERROR
);
1421 * See if the cursor we just used is trash.
1422 * Can't trash the caller's cursor, but otherwise we should
1423 * if ncur is a new cursor or we're about to be done.
1425 if (pcur
!= cur
&& (ncur
|| nbno
== NULLAGBLOCK
)) {
1426 cur
->bc_nlevels
= pcur
->bc_nlevels
;
1427 xfs_btree_del_cursor(pcur
, XFS_BTREE_NOERROR
);
1430 * If we got a new cursor, switch to it.
1434 ncur
= (xfs_btree_cur_t
*)0;
1436 } while (nbno
!= NULLAGBLOCK
);
1442 * Lookup the record equal to ino in the btree given by cur.
1445 xfs_inobt_lookup_eq(
1446 xfs_btree_cur_t
*cur
, /* btree cursor */
1447 xfs_agino_t ino
, /* starting inode of chunk */
1448 __int32_t fcnt
, /* free inode count */
1449 xfs_inofree_t free
, /* free inode mask */
1450 int *stat
) /* success/failure */
1452 cur
->bc_rec
.i
.ir_startino
= ino
;
1453 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
1454 cur
->bc_rec
.i
.ir_free
= free
;
1455 return xfs_inobt_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
1459 * Lookup the first record greater than or equal to ino
1460 * in the btree given by cur.
1463 xfs_inobt_lookup_ge(
1464 xfs_btree_cur_t
*cur
, /* btree cursor */
1465 xfs_agino_t ino
, /* starting inode of chunk */
1466 __int32_t fcnt
, /* free inode count */
1467 xfs_inofree_t free
, /* free inode mask */
1468 int *stat
) /* success/failure */
1470 cur
->bc_rec
.i
.ir_startino
= ino
;
1471 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
1472 cur
->bc_rec
.i
.ir_free
= free
;
1473 return xfs_inobt_lookup(cur
, XFS_LOOKUP_GE
, stat
);
1477 * Lookup the first record less than or equal to ino
1478 * in the btree given by cur.
1481 xfs_inobt_lookup_le(
1482 xfs_btree_cur_t
*cur
, /* btree cursor */
1483 xfs_agino_t ino
, /* starting inode of chunk */
1484 __int32_t fcnt
, /* free inode count */
1485 xfs_inofree_t free
, /* free inode mask */
1486 int *stat
) /* success/failure */
1488 cur
->bc_rec
.i
.ir_startino
= ino
;
1489 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
1490 cur
->bc_rec
.i
.ir_free
= free
;
1491 return xfs_inobt_lookup(cur
, XFS_LOOKUP_LE
, stat
);
1495 * Update the record referred to by cur, to the value given
1496 * by [ino, fcnt, free].
1497 * This either works (return 0) or gets an EFSCORRUPTED error.
1501 xfs_btree_cur_t
*cur
, /* btree cursor */
1502 xfs_agino_t ino
, /* starting inode of chunk */
1503 __int32_t fcnt
, /* free inode count */
1504 xfs_inofree_t free
) /* free inode mask */
1506 xfs_inobt_block_t
*block
; /* btree block to update */
1507 xfs_buf_t
*bp
; /* buffer containing btree block */
1508 int error
; /* error return value */
1509 int ptr
; /* current record number (updating) */
1510 xfs_inobt_rec_t
*rp
; /* pointer to updated record */
1513 * Pick up the current block.
1515 bp
= cur
->bc_bufs
[0];
1516 block
= XFS_BUF_TO_INOBT_BLOCK(bp
);
1518 if ((error
= xfs_btree_check_sblock(cur
, block
, 0, bp
)))
1522 * Get the address of the rec to be updated.
1524 ptr
= cur
->bc_ptrs
[0];
1525 rp
= XFS_INOBT_REC_ADDR(block
, ptr
, cur
);
1527 * Fill in the new contents and log them.
1529 INT_SET(rp
->ir_startino
, ARCH_CONVERT
, ino
);
1530 INT_SET(rp
->ir_freecount
, ARCH_CONVERT
, fcnt
);
1531 INT_SET(rp
->ir_free
, ARCH_CONVERT
, free
);
1532 xfs_inobt_log_recs(cur
, bp
, ptr
, ptr
);
1534 * Updating first record in leaf. Pass new key value up to our parent.
1537 xfs_inobt_key_t key
; /* key containing [ino] */
1539 INT_SET(key
.ir_startino
, ARCH_CONVERT
, ino
);
1540 if ((error
= xfs_inobt_updkey(cur
, &key
, 1)))