xfs: remove xfs_flushinval_pages
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / xfs / xfs_btree.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4
LT
22#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4 26#include "xfs_mount.h"
1da177e4 27#include "xfs_bmap_btree.h"
a844f451 28#include "xfs_alloc_btree.h"
1da177e4 29#include "xfs_ialloc_btree.h"
1da177e4
LT
30#include "xfs_dinode.h"
31#include "xfs_inode.h"
38bb7423 32#include "xfs_inode_item.h"
a844f451 33#include "xfs_btree.h"
1da177e4 34#include "xfs_error.h"
0b1b213f 35#include "xfs_trace.h"
1da177e4
LT
36
37/*
38 * Cursor allocation zone.
39 */
40kmem_zone_t *xfs_btree_cur_zone;
41
42/*
43 * Btree magic numbers.
44 */
cdcf4333 45const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
1da177e4
LT
46 XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
47};
48
1da177e4 49
7cc95a82 50STATIC int /* error (0 or EFSCORRUPTED) */
a23f6ef8
CH
51xfs_btree_check_lblock(
52 struct xfs_btree_cur *cur, /* btree cursor */
7cc95a82 53 struct xfs_btree_block *block, /* btree long form block pointer */
a23f6ef8
CH
54 int level, /* level of the btree block */
55 struct xfs_buf *bp) /* buffer for block, if any */
56{
57 int lblock_ok; /* block passes checks */
58 struct xfs_mount *mp; /* file system mount point */
59
60 mp = cur->bc_mp;
61 lblock_ok =
62 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
63 be16_to_cpu(block->bb_level) == level &&
64 be16_to_cpu(block->bb_numrecs) <=
ce5e42db 65 cur->bc_ops->get_maxrecs(cur, level) &&
7cc95a82 66 block->bb_u.l.bb_leftsib &&
69ef921b 67 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
7cc95a82
CH
68 XFS_FSB_SANITY_CHECK(mp,
69 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
70 block->bb_u.l.bb_rightsib &&
69ef921b 71 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
7cc95a82
CH
72 XFS_FSB_SANITY_CHECK(mp,
73 be64_to_cpu(block->bb_u.l.bb_rightsib)));
a23f6ef8
CH
74 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
75 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
76 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
77 if (bp)
0b1b213f 78 trace_xfs_btree_corrupt(bp, _RET_IP_);
a23f6ef8
CH
79 XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
80 mp);
81 return XFS_ERROR(EFSCORRUPTED);
82 }
83 return 0;
84}
85
3cc7524c 86STATIC int /* error (0 or EFSCORRUPTED) */
1da177e4 87xfs_btree_check_sblock(
a23f6ef8 88 struct xfs_btree_cur *cur, /* btree cursor */
7cc95a82 89 struct xfs_btree_block *block, /* btree short form block pointer */
1da177e4 90 int level, /* level of the btree block */
a23f6ef8 91 struct xfs_buf *bp) /* buffer containing block */
1da177e4 92{
a23f6ef8
CH
93 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
94 struct xfs_agf *agf; /* ag. freespace structure */
1da177e4
LT
95 xfs_agblock_t agflen; /* native ag. freespace length */
96 int sblock_ok; /* block passes checks */
97
98 agbp = cur->bc_private.a.agbp;
99 agf = XFS_BUF_TO_AGF(agbp);
16259e7d 100 agflen = be32_to_cpu(agf->agf_length);
1da177e4 101 sblock_ok =
16259e7d
CH
102 be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
103 be16_to_cpu(block->bb_level) == level &&
104 be16_to_cpu(block->bb_numrecs) <=
ce5e42db 105 cur->bc_ops->get_maxrecs(cur, level) &&
69ef921b 106 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
7cc95a82
CH
107 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
108 block->bb_u.s.bb_leftsib &&
69ef921b 109 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
7cc95a82
CH
110 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
111 block->bb_u.s.bb_rightsib;
1da177e4
LT
112 if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
113 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
114 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
115 if (bp)
0b1b213f 116 trace_xfs_btree_corrupt(bp, _RET_IP_);
e0c222c4
ES
117 XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
118 XFS_ERRLEVEL_LOW, cur->bc_mp, block);
1da177e4
LT
119 return XFS_ERROR(EFSCORRUPTED);
120 }
121 return 0;
122}
123
124/*
a23f6ef8
CH
125 * Debug routine: check that block header is ok.
126 */
127int
128xfs_btree_check_block(
129 struct xfs_btree_cur *cur, /* btree cursor */
130 struct xfs_btree_block *block, /* generic btree block pointer */
131 int level, /* level of the btree block */
132 struct xfs_buf *bp) /* buffer containing block, if any */
133{
7cc95a82
CH
134 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
135 return xfs_btree_check_lblock(cur, block, level, bp);
136 else
137 return xfs_btree_check_sblock(cur, block, level, bp);
a23f6ef8
CH
138}
139
140/*
141 * Check that (long) pointer is ok.
142 */
143int /* error (0 or EFSCORRUPTED) */
144xfs_btree_check_lptr(
145 struct xfs_btree_cur *cur, /* btree cursor */
146 xfs_dfsbno_t bno, /* btree block disk address */
147 int level) /* btree block level */
148{
149 XFS_WANT_CORRUPTED_RETURN(
150 level > 0 &&
151 bno != NULLDFSBNO &&
152 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
153 return 0;
154}
155
24ee0e49 156#ifdef DEBUG
a23f6ef8
CH
157/*
158 * Check that (short) pointer is ok.
1da177e4 159 */
3cc7524c 160STATIC int /* error (0 or EFSCORRUPTED) */
1da177e4 161xfs_btree_check_sptr(
a23f6ef8
CH
162 struct xfs_btree_cur *cur, /* btree cursor */
163 xfs_agblock_t bno, /* btree block disk address */
164 int level) /* btree block level */
1da177e4 165{
a23f6ef8 166 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
1da177e4 167
1da177e4
LT
168 XFS_WANT_CORRUPTED_RETURN(
169 level > 0 &&
a23f6ef8
CH
170 bno != NULLAGBLOCK &&
171 bno != 0 &&
172 bno < agblocks);
1da177e4
LT
173 return 0;
174}
175
a23f6ef8
CH
176/*
177 * Check that block ptr is ok.
178 */
3cc7524c 179STATIC int /* error (0 or EFSCORRUPTED) */
a23f6ef8
CH
180xfs_btree_check_ptr(
181 struct xfs_btree_cur *cur, /* btree cursor */
182 union xfs_btree_ptr *ptr, /* btree block disk address */
183 int index, /* offset from ptr to check */
184 int level) /* btree block level */
185{
186 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
187 return xfs_btree_check_lptr(cur,
188 be64_to_cpu((&ptr->l)[index]), level);
189 } else {
190 return xfs_btree_check_sptr(cur,
191 be32_to_cpu((&ptr->s)[index]), level);
192 }
193}
24ee0e49 194#endif
a23f6ef8 195
1da177e4
LT
196/*
197 * Delete the btree cursor.
198 */
199void
200xfs_btree_del_cursor(
201 xfs_btree_cur_t *cur, /* btree cursor */
202 int error) /* del because of error */
203{
204 int i; /* btree level */
205
206 /*
207 * Clear the buffer pointers, and release the buffers.
208 * If we're doing this in the face of an error, we
209 * need to make sure to inspect all of the entries
210 * in the bc_bufs array for buffers to be unlocked.
211 * This is because some of the btree code works from
212 * level n down to 0, and if we get an error along
213 * the way we won't have initialized all the entries
214 * down to 0.
215 */
216 for (i = 0; i < cur->bc_nlevels; i++) {
217 if (cur->bc_bufs[i])
c0e59e1a 218 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
1da177e4
LT
219 else if (!error)
220 break;
221 }
222 /*
223 * Can't free a bmap cursor without having dealt with the
224 * allocated indirect blocks' accounting.
225 */
226 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
227 cur->bc_private.b.allocated == 0);
228 /*
229 * Free the cursor.
230 */
231 kmem_zone_free(xfs_btree_cur_zone, cur);
232}
233
234/*
235 * Duplicate the btree cursor.
236 * Allocate a new one, copy the record, re-get the buffers.
237 */
238int /* error */
239xfs_btree_dup_cursor(
240 xfs_btree_cur_t *cur, /* input cursor */
241 xfs_btree_cur_t **ncur) /* output cursor */
242{
243 xfs_buf_t *bp; /* btree block's buffer pointer */
244 int error; /* error return value */
245 int i; /* level number of btree block */
246 xfs_mount_t *mp; /* mount structure for filesystem */
247 xfs_btree_cur_t *new; /* new cursor value */
248 xfs_trans_t *tp; /* transaction pointer, can be NULL */
249
250 tp = cur->bc_tp;
251 mp = cur->bc_mp;
561f7d17 252
1da177e4
LT
253 /*
254 * Allocate a new cursor like the old one.
255 */
561f7d17
CH
256 new = cur->bc_ops->dup_cursor(cur);
257
1da177e4
LT
258 /*
259 * Copy the record currently in the cursor.
260 */
261 new->bc_rec = cur->bc_rec;
561f7d17 262
1da177e4
LT
263 /*
264 * For each level current, re-get the buffer and copy the ptr value.
265 */
266 for (i = 0; i < new->bc_nlevels; i++) {
267 new->bc_ptrs[i] = cur->bc_ptrs[i];
268 new->bc_ra[i] = cur->bc_ra[i];
269 if ((bp = cur->bc_bufs[i])) {
270 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
271 XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
272 xfs_btree_del_cursor(new, error);
273 *ncur = NULL;
274 return error;
275 }
276 new->bc_bufs[i] = bp;
5a52c2a5 277 ASSERT(!xfs_buf_geterror(bp));
1da177e4
LT
278 } else
279 new->bc_bufs[i] = NULL;
280 }
1da177e4
LT
281 *ncur = new;
282 return 0;
283}
284
65f1eaea
CH
285/*
286 * XFS btree block layout and addressing:
287 *
288 * There are two types of blocks in the btree: leaf and non-leaf blocks.
289 *
290 * The leaf record start with a header then followed by records containing
291 * the values. A non-leaf block also starts with the same header, and
292 * then first contains lookup keys followed by an equal number of pointers
293 * to the btree blocks at the previous level.
294 *
295 * +--------+-------+-------+-------+-------+-------+-------+
296 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
297 * +--------+-------+-------+-------+-------+-------+-------+
298 *
299 * +--------+-------+-------+-------+-------+-------+-------+
300 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
301 * +--------+-------+-------+-------+-------+-------+-------+
302 *
303 * The header is called struct xfs_btree_block for reasons better left unknown
304 * and comes in different versions for short (32bit) and long (64bit) block
305 * pointers. The record and key structures are defined by the btree instances
306 * and opaque to the btree core. The block pointers are simple disk endian
307 * integers, available in a short (32bit) and long (64bit) variant.
308 *
309 * The helpers below calculate the offset of a given record, key or pointer
310 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
311 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
312 * inside the btree block is done using indices starting at one, not zero!
313 */
314
315/*
316 * Return size of the btree block header for this btree instance.
317 */
318static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
319{
320 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
7cc95a82
CH
321 XFS_BTREE_LBLOCK_LEN :
322 XFS_BTREE_SBLOCK_LEN;
65f1eaea
CH
323}
324
325/*
326 * Return size of btree block pointers for this btree instance.
327 */
328static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
329{
330 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
331 sizeof(__be64) : sizeof(__be32);
332}
333
334/*
335 * Calculate offset of the n-th record in a btree block.
336 */
337STATIC size_t
338xfs_btree_rec_offset(
339 struct xfs_btree_cur *cur,
340 int n)
341{
342 return xfs_btree_block_len(cur) +
343 (n - 1) * cur->bc_ops->rec_len;
344}
345
346/*
347 * Calculate offset of the n-th key in a btree block.
348 */
349STATIC size_t
350xfs_btree_key_offset(
351 struct xfs_btree_cur *cur,
352 int n)
353{
354 return xfs_btree_block_len(cur) +
355 (n - 1) * cur->bc_ops->key_len;
356}
357
358/*
359 * Calculate offset of the n-th block pointer in a btree block.
360 */
361STATIC size_t
362xfs_btree_ptr_offset(
363 struct xfs_btree_cur *cur,
364 int n,
365 int level)
366{
367 return xfs_btree_block_len(cur) +
368 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
369 (n - 1) * xfs_btree_ptr_len(cur);
370}
371
372/*
373 * Return a pointer to the n-th record in the btree block.
374 */
375STATIC union xfs_btree_rec *
376xfs_btree_rec_addr(
377 struct xfs_btree_cur *cur,
378 int n,
379 struct xfs_btree_block *block)
380{
381 return (union xfs_btree_rec *)
382 ((char *)block + xfs_btree_rec_offset(cur, n));
383}
384
385/*
386 * Return a pointer to the n-th key in the btree block.
387 */
388STATIC union xfs_btree_key *
389xfs_btree_key_addr(
390 struct xfs_btree_cur *cur,
391 int n,
392 struct xfs_btree_block *block)
393{
394 return (union xfs_btree_key *)
395 ((char *)block + xfs_btree_key_offset(cur, n));
396}
397
398/*
399 * Return a pointer to the n-th block pointer in the btree block.
400 */
401STATIC union xfs_btree_ptr *
402xfs_btree_ptr_addr(
403 struct xfs_btree_cur *cur,
404 int n,
405 struct xfs_btree_block *block)
406{
407 int level = xfs_btree_get_level(block);
408
409 ASSERT(block->bb_level != 0);
410
411 return (union xfs_btree_ptr *)
412 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
413}
414
8186e517
CH
415/*
416 * Get a the root block which is stored in the inode.
417 *
418 * For now this btree implementation assumes the btree root is always
419 * stored in the if_broot field of an inode fork.
420 */
421STATIC struct xfs_btree_block *
422xfs_btree_get_iroot(
423 struct xfs_btree_cur *cur)
424{
425 struct xfs_ifork *ifp;
426
427 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
428 return (struct xfs_btree_block *)ifp->if_broot;
429}
430
1da177e4
LT
431/*
432 * Retrieve the block pointer from the cursor at the given level.
8186e517 433 * This may be an inode btree root or from a buffer.
1da177e4 434 */
8186e517 435STATIC struct xfs_btree_block * /* generic btree block pointer */
1da177e4 436xfs_btree_get_block(
8186e517 437 struct xfs_btree_cur *cur, /* btree cursor */
1da177e4 438 int level, /* level in btree */
8186e517 439 struct xfs_buf **bpp) /* buffer containing the block */
1da177e4 440{
8186e517
CH
441 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
442 (level == cur->bc_nlevels - 1)) {
443 *bpp = NULL;
444 return xfs_btree_get_iroot(cur);
1da177e4 445 }
8186e517
CH
446
447 *bpp = cur->bc_bufs[level];
448 return XFS_BUF_TO_BLOCK(*bpp);
1da177e4
LT
449}
450
451/*
452 * Get a buffer for the block, return it with no data read.
453 * Long-form addressing.
454 */
455xfs_buf_t * /* buffer for fsbno */
456xfs_btree_get_bufl(
457 xfs_mount_t *mp, /* file system mount point */
458 xfs_trans_t *tp, /* transaction pointer */
459 xfs_fsblock_t fsbno, /* file system block number */
460 uint lock) /* lock flags for get_buf */
461{
462 xfs_buf_t *bp; /* buffer pointer (return value) */
463 xfs_daddr_t d; /* real disk block address */
464
465 ASSERT(fsbno != NULLFSBLOCK);
466 d = XFS_FSB_TO_DADDR(mp, fsbno);
467 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
5a52c2a5 468 ASSERT(!xfs_buf_geterror(bp));
1da177e4
LT
469 return bp;
470}
471
472/*
473 * Get a buffer for the block, return it with no data read.
474 * Short-form addressing.
475 */
476xfs_buf_t * /* buffer for agno/agbno */
477xfs_btree_get_bufs(
478 xfs_mount_t *mp, /* file system mount point */
479 xfs_trans_t *tp, /* transaction pointer */
480 xfs_agnumber_t agno, /* allocation group number */
481 xfs_agblock_t agbno, /* allocation group block number */
482 uint lock) /* lock flags for get_buf */
483{
484 xfs_buf_t *bp; /* buffer pointer (return value) */
485 xfs_daddr_t d; /* real disk block address */
486
487 ASSERT(agno != NULLAGNUMBER);
488 ASSERT(agbno != NULLAGBLOCK);
489 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
490 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
5a52c2a5 491 ASSERT(!xfs_buf_geterror(bp));
1da177e4
LT
492 return bp;
493}
494
1da177e4
LT
495/*
496 * Check for the cursor referring to the last block at the given level.
497 */
498int /* 1=is last block, 0=not last block */
499xfs_btree_islastblock(
500 xfs_btree_cur_t *cur, /* btree cursor */
501 int level) /* level to check */
502{
7cc95a82 503 struct xfs_btree_block *block; /* generic btree block pointer */
1da177e4
LT
504 xfs_buf_t *bp; /* buffer containing block */
505
506 block = xfs_btree_get_block(cur, level, &bp);
507 xfs_btree_check_block(cur, block, level, bp);
e99ab90d 508 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
69ef921b 509 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
1da177e4 510 else
69ef921b 511 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
1da177e4
LT
512}
513
cdcf4333
CH
514/*
515 * Change the cursor to point to the first record at the given level.
516 * Other levels are unaffected.
517 */
3cc7524c 518STATIC int /* success=1, failure=0 */
cdcf4333
CH
519xfs_btree_firstrec(
520 xfs_btree_cur_t *cur, /* btree cursor */
521 int level) /* level to change */
522{
7cc95a82 523 struct xfs_btree_block *block; /* generic btree block pointer */
cdcf4333
CH
524 xfs_buf_t *bp; /* buffer containing block */
525
526 /*
527 * Get the block pointer for this level.
528 */
529 block = xfs_btree_get_block(cur, level, &bp);
530 xfs_btree_check_block(cur, block, level, bp);
531 /*
532 * It's empty, there is no such record.
533 */
f2277f06 534 if (!block->bb_numrecs)
cdcf4333
CH
535 return 0;
536 /*
537 * Set the ptr value to 1, that's the first record/key.
538 */
539 cur->bc_ptrs[level] = 1;
540 return 1;
541}
542
1da177e4
LT
543/*
544 * Change the cursor to point to the last record in the current block
545 * at the given level. Other levels are unaffected.
546 */
3cc7524c 547STATIC int /* success=1, failure=0 */
1da177e4
LT
548xfs_btree_lastrec(
549 xfs_btree_cur_t *cur, /* btree cursor */
550 int level) /* level to change */
551{
7cc95a82 552 struct xfs_btree_block *block; /* generic btree block pointer */
1da177e4
LT
553 xfs_buf_t *bp; /* buffer containing block */
554
555 /*
556 * Get the block pointer for this level.
557 */
558 block = xfs_btree_get_block(cur, level, &bp);
559 xfs_btree_check_block(cur, block, level, bp);
560 /*
561 * It's empty, there is no such record.
562 */
f2277f06 563 if (!block->bb_numrecs)
1da177e4
LT
564 return 0;
565 /*
566 * Set the ptr value to numrecs, that's the last record/key.
567 */
f2277f06 568 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
1da177e4
LT
569 return 1;
570}
571
572/*
573 * Compute first and last byte offsets for the fields given.
574 * Interprets the offsets table, which contains struct field offsets.
575 */
576void
577xfs_btree_offsets(
578 __int64_t fields, /* bitmask of fields */
579 const short *offsets, /* table of field offsets */
580 int nbits, /* number of bits to inspect */
581 int *first, /* output: first byte offset */
582 int *last) /* output: last byte offset */
583{
584 int i; /* current bit number */
585 __int64_t imask; /* mask for current bit number */
586
587 ASSERT(fields != 0);
588 /*
589 * Find the lowest bit, so the first byte offset.
590 */
591 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
592 if (imask & fields) {
593 *first = offsets[i];
594 break;
595 }
596 }
597 /*
598 * Find the highest bit, so the last byte offset.
599 */
600 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
601 if (imask & fields) {
602 *last = offsets[i + 1] - 1;
603 break;
604 }
605 }
606}
607
608/*
609 * Get a buffer for the block, return it read in.
610 * Long-form addressing.
611 */
612int /* error */
613xfs_btree_read_bufl(
614 xfs_mount_t *mp, /* file system mount point */
615 xfs_trans_t *tp, /* transaction pointer */
616 xfs_fsblock_t fsbno, /* file system block number */
617 uint lock, /* lock flags for read_buf */
618 xfs_buf_t **bpp, /* buffer for fsbno */
619 int refval) /* ref count value for buffer */
620{
621 xfs_buf_t *bp; /* return value */
622 xfs_daddr_t d; /* real disk block address */
623 int error;
624
625 ASSERT(fsbno != NULLFSBLOCK);
626 d = XFS_FSB_TO_DADDR(mp, fsbno);
627 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
628 mp->m_bsize, lock, &bp))) {
629 return error;
630 }
5a52c2a5 631 ASSERT(!xfs_buf_geterror(bp));
821eb21d 632 if (bp)
38f23232 633 xfs_buf_set_ref(bp, refval);
1da177e4
LT
634 *bpp = bp;
635 return 0;
636}
637
1da177e4
LT
638/*
639 * Read-ahead the block, don't wait for it, don't return a buffer.
640 * Long-form addressing.
641 */
642/* ARGSUSED */
643void
644xfs_btree_reada_bufl(
645 xfs_mount_t *mp, /* file system mount point */
646 xfs_fsblock_t fsbno, /* file system block number */
647 xfs_extlen_t count) /* count of filesystem blocks */
648{
649 xfs_daddr_t d;
650
651 ASSERT(fsbno != NULLFSBLOCK);
652 d = XFS_FSB_TO_DADDR(mp, fsbno);
1a1a3e97 653 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
1da177e4
LT
654}
655
656/*
657 * Read-ahead the block, don't wait for it, don't return a buffer.
658 * Short-form addressing.
659 */
660/* ARGSUSED */
661void
662xfs_btree_reada_bufs(
663 xfs_mount_t *mp, /* file system mount point */
664 xfs_agnumber_t agno, /* allocation group number */
665 xfs_agblock_t agbno, /* allocation group block number */
666 xfs_extlen_t count) /* count of filesystem blocks */
667{
668 xfs_daddr_t d;
669
670 ASSERT(agno != NULLAGNUMBER);
671 ASSERT(agbno != NULLAGBLOCK);
672 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
1a1a3e97 673 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
1da177e4
LT
674}
675
b524bfee
CH
676STATIC int
677xfs_btree_readahead_lblock(
678 struct xfs_btree_cur *cur,
679 int lr,
680 struct xfs_btree_block *block)
681{
682 int rval = 0;
e6edbd1c
CH
683 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
684 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
b524bfee
CH
685
686 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
687 xfs_btree_reada_bufl(cur->bc_mp, left, 1);
688 rval++;
689 }
690
691 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
692 xfs_btree_reada_bufl(cur->bc_mp, right, 1);
693 rval++;
694 }
695
696 return rval;
697}
698
699STATIC int
700xfs_btree_readahead_sblock(
701 struct xfs_btree_cur *cur,
702 int lr,
703 struct xfs_btree_block *block)
704{
705 int rval = 0;
706 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
707 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
708
709
710 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
711 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
712 left, 1);
713 rval++;
714 }
715
716 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
717 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
718 right, 1);
719 rval++;
720 }
721
722 return rval;
723}
724
1da177e4
LT
725/*
726 * Read-ahead btree blocks, at the given level.
727 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
728 */
3cc7524c 729STATIC int
b524bfee
CH
730xfs_btree_readahead(
731 struct xfs_btree_cur *cur, /* btree cursor */
1da177e4
LT
732 int lev, /* level in btree */
733 int lr) /* left/right bits */
734{
b524bfee
CH
735 struct xfs_btree_block *block;
736
737 /*
738 * No readahead needed if we are at the root level and the
739 * btree root is stored in the inode.
740 */
741 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
742 (lev == cur->bc_nlevels - 1))
743 return 0;
744
745 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
746 return 0;
1da177e4 747
1da177e4 748 cur->bc_ra[lev] |= lr;
b524bfee
CH
749 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
750
751 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
752 return xfs_btree_readahead_lblock(cur, lr, block);
753 return xfs_btree_readahead_sblock(cur, lr, block);
1da177e4
LT
754}
755
756/*
757 * Set the buffer for level "lev" in the cursor to bp, releasing
758 * any previous buffer.
759 */
c0e59e1a 760STATIC void
1da177e4
LT
761xfs_btree_setbuf(
762 xfs_btree_cur_t *cur, /* btree cursor */
763 int lev, /* level in btree */
764 xfs_buf_t *bp) /* new buffer to set */
765{
7cc95a82 766 struct xfs_btree_block *b; /* btree block */
1da177e4 767
c0e59e1a
CH
768 if (cur->bc_bufs[lev])
769 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
1da177e4
LT
770 cur->bc_bufs[lev] = bp;
771 cur->bc_ra[lev] = 0;
c0e59e1a 772
1da177e4 773 b = XFS_BUF_TO_BLOCK(bp);
e99ab90d 774 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
69ef921b 775 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
1da177e4 776 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
69ef921b 777 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
1da177e4
LT
778 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
779 } else {
69ef921b 780 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
1da177e4 781 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
69ef921b 782 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
1da177e4
LT
783 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
784 }
785}
637aa50f
CH
786
787STATIC int
788xfs_btree_ptr_is_null(
789 struct xfs_btree_cur *cur,
790 union xfs_btree_ptr *ptr)
791{
792 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
69ef921b 793 return ptr->l == cpu_to_be64(NULLDFSBNO);
637aa50f 794 else
69ef921b 795 return ptr->s == cpu_to_be32(NULLAGBLOCK);
637aa50f
CH
796}
797
4b22a571
CH
798STATIC void
799xfs_btree_set_ptr_null(
800 struct xfs_btree_cur *cur,
801 union xfs_btree_ptr *ptr)
802{
803 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
33ad965d 804 ptr->l = cpu_to_be64(NULLDFSBNO);
4b22a571
CH
805 else
806 ptr->s = cpu_to_be32(NULLAGBLOCK);
807}
808
637aa50f
CH
809/*
810 * Get/set/init sibling pointers
811 */
812STATIC void
813xfs_btree_get_sibling(
814 struct xfs_btree_cur *cur,
815 struct xfs_btree_block *block,
816 union xfs_btree_ptr *ptr,
817 int lr)
818{
819 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
820
821 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
822 if (lr == XFS_BB_RIGHTSIB)
823 ptr->l = block->bb_u.l.bb_rightsib;
824 else
825 ptr->l = block->bb_u.l.bb_leftsib;
826 } else {
827 if (lr == XFS_BB_RIGHTSIB)
828 ptr->s = block->bb_u.s.bb_rightsib;
829 else
830 ptr->s = block->bb_u.s.bb_leftsib;
831 }
832}
833
f5eb8e7c
CH
834STATIC void
835xfs_btree_set_sibling(
836 struct xfs_btree_cur *cur,
837 struct xfs_btree_block *block,
838 union xfs_btree_ptr *ptr,
839 int lr)
840{
841 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
842
843 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
844 if (lr == XFS_BB_RIGHTSIB)
845 block->bb_u.l.bb_rightsib = ptr->l;
846 else
847 block->bb_u.l.bb_leftsib = ptr->l;
848 } else {
849 if (lr == XFS_BB_RIGHTSIB)
850 block->bb_u.s.bb_rightsib = ptr->s;
851 else
852 block->bb_u.s.bb_leftsib = ptr->s;
853 }
854}
855
b64f3a39 856void
f5eb8e7c 857xfs_btree_init_block(
b64f3a39
DC
858 struct xfs_mount *mp,
859 struct xfs_buf *bp,
860 __u32 magic,
861 __u16 level,
862 __u16 numrecs,
863 unsigned int flags)
f5eb8e7c 864{
b64f3a39
DC
865 struct xfs_btree_block *new = XFS_BUF_TO_BLOCK(bp);
866
867 new->bb_magic = cpu_to_be32(magic);
f5eb8e7c
CH
868 new->bb_level = cpu_to_be16(level);
869 new->bb_numrecs = cpu_to_be16(numrecs);
870
b64f3a39 871 if (flags & XFS_BTREE_LONG_PTRS) {
33ad965d
DC
872 new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
873 new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
f5eb8e7c
CH
874 } else {
875 new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
876 new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
877 }
878}
879
b64f3a39
DC
880STATIC void
881xfs_btree_init_block_cur(
882 struct xfs_btree_cur *cur,
883 int level,
884 int numrecs,
885 struct xfs_buf *bp)
886{
887 xfs_btree_init_block(cur->bc_mp, bp, xfs_magics[cur->bc_btnum],
888 level, numrecs, cur->bc_flags);
889}
890
278d0ca1
CH
891/*
892 * Return true if ptr is the last record in the btree and
893 * we need to track updateѕ to this record. The decision
894 * will be further refined in the update_lastrec method.
895 */
896STATIC int
897xfs_btree_is_lastrec(
898 struct xfs_btree_cur *cur,
899 struct xfs_btree_block *block,
900 int level)
901{
902 union xfs_btree_ptr ptr;
903
904 if (level > 0)
905 return 0;
906 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
907 return 0;
908
909 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
910 if (!xfs_btree_ptr_is_null(cur, &ptr))
911 return 0;
912 return 1;
913}
914
f5eb8e7c
CH
915STATIC void
916xfs_btree_buf_to_ptr(
917 struct xfs_btree_cur *cur,
918 struct xfs_buf *bp,
919 union xfs_btree_ptr *ptr)
920{
921 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
922 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
923 XFS_BUF_ADDR(bp)));
924 else {
9d87c319 925 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
f5eb8e7c
CH
926 XFS_BUF_ADDR(bp)));
927 }
928}
929
637aa50f
CH
930STATIC xfs_daddr_t
931xfs_btree_ptr_to_daddr(
932 struct xfs_btree_cur *cur,
933 union xfs_btree_ptr *ptr)
934{
935 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
69ef921b 936 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
637aa50f
CH
937
938 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
939 } else {
940 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
69ef921b 941 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
637aa50f
CH
942
943 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
944 be32_to_cpu(ptr->s));
945 }
946}
947
948STATIC void
949xfs_btree_set_refs(
950 struct xfs_btree_cur *cur,
951 struct xfs_buf *bp)
952{
953 switch (cur->bc_btnum) {
954 case XFS_BTNUM_BNO:
955 case XFS_BTNUM_CNT:
38f23232 956 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
637aa50f
CH
957 break;
958 case XFS_BTNUM_INO:
38f23232 959 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
637aa50f
CH
960 break;
961 case XFS_BTNUM_BMAP:
38f23232 962 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
637aa50f
CH
963 break;
964 default:
965 ASSERT(0);
966 }
967}
968
f5eb8e7c
CH
969STATIC int
970xfs_btree_get_buf_block(
971 struct xfs_btree_cur *cur,
972 union xfs_btree_ptr *ptr,
973 int flags,
974 struct xfs_btree_block **block,
975 struct xfs_buf **bpp)
976{
977 struct xfs_mount *mp = cur->bc_mp;
978 xfs_daddr_t d;
979
980 /* need to sort out how callers deal with failures first */
0cadda1c 981 ASSERT(!(flags & XBF_TRYLOCK));
f5eb8e7c
CH
982
983 d = xfs_btree_ptr_to_daddr(cur, ptr);
984 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
985 mp->m_bsize, flags);
986
2a30f36d
CS
987 if (!*bpp)
988 return ENOMEM;
f5eb8e7c
CH
989
990 *block = XFS_BUF_TO_BLOCK(*bpp);
991 return 0;
992}
993
637aa50f
CH
994/*
995 * Read in the buffer at the given ptr and return the buffer and
996 * the block pointer within the buffer.
997 */
998STATIC int
999xfs_btree_read_buf_block(
1000 struct xfs_btree_cur *cur,
1001 union xfs_btree_ptr *ptr,
1002 int level,
1003 int flags,
1004 struct xfs_btree_block **block,
1005 struct xfs_buf **bpp)
1006{
1007 struct xfs_mount *mp = cur->bc_mp;
1008 xfs_daddr_t d;
1009 int error;
1010
1011 /* need to sort out how callers deal with failures first */
0cadda1c 1012 ASSERT(!(flags & XBF_TRYLOCK));
637aa50f
CH
1013
1014 d = xfs_btree_ptr_to_daddr(cur, ptr);
1015 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
1016 mp->m_bsize, flags, bpp);
1017 if (error)
1018 return error;
1019
5a52c2a5 1020 ASSERT(!xfs_buf_geterror(*bpp));
637aa50f
CH
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
38bb7423
CH
1031/*
1032 * 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
278d0ca1
CH
1045/*
1046 * 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
9eaead51
CH
1059/*
1060 * 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
38bb7423
CH
1130/*
1131 * 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
278d0ca1
CH
1155/*
1156 * Log record values from the btree block.
1157 */
fd6bcc5b 1158void
278d0ca1
CH
1159xfs_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
9eaead51
CH
1175/*
1176 * 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 */
fd6bcc5b 1206void
9eaead51
CH
1207xfs_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) */
7cc95a82
CH
1215 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
9eaead51
CH
1221 };
1222 static const short loffsets[] = { /* table of offsets (long) */
7cc95a82
CH
1223 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
9eaead51
CH
1229 };
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
637aa50f
CH
1248/*
1249 * 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}
8df4da4a
CH
1355
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
fe033cc8
CH
1454STATIC 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}
38bb7423
CH
1672
1673/*
1674 * Update keys at all levels from here to the root along the cursor's path.
1675 */
3cc7524c 1676STATIC int
38bb7423
CH
1677xfs_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}
278d0ca1
CH
1719
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
687b890a
CH
1782/*
1783 * Move 1 record left from cur/level if possible.
1784 * Update cur to reflect the new path.
1785 */
3cc7524c 1786STATIC int /* error */
687b890a
CH
1787xfs_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.
9da096fd 1847 * Account for it here, the changes will be updated on disk and logged
687b890a
CH
1848 * 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
4a26e66e
CH
1881 ASSERT(cur->bc_ops->keys_inorder(cur,
1882 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
687b890a
CH
1883 } 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
4a26e66e
CH
1893 ASSERT(cur->bc_ops->recs_inorder(cur,
1894 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
687b890a
CH
1895 }
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
9eaead51
CH
1965/*
1966 * Move 1 record right from cur/level if possible.
1967 * Update cur to reflect the new path.
1968 */
3cc7524c 1969STATIC int /* error */
9eaead51
CH
1970xfs_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
4a26e66e
CH
2069 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2070 xfs_btree_key_addr(cur, 2, right)));
9eaead51
CH
2071 } 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
4a26e66e
CH
2088 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2089 xfs_btree_rec_addr(cur, 2, right)));
9eaead51
CH
2090 }
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}
f5eb8e7c
CH
2139
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 */
3cc7524c 2145STATIC int /* error */
f5eb8e7c
CH
2146xfs_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. */
b64f3a39 2201 xfs_btree_init_block_cur(cur, xfs_btree_get_level(left), 0, rbp);
f5eb8e7c
CH
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}
344207ce 2325
ea77b0a6
CH
2326/*
2327 * 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 |=
9d87c319 2418 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
ea77b0a6
CH
2419 *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
344207ce
CH
2427/*
2428 * Allocate a new root block, fill it in.
2429 */
3cc7524c 2430STATIC int /* error */
344207ce
CH
2431xfs_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. */
b64f3a39 2510 xfs_btree_init_block_cur(cur, cur->bc_nlevels, 2, nbp);
344207ce
CH
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}
4b22a571
CH
2555
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) {
4a26e66e
CH
2696 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2697 xfs_btree_rec_addr(cur, ptr, block)));
4b22a571 2698 } else {
4a26e66e
CH
2699 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2700 xfs_btree_key_addr(cur, ptr, block)));
4b22a571
CH
2701 }
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) {
4a26e66e
CH
2770 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2771 xfs_btree_key_addr(cur, ptr + 1, block)));
4b22a571
CH
2772 }
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) {
4a26e66e
CH
2788 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2789 xfs_btree_rec_addr(cur, ptr + 1, block)));
4b22a571
CH
2790 }
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}
d4b3a4b7
CH
2906
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 */
d96f8f89 2915STATIC int
d4b3a4b7
CH
2916xfs_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);
7cc95a82 2981 block = ifp->if_broot;
d4b3a4b7
CH
2982 }
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,
9d87c319 3012 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
d4b3a4b7
CH
3013 cur->bc_nlevels--;
3014out0:
3015 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3016 return 0;
3017}
91cca5df 3018
c0e59e1a
CH
3019/*
3020 * Kill the current root node, and replace it with it's only child node.
3021 */
3022STATIC int
3023xfs_btree_kill_root(
3024 struct xfs_btree_cur *cur,
3025 struct xfs_buf *bp,
3026 int level,
3027 union xfs_btree_ptr *newroot)
3028{
3029 int error;
3030
3031 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3032 XFS_BTREE_STATS_INC(cur, killroot);
3033
3034 /*
3035 * Update the root pointer, decreasing the level by 1 and then
3036 * free the old root.
3037 */
3038 cur->bc_ops->set_root(cur, newroot, -1);
3039
3040 error = cur->bc_ops->free_block(cur, bp);
3041 if (error) {
3042 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3043 return error;
3044 }
3045
3046 XFS_BTREE_STATS_INC(cur, free);
3047
3048 cur->bc_bufs[level] = NULL;
3049 cur->bc_ra[level] = 0;
3050 cur->bc_nlevels--;
3051
3052 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3053 return 0;
3054}
3055
91cca5df
CH
3056STATIC int
3057xfs_btree_dec_cursor(
3058 struct xfs_btree_cur *cur,
3059 int level,
3060 int *stat)
3061{
3062 int error;
3063 int i;
3064
3065 if (level > 0) {
3066 error = xfs_btree_decrement(cur, level, &i);
3067 if (error)
3068 return error;
3069 }
3070
3071 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3072 *stat = 1;
3073 return 0;
3074}
3075
3076/*
3077 * Single level of the btree record deletion routine.
3078 * Delete record pointed to by cur/level.
3079 * Remove the record from its block then rebalance the tree.
3080 * Return 0 for error, 1 for done, 2 to go on to the next level.
3081 */
3082STATIC int /* error */
3083xfs_btree_delrec(
3084 struct xfs_btree_cur *cur, /* btree cursor */
3085 int level, /* level removing record from */
3086 int *stat) /* fail/done/go-on */
3087{
3088 struct xfs_btree_block *block; /* btree block */
3089 union xfs_btree_ptr cptr; /* current block ptr */
3090 struct xfs_buf *bp; /* buffer for block */
3091 int error; /* error return value */
3092 int i; /* loop counter */
3093 union xfs_btree_key key; /* storage for keyp */
3094 union xfs_btree_key *keyp = &key; /* passed to the next level */
3095 union xfs_btree_ptr lptr; /* left sibling block ptr */
3096 struct xfs_buf *lbp; /* left buffer pointer */
3097 struct xfs_btree_block *left; /* left btree block */
3098 int lrecs = 0; /* left record count */
3099 int ptr; /* key/record index */
3100 union xfs_btree_ptr rptr; /* right sibling block ptr */
3101 struct xfs_buf *rbp; /* right buffer pointer */
3102 struct xfs_btree_block *right; /* right btree block */
3103 struct xfs_btree_block *rrblock; /* right-right btree block */
3104 struct xfs_buf *rrbp; /* right-right buffer pointer */
3105 int rrecs = 0; /* right record count */
3106 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3107 int numrecs; /* temporary numrec count */
3108
3109 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3110 XFS_BTREE_TRACE_ARGI(cur, level);
3111
3112 tcur = NULL;
3113
3114 /* Get the index of the entry being deleted, check for nothing there. */
3115 ptr = cur->bc_ptrs[level];
3116 if (ptr == 0) {
3117 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3118 *stat = 0;
3119 return 0;
3120 }
3121
3122 /* Get the buffer & block containing the record or key/ptr. */
3123 block = xfs_btree_get_block(cur, level, &bp);
3124 numrecs = xfs_btree_get_numrecs(block);
3125
3126#ifdef DEBUG
3127 error = xfs_btree_check_block(cur, block, level, bp);
3128 if (error)
3129 goto error0;
3130#endif
3131
3132 /* Fail if we're off the end of the block. */
3133 if (ptr > numrecs) {
3134 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3135 *stat = 0;
3136 return 0;
3137 }
3138
3139 XFS_BTREE_STATS_INC(cur, delrec);
3140 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3141
3142 /* Excise the entries being deleted. */
3143 if (level > 0) {
3144 /* It's a nonleaf. operate on keys and ptrs */
3145 union xfs_btree_key *lkp;
3146 union xfs_btree_ptr *lpp;
3147
3148 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3149 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3150
3151#ifdef DEBUG
3152 for (i = 0; i < numrecs - ptr; i++) {
3153 error = xfs_btree_check_ptr(cur, lpp, i, level);
3154 if (error)
3155 goto error0;
3156 }
3157#endif
3158
3159 if (ptr < numrecs) {
3160 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3161 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3162 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3163 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3164 }
3165
3166 /*
3167 * If it's the first record in the block, we'll need to pass a
3168 * key up to the next level (updkey).
3169 */
3170 if (ptr == 1)
3171 keyp = xfs_btree_key_addr(cur, 1, block);
3172 } else {
3173 /* It's a leaf. operate on records */
3174 if (ptr < numrecs) {
3175 xfs_btree_shift_recs(cur,
3176 xfs_btree_rec_addr(cur, ptr + 1, block),
3177 -1, numrecs - ptr);
3178 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3179 }
3180
3181 /*
3182 * If it's the first record in the block, we'll need a key
3183 * structure to pass up to the next level (updkey).
3184 */
3185 if (ptr == 1) {
3186 cur->bc_ops->init_key_from_rec(&key,
3187 xfs_btree_rec_addr(cur, 1, block));
3188 keyp = &key;
3189 }
3190 }
3191
3192 /*
3193 * Decrement and log the number of entries in the block.
3194 */
3195 xfs_btree_set_numrecs(block, --numrecs);
3196 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3197
3198 /*
3199 * If we are tracking the last record in the tree and
3200 * we are at the far right edge of the tree, update it.
3201 */
3202 if (xfs_btree_is_lastrec(cur, block, level)) {
3203 cur->bc_ops->update_lastrec(cur, block, NULL,
3204 ptr, LASTREC_DELREC);
3205 }
3206
3207 /*
3208 * We're at the root level. First, shrink the root block in-memory.
3209 * Try to get rid of the next level down. If we can't then there's
3210 * nothing left to do.
3211 */
3212 if (level == cur->bc_nlevels - 1) {
3213 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3214 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3215 cur->bc_private.b.whichfork);
3216
3217 error = xfs_btree_kill_iroot(cur);
3218 if (error)
3219 goto error0;
3220
3221 error = xfs_btree_dec_cursor(cur, level, stat);
3222 if (error)
3223 goto error0;
3224 *stat = 1;
3225 return 0;
3226 }
3227
3228 /*
3229 * If this is the root level, and there's only one entry left,
3230 * and it's NOT the leaf level, then we can get rid of this
3231 * level.
3232 */
3233 if (numrecs == 1 && level > 0) {
3234 union xfs_btree_ptr *pp;
3235 /*
3236 * pp is still set to the first pointer in the block.
3237 * Make it the new root of the btree.
3238 */
3239 pp = xfs_btree_ptr_addr(cur, 1, block);
c0e59e1a 3240 error = xfs_btree_kill_root(cur, bp, level, pp);
91cca5df
CH
3241 if (error)
3242 goto error0;
3243 } else if (level > 0) {
3244 error = xfs_btree_dec_cursor(cur, level, stat);
3245 if (error)
3246 goto error0;
3247 }
3248 *stat = 1;
3249 return 0;
3250 }
3251
3252 /*
3253 * If we deleted the leftmost entry in the block, update the
3254 * key values above us in the tree.
3255 */
3256 if (ptr == 1) {
3257 error = xfs_btree_updkey(cur, keyp, level + 1);
3258 if (error)
3259 goto error0;
3260 }
3261
3262 /*
3263 * If the number of records remaining in the block is at least
3264 * the minimum, we're done.
3265 */
3266 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3267 error = xfs_btree_dec_cursor(cur, level, stat);
3268 if (error)
3269 goto error0;
3270 return 0;
3271 }
3272
3273 /*
3274 * Otherwise, we have to move some records around to keep the
3275 * tree balanced. Look at the left and right sibling blocks to
3276 * see if we can re-balance by moving only one record.
3277 */
3278 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3279 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3280
3281 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3282 /*
3283 * One child of root, need to get a chance to copy its contents
3284 * into the root and delete it. Can't go up to next level,
3285 * there's nothing to delete there.
3286 */
3287 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3288 xfs_btree_ptr_is_null(cur, &lptr) &&
3289 level == cur->bc_nlevels - 2) {
3290 error = xfs_btree_kill_iroot(cur);
3291 if (!error)
3292 error = xfs_btree_dec_cursor(cur, level, stat);
3293 if (error)
3294 goto error0;
3295 return 0;
3296 }
3297 }
3298
3299 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3300 !xfs_btree_ptr_is_null(cur, &lptr));
3301
3302 /*
3303 * Duplicate the cursor so our btree manipulations here won't
3304 * disrupt the next level up.
3305 */
3306 error = xfs_btree_dup_cursor(cur, &tcur);
3307 if (error)
3308 goto error0;
3309
3310 /*
3311 * If there's a right sibling, see if it's ok to shift an entry
3312 * out of it.
3313 */
3314 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3315 /*
3316 * Move the temp cursor to the last entry in the next block.
3317 * Actually any entry but the first would suffice.
3318 */
3319 i = xfs_btree_lastrec(tcur, level);
3320 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3321
3322 error = xfs_btree_increment(tcur, level, &i);
3323 if (error)
3324 goto error0;
3325 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3326
3327 i = xfs_btree_lastrec(tcur, level);
3328 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3329
3330 /* Grab a pointer to the block. */
3331 right = xfs_btree_get_block(tcur, level, &rbp);
3332#ifdef DEBUG
3333 error = xfs_btree_check_block(tcur, right, level, rbp);
3334 if (error)
3335 goto error0;
3336#endif
3337 /* Grab the current block number, for future use. */
3338 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3339
3340 /*
3341 * If right block is full enough so that removing one entry
3342 * won't make it too empty, and left-shifting an entry out
3343 * of right to us works, we're done.
3344 */
3345 if (xfs_btree_get_numrecs(right) - 1 >=
3346 cur->bc_ops->get_minrecs(tcur, level)) {
3347 error = xfs_btree_lshift(tcur, level, &i);
3348 if (error)
3349 goto error0;
3350 if (i) {
3351 ASSERT(xfs_btree_get_numrecs(block) >=
3352 cur->bc_ops->get_minrecs(tcur, level));
3353
3354 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3355 tcur = NULL;
3356
3357 error = xfs_btree_dec_cursor(cur, level, stat);
3358 if (error)
3359 goto error0;
3360 return 0;
3361 }
3362 }
3363
3364 /*
3365 * Otherwise, grab the number of records in right for
3366 * future reference, and fix up the temp cursor to point
3367 * to our block again (last record).
3368 */
3369 rrecs = xfs_btree_get_numrecs(right);
3370 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3371 i = xfs_btree_firstrec(tcur, level);
3372 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3373
3374 error = xfs_btree_decrement(tcur, level, &i);
3375 if (error)
3376 goto error0;
3377 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3378 }
3379 }
3380
3381 /*
3382 * If there's a left sibling, see if it's ok to shift an entry
3383 * out of it.
3384 */
3385 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3386 /*
3387 * Move the temp cursor to the first entry in the
3388 * previous block.
3389 */
3390 i = xfs_btree_firstrec(tcur, level);
3391 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3392
3393 error = xfs_btree_decrement(tcur, level, &i);
3394 if (error)
3395 goto error0;
3396 i = xfs_btree_firstrec(tcur, level);
3397 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3398
3399 /* Grab a pointer to the block. */
3400 left = xfs_btree_get_block(tcur, level, &lbp);
3401#ifdef DEBUG
3402 error = xfs_btree_check_block(cur, left, level, lbp);
3403 if (error)
3404 goto error0;
3405#endif
3406 /* Grab the current block number, for future use. */
3407 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3408
3409 /*
3410 * If left block is full enough so that removing one entry
3411 * won't make it too empty, and right-shifting an entry out
3412 * of left to us works, we're done.
3413 */
3414 if (xfs_btree_get_numrecs(left) - 1 >=
3415 cur->bc_ops->get_minrecs(tcur, level)) {
3416 error = xfs_btree_rshift(tcur, level, &i);
3417 if (error)
3418 goto error0;
3419 if (i) {
3420 ASSERT(xfs_btree_get_numrecs(block) >=
3421 cur->bc_ops->get_minrecs(tcur, level));
3422 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3423 tcur = NULL;
3424 if (level == 0)
3425 cur->bc_ptrs[0]++;
3426 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3427 *stat = 1;
3428 return 0;
3429 }
3430 }
3431
3432 /*
3433 * Otherwise, grab the number of records in right for
3434 * future reference.
3435 */
3436 lrecs = xfs_btree_get_numrecs(left);
3437 }
3438
3439 /* Delete the temp cursor, we're done with it. */
3440 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3441 tcur = NULL;
3442
3443 /* If here, we need to do a join to keep the tree balanced. */
3444 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3445
3446 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3447 lrecs + xfs_btree_get_numrecs(block) <=
3448 cur->bc_ops->get_maxrecs(cur, level)) {
3449 /*
3450 * Set "right" to be the starting block,
3451 * "left" to be the left neighbor.
3452 */
3453 rptr = cptr;
3454 right = block;
3455 rbp = bp;
3456 error = xfs_btree_read_buf_block(cur, &lptr, level,
3457 0, &left, &lbp);
3458 if (error)
3459 goto error0;
3460
3461 /*
3462 * If that won't work, see if we can join with the right neighbor block.
3463 */
3464 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3465 rrecs + xfs_btree_get_numrecs(block) <=
3466 cur->bc_ops->get_maxrecs(cur, level)) {
3467 /*
3468 * Set "left" to be the starting block,
3469 * "right" to be the right neighbor.
3470 */
3471 lptr = cptr;
3472 left = block;
3473 lbp = bp;
3474 error = xfs_btree_read_buf_block(cur, &rptr, level,
3475 0, &right, &rbp);
3476 if (error)
3477 goto error0;
3478
3479 /*
3480 * Otherwise, we can't fix the imbalance.
3481 * Just return. This is probably a logic error, but it's not fatal.
3482 */
3483 } else {
3484 error = xfs_btree_dec_cursor(cur, level, stat);
3485 if (error)
3486 goto error0;
3487 return 0;
3488 }
3489
3490 rrecs = xfs_btree_get_numrecs(right);
3491 lrecs = xfs_btree_get_numrecs(left);
3492
3493 /*
3494 * We're now going to join "left" and "right" by moving all the stuff
3495 * in "right" to "left" and deleting "right".
3496 */
3497 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3498 if (level > 0) {
3499 /* It's a non-leaf. Move keys and pointers. */
3500 union xfs_btree_key *lkp; /* left btree key */
3501 union xfs_btree_ptr *lpp; /* left address pointer */
3502 union xfs_btree_key *rkp; /* right btree key */
3503 union xfs_btree_ptr *rpp; /* right address pointer */
3504
3505 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3506 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3507 rkp = xfs_btree_key_addr(cur, 1, right);
3508 rpp = xfs_btree_ptr_addr(cur, 1, right);
3509#ifdef DEBUG
3510 for (i = 1; i < rrecs; i++) {
3511 error = xfs_btree_check_ptr(cur, rpp, i, level);
3512 if (error)
3513 goto error0;
3514 }
3515#endif
3516 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3517 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3518
3519 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3520 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3521 } else {
3522 /* It's a leaf. Move records. */
3523 union xfs_btree_rec *lrp; /* left record pointer */
3524 union xfs_btree_rec *rrp; /* right record pointer */
3525
3526 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3527 rrp = xfs_btree_rec_addr(cur, 1, right);
3528
3529 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3530 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3531 }
3532
3533 XFS_BTREE_STATS_INC(cur, join);
3534
3535 /*
9da096fd 3536 * Fix up the number of records and right block pointer in the
91cca5df
CH
3537 * surviving block, and log it.
3538 */
3539 xfs_btree_set_numrecs(left, lrecs + rrecs);
3540 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3541 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3542 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3543
3544 /* If there is a right sibling, point it to the remaining block. */
3545 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3546 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
3547 error = xfs_btree_read_buf_block(cur, &cptr, level,
3548 0, &rrblock, &rrbp);
3549 if (error)
3550 goto error0;
3551 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3552 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3553 }
3554
3555 /* Free the deleted block. */
3556 error = cur->bc_ops->free_block(cur, rbp);
3557 if (error)
3558 goto error0;
3559 XFS_BTREE_STATS_INC(cur, free);
3560
3561 /*
3562 * If we joined with the left neighbor, set the buffer in the
3563 * cursor to the left block, and fix up the index.
3564 */
3565 if (bp != lbp) {
3566 cur->bc_bufs[level] = lbp;
3567 cur->bc_ptrs[level] += lrecs;
3568 cur->bc_ra[level] = 0;
3569 }
3570 /*
3571 * If we joined with the right neighbor and there's a level above
3572 * us, increment the cursor at that level.
3573 */
3574 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3575 (level + 1 < cur->bc_nlevels)) {
3576 error = xfs_btree_increment(cur, level + 1, &i);
3577 if (error)
3578 goto error0;
3579 }
3580
3581 /*
3582 * Readjust the ptr at this level if it's not a leaf, since it's
3583 * still pointing at the deletion point, which makes the cursor
3584 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3585 * We can't use decrement because it would change the next level up.
3586 */
3587 if (level > 0)
3588 cur->bc_ptrs[level]--;
3589
3590 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3591 /* Return value means the next level up has something to do. */
3592 *stat = 2;
3593 return 0;
3594
3595error0:
3596 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3597 if (tcur)
3598 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3599 return error;
3600}
3601
3602/*
3603 * Delete the record pointed to by cur.
3604 * The cursor refers to the place where the record was (could be inserted)
3605 * when the operation returns.
3606 */
3607int /* error */
3608xfs_btree_delete(
3609 struct xfs_btree_cur *cur,
3610 int *stat) /* success/failure */
3611{
3612 int error; /* error return value */
3613 int level;
3614 int i;
3615
3616 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3617
3618 /*
3619 * Go up the tree, starting at leaf level.
3620 *
3621 * If 2 is returned then a join was done; go to the next level.
3622 * Otherwise we are done.
3623 */
3624 for (level = 0, i = 2; i == 2; level++) {
3625 error = xfs_btree_delrec(cur, level, &i);
3626 if (error)
3627 goto error0;
3628 }
3629
3630 if (i == 0) {
3631 for (level = 1; level < cur->bc_nlevels; level++) {
3632 if (cur->bc_ptrs[level] == 0) {
3633 error = xfs_btree_decrement(cur, level, &i);
3634 if (error)
3635 goto error0;
3636 break;
3637 }
3638 }
3639 }
3640
3641 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3642 *stat = i;
3643 return 0;
3644error0:
3645 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3646 return error;
3647}
8cc938fe
CH
3648
3649/*
3650 * Get the data from the pointed-to record.
3651 */
3652int /* error */
3653xfs_btree_get_rec(
3654 struct xfs_btree_cur *cur, /* btree cursor */
3655 union xfs_btree_rec **recp, /* output: btree record */
3656 int *stat) /* output: success/failure */
3657{
3658 struct xfs_btree_block *block; /* btree block */
3659 struct xfs_buf *bp; /* buffer pointer */
3660 int ptr; /* record number */
3661#ifdef DEBUG
3662 int error; /* error return value */
3663#endif
3664
3665 ptr = cur->bc_ptrs[0];
3666 block = xfs_btree_get_block(cur, 0, &bp);
3667
3668#ifdef DEBUG
3669 error = xfs_btree_check_block(cur, block, 0, bp);
3670 if (error)
3671 return error;
3672#endif
3673
3674 /*
3675 * Off the right end or left end, return failure.
3676 */
3677 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3678 *stat = 0;
3679 return 0;
3680 }
3681
3682 /*
3683 * Point to the record and extract its data.
3684 */
3685 *recp = xfs_btree_rec_addr(cur, ptr, block);
3686 *stat = 1;
3687 return 0;
3688}