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