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