xref: /openbmc/linux/fs/xfs/libxfs/xfs_btree.h (revision fadbafc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #ifndef __XFS_BTREE_H__
7 #define	__XFS_BTREE_H__
8 
9 struct xfs_buf;
10 struct xfs_inode;
11 struct xfs_mount;
12 struct xfs_trans;
13 struct xfs_ifork;
14 struct xfs_perag;
15 
16 /*
17  * Generic key, ptr and record wrapper structures.
18  *
19  * These are disk format structures, and are converted where necessary
20  * by the btree specific code that needs to interpret them.
21  */
22 union xfs_btree_ptr {
23 	__be32			s;	/* short form ptr */
24 	__be64			l;	/* long form ptr */
25 };
26 
27 /*
28  * The in-core btree key.  Overlapping btrees actually store two keys
29  * per pointer, so we reserve enough memory to hold both.  The __*bigkey
30  * items should never be accessed directly.
31  */
32 union xfs_btree_key {
33 	struct xfs_bmbt_key		bmbt;
34 	xfs_bmdr_key_t			bmbr;	/* bmbt root block */
35 	xfs_alloc_key_t			alloc;
36 	struct xfs_inobt_key		inobt;
37 	struct xfs_rmap_key		rmap;
38 	struct xfs_rmap_key		__rmap_bigkey[2];
39 	struct xfs_refcount_key		refc;
40 };
41 
42 union xfs_btree_rec {
43 	struct xfs_bmbt_rec		bmbt;
44 	xfs_bmdr_rec_t			bmbr;	/* bmbt root block */
45 	struct xfs_alloc_rec		alloc;
46 	struct xfs_inobt_rec		inobt;
47 	struct xfs_rmap_rec		rmap;
48 	struct xfs_refcount_rec		refc;
49 };
50 
51 /*
52  * This nonsense is to make -wlint happy.
53  */
54 #define	XFS_LOOKUP_EQ	((xfs_lookup_t)XFS_LOOKUP_EQi)
55 #define	XFS_LOOKUP_LE	((xfs_lookup_t)XFS_LOOKUP_LEi)
56 #define	XFS_LOOKUP_GE	((xfs_lookup_t)XFS_LOOKUP_GEi)
57 
58 #define	XFS_BTNUM_BNO	((xfs_btnum_t)XFS_BTNUM_BNOi)
59 #define	XFS_BTNUM_CNT	((xfs_btnum_t)XFS_BTNUM_CNTi)
60 #define	XFS_BTNUM_BMAP	((xfs_btnum_t)XFS_BTNUM_BMAPi)
61 #define	XFS_BTNUM_INO	((xfs_btnum_t)XFS_BTNUM_INOi)
62 #define	XFS_BTNUM_FINO	((xfs_btnum_t)XFS_BTNUM_FINOi)
63 #define	XFS_BTNUM_RMAP	((xfs_btnum_t)XFS_BTNUM_RMAPi)
64 #define	XFS_BTNUM_REFC	((xfs_btnum_t)XFS_BTNUM_REFCi)
65 
66 uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
67 
68 /*
69  * For logging record fields.
70  */
71 #define	XFS_BB_MAGIC		(1u << 0)
72 #define	XFS_BB_LEVEL		(1u << 1)
73 #define	XFS_BB_NUMRECS		(1u << 2)
74 #define	XFS_BB_LEFTSIB		(1u << 3)
75 #define	XFS_BB_RIGHTSIB		(1u << 4)
76 #define	XFS_BB_BLKNO		(1u << 5)
77 #define	XFS_BB_LSN		(1u << 6)
78 #define	XFS_BB_UUID		(1u << 7)
79 #define	XFS_BB_OWNER		(1u << 8)
80 #define	XFS_BB_NUM_BITS		5
81 #define	XFS_BB_ALL_BITS		((1u << XFS_BB_NUM_BITS) - 1)
82 #define	XFS_BB_NUM_BITS_CRC	9
83 #define	XFS_BB_ALL_BITS_CRC	((1u << XFS_BB_NUM_BITS_CRC) - 1)
84 
85 /*
86  * Generic stats interface
87  */
88 #define XFS_BTREE_STATS_INC(cur, stat)	\
89 	XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
90 #define XFS_BTREE_STATS_ADD(cur, stat, val)	\
91 	XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
92 
93 struct xfs_btree_ops {
94 	/* size of the key and record structures */
95 	size_t	key_len;
96 	size_t	rec_len;
97 
98 	/* cursor operations */
99 	struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
100 	void	(*update_cursor)(struct xfs_btree_cur *src,
101 				 struct xfs_btree_cur *dst);
102 
103 	/* update btree root pointer */
104 	void	(*set_root)(struct xfs_btree_cur *cur,
105 			    const union xfs_btree_ptr *nptr, int level_change);
106 
107 	/* block allocation / freeing */
108 	int	(*alloc_block)(struct xfs_btree_cur *cur,
109 			       const union xfs_btree_ptr *start_bno,
110 			       union xfs_btree_ptr *new_bno,
111 			       int *stat);
112 	int	(*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
113 
114 	/* update last record information */
115 	void	(*update_lastrec)(struct xfs_btree_cur *cur,
116 				  const struct xfs_btree_block *block,
117 				  const union xfs_btree_rec *rec,
118 				  int ptr, int reason);
119 
120 	/* records in block/level */
121 	int	(*get_minrecs)(struct xfs_btree_cur *cur, int level);
122 	int	(*get_maxrecs)(struct xfs_btree_cur *cur, int level);
123 
124 	/* records on disk.  Matter for the root in inode case. */
125 	int	(*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
126 
127 	/* init values of btree structures */
128 	void	(*init_key_from_rec)(union xfs_btree_key *key,
129 				     const union xfs_btree_rec *rec);
130 	void	(*init_rec_from_cur)(struct xfs_btree_cur *cur,
131 				     union xfs_btree_rec *rec);
132 	void	(*init_ptr_from_cur)(struct xfs_btree_cur *cur,
133 				     union xfs_btree_ptr *ptr);
134 	void	(*init_high_key_from_rec)(union xfs_btree_key *key,
135 					  const union xfs_btree_rec *rec);
136 
137 	/* difference between key value and cursor value */
138 	int64_t (*key_diff)(struct xfs_btree_cur *cur,
139 			    const union xfs_btree_key *key);
140 
141 	/*
142 	 * Difference between key2 and key1 -- positive if key1 > key2,
143 	 * negative if key1 < key2, and zero if equal.
144 	 */
145 	int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
146 				 const union xfs_btree_key *key1,
147 				 const union xfs_btree_key *key2);
148 
149 	const struct xfs_buf_ops	*buf_ops;
150 
151 	/* check that k1 is lower than k2 */
152 	int	(*keys_inorder)(struct xfs_btree_cur *cur,
153 				const union xfs_btree_key *k1,
154 				const union xfs_btree_key *k2);
155 
156 	/* check that r1 is lower than r2 */
157 	int	(*recs_inorder)(struct xfs_btree_cur *cur,
158 				const union xfs_btree_rec *r1,
159 				const union xfs_btree_rec *r2);
160 };
161 
162 /*
163  * Reasons for the update_lastrec method to be called.
164  */
165 #define LASTREC_UPDATE	0
166 #define LASTREC_INSREC	1
167 #define LASTREC_DELREC	2
168 
169 
170 union xfs_btree_irec {
171 	struct xfs_alloc_rec_incore	a;
172 	struct xfs_bmbt_irec		b;
173 	struct xfs_inobt_rec_incore	i;
174 	struct xfs_rmap_irec		r;
175 	struct xfs_refcount_irec	rc;
176 };
177 
178 /* Per-AG btree information. */
179 struct xfs_btree_cur_ag {
180 	struct xfs_perag		*pag;
181 	union {
182 		struct xfs_buf		*agbp;
183 		struct xbtree_afakeroot	*afake;	/* for staging cursor */
184 	};
185 	union {
186 		struct {
187 			unsigned int	nr_ops;	/* # record updates */
188 			unsigned int	shape_changes;	/* # of extent splits */
189 		} refc;
190 		struct {
191 			bool		active;	/* allocation cursor state */
192 		} abt;
193 	};
194 };
195 
196 /* Btree-in-inode cursor information */
197 struct xfs_btree_cur_ino {
198 	struct xfs_inode		*ip;
199 	struct xbtree_ifakeroot		*ifake;	/* for staging cursor */
200 	int				allocated;
201 	short				forksize;
202 	char				whichfork;
203 	char				flags;
204 /* We are converting a delalloc reservation */
205 #define	XFS_BTCUR_BMBT_WASDEL		(1 << 0)
206 
207 /* For extent swap, ignore owner check in verifier */
208 #define	XFS_BTCUR_BMBT_INVALID_OWNER	(1 << 1)
209 };
210 
211 struct xfs_btree_level {
212 	/* buffer pointer */
213 	struct xfs_buf		*bp;
214 
215 	/* key/record number */
216 	uint16_t		ptr;
217 
218 	/* readahead info */
219 #define XFS_BTCUR_LEFTRA	(1 << 0) /* left sibling has been read-ahead */
220 #define XFS_BTCUR_RIGHTRA	(1 << 1) /* right sibling has been read-ahead */
221 	uint16_t		ra;
222 };
223 
224 /*
225  * Btree cursor structure.
226  * This collects all information needed by the btree code in one place.
227  */
228 struct xfs_btree_cur
229 {
230 	struct xfs_trans	*bc_tp;	/* transaction we're in, if any */
231 	struct xfs_mount	*bc_mp;	/* file system mount struct */
232 	const struct xfs_btree_ops *bc_ops;
233 	struct kmem_cache	*bc_cache; /* cursor cache */
234 	unsigned int		bc_flags; /* btree features - below */
235 	xfs_btnum_t		bc_btnum; /* identifies which btree type */
236 	union xfs_btree_irec	bc_rec;	/* current insert/search record value */
237 	uint8_t			bc_nlevels; /* number of levels in the tree */
238 	uint8_t			bc_maxlevels; /* maximum levels for this btree type */
239 	int			bc_statoff; /* offset of btree stats array */
240 
241 	/*
242 	 * Short btree pointers need an agno to be able to turn the pointers
243 	 * into physical addresses for IO, so the btree cursor switches between
244 	 * bc_ino and bc_ag based on whether XFS_BTREE_LONG_PTRS is set for the
245 	 * cursor.
246 	 */
247 	union {
248 		struct xfs_btree_cur_ag	bc_ag;
249 		struct xfs_btree_cur_ino bc_ino;
250 	};
251 
252 	/* Must be at the end of the struct! */
253 	struct xfs_btree_level	bc_levels[];
254 };
255 
256 /*
257  * Compute the size of a btree cursor that can handle a btree of a given
258  * height.  The bc_levels array handles node and leaf blocks, so its size
259  * is exactly nlevels.
260  */
261 static inline size_t
262 xfs_btree_cur_sizeof(unsigned int nlevels)
263 {
264 	return struct_size((struct xfs_btree_cur *)NULL, bc_levels, nlevels);
265 }
266 
267 /* cursor flags */
268 #define XFS_BTREE_LONG_PTRS		(1<<0)	/* pointers are 64bits long */
269 #define XFS_BTREE_ROOT_IN_INODE		(1<<1)	/* root may be variable size */
270 #define XFS_BTREE_LASTREC_UPDATE	(1<<2)	/* track last rec externally */
271 #define XFS_BTREE_CRC_BLOCKS		(1<<3)	/* uses extended btree blocks */
272 #define XFS_BTREE_OVERLAPPING		(1<<4)	/* overlapping intervals */
273 /*
274  * The root of this btree is a fakeroot structure so that we can stage a btree
275  * rebuild without leaving it accessible via primary metadata.  The ops struct
276  * is dynamically allocated and must be freed when the cursor is deleted.
277  */
278 #define XFS_BTREE_STAGING		(1<<5)
279 
280 #define	XFS_BTREE_NOERROR	0
281 #define	XFS_BTREE_ERROR		1
282 
283 /*
284  * Convert from buffer to btree block header.
285  */
286 #define	XFS_BUF_TO_BLOCK(bp)	((struct xfs_btree_block *)((bp)->b_addr))
287 
288 /*
289  * Internal long and short btree block checks.  They return NULL if the
290  * block is ok or the address of the failed check otherwise.
291  */
292 xfs_failaddr_t __xfs_btree_check_lblock(struct xfs_btree_cur *cur,
293 		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
294 xfs_failaddr_t __xfs_btree_check_sblock(struct xfs_btree_cur *cur,
295 		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
296 
297 /*
298  * Check that block header is ok.
299  */
300 int
301 xfs_btree_check_block(
302 	struct xfs_btree_cur	*cur,	/* btree cursor */
303 	struct xfs_btree_block	*block,	/* generic btree block pointer */
304 	int			level,	/* level of the btree block */
305 	struct xfs_buf		*bp);	/* buffer containing block, if any */
306 
307 /*
308  * Check that (long) pointer is ok.
309  */
310 bool					/* error (0 or EFSCORRUPTED) */
311 xfs_btree_check_lptr(
312 	struct xfs_btree_cur	*cur,	/* btree cursor */
313 	xfs_fsblock_t		fsbno,	/* btree block disk address */
314 	int			level);	/* btree block level */
315 
316 /*
317  * Check that (short) pointer is ok.
318  */
319 bool					/* error (0 or EFSCORRUPTED) */
320 xfs_btree_check_sptr(
321 	struct xfs_btree_cur	*cur,	/* btree cursor */
322 	xfs_agblock_t		agbno,	/* btree block disk address */
323 	int			level);	/* btree block level */
324 
325 /*
326  * Delete the btree cursor.
327  */
328 void
329 xfs_btree_del_cursor(
330 	struct xfs_btree_cur	*cur,	/* btree cursor */
331 	int			error);	/* del because of error */
332 
333 /*
334  * Duplicate the btree cursor.
335  * Allocate a new one, copy the record, re-get the buffers.
336  */
337 int					/* error */
338 xfs_btree_dup_cursor(
339 	struct xfs_btree_cur		*cur,	/* input cursor */
340 	struct xfs_btree_cur		**ncur);/* output cursor */
341 
342 /*
343  * Compute first and last byte offsets for the fields given.
344  * Interprets the offsets table, which contains struct field offsets.
345  */
346 void
347 xfs_btree_offsets(
348 	uint32_t		fields,	/* bitmask of fields */
349 	const short		*offsets,/* table of field offsets */
350 	int			nbits,	/* number of bits to inspect */
351 	int			*first,	/* output: first byte offset */
352 	int			*last);	/* output: last byte offset */
353 
354 /*
355  * Get a buffer for the block, return it read in.
356  * Long-form addressing.
357  */
358 int					/* error */
359 xfs_btree_read_bufl(
360 	struct xfs_mount	*mp,	/* file system mount point */
361 	struct xfs_trans	*tp,	/* transaction pointer */
362 	xfs_fsblock_t		fsbno,	/* file system block number */
363 	struct xfs_buf		**bpp,	/* buffer for fsbno */
364 	int			refval,	/* ref count value for buffer */
365 	const struct xfs_buf_ops *ops);
366 
367 /*
368  * Read-ahead the block, don't wait for it, don't return a buffer.
369  * Long-form addressing.
370  */
371 void					/* error */
372 xfs_btree_reada_bufl(
373 	struct xfs_mount	*mp,	/* file system mount point */
374 	xfs_fsblock_t		fsbno,	/* file system block number */
375 	xfs_extlen_t		count,	/* count of filesystem blocks */
376 	const struct xfs_buf_ops *ops);
377 
378 /*
379  * Read-ahead the block, don't wait for it, don't return a buffer.
380  * Short-form addressing.
381  */
382 void					/* error */
383 xfs_btree_reada_bufs(
384 	struct xfs_mount	*mp,	/* file system mount point */
385 	xfs_agnumber_t		agno,	/* allocation group number */
386 	xfs_agblock_t		agbno,	/* allocation group block number */
387 	xfs_extlen_t		count,	/* count of filesystem blocks */
388 	const struct xfs_buf_ops *ops);
389 
390 /*
391  * Initialise a new btree block header
392  */
393 void
394 xfs_btree_init_block(
395 	struct xfs_mount *mp,
396 	struct xfs_buf	*bp,
397 	xfs_btnum_t	btnum,
398 	__u16		level,
399 	__u16		numrecs,
400 	__u64		owner);
401 
402 void
403 xfs_btree_init_block_int(
404 	struct xfs_mount	*mp,
405 	struct xfs_btree_block	*buf,
406 	xfs_daddr_t		blkno,
407 	xfs_btnum_t		btnum,
408 	__u16			level,
409 	__u16			numrecs,
410 	__u64			owner,
411 	unsigned int		flags);
412 
413 /*
414  * Common btree core entry points.
415  */
416 int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
417 int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
418 int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
419 int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
420 int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
421 int xfs_btree_insert(struct xfs_btree_cur *, int *);
422 int xfs_btree_delete(struct xfs_btree_cur *, int *);
423 int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
424 int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
425 			   struct list_head *buffer_list);
426 
427 /*
428  * btree block CRC helpers
429  */
430 void xfs_btree_lblock_calc_crc(struct xfs_buf *);
431 bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
432 void xfs_btree_sblock_calc_crc(struct xfs_buf *);
433 bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
434 
435 /*
436  * Internal btree helpers also used by xfs_bmap.c.
437  */
438 void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, uint32_t);
439 void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
440 
441 /*
442  * Helpers.
443  */
444 static inline int xfs_btree_get_numrecs(const struct xfs_btree_block *block)
445 {
446 	return be16_to_cpu(block->bb_numrecs);
447 }
448 
449 static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
450 		uint16_t numrecs)
451 {
452 	block->bb_numrecs = cpu_to_be16(numrecs);
453 }
454 
455 static inline int xfs_btree_get_level(const struct xfs_btree_block *block)
456 {
457 	return be16_to_cpu(block->bb_level);
458 }
459 
460 
461 /*
462  * Min and max functions for extlen, agblock, fileoff, and filblks types.
463  */
464 #define	XFS_EXTLEN_MIN(a,b)	min_t(xfs_extlen_t, (a), (b))
465 #define	XFS_EXTLEN_MAX(a,b)	max_t(xfs_extlen_t, (a), (b))
466 #define	XFS_AGBLOCK_MIN(a,b)	min_t(xfs_agblock_t, (a), (b))
467 #define	XFS_AGBLOCK_MAX(a,b)	max_t(xfs_agblock_t, (a), (b))
468 #define	XFS_FILEOFF_MIN(a,b)	min_t(xfs_fileoff_t, (a), (b))
469 #define	XFS_FILEOFF_MAX(a,b)	max_t(xfs_fileoff_t, (a), (b))
470 #define	XFS_FILBLKS_MIN(a,b)	min_t(xfs_filblks_t, (a), (b))
471 #define	XFS_FILBLKS_MAX(a,b)	max_t(xfs_filblks_t, (a), (b))
472 
473 xfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
474 xfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
475 		unsigned int max_recs);
476 xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
477 		uint64_t owner);
478 xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
479 		unsigned int max_recs);
480 
481 unsigned int xfs_btree_compute_maxlevels(const unsigned int *limits,
482 		unsigned long long records);
483 unsigned long long xfs_btree_calc_size(const unsigned int *limits,
484 		unsigned long long records);
485 unsigned int xfs_btree_space_to_height(const unsigned int *limits,
486 		unsigned long long blocks);
487 
488 /*
489  * Return codes for the query range iterator function are 0 to continue
490  * iterating, and non-zero to stop iterating.  Any non-zero value will be
491  * passed up to the _query_range caller.  The special value -ECANCELED can be
492  * used to stop iteration, because _query_range never generates that error
493  * code on its own.
494  */
495 typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
496 		const union xfs_btree_rec *rec, void *priv);
497 
498 int xfs_btree_query_range(struct xfs_btree_cur *cur,
499 		const union xfs_btree_irec *low_rec,
500 		const union xfs_btree_irec *high_rec,
501 		xfs_btree_query_range_fn fn, void *priv);
502 int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
503 		void *priv);
504 
505 typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
506 		void *data);
507 /* Visit record blocks. */
508 #define XFS_BTREE_VISIT_RECORDS		(1 << 0)
509 /* Visit leaf blocks. */
510 #define XFS_BTREE_VISIT_LEAVES		(1 << 1)
511 /* Visit all blocks. */
512 #define XFS_BTREE_VISIT_ALL		(XFS_BTREE_VISIT_RECORDS | \
513 					 XFS_BTREE_VISIT_LEAVES)
514 int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
515 		xfs_btree_visit_blocks_fn fn, unsigned int flags, void *data);
516 
517 int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
518 
519 union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
520 		struct xfs_btree_block *block);
521 union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
522 		struct xfs_btree_block *block);
523 union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
524 		struct xfs_btree_block *block);
525 union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
526 		struct xfs_btree_block *block);
527 int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
528 		const union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
529 struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
530 		int level, struct xfs_buf **bpp);
531 bool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur,
532 		const union xfs_btree_ptr *ptr);
533 int64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur *cur,
534 				const union xfs_btree_ptr *a,
535 				const union xfs_btree_ptr *b);
536 void xfs_btree_get_sibling(struct xfs_btree_cur *cur,
537 			   struct xfs_btree_block *block,
538 			   union xfs_btree_ptr *ptr, int lr);
539 void xfs_btree_get_keys(struct xfs_btree_cur *cur,
540 		struct xfs_btree_block *block, union xfs_btree_key *key);
541 union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
542 		union xfs_btree_key *key);
543 int xfs_btree_has_record(struct xfs_btree_cur *cur,
544 		const union xfs_btree_irec *low,
545 		const union xfs_btree_irec *high, bool *exists);
546 bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
547 struct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
548 
549 /* Does this cursor point to the last block in the given level? */
550 static inline bool
551 xfs_btree_islastblock(
552 	struct xfs_btree_cur	*cur,
553 	int			level)
554 {
555 	struct xfs_btree_block	*block;
556 	struct xfs_buf		*bp;
557 
558 	block = xfs_btree_get_block(cur, level, &bp);
559 	ASSERT(block && xfs_btree_check_block(cur, block, level, bp) == 0);
560 
561 	if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
562 		return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
563 	return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
564 }
565 
566 void xfs_btree_set_ptr_null(struct xfs_btree_cur *cur,
567 		union xfs_btree_ptr *ptr);
568 int xfs_btree_get_buf_block(struct xfs_btree_cur *cur,
569 		const union xfs_btree_ptr *ptr, struct xfs_btree_block **block,
570 		struct xfs_buf **bpp);
571 void xfs_btree_set_sibling(struct xfs_btree_cur *cur,
572 		struct xfs_btree_block *block, const union xfs_btree_ptr *ptr,
573 		int lr);
574 void xfs_btree_init_block_cur(struct xfs_btree_cur *cur,
575 		struct xfs_buf *bp, int level, int numrecs);
576 void xfs_btree_copy_ptrs(struct xfs_btree_cur *cur,
577 		union xfs_btree_ptr *dst_ptr,
578 		const union xfs_btree_ptr *src_ptr, int numptrs);
579 void xfs_btree_copy_keys(struct xfs_btree_cur *cur,
580 		union xfs_btree_key *dst_key,
581 		const union xfs_btree_key *src_key, int numkeys);
582 
583 static inline struct xfs_btree_cur *
584 xfs_btree_alloc_cursor(
585 	struct xfs_mount	*mp,
586 	struct xfs_trans	*tp,
587 	xfs_btnum_t		btnum,
588 	uint8_t			maxlevels,
589 	struct kmem_cache	*cache)
590 {
591 	struct xfs_btree_cur	*cur;
592 
593 	cur = kmem_cache_zalloc(cache, GFP_NOFS | __GFP_NOFAIL);
594 	cur->bc_tp = tp;
595 	cur->bc_mp = mp;
596 	cur->bc_btnum = btnum;
597 	cur->bc_maxlevels = maxlevels;
598 	cur->bc_cache = cache;
599 
600 	return cur;
601 }
602 
603 int __init xfs_btree_init_cur_caches(void);
604 void xfs_btree_destroy_cur_caches(void);
605 
606 #endif	/* __XFS_BTREE_H__ */
607