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"
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 *);
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 */
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 */
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
];
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
);
110 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
114 * Fail if we're off the end of the block.
116 numrecs
= be16_to_cpu(block
->bb_numrecs
);
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.
128 lkp
= XFS_ALLOC_KEY_ADDR(block
, 1, cur
);
129 lpp
= XFS_ALLOC_PTR_ADDR(block
, 1, cur
);
131 for (i
= ptr
; i
< numrecs
; i
++) {
132 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(lpp
[i
]), level
)))
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.
150 lrp
= XFS_ALLOC_REC_ADDR(block
, 1, cur
);
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).
161 key
.ar_startblock
= lrp
->ar_startblock
;
162 key
.ar_blockcount
= lrp
->ar_blockcount
;
167 * Decrement and log the number of entries in the block.
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
);
182 cur
->bc_btnum
== XFS_BTNUM_CNT
&&
183 be32_to_cpu(block
->bb_rightsib
) == NULLAGBLOCK
&&
185 ASSERT(ptr
== numrecs
+ 1);
187 * There are still records in the block. Grab the size
191 rrp
= XFS_ALLOC_REC_ADDR(block
, numrecs
, cur
);
192 agf
->agf_longest
= rrp
->ar_blockcount
;
195 * No free extents left.
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
,
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);
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
);
255 } else if (level
> 0 &&
256 (error
= xfs_btree_decrement(cur
, level
, &i
)))
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)))
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
)))
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
);
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
)))
293 * If there's a right sibling, see if it's ok to shift an entry
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
)))
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
);
314 if ((error
= xfs_btree_check_sblock(cur
, right
, level
, rbp
)))
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
)))
331 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
332 XFS_ALLOC_BLOCK_MINRECS(level
, cur
));
333 xfs_btree_del_cursor(tcur
,
336 (error
= xfs_btree_decrement(cur
, level
,
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
)))
354 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
358 * If there's a left sibling, see if it's ok to shift an entry
361 if (lbno
!= NULLAGBLOCK
) {
363 * Move the temp cursor to the first entry in the
366 i
= xfs_btree_firstrec(tcur
, level
);
367 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
368 if ((error
= xfs_btree_decrement(tcur
, level
, &i
)))
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
);
378 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
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
)))
395 ASSERT(be16_to_cpu(block
->bb_numrecs
) >=
396 XFS_ALLOC_BLOCK_MINRECS(level
, cur
));
397 xfs_btree_del_cursor(tcur
,
406 * Otherwise, grab the number of records in right for
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.
430 rrecs
= be16_to_cpu(right
->bb_numrecs
);
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
)))
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
)))
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.
452 lrecs
= be16_to_cpu(left
->bb_numrecs
);
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
)))
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
)))
464 * Otherwise, we can't fix the imbalance.
465 * Just return. This is probably a logic error, but it's not fatal.
468 if (level
> 0 && (error
= xfs_btree_decrement(cur
, level
, &i
)))
474 * We're now going to join "left" and "right" by moving all the stuff
475 * in "right" to "left" and deleting "right".
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
);
486 for (i
= 0; i
< rrecs
; i
++) {
487 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(rpp
[i
]), level
)))
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
);
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.
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
)))
520 * Fix up the number of records in the surviving block.
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
533 if (be32_to_cpu(left
->bb_rightsib
) != NULLAGBLOCK
) {
534 xfs_alloc_block_t
*rrblock
;
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
)))
541 rrblock
= XFS_BUF_TO_ALLOC_BLOCK(rrbp
);
542 if ((error
= xfs_btree_check_sblock(cur
, rrblock
, level
, rrbp
)))
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);
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.
575 cur
->bc_ptrs
[level
]--;
577 * Return value means the next level up has something to do.
583 xfs_btree_del_cursor(tcur
, XFS_BTREE_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 */
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 */
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
624 memset(&nrec
, 0, sizeof(nrec
));
627 * If we made it to the root level, allocate a new root block
630 if (level
>= cur
->bc_nlevels
) {
631 XFS_STATS_INC(xs_abt_insrec
);
632 if ((error
= xfs_alloc_newroot(cur
, &i
)))
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.
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
);
659 if ((error
= xfs_btree_check_sblock(cur
, block
, level
, bp
)))
662 * Check that the new entry is being inserted in the right place.
664 if (ptr
<= numrecs
) {
666 rp
= XFS_ALLOC_REC_ADDR(block
, ptr
, cur
);
667 xfs_btree_check_rec(cur
->bc_btnum
, recp
, rp
);
669 kp
= XFS_ALLOC_KEY_ADDR(block
, ptr
, cur
);
670 xfs_btree_check_key(cur
->bc_btnum
, &key
, kp
);
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
)))
690 * Next, try shifting an entry to the left neighbor.
693 if ((error
= xfs_btree_lshift(cur
, level
, &i
)))
696 optr
= ptr
= cur
->bc_ptrs
[level
];
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
,
708 bp
= cur
->bc_bufs
[level
];
709 block
= XFS_BUF_TO_ALLOC_BLOCK(bp
);
712 xfs_btree_check_sblock(cur
,
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.
731 * At this point we know there's room for our new entry in the block
734 numrecs
= be16_to_cpu(block
->bb_numrecs
);
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
);
743 for (i
= numrecs
; i
>= ptr
; i
--) {
744 if ((error
= xfs_btree_check_sptr(cur
, be32_to_cpu(pp
[i
- 1]), level
)))
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
));
753 if ((error
= xfs_btree_check_sptr(cur
, *bnop
, level
)))
757 * Now stuff the new data in, bump numrecs and log the new data.
760 pp
[ptr
- 1] = cpu_to_be32(*bnop
);
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
);
767 xfs_btree_check_key(cur
->bc_btnum
, kp
+ ptr
- 1,
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.
783 block
->bb_numrecs
= cpu_to_be16(numrecs
);
784 xfs_alloc_log_recs(cur
, bp
, ptr
, numrecs
);
787 xfs_btree_check_rec(cur
->bc_btnum
, rp
+ ptr
- 1,
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)))
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
);
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
,
822 * Return the new block number, if any.
823 * If there is one, give back a record value and a cursor too.
826 if (nbno
!= NULLAGBLOCK
) {
835 * Log header fields from a btree 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).
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).
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).
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
);
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
));
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 */
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 */
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);
969 * None available, we fail.
971 if (nbno
== NULLAGBLOCK
) {
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
);
1003 if ((error
= xfs_btree_check_sblock(cur
, left
, cur
->bc_nlevels
- 1, lbp
)))
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
)))
1016 right
= XFS_BUF_TO_ALLOC_BLOCK(rbp
);
1017 if ((error
= xfs_btree_check_sblock(cur
, right
,
1018 cur
->bc_nlevels
- 1, rbp
)))
1023 * Our block is right, pick up the left block.
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
)))
1033 left
= XFS_BUF_TO_ALLOC_BLOCK(lbp
);
1034 if ((error
= xfs_btree_check_sblock(cur
, left
,
1035 cur
->bc_nlevels
- 1, lbp
)))
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
);
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
;
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 */
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);
1122 if (rbno
== NULLAGBLOCK
) {
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
,
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
);
1139 if ((error
= xfs_btree_check_sblock(cur
, left
, level
, lbp
)))
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.
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
);
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
)))
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
));
1182 * For leaf blocks, copy records over to the new block.
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
)))
1218 rrblock
= XFS_BUF_TO_ALLOC_BLOCK(rrbp
);
1219 if ((error
= xfs_btree_check_sblock(cur
, rrblock
, level
, rrbp
)))
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
)))
1240 (*curp
)->bc_ptrs
[level
+ 1]++;
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.
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
)))
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
)))
1288 * Get the data from the pointed-to record.
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 */
1299 int error
; /* error return value */
1301 int ptr
; /* record number */
1303 ptr
= cur
->bc_ptrs
[0];
1304 block
= XFS_BUF_TO_ALLOC_BLOCK(cur
->bc_bufs
[0]);
1306 if ((error
= xfs_btree_check_sblock(cur
, block
, 0, cur
->bc_bufs
[0])))
1310 * Off the right end or left end, return failure.
1312 if (ptr
> be16_to_cpu(block
->bb_numrecs
) || ptr
<= 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
);
1331 * Insert the current record at the point referenced by cur.
1332 * The cursor may be inconsistent on return if splits have been done.
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 */
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
);
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.
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
,
1366 xfs_btree_del_cursor(pcur
, XFS_BTREE_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.
1385 } while (nbno
!= NULLAGBLOCK
);
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
,
1400 * Update the longest extent in the AGF
1403 xfs_allocbt_update_lastrec(
1404 struct xfs_btree_cur
*cur
,
1405 struct xfs_btree_block
*block
,
1406 union xfs_btree_rec
*rec
,
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
);
1414 ASSERT(cur
->bc_btnum
== XFS_BTNUM_CNT
);
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
))
1424 len
= rec
->alloc
.ar_blockcount
;
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
);
1437 xfs_allocbt_get_maxrecs(
1438 struct xfs_btree_cur
*cur
,
1441 return cur
->bc_mp
->m_alloc_mxr
[level
!= 0];
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
;
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
];
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
;
1477 if (cur
->bc_btnum
== XFS_BTNUM_BNO
) {
1478 return (__int64_t
)be32_to_cpu(kp
->ar_startblock
) -
1482 diff
= (__int64_t
)be32_to_cpu(kp
->ar_blockcount
) - rec
->ar_blockcount
;
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
;
1493 xfs_allocbt_trace_enter(
1494 struct xfs_btree_cur
*cur
,
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
);
1519 xfs_allocbt_trace_cursor(
1520 struct xfs_btree_cur
*cur
,
1525 *s0
= cur
->bc_private
.a
.agno
;
1526 *l0
= cur
->bc_rec
.a
.ar_startblock
;
1527 *l1
= cur
->bc_rec
.a
.ar_blockcount
;
1531 xfs_allocbt_trace_key(
1532 struct xfs_btree_cur
*cur
,
1533 union xfs_btree_key
*key
,
1537 *l0
= be32_to_cpu(key
->alloc
.ar_startblock
);
1538 *l1
= be32_to_cpu(key
->alloc
.ar_blockcount
);
1542 xfs_allocbt_trace_record(
1543 struct xfs_btree_cur
*cur
,
1544 union xfs_btree_rec
*rec
,
1549 *l0
= be32_to_cpu(rec
->alloc
.ar_startblock
);
1550 *l1
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
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
,
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
);
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
;