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