[XFS] implement generic xfs_btree_lshift
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_alloc_btree.c
blob974a412ebc8a34d571f91e3f895cae0afccca12a
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.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"
43 * Prototypes for internal functions.
46 STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
47 STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
48 STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
49 STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
50 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
51 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
52 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
55 * Internal functions.
59 * Single level of the xfs_alloc_delete record deletion routine.
60 * Delete record pointed to by cur/level.
61 * Remove the record from its block then rebalance the tree.
62 * Return 0 for error, 1 for done, 2 to go on to the next level.
64 STATIC int /* error */
65 xfs_alloc_delrec(
66 xfs_btree_cur_t *cur, /* btree cursor */
67 int level, /* level removing record from */
68 int *stat) /* fail/done/go-on */
70 xfs_agf_t *agf; /* allocation group freelist header */
71 xfs_alloc_block_t *block; /* btree block record/key lives in */
72 xfs_agblock_t bno; /* btree block number */
73 xfs_buf_t *bp; /* buffer for block */
74 int error; /* error return value */
75 int i; /* loop index */
76 xfs_alloc_key_t key; /* kp points here if block is level 0 */
77 xfs_agblock_t lbno; /* left block's block number */
78 xfs_buf_t *lbp; /* left block's buffer pointer */
79 xfs_alloc_block_t *left; /* left btree block */
80 xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
81 xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
82 int lrecs=0; /* number of records in left block */
83 xfs_alloc_rec_t *lrp; /* left block record pointer */
84 xfs_mount_t *mp; /* mount structure */
85 int ptr; /* index in btree block for this rec */
86 xfs_agblock_t rbno; /* right block's block number */
87 xfs_buf_t *rbp; /* right block's buffer pointer */
88 xfs_alloc_block_t *right; /* right btree block */
89 xfs_alloc_key_t *rkp; /* right block key pointer */
90 xfs_alloc_ptr_t *rpp; /* right block address pointer */
91 int rrecs=0; /* number of records in right block */
92 int numrecs;
93 xfs_alloc_rec_t *rrp; /* right block record pointer */
94 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];
100 if (ptr == 0) {
101 *stat = 0;
102 return 0;
105 * Get the buffer & block containing the record or key/ptr.
107 bp = cur->bc_bufs[level];
108 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
109 #ifdef DEBUG
110 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
111 return error;
112 #endif
114 * Fail if we're off the end of the block.
116 numrecs = be16_to_cpu(block->bb_numrecs);
117 if (ptr > numrecs) {
118 *stat = 0;
119 return 0;
121 XFS_STATS_INC(xs_abt_delrec);
123 * It's a nonleaf. Excise the key and ptr being deleted, by
124 * sliding the entries past them down one.
125 * Log the changed areas of the block.
127 if (level > 0) {
128 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
129 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
130 #ifdef DEBUG
131 for (i = ptr; i < numrecs; i++) {
132 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
133 return error;
135 #endif
136 if (ptr < numrecs) {
137 memmove(&lkp[ptr - 1], &lkp[ptr],
138 (numrecs - ptr) * sizeof(*lkp));
139 memmove(&lpp[ptr - 1], &lpp[ptr],
140 (numrecs - ptr) * sizeof(*lpp));
141 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
142 xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
146 * It's a leaf. Excise the record being deleted, by sliding the
147 * entries past it down one. Log the changed areas of the block.
149 else {
150 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
151 if (ptr < numrecs) {
152 memmove(&lrp[ptr - 1], &lrp[ptr],
153 (numrecs - ptr) * sizeof(*lrp));
154 xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
157 * If it's the first record in the block, we'll need a key
158 * structure to pass up to the next level (updkey).
160 if (ptr == 1) {
161 key.ar_startblock = lrp->ar_startblock;
162 key.ar_blockcount = lrp->ar_blockcount;
163 lkp = &key;
167 * Decrement and log the number of entries in the block.
169 numrecs--;
170 block->bb_numrecs = cpu_to_be16(numrecs);
171 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
173 * See if the longest free extent in the allocation group was
174 * changed by this operation. True if it's the by-size btree, and
175 * this is the leaf level, and there is no right sibling block,
176 * and this was the last record.
178 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
179 mp = cur->bc_mp;
181 if (level == 0 &&
182 cur->bc_btnum == XFS_BTNUM_CNT &&
183 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
184 ptr > numrecs) {
185 ASSERT(ptr == numrecs + 1);
187 * There are still records in the block. Grab the size
188 * from the last one.
190 if (numrecs) {
191 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
192 agf->agf_longest = rrp->ar_blockcount;
195 * No free extents left.
197 else
198 agf->agf_longest = 0;
199 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
200 be32_to_cpu(agf->agf_longest);
201 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
202 XFS_AGF_LONGEST);
205 * Is this the root level? If so, we're almost done.
207 if (level == cur->bc_nlevels - 1) {
209 * If this is the root level,
210 * and there's only one entry left,
211 * and it's NOT the leaf level,
212 * then we can get rid of this level.
214 if (numrecs == 1 && level > 0) {
216 * lpp is still set to the first pointer in the block.
217 * Make it the new root of the btree.
219 bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
220 agf->agf_roots[cur->bc_btnum] = *lpp;
221 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
222 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
224 * Put this buffer/block on the ag's freelist.
226 error = xfs_alloc_put_freelist(cur->bc_tp,
227 cur->bc_private.a.agbp, NULL, bno, 1);
228 if (error)
229 return error;
231 * Since blocks move to the free list without the
232 * coordination used in xfs_bmap_finish, we can't allow
233 * block to be available for reallocation and
234 * non-transaction writing (user data) until we know
235 * that the transaction that moved it to the free list
236 * is permanently on disk. We track the blocks by
237 * declaring these blocks as "busy"; the busy list is
238 * maintained on a per-ag basis and each transaction
239 * records which entries should be removed when the
240 * iclog commits to disk. If a busy block is
241 * allocated, the iclog is pushed up to the LSN
242 * that freed the block.
244 xfs_alloc_mark_busy(cur->bc_tp,
245 be32_to_cpu(agf->agf_seqno), bno, 1);
247 xfs_trans_agbtree_delta(cur->bc_tp, -1);
248 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
249 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
251 * Update the cursor so there's one fewer level.
253 xfs_btree_setbuf(cur, level, NULL);
254 cur->bc_nlevels--;
255 } else if (level > 0 &&
256 (error = xfs_btree_decrement(cur, level, &i)))
257 return error;
258 *stat = 1;
259 return 0;
262 * If we deleted the leftmost entry in the block, update the
263 * key values above us in the tree.
265 if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
266 return error;
268 * If the number of records remaining in the block is at least
269 * the minimum, we're done.
271 if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
272 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
273 return error;
274 *stat = 1;
275 return 0;
278 * Otherwise, we have to move some records around to keep the
279 * tree balanced. Look at the left and right sibling blocks to
280 * see if we can re-balance by moving only one record.
282 rbno = be32_to_cpu(block->bb_rightsib);
283 lbno = be32_to_cpu(block->bb_leftsib);
284 bno = NULLAGBLOCK;
285 ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
287 * Duplicate the cursor so our btree manipulations here won't
288 * disrupt the next level up.
290 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
291 return error;
293 * If there's a right sibling, see if it's ok to shift an entry
294 * out of it.
296 if (rbno != NULLAGBLOCK) {
298 * Move the temp cursor to the last entry in the next block.
299 * Actually any entry but the first would suffice.
301 i = xfs_btree_lastrec(tcur, level);
302 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
303 if ((error = xfs_btree_increment(tcur, level, &i)))
304 goto error0;
305 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
306 i = xfs_btree_lastrec(tcur, level);
307 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
309 * Grab a pointer to the block.
311 rbp = tcur->bc_bufs[level];
312 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
313 #ifdef DEBUG
314 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
315 goto error0;
316 #endif
318 * Grab the current block number, for future use.
320 bno = be32_to_cpu(right->bb_leftsib);
322 * If right block is full enough so that removing one entry
323 * won't make it too empty, and left-shifting an entry out
324 * of right to us works, we're done.
326 if (be16_to_cpu(right->bb_numrecs) - 1 >=
327 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
328 if ((error = xfs_btree_lshift(tcur, level, &i)))
329 goto error0;
330 if (i) {
331 ASSERT(be16_to_cpu(block->bb_numrecs) >=
332 XFS_ALLOC_BLOCK_MINRECS(level, cur));
333 xfs_btree_del_cursor(tcur,
334 XFS_BTREE_NOERROR);
335 if (level > 0 &&
336 (error = xfs_btree_decrement(cur, level,
337 &i)))
338 return error;
339 *stat = 1;
340 return 0;
344 * Otherwise, grab the number of records in right for
345 * future reference, and fix up the temp cursor to point
346 * to our block again (last record).
348 rrecs = be16_to_cpu(right->bb_numrecs);
349 if (lbno != NULLAGBLOCK) {
350 i = xfs_btree_firstrec(tcur, level);
351 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
352 if ((error = xfs_btree_decrement(tcur, level, &i)))
353 goto error0;
354 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
358 * If there's a left sibling, see if it's ok to shift an entry
359 * out of it.
361 if (lbno != NULLAGBLOCK) {
363 * Move the temp cursor to the first entry in the
364 * previous block.
366 i = xfs_btree_firstrec(tcur, level);
367 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
368 if ((error = xfs_btree_decrement(tcur, level, &i)))
369 goto error0;
370 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
371 xfs_btree_firstrec(tcur, level);
373 * Grab a pointer to the block.
375 lbp = tcur->bc_bufs[level];
376 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
377 #ifdef DEBUG
378 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
379 goto error0;
380 #endif
382 * Grab the current block number, for future use.
384 bno = be32_to_cpu(left->bb_rightsib);
386 * If left block is full enough so that removing one entry
387 * won't make it too empty, and right-shifting an entry out
388 * of left to us works, we're done.
390 if (be16_to_cpu(left->bb_numrecs) - 1 >=
391 XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
392 if ((error = xfs_btree_rshift(tcur, level, &i)))
393 goto error0;
394 if (i) {
395 ASSERT(be16_to_cpu(block->bb_numrecs) >=
396 XFS_ALLOC_BLOCK_MINRECS(level, cur));
397 xfs_btree_del_cursor(tcur,
398 XFS_BTREE_NOERROR);
399 if (level == 0)
400 cur->bc_ptrs[0]++;
401 *stat = 1;
402 return 0;
406 * Otherwise, grab the number of records in right for
407 * future reference.
409 lrecs = be16_to_cpu(left->bb_numrecs);
412 * Delete the temp cursor, we're done with it.
414 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
416 * If here, we need to do a join to keep the tree balanced.
418 ASSERT(bno != NULLAGBLOCK);
420 * See if we can join with the left neighbor block.
422 if (lbno != NULLAGBLOCK &&
423 lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
425 * Set "right" to be the starting block,
426 * "left" to be the left neighbor.
428 rbno = bno;
429 right = block;
430 rrecs = be16_to_cpu(right->bb_numrecs);
431 rbp = bp;
432 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
433 cur->bc_private.a.agno, lbno, 0, &lbp,
434 XFS_ALLOC_BTREE_REF)))
435 return error;
436 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
437 lrecs = be16_to_cpu(left->bb_numrecs);
438 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
439 return error;
442 * If that won't work, see if we can join with the right neighbor block.
444 else if (rbno != NULLAGBLOCK &&
445 rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
447 * Set "left" to be the starting block,
448 * "right" to be the right neighbor.
450 lbno = bno;
451 left = block;
452 lrecs = be16_to_cpu(left->bb_numrecs);
453 lbp = bp;
454 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
455 cur->bc_private.a.agno, rbno, 0, &rbp,
456 XFS_ALLOC_BTREE_REF)))
457 return error;
458 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
459 rrecs = be16_to_cpu(right->bb_numrecs);
460 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
461 return error;
464 * Otherwise, we can't fix the imbalance.
465 * Just return. This is probably a logic error, but it's not fatal.
467 else {
468 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
469 return error;
470 *stat = 1;
471 return 0;
474 * We're now going to join "left" and "right" by moving all the stuff
475 * in "right" to "left" and deleting "right".
477 if (level > 0) {
479 * It's a non-leaf. Move keys and pointers.
481 lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
482 lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
483 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
484 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
485 #ifdef DEBUG
486 for (i = 0; i < rrecs; i++) {
487 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
488 return error;
490 #endif
491 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
492 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
493 xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
494 xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
495 } else {
497 * It's a leaf. Move records.
499 lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
500 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
501 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
502 xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
505 * If we joined with the left neighbor, set the buffer in the
506 * cursor to the left block, and fix up the index.
508 if (bp != lbp) {
509 xfs_btree_setbuf(cur, level, lbp);
510 cur->bc_ptrs[level] += lrecs;
513 * If we joined with the right neighbor and there's a level above
514 * us, increment the cursor at that level.
516 else if (level + 1 < cur->bc_nlevels &&
517 (error = xfs_btree_increment(cur, level + 1, &i)))
518 return error;
520 * Fix up the number of records in the surviving block.
522 lrecs += rrecs;
523 left->bb_numrecs = cpu_to_be16(lrecs);
525 * Fix up the right block pointer in the surviving block, and log it.
527 left->bb_rightsib = right->bb_rightsib;
528 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
530 * If there is a right sibling now, make it point to the
531 * remaining block.
533 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
534 xfs_alloc_block_t *rrblock;
535 xfs_buf_t *rrbp;
537 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
538 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
539 &rrbp, XFS_ALLOC_BTREE_REF)))
540 return error;
541 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
542 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
543 return error;
544 rrblock->bb_leftsib = cpu_to_be32(lbno);
545 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
548 * Free the deleting block by putting it on the freelist.
550 error = xfs_alloc_put_freelist(cur->bc_tp,
551 cur->bc_private.a.agbp, NULL, rbno, 1);
552 if (error)
553 return error;
555 * Since blocks move to the free list without the coordination
556 * used in xfs_bmap_finish, we can't allow block to be available
557 * for reallocation and non-transaction writing (user data)
558 * until we know that the transaction that moved it to the free
559 * list is permanently on disk. We track the blocks by declaring
560 * these blocks as "busy"; the busy list is maintained on a
561 * per-ag basis and each transaction records which entries
562 * should be removed when the iclog commits to disk. If a
563 * busy block is allocated, the iclog is pushed up to the
564 * LSN that freed the block.
566 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
567 xfs_trans_agbtree_delta(cur->bc_tp, -1);
570 * Adjust the current level's cursor so that we're left referring
571 * to the right node, after we're done.
572 * If this leaves the ptr value 0 our caller will fix it up.
574 if (level > 0)
575 cur->bc_ptrs[level]--;
577 * Return value means the next level up has something to do.
579 *stat = 2;
580 return 0;
582 error0:
583 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
584 return error;
588 * Insert one record/level. Return information to the caller
589 * allowing the next level up to proceed if necessary.
591 STATIC int /* error */
592 xfs_alloc_insrec(
593 xfs_btree_cur_t *cur, /* btree cursor */
594 int level, /* level to insert record at */
595 xfs_agblock_t *bnop, /* i/o: block number inserted */
596 xfs_alloc_rec_t *recp, /* i/o: record data inserted */
597 xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
598 int *stat) /* output: success/failure */
600 xfs_agf_t *agf; /* allocation group freelist header */
601 xfs_alloc_block_t *block; /* btree block record/key lives in */
602 xfs_buf_t *bp; /* buffer for block */
603 int error; /* error return value */
604 int i; /* loop index */
605 xfs_alloc_key_t key; /* key value being inserted */
606 xfs_alloc_key_t *kp; /* pointer to btree keys */
607 xfs_agblock_t nbno; /* block number of allocated block */
608 xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
609 xfs_alloc_key_t nkey; /* new key value, from split */
610 xfs_alloc_rec_t nrec; /* new record value, for caller */
611 int numrecs;
612 int optr; /* old ptr value */
613 xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
614 int ptr; /* index in btree block for this rec */
615 xfs_alloc_rec_t *rp; /* pointer to btree records */
617 ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
620 * GCC doesn't understand the (arguably complex) control flow in
621 * this function and complains about uninitialized structure fields
622 * without this.
624 memset(&nrec, 0, sizeof(nrec));
627 * If we made it to the root level, allocate a new root block
628 * and we're done.
630 if (level >= cur->bc_nlevels) {
631 XFS_STATS_INC(xs_abt_insrec);
632 if ((error = xfs_alloc_newroot(cur, &i)))
633 return error;
634 *bnop = NULLAGBLOCK;
635 *stat = i;
636 return 0;
639 * Make a key out of the record data to be inserted, and save it.
641 key.ar_startblock = recp->ar_startblock;
642 key.ar_blockcount = recp->ar_blockcount;
643 optr = ptr = cur->bc_ptrs[level];
645 * If we're off the left edge, return failure.
647 if (ptr == 0) {
648 *stat = 0;
649 return 0;
651 XFS_STATS_INC(xs_abt_insrec);
653 * Get pointers to the btree buffer and block.
655 bp = cur->bc_bufs[level];
656 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
657 numrecs = be16_to_cpu(block->bb_numrecs);
658 #ifdef DEBUG
659 if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
660 return error;
662 * Check that the new entry is being inserted in the right place.
664 if (ptr <= numrecs) {
665 if (level == 0) {
666 rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
667 xfs_btree_check_rec(cur->bc_btnum, recp, rp);
668 } else {
669 kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
670 xfs_btree_check_key(cur->bc_btnum, &key, kp);
673 #endif
674 nbno = NULLAGBLOCK;
675 ncur = NULL;
677 * If the block is full, we can't insert the new entry until we
678 * make the block un-full.
680 if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
682 * First, try shifting an entry to the right neighbor.
684 if ((error = xfs_btree_rshift(cur, level, &i)))
685 return error;
686 if (i) {
687 /* nothing */
690 * Next, try shifting an entry to the left neighbor.
692 else {
693 if ((error = xfs_btree_lshift(cur, level, &i)))
694 return error;
695 if (i)
696 optr = ptr = cur->bc_ptrs[level];
697 else {
699 * Next, try splitting the current block in
700 * half. If this works we have to re-set our
701 * variables because we could be in a
702 * different block now.
704 if ((error = xfs_alloc_split(cur, level, &nbno,
705 &nkey, &ncur, &i)))
706 return error;
707 if (i) {
708 bp = cur->bc_bufs[level];
709 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
710 #ifdef DEBUG
711 if ((error =
712 xfs_btree_check_sblock(cur,
713 block, level, bp)))
714 return error;
715 #endif
716 ptr = cur->bc_ptrs[level];
717 nrec.ar_startblock = nkey.ar_startblock;
718 nrec.ar_blockcount = nkey.ar_blockcount;
721 * Otherwise the insert fails.
723 else {
724 *stat = 0;
725 return 0;
731 * At this point we know there's room for our new entry in the block
732 * we're pointing at.
734 numrecs = be16_to_cpu(block->bb_numrecs);
735 if (level > 0) {
737 * It's a non-leaf entry. Make a hole for the new data
738 * in the key and ptr regions of the block.
740 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
741 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
742 #ifdef DEBUG
743 for (i = numrecs; i >= ptr; i--) {
744 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
745 return error;
747 #endif
748 memmove(&kp[ptr], &kp[ptr - 1],
749 (numrecs - ptr + 1) * sizeof(*kp));
750 memmove(&pp[ptr], &pp[ptr - 1],
751 (numrecs - ptr + 1) * sizeof(*pp));
752 #ifdef DEBUG
753 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
754 return error;
755 #endif
757 * Now stuff the new data in, bump numrecs and log the new data.
759 kp[ptr - 1] = key;
760 pp[ptr - 1] = cpu_to_be32(*bnop);
761 numrecs++;
762 block->bb_numrecs = cpu_to_be16(numrecs);
763 xfs_alloc_log_keys(cur, bp, ptr, numrecs);
764 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
765 #ifdef DEBUG
766 if (ptr < numrecs)
767 xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
768 kp + ptr);
769 #endif
770 } else {
772 * It's a leaf entry. Make a hole for the new record.
774 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
775 memmove(&rp[ptr], &rp[ptr - 1],
776 (numrecs - ptr + 1) * sizeof(*rp));
778 * Now stuff the new record in, bump numrecs
779 * and log the new data.
781 rp[ptr - 1] = *recp;
782 numrecs++;
783 block->bb_numrecs = cpu_to_be16(numrecs);
784 xfs_alloc_log_recs(cur, bp, ptr, numrecs);
785 #ifdef DEBUG
786 if (ptr < numrecs)
787 xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
788 rp + ptr);
789 #endif
792 * Log the new number of records in the btree header.
794 xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
796 * If we inserted at the start of a block, update the parents' keys.
798 if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
799 return error;
801 * Look to see if the longest extent in the allocation group
802 * needs to be updated.
805 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
806 if (level == 0 &&
807 cur->bc_btnum == XFS_BTNUM_CNT &&
808 be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
809 be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
811 * If this is a leaf in the by-size btree and there
812 * is no right sibling block and this block is bigger
813 * than the previous longest block, update it.
815 agf->agf_longest = recp->ar_blockcount;
816 cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
817 = be32_to_cpu(recp->ar_blockcount);
818 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
819 XFS_AGF_LONGEST);
822 * Return the new block number, if any.
823 * If there is one, give back a record value and a cursor too.
825 *bnop = nbno;
826 if (nbno != NULLAGBLOCK) {
827 *recp = nrec;
828 *curp = ncur;
830 *stat = 1;
831 return 0;
835 * Log header fields from a btree block.
837 STATIC void
838 xfs_alloc_log_block(
839 xfs_trans_t *tp, /* transaction pointer */
840 xfs_buf_t *bp, /* buffer containing btree block */
841 int fields) /* mask of fields: XFS_BB_... */
843 int first; /* first byte offset logged */
844 int last; /* last byte offset logged */
845 static const short offsets[] = { /* table of offsets */
846 offsetof(xfs_alloc_block_t, bb_magic),
847 offsetof(xfs_alloc_block_t, bb_level),
848 offsetof(xfs_alloc_block_t, bb_numrecs),
849 offsetof(xfs_alloc_block_t, bb_leftsib),
850 offsetof(xfs_alloc_block_t, bb_rightsib),
851 sizeof(xfs_alloc_block_t)
854 xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
855 xfs_trans_log_buf(tp, bp, first, last);
859 * Log keys from a btree block (nonleaf).
861 STATIC void
862 xfs_alloc_log_keys(
863 xfs_btree_cur_t *cur, /* btree cursor */
864 xfs_buf_t *bp, /* buffer containing btree block */
865 int kfirst, /* index of first key to log */
866 int klast) /* index of last key to log */
868 xfs_alloc_block_t *block; /* btree block to log from */
869 int first; /* first byte offset logged */
870 xfs_alloc_key_t *kp; /* key pointer in btree block */
871 int last; /* last byte offset logged */
873 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
874 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
875 first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
876 last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
877 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
881 * Log block pointer fields from a btree block (nonleaf).
883 STATIC void
884 xfs_alloc_log_ptrs(
885 xfs_btree_cur_t *cur, /* btree cursor */
886 xfs_buf_t *bp, /* buffer containing btree block */
887 int pfirst, /* index of first pointer to log */
888 int plast) /* index of last pointer to log */
890 xfs_alloc_block_t *block; /* btree block to log from */
891 int first; /* first byte offset logged */
892 int last; /* last byte offset logged */
893 xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
895 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
896 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
897 first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
898 last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
899 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
903 * Log records from a btree block (leaf).
905 STATIC void
906 xfs_alloc_log_recs(
907 xfs_btree_cur_t *cur, /* btree cursor */
908 xfs_buf_t *bp, /* buffer containing btree block */
909 int rfirst, /* index of first record to log */
910 int rlast) /* index of last record to log */
912 xfs_alloc_block_t *block; /* btree block to log from */
913 int first; /* first byte offset logged */
914 int last; /* last byte offset logged */
915 xfs_alloc_rec_t *rp; /* record pointer for btree block */
918 block = XFS_BUF_TO_ALLOC_BLOCK(bp);
919 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
920 #ifdef DEBUG
922 xfs_agf_t *agf;
923 xfs_alloc_rec_t *p;
925 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
926 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
927 ASSERT(be32_to_cpu(p->ar_startblock) +
928 be32_to_cpu(p->ar_blockcount) <=
929 be32_to_cpu(agf->agf_length));
931 #endif
932 first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
933 last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
934 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
938 * Allocate a new root block, fill it in.
940 STATIC int /* error */
941 xfs_alloc_newroot(
942 xfs_btree_cur_t *cur, /* btree cursor */
943 int *stat) /* success/failure */
945 int error; /* error return value */
946 xfs_agblock_t lbno; /* left block number */
947 xfs_buf_t *lbp; /* left btree buffer */
948 xfs_alloc_block_t *left; /* left btree block */
949 xfs_mount_t *mp; /* mount structure */
950 xfs_agblock_t nbno; /* new block number */
951 xfs_buf_t *nbp; /* new (root) buffer */
952 xfs_alloc_block_t *new; /* new (root) btree block */
953 int nptr; /* new value for key index, 1 or 2 */
954 xfs_agblock_t rbno; /* right block number */
955 xfs_buf_t *rbp; /* right btree buffer */
956 xfs_alloc_block_t *right; /* right btree block */
958 mp = cur->bc_mp;
960 ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
962 * Get a buffer from the freelist blocks, for the new root.
964 error = xfs_alloc_get_freelist(cur->bc_tp,
965 cur->bc_private.a.agbp, &nbno, 1);
966 if (error)
967 return error;
969 * None available, we fail.
971 if (nbno == NULLAGBLOCK) {
972 *stat = 0;
973 return 0;
975 xfs_trans_agbtree_delta(cur->bc_tp, 1);
976 nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
978 new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
980 * Set the root data in the a.g. freespace structure.
983 xfs_agf_t *agf; /* a.g. freespace header */
984 xfs_agnumber_t seqno;
986 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
987 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
988 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
989 seqno = be32_to_cpu(agf->agf_seqno);
990 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
991 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
992 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
995 * At the previous root level there are now two blocks: the old
996 * root, and the new block generated when it was split.
997 * We don't know which one the cursor is pointing at, so we
998 * set up variables "left" and "right" for each case.
1000 lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1001 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1002 #ifdef DEBUG
1003 if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1004 return error;
1005 #endif
1006 if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
1008 * Our block is left, pick up the right block.
1010 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1011 rbno = be32_to_cpu(left->bb_rightsib);
1012 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1013 cur->bc_private.a.agno, rbno, 0, &rbp,
1014 XFS_ALLOC_BTREE_REF)))
1015 return error;
1016 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1017 if ((error = xfs_btree_check_sblock(cur, right,
1018 cur->bc_nlevels - 1, rbp)))
1019 return error;
1020 nptr = 1;
1021 } else {
1023 * Our block is right, pick up the left block.
1025 rbp = lbp;
1026 right = left;
1027 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1028 lbno = be32_to_cpu(right->bb_leftsib);
1029 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1030 cur->bc_private.a.agno, lbno, 0, &lbp,
1031 XFS_ALLOC_BTREE_REF)))
1032 return error;
1033 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1034 if ((error = xfs_btree_check_sblock(cur, left,
1035 cur->bc_nlevels - 1, lbp)))
1036 return error;
1037 nptr = 2;
1040 * Fill in the new block's btree header and log it.
1042 new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1043 new->bb_level = cpu_to_be16(cur->bc_nlevels);
1044 new->bb_numrecs = cpu_to_be16(2);
1045 new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1046 new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1047 xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1048 ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1050 * Fill in the key data in the new root.
1053 xfs_alloc_key_t *kp; /* btree key pointer */
1055 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1056 if (be16_to_cpu(left->bb_level) > 0) {
1057 kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
1058 kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
1059 } else {
1060 xfs_alloc_rec_t *rp; /* btree record pointer */
1062 rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1063 kp[0].ar_startblock = rp->ar_startblock;
1064 kp[0].ar_blockcount = rp->ar_blockcount;
1065 rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1066 kp[1].ar_startblock = rp->ar_startblock;
1067 kp[1].ar_blockcount = rp->ar_blockcount;
1070 xfs_alloc_log_keys(cur, nbp, 1, 2);
1072 * Fill in the pointer data in the new root.
1075 xfs_alloc_ptr_t *pp; /* btree address pointer */
1077 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1078 pp[0] = cpu_to_be32(lbno);
1079 pp[1] = cpu_to_be32(rbno);
1081 xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1083 * Fix up the cursor.
1085 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1086 cur->bc_ptrs[cur->bc_nlevels] = nptr;
1087 cur->bc_nlevels++;
1088 *stat = 1;
1089 return 0;
1093 * Split cur/level block in half.
1094 * Return new block number and its first record (to be inserted into parent).
1096 STATIC int /* error */
1097 xfs_alloc_split(
1098 xfs_btree_cur_t *cur, /* btree cursor */
1099 int level, /* level to split */
1100 xfs_agblock_t *bnop, /* output: block number allocated */
1101 xfs_alloc_key_t *keyp, /* output: first key of new block */
1102 xfs_btree_cur_t **curp, /* output: new cursor */
1103 int *stat) /* success/failure */
1105 int error; /* error return value */
1106 int i; /* loop index/record number */
1107 xfs_agblock_t lbno; /* left (current) block number */
1108 xfs_buf_t *lbp; /* buffer for left block */
1109 xfs_alloc_block_t *left; /* left (current) btree block */
1110 xfs_agblock_t rbno; /* right (new) block number */
1111 xfs_buf_t *rbp; /* buffer for right block */
1112 xfs_alloc_block_t *right; /* right (new) btree block */
1115 * Allocate the new block from the freelist.
1116 * If we can't do it, we're toast. Give up.
1118 error = xfs_alloc_get_freelist(cur->bc_tp,
1119 cur->bc_private.a.agbp, &rbno, 1);
1120 if (error)
1121 return error;
1122 if (rbno == NULLAGBLOCK) {
1123 *stat = 0;
1124 return 0;
1126 xfs_trans_agbtree_delta(cur->bc_tp, 1);
1127 rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1128 rbno, 0);
1130 * Set up the new block as "right".
1132 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1134 * "Left" is the current (according to the cursor) block.
1136 lbp = cur->bc_bufs[level];
1137 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1138 #ifdef DEBUG
1139 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1140 return error;
1141 #endif
1143 * Fill in the btree header for the new block.
1145 right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1146 right->bb_level = left->bb_level;
1147 right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1149 * Make sure that if there's an odd number of entries now, that
1150 * each new block will have the same number of entries.
1152 if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1153 cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1154 be16_add_cpu(&right->bb_numrecs, 1);
1155 i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1157 * For non-leaf blocks, copy keys and addresses over to the new block.
1159 if (level > 0) {
1160 xfs_alloc_key_t *lkp; /* left btree key pointer */
1161 xfs_alloc_ptr_t *lpp; /* left btree address pointer */
1162 xfs_alloc_key_t *rkp; /* right btree key pointer */
1163 xfs_alloc_ptr_t *rpp; /* right btree address pointer */
1165 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1166 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1167 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1168 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1169 #ifdef DEBUG
1170 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1171 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
1172 return error;
1174 #endif
1175 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1176 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1177 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1178 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1179 *keyp = *rkp;
1182 * For leaf blocks, copy records over to the new block.
1184 else {
1185 xfs_alloc_rec_t *lrp; /* left btree record pointer */
1186 xfs_alloc_rec_t *rrp; /* right btree record pointer */
1188 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1189 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1190 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1191 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1192 keyp->ar_startblock = rrp->ar_startblock;
1193 keyp->ar_blockcount = rrp->ar_blockcount;
1196 * Find the left block number by looking in the buffer.
1197 * Adjust numrecs, sibling pointers.
1199 lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1200 be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1201 right->bb_rightsib = left->bb_rightsib;
1202 left->bb_rightsib = cpu_to_be32(rbno);
1203 right->bb_leftsib = cpu_to_be32(lbno);
1204 xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1205 xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1207 * If there's a block to the new block's right, make that block
1208 * point back to right instead of to left.
1210 if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
1211 xfs_alloc_block_t *rrblock; /* rr btree block */
1212 xfs_buf_t *rrbp; /* buffer for rrblock */
1214 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1215 cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
1216 &rrbp, XFS_ALLOC_BTREE_REF)))
1217 return error;
1218 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1219 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1220 return error;
1221 rrblock->bb_leftsib = cpu_to_be32(rbno);
1222 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1225 * If the cursor is really in the right block, move it there.
1226 * If it's just pointing past the last entry in left, then we'll
1227 * insert there, so don't change anything in that case.
1229 if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1230 xfs_btree_setbuf(cur, level, rbp);
1231 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1234 * If there are more levels, we'll need another cursor which refers to
1235 * the right block, no matter where this cursor was.
1237 if (level + 1 < cur->bc_nlevels) {
1238 if ((error = xfs_btree_dup_cursor(cur, curp)))
1239 return error;
1240 (*curp)->bc_ptrs[level + 1]++;
1242 *bnop = rbno;
1243 *stat = 1;
1244 return 0;
1248 * Externally visible routines.
1252 * Delete the record pointed to by cur.
1253 * The cursor refers to the place where the record was (could be inserted)
1254 * when the operation returns.
1256 int /* error */
1257 xfs_alloc_delete(
1258 xfs_btree_cur_t *cur, /* btree cursor */
1259 int *stat) /* success/failure */
1261 int error; /* error return value */
1262 int i; /* result code */
1263 int level; /* btree level */
1266 * Go up the tree, starting at leaf level.
1267 * If 2 is returned then a join was done; go to the next level.
1268 * Otherwise we are done.
1270 for (level = 0, i = 2; i == 2; level++) {
1271 if ((error = xfs_alloc_delrec(cur, level, &i)))
1272 return error;
1274 if (i == 0) {
1275 for (level = 1; level < cur->bc_nlevels; level++) {
1276 if (cur->bc_ptrs[level] == 0) {
1277 if ((error = xfs_btree_decrement(cur, level, &i)))
1278 return error;
1279 break;
1283 *stat = i;
1284 return 0;
1288 * Get the data from the pointed-to record.
1290 int /* error */
1291 xfs_alloc_get_rec(
1292 xfs_btree_cur_t *cur, /* btree cursor */
1293 xfs_agblock_t *bno, /* output: starting block of extent */
1294 xfs_extlen_t *len, /* output: length of extent */
1295 int *stat) /* output: success/failure */
1297 xfs_alloc_block_t *block; /* btree block */
1298 #ifdef DEBUG
1299 int error; /* error return value */
1300 #endif
1301 int ptr; /* record number */
1303 ptr = cur->bc_ptrs[0];
1304 block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1305 #ifdef DEBUG
1306 if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1307 return error;
1308 #endif
1310 * Off the right end or left end, return failure.
1312 if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
1313 *stat = 0;
1314 return 0;
1317 * Point to the record and extract its data.
1320 xfs_alloc_rec_t *rec; /* record data */
1322 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1323 *bno = be32_to_cpu(rec->ar_startblock);
1324 *len = be32_to_cpu(rec->ar_blockcount);
1326 *stat = 1;
1327 return 0;
1331 * Insert the current record at the point referenced by cur.
1332 * The cursor may be inconsistent on return if splits have been done.
1334 int /* error */
1335 xfs_alloc_insert(
1336 xfs_btree_cur_t *cur, /* btree cursor */
1337 int *stat) /* success/failure */
1339 int error; /* error return value */
1340 int i; /* result value, 0 for failure */
1341 int level; /* current level number in btree */
1342 xfs_agblock_t nbno; /* new block number (split result) */
1343 xfs_btree_cur_t *ncur; /* new cursor (split result) */
1344 xfs_alloc_rec_t nrec; /* record being inserted this level */
1345 xfs_btree_cur_t *pcur; /* previous level's cursor */
1347 level = 0;
1348 nbno = NULLAGBLOCK;
1349 nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
1350 nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
1351 ncur = NULL;
1352 pcur = cur;
1354 * Loop going up the tree, starting at the leaf level.
1355 * Stop when we don't get a split block, that must mean that
1356 * the insert is finished with this level.
1358 do {
1360 * Insert nrec/nbno into this level of the tree.
1361 * Note if we fail, nbno will be null.
1363 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
1364 &i))) {
1365 if (pcur != cur)
1366 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1367 return error;
1370 * See if the cursor we just used is trash.
1371 * Can't trash the caller's cursor, but otherwise we should
1372 * if ncur is a new cursor or we're about to be done.
1374 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1375 cur->bc_nlevels = pcur->bc_nlevels;
1376 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1379 * If we got a new cursor, switch to it.
1381 if (ncur) {
1382 pcur = ncur;
1383 ncur = NULL;
1385 } while (nbno != NULLAGBLOCK);
1386 *stat = i;
1387 return 0;
1390 STATIC struct xfs_btree_cur *
1391 xfs_allocbt_dup_cursor(
1392 struct xfs_btree_cur *cur)
1394 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
1395 cur->bc_private.a.agbp, cur->bc_private.a.agno,
1396 cur->bc_btnum);
1400 * Update the longest extent in the AGF
1402 STATIC void
1403 xfs_allocbt_update_lastrec(
1404 struct xfs_btree_cur *cur,
1405 struct xfs_btree_block *block,
1406 union xfs_btree_rec *rec,
1407 int ptr,
1408 int reason)
1410 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1411 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
1412 __be32 len;
1414 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1416 switch (reason) {
1417 case LASTREC_UPDATE:
1419 * If this is the last leaf block and it's the last record,
1420 * then update the size of the longest extent in the AG.
1422 if (ptr != xfs_btree_get_numrecs(block))
1423 return;
1424 len = rec->alloc.ar_blockcount;
1425 break;
1426 default:
1427 ASSERT(0);
1428 return;
1431 agf->agf_longest = len;
1432 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
1433 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
1436 STATIC int
1437 xfs_allocbt_get_maxrecs(
1438 struct xfs_btree_cur *cur,
1439 int level)
1441 return cur->bc_mp->m_alloc_mxr[level != 0];
1444 STATIC void
1445 xfs_allocbt_init_key_from_rec(
1446 union xfs_btree_key *key,
1447 union xfs_btree_rec *rec)
1449 ASSERT(rec->alloc.ar_startblock != 0);
1451 key->alloc.ar_startblock = rec->alloc.ar_startblock;
1452 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
1455 STATIC void
1456 xfs_allocbt_init_ptr_from_cur(
1457 struct xfs_btree_cur *cur,
1458 union xfs_btree_ptr *ptr)
1460 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1462 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
1463 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
1465 ptr->s = agf->agf_roots[cur->bc_btnum];
1468 STATIC __int64_t
1469 xfs_allocbt_key_diff(
1470 struct xfs_btree_cur *cur,
1471 union xfs_btree_key *key)
1473 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
1474 xfs_alloc_key_t *kp = &key->alloc;
1475 __int64_t diff;
1477 if (cur->bc_btnum == XFS_BTNUM_BNO) {
1478 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
1479 rec->ar_startblock;
1482 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
1483 if (diff)
1484 return diff;
1486 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
1489 #ifdef XFS_BTREE_TRACE
1490 ktrace_t *xfs_allocbt_trace_buf;
1492 STATIC void
1493 xfs_allocbt_trace_enter(
1494 struct xfs_btree_cur *cur,
1495 const char *func,
1496 char *s,
1497 int type,
1498 int line,
1499 __psunsigned_t a0,
1500 __psunsigned_t a1,
1501 __psunsigned_t a2,
1502 __psunsigned_t a3,
1503 __psunsigned_t a4,
1504 __psunsigned_t a5,
1505 __psunsigned_t a6,
1506 __psunsigned_t a7,
1507 __psunsigned_t a8,
1508 __psunsigned_t a9,
1509 __psunsigned_t a10)
1511 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
1512 (void *)func, (void *)s, NULL, (void *)cur,
1513 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1514 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1515 (void *)a8, (void *)a9, (void *)a10);
1518 STATIC void
1519 xfs_allocbt_trace_cursor(
1520 struct xfs_btree_cur *cur,
1521 __uint32_t *s0,
1522 __uint64_t *l0,
1523 __uint64_t *l1)
1525 *s0 = cur->bc_private.a.agno;
1526 *l0 = cur->bc_rec.a.ar_startblock;
1527 *l1 = cur->bc_rec.a.ar_blockcount;
1530 STATIC void
1531 xfs_allocbt_trace_key(
1532 struct xfs_btree_cur *cur,
1533 union xfs_btree_key *key,
1534 __uint64_t *l0,
1535 __uint64_t *l1)
1537 *l0 = be32_to_cpu(key->alloc.ar_startblock);
1538 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
1541 STATIC void
1542 xfs_allocbt_trace_record(
1543 struct xfs_btree_cur *cur,
1544 union xfs_btree_rec *rec,
1545 __uint64_t *l0,
1546 __uint64_t *l1,
1547 __uint64_t *l2)
1549 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
1550 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
1551 *l2 = 0;
1553 #endif /* XFS_BTREE_TRACE */
1555 static const struct xfs_btree_ops xfs_allocbt_ops = {
1556 .rec_len = sizeof(xfs_alloc_rec_t),
1557 .key_len = sizeof(xfs_alloc_key_t),
1559 .dup_cursor = xfs_allocbt_dup_cursor,
1560 .update_lastrec = xfs_allocbt_update_lastrec,
1561 .get_maxrecs = xfs_allocbt_get_maxrecs,
1562 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
1563 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
1564 .key_diff = xfs_allocbt_key_diff,
1566 #ifdef XFS_BTREE_TRACE
1567 .trace_enter = xfs_allocbt_trace_enter,
1568 .trace_cursor = xfs_allocbt_trace_cursor,
1569 .trace_key = xfs_allocbt_trace_key,
1570 .trace_record = xfs_allocbt_trace_record,
1571 #endif
1575 * Allocate a new allocation btree cursor.
1577 struct xfs_btree_cur * /* new alloc btree cursor */
1578 xfs_allocbt_init_cursor(
1579 struct xfs_mount *mp, /* file system mount point */
1580 struct xfs_trans *tp, /* transaction pointer */
1581 struct xfs_buf *agbp, /* buffer for agf structure */
1582 xfs_agnumber_t agno, /* allocation group number */
1583 xfs_btnum_t btnum) /* btree identifier */
1585 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
1586 struct xfs_btree_cur *cur;
1588 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
1590 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1592 cur->bc_tp = tp;
1593 cur->bc_mp = mp;
1594 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1595 cur->bc_btnum = btnum;
1596 cur->bc_blocklog = mp->m_sb.sb_blocklog;
1598 cur->bc_ops = &xfs_allocbt_ops;
1599 if (btnum == XFS_BTNUM_CNT)
1600 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
1602 cur->bc_private.a.agbp = agbp;
1603 cur->bc_private.a.agno = agno;
1605 return cur;