]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_btree.c
Merge branch 'xfs-misc-fixes-1-for-3.16' into for-next
[karo-tx-linux.git] / fs / xfs / xfs_btree.c
1 /*
2  * Copyright (c) 2000-2002,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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_btree.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
36
37 /*
38  * Cursor allocation zone.
39  */
40 kmem_zone_t     *xfs_btree_cur_zone;
41
42 /*
43  * Btree magic numbers.
44  */
45 static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
46         { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
47           XFS_FIBT_MAGIC },
48         { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
49           XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC }
50 };
51 #define xfs_btree_magic(cur) \
52         xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
53
54
55 STATIC int                              /* error (0 or EFSCORRUPTED) */
56 xfs_btree_check_lblock(
57         struct xfs_btree_cur    *cur,   /* btree cursor */
58         struct xfs_btree_block  *block, /* btree long form block pointer */
59         int                     level,  /* level of the btree block */
60         struct xfs_buf          *bp)    /* buffer for block, if any */
61 {
62         int                     lblock_ok = 1; /* block passes checks */
63         struct xfs_mount        *mp;    /* file system mount point */
64
65         mp = cur->bc_mp;
66
67         if (xfs_sb_version_hascrc(&mp->m_sb)) {
68                 lblock_ok = lblock_ok &&
69                         uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
70                         block->bb_u.l.bb_blkno == cpu_to_be64(
71                                 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
72         }
73
74         lblock_ok = lblock_ok &&
75                 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
76                 be16_to_cpu(block->bb_level) == level &&
77                 be16_to_cpu(block->bb_numrecs) <=
78                         cur->bc_ops->get_maxrecs(cur, level) &&
79                 block->bb_u.l.bb_leftsib &&
80                 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
81                  XFS_FSB_SANITY_CHECK(mp,
82                         be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
83                 block->bb_u.l.bb_rightsib &&
84                 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
85                  XFS_FSB_SANITY_CHECK(mp,
86                         be64_to_cpu(block->bb_u.l.bb_rightsib)));
87
88         if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
89                         XFS_ERRTAG_BTREE_CHECK_LBLOCK,
90                         XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
91                 if (bp)
92                         trace_xfs_btree_corrupt(bp, _RET_IP_);
93                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
94                 return XFS_ERROR(EFSCORRUPTED);
95         }
96         return 0;
97 }
98
99 STATIC int                              /* error (0 or EFSCORRUPTED) */
100 xfs_btree_check_sblock(
101         struct xfs_btree_cur    *cur,   /* btree cursor */
102         struct xfs_btree_block  *block, /* btree short form block pointer */
103         int                     level,  /* level of the btree block */
104         struct xfs_buf          *bp)    /* buffer containing block */
105 {
106         struct xfs_mount        *mp;    /* file system mount point */
107         struct xfs_buf          *agbp;  /* buffer for ag. freespace struct */
108         struct xfs_agf          *agf;   /* ag. freespace structure */
109         xfs_agblock_t           agflen; /* native ag. freespace length */
110         int                     sblock_ok = 1; /* block passes checks */
111
112         mp = cur->bc_mp;
113         agbp = cur->bc_private.a.agbp;
114         agf = XFS_BUF_TO_AGF(agbp);
115         agflen = be32_to_cpu(agf->agf_length);
116
117         if (xfs_sb_version_hascrc(&mp->m_sb)) {
118                 sblock_ok = sblock_ok &&
119                         uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
120                         block->bb_u.s.bb_blkno == cpu_to_be64(
121                                 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
122         }
123
124         sblock_ok = sblock_ok &&
125                 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
126                 be16_to_cpu(block->bb_level) == level &&
127                 be16_to_cpu(block->bb_numrecs) <=
128                         cur->bc_ops->get_maxrecs(cur, level) &&
129                 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
130                  be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
131                 block->bb_u.s.bb_leftsib &&
132                 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
133                  be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
134                 block->bb_u.s.bb_rightsib;
135
136         if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
137                         XFS_ERRTAG_BTREE_CHECK_SBLOCK,
138                         XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
139                 if (bp)
140                         trace_xfs_btree_corrupt(bp, _RET_IP_);
141                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
142                 return XFS_ERROR(EFSCORRUPTED);
143         }
144         return 0;
145 }
146
147 /*
148  * Debug routine: check that block header is ok.
149  */
150 int
151 xfs_btree_check_block(
152         struct xfs_btree_cur    *cur,   /* btree cursor */
153         struct xfs_btree_block  *block, /* generic btree block pointer */
154         int                     level,  /* level of the btree block */
155         struct xfs_buf          *bp)    /* buffer containing block, if any */
156 {
157         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
158                 return xfs_btree_check_lblock(cur, block, level, bp);
159         else
160                 return xfs_btree_check_sblock(cur, block, level, bp);
161 }
162
163 /*
164  * Check that (long) pointer is ok.
165  */
166 int                                     /* error (0 or EFSCORRUPTED) */
167 xfs_btree_check_lptr(
168         struct xfs_btree_cur    *cur,   /* btree cursor */
169         xfs_dfsbno_t            bno,    /* btree block disk address */
170         int                     level)  /* btree block level */
171 {
172         XFS_WANT_CORRUPTED_RETURN(
173                 level > 0 &&
174                 bno != NULLDFSBNO &&
175                 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
176         return 0;
177 }
178
179 #ifdef DEBUG
180 /*
181  * Check that (short) pointer is ok.
182  */
183 STATIC int                              /* error (0 or EFSCORRUPTED) */
184 xfs_btree_check_sptr(
185         struct xfs_btree_cur    *cur,   /* btree cursor */
186         xfs_agblock_t           bno,    /* btree block disk address */
187         int                     level)  /* btree block level */
188 {
189         xfs_agblock_t           agblocks = cur->bc_mp->m_sb.sb_agblocks;
190
191         XFS_WANT_CORRUPTED_RETURN(
192                 level > 0 &&
193                 bno != NULLAGBLOCK &&
194                 bno != 0 &&
195                 bno < agblocks);
196         return 0;
197 }
198
199 /*
200  * Check that block ptr is ok.
201  */
202 STATIC int                              /* error (0 or EFSCORRUPTED) */
203 xfs_btree_check_ptr(
204         struct xfs_btree_cur    *cur,   /* btree cursor */
205         union xfs_btree_ptr     *ptr,   /* btree block disk address */
206         int                     index,  /* offset from ptr to check */
207         int                     level)  /* btree block level */
208 {
209         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
210                 return xfs_btree_check_lptr(cur,
211                                 be64_to_cpu((&ptr->l)[index]), level);
212         } else {
213                 return xfs_btree_check_sptr(cur,
214                                 be32_to_cpu((&ptr->s)[index]), level);
215         }
216 }
217 #endif
218
219 /*
220  * Calculate CRC on the whole btree block and stuff it into the
221  * long-form btree header.
222  *
223  * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
224  * it into the buffer so recovery knows what the last modifcation was that made
225  * it to disk.
226  */
227 void
228 xfs_btree_lblock_calc_crc(
229         struct xfs_buf          *bp)
230 {
231         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
232         struct xfs_buf_log_item *bip = bp->b_fspriv;
233
234         if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
235                 return;
236         if (bip)
237                 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
238         xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
239 }
240
241 bool
242 xfs_btree_lblock_verify_crc(
243         struct xfs_buf          *bp)
244 {
245         if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
246                 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
247
248         return true;
249 }
250
251 /*
252  * Calculate CRC on the whole btree block and stuff it into the
253  * short-form btree header.
254  *
255  * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
256  * it into the buffer so recovery knows what the last modifcation was that made
257  * it to disk.
258  */
259 void
260 xfs_btree_sblock_calc_crc(
261         struct xfs_buf          *bp)
262 {
263         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
264         struct xfs_buf_log_item *bip = bp->b_fspriv;
265
266         if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
267                 return;
268         if (bip)
269                 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
270         xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
271 }
272
273 bool
274 xfs_btree_sblock_verify_crc(
275         struct xfs_buf          *bp)
276 {
277         if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
278                 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
279
280         return true;
281 }
282
283 /*
284  * Delete the btree cursor.
285  */
286 void
287 xfs_btree_del_cursor(
288         xfs_btree_cur_t *cur,           /* btree cursor */
289         int             error)          /* del because of error */
290 {
291         int             i;              /* btree level */
292
293         /*
294          * Clear the buffer pointers, and release the buffers.
295          * If we're doing this in the face of an error, we
296          * need to make sure to inspect all of the entries
297          * in the bc_bufs array for buffers to be unlocked.
298          * This is because some of the btree code works from
299          * level n down to 0, and if we get an error along
300          * the way we won't have initialized all the entries
301          * down to 0.
302          */
303         for (i = 0; i < cur->bc_nlevels; i++) {
304                 if (cur->bc_bufs[i])
305                         xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
306                 else if (!error)
307                         break;
308         }
309         /*
310          * Can't free a bmap cursor without having dealt with the
311          * allocated indirect blocks' accounting.
312          */
313         ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
314                cur->bc_private.b.allocated == 0);
315         /*
316          * Free the cursor.
317          */
318         kmem_zone_free(xfs_btree_cur_zone, cur);
319 }
320
321 /*
322  * Duplicate the btree cursor.
323  * Allocate a new one, copy the record, re-get the buffers.
324  */
325 int                                     /* error */
326 xfs_btree_dup_cursor(
327         xfs_btree_cur_t *cur,           /* input cursor */
328         xfs_btree_cur_t **ncur)         /* output cursor */
329 {
330         xfs_buf_t       *bp;            /* btree block's buffer pointer */
331         int             error;          /* error return value */
332         int             i;              /* level number of btree block */
333         xfs_mount_t     *mp;            /* mount structure for filesystem */
334         xfs_btree_cur_t *new;           /* new cursor value */
335         xfs_trans_t     *tp;            /* transaction pointer, can be NULL */
336
337         tp = cur->bc_tp;
338         mp = cur->bc_mp;
339
340         /*
341          * Allocate a new cursor like the old one.
342          */
343         new = cur->bc_ops->dup_cursor(cur);
344
345         /*
346          * Copy the record currently in the cursor.
347          */
348         new->bc_rec = cur->bc_rec;
349
350         /*
351          * For each level current, re-get the buffer and copy the ptr value.
352          */
353         for (i = 0; i < new->bc_nlevels; i++) {
354                 new->bc_ptrs[i] = cur->bc_ptrs[i];
355                 new->bc_ra[i] = cur->bc_ra[i];
356                 bp = cur->bc_bufs[i];
357                 if (bp) {
358                         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
359                                                    XFS_BUF_ADDR(bp), mp->m_bsize,
360                                                    0, &bp,
361                                                    cur->bc_ops->buf_ops);
362                         if (error) {
363                                 xfs_btree_del_cursor(new, error);
364                                 *ncur = NULL;
365                                 return error;
366                         }
367                 }
368                 new->bc_bufs[i] = bp;
369         }
370         *ncur = new;
371         return 0;
372 }
373
374 /*
375  * XFS btree block layout and addressing:
376  *
377  * There are two types of blocks in the btree: leaf and non-leaf blocks.
378  *
379  * The leaf record start with a header then followed by records containing
380  * the values.  A non-leaf block also starts with the same header, and
381  * then first contains lookup keys followed by an equal number of pointers
382  * to the btree blocks at the previous level.
383  *
384  *              +--------+-------+-------+-------+-------+-------+-------+
385  * Leaf:        | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
386  *              +--------+-------+-------+-------+-------+-------+-------+
387  *
388  *              +--------+-------+-------+-------+-------+-------+-------+
389  * Non-Leaf:    | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
390  *              +--------+-------+-------+-------+-------+-------+-------+
391  *
392  * The header is called struct xfs_btree_block for reasons better left unknown
393  * and comes in different versions for short (32bit) and long (64bit) block
394  * pointers.  The record and key structures are defined by the btree instances
395  * and opaque to the btree core.  The block pointers are simple disk endian
396  * integers, available in a short (32bit) and long (64bit) variant.
397  *
398  * The helpers below calculate the offset of a given record, key or pointer
399  * into a btree block (xfs_btree_*_offset) or return a pointer to the given
400  * record, key or pointer (xfs_btree_*_addr).  Note that all addressing
401  * inside the btree block is done using indices starting at one, not zero!
402  */
403
404 /*
405  * Return size of the btree block header for this btree instance.
406  */
407 static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
408 {
409         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
410                 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
411                         return XFS_BTREE_LBLOCK_CRC_LEN;
412                 return XFS_BTREE_LBLOCK_LEN;
413         }
414         if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
415                 return XFS_BTREE_SBLOCK_CRC_LEN;
416         return XFS_BTREE_SBLOCK_LEN;
417 }
418
419 /*
420  * Return size of btree block pointers for this btree instance.
421  */
422 static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
423 {
424         return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
425                 sizeof(__be64) : sizeof(__be32);
426 }
427
428 /*
429  * Calculate offset of the n-th record in a btree block.
430  */
431 STATIC size_t
432 xfs_btree_rec_offset(
433         struct xfs_btree_cur    *cur,
434         int                     n)
435 {
436         return xfs_btree_block_len(cur) +
437                 (n - 1) * cur->bc_ops->rec_len;
438 }
439
440 /*
441  * Calculate offset of the n-th key in a btree block.
442  */
443 STATIC size_t
444 xfs_btree_key_offset(
445         struct xfs_btree_cur    *cur,
446         int                     n)
447 {
448         return xfs_btree_block_len(cur) +
449                 (n - 1) * cur->bc_ops->key_len;
450 }
451
452 /*
453  * Calculate offset of the n-th block pointer in a btree block.
454  */
455 STATIC size_t
456 xfs_btree_ptr_offset(
457         struct xfs_btree_cur    *cur,
458         int                     n,
459         int                     level)
460 {
461         return xfs_btree_block_len(cur) +
462                 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
463                 (n - 1) * xfs_btree_ptr_len(cur);
464 }
465
466 /*
467  * Return a pointer to the n-th record in the btree block.
468  */
469 STATIC union xfs_btree_rec *
470 xfs_btree_rec_addr(
471         struct xfs_btree_cur    *cur,
472         int                     n,
473         struct xfs_btree_block  *block)
474 {
475         return (union xfs_btree_rec *)
476                 ((char *)block + xfs_btree_rec_offset(cur, n));
477 }
478
479 /*
480  * Return a pointer to the n-th key in the btree block.
481  */
482 STATIC union xfs_btree_key *
483 xfs_btree_key_addr(
484         struct xfs_btree_cur    *cur,
485         int                     n,
486         struct xfs_btree_block  *block)
487 {
488         return (union xfs_btree_key *)
489                 ((char *)block + xfs_btree_key_offset(cur, n));
490 }
491
492 /*
493  * Return a pointer to the n-th block pointer in the btree block.
494  */
495 STATIC union xfs_btree_ptr *
496 xfs_btree_ptr_addr(
497         struct xfs_btree_cur    *cur,
498         int                     n,
499         struct xfs_btree_block  *block)
500 {
501         int                     level = xfs_btree_get_level(block);
502
503         ASSERT(block->bb_level != 0);
504
505         return (union xfs_btree_ptr *)
506                 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
507 }
508
509 /*
510  * Get the root block which is stored in the inode.
511  *
512  * For now this btree implementation assumes the btree root is always
513  * stored in the if_broot field of an inode fork.
514  */
515 STATIC struct xfs_btree_block *
516 xfs_btree_get_iroot(
517        struct xfs_btree_cur    *cur)
518 {
519        struct xfs_ifork        *ifp;
520
521        ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
522        return (struct xfs_btree_block *)ifp->if_broot;
523 }
524
525 /*
526  * Retrieve the block pointer from the cursor at the given level.
527  * This may be an inode btree root or from a buffer.
528  */
529 STATIC struct xfs_btree_block *         /* generic btree block pointer */
530 xfs_btree_get_block(
531         struct xfs_btree_cur    *cur,   /* btree cursor */
532         int                     level,  /* level in btree */
533         struct xfs_buf          **bpp)  /* buffer containing the block */
534 {
535         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
536             (level == cur->bc_nlevels - 1)) {
537                 *bpp = NULL;
538                 return xfs_btree_get_iroot(cur);
539         }
540
541         *bpp = cur->bc_bufs[level];
542         return XFS_BUF_TO_BLOCK(*bpp);
543 }
544
545 /*
546  * Get a buffer for the block, return it with no data read.
547  * Long-form addressing.
548  */
549 xfs_buf_t *                             /* buffer for fsbno */
550 xfs_btree_get_bufl(
551         xfs_mount_t     *mp,            /* file system mount point */
552         xfs_trans_t     *tp,            /* transaction pointer */
553         xfs_fsblock_t   fsbno,          /* file system block number */
554         uint            lock)           /* lock flags for get_buf */
555 {
556         xfs_buf_t       *bp;            /* buffer pointer (return value) */
557         xfs_daddr_t             d;              /* real disk block address */
558
559         ASSERT(fsbno != NULLFSBLOCK);
560         d = XFS_FSB_TO_DADDR(mp, fsbno);
561         bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
562         ASSERT(!xfs_buf_geterror(bp));
563         return bp;
564 }
565
566 /*
567  * Get a buffer for the block, return it with no data read.
568  * Short-form addressing.
569  */
570 xfs_buf_t *                             /* buffer for agno/agbno */
571 xfs_btree_get_bufs(
572         xfs_mount_t     *mp,            /* file system mount point */
573         xfs_trans_t     *tp,            /* transaction pointer */
574         xfs_agnumber_t  agno,           /* allocation group number */
575         xfs_agblock_t   agbno,          /* allocation group block number */
576         uint            lock)           /* lock flags for get_buf */
577 {
578         xfs_buf_t       *bp;            /* buffer pointer (return value) */
579         xfs_daddr_t             d;              /* real disk block address */
580
581         ASSERT(agno != NULLAGNUMBER);
582         ASSERT(agbno != NULLAGBLOCK);
583         d = XFS_AGB_TO_DADDR(mp, agno, agbno);
584         bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
585         ASSERT(!xfs_buf_geterror(bp));
586         return bp;
587 }
588
589 /*
590  * Check for the cursor referring to the last block at the given level.
591  */
592 int                                     /* 1=is last block, 0=not last block */
593 xfs_btree_islastblock(
594         xfs_btree_cur_t         *cur,   /* btree cursor */
595         int                     level)  /* level to check */
596 {
597         struct xfs_btree_block  *block; /* generic btree block pointer */
598         xfs_buf_t               *bp;    /* buffer containing block */
599
600         block = xfs_btree_get_block(cur, level, &bp);
601         xfs_btree_check_block(cur, block, level, bp);
602         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
603                 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
604         else
605                 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
606 }
607
608 /*
609  * Change the cursor to point to the first record at the given level.
610  * Other levels are unaffected.
611  */
612 STATIC int                              /* success=1, failure=0 */
613 xfs_btree_firstrec(
614         xfs_btree_cur_t         *cur,   /* btree cursor */
615         int                     level)  /* level to change */
616 {
617         struct xfs_btree_block  *block; /* generic btree block pointer */
618         xfs_buf_t               *bp;    /* buffer containing block */
619
620         /*
621          * Get the block pointer for this level.
622          */
623         block = xfs_btree_get_block(cur, level, &bp);
624         xfs_btree_check_block(cur, block, level, bp);
625         /*
626          * It's empty, there is no such record.
627          */
628         if (!block->bb_numrecs)
629                 return 0;
630         /*
631          * Set the ptr value to 1, that's the first record/key.
632          */
633         cur->bc_ptrs[level] = 1;
634         return 1;
635 }
636
637 /*
638  * Change the cursor to point to the last record in the current block
639  * at the given level.  Other levels are unaffected.
640  */
641 STATIC int                              /* success=1, failure=0 */
642 xfs_btree_lastrec(
643         xfs_btree_cur_t         *cur,   /* btree cursor */
644         int                     level)  /* level to change */
645 {
646         struct xfs_btree_block  *block; /* generic btree block pointer */
647         xfs_buf_t               *bp;    /* buffer containing block */
648
649         /*
650          * Get the block pointer for this level.
651          */
652         block = xfs_btree_get_block(cur, level, &bp);
653         xfs_btree_check_block(cur, block, level, bp);
654         /*
655          * It's empty, there is no such record.
656          */
657         if (!block->bb_numrecs)
658                 return 0;
659         /*
660          * Set the ptr value to numrecs, that's the last record/key.
661          */
662         cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
663         return 1;
664 }
665
666 /*
667  * Compute first and last byte offsets for the fields given.
668  * Interprets the offsets table, which contains struct field offsets.
669  */
670 void
671 xfs_btree_offsets(
672         __int64_t       fields,         /* bitmask of fields */
673         const short     *offsets,       /* table of field offsets */
674         int             nbits,          /* number of bits to inspect */
675         int             *first,         /* output: first byte offset */
676         int             *last)          /* output: last byte offset */
677 {
678         int             i;              /* current bit number */
679         __int64_t       imask;          /* mask for current bit number */
680
681         ASSERT(fields != 0);
682         /*
683          * Find the lowest bit, so the first byte offset.
684          */
685         for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
686                 if (imask & fields) {
687                         *first = offsets[i];
688                         break;
689                 }
690         }
691         /*
692          * Find the highest bit, so the last byte offset.
693          */
694         for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
695                 if (imask & fields) {
696                         *last = offsets[i + 1] - 1;
697                         break;
698                 }
699         }
700 }
701
702 /*
703  * Get a buffer for the block, return it read in.
704  * Long-form addressing.
705  */
706 int
707 xfs_btree_read_bufl(
708         struct xfs_mount        *mp,            /* file system mount point */
709         struct xfs_trans        *tp,            /* transaction pointer */
710         xfs_fsblock_t           fsbno,          /* file system block number */
711         uint                    lock,           /* lock flags for read_buf */
712         struct xfs_buf          **bpp,          /* buffer for fsbno */
713         int                     refval,         /* ref count value for buffer */
714         const struct xfs_buf_ops *ops)
715 {
716         struct xfs_buf          *bp;            /* return value */
717         xfs_daddr_t             d;              /* real disk block address */
718         int                     error;
719
720         ASSERT(fsbno != NULLFSBLOCK);
721         d = XFS_FSB_TO_DADDR(mp, fsbno);
722         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
723                                    mp->m_bsize, lock, &bp, ops);
724         if (error)
725                 return error;
726         ASSERT(!xfs_buf_geterror(bp));
727         if (bp)
728                 xfs_buf_set_ref(bp, refval);
729         *bpp = bp;
730         return 0;
731 }
732
733 /*
734  * Read-ahead the block, don't wait for it, don't return a buffer.
735  * Long-form addressing.
736  */
737 /* ARGSUSED */
738 void
739 xfs_btree_reada_bufl(
740         struct xfs_mount        *mp,            /* file system mount point */
741         xfs_fsblock_t           fsbno,          /* file system block number */
742         xfs_extlen_t            count,          /* count of filesystem blocks */
743         const struct xfs_buf_ops *ops)
744 {
745         xfs_daddr_t             d;
746
747         ASSERT(fsbno != NULLFSBLOCK);
748         d = XFS_FSB_TO_DADDR(mp, fsbno);
749         xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
750 }
751
752 /*
753  * Read-ahead the block, don't wait for it, don't return a buffer.
754  * Short-form addressing.
755  */
756 /* ARGSUSED */
757 void
758 xfs_btree_reada_bufs(
759         struct xfs_mount        *mp,            /* file system mount point */
760         xfs_agnumber_t          agno,           /* allocation group number */
761         xfs_agblock_t           agbno,          /* allocation group block number */
762         xfs_extlen_t            count,          /* count of filesystem blocks */
763         const struct xfs_buf_ops *ops)
764 {
765         xfs_daddr_t             d;
766
767         ASSERT(agno != NULLAGNUMBER);
768         ASSERT(agbno != NULLAGBLOCK);
769         d = XFS_AGB_TO_DADDR(mp, agno, agbno);
770         xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
771 }
772
773 STATIC int
774 xfs_btree_readahead_lblock(
775         struct xfs_btree_cur    *cur,
776         int                     lr,
777         struct xfs_btree_block  *block)
778 {
779         int                     rval = 0;
780         xfs_dfsbno_t            left = be64_to_cpu(block->bb_u.l.bb_leftsib);
781         xfs_dfsbno_t            right = be64_to_cpu(block->bb_u.l.bb_rightsib);
782
783         if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
784                 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
785                                      cur->bc_ops->buf_ops);
786                 rval++;
787         }
788
789         if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
790                 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
791                                      cur->bc_ops->buf_ops);
792                 rval++;
793         }
794
795         return rval;
796 }
797
798 STATIC int
799 xfs_btree_readahead_sblock(
800         struct xfs_btree_cur    *cur,
801         int                     lr,
802         struct xfs_btree_block *block)
803 {
804         int                     rval = 0;
805         xfs_agblock_t           left = be32_to_cpu(block->bb_u.s.bb_leftsib);
806         xfs_agblock_t           right = be32_to_cpu(block->bb_u.s.bb_rightsib);
807
808
809         if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
810                 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
811                                      left, 1, cur->bc_ops->buf_ops);
812                 rval++;
813         }
814
815         if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
816                 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
817                                      right, 1, cur->bc_ops->buf_ops);
818                 rval++;
819         }
820
821         return rval;
822 }
823
824 /*
825  * Read-ahead btree blocks, at the given level.
826  * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
827  */
828 STATIC int
829 xfs_btree_readahead(
830         struct xfs_btree_cur    *cur,           /* btree cursor */
831         int                     lev,            /* level in btree */
832         int                     lr)             /* left/right bits */
833 {
834         struct xfs_btree_block  *block;
835
836         /*
837          * No readahead needed if we are at the root level and the
838          * btree root is stored in the inode.
839          */
840         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
841             (lev == cur->bc_nlevels - 1))
842                 return 0;
843
844         if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
845                 return 0;
846
847         cur->bc_ra[lev] |= lr;
848         block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
849
850         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
851                 return xfs_btree_readahead_lblock(cur, lr, block);
852         return xfs_btree_readahead_sblock(cur, lr, block);
853 }
854
855 STATIC xfs_daddr_t
856 xfs_btree_ptr_to_daddr(
857         struct xfs_btree_cur    *cur,
858         union xfs_btree_ptr     *ptr)
859 {
860         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
861                 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
862
863                 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
864         } else {
865                 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
866                 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
867
868                 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
869                                         be32_to_cpu(ptr->s));
870         }
871 }
872
873 /*
874  * Readahead @count btree blocks at the given @ptr location.
875  *
876  * We don't need to care about long or short form btrees here as we have a
877  * method of converting the ptr directly to a daddr available to us.
878  */
879 STATIC void
880 xfs_btree_readahead_ptr(
881         struct xfs_btree_cur    *cur,
882         union xfs_btree_ptr     *ptr,
883         xfs_extlen_t            count)
884 {
885         xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
886                           xfs_btree_ptr_to_daddr(cur, ptr),
887                           cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
888 }
889
890 /*
891  * Set the buffer for level "lev" in the cursor to bp, releasing
892  * any previous buffer.
893  */
894 STATIC void
895 xfs_btree_setbuf(
896         xfs_btree_cur_t         *cur,   /* btree cursor */
897         int                     lev,    /* level in btree */
898         xfs_buf_t               *bp)    /* new buffer to set */
899 {
900         struct xfs_btree_block  *b;     /* btree block */
901
902         if (cur->bc_bufs[lev])
903                 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
904         cur->bc_bufs[lev] = bp;
905         cur->bc_ra[lev] = 0;
906
907         b = XFS_BUF_TO_BLOCK(bp);
908         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
909                 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
910                         cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
911                 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
912                         cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
913         } else {
914                 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
915                         cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
916                 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
917                         cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
918         }
919 }
920
921 STATIC int
922 xfs_btree_ptr_is_null(
923         struct xfs_btree_cur    *cur,
924         union xfs_btree_ptr     *ptr)
925 {
926         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
927                 return ptr->l == cpu_to_be64(NULLDFSBNO);
928         else
929                 return ptr->s == cpu_to_be32(NULLAGBLOCK);
930 }
931
932 STATIC void
933 xfs_btree_set_ptr_null(
934         struct xfs_btree_cur    *cur,
935         union xfs_btree_ptr     *ptr)
936 {
937         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
938                 ptr->l = cpu_to_be64(NULLDFSBNO);
939         else
940                 ptr->s = cpu_to_be32(NULLAGBLOCK);
941 }
942
943 /*
944  * Get/set/init sibling pointers
945  */
946 STATIC void
947 xfs_btree_get_sibling(
948         struct xfs_btree_cur    *cur,
949         struct xfs_btree_block  *block,
950         union xfs_btree_ptr     *ptr,
951         int                     lr)
952 {
953         ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
954
955         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
956                 if (lr == XFS_BB_RIGHTSIB)
957                         ptr->l = block->bb_u.l.bb_rightsib;
958                 else
959                         ptr->l = block->bb_u.l.bb_leftsib;
960         } else {
961                 if (lr == XFS_BB_RIGHTSIB)
962                         ptr->s = block->bb_u.s.bb_rightsib;
963                 else
964                         ptr->s = block->bb_u.s.bb_leftsib;
965         }
966 }
967
968 STATIC void
969 xfs_btree_set_sibling(
970         struct xfs_btree_cur    *cur,
971         struct xfs_btree_block  *block,
972         union xfs_btree_ptr     *ptr,
973         int                     lr)
974 {
975         ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
976
977         if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
978                 if (lr == XFS_BB_RIGHTSIB)
979                         block->bb_u.l.bb_rightsib = ptr->l;
980                 else
981                         block->bb_u.l.bb_leftsib = ptr->l;
982         } else {
983                 if (lr == XFS_BB_RIGHTSIB)
984                         block->bb_u.s.bb_rightsib = ptr->s;
985                 else
986                         block->bb_u.s.bb_leftsib = ptr->s;
987         }
988 }
989
990 void
991 xfs_btree_init_block_int(
992         struct xfs_mount        *mp,
993         struct xfs_btree_block  *buf,
994         xfs_daddr_t             blkno,
995         __u32                   magic,
996         __u16                   level,
997         __u16                   numrecs,
998         __u64                   owner,
999         unsigned int            flags)
1000 {
1001         buf->bb_magic = cpu_to_be32(magic);
1002         buf->bb_level = cpu_to_be16(level);
1003         buf->bb_numrecs = cpu_to_be16(numrecs);
1004
1005         if (flags & XFS_BTREE_LONG_PTRS) {
1006                 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1007                 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1008                 if (flags & XFS_BTREE_CRC_BLOCKS) {
1009                         buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1010                         buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1011                         uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1012                         buf->bb_u.l.bb_pad = 0;
1013                         buf->bb_u.l.bb_lsn = 0;
1014                 }
1015         } else {
1016                 /* owner is a 32 bit value on short blocks */
1017                 __u32 __owner = (__u32)owner;
1018
1019                 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1020                 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1021                 if (flags & XFS_BTREE_CRC_BLOCKS) {
1022                         buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1023                         buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1024                         uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
1025                         buf->bb_u.s.bb_lsn = 0;
1026                 }
1027         }
1028 }
1029
1030 void
1031 xfs_btree_init_block(
1032         struct xfs_mount *mp,
1033         struct xfs_buf  *bp,
1034         __u32           magic,
1035         __u16           level,
1036         __u16           numrecs,
1037         __u64           owner,
1038         unsigned int    flags)
1039 {
1040         xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1041                                  magic, level, numrecs, owner, flags);
1042 }
1043
1044 STATIC void
1045 xfs_btree_init_block_cur(
1046         struct xfs_btree_cur    *cur,
1047         struct xfs_buf          *bp,
1048         int                     level,
1049         int                     numrecs)
1050 {
1051         __u64 owner;
1052
1053         /*
1054          * we can pull the owner from the cursor right now as the different
1055          * owners align directly with the pointer size of the btree. This may
1056          * change in future, but is safe for current users of the generic btree
1057          * code.
1058          */
1059         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1060                 owner = cur->bc_private.b.ip->i_ino;
1061         else
1062                 owner = cur->bc_private.a.agno;
1063
1064         xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1065                                  xfs_btree_magic(cur), level, numrecs,
1066                                  owner, cur->bc_flags);
1067 }
1068
1069 /*
1070  * Return true if ptr is the last record in the btree and
1071  * we need to track updates to this record.  The decision
1072  * will be further refined in the update_lastrec method.
1073  */
1074 STATIC int
1075 xfs_btree_is_lastrec(
1076         struct xfs_btree_cur    *cur,
1077         struct xfs_btree_block  *block,
1078         int                     level)
1079 {
1080         union xfs_btree_ptr     ptr;
1081
1082         if (level > 0)
1083                 return 0;
1084         if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1085                 return 0;
1086
1087         xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1088         if (!xfs_btree_ptr_is_null(cur, &ptr))
1089                 return 0;
1090         return 1;
1091 }
1092
1093 STATIC void
1094 xfs_btree_buf_to_ptr(
1095         struct xfs_btree_cur    *cur,
1096         struct xfs_buf          *bp,
1097         union xfs_btree_ptr     *ptr)
1098 {
1099         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1100                 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1101                                         XFS_BUF_ADDR(bp)));
1102         else {
1103                 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
1104                                         XFS_BUF_ADDR(bp)));
1105         }
1106 }
1107
1108 STATIC void
1109 xfs_btree_set_refs(
1110         struct xfs_btree_cur    *cur,
1111         struct xfs_buf          *bp)
1112 {
1113         switch (cur->bc_btnum) {
1114         case XFS_BTNUM_BNO:
1115         case XFS_BTNUM_CNT:
1116                 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
1117                 break;
1118         case XFS_BTNUM_INO:
1119         case XFS_BTNUM_FINO:
1120                 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
1121                 break;
1122         case XFS_BTNUM_BMAP:
1123                 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
1124                 break;
1125         default:
1126                 ASSERT(0);
1127         }
1128 }
1129
1130 STATIC int
1131 xfs_btree_get_buf_block(
1132         struct xfs_btree_cur    *cur,
1133         union xfs_btree_ptr     *ptr,
1134         int                     flags,
1135         struct xfs_btree_block  **block,
1136         struct xfs_buf          **bpp)
1137 {
1138         struct xfs_mount        *mp = cur->bc_mp;
1139         xfs_daddr_t             d;
1140
1141         /* need to sort out how callers deal with failures first */
1142         ASSERT(!(flags & XBF_TRYLOCK));
1143
1144         d = xfs_btree_ptr_to_daddr(cur, ptr);
1145         *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1146                                  mp->m_bsize, flags);
1147
1148         if (!*bpp)
1149                 return ENOMEM;
1150
1151         (*bpp)->b_ops = cur->bc_ops->buf_ops;
1152         *block = XFS_BUF_TO_BLOCK(*bpp);
1153         return 0;
1154 }
1155
1156 /*
1157  * Read in the buffer at the given ptr and return the buffer and
1158  * the block pointer within the buffer.
1159  */
1160 STATIC int
1161 xfs_btree_read_buf_block(
1162         struct xfs_btree_cur    *cur,
1163         union xfs_btree_ptr     *ptr,
1164         int                     flags,
1165         struct xfs_btree_block  **block,
1166         struct xfs_buf          **bpp)
1167 {
1168         struct xfs_mount        *mp = cur->bc_mp;
1169         xfs_daddr_t             d;
1170         int                     error;
1171
1172         /* need to sort out how callers deal with failures first */
1173         ASSERT(!(flags & XBF_TRYLOCK));
1174
1175         d = xfs_btree_ptr_to_daddr(cur, ptr);
1176         error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1177                                    mp->m_bsize, flags, bpp,
1178                                    cur->bc_ops->buf_ops);
1179         if (error)
1180                 return error;
1181
1182         ASSERT(!xfs_buf_geterror(*bpp));
1183         xfs_btree_set_refs(cur, *bpp);
1184         *block = XFS_BUF_TO_BLOCK(*bpp);
1185         return 0;
1186 }
1187
1188 /*
1189  * Copy keys from one btree block to another.
1190  */
1191 STATIC void
1192 xfs_btree_copy_keys(
1193         struct xfs_btree_cur    *cur,
1194         union xfs_btree_key     *dst_key,
1195         union xfs_btree_key     *src_key,
1196         int                     numkeys)
1197 {
1198         ASSERT(numkeys >= 0);
1199         memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1200 }
1201
1202 /*
1203  * Copy records from one btree block to another.
1204  */
1205 STATIC void
1206 xfs_btree_copy_recs(
1207         struct xfs_btree_cur    *cur,
1208         union xfs_btree_rec     *dst_rec,
1209         union xfs_btree_rec     *src_rec,
1210         int                     numrecs)
1211 {
1212         ASSERT(numrecs >= 0);
1213         memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1214 }
1215
1216 /*
1217  * Copy block pointers from one btree block to another.
1218  */
1219 STATIC void
1220 xfs_btree_copy_ptrs(
1221         struct xfs_btree_cur    *cur,
1222         union xfs_btree_ptr     *dst_ptr,
1223         union xfs_btree_ptr     *src_ptr,
1224         int                     numptrs)
1225 {
1226         ASSERT(numptrs >= 0);
1227         memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1228 }
1229
1230 /*
1231  * Shift keys one index left/right inside a single btree block.
1232  */
1233 STATIC void
1234 xfs_btree_shift_keys(
1235         struct xfs_btree_cur    *cur,
1236         union xfs_btree_key     *key,
1237         int                     dir,
1238         int                     numkeys)
1239 {
1240         char                    *dst_key;
1241
1242         ASSERT(numkeys >= 0);
1243         ASSERT(dir == 1 || dir == -1);
1244
1245         dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1246         memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1247 }
1248
1249 /*
1250  * Shift records one index left/right inside a single btree block.
1251  */
1252 STATIC void
1253 xfs_btree_shift_recs(
1254         struct xfs_btree_cur    *cur,
1255         union xfs_btree_rec     *rec,
1256         int                     dir,
1257         int                     numrecs)
1258 {
1259         char                    *dst_rec;
1260
1261         ASSERT(numrecs >= 0);
1262         ASSERT(dir == 1 || dir == -1);
1263
1264         dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1265         memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1266 }
1267
1268 /*
1269  * Shift block pointers one index left/right inside a single btree block.
1270  */
1271 STATIC void
1272 xfs_btree_shift_ptrs(
1273         struct xfs_btree_cur    *cur,
1274         union xfs_btree_ptr     *ptr,
1275         int                     dir,
1276         int                     numptrs)
1277 {
1278         char                    *dst_ptr;
1279
1280         ASSERT(numptrs >= 0);
1281         ASSERT(dir == 1 || dir == -1);
1282
1283         dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1284         memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1285 }
1286
1287 /*
1288  * Log key values from the btree block.
1289  */
1290 STATIC void
1291 xfs_btree_log_keys(
1292         struct xfs_btree_cur    *cur,
1293         struct xfs_buf          *bp,
1294         int                     first,
1295         int                     last)
1296 {
1297         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1298         XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1299
1300         if (bp) {
1301                 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1302                 xfs_trans_log_buf(cur->bc_tp, bp,
1303                                   xfs_btree_key_offset(cur, first),
1304                                   xfs_btree_key_offset(cur, last + 1) - 1);
1305         } else {
1306                 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1307                                 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1308         }
1309
1310         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1311 }
1312
1313 /*
1314  * Log record values from the btree block.
1315  */
1316 void
1317 xfs_btree_log_recs(
1318         struct xfs_btree_cur    *cur,
1319         struct xfs_buf          *bp,
1320         int                     first,
1321         int                     last)
1322 {
1323         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1324         XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1325
1326         xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1327         xfs_trans_log_buf(cur->bc_tp, bp,
1328                           xfs_btree_rec_offset(cur, first),
1329                           xfs_btree_rec_offset(cur, last + 1) - 1);
1330
1331         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1332 }
1333
1334 /*
1335  * Log block pointer fields from a btree block (nonleaf).
1336  */
1337 STATIC void
1338 xfs_btree_log_ptrs(
1339         struct xfs_btree_cur    *cur,   /* btree cursor */
1340         struct xfs_buf          *bp,    /* buffer containing btree block */
1341         int                     first,  /* index of first pointer to log */
1342         int                     last)   /* index of last pointer to log */
1343 {
1344         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1345         XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1346
1347         if (bp) {
1348                 struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
1349                 int                     level = xfs_btree_get_level(block);
1350
1351                 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1352                 xfs_trans_log_buf(cur->bc_tp, bp,
1353                                 xfs_btree_ptr_offset(cur, first, level),
1354                                 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1355         } else {
1356                 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1357                         xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1358         }
1359
1360         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1361 }
1362
1363 /*
1364  * Log fields from a btree block header.
1365  */
1366 void
1367 xfs_btree_log_block(
1368         struct xfs_btree_cur    *cur,   /* btree cursor */
1369         struct xfs_buf          *bp,    /* buffer containing btree block */
1370         int                     fields) /* mask of fields: XFS_BB_... */
1371 {
1372         int                     first;  /* first byte offset logged */
1373         int                     last;   /* last byte offset logged */
1374         static const short      soffsets[] = {  /* table of offsets (short) */
1375                 offsetof(struct xfs_btree_block, bb_magic),
1376                 offsetof(struct xfs_btree_block, bb_level),
1377                 offsetof(struct xfs_btree_block, bb_numrecs),
1378                 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1379                 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
1380                 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1381                 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1382                 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1383                 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1384                 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1385                 XFS_BTREE_SBLOCK_CRC_LEN
1386         };
1387         static const short      loffsets[] = {  /* table of offsets (long) */
1388                 offsetof(struct xfs_btree_block, bb_magic),
1389                 offsetof(struct xfs_btree_block, bb_level),
1390                 offsetof(struct xfs_btree_block, bb_numrecs),
1391                 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1392                 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
1393                 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1394                 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1395                 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1396                 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1397                 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1398                 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1399                 XFS_BTREE_LBLOCK_CRC_LEN
1400         };
1401
1402         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1403         XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1404
1405         if (bp) {
1406                 int nbits;
1407
1408                 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1409                         /*
1410                          * We don't log the CRC when updating a btree
1411                          * block but instead recreate it during log
1412                          * recovery.  As the log buffers have checksums
1413                          * of their own this is safe and avoids logging a crc
1414                          * update in a lot of places.
1415                          */
1416                         if (fields == XFS_BB_ALL_BITS)
1417                                 fields = XFS_BB_ALL_BITS_CRC;
1418                         nbits = XFS_BB_NUM_BITS_CRC;
1419                 } else {
1420                         nbits = XFS_BB_NUM_BITS;
1421                 }
1422                 xfs_btree_offsets(fields,
1423                                   (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1424                                         loffsets : soffsets,
1425                                   nbits, &first, &last);
1426                 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
1427                 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1428         } else {
1429                 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1430                         xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1431         }
1432
1433         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1434 }
1435
1436 /*
1437  * Increment cursor by one record at the level.
1438  * For nonzero levels the leaf-ward information is untouched.
1439  */
1440 int                                             /* error */
1441 xfs_btree_increment(
1442         struct xfs_btree_cur    *cur,
1443         int                     level,
1444         int                     *stat)          /* success/failure */
1445 {
1446         struct xfs_btree_block  *block;
1447         union xfs_btree_ptr     ptr;
1448         struct xfs_buf          *bp;
1449         int                     error;          /* error return value */
1450         int                     lev;
1451
1452         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1453         XFS_BTREE_TRACE_ARGI(cur, level);
1454
1455         ASSERT(level < cur->bc_nlevels);
1456
1457         /* Read-ahead to the right at this level. */
1458         xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1459
1460         /* Get a pointer to the btree block. */
1461         block = xfs_btree_get_block(cur, level, &bp);
1462
1463 #ifdef DEBUG
1464         error = xfs_btree_check_block(cur, block, level, bp);
1465         if (error)
1466                 goto error0;
1467 #endif
1468
1469         /* We're done if we remain in the block after the increment. */
1470         if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1471                 goto out1;
1472
1473         /* Fail if we just went off the right edge of the tree. */
1474         xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1475         if (xfs_btree_ptr_is_null(cur, &ptr))
1476                 goto out0;
1477
1478         XFS_BTREE_STATS_INC(cur, increment);
1479
1480         /*
1481          * March up the tree incrementing pointers.
1482          * Stop when we don't go off the right edge of a block.
1483          */
1484         for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1485                 block = xfs_btree_get_block(cur, lev, &bp);
1486
1487 #ifdef DEBUG
1488                 error = xfs_btree_check_block(cur, block, lev, bp);
1489                 if (error)
1490                         goto error0;
1491 #endif
1492
1493                 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1494                         break;
1495
1496                 /* Read-ahead the right block for the next loop. */
1497                 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1498         }
1499
1500         /*
1501          * If we went off the root then we are either seriously
1502          * confused or have the tree root in an inode.
1503          */
1504         if (lev == cur->bc_nlevels) {
1505                 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1506                         goto out0;
1507                 ASSERT(0);
1508                 error = EFSCORRUPTED;
1509                 goto error0;
1510         }
1511         ASSERT(lev < cur->bc_nlevels);
1512
1513         /*
1514          * Now walk back down the tree, fixing up the cursor's buffer
1515          * pointers and key numbers.
1516          */
1517         for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1518                 union xfs_btree_ptr     *ptrp;
1519
1520                 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1521                 --lev;
1522                 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1523                 if (error)
1524                         goto error0;
1525
1526                 xfs_btree_setbuf(cur, lev, bp);
1527                 cur->bc_ptrs[lev] = 1;
1528         }
1529 out1:
1530         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1531         *stat = 1;
1532         return 0;
1533
1534 out0:
1535         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1536         *stat = 0;
1537         return 0;
1538
1539 error0:
1540         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1541         return error;
1542 }
1543
1544 /*
1545  * Decrement cursor by one record at the level.
1546  * For nonzero levels the leaf-ward information is untouched.
1547  */
1548 int                                             /* error */
1549 xfs_btree_decrement(
1550         struct xfs_btree_cur    *cur,
1551         int                     level,
1552         int                     *stat)          /* success/failure */
1553 {
1554         struct xfs_btree_block  *block;
1555         xfs_buf_t               *bp;
1556         int                     error;          /* error return value */
1557         int                     lev;
1558         union xfs_btree_ptr     ptr;
1559
1560         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1561         XFS_BTREE_TRACE_ARGI(cur, level);
1562
1563         ASSERT(level < cur->bc_nlevels);
1564
1565         /* Read-ahead to the left at this level. */
1566         xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1567
1568         /* We're done if we remain in the block after the decrement. */
1569         if (--cur->bc_ptrs[level] > 0)
1570                 goto out1;
1571
1572         /* Get a pointer to the btree block. */
1573         block = xfs_btree_get_block(cur, level, &bp);
1574
1575 #ifdef DEBUG
1576         error = xfs_btree_check_block(cur, block, level, bp);
1577         if (error)
1578                 goto error0;
1579 #endif
1580
1581         /* Fail if we just went off the left edge of the tree. */
1582         xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1583         if (xfs_btree_ptr_is_null(cur, &ptr))
1584                 goto out0;
1585
1586         XFS_BTREE_STATS_INC(cur, decrement);
1587
1588         /*
1589          * March up the tree decrementing pointers.
1590          * Stop when we don't go off the left edge of a block.
1591          */
1592         for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1593                 if (--cur->bc_ptrs[lev] > 0)
1594                         break;
1595                 /* Read-ahead the left block for the next loop. */
1596                 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1597         }
1598
1599         /*
1600          * If we went off the root then we are seriously confused.
1601          * or the root of the tree is in an inode.
1602          */
1603         if (lev == cur->bc_nlevels) {
1604                 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1605                         goto out0;
1606                 ASSERT(0);
1607                 error = EFSCORRUPTED;
1608                 goto error0;
1609         }
1610         ASSERT(lev < cur->bc_nlevels);
1611
1612         /*
1613          * Now walk back down the tree, fixing up the cursor's buffer
1614          * pointers and key numbers.
1615          */
1616         for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1617                 union xfs_btree_ptr     *ptrp;
1618
1619                 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
1620                 --lev;
1621                 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
1622                 if (error)
1623                         goto error0;
1624                 xfs_btree_setbuf(cur, lev, bp);
1625                 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1626         }
1627 out1:
1628         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1629         *stat = 1;
1630         return 0;
1631
1632 out0:
1633         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1634         *stat = 0;
1635         return 0;
1636
1637 error0:
1638         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1639         return error;
1640 }
1641
1642 STATIC int
1643 xfs_btree_lookup_get_block(
1644         struct xfs_btree_cur    *cur,   /* btree cursor */
1645         int                     level,  /* level in the btree */
1646         union xfs_btree_ptr     *pp,    /* ptr to btree block */
1647         struct xfs_btree_block  **blkp) /* return btree block */
1648 {
1649         struct xfs_buf          *bp;    /* buffer pointer for btree block */
1650         int                     error = 0;
1651
1652         /* special case the root block if in an inode */
1653         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1654             (level == cur->bc_nlevels - 1)) {
1655                 *blkp = xfs_btree_get_iroot(cur);
1656                 return 0;
1657         }
1658
1659         /*
1660          * If the old buffer at this level for the disk address we are
1661          * looking for re-use it.
1662          *
1663          * Otherwise throw it away and get a new one.
1664          */
1665         bp = cur->bc_bufs[level];
1666         if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1667                 *blkp = XFS_BUF_TO_BLOCK(bp);
1668                 return 0;
1669         }
1670
1671         error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
1672         if (error)
1673                 return error;
1674
1675         xfs_btree_setbuf(cur, level, bp);
1676         return 0;
1677 }
1678
1679 /*
1680  * Get current search key.  For level 0 we don't actually have a key
1681  * structure so we make one up from the record.  For all other levels
1682  * we just return the right key.
1683  */
1684 STATIC union xfs_btree_key *
1685 xfs_lookup_get_search_key(
1686         struct xfs_btree_cur    *cur,
1687         int                     level,
1688         int                     keyno,
1689         struct xfs_btree_block  *block,
1690         union xfs_btree_key     *kp)
1691 {
1692         if (level == 0) {
1693                 cur->bc_ops->init_key_from_rec(kp,
1694                                 xfs_btree_rec_addr(cur, keyno, block));
1695                 return kp;
1696         }
1697
1698         return xfs_btree_key_addr(cur, keyno, block);
1699 }
1700
1701 /*
1702  * Lookup the record.  The cursor is made to point to it, based on dir.
1703  * stat is set to 0 if can't find any such record, 1 for success.
1704  */
1705 int                                     /* error */
1706 xfs_btree_lookup(
1707         struct xfs_btree_cur    *cur,   /* btree cursor */
1708         xfs_lookup_t            dir,    /* <=, ==, or >= */
1709         int                     *stat)  /* success/failure */
1710 {
1711         struct xfs_btree_block  *block; /* current btree block */
1712         __int64_t               diff;   /* difference for the current key */
1713         int                     error;  /* error return value */
1714         int                     keyno;  /* current key number */
1715         int                     level;  /* level in the btree */
1716         union xfs_btree_ptr     *pp;    /* ptr to btree block */
1717         union xfs_btree_ptr     ptr;    /* ptr to btree block */
1718
1719         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1720         XFS_BTREE_TRACE_ARGI(cur, dir);
1721
1722         XFS_BTREE_STATS_INC(cur, lookup);
1723
1724         block = NULL;
1725         keyno = 0;
1726
1727         /* initialise start pointer from cursor */
1728         cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1729         pp = &ptr;
1730
1731         /*
1732          * Iterate over each level in the btree, starting at the root.
1733          * For each level above the leaves, find the key we need, based
1734          * on the lookup record, then follow the corresponding block
1735          * pointer down to the next level.
1736          */
1737         for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1738                 /* Get the block we need to do the lookup on. */
1739                 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1740                 if (error)
1741                         goto error0;
1742
1743                 if (diff == 0) {
1744                         /*
1745                          * If we already had a key match at a higher level, we
1746                          * know we need to use the first entry in this block.
1747                          */
1748                         keyno = 1;
1749                 } else {
1750                         /* Otherwise search this block. Do a binary search. */
1751
1752                         int     high;   /* high entry number */
1753                         int     low;    /* low entry number */
1754
1755                         /* Set low and high entry numbers, 1-based. */
1756                         low = 1;
1757                         high = xfs_btree_get_numrecs(block);
1758                         if (!high) {
1759                                 /* Block is empty, must be an empty leaf. */
1760                                 ASSERT(level == 0 && cur->bc_nlevels == 1);
1761
1762                                 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1763                                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1764                                 *stat = 0;
1765                                 return 0;
1766                         }
1767
1768                         /* Binary search the block. */
1769                         while (low <= high) {
1770                                 union xfs_btree_key     key;
1771                                 union xfs_btree_key     *kp;
1772
1773                                 XFS_BTREE_STATS_INC(cur, compare);
1774
1775                                 /* keyno is average of low and high. */
1776                                 keyno = (low + high) >> 1;
1777
1778                                 /* Get current search key */
1779                                 kp = xfs_lookup_get_search_key(cur, level,
1780                                                 keyno, block, &key);
1781
1782                                 /*
1783                                  * Compute difference to get next direction:
1784                                  *  - less than, move right
1785                                  *  - greater than, move left
1786                                  *  - equal, we're done
1787                                  */
1788                                 diff = cur->bc_ops->key_diff(cur, kp);
1789                                 if (diff < 0)
1790                                         low = keyno + 1;
1791                                 else if (diff > 0)
1792                                         high = keyno - 1;
1793                                 else
1794                                         break;
1795                         }
1796                 }
1797
1798                 /*
1799                  * If there are more levels, set up for the next level
1800                  * by getting the block number and filling in the cursor.
1801                  */
1802                 if (level > 0) {
1803                         /*
1804                          * If we moved left, need the previous key number,
1805                          * unless there isn't one.
1806                          */
1807                         if (diff > 0 && --keyno < 1)
1808                                 keyno = 1;
1809                         pp = xfs_btree_ptr_addr(cur, keyno, block);
1810
1811 #ifdef DEBUG
1812                         error = xfs_btree_check_ptr(cur, pp, 0, level);
1813                         if (error)
1814                                 goto error0;
1815 #endif
1816                         cur->bc_ptrs[level] = keyno;
1817                 }
1818         }
1819
1820         /* Done with the search. See if we need to adjust the results. */
1821         if (dir != XFS_LOOKUP_LE && diff < 0) {
1822                 keyno++;
1823                 /*
1824                  * If ge search and we went off the end of the block, but it's
1825                  * not the last block, we're in the wrong block.
1826                  */
1827                 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1828                 if (dir == XFS_LOOKUP_GE &&
1829                     keyno > xfs_btree_get_numrecs(block) &&
1830                     !xfs_btree_ptr_is_null(cur, &ptr)) {
1831                         int     i;
1832
1833                         cur->bc_ptrs[0] = keyno;
1834                         error = xfs_btree_increment(cur, 0, &i);
1835                         if (error)
1836                                 goto error0;
1837                         XFS_WANT_CORRUPTED_RETURN(i == 1);
1838                         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1839                         *stat = 1;
1840                         return 0;
1841                 }
1842         } else if (dir == XFS_LOOKUP_LE && diff > 0)
1843                 keyno--;
1844         cur->bc_ptrs[0] = keyno;
1845
1846         /* Return if we succeeded or not. */
1847         if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1848                 *stat = 0;
1849         else if (dir != XFS_LOOKUP_EQ || diff == 0)
1850                 *stat = 1;
1851         else
1852                 *stat = 0;
1853         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1854         return 0;
1855
1856 error0:
1857         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1858         return error;
1859 }
1860
1861 /*
1862  * Update keys at all levels from here to the root along the cursor's path.
1863  */
1864 STATIC int
1865 xfs_btree_updkey(
1866         struct xfs_btree_cur    *cur,
1867         union xfs_btree_key     *keyp,
1868         int                     level)
1869 {
1870         struct xfs_btree_block  *block;
1871         struct xfs_buf          *bp;
1872         union xfs_btree_key     *kp;
1873         int                     ptr;
1874
1875         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1876         XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1877
1878         ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1879
1880         /*
1881          * Go up the tree from this level toward the root.
1882          * At each level, update the key value to the value input.
1883          * Stop when we reach a level where the cursor isn't pointing
1884          * at the first entry in the block.
1885          */
1886         for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1887 #ifdef DEBUG
1888                 int             error;
1889 #endif
1890                 block = xfs_btree_get_block(cur, level, &bp);
1891 #ifdef DEBUG
1892                 error = xfs_btree_check_block(cur, block, level, bp);
1893                 if (error) {
1894                         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1895                         return error;
1896                 }
1897 #endif
1898                 ptr = cur->bc_ptrs[level];
1899                 kp = xfs_btree_key_addr(cur, ptr, block);
1900                 xfs_btree_copy_keys(cur, kp, keyp, 1);
1901                 xfs_btree_log_keys(cur, bp, ptr, ptr);
1902         }
1903
1904         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1905         return 0;
1906 }
1907
1908 /*
1909  * Update the record referred to by cur to the value in the
1910  * given record. This either works (return 0) or gets an
1911  * EFSCORRUPTED error.
1912  */
1913 int
1914 xfs_btree_update(
1915         struct xfs_btree_cur    *cur,
1916         union xfs_btree_rec     *rec)
1917 {
1918         struct xfs_btree_block  *block;
1919         struct xfs_buf          *bp;
1920         int                     error;
1921         int                     ptr;
1922         union xfs_btree_rec     *rp;
1923
1924         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1925         XFS_BTREE_TRACE_ARGR(cur, rec);
1926
1927         /* Pick up the current block. */
1928         block = xfs_btree_get_block(cur, 0, &bp);
1929
1930 #ifdef DEBUG
1931         error = xfs_btree_check_block(cur, block, 0, bp);
1932         if (error)
1933                 goto error0;
1934 #endif
1935         /* Get the address of the rec to be updated. */
1936         ptr = cur->bc_ptrs[0];
1937         rp = xfs_btree_rec_addr(cur, ptr, block);
1938
1939         /* Fill in the new contents and log them. */
1940         xfs_btree_copy_recs(cur, rp, rec, 1);
1941         xfs_btree_log_recs(cur, bp, ptr, ptr);
1942
1943         /*
1944          * If we are tracking the last record in the tree and
1945          * we are at the far right edge of the tree, update it.
1946          */
1947         if (xfs_btree_is_lastrec(cur, block, 0)) {
1948                 cur->bc_ops->update_lastrec(cur, block, rec,
1949                                             ptr, LASTREC_UPDATE);
1950         }
1951
1952         /* Updating first rec in leaf. Pass new key value up to our parent. */
1953         if (ptr == 1) {
1954                 union xfs_btree_key     key;
1955
1956                 cur->bc_ops->init_key_from_rec(&key, rec);
1957                 error = xfs_btree_updkey(cur, &key, 1);
1958                 if (error)
1959                         goto error0;
1960         }
1961
1962         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1963         return 0;
1964
1965 error0:
1966         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1967         return error;
1968 }
1969
1970 /*
1971  * Move 1 record left from cur/level if possible.
1972  * Update cur to reflect the new path.
1973  */
1974 STATIC int                                      /* error */
1975 xfs_btree_lshift(
1976         struct xfs_btree_cur    *cur,
1977         int                     level,
1978         int                     *stat)          /* success/failure */
1979 {
1980         union xfs_btree_key     key;            /* btree key */
1981         struct xfs_buf          *lbp;           /* left buffer pointer */
1982         struct xfs_btree_block  *left;          /* left btree block */
1983         int                     lrecs;          /* left record count */
1984         struct xfs_buf          *rbp;           /* right buffer pointer */
1985         struct xfs_btree_block  *right;         /* right btree block */
1986         int                     rrecs;          /* right record count */
1987         union xfs_btree_ptr     lptr;           /* left btree pointer */
1988         union xfs_btree_key     *rkp = NULL;    /* right btree key */
1989         union xfs_btree_ptr     *rpp = NULL;    /* right address pointer */
1990         union xfs_btree_rec     *rrp = NULL;    /* right record pointer */
1991         int                     error;          /* error return value */
1992
1993         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1994         XFS_BTREE_TRACE_ARGI(cur, level);
1995
1996         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1997             level == cur->bc_nlevels - 1)
1998                 goto out0;
1999
2000         /* Set up variables for this block as "right". */
2001         right = xfs_btree_get_block(cur, level, &rbp);
2002
2003 #ifdef DEBUG
2004         error = xfs_btree_check_block(cur, right, level, rbp);
2005         if (error)
2006                 goto error0;
2007 #endif
2008
2009         /* If we've got no left sibling then we can't shift an entry left. */
2010         xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2011         if (xfs_btree_ptr_is_null(cur, &lptr))
2012                 goto out0;
2013
2014         /*
2015          * If the cursor entry is the one that would be moved, don't
2016          * do it... it's too complicated.
2017          */
2018         if (cur->bc_ptrs[level] <= 1)
2019                 goto out0;
2020
2021         /* Set up the left neighbor as "left". */
2022         error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2023         if (error)
2024                 goto error0;
2025
2026         /* If it's full, it can't take another entry. */
2027         lrecs = xfs_btree_get_numrecs(left);
2028         if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2029                 goto out0;
2030
2031         rrecs = xfs_btree_get_numrecs(right);
2032
2033         /*
2034          * We add one entry to the left side and remove one for the right side.
2035          * Account for it here, the changes will be updated on disk and logged
2036          * later.
2037          */
2038         lrecs++;
2039         rrecs--;
2040
2041         XFS_BTREE_STATS_INC(cur, lshift);
2042         XFS_BTREE_STATS_ADD(cur, moves, 1);
2043
2044         /*
2045          * If non-leaf, copy a key and a ptr to the left block.
2046          * Log the changes to the left block.
2047          */
2048         if (level > 0) {
2049                 /* It's a non-leaf.  Move keys and pointers. */
2050                 union xfs_btree_key     *lkp;   /* left btree key */
2051                 union xfs_btree_ptr     *lpp;   /* left address pointer */
2052
2053                 lkp = xfs_btree_key_addr(cur, lrecs, left);
2054                 rkp = xfs_btree_key_addr(cur, 1, right);
2055
2056                 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2057                 rpp = xfs_btree_ptr_addr(cur, 1, right);
2058 #ifdef DEBUG
2059                 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2060                 if (error)
2061                         goto error0;
2062 #endif
2063                 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2064                 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2065
2066                 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2067                 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2068
2069                 ASSERT(cur->bc_ops->keys_inorder(cur,
2070                         xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
2071         } else {
2072                 /* It's a leaf.  Move records.  */
2073                 union xfs_btree_rec     *lrp;   /* left record pointer */
2074
2075                 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2076                 rrp = xfs_btree_rec_addr(cur, 1, right);
2077
2078                 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2079                 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2080
2081                 ASSERT(cur->bc_ops->recs_inorder(cur,
2082                         xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
2083         }
2084
2085         xfs_btree_set_numrecs(left, lrecs);
2086         xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2087
2088         xfs_btree_set_numrecs(right, rrecs);
2089         xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2090
2091         /*
2092          * Slide the contents of right down one entry.
2093          */
2094         XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2095         if (level > 0) {
2096                 /* It's a nonleaf. operate on keys and ptrs */
2097 #ifdef DEBUG
2098                 int                     i;              /* loop index */
2099
2100                 for (i = 0; i < rrecs; i++) {
2101                         error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2102                         if (error)
2103                                 goto error0;
2104                 }
2105 #endif
2106                 xfs_btree_shift_keys(cur,
2107                                 xfs_btree_key_addr(cur, 2, right),
2108                                 -1, rrecs);
2109                 xfs_btree_shift_ptrs(cur,
2110                                 xfs_btree_ptr_addr(cur, 2, right),
2111                                 -1, rrecs);
2112
2113                 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2114                 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2115         } else {
2116                 /* It's a leaf. operate on records */
2117                 xfs_btree_shift_recs(cur,
2118                         xfs_btree_rec_addr(cur, 2, right),
2119                         -1, rrecs);
2120                 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2121
2122                 /*
2123                  * If it's the first record in the block, we'll need a key
2124                  * structure to pass up to the next level (updkey).
2125                  */
2126                 cur->bc_ops->init_key_from_rec(&key,
2127                         xfs_btree_rec_addr(cur, 1, right));
2128                 rkp = &key;
2129         }
2130
2131         /* Update the parent key values of right. */
2132         error = xfs_btree_updkey(cur, rkp, level + 1);
2133         if (error)
2134                 goto error0;
2135
2136         /* Slide the cursor value left one. */
2137         cur->bc_ptrs[level]--;
2138
2139         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2140         *stat = 1;
2141         return 0;
2142
2143 out0:
2144         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2145         *stat = 0;
2146         return 0;
2147
2148 error0:
2149         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2150         return error;
2151 }
2152
2153 /*
2154  * Move 1 record right from cur/level if possible.
2155  * Update cur to reflect the new path.
2156  */
2157 STATIC int                                      /* error */
2158 xfs_btree_rshift(
2159         struct xfs_btree_cur    *cur,
2160         int                     level,
2161         int                     *stat)          /* success/failure */
2162 {
2163         union xfs_btree_key     key;            /* btree key */
2164         struct xfs_buf          *lbp;           /* left buffer pointer */
2165         struct xfs_btree_block  *left;          /* left btree block */
2166         struct xfs_buf          *rbp;           /* right buffer pointer */
2167         struct xfs_btree_block  *right;         /* right btree block */
2168         struct xfs_btree_cur    *tcur;          /* temporary btree cursor */
2169         union xfs_btree_ptr     rptr;           /* right block pointer */
2170         union xfs_btree_key     *rkp;           /* right btree key */
2171         int                     rrecs;          /* right record count */
2172         int                     lrecs;          /* left record count */
2173         int                     error;          /* error return value */
2174         int                     i;              /* loop counter */
2175
2176         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2177         XFS_BTREE_TRACE_ARGI(cur, level);
2178
2179         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2180             (level == cur->bc_nlevels - 1))
2181                 goto out0;
2182
2183         /* Set up variables for this block as "left". */
2184         left = xfs_btree_get_block(cur, level, &lbp);
2185
2186 #ifdef DEBUG
2187         error = xfs_btree_check_block(cur, left, level, lbp);
2188         if (error)
2189                 goto error0;
2190 #endif
2191
2192         /* If we've got no right sibling then we can't shift an entry right. */
2193         xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2194         if (xfs_btree_ptr_is_null(cur, &rptr))
2195                 goto out0;
2196
2197         /*
2198          * If the cursor entry is the one that would be moved, don't
2199          * do it... it's too complicated.
2200          */
2201         lrecs = xfs_btree_get_numrecs(left);
2202         if (cur->bc_ptrs[level] >= lrecs)
2203                 goto out0;
2204
2205         /* Set up the right neighbor as "right". */
2206         error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2207         if (error)
2208                 goto error0;
2209
2210         /* If it's full, it can't take another entry. */
2211         rrecs = xfs_btree_get_numrecs(right);
2212         if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2213                 goto out0;
2214
2215         XFS_BTREE_STATS_INC(cur, rshift);
2216         XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2217
2218         /*
2219          * Make a hole at the start of the right neighbor block, then
2220          * copy the last left block entry to the hole.
2221          */
2222         if (level > 0) {
2223                 /* It's a nonleaf. make a hole in the keys and ptrs */
2224                 union xfs_btree_key     *lkp;
2225                 union xfs_btree_ptr     *lpp;
2226                 union xfs_btree_ptr     *rpp;
2227
2228                 lkp = xfs_btree_key_addr(cur, lrecs, left);
2229                 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2230                 rkp = xfs_btree_key_addr(cur, 1, right);
2231                 rpp = xfs_btree_ptr_addr(cur, 1, right);
2232
2233 #ifdef DEBUG
2234                 for (i = rrecs - 1; i >= 0; i--) {
2235                         error = xfs_btree_check_ptr(cur, rpp, i, level);
2236                         if (error)
2237                                 goto error0;
2238                 }
2239 #endif
2240
2241                 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2242                 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2243
2244 #ifdef DEBUG
2245                 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2246                 if (error)
2247                         goto error0;
2248 #endif
2249
2250                 /* Now put the new data in, and log it. */
2251                 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2252                 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2253
2254                 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2255                 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2256
2257                 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2258                         xfs_btree_key_addr(cur, 2, right)));
2259         } else {
2260                 /* It's a leaf. make a hole in the records */
2261                 union xfs_btree_rec     *lrp;
2262                 union xfs_btree_rec     *rrp;
2263
2264                 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2265                 rrp = xfs_btree_rec_addr(cur, 1, right);
2266
2267                 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2268
2269                 /* Now put the new data in, and log it. */
2270                 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2271                 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2272
2273                 cur->bc_ops->init_key_from_rec(&key, rrp);
2274                 rkp = &key;
2275
2276                 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2277                         xfs_btree_rec_addr(cur, 2, right)));
2278         }
2279
2280         /*
2281          * Decrement and log left's numrecs, bump and log right's numrecs.
2282          */
2283         xfs_btree_set_numrecs(left, --lrecs);
2284         xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2285
2286         xfs_btree_set_numrecs(right, ++rrecs);
2287         xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2288
2289         /*
2290          * Using a temporary cursor, update the parent key values of the
2291          * block on the right.
2292          */
2293         error = xfs_btree_dup_cursor(cur, &tcur);
2294         if (error)
2295                 goto error0;
2296         i = xfs_btree_lastrec(tcur, level);
2297         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2298
2299         error = xfs_btree_increment(tcur, level, &i);
2300         if (error)
2301                 goto error1;
2302
2303         error = xfs_btree_updkey(tcur, rkp, level + 1);
2304         if (error)
2305                 goto error1;
2306
2307         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2308
2309         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2310         *stat = 1;
2311         return 0;
2312
2313 out0:
2314         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2315         *stat = 0;
2316         return 0;
2317
2318 error0:
2319         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2320         return error;
2321
2322 error1:
2323         XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2324         xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2325         return error;
2326 }
2327
2328 /*
2329  * Split cur/level block in half.
2330  * Return new block number and the key to its first
2331  * record (to be inserted into parent).
2332  */
2333 STATIC int                                      /* error */
2334 xfs_btree_split(
2335         struct xfs_btree_cur    *cur,
2336         int                     level,
2337         union xfs_btree_ptr     *ptrp,
2338         union xfs_btree_key     *key,
2339         struct xfs_btree_cur    **curp,
2340         int                     *stat)          /* success/failure */
2341 {
2342         union xfs_btree_ptr     lptr;           /* left sibling block ptr */
2343         struct xfs_buf          *lbp;           /* left buffer pointer */
2344         struct xfs_btree_block  *left;          /* left btree block */
2345         union xfs_btree_ptr     rptr;           /* right sibling block ptr */
2346         struct xfs_buf          *rbp;           /* right buffer pointer */
2347         struct xfs_btree_block  *right;         /* right btree block */
2348         union xfs_btree_ptr     rrptr;          /* right-right sibling ptr */
2349         struct xfs_buf          *rrbp;          /* right-right buffer pointer */
2350         struct xfs_btree_block  *rrblock;       /* right-right btree block */
2351         int                     lrecs;
2352         int                     rrecs;
2353         int                     src_index;
2354         int                     error;          /* error return value */
2355 #ifdef DEBUG
2356         int                     i;
2357 #endif
2358
2359         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2360         XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2361
2362         XFS_BTREE_STATS_INC(cur, split);
2363
2364         /* Set up left block (current one). */
2365         left = xfs_btree_get_block(cur, level, &lbp);
2366
2367 #ifdef DEBUG
2368         error = xfs_btree_check_block(cur, left, level, lbp);
2369         if (error)
2370                 goto error0;
2371 #endif
2372
2373         xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2374
2375         /* Allocate the new block. If we can't do it, we're toast. Give up. */
2376         error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
2377         if (error)
2378                 goto error0;
2379         if (*stat == 0)
2380                 goto out0;
2381         XFS_BTREE_STATS_INC(cur, alloc);
2382
2383         /* Set up the new block as "right". */
2384         error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2385         if (error)
2386                 goto error0;
2387
2388         /* Fill in the btree header for the new right block. */
2389         xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
2390
2391         /*
2392          * Split the entries between the old and the new block evenly.
2393          * Make sure that if there's an odd number of entries now, that
2394          * each new block will have the same number of entries.
2395          */
2396         lrecs = xfs_btree_get_numrecs(left);
2397         rrecs = lrecs / 2;
2398         if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2399                 rrecs++;
2400         src_index = (lrecs - rrecs + 1);
2401
2402         XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2403
2404         /*
2405          * Copy btree block entries from the left block over to the
2406          * new block, the right. Update the right block and log the
2407          * changes.
2408          */
2409         if (level > 0) {
2410                 /* It's a non-leaf.  Move keys and pointers. */
2411                 union xfs_btree_key     *lkp;   /* left btree key */
2412                 union xfs_btree_ptr     *lpp;   /* left address pointer */
2413                 union xfs_btree_key     *rkp;   /* right btree key */
2414                 union xfs_btree_ptr     *rpp;   /* right address pointer */
2415
2416                 lkp = xfs_btree_key_addr(cur, src_index, left);
2417                 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2418                 rkp = xfs_btree_key_addr(cur, 1, right);
2419                 rpp = xfs_btree_ptr_addr(cur, 1, right);
2420
2421 #ifdef DEBUG
2422                 for (i = src_index; i < rrecs; i++) {
2423                         error = xfs_btree_check_ptr(cur, lpp, i, level);
2424                         if (error)
2425                                 goto error0;
2426                 }
2427 #endif
2428
2429                 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2430                 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2431
2432                 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2433                 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2434
2435                 /* Grab the keys to the entries moved to the right block */
2436                 xfs_btree_copy_keys(cur, key, rkp, 1);
2437         } else {
2438                 /* It's a leaf.  Move records.  */
2439                 union xfs_btree_rec     *lrp;   /* left record pointer */
2440                 union xfs_btree_rec     *rrp;   /* right record pointer */
2441
2442                 lrp = xfs_btree_rec_addr(cur, src_index, left);
2443                 rrp = xfs_btree_rec_addr(cur, 1, right);
2444
2445                 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2446                 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2447
2448                 cur->bc_ops->init_key_from_rec(key,
2449                         xfs_btree_rec_addr(cur, 1, right));
2450         }
2451
2452
2453         /*
2454          * Find the left block number by looking in the buffer.
2455          * Adjust numrecs, sibling pointers.
2456          */
2457         xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2458         xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2459         xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2460         xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2461
2462         lrecs -= rrecs;
2463         xfs_btree_set_numrecs(left, lrecs);
2464         xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2465
2466         xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2467         xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2468
2469         /*
2470          * If there's a block to the new block's right, make that block
2471          * point back to right instead of to left.
2472          */
2473         if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
2474                 error = xfs_btree_read_buf_block(cur, &rrptr,
2475                                                         0, &rrblock, &rrbp);
2476                 if (error)
2477                         goto error0;
2478                 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2479                 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2480         }
2481         /*
2482          * If the cursor is really in the right block, move it there.
2483          * If it's just pointing past the last entry in left, then we'll
2484          * insert there, so don't change anything in that case.
2485          */
2486         if (cur->bc_ptrs[level] > lrecs + 1) {
2487                 xfs_btree_setbuf(cur, level, rbp);
2488                 cur->bc_ptrs[level] -= lrecs;
2489         }
2490         /*
2491          * If there are more levels, we'll need another cursor which refers
2492          * the right block, no matter where this cursor was.
2493          */
2494         if (level + 1 < cur->bc_nlevels) {
2495                 error = xfs_btree_dup_cursor(cur, curp);
2496                 if (error)
2497                         goto error0;
2498                 (*curp)->bc_ptrs[level + 1]++;
2499         }
2500         *ptrp = rptr;
2501         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2502         *stat = 1;
2503         return 0;
2504 out0:
2505         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2506         *stat = 0;
2507         return 0;
2508
2509 error0:
2510         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2511         return error;
2512 }
2513
2514 /*
2515  * Copy the old inode root contents into a real block and make the
2516  * broot point to it.
2517  */
2518 int                                             /* error */
2519 xfs_btree_new_iroot(
2520         struct xfs_btree_cur    *cur,           /* btree cursor */
2521         int                     *logflags,      /* logging flags for inode */
2522         int                     *stat)          /* return status - 0 fail */
2523 {
2524         struct xfs_buf          *cbp;           /* buffer for cblock */
2525         struct xfs_btree_block  *block;         /* btree block */
2526         struct xfs_btree_block  *cblock;        /* child btree block */
2527         union xfs_btree_key     *ckp;           /* child key pointer */
2528         union xfs_btree_ptr     *cpp;           /* child ptr pointer */
2529         union xfs_btree_key     *kp;            /* pointer to btree key */
2530         union xfs_btree_ptr     *pp;            /* pointer to block addr */
2531         union xfs_btree_ptr     nptr;           /* new block addr */
2532         int                     level;          /* btree level */
2533         int                     error;          /* error return code */
2534 #ifdef DEBUG
2535         int                     i;              /* loop counter */
2536 #endif
2537
2538         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2539         XFS_BTREE_STATS_INC(cur, newroot);
2540
2541         ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2542
2543         level = cur->bc_nlevels - 1;
2544
2545         block = xfs_btree_get_iroot(cur);
2546         pp = xfs_btree_ptr_addr(cur, 1, block);
2547
2548         /* Allocate the new block. If we can't do it, we're toast. Give up. */
2549         error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
2550         if (error)
2551                 goto error0;
2552         if (*stat == 0) {
2553                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2554                 return 0;
2555         }
2556         XFS_BTREE_STATS_INC(cur, alloc);
2557
2558         /* Copy the root into a real block. */
2559         error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2560         if (error)
2561                 goto error0;
2562
2563         /*
2564          * we can't just memcpy() the root in for CRC enabled btree blocks.
2565          * In that case have to also ensure the blkno remains correct
2566          */
2567         memcpy(cblock, block, xfs_btree_block_len(cur));
2568         if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2569                 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2570                         cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2571                 else
2572                         cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2573         }
2574
2575         be16_add_cpu(&block->bb_level, 1);
2576         xfs_btree_set_numrecs(block, 1);
2577         cur->bc_nlevels++;
2578         cur->bc_ptrs[level + 1] = 1;
2579
2580         kp = xfs_btree_key_addr(cur, 1, block);
2581         ckp = xfs_btree_key_addr(cur, 1, cblock);
2582         xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2583
2584         cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2585 #ifdef DEBUG
2586         for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2587                 error = xfs_btree_check_ptr(cur, pp, i, level);
2588                 if (error)
2589                         goto error0;
2590         }
2591 #endif
2592         xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2593
2594 #ifdef DEBUG
2595         error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2596         if (error)
2597                 goto error0;
2598 #endif
2599         xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2600
2601         xfs_iroot_realloc(cur->bc_private.b.ip,
2602                           1 - xfs_btree_get_numrecs(cblock),
2603                           cur->bc_private.b.whichfork);
2604
2605         xfs_btree_setbuf(cur, level, cbp);
2606
2607         /*
2608          * Do all this logging at the end so that
2609          * the root is at the right level.
2610          */
2611         xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2612         xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2613         xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2614
2615         *logflags |=
2616                 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
2617         *stat = 1;
2618         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2619         return 0;
2620 error0:
2621         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2622         return error;
2623 }
2624
2625 /*
2626  * Allocate a new root block, fill it in.
2627  */
2628 STATIC int                              /* error */
2629 xfs_btree_new_root(
2630         struct xfs_btree_cur    *cur,   /* btree cursor */
2631         int                     *stat)  /* success/failure */
2632 {
2633         struct xfs_btree_block  *block; /* one half of the old root block */
2634         struct xfs_buf          *bp;    /* buffer containing block */
2635         int                     error;  /* error return value */
2636         struct xfs_buf          *lbp;   /* left buffer pointer */
2637         struct xfs_btree_block  *left;  /* left btree block */
2638         struct xfs_buf          *nbp;   /* new (root) buffer */
2639         struct xfs_btree_block  *new;   /* new (root) btree block */
2640         int                     nptr;   /* new value for key index, 1 or 2 */
2641         struct xfs_buf          *rbp;   /* right buffer pointer */
2642         struct xfs_btree_block  *right; /* right btree block */
2643         union xfs_btree_ptr     rptr;
2644         union xfs_btree_ptr     lptr;
2645
2646         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2647         XFS_BTREE_STATS_INC(cur, newroot);
2648
2649         /* initialise our start point from the cursor */
2650         cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2651
2652         /* Allocate the new block. If we can't do it, we're toast. Give up. */
2653         error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
2654         if (error)
2655                 goto error0;
2656         if (*stat == 0)
2657                 goto out0;
2658         XFS_BTREE_STATS_INC(cur, alloc);
2659
2660         /* Set up the new block. */
2661         error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2662         if (error)
2663                 goto error0;
2664
2665         /* Set the root in the holding structure  increasing the level by 1. */
2666         cur->bc_ops->set_root(cur, &lptr, 1);
2667
2668         /*
2669          * At the previous root level there are now two blocks: the old root,
2670          * and the new block generated when it was split.  We don't know which
2671          * one the cursor is pointing at, so we set up variables "left" and
2672          * "right" for each case.
2673          */
2674         block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2675
2676 #ifdef DEBUG
2677         error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2678         if (error)
2679                 goto error0;
2680 #endif
2681
2682         xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2683         if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2684                 /* Our block is left, pick up the right block. */
2685                 lbp = bp;
2686                 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2687                 left = block;
2688                 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
2689                 if (error)
2690                         goto error0;
2691                 bp = rbp;
2692                 nptr = 1;
2693         } else {
2694                 /* Our block is right, pick up the left block. */
2695                 rbp = bp;
2696                 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2697                 right = block;
2698                 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2699                 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
2700                 if (error)
2701                         goto error0;
2702                 bp = lbp;
2703                 nptr = 2;
2704         }
2705         /* Fill in the new block's btree header and log it. */
2706         xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
2707         xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2708         ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2709                         !xfs_btree_ptr_is_null(cur, &rptr));
2710
2711         /* Fill in the key data in the new root. */
2712         if (xfs_btree_get_level(left) > 0) {
2713                 xfs_btree_copy_keys(cur,
2714                                 xfs_btree_key_addr(cur, 1, new),
2715                                 xfs_btree_key_addr(cur, 1, left), 1);
2716                 xfs_btree_copy_keys(cur,
2717                                 xfs_btree_key_addr(cur, 2, new),
2718                                 xfs_btree_key_addr(cur, 1, right), 1);
2719         } else {
2720                 cur->bc_ops->init_key_from_rec(
2721                                 xfs_btree_key_addr(cur, 1, new),
2722                                 xfs_btree_rec_addr(cur, 1, left));
2723                 cur->bc_ops->init_key_from_rec(
2724                                 xfs_btree_key_addr(cur, 2, new),
2725                                 xfs_btree_rec_addr(cur, 1, right));
2726         }
2727         xfs_btree_log_keys(cur, nbp, 1, 2);
2728
2729         /* Fill in the pointer data in the new root. */
2730         xfs_btree_copy_ptrs(cur,
2731                 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2732         xfs_btree_copy_ptrs(cur,
2733                 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2734         xfs_btree_log_ptrs(cur, nbp, 1, 2);
2735
2736         /* Fix up the cursor. */
2737         xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2738         cur->bc_ptrs[cur->bc_nlevels] = nptr;
2739         cur->bc_nlevels++;
2740         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2741         *stat = 1;
2742         return 0;
2743 error0:
2744         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2745         return error;
2746 out0:
2747         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2748         *stat = 0;
2749         return 0;
2750 }
2751
2752 STATIC int
2753 xfs_btree_make_block_unfull(
2754         struct xfs_btree_cur    *cur,   /* btree cursor */
2755         int                     level,  /* btree level */
2756         int                     numrecs,/* # of recs in block */
2757         int                     *oindex,/* old tree index */
2758         int                     *index, /* new tree index */
2759         union xfs_btree_ptr     *nptr,  /* new btree ptr */
2760         struct xfs_btree_cur    **ncur, /* new btree cursor */
2761         union xfs_btree_rec     *nrec,  /* new record */
2762         int                     *stat)
2763 {
2764         union xfs_btree_key     key;    /* new btree key value */
2765         int                     error = 0;
2766
2767         if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2768             level == cur->bc_nlevels - 1) {
2769                 struct xfs_inode *ip = cur->bc_private.b.ip;
2770
2771                 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2772                         /* A root block that can be made bigger. */
2773                         xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2774                 } else {
2775                         /* A root block that needs replacing */
2776                         int     logflags = 0;
2777
2778                         error = xfs_btree_new_iroot(cur, &logflags, stat);
2779                         if (error || *stat == 0)
2780                                 return error;
2781
2782                         xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2783                 }
2784
2785                 return 0;
2786         }
2787
2788         /* First, try shifting an entry to the right neighbor. */
2789         error = xfs_btree_rshift(cur, level, stat);
2790         if (error || *stat)
2791                 return error;
2792
2793         /* Next, try shifting an entry to the left neighbor. */
2794         error = xfs_btree_lshift(cur, level, stat);
2795         if (error)
2796                 return error;
2797
2798         if (*stat) {
2799                 *oindex = *index = cur->bc_ptrs[level];
2800                 return 0;
2801         }
2802
2803         /*
2804          * Next, try splitting the current block in half.
2805          *
2806          * If this works we have to re-set our variables because we
2807          * could be in a different block now.
2808          */
2809         error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2810         if (error || *stat == 0)
2811                 return error;
2812
2813
2814         *index = cur->bc_ptrs[level];
2815         cur->bc_ops->init_rec_from_key(&key, nrec);
2816         return 0;
2817 }
2818
2819 /*
2820  * Insert one record/level.  Return information to the caller
2821  * allowing the next level up to proceed if necessary.
2822  */
2823 STATIC int
2824 xfs_btree_insrec(
2825         struct xfs_btree_cur    *cur,   /* btree cursor */
2826         int                     level,  /* level to insert record at */
2827         union xfs_btree_ptr     *ptrp,  /* i/o: block number inserted */
2828         union xfs_btree_rec     *recp,  /* i/o: record data inserted */
2829         struct xfs_btree_cur    **curp, /* output: new cursor replacing cur */
2830         int                     *stat)  /* success/failure */
2831 {
2832         struct xfs_btree_block  *block; /* btree block */
2833         struct xfs_buf          *bp;    /* buffer for block */
2834         union xfs_btree_key     key;    /* btree key */
2835         union xfs_btree_ptr     nptr;   /* new block ptr */
2836         struct xfs_btree_cur    *ncur;  /* new btree cursor */
2837         union xfs_btree_rec     nrec;   /* new record count */
2838         int                     optr;   /* old key/record index */
2839         int                     ptr;    /* key/record index */
2840         int                     numrecs;/* number of records */
2841         int                     error;  /* error return value */
2842 #ifdef DEBUG
2843         int                     i;
2844 #endif
2845
2846         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2847         XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2848
2849         ncur = NULL;
2850
2851         /*
2852          * If we have an external root pointer, and we've made it to the
2853          * root level, allocate a new root block and we're done.
2854          */
2855         if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2856             (level >= cur->bc_nlevels)) {
2857                 error = xfs_btree_new_root(cur, stat);
2858                 xfs_btree_set_ptr_null(cur, ptrp);
2859
2860                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2861                 return error;
2862         }
2863
2864         /* If we're off the left edge, return failure. */
2865         ptr = cur->bc_ptrs[level];
2866         if (ptr == 0) {
2867                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2868                 *stat = 0;
2869                 return 0;
2870         }
2871
2872         /* Make a key out of the record data to be inserted, and save it. */
2873         cur->bc_ops->init_key_from_rec(&key, recp);
2874
2875         optr = ptr;
2876
2877         XFS_BTREE_STATS_INC(cur, insrec);
2878
2879         /* Get pointers to the btree buffer and block. */
2880         block = xfs_btree_get_block(cur, level, &bp);
2881         numrecs = xfs_btree_get_numrecs(block);
2882
2883 #ifdef DEBUG
2884         error = xfs_btree_check_block(cur, block, level, bp);
2885         if (error)
2886                 goto error0;
2887
2888         /* Check that the new entry is being inserted in the right place. */
2889         if (ptr <= numrecs) {
2890                 if (level == 0) {
2891                         ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2892                                 xfs_btree_rec_addr(cur, ptr, block)));
2893                 } else {
2894                         ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2895                                 xfs_btree_key_addr(cur, ptr, block)));
2896                 }
2897         }
2898 #endif
2899
2900         /*
2901          * If the block is full, we can't insert the new entry until we
2902          * make the block un-full.
2903          */
2904         xfs_btree_set_ptr_null(cur, &nptr);
2905         if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2906                 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2907                                         &optr, &ptr, &nptr, &ncur, &nrec, stat);
2908                 if (error || *stat == 0)
2909                         goto error0;
2910         }
2911
2912         /*
2913          * The current block may have changed if the block was
2914          * previously full and we have just made space in it.
2915          */
2916         block = xfs_btree_get_block(cur, level, &bp);
2917         numrecs = xfs_btree_get_numrecs(block);
2918
2919 #ifdef DEBUG
2920         error = xfs_btree_check_block(cur, block, level, bp);
2921         if (error)
2922                 return error;
2923 #endif
2924
2925         /*
2926          * At this point we know there's room for our new entry in the block
2927          * we're pointing at.
2928          */
2929         XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2930
2931         if (level > 0) {
2932                 /* It's a nonleaf. make a hole in the keys and ptrs */
2933                 union xfs_btree_key     *kp;
2934                 union xfs_btree_ptr     *pp;
2935
2936                 kp = xfs_btree_key_addr(cur, ptr, block);
2937                 pp = xfs_btree_ptr_addr(cur, ptr, block);
2938
2939 #ifdef DEBUG
2940                 for (i = numrecs - ptr; i >= 0; i--) {
2941                         error = xfs_btree_check_ptr(cur, pp, i, level);
2942                         if (error)
2943                                 return error;
2944                 }
2945 #endif
2946
2947                 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2948                 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2949
2950 #ifdef DEBUG
2951                 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2952                 if (error)
2953                         goto error0;
2954 #endif
2955
2956                 /* Now put the new data in, bump numrecs and log it. */
2957                 xfs_btree_copy_keys(cur, kp, &key, 1);
2958                 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2959                 numrecs++;
2960                 xfs_btree_set_numrecs(block, numrecs);
2961                 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2962                 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2963 #ifdef DEBUG
2964                 if (ptr < numrecs) {
2965                         ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2966                                 xfs_btree_key_addr(cur, ptr + 1, block)));
2967                 }
2968 #endif
2969         } else {
2970                 /* It's a leaf. make a hole in the records */
2971                 union xfs_btree_rec             *rp;
2972
2973                 rp = xfs_btree_rec_addr(cur, ptr, block);
2974
2975                 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2976
2977                 /* Now put the new data in, bump numrecs and log it. */
2978                 xfs_btree_copy_recs(cur, rp, recp, 1);
2979                 xfs_btree_set_numrecs(block, ++numrecs);
2980                 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2981 #ifdef DEBUG
2982                 if (ptr < numrecs) {
2983                         ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2984                                 xfs_btree_rec_addr(cur, ptr + 1, block)));
2985                 }
2986 #endif
2987         }
2988
2989         /* Log the new number of records in the btree header. */
2990         xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2991
2992         /* If we inserted at the start of a block, update the parents' keys. */
2993         if (optr == 1) {
2994                 error = xfs_btree_updkey(cur, &key, level + 1);
2995                 if (error)
2996                         goto error0;
2997         }
2998
2999         /*
3000          * If we are tracking the last record in the tree and
3001          * we are at the far right edge of the tree, update it.
3002          */
3003         if (xfs_btree_is_lastrec(cur, block, level)) {
3004                 cur->bc_ops->update_lastrec(cur, block, recp,
3005                                             ptr, LASTREC_INSREC);
3006         }
3007
3008         /*
3009          * Return the new block number, if any.
3010          * If there is one, give back a record value and a cursor too.
3011          */
3012         *ptrp = nptr;
3013         if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3014                 *recp = nrec;
3015                 *curp = ncur;
3016         }
3017
3018         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3019         *stat = 1;
3020         return 0;
3021
3022 error0:
3023         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3024         return error;
3025 }
3026
3027 /*
3028  * Insert the record at the point referenced by cur.
3029  *
3030  * A multi-level split of the tree on insert will invalidate the original
3031  * cursor.  All callers of this function should assume that the cursor is
3032  * no longer valid and revalidate it.
3033  */
3034 int
3035 xfs_btree_insert(
3036         struct xfs_btree_cur    *cur,
3037         int                     *stat)
3038 {
3039         int                     error;  /* error return value */
3040         int                     i;      /* result value, 0 for failure */
3041         int                     level;  /* current level number in btree */
3042         union xfs_btree_ptr     nptr;   /* new block number (split result) */
3043         struct xfs_btree_cur    *ncur;  /* new cursor (split result) */
3044         struct xfs_btree_cur    *pcur;  /* previous level's cursor */
3045         union xfs_btree_rec     rec;    /* record to insert */
3046
3047         level = 0;
3048         ncur = NULL;
3049         pcur = cur;
3050
3051         xfs_btree_set_ptr_null(cur, &nptr);
3052         cur->bc_ops->init_rec_from_cur(cur, &rec);
3053
3054         /*
3055          * Loop going up the tree, starting at the leaf level.
3056          * Stop when we don't get a split block, that must mean that
3057          * the insert is finished with this level.
3058          */
3059         do {
3060                 /*
3061                  * Insert nrec/nptr into this level of the tree.
3062                  * Note if we fail, nptr will be null.
3063                  */
3064                 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3065                 if (error) {
3066                         if (pcur != cur)
3067                                 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3068                         goto error0;
3069                 }
3070
3071                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3072                 level++;
3073
3074                 /*
3075                  * See if the cursor we just used is trash.
3076                  * Can't trash the caller's cursor, but otherwise we should
3077                  * if ncur is a new cursor or we're about to be done.
3078                  */
3079                 if (pcur != cur &&
3080                     (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3081                         /* Save the state from the cursor before we trash it */
3082                         if (cur->bc_ops->update_cursor)
3083                                 cur->bc_ops->update_cursor(pcur, cur);
3084                         cur->bc_nlevels = pcur->bc_nlevels;
3085                         xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3086                 }
3087                 /* If we got a new cursor, switch to it. */
3088                 if (ncur) {
3089                         pcur = ncur;
3090                         ncur = NULL;
3091                 }
3092         } while (!xfs_btree_ptr_is_null(cur, &nptr));
3093
3094         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3095         *stat = i;
3096         return 0;
3097 error0:
3098         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3099         return error;
3100 }
3101
3102 /*
3103  * Try to merge a non-leaf block back into the inode root.
3104  *
3105  * Note: the killroot names comes from the fact that we're effectively
3106  * killing the old root block.  But because we can't just delete the
3107  * inode we have to copy the single block it was pointing to into the
3108  * inode.
3109  */
3110 STATIC int
3111 xfs_btree_kill_iroot(
3112         struct xfs_btree_cur    *cur)
3113 {
3114         int                     whichfork = cur->bc_private.b.whichfork;
3115         struct xfs_inode        *ip = cur->bc_private.b.ip;
3116         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
3117         struct xfs_btree_block  *block;
3118         struct xfs_btree_block  *cblock;
3119         union xfs_btree_key     *kp;
3120         union xfs_btree_key     *ckp;
3121         union xfs_btree_ptr     *pp;
3122         union xfs_btree_ptr     *cpp;
3123         struct xfs_buf          *cbp;
3124         int                     level;
3125         int                     index;
3126         int                     numrecs;
3127 #ifdef DEBUG
3128         union xfs_btree_ptr     ptr;
3129         int                     i;
3130 #endif
3131
3132         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3133
3134         ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3135         ASSERT(cur->bc_nlevels > 1);
3136
3137         /*
3138          * Don't deal with the root block needs to be a leaf case.
3139          * We're just going to turn the thing back into extents anyway.
3140          */
3141         level = cur->bc_nlevels - 1;
3142         if (level == 1)
3143                 goto out0;
3144
3145         /*
3146          * Give up if the root has multiple children.
3147          */
3148         block = xfs_btree_get_iroot(cur);
3149         if (xfs_btree_get_numrecs(block) != 1)
3150                 goto out0;
3151
3152         cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3153         numrecs = xfs_btree_get_numrecs(cblock);
3154
3155         /*
3156          * Only do this if the next level will fit.
3157          * Then the data must be copied up to the inode,
3158          * instead of freeing the root you free the next level.
3159          */
3160         if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3161                 goto out0;
3162
3163         XFS_BTREE_STATS_INC(cur, killroot);
3164
3165 #ifdef DEBUG
3166         xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3167         ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3168         xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3169         ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3170 #endif
3171
3172         index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3173         if (index) {
3174                 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3175                                   cur->bc_private.b.whichfork);
3176                 block = ifp->if_broot;
3177         }
3178
3179         be16_add_cpu(&block->bb_numrecs, index);
3180         ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3181
3182         kp = xfs_btree_key_addr(cur, 1, block);
3183         ckp = xfs_btree_key_addr(cur, 1, cblock);
3184         xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3185
3186         pp = xfs_btree_ptr_addr(cur, 1, block);
3187         cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3188 #ifdef DEBUG
3189         for (i = 0; i < numrecs; i++) {
3190                 int             error;
3191
3192                 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3193                 if (error) {
3194                         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3195                         return error;
3196                 }
3197         }
3198 #endif
3199         xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3200
3201         cur->bc_ops->free_block(cur, cbp);
3202         XFS_BTREE_STATS_INC(cur, free);
3203
3204         cur->bc_bufs[level - 1] = NULL;
3205         be16_add_cpu(&block->bb_level, -1);
3206         xfs_trans_log_inode(cur->bc_tp, ip,
3207                 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
3208         cur->bc_nlevels--;
3209 out0:
3210         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3211         return 0;
3212 }
3213
3214 /*
3215  * Kill the current root node, and replace it with it's only child node.
3216  */
3217 STATIC int
3218 xfs_btree_kill_root(
3219         struct xfs_btree_cur    *cur,
3220         struct xfs_buf          *bp,
3221         int                     level,
3222         union xfs_btree_ptr     *newroot)
3223 {
3224         int                     error;
3225
3226         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3227         XFS_BTREE_STATS_INC(cur, killroot);
3228
3229         /*
3230          * Update the root pointer, decreasing the level by 1 and then
3231          * free the old root.
3232          */
3233         cur->bc_ops->set_root(cur, newroot, -1);
3234
3235         error = cur->bc_ops->free_block(cur, bp);
3236         if (error) {
3237                 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3238                 return error;
3239         }
3240
3241         XFS_BTREE_STATS_INC(cur, free);
3242
3243         cur->bc_bufs[level] = NULL;
3244         cur->bc_ra[level] = 0;
3245         cur->bc_nlevels--;
3246
3247         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3248         return 0;
3249 }
3250
3251 STATIC int
3252 xfs_btree_dec_cursor(
3253         struct xfs_btree_cur    *cur,
3254         int                     level,
3255         int                     *stat)
3256 {
3257         int                     error;
3258         int                     i;
3259
3260         if (level > 0) {
3261                 error = xfs_btree_decrement(cur, level, &i);
3262                 if (error)
3263                         return error;
3264         }
3265
3266         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3267         *stat = 1;
3268         return 0;
3269 }
3270
3271 /*
3272  * Single level of the btree record deletion routine.
3273  * Delete record pointed to by cur/level.
3274  * Remove the record from its block then rebalance the tree.
3275  * Return 0 for error, 1 for done, 2 to go on to the next level.
3276  */
3277 STATIC int                                      /* error */
3278 xfs_btree_delrec(
3279         struct xfs_btree_cur    *cur,           /* btree cursor */
3280         int                     level,          /* level removing record from */
3281         int                     *stat)          /* fail/done/go-on */
3282 {
3283         struct xfs_btree_block  *block;         /* btree block */
3284         union xfs_btree_ptr     cptr;           /* current block ptr */
3285         struct xfs_buf          *bp;            /* buffer for block */
3286         int                     error;          /* error return value */
3287         int                     i;              /* loop counter */
3288         union xfs_btree_key     key;            /* storage for keyp */
3289         union xfs_btree_key     *keyp = &key;   /* passed to the next level */
3290         union xfs_btree_ptr     lptr;           /* left sibling block ptr */
3291         struct xfs_buf          *lbp;           /* left buffer pointer */
3292         struct xfs_btree_block  *left;          /* left btree block */
3293         int                     lrecs = 0;      /* left record count */
3294         int                     ptr;            /* key/record index */
3295         union xfs_btree_ptr     rptr;           /* right sibling block ptr */
3296         struct xfs_buf          *rbp;           /* right buffer pointer */
3297         struct xfs_btree_block  *right;         /* right btree block */
3298         struct xfs_btree_block  *rrblock;       /* right-right btree block */
3299         struct xfs_buf          *rrbp;          /* right-right buffer pointer */
3300         int                     rrecs = 0;      /* right record count */
3301         struct xfs_btree_cur    *tcur;          /* temporary btree cursor */
3302         int                     numrecs;        /* temporary numrec count */
3303
3304         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3305         XFS_BTREE_TRACE_ARGI(cur, level);
3306
3307         tcur = NULL;
3308
3309         /* Get the index of the entry being deleted, check for nothing there. */
3310         ptr = cur->bc_ptrs[level];
3311         if (ptr == 0) {
3312                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3313                 *stat = 0;
3314                 return 0;
3315         }
3316
3317         /* Get the buffer & block containing the record or key/ptr. */
3318         block = xfs_btree_get_block(cur, level, &bp);
3319         numrecs = xfs_btree_get_numrecs(block);
3320
3321 #ifdef DEBUG
3322         error = xfs_btree_check_block(cur, block, level, bp);
3323         if (error)
3324                 goto error0;
3325 #endif
3326
3327         /* Fail if we're off the end of the block. */
3328         if (ptr > numrecs) {
3329                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3330                 *stat = 0;
3331                 return 0;
3332         }
3333
3334         XFS_BTREE_STATS_INC(cur, delrec);
3335         XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3336
3337         /* Excise the entries being deleted. */
3338         if (level > 0) {
3339                 /* It's a nonleaf. operate on keys and ptrs */
3340                 union xfs_btree_key     *lkp;
3341                 union xfs_btree_ptr     *lpp;
3342
3343                 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3344                 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3345
3346 #ifdef DEBUG
3347                 for (i = 0; i < numrecs - ptr; i++) {
3348                         error = xfs_btree_check_ptr(cur, lpp, i, level);
3349                         if (error)
3350                                 goto error0;
3351                 }
3352 #endif
3353
3354                 if (ptr < numrecs) {
3355                         xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3356                         xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3357                         xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3358                         xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3359                 }
3360
3361                 /*
3362                  * If it's the first record in the block, we'll need to pass a
3363                  * key up to the next level (updkey).
3364                  */
3365                 if (ptr == 1)
3366                         keyp = xfs_btree_key_addr(cur, 1, block);
3367         } else {
3368                 /* It's a leaf. operate on records */
3369                 if (ptr < numrecs) {
3370                         xfs_btree_shift_recs(cur,
3371                                 xfs_btree_rec_addr(cur, ptr + 1, block),
3372                                 -1, numrecs - ptr);
3373                         xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3374                 }
3375
3376                 /*
3377                  * If it's the first record in the block, we'll need a key
3378                  * structure to pass up to the next level (updkey).
3379                  */
3380                 if (ptr == 1) {
3381                         cur->bc_ops->init_key_from_rec(&key,
3382                                         xfs_btree_rec_addr(cur, 1, block));
3383                         keyp = &key;
3384                 }
3385         }
3386
3387         /*
3388          * Decrement and log the number of entries in the block.
3389          */
3390         xfs_btree_set_numrecs(block, --numrecs);
3391         xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3392
3393         /*
3394          * If we are tracking the last record in the tree and
3395          * we are at the far right edge of the tree, update it.
3396          */
3397         if (xfs_btree_is_lastrec(cur, block, level)) {
3398                 cur->bc_ops->update_lastrec(cur, block, NULL,
3399                                             ptr, LASTREC_DELREC);
3400         }
3401
3402         /*
3403          * We're at the root level.  First, shrink the root block in-memory.
3404          * Try to get rid of the next level down.  If we can't then there's
3405          * nothing left to do.
3406          */
3407         if (level == cur->bc_nlevels - 1) {
3408                 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3409                         xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3410                                           cur->bc_private.b.whichfork);
3411
3412                         error = xfs_btree_kill_iroot(cur);
3413                         if (error)
3414                                 goto error0;
3415
3416                         error = xfs_btree_dec_cursor(cur, level, stat);
3417                         if (error)
3418                                 goto error0;
3419                         *stat = 1;
3420                         return 0;
3421                 }
3422
3423                 /*
3424                  * If this is the root level, and there's only one entry left,
3425                  * and it's NOT the leaf level, then we can get rid of this
3426                  * level.
3427                  */
3428                 if (numrecs == 1 && level > 0) {
3429                         union xfs_btree_ptr     *pp;
3430                         /*
3431                          * pp is still set to the first pointer in the block.
3432                          * Make it the new root of the btree.
3433                          */
3434                         pp = xfs_btree_ptr_addr(cur, 1, block);
3435                         error = xfs_btree_kill_root(cur, bp, level, pp);
3436                         if (error)
3437                                 goto error0;
3438                 } else if (level > 0) {
3439                         error = xfs_btree_dec_cursor(cur, level, stat);
3440                         if (error)
3441                                 goto error0;
3442                 }
3443                 *stat = 1;
3444                 return 0;
3445         }
3446
3447         /*
3448          * If we deleted the leftmost entry in the block, update the
3449          * key values above us in the tree.
3450          */
3451         if (ptr == 1) {
3452                 error = xfs_btree_updkey(cur, keyp, level + 1);
3453                 if (error)
3454                         goto error0;
3455         }
3456
3457         /*
3458          * If the number of records remaining in the block is at least
3459          * the minimum, we're done.
3460          */
3461         if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3462                 error = xfs_btree_dec_cursor(cur, level, stat);
3463                 if (error)
3464                         goto error0;
3465                 return 0;
3466         }
3467
3468         /*
3469          * Otherwise, we have to move some records around to keep the
3470          * tree balanced.  Look at the left and right sibling blocks to
3471          * see if we can re-balance by moving only one record.
3472          */
3473         xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3474         xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3475
3476         if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3477                 /*
3478                  * One child of root, need to get a chance to copy its contents
3479                  * into the root and delete it. Can't go up to next level,
3480                  * there's nothing to delete there.
3481                  */
3482                 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3483                     xfs_btree_ptr_is_null(cur, &lptr) &&
3484                     level == cur->bc_nlevels - 2) {
3485                         error = xfs_btree_kill_iroot(cur);
3486                         if (!error)
3487                                 error = xfs_btree_dec_cursor(cur, level, stat);
3488                         if (error)
3489                                 goto error0;
3490                         return 0;
3491                 }
3492         }
3493
3494         ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3495                !xfs_btree_ptr_is_null(cur, &lptr));
3496
3497         /*
3498          * Duplicate the cursor so our btree manipulations here won't
3499          * disrupt the next level up.
3500          */
3501         error = xfs_btree_dup_cursor(cur, &tcur);
3502         if (error)
3503                 goto error0;
3504
3505         /*
3506          * If there's a right sibling, see if it's ok to shift an entry
3507          * out of it.
3508          */
3509         if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3510                 /*
3511                  * Move the temp cursor to the last entry in the next block.
3512                  * Actually any entry but the first would suffice.
3513                  */
3514                 i = xfs_btree_lastrec(tcur, level);
3515                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3516
3517                 error = xfs_btree_increment(tcur, level, &i);
3518                 if (error)
3519                         goto error0;
3520                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3521
3522                 i = xfs_btree_lastrec(tcur, level);
3523                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3524
3525                 /* Grab a pointer to the block. */
3526                 right = xfs_btree_get_block(tcur, level, &rbp);
3527 #ifdef DEBUG
3528                 error = xfs_btree_check_block(tcur, right, level, rbp);
3529                 if (error)
3530                         goto error0;
3531 #endif
3532                 /* Grab the current block number, for future use. */
3533                 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3534
3535                 /*
3536                  * If right block is full enough so that removing one entry
3537                  * won't make it too empty, and left-shifting an entry out
3538                  * of right to us works, we're done.
3539                  */
3540                 if (xfs_btree_get_numrecs(right) - 1 >=
3541                     cur->bc_ops->get_minrecs(tcur, level)) {
3542                         error = xfs_btree_lshift(tcur, level, &i);
3543                         if (error)
3544                                 goto error0;
3545                         if (i) {
3546                                 ASSERT(xfs_btree_get_numrecs(block) >=
3547                                        cur->bc_ops->get_minrecs(tcur, level));
3548
3549                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3550                                 tcur = NULL;
3551
3552                                 error = xfs_btree_dec_cursor(cur, level, stat);
3553                                 if (error)
3554                                         goto error0;
3555                                 return 0;
3556                         }
3557                 }
3558
3559                 /*
3560                  * Otherwise, grab the number of records in right for
3561                  * future reference, and fix up the temp cursor to point
3562                  * to our block again (last record).
3563                  */
3564                 rrecs = xfs_btree_get_numrecs(right);
3565                 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3566                         i = xfs_btree_firstrec(tcur, level);
3567                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3568
3569                         error = xfs_btree_decrement(tcur, level, &i);
3570                         if (error)
3571                                 goto error0;
3572                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3573                 }
3574         }
3575
3576         /*
3577          * If there's a left sibling, see if it's ok to shift an entry
3578          * out of it.
3579          */
3580         if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3581                 /*
3582                  * Move the temp cursor to the first entry in the
3583                  * previous block.
3584                  */
3585                 i = xfs_btree_firstrec(tcur, level);
3586                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3587
3588                 error = xfs_btree_decrement(tcur, level, &i);
3589                 if (error)
3590                         goto error0;
3591                 i = xfs_btree_firstrec(tcur, level);
3592                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3593
3594                 /* Grab a pointer to the block. */
3595                 left = xfs_btree_get_block(tcur, level, &lbp);
3596 #ifdef DEBUG
3597                 error = xfs_btree_check_block(cur, left, level, lbp);
3598                 if (error)
3599                         goto error0;
3600 #endif
3601                 /* Grab the current block number, for future use. */
3602                 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3603
3604                 /*
3605                  * If left block is full enough so that removing one entry
3606                  * won't make it too empty, and right-shifting an entry out
3607                  * of left to us works, we're done.
3608                  */
3609                 if (xfs_btree_get_numrecs(left) - 1 >=
3610                     cur->bc_ops->get_minrecs(tcur, level)) {
3611                         error = xfs_btree_rshift(tcur, level, &i);
3612                         if (error)
3613                                 goto error0;
3614                         if (i) {
3615                                 ASSERT(xfs_btree_get_numrecs(block) >=
3616                                        cur->bc_ops->get_minrecs(tcur, level));
3617                                 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3618                                 tcur = NULL;
3619                                 if (level == 0)
3620                                         cur->bc_ptrs[0]++;
3621                                 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3622                                 *stat = 1;
3623                                 return 0;
3624                         }
3625                 }
3626
3627                 /*
3628                  * Otherwise, grab the number of records in right for
3629                  * future reference.
3630                  */
3631                 lrecs = xfs_btree_get_numrecs(left);
3632         }
3633
3634         /* Delete the temp cursor, we're done with it. */
3635         xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3636         tcur = NULL;
3637
3638         /* If here, we need to do a join to keep the tree balanced. */
3639         ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3640
3641         if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3642             lrecs + xfs_btree_get_numrecs(block) <=
3643                         cur->bc_ops->get_maxrecs(cur, level)) {
3644                 /*
3645                  * Set "right" to be the starting block,
3646                  * "left" to be the left neighbor.
3647                  */
3648                 rptr = cptr;
3649                 right = block;
3650                 rbp = bp;
3651                 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
3652                 if (error)
3653                         goto error0;
3654
3655         /*
3656          * If that won't work, see if we can join with the right neighbor block.
3657          */
3658         } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3659                    rrecs + xfs_btree_get_numrecs(block) <=
3660                         cur->bc_ops->get_maxrecs(cur, level)) {
3661                 /*
3662                  * Set "left" to be the starting block,
3663                  * "right" to be the right neighbor.
3664                  */
3665                 lptr = cptr;
3666                 left = block;
3667                 lbp = bp;
3668                 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
3669                 if (error)
3670                         goto error0;
3671
3672         /*
3673          * Otherwise, we can't fix the imbalance.
3674          * Just return.  This is probably a logic error, but it's not fatal.
3675          */
3676         } else {
3677                 error = xfs_btree_dec_cursor(cur, level, stat);
3678                 if (error)
3679                         goto error0;
3680                 return 0;
3681         }
3682
3683         rrecs = xfs_btree_get_numrecs(right);
3684         lrecs = xfs_btree_get_numrecs(left);
3685
3686         /*
3687          * We're now going to join "left" and "right" by moving all the stuff
3688          * in "right" to "left" and deleting "right".
3689          */
3690         XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3691         if (level > 0) {
3692                 /* It's a non-leaf.  Move keys and pointers. */
3693                 union xfs_btree_key     *lkp;   /* left btree key */
3694                 union xfs_btree_ptr     *lpp;   /* left address pointer */
3695                 union xfs_btree_key     *rkp;   /* right btree key */
3696                 union xfs_btree_ptr     *rpp;   /* right address pointer */
3697
3698                 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3699                 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3700                 rkp = xfs_btree_key_addr(cur, 1, right);
3701                 rpp = xfs_btree_ptr_addr(cur, 1, right);
3702 #ifdef DEBUG
3703                 for (i = 1; i < rrecs; i++) {
3704                         error = xfs_btree_check_ptr(cur, rpp, i, level);
3705                         if (error)
3706                                 goto error0;
3707                 }
3708 #endif
3709                 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3710                 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3711
3712                 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3713                 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3714         } else {
3715                 /* It's a leaf.  Move records.  */
3716                 union xfs_btree_rec     *lrp;   /* left record pointer */
3717                 union xfs_btree_rec     *rrp;   /* right record pointer */
3718
3719                 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3720                 rrp = xfs_btree_rec_addr(cur, 1, right);
3721
3722                 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3723                 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3724         }
3725
3726         XFS_BTREE_STATS_INC(cur, join);
3727
3728         /*
3729          * Fix up the number of records and right block pointer in the
3730          * surviving block, and log it.
3731          */
3732         xfs_btree_set_numrecs(left, lrecs + rrecs);
3733         xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3734         xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3735         xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3736
3737         /* If there is a right sibling, point it to the remaining block. */
3738         xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3739         if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3740                 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
3741                 if (error)
3742                         goto error0;
3743                 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3744                 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3745         }
3746
3747         /* Free the deleted block. */
3748         error = cur->bc_ops->free_block(cur, rbp);
3749         if (error)
3750                 goto error0;
3751         XFS_BTREE_STATS_INC(cur, free);
3752
3753         /*
3754          * If we joined with the left neighbor, set the buffer in the
3755          * cursor to the left block, and fix up the index.
3756          */
3757         if (bp != lbp) {
3758                 cur->bc_bufs[level] = lbp;
3759                 cur->bc_ptrs[level] += lrecs;
3760                 cur->bc_ra[level] = 0;
3761         }
3762         /*
3763          * If we joined with the right neighbor and there's a level above
3764          * us, increment the cursor at that level.
3765          */
3766         else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3767                    (level + 1 < cur->bc_nlevels)) {
3768                 error = xfs_btree_increment(cur, level + 1, &i);
3769                 if (error)
3770                         goto error0;
3771         }
3772
3773         /*
3774          * Readjust the ptr at this level if it's not a leaf, since it's
3775          * still pointing at the deletion point, which makes the cursor
3776          * inconsistent.  If this makes the ptr 0, the caller fixes it up.
3777          * We can't use decrement because it would change the next level up.
3778          */
3779         if (level > 0)
3780                 cur->bc_ptrs[level]--;
3781
3782         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3783         /* Return value means the next level up has something to do. */
3784         *stat = 2;
3785         return 0;
3786
3787 error0:
3788         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3789         if (tcur)
3790                 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3791         return error;
3792 }
3793
3794 /*
3795  * Delete the record pointed to by cur.
3796  * The cursor refers to the place where the record was (could be inserted)
3797  * when the operation returns.
3798  */
3799 int                                     /* error */
3800 xfs_btree_delete(
3801         struct xfs_btree_cur    *cur,
3802         int                     *stat)  /* success/failure */
3803 {
3804         int                     error;  /* error return value */
3805         int                     level;
3806         int                     i;
3807
3808         XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3809
3810         /*
3811          * Go up the tree, starting at leaf level.
3812          *
3813          * If 2 is returned then a join was done; go to the next level.
3814          * Otherwise we are done.
3815          */
3816         for (level = 0, i = 2; i == 2; level++) {
3817                 error = xfs_btree_delrec(cur, level, &i);
3818                 if (error)
3819                         goto error0;
3820         }
3821
3822         if (i == 0) {
3823                 for (level = 1; level < cur->bc_nlevels; level++) {
3824                         if (cur->bc_ptrs[level] == 0) {
3825                                 error = xfs_btree_decrement(cur, level, &i);
3826                                 if (error)
3827                                         goto error0;
3828                                 break;
3829                         }
3830                 }
3831         }
3832
3833         XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3834         *stat = i;
3835         return 0;
3836 error0:
3837         XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3838         return error;
3839 }
3840
3841 /*
3842  * Get the data from the pointed-to record.
3843  */
3844 int                                     /* error */
3845 xfs_btree_get_rec(
3846         struct xfs_btree_cur    *cur,   /* btree cursor */
3847         union xfs_btree_rec     **recp, /* output: btree record */
3848         int                     *stat)  /* output: success/failure */
3849 {
3850         struct xfs_btree_block  *block; /* btree block */
3851         struct xfs_buf          *bp;    /* buffer pointer */
3852         int                     ptr;    /* record number */
3853 #ifdef DEBUG
3854         int                     error;  /* error return value */
3855 #endif
3856
3857         ptr = cur->bc_ptrs[0];
3858         block = xfs_btree_get_block(cur, 0, &bp);
3859
3860 #ifdef DEBUG
3861         error = xfs_btree_check_block(cur, block, 0, bp);
3862         if (error)
3863                 return error;
3864 #endif
3865
3866         /*
3867          * Off the right end or left end, return failure.
3868          */
3869         if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3870                 *stat = 0;
3871                 return 0;
3872         }
3873
3874         /*
3875          * Point to the record and extract its data.
3876          */
3877         *recp = xfs_btree_rec_addr(cur, ptr, block);
3878         *stat = 1;
3879         return 0;
3880 }
3881
3882 /*
3883  * Change the owner of a btree.
3884  *
3885  * The mechanism we use here is ordered buffer logging. Because we don't know
3886  * how many buffers were are going to need to modify, we don't really want to
3887  * have to make transaction reservations for the worst case of every buffer in a
3888  * full size btree as that may be more space that we can fit in the log....
3889  *
3890  * We do the btree walk in the most optimal manner possible - we have sibling
3891  * pointers so we can just walk all the blocks on each level from left to right
3892  * in a single pass, and then move to the next level and do the same. We can
3893  * also do readahead on the sibling pointers to get IO moving more quickly,
3894  * though for slow disks this is unlikely to make much difference to performance
3895  * as the amount of CPU work we have to do before moving to the next block is
3896  * relatively small.
3897  *
3898  * For each btree block that we load, modify the owner appropriately, set the
3899  * buffer as an ordered buffer and log it appropriately. We need to ensure that
3900  * we mark the region we change dirty so that if the buffer is relogged in
3901  * a subsequent transaction the changes we make here as an ordered buffer are
3902  * correctly relogged in that transaction.  If we are in recovery context, then
3903  * just queue the modified buffer as delayed write buffer so the transaction
3904  * recovery completion writes the changes to disk.
3905  */
3906 static int
3907 xfs_btree_block_change_owner(
3908         struct xfs_btree_cur    *cur,
3909         int                     level,
3910         __uint64_t              new_owner,
3911         struct list_head        *buffer_list)
3912 {
3913         struct xfs_btree_block  *block;
3914         struct xfs_buf          *bp;
3915         union xfs_btree_ptr     rptr;
3916
3917         /* do right sibling readahead */
3918         xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3919
3920         /* modify the owner */
3921         block = xfs_btree_get_block(cur, level, &bp);
3922         if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3923                 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3924         else
3925                 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
3926
3927         /*
3928          * If the block is a root block hosted in an inode, we might not have a
3929          * buffer pointer here and we shouldn't attempt to log the change as the
3930          * information is already held in the inode and discarded when the root
3931          * block is formatted into the on-disk inode fork. We still change it,
3932          * though, so everything is consistent in memory.
3933          */
3934         if (bp) {
3935                 if (cur->bc_tp) {
3936                         xfs_trans_ordered_buf(cur->bc_tp, bp);
3937                         xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
3938                 } else {
3939                         xfs_buf_delwri_queue(bp, buffer_list);
3940                 }
3941         } else {
3942                 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3943                 ASSERT(level == cur->bc_nlevels - 1);
3944         }
3945
3946         /* now read rh sibling block for next iteration */
3947         xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3948         if (xfs_btree_ptr_is_null(cur, &rptr))
3949                 return ENOENT;
3950
3951         return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
3952 }
3953
3954 int
3955 xfs_btree_change_owner(
3956         struct xfs_btree_cur    *cur,
3957         __uint64_t              new_owner,
3958         struct list_head        *buffer_list)
3959 {
3960         union xfs_btree_ptr     lptr;
3961         int                     level;
3962         struct xfs_btree_block  *block = NULL;
3963         int                     error = 0;
3964
3965         cur->bc_ops->init_ptr_from_cur(cur, &lptr);
3966
3967         /* for each level */
3968         for (level = cur->bc_nlevels - 1; level >= 0; level--) {
3969                 /* grab the left hand block */
3970                 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
3971                 if (error)
3972                         return error;
3973
3974                 /* readahead the left most block for the next level down */
3975                 if (level > 0) {
3976                         union xfs_btree_ptr     *ptr;
3977
3978                         ptr = xfs_btree_ptr_addr(cur, 1, block);
3979                         xfs_btree_readahead_ptr(cur, ptr, 1);
3980
3981                         /* save for the next iteration of the loop */
3982                         lptr = *ptr;
3983                 }
3984
3985                 /* for each buffer in the level */
3986                 do {
3987                         error = xfs_btree_block_change_owner(cur, level,
3988                                                              new_owner,
3989                                                              buffer_list);
3990                 } while (!error);
3991
3992                 if (error != ENOENT)
3993                         return error;
3994         }
3995
3996         return 0;
3997 }