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