[XFS] implement generic xfs_btree_rshift
[linux-2.6] / fs / xfs / xfs_alloc_btree.c
1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
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"
41
42 /*
43  * Prototypes for internal functions.
44  */
45
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_lshift(xfs_btree_cur_t *, int, int *);
51 STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
52 STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
53                 xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
54
55 /*
56  * Internal functions.
57  */
58
59 /*
60  * Single level of the xfs_alloc_delete record deletion routine.
61  * Delete record pointed to by cur/level.
62  * Remove the record from its block then rebalance the tree.
63  * Return 0 for error, 1 for done, 2 to go on to the next level.
64  */
65 STATIC int                              /* error */
66 xfs_alloc_delrec(
67         xfs_btree_cur_t         *cur,   /* btree cursor */
68         int                     level,  /* level removing record from */
69         int                     *stat)  /* fail/done/go-on */
70 {
71         xfs_agf_t               *agf;   /* allocation group freelist header */
72         xfs_alloc_block_t       *block; /* btree block record/key lives in */
73         xfs_agblock_t           bno;    /* btree block number */
74         xfs_buf_t               *bp;    /* buffer for block */
75         int                     error;  /* error return value */
76         int                     i;      /* loop index */
77         xfs_alloc_key_t         key;    /* kp points here if block is level 0 */
78         xfs_agblock_t           lbno;   /* left block's block number */
79         xfs_buf_t               *lbp;   /* left block's buffer pointer */
80         xfs_alloc_block_t       *left;  /* left btree block */
81         xfs_alloc_key_t         *lkp=NULL;      /* left block key pointer */
82         xfs_alloc_ptr_t         *lpp=NULL;      /* left block address pointer */
83         int                     lrecs=0;        /* number of records in left block */
84         xfs_alloc_rec_t         *lrp;   /* left block record pointer */
85         xfs_mount_t             *mp;    /* mount structure */
86         int                     ptr;    /* index in btree block for this rec */
87         xfs_agblock_t           rbno;   /* right block's block number */
88         xfs_buf_t               *rbp;   /* right block's buffer pointer */
89         xfs_alloc_block_t       *right; /* right btree block */
90         xfs_alloc_key_t         *rkp;   /* right block key pointer */
91         xfs_alloc_ptr_t         *rpp;   /* right block address pointer */
92         int                     rrecs=0;        /* number of records in right block */
93         int                     numrecs;
94         xfs_alloc_rec_t         *rrp;   /* right block record pointer */
95         xfs_btree_cur_t         *tcur;  /* temporary btree cursor */
96
97         /*
98          * Get the index of the entry being deleted, check for nothing there.
99          */
100         ptr = cur->bc_ptrs[level];
101         if (ptr == 0) {
102                 *stat = 0;
103                 return 0;
104         }
105         /*
106          * Get the buffer & block containing the record or key/ptr.
107          */
108         bp = cur->bc_bufs[level];
109         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
110 #ifdef DEBUG
111         if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
112                 return error;
113 #endif
114         /*
115          * Fail if we're off the end of the block.
116          */
117         numrecs = be16_to_cpu(block->bb_numrecs);
118         if (ptr > numrecs) {
119                 *stat = 0;
120                 return 0;
121         }
122         XFS_STATS_INC(xs_abt_delrec);
123         /*
124          * It's a nonleaf.  Excise the key and ptr being deleted, by
125          * sliding the entries past them down one.
126          * Log the changed areas of the block.
127          */
128         if (level > 0) {
129                 lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
130                 lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
131 #ifdef DEBUG
132                 for (i = ptr; i < numrecs; i++) {
133                         if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
134                                 return error;
135                 }
136 #endif
137                 if (ptr < numrecs) {
138                         memmove(&lkp[ptr - 1], &lkp[ptr],
139                                 (numrecs - ptr) * sizeof(*lkp));
140                         memmove(&lpp[ptr - 1], &lpp[ptr],
141                                 (numrecs - ptr) * sizeof(*lpp));
142                         xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
143                         xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
144                 }
145         }
146         /*
147          * It's a leaf.  Excise the record being deleted, by sliding the
148          * entries past it down one.  Log the changed areas of the block.
149          */
150         else {
151                 lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
152                 if (ptr < numrecs) {
153                         memmove(&lrp[ptr - 1], &lrp[ptr],
154                                 (numrecs - ptr) * sizeof(*lrp));
155                         xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
156                 }
157                 /*
158                  * If it's the first record in the block, we'll need a key
159                  * structure to pass up to the next level (updkey).
160                  */
161                 if (ptr == 1) {
162                         key.ar_startblock = lrp->ar_startblock;
163                         key.ar_blockcount = lrp->ar_blockcount;
164                         lkp = &key;
165                 }
166         }
167         /*
168          * Decrement and log the number of entries in the block.
169          */
170         numrecs--;
171         block->bb_numrecs = cpu_to_be16(numrecs);
172         xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
173         /*
174          * See if the longest free extent in the allocation group was
175          * changed by this operation.  True if it's the by-size btree, and
176          * this is the leaf level, and there is no right sibling block,
177          * and this was the last record.
178          */
179         agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
180         mp = cur->bc_mp;
181
182         if (level == 0 &&
183             cur->bc_btnum == XFS_BTNUM_CNT &&
184             be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
185             ptr > numrecs) {
186                 ASSERT(ptr == numrecs + 1);
187                 /*
188                  * There are still records in the block.  Grab the size
189                  * from the last one.
190                  */
191                 if (numrecs) {
192                         rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
193                         agf->agf_longest = rrp->ar_blockcount;
194                 }
195                 /*
196                  * No free extents left.
197                  */
198                 else
199                         agf->agf_longest = 0;
200                 mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
201                         be32_to_cpu(agf->agf_longest);
202                 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
203                         XFS_AGF_LONGEST);
204         }
205         /*
206          * Is this the root level?  If so, we're almost done.
207          */
208         if (level == cur->bc_nlevels - 1) {
209                 /*
210                  * If this is the root level,
211                  * and there's only one entry left,
212                  * and it's NOT the leaf level,
213                  * then we can get rid of this level.
214                  */
215                 if (numrecs == 1 && level > 0) {
216                         /*
217                          * lpp is still set to the first pointer in the block.
218                          * Make it the new root of the btree.
219                          */
220                         bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
221                         agf->agf_roots[cur->bc_btnum] = *lpp;
222                         be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
223                         mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
224                         /*
225                          * Put this buffer/block on the ag's freelist.
226                          */
227                         error = xfs_alloc_put_freelist(cur->bc_tp,
228                                         cur->bc_private.a.agbp, NULL, bno, 1);
229                         if (error)
230                                 return error;
231                         /*
232                          * Since blocks move to the free list without the
233                          * coordination used in xfs_bmap_finish, we can't allow
234                          * block to be available for reallocation and
235                          * non-transaction writing (user data) until we know
236                          * that the transaction that moved it to the free list
237                          * is permanently on disk. We track the blocks by
238                          * declaring these blocks as "busy"; the busy list is
239                          * maintained on a per-ag basis and each transaction
240                          * records which entries should be removed when the
241                          * iclog commits to disk. If a busy block is
242                          * allocated, the iclog is pushed up to the LSN
243                          * that freed the block.
244                          */
245                         xfs_alloc_mark_busy(cur->bc_tp,
246                                 be32_to_cpu(agf->agf_seqno), bno, 1);
247
248                         xfs_trans_agbtree_delta(cur->bc_tp, -1);
249                         xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
250                                 XFS_AGF_ROOTS | XFS_AGF_LEVELS);
251                         /*
252                          * Update the cursor so there's one fewer level.
253                          */
254                         xfs_btree_setbuf(cur, level, NULL);
255                         cur->bc_nlevels--;
256                 } else if (level > 0 &&
257                            (error = xfs_btree_decrement(cur, level, &i)))
258                         return error;
259                 *stat = 1;
260                 return 0;
261         }
262         /*
263          * If we deleted the leftmost entry in the block, update the
264          * key values above us in the tree.
265          */
266         if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
267                 return error;
268         /*
269          * If the number of records remaining in the block is at least
270          * the minimum, we're done.
271          */
272         if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
273                 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
274                         return error;
275                 *stat = 1;
276                 return 0;
277         }
278         /*
279          * Otherwise, we have to move some records around to keep the
280          * tree balanced.  Look at the left and right sibling blocks to
281          * see if we can re-balance by moving only one record.
282          */
283         rbno = be32_to_cpu(block->bb_rightsib);
284         lbno = be32_to_cpu(block->bb_leftsib);
285         bno = NULLAGBLOCK;
286         ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
287         /*
288          * Duplicate the cursor so our btree manipulations here won't
289          * disrupt the next level up.
290          */
291         if ((error = xfs_btree_dup_cursor(cur, &tcur)))
292                 return error;
293         /*
294          * If there's a right sibling, see if it's ok to shift an entry
295          * out of it.
296          */
297         if (rbno != NULLAGBLOCK) {
298                 /*
299                  * Move the temp cursor to the last entry in the next block.
300                  * Actually any entry but the first would suffice.
301                  */
302                 i = xfs_btree_lastrec(tcur, level);
303                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
304                 if ((error = xfs_btree_increment(tcur, level, &i)))
305                         goto error0;
306                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
307                 i = xfs_btree_lastrec(tcur, level);
308                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
309                 /*
310                  * Grab a pointer to the block.
311                  */
312                 rbp = tcur->bc_bufs[level];
313                 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
314 #ifdef DEBUG
315                 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
316                         goto error0;
317 #endif
318                 /*
319                  * Grab the current block number, for future use.
320                  */
321                 bno = be32_to_cpu(right->bb_leftsib);
322                 /*
323                  * If right block is full enough so that removing one entry
324                  * won't make it too empty, and left-shifting an entry out
325                  * of right to us works, we're done.
326                  */
327                 if (be16_to_cpu(right->bb_numrecs) - 1 >=
328                      XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
329                         if ((error = xfs_alloc_lshift(tcur, level, &i)))
330                                 goto error0;
331                         if (i) {
332                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
333                                        XFS_ALLOC_BLOCK_MINRECS(level, cur));
334                                 xfs_btree_del_cursor(tcur,
335                                                      XFS_BTREE_NOERROR);
336                                 if (level > 0 &&
337                                     (error = xfs_btree_decrement(cur, level,
338                                             &i)))
339                                         return error;
340                                 *stat = 1;
341                                 return 0;
342                         }
343                 }
344                 /*
345                  * Otherwise, grab the number of records in right for
346                  * future reference, and fix up the temp cursor to point
347                  * to our block again (last record).
348                  */
349                 rrecs = be16_to_cpu(right->bb_numrecs);
350                 if (lbno != NULLAGBLOCK) {
351                         i = xfs_btree_firstrec(tcur, level);
352                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
353                         if ((error = xfs_btree_decrement(tcur, level, &i)))
354                                 goto error0;
355                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
356                 }
357         }
358         /*
359          * If there's a left sibling, see if it's ok to shift an entry
360          * out of it.
361          */
362         if (lbno != NULLAGBLOCK) {
363                 /*
364                  * Move the temp cursor to the first entry in the
365                  * previous block.
366                  */
367                 i = xfs_btree_firstrec(tcur, level);
368                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
369                 if ((error = xfs_btree_decrement(tcur, level, &i)))
370                         goto error0;
371                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
372                 xfs_btree_firstrec(tcur, level);
373                 /*
374                  * Grab a pointer to the block.
375                  */
376                 lbp = tcur->bc_bufs[level];
377                 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
378 #ifdef DEBUG
379                 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
380                         goto error0;
381 #endif
382                 /*
383                  * Grab the current block number, for future use.
384                  */
385                 bno = be32_to_cpu(left->bb_rightsib);
386                 /*
387                  * If left block is full enough so that removing one entry
388                  * won't make it too empty, and right-shifting an entry out
389                  * of left to us works, we're done.
390                  */
391                 if (be16_to_cpu(left->bb_numrecs) - 1 >=
392                      XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
393                         if ((error = xfs_btree_rshift(tcur, level, &i)))
394                                 goto error0;
395                         if (i) {
396                                 ASSERT(be16_to_cpu(block->bb_numrecs) >=
397                                        XFS_ALLOC_BLOCK_MINRECS(level, cur));
398                                 xfs_btree_del_cursor(tcur,
399                                                      XFS_BTREE_NOERROR);
400                                 if (level == 0)
401                                         cur->bc_ptrs[0]++;
402                                 *stat = 1;
403                                 return 0;
404                         }
405                 }
406                 /*
407                  * Otherwise, grab the number of records in right for
408                  * future reference.
409                  */
410                 lrecs = be16_to_cpu(left->bb_numrecs);
411         }
412         /*
413          * Delete the temp cursor, we're done with it.
414          */
415         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
416         /*
417          * If here, we need to do a join to keep the tree balanced.
418          */
419         ASSERT(bno != NULLAGBLOCK);
420         /*
421          * See if we can join with the left neighbor block.
422          */
423         if (lbno != NULLAGBLOCK &&
424             lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
425                 /*
426                  * Set "right" to be the starting block,
427                  * "left" to be the left neighbor.
428                  */
429                 rbno = bno;
430                 right = block;
431                 rrecs = be16_to_cpu(right->bb_numrecs);
432                 rbp = bp;
433                 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
434                                 cur->bc_private.a.agno, lbno, 0, &lbp,
435                                 XFS_ALLOC_BTREE_REF)))
436                         return error;
437                 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
438                 lrecs = be16_to_cpu(left->bb_numrecs);
439                 if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
440                         return error;
441         }
442         /*
443          * If that won't work, see if we can join with the right neighbor block.
444          */
445         else if (rbno != NULLAGBLOCK &&
446                  rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
447                 /*
448                  * Set "left" to be the starting block,
449                  * "right" to be the right neighbor.
450                  */
451                 lbno = bno;
452                 left = block;
453                 lrecs = be16_to_cpu(left->bb_numrecs);
454                 lbp = bp;
455                 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
456                                 cur->bc_private.a.agno, rbno, 0, &rbp,
457                                 XFS_ALLOC_BTREE_REF)))
458                         return error;
459                 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
460                 rrecs = be16_to_cpu(right->bb_numrecs);
461                 if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
462                         return error;
463         }
464         /*
465          * Otherwise, we can't fix the imbalance.
466          * Just return.  This is probably a logic error, but it's not fatal.
467          */
468         else {
469                 if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
470                         return error;
471                 *stat = 1;
472                 return 0;
473         }
474         /*
475          * We're now going to join "left" and "right" by moving all the stuff
476          * in "right" to "left" and deleting "right".
477          */
478         if (level > 0) {
479                 /*
480                  * It's a non-leaf.  Move keys and pointers.
481                  */
482                 lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
483                 lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
484                 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
485                 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
486 #ifdef DEBUG
487                 for (i = 0; i < rrecs; i++) {
488                         if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
489                                 return error;
490                 }
491 #endif
492                 memcpy(lkp, rkp, rrecs * sizeof(*lkp));
493                 memcpy(lpp, rpp, rrecs * sizeof(*lpp));
494                 xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
495                 xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
496         } else {
497                 /*
498                  * It's a leaf.  Move records.
499                  */
500                 lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
501                 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
502                 memcpy(lrp, rrp, rrecs * sizeof(*lrp));
503                 xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
504         }
505         /*
506          * If we joined with the left neighbor, set the buffer in the
507          * cursor to the left block, and fix up the index.
508          */
509         if (bp != lbp) {
510                 xfs_btree_setbuf(cur, level, lbp);
511                 cur->bc_ptrs[level] += lrecs;
512         }
513         /*
514          * If we joined with the right neighbor and there's a level above
515          * us, increment the cursor at that level.
516          */
517         else if (level + 1 < cur->bc_nlevels &&
518                  (error = xfs_btree_increment(cur, level + 1, &i)))
519                 return error;
520         /*
521          * Fix up the number of records in the surviving block.
522          */
523         lrecs += rrecs;
524         left->bb_numrecs = cpu_to_be16(lrecs);
525         /*
526          * Fix up the right block pointer in the surviving block, and log it.
527          */
528         left->bb_rightsib = right->bb_rightsib;
529         xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
530         /*
531          * If there is a right sibling now, make it point to the
532          * remaining block.
533          */
534         if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
535                 xfs_alloc_block_t       *rrblock;
536                 xfs_buf_t               *rrbp;
537
538                 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
539                                 cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
540                                 &rrbp, XFS_ALLOC_BTREE_REF)))
541                         return error;
542                 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
543                 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
544                         return error;
545                 rrblock->bb_leftsib = cpu_to_be32(lbno);
546                 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
547         }
548         /*
549          * Free the deleting block by putting it on the freelist.
550          */
551         error = xfs_alloc_put_freelist(cur->bc_tp,
552                                          cur->bc_private.a.agbp, NULL, rbno, 1);
553         if (error)
554                 return error;
555         /*
556          * Since blocks move to the free list without the coordination
557          * used in xfs_bmap_finish, we can't allow block to be available
558          * for reallocation and non-transaction writing (user data)
559          * until we know that the transaction that moved it to the free
560          * list is permanently on disk. We track the blocks by declaring
561          * these blocks as "busy"; the busy list is maintained on a
562          * per-ag basis and each transaction records which entries
563          * should be removed when the iclog commits to disk. If a
564          * busy block is allocated, the iclog is pushed up to the
565          * LSN that freed the block.
566          */
567         xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
568         xfs_trans_agbtree_delta(cur->bc_tp, -1);
569
570         /*
571          * Adjust the current level's cursor so that we're left referring
572          * to the right node, after we're done.
573          * If this leaves the ptr value 0 our caller will fix it up.
574          */
575         if (level > 0)
576                 cur->bc_ptrs[level]--;
577         /*
578          * Return value means the next level up has something to do.
579          */
580         *stat = 2;
581         return 0;
582
583 error0:
584         xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
585         return error;
586 }
587
588 /*
589  * Insert one record/level.  Return information to the caller
590  * allowing the next level up to proceed if necessary.
591  */
592 STATIC int                              /* error */
593 xfs_alloc_insrec(
594         xfs_btree_cur_t         *cur,   /* btree cursor */
595         int                     level,  /* level to insert record at */
596         xfs_agblock_t           *bnop,  /* i/o: block number inserted */
597         xfs_alloc_rec_t         *recp,  /* i/o: record data inserted */
598         xfs_btree_cur_t         **curp, /* output: new cursor replacing cur */
599         int                     *stat)  /* output: success/failure */
600 {
601         xfs_agf_t               *agf;   /* allocation group freelist header */
602         xfs_alloc_block_t       *block; /* btree block record/key lives in */
603         xfs_buf_t               *bp;    /* buffer for block */
604         int                     error;  /* error return value */
605         int                     i;      /* loop index */
606         xfs_alloc_key_t         key;    /* key value being inserted */
607         xfs_alloc_key_t         *kp;    /* pointer to btree keys */
608         xfs_agblock_t           nbno;   /* block number of allocated block */
609         xfs_btree_cur_t         *ncur;  /* new cursor to be used at next lvl */
610         xfs_alloc_key_t         nkey;   /* new key value, from split */
611         xfs_alloc_rec_t         nrec;   /* new record value, for caller */
612         int                     numrecs;
613         int                     optr;   /* old ptr value */
614         xfs_alloc_ptr_t         *pp;    /* pointer to btree addresses */
615         int                     ptr;    /* index in btree block for this rec */
616         xfs_alloc_rec_t         *rp;    /* pointer to btree records */
617
618         ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
619
620         /*
621          * GCC doesn't understand the (arguably complex) control flow in
622          * this function and complains about uninitialized structure fields
623          * without this.
624          */
625         memset(&nrec, 0, sizeof(nrec));
626
627         /*
628          * If we made it to the root level, allocate a new root block
629          * and we're done.
630          */
631         if (level >= cur->bc_nlevels) {
632                 XFS_STATS_INC(xs_abt_insrec);
633                 if ((error = xfs_alloc_newroot(cur, &i)))
634                         return error;
635                 *bnop = NULLAGBLOCK;
636                 *stat = i;
637                 return 0;
638         }
639         /*
640          * Make a key out of the record data to be inserted, and save it.
641          */
642         key.ar_startblock = recp->ar_startblock;
643         key.ar_blockcount = recp->ar_blockcount;
644         optr = ptr = cur->bc_ptrs[level];
645         /*
646          * If we're off the left edge, return failure.
647          */
648         if (ptr == 0) {
649                 *stat = 0;
650                 return 0;
651         }
652         XFS_STATS_INC(xs_abt_insrec);
653         /*
654          * Get pointers to the btree buffer and block.
655          */
656         bp = cur->bc_bufs[level];
657         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
658         numrecs = be16_to_cpu(block->bb_numrecs);
659 #ifdef DEBUG
660         if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
661                 return error;
662         /*
663          * Check that the new entry is being inserted in the right place.
664          */
665         if (ptr <= numrecs) {
666                 if (level == 0) {
667                         rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
668                         xfs_btree_check_rec(cur->bc_btnum, recp, rp);
669                 } else {
670                         kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
671                         xfs_btree_check_key(cur->bc_btnum, &key, kp);
672                 }
673         }
674 #endif
675         nbno = NULLAGBLOCK;
676         ncur = NULL;
677         /*
678          * If the block is full, we can't insert the new entry until we
679          * make the block un-full.
680          */
681         if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
682                 /*
683                  * First, try shifting an entry to the right neighbor.
684                  */
685                 if ((error = xfs_btree_rshift(cur, level, &i)))
686                         return error;
687                 if (i) {
688                         /* nothing */
689                 }
690                 /*
691                  * Next, try shifting an entry to the left neighbor.
692                  */
693                 else {
694                         if ((error = xfs_alloc_lshift(cur, level, &i)))
695                                 return error;
696                         if (i)
697                                 optr = ptr = cur->bc_ptrs[level];
698                         else {
699                                 /*
700                                  * Next, try splitting the current block in
701                                  * half. If this works we have to re-set our
702                                  * variables because we could be in a
703                                  * different block now.
704                                  */
705                                 if ((error = xfs_alloc_split(cur, level, &nbno,
706                                                 &nkey, &ncur, &i)))
707                                         return error;
708                                 if (i) {
709                                         bp = cur->bc_bufs[level];
710                                         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
711 #ifdef DEBUG
712                                         if ((error =
713                                                 xfs_btree_check_sblock(cur,
714                                                         block, level, bp)))
715                                                 return error;
716 #endif
717                                         ptr = cur->bc_ptrs[level];
718                                         nrec.ar_startblock = nkey.ar_startblock;
719                                         nrec.ar_blockcount = nkey.ar_blockcount;
720                                 }
721                                 /*
722                                  * Otherwise the insert fails.
723                                  */
724                                 else {
725                                         *stat = 0;
726                                         return 0;
727                                 }
728                         }
729                 }
730         }
731         /*
732          * At this point we know there's room for our new entry in the block
733          * we're pointing at.
734          */
735         numrecs = be16_to_cpu(block->bb_numrecs);
736         if (level > 0) {
737                 /*
738                  * It's a non-leaf entry.  Make a hole for the new data
739                  * in the key and ptr regions of the block.
740                  */
741                 kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
742                 pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
743 #ifdef DEBUG
744                 for (i = numrecs; i >= ptr; i--) {
745                         if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
746                                 return error;
747                 }
748 #endif
749                 memmove(&kp[ptr], &kp[ptr - 1],
750                         (numrecs - ptr + 1) * sizeof(*kp));
751                 memmove(&pp[ptr], &pp[ptr - 1],
752                         (numrecs - ptr + 1) * sizeof(*pp));
753 #ifdef DEBUG
754                 if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
755                         return error;
756 #endif
757                 /*
758                  * Now stuff the new data in, bump numrecs and log the new data.
759                  */
760                 kp[ptr - 1] = key;
761                 pp[ptr - 1] = cpu_to_be32(*bnop);
762                 numrecs++;
763                 block->bb_numrecs = cpu_to_be16(numrecs);
764                 xfs_alloc_log_keys(cur, bp, ptr, numrecs);
765                 xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
766 #ifdef DEBUG
767                 if (ptr < numrecs)
768                         xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
769                                 kp + ptr);
770 #endif
771         } else {
772                 /*
773                  * It's a leaf entry.  Make a hole for the new record.
774                  */
775                 rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
776                 memmove(&rp[ptr], &rp[ptr - 1],
777                         (numrecs - ptr + 1) * sizeof(*rp));
778                 /*
779                  * Now stuff the new record in, bump numrecs
780                  * and log the new data.
781                  */
782                 rp[ptr - 1] = *recp;
783                 numrecs++;
784                 block->bb_numrecs = cpu_to_be16(numrecs);
785                 xfs_alloc_log_recs(cur, bp, ptr, numrecs);
786 #ifdef DEBUG
787                 if (ptr < numrecs)
788                         xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
789                                 rp + ptr);
790 #endif
791         }
792         /*
793          * Log the new number of records in the btree header.
794          */
795         xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
796         /*
797          * If we inserted at the start of a block, update the parents' keys.
798          */
799         if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
800                 return error;
801         /*
802          * Look to see if the longest extent in the allocation group
803          * needs to be updated.
804          */
805
806         agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
807         if (level == 0 &&
808             cur->bc_btnum == XFS_BTNUM_CNT &&
809             be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
810             be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
811                 /*
812                  * If this is a leaf in the by-size btree and there
813                  * is no right sibling block and this block is bigger
814                  * than the previous longest block, update it.
815                  */
816                 agf->agf_longest = recp->ar_blockcount;
817                 cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
818                         = be32_to_cpu(recp->ar_blockcount);
819                 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
820                         XFS_AGF_LONGEST);
821         }
822         /*
823          * Return the new block number, if any.
824          * If there is one, give back a record value and a cursor too.
825          */
826         *bnop = nbno;
827         if (nbno != NULLAGBLOCK) {
828                 *recp = nrec;
829                 *curp = ncur;
830         }
831         *stat = 1;
832         return 0;
833 }
834
835 /*
836  * Log header fields from a btree block.
837  */
838 STATIC void
839 xfs_alloc_log_block(
840         xfs_trans_t             *tp,    /* transaction pointer */
841         xfs_buf_t               *bp,    /* buffer containing btree block */
842         int                     fields) /* mask of fields: XFS_BB_... */
843 {
844         int                     first;  /* first byte offset logged */
845         int                     last;   /* last byte offset logged */
846         static const short      offsets[] = {   /* table of offsets */
847                 offsetof(xfs_alloc_block_t, bb_magic),
848                 offsetof(xfs_alloc_block_t, bb_level),
849                 offsetof(xfs_alloc_block_t, bb_numrecs),
850                 offsetof(xfs_alloc_block_t, bb_leftsib),
851                 offsetof(xfs_alloc_block_t, bb_rightsib),
852                 sizeof(xfs_alloc_block_t)
853         };
854
855         xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
856         xfs_trans_log_buf(tp, bp, first, last);
857 }
858
859 /*
860  * Log keys from a btree block (nonleaf).
861  */
862 STATIC void
863 xfs_alloc_log_keys(
864         xfs_btree_cur_t         *cur,   /* btree cursor */
865         xfs_buf_t               *bp,    /* buffer containing btree block */
866         int                     kfirst, /* index of first key to log */
867         int                     klast)  /* index of last key to log */
868 {
869         xfs_alloc_block_t       *block; /* btree block to log from */
870         int                     first;  /* first byte offset logged */
871         xfs_alloc_key_t         *kp;    /* key pointer in btree block */
872         int                     last;   /* last byte offset logged */
873
874         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
875         kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
876         first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
877         last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
878         xfs_trans_log_buf(cur->bc_tp, bp, first, last);
879 }
880
881 /*
882  * Log block pointer fields from a btree block (nonleaf).
883  */
884 STATIC void
885 xfs_alloc_log_ptrs(
886         xfs_btree_cur_t         *cur,   /* btree cursor */
887         xfs_buf_t               *bp,    /* buffer containing btree block */
888         int                     pfirst, /* index of first pointer to log */
889         int                     plast)  /* index of last pointer to log */
890 {
891         xfs_alloc_block_t       *block; /* btree block to log from */
892         int                     first;  /* first byte offset logged */
893         int                     last;   /* last byte offset logged */
894         xfs_alloc_ptr_t         *pp;    /* block-pointer pointer in btree blk */
895
896         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
897         pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
898         first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
899         last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
900         xfs_trans_log_buf(cur->bc_tp, bp, first, last);
901 }
902
903 /*
904  * Log records from a btree block (leaf).
905  */
906 STATIC void
907 xfs_alloc_log_recs(
908         xfs_btree_cur_t         *cur,   /* btree cursor */
909         xfs_buf_t               *bp,    /* buffer containing btree block */
910         int                     rfirst, /* index of first record to log */
911         int                     rlast)  /* index of last record to log */
912 {
913         xfs_alloc_block_t       *block; /* btree block to log from */
914         int                     first;  /* first byte offset logged */
915         int                     last;   /* last byte offset logged */
916         xfs_alloc_rec_t         *rp;    /* record pointer for btree block */
917
918
919         block = XFS_BUF_TO_ALLOC_BLOCK(bp);
920         rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
921 #ifdef DEBUG
922         {
923                 xfs_agf_t       *agf;
924                 xfs_alloc_rec_t *p;
925
926                 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
927                 for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
928                         ASSERT(be32_to_cpu(p->ar_startblock) +
929                                be32_to_cpu(p->ar_blockcount) <=
930                                be32_to_cpu(agf->agf_length));
931         }
932 #endif
933         first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
934         last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
935         xfs_trans_log_buf(cur->bc_tp, bp, first, last);
936 }
937
938 /*
939  * Move 1 record left from cur/level if possible.
940  * Update cur to reflect the new path.
941  */
942 STATIC int                              /* error */
943 xfs_alloc_lshift(
944         xfs_btree_cur_t         *cur,   /* btree cursor */
945         int                     level,  /* level to shift record on */
946         int                     *stat)  /* success/failure */
947 {
948         int                     error;  /* error return value */
949 #ifdef DEBUG
950         int                     i;      /* loop index */
951 #endif
952         xfs_alloc_key_t         key;    /* key value for leaf level upward */
953         xfs_buf_t               *lbp;   /* buffer for left neighbor block */
954         xfs_alloc_block_t       *left;  /* left neighbor btree block */
955         int                     nrec;   /* new number of left block entries */
956         xfs_buf_t               *rbp;   /* buffer for right (current) block */
957         xfs_alloc_block_t       *right; /* right (current) btree block */
958         xfs_alloc_key_t         *rkp=NULL;      /* key pointer for right block */
959         xfs_alloc_ptr_t         *rpp=NULL;      /* address pointer for right block */
960         xfs_alloc_rec_t         *rrp=NULL;      /* record pointer for right block */
961
962         /*
963          * Set up variables for this block as "right".
964          */
965         rbp = cur->bc_bufs[level];
966         right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
967 #ifdef DEBUG
968         if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
969                 return error;
970 #endif
971         /*
972          * If we've got no left sibling then we can't shift an entry left.
973          */
974         if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
975                 *stat = 0;
976                 return 0;
977         }
978         /*
979          * If the cursor entry is the one that would be moved, don't
980          * do it... it's too complicated.
981          */
982         if (cur->bc_ptrs[level] <= 1) {
983                 *stat = 0;
984                 return 0;
985         }
986         /*
987          * Set up the left neighbor as "left".
988          */
989         if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
990                         cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
991                         0, &lbp, XFS_ALLOC_BTREE_REF)))
992                 return error;
993         left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
994         if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
995                 return error;
996         /*
997          * If it's full, it can't take another entry.
998          */
999         if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
1000                 *stat = 0;
1001                 return 0;
1002         }
1003         nrec = be16_to_cpu(left->bb_numrecs) + 1;
1004         /*
1005          * If non-leaf, copy a key and a ptr to the left block.
1006          */
1007         if (level > 0) {
1008                 xfs_alloc_key_t *lkp;   /* key pointer for left block */
1009                 xfs_alloc_ptr_t *lpp;   /* address pointer for left block */
1010
1011                 lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
1012                 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1013                 *lkp = *rkp;
1014                 xfs_alloc_log_keys(cur, lbp, nrec, nrec);
1015                 lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
1016                 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1017 #ifdef DEBUG
1018                 if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
1019                         return error;
1020 #endif
1021                 *lpp = *rpp;
1022                 xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
1023                 xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
1024         }
1025         /*
1026          * If leaf, copy a record to the left block.
1027          */
1028         else {
1029                 xfs_alloc_rec_t *lrp;   /* record pointer for left block */
1030
1031                 lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
1032                 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1033                 *lrp = *rrp;
1034                 xfs_alloc_log_recs(cur, lbp, nrec, nrec);
1035                 xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
1036         }
1037         /*
1038          * Bump and log left's numrecs, decrement and log right's numrecs.
1039          */
1040         be16_add_cpu(&left->bb_numrecs, 1);
1041         xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
1042         be16_add_cpu(&right->bb_numrecs, -1);
1043         xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
1044         /*
1045          * Slide the contents of right down one entry.
1046          */
1047         if (level > 0) {
1048 #ifdef DEBUG
1049                 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1050                         if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
1051                                         level)))
1052                                 return error;
1053                 }
1054 #endif
1055                 memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1056                 memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1057                 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1058                 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1059         } else {
1060                 memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1061                 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1062                 key.ar_startblock = rrp->ar_startblock;
1063                 key.ar_blockcount = rrp->ar_blockcount;
1064                 rkp = &key;
1065         }
1066         /*
1067          * Update the parent key values of right.
1068          */
1069         if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
1070                 return error;
1071         /*
1072          * Slide the cursor value left one.
1073          */
1074         cur->bc_ptrs[level]--;
1075         *stat = 1;
1076         return 0;
1077 }
1078
1079 /*
1080  * Allocate a new root block, fill it in.
1081  */
1082 STATIC int                              /* error */
1083 xfs_alloc_newroot(
1084         xfs_btree_cur_t         *cur,   /* btree cursor */
1085         int                     *stat)  /* success/failure */
1086 {
1087         int                     error;  /* error return value */
1088         xfs_agblock_t           lbno;   /* left block number */
1089         xfs_buf_t               *lbp;   /* left btree buffer */
1090         xfs_alloc_block_t       *left;  /* left btree block */
1091         xfs_mount_t             *mp;    /* mount structure */
1092         xfs_agblock_t           nbno;   /* new block number */
1093         xfs_buf_t               *nbp;   /* new (root) buffer */
1094         xfs_alloc_block_t       *new;   /* new (root) btree block */
1095         int                     nptr;   /* new value for key index, 1 or 2 */
1096         xfs_agblock_t           rbno;   /* right block number */
1097         xfs_buf_t               *rbp;   /* right btree buffer */
1098         xfs_alloc_block_t       *right; /* right btree block */
1099
1100         mp = cur->bc_mp;
1101
1102         ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
1103         /*
1104          * Get a buffer from the freelist blocks, for the new root.
1105          */
1106         error = xfs_alloc_get_freelist(cur->bc_tp,
1107                                         cur->bc_private.a.agbp, &nbno, 1);
1108         if (error)
1109                 return error;
1110         /*
1111          * None available, we fail.
1112          */
1113         if (nbno == NULLAGBLOCK) {
1114                 *stat = 0;
1115                 return 0;
1116         }
1117         xfs_trans_agbtree_delta(cur->bc_tp, 1);
1118         nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
1119                 0);
1120         new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
1121         /*
1122          * Set the root data in the a.g. freespace structure.
1123          */
1124         {
1125                 xfs_agf_t       *agf;   /* a.g. freespace header */
1126                 xfs_agnumber_t  seqno;
1127
1128                 agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1129                 agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
1130                 be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
1131                 seqno = be32_to_cpu(agf->agf_seqno);
1132                 mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
1133                 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
1134                         XFS_AGF_ROOTS | XFS_AGF_LEVELS);
1135         }
1136         /*
1137          * At the previous root level there are now two blocks: the old
1138          * root, and the new block generated when it was split.
1139          * We don't know which one the cursor is pointing at, so we
1140          * set up variables "left" and "right" for each case.
1141          */
1142         lbp = cur->bc_bufs[cur->bc_nlevels - 1];
1143         left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1144 #ifdef DEBUG
1145         if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
1146                 return error;
1147 #endif
1148         if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
1149                 /*
1150                  * Our block is left, pick up the right block.
1151                  */
1152                 lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
1153                 rbno = be32_to_cpu(left->bb_rightsib);
1154                 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1155                                 cur->bc_private.a.agno, rbno, 0, &rbp,
1156                                 XFS_ALLOC_BTREE_REF)))
1157                         return error;
1158                 right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1159                 if ((error = xfs_btree_check_sblock(cur, right,
1160                                 cur->bc_nlevels - 1, rbp)))
1161                         return error;
1162                 nptr = 1;
1163         } else {
1164                 /*
1165                  * Our block is right, pick up the left block.
1166                  */
1167                 rbp = lbp;
1168                 right = left;
1169                 rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
1170                 lbno = be32_to_cpu(right->bb_leftsib);
1171                 if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
1172                                 cur->bc_private.a.agno, lbno, 0, &lbp,
1173                                 XFS_ALLOC_BTREE_REF)))
1174                         return error;
1175                 left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1176                 if ((error = xfs_btree_check_sblock(cur, left,
1177                                 cur->bc_nlevels - 1, lbp)))
1178                         return error;
1179                 nptr = 2;
1180         }
1181         /*
1182          * Fill in the new block's btree header and log it.
1183          */
1184         new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1185         new->bb_level = cpu_to_be16(cur->bc_nlevels);
1186         new->bb_numrecs = cpu_to_be16(2);
1187         new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1188         new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1189         xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
1190         ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
1191         /*
1192          * Fill in the key data in the new root.
1193          */
1194         {
1195                 xfs_alloc_key_t         *kp;    /* btree key pointer */
1196
1197                 kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
1198                 if (be16_to_cpu(left->bb_level) > 0) {
1199                         kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
1200                         kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
1201                 } else {
1202                         xfs_alloc_rec_t *rp;    /* btree record pointer */
1203
1204                         rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
1205                         kp[0].ar_startblock = rp->ar_startblock;
1206                         kp[0].ar_blockcount = rp->ar_blockcount;
1207                         rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1208                         kp[1].ar_startblock = rp->ar_startblock;
1209                         kp[1].ar_blockcount = rp->ar_blockcount;
1210                 }
1211         }
1212         xfs_alloc_log_keys(cur, nbp, 1, 2);
1213         /*
1214          * Fill in the pointer data in the new root.
1215          */
1216         {
1217                 xfs_alloc_ptr_t         *pp;    /* btree address pointer */
1218
1219                 pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
1220                 pp[0] = cpu_to_be32(lbno);
1221                 pp[1] = cpu_to_be32(rbno);
1222         }
1223         xfs_alloc_log_ptrs(cur, nbp, 1, 2);
1224         /*
1225          * Fix up the cursor.
1226          */
1227         xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
1228         cur->bc_ptrs[cur->bc_nlevels] = nptr;
1229         cur->bc_nlevels++;
1230         *stat = 1;
1231         return 0;
1232 }
1233
1234 /*
1235  * Split cur/level block in half.
1236  * Return new block number and its first record (to be inserted into parent).
1237  */
1238 STATIC int                              /* error */
1239 xfs_alloc_split(
1240         xfs_btree_cur_t         *cur,   /* btree cursor */
1241         int                     level,  /* level to split */
1242         xfs_agblock_t           *bnop,  /* output: block number allocated */
1243         xfs_alloc_key_t         *keyp,  /* output: first key of new block */
1244         xfs_btree_cur_t         **curp, /* output: new cursor */
1245         int                     *stat)  /* success/failure */
1246 {
1247         int                     error;  /* error return value */
1248         int                     i;      /* loop index/record number */
1249         xfs_agblock_t           lbno;   /* left (current) block number */
1250         xfs_buf_t               *lbp;   /* buffer for left block */
1251         xfs_alloc_block_t       *left;  /* left (current) btree block */
1252         xfs_agblock_t           rbno;   /* right (new) block number */
1253         xfs_buf_t               *rbp;   /* buffer for right block */
1254         xfs_alloc_block_t       *right; /* right (new) btree block */
1255
1256         /*
1257          * Allocate the new block from the freelist.
1258          * If we can't do it, we're toast.  Give up.
1259          */
1260         error = xfs_alloc_get_freelist(cur->bc_tp,
1261                                          cur->bc_private.a.agbp, &rbno, 1);
1262         if (error)
1263                 return error;
1264         if (rbno == NULLAGBLOCK) {
1265                 *stat = 0;
1266                 return 0;
1267         }
1268         xfs_trans_agbtree_delta(cur->bc_tp, 1);
1269         rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
1270                 rbno, 0);
1271         /*
1272          * Set up the new block as "right".
1273          */
1274         right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
1275         /*
1276          * "Left" is the current (according to the cursor) block.
1277          */
1278         lbp = cur->bc_bufs[level];
1279         left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
1280 #ifdef DEBUG
1281         if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
1282                 return error;
1283 #endif
1284         /*
1285          * Fill in the btree header for the new block.
1286          */
1287         right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
1288         right->bb_level = left->bb_level;
1289         right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
1290         /*
1291          * Make sure that if there's an odd number of entries now, that
1292          * each new block will have the same number of entries.
1293          */
1294         if ((be16_to_cpu(left->bb_numrecs) & 1) &&
1295             cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
1296                 be16_add_cpu(&right->bb_numrecs, 1);
1297         i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
1298         /*
1299          * For non-leaf blocks, copy keys and addresses over to the new block.
1300          */
1301         if (level > 0) {
1302                 xfs_alloc_key_t *lkp;   /* left btree key pointer */
1303                 xfs_alloc_ptr_t *lpp;   /* left btree address pointer */
1304                 xfs_alloc_key_t *rkp;   /* right btree key pointer */
1305                 xfs_alloc_ptr_t *rpp;   /* right btree address pointer */
1306
1307                 lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
1308                 lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
1309                 rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
1310                 rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
1311 #ifdef DEBUG
1312                 for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
1313                         if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
1314                                 return error;
1315                 }
1316 #endif
1317                 memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
1318                 memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
1319                 xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1320                 xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1321                 *keyp = *rkp;
1322         }
1323         /*
1324          * For leaf blocks, copy records over to the new block.
1325          */
1326         else {
1327                 xfs_alloc_rec_t *lrp;   /* left btree record pointer */
1328                 xfs_alloc_rec_t *rrp;   /* right btree record pointer */
1329
1330                 lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
1331                 rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
1332                 memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
1333                 xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
1334                 keyp->ar_startblock = rrp->ar_startblock;
1335                 keyp->ar_blockcount = rrp->ar_blockcount;
1336         }
1337         /*
1338          * Find the left block number by looking in the buffer.
1339          * Adjust numrecs, sibling pointers.
1340          */
1341         lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
1342         be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
1343         right->bb_rightsib = left->bb_rightsib;
1344         left->bb_rightsib = cpu_to_be32(rbno);
1345         right->bb_leftsib = cpu_to_be32(lbno);
1346         xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
1347         xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
1348         /*
1349          * If there's a block to the new block's right, make that block
1350          * point back to right instead of to left.
1351          */
1352         if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
1353                 xfs_alloc_block_t       *rrblock;       /* rr btree block */
1354                 xfs_buf_t               *rrbp;          /* buffer for rrblock */
1355
1356                 if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
1357                                 cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
1358                                 &rrbp, XFS_ALLOC_BTREE_REF)))
1359                         return error;
1360                 rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
1361                 if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
1362                         return error;
1363                 rrblock->bb_leftsib = cpu_to_be32(rbno);
1364                 xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
1365         }
1366         /*
1367          * If the cursor is really in the right block, move it there.
1368          * If it's just pointing past the last entry in left, then we'll
1369          * insert there, so don't change anything in that case.
1370          */
1371         if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
1372                 xfs_btree_setbuf(cur, level, rbp);
1373                 cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
1374         }
1375         /*
1376          * If there are more levels, we'll need another cursor which refers to
1377          * the right block, no matter where this cursor was.
1378          */
1379         if (level + 1 < cur->bc_nlevels) {
1380                 if ((error = xfs_btree_dup_cursor(cur, curp)))
1381                         return error;
1382                 (*curp)->bc_ptrs[level + 1]++;
1383         }
1384         *bnop = rbno;
1385         *stat = 1;
1386         return 0;
1387 }
1388
1389 /*
1390  * Externally visible routines.
1391  */
1392
1393 /*
1394  * Delete the record pointed to by cur.
1395  * The cursor refers to the place where the record was (could be inserted)
1396  * when the operation returns.
1397  */
1398 int                                     /* error */
1399 xfs_alloc_delete(
1400         xfs_btree_cur_t *cur,           /* btree cursor */
1401         int             *stat)          /* success/failure */
1402 {
1403         int             error;          /* error return value */
1404         int             i;              /* result code */
1405         int             level;          /* btree level */
1406
1407         /*
1408          * Go up the tree, starting at leaf level.
1409          * If 2 is returned then a join was done; go to the next level.
1410          * Otherwise we are done.
1411          */
1412         for (level = 0, i = 2; i == 2; level++) {
1413                 if ((error = xfs_alloc_delrec(cur, level, &i)))
1414                         return error;
1415         }
1416         if (i == 0) {
1417                 for (level = 1; level < cur->bc_nlevels; level++) {
1418                         if (cur->bc_ptrs[level] == 0) {
1419                                 if ((error = xfs_btree_decrement(cur, level, &i)))
1420                                         return error;
1421                                 break;
1422                         }
1423                 }
1424         }
1425         *stat = i;
1426         return 0;
1427 }
1428
1429 /*
1430  * Get the data from the pointed-to record.
1431  */
1432 int                                     /* error */
1433 xfs_alloc_get_rec(
1434         xfs_btree_cur_t         *cur,   /* btree cursor */
1435         xfs_agblock_t           *bno,   /* output: starting block of extent */
1436         xfs_extlen_t            *len,   /* output: length of extent */
1437         int                     *stat)  /* output: success/failure */
1438 {
1439         xfs_alloc_block_t       *block; /* btree block */
1440 #ifdef DEBUG
1441         int                     error;  /* error return value */
1442 #endif
1443         int                     ptr;    /* record number */
1444
1445         ptr = cur->bc_ptrs[0];
1446         block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
1447 #ifdef DEBUG
1448         if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
1449                 return error;
1450 #endif
1451         /*
1452          * Off the right end or left end, return failure.
1453          */
1454         if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
1455                 *stat = 0;
1456                 return 0;
1457         }
1458         /*
1459          * Point to the record and extract its data.
1460          */
1461         {
1462                 xfs_alloc_rec_t         *rec;   /* record data */
1463
1464                 rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
1465                 *bno = be32_to_cpu(rec->ar_startblock);
1466                 *len = be32_to_cpu(rec->ar_blockcount);
1467         }
1468         *stat = 1;
1469         return 0;
1470 }
1471
1472 /*
1473  * Insert the current record at the point referenced by cur.
1474  * The cursor may be inconsistent on return if splits have been done.
1475  */
1476 int                                     /* error */
1477 xfs_alloc_insert(
1478         xfs_btree_cur_t *cur,           /* btree cursor */
1479         int             *stat)          /* success/failure */
1480 {
1481         int             error;          /* error return value */
1482         int             i;              /* result value, 0 for failure */
1483         int             level;          /* current level number in btree */
1484         xfs_agblock_t   nbno;           /* new block number (split result) */
1485         xfs_btree_cur_t *ncur;          /* new cursor (split result) */
1486         xfs_alloc_rec_t nrec;           /* record being inserted this level */
1487         xfs_btree_cur_t *pcur;          /* previous level's cursor */
1488
1489         level = 0;
1490         nbno = NULLAGBLOCK;
1491         nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
1492         nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
1493         ncur = NULL;
1494         pcur = cur;
1495         /*
1496          * Loop going up the tree, starting at the leaf level.
1497          * Stop when we don't get a split block, that must mean that
1498          * the insert is finished with this level.
1499          */
1500         do {
1501                 /*
1502                  * Insert nrec/nbno into this level of the tree.
1503                  * Note if we fail, nbno will be null.
1504                  */
1505                 if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
1506                                 &i))) {
1507                         if (pcur != cur)
1508                                 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
1509                         return error;
1510                 }
1511                 /*
1512                  * See if the cursor we just used is trash.
1513                  * Can't trash the caller's cursor, but otherwise we should
1514                  * if ncur is a new cursor or we're about to be done.
1515                  */
1516                 if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
1517                         cur->bc_nlevels = pcur->bc_nlevels;
1518                         xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
1519                 }
1520                 /*
1521                  * If we got a new cursor, switch to it.
1522                  */
1523                 if (ncur) {
1524                         pcur = ncur;
1525                         ncur = NULL;
1526                 }
1527         } while (nbno != NULLAGBLOCK);
1528         *stat = i;
1529         return 0;
1530 }
1531
1532 STATIC struct xfs_btree_cur *
1533 xfs_allocbt_dup_cursor(
1534         struct xfs_btree_cur    *cur)
1535 {
1536         return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
1537                         cur->bc_private.a.agbp, cur->bc_private.a.agno,
1538                         cur->bc_btnum);
1539 }
1540
1541 /*
1542  * Update the longest extent in the AGF
1543  */
1544 STATIC void
1545 xfs_allocbt_update_lastrec(
1546         struct xfs_btree_cur    *cur,
1547         struct xfs_btree_block  *block,
1548         union xfs_btree_rec     *rec,
1549         int                     ptr,
1550         int                     reason)
1551 {
1552         struct xfs_agf          *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1553         xfs_agnumber_t          seqno = be32_to_cpu(agf->agf_seqno);
1554         __be32                  len;
1555
1556         ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1557
1558         switch (reason) {
1559         case LASTREC_UPDATE:
1560                 /*
1561                  * If this is the last leaf block and it's the last record,
1562                  * then update the size of the longest extent in the AG.
1563                  */
1564                 if (ptr != xfs_btree_get_numrecs(block))
1565                         return;
1566                 len = rec->alloc.ar_blockcount;
1567                 break;
1568         default:
1569                 ASSERT(0);
1570                 return;
1571         }
1572
1573         agf->agf_longest = len;
1574         cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
1575         xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
1576 }
1577
1578 STATIC int
1579 xfs_allocbt_get_maxrecs(
1580         struct xfs_btree_cur    *cur,
1581         int                     level)
1582 {
1583         return cur->bc_mp->m_alloc_mxr[level != 0];
1584 }
1585
1586 STATIC void
1587 xfs_allocbt_init_key_from_rec(
1588         union xfs_btree_key     *key,
1589         union xfs_btree_rec     *rec)
1590 {
1591         ASSERT(rec->alloc.ar_startblock != 0);
1592
1593         key->alloc.ar_startblock = rec->alloc.ar_startblock;
1594         key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
1595 }
1596
1597 STATIC void
1598 xfs_allocbt_init_ptr_from_cur(
1599         struct xfs_btree_cur    *cur,
1600         union xfs_btree_ptr     *ptr)
1601 {
1602         struct xfs_agf          *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
1603
1604         ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
1605         ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
1606
1607         ptr->s = agf->agf_roots[cur->bc_btnum];
1608 }
1609
1610 STATIC __int64_t
1611 xfs_allocbt_key_diff(
1612         struct xfs_btree_cur    *cur,
1613         union xfs_btree_key     *key)
1614 {
1615         xfs_alloc_rec_incore_t  *rec = &cur->bc_rec.a;
1616         xfs_alloc_key_t         *kp = &key->alloc;
1617         __int64_t               diff;
1618
1619         if (cur->bc_btnum == XFS_BTNUM_BNO) {
1620                 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
1621                                 rec->ar_startblock;
1622         }
1623
1624         diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
1625         if (diff)
1626                 return diff;
1627
1628         return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
1629 }
1630
1631 #ifdef XFS_BTREE_TRACE
1632 ktrace_t        *xfs_allocbt_trace_buf;
1633
1634 STATIC void
1635 xfs_allocbt_trace_enter(
1636         struct xfs_btree_cur    *cur,
1637         const char              *func,
1638         char                    *s,
1639         int                     type,
1640         int                     line,
1641         __psunsigned_t          a0,
1642         __psunsigned_t          a1,
1643         __psunsigned_t          a2,
1644         __psunsigned_t          a3,
1645         __psunsigned_t          a4,
1646         __psunsigned_t          a5,
1647         __psunsigned_t          a6,
1648         __psunsigned_t          a7,
1649         __psunsigned_t          a8,
1650         __psunsigned_t          a9,
1651         __psunsigned_t          a10)
1652 {
1653         ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
1654                 (void *)func, (void *)s, NULL, (void *)cur,
1655                 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
1656                 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
1657                 (void *)a8, (void *)a9, (void *)a10);
1658 }
1659
1660 STATIC void
1661 xfs_allocbt_trace_cursor(
1662         struct xfs_btree_cur    *cur,
1663         __uint32_t              *s0,
1664         __uint64_t              *l0,
1665         __uint64_t              *l1)
1666 {
1667         *s0 = cur->bc_private.a.agno;
1668         *l0 = cur->bc_rec.a.ar_startblock;
1669         *l1 = cur->bc_rec.a.ar_blockcount;
1670 }
1671
1672 STATIC void
1673 xfs_allocbt_trace_key(
1674         struct xfs_btree_cur    *cur,
1675         union xfs_btree_key     *key,
1676         __uint64_t              *l0,
1677         __uint64_t              *l1)
1678 {
1679         *l0 = be32_to_cpu(key->alloc.ar_startblock);
1680         *l1 = be32_to_cpu(key->alloc.ar_blockcount);
1681 }
1682
1683 STATIC void
1684 xfs_allocbt_trace_record(
1685         struct xfs_btree_cur    *cur,
1686         union xfs_btree_rec     *rec,
1687         __uint64_t              *l0,
1688         __uint64_t              *l1,
1689         __uint64_t              *l2)
1690 {
1691         *l0 = be32_to_cpu(rec->alloc.ar_startblock);
1692         *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
1693         *l2 = 0;
1694 }
1695 #endif /* XFS_BTREE_TRACE */
1696
1697 static const struct xfs_btree_ops xfs_allocbt_ops = {
1698         .rec_len                = sizeof(xfs_alloc_rec_t),
1699         .key_len                = sizeof(xfs_alloc_key_t),
1700
1701         .dup_cursor             = xfs_allocbt_dup_cursor,
1702         .update_lastrec         = xfs_allocbt_update_lastrec,
1703         .get_maxrecs            = xfs_allocbt_get_maxrecs,
1704         .init_key_from_rec      = xfs_allocbt_init_key_from_rec,
1705         .init_ptr_from_cur      = xfs_allocbt_init_ptr_from_cur,
1706         .key_diff               = xfs_allocbt_key_diff,
1707
1708 #ifdef XFS_BTREE_TRACE
1709         .trace_enter            = xfs_allocbt_trace_enter,
1710         .trace_cursor           = xfs_allocbt_trace_cursor,
1711         .trace_key              = xfs_allocbt_trace_key,
1712         .trace_record           = xfs_allocbt_trace_record,
1713 #endif
1714 };
1715
1716 /*
1717  * Allocate a new allocation btree cursor.
1718  */
1719 struct xfs_btree_cur *                  /* new alloc btree cursor */
1720 xfs_allocbt_init_cursor(
1721         struct xfs_mount        *mp,            /* file system mount point */
1722         struct xfs_trans        *tp,            /* transaction pointer */
1723         struct xfs_buf          *agbp,          /* buffer for agf structure */
1724         xfs_agnumber_t          agno,           /* allocation group number */
1725         xfs_btnum_t             btnum)          /* btree identifier */
1726 {
1727         struct xfs_agf          *agf = XFS_BUF_TO_AGF(agbp);
1728         struct xfs_btree_cur    *cur;
1729
1730         ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
1731
1732         cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
1733
1734         cur->bc_tp = tp;
1735         cur->bc_mp = mp;
1736         cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1737         cur->bc_btnum = btnum;
1738         cur->bc_blocklog = mp->m_sb.sb_blocklog;
1739
1740         cur->bc_ops = &xfs_allocbt_ops;
1741         if (btnum == XFS_BTNUM_CNT)
1742                 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
1743
1744         cur->bc_private.a.agbp = agbp;
1745         cur->bc_private.a.agno = agno;
1746
1747         return cur;
1748 }