1508578f2SNishad Kamdar/* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds/*
37b718769SNathan Scott * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
47b718769SNathan Scott * All Rights Reserved.
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds#ifndef __XFS_BTREE_H__
71da177e4SLinus Torvalds#define	__XFS_BTREE_H__
81da177e4SLinus Torvalds
91da177e4SLinus Torvaldsstruct xfs_buf;
101da177e4SLinus Torvaldsstruct xfs_inode;
111da177e4SLinus Torvaldsstruct xfs_mount;
121da177e4SLinus Torvaldsstruct xfs_trans;
13349e1c03SDarrick J. Wongstruct xfs_ifork;
14be9fb17dSDave Chinnerstruct xfs_perag;
151da177e4SLinus Torvalds
16de227dd9SChristoph Hellwig/*
17de227dd9SChristoph Hellwig * Generic key, ptr and record wrapper structures.
18de227dd9SChristoph Hellwig *
19de227dd9SChristoph Hellwig * These are disk format structures, and are converted where necessary
20de227dd9SChristoph Hellwig * by the btree specific code that needs to interpret them.
21de227dd9SChristoph Hellwig */
22de227dd9SChristoph Hellwigunion xfs_btree_ptr {
23de227dd9SChristoph Hellwig	__be32			s;	/* short form ptr */
24de227dd9SChristoph Hellwig	__be64			l;	/* long form ptr */
25de227dd9SChristoph Hellwig};
26de227dd9SChristoph Hellwig
272c813ad6SDarrick J. Wong/*
28a1d46cffSDarrick J. Wong * The in-core btree key.  Overlapping btrees actually store two keys
29a1d46cffSDarrick J. Wong * per pointer, so we reserve enough memory to hold both.  The __*bigkey
30a1d46cffSDarrick J. Wong * items should never be accessed directly.
312c813ad6SDarrick J. Wong */
32a1d46cffSDarrick J. Wongunion xfs_btree_key {
332c813ad6SDarrick J. Wong	struct xfs_bmbt_key		bmbt;
342c813ad6SDarrick J. Wong	xfs_bmdr_key_t			bmbr;	/* bmbt root block */
352c813ad6SDarrick J. Wong	xfs_alloc_key_t			alloc;
362c813ad6SDarrick J. Wong	struct xfs_inobt_key		inobt;
37a1d46cffSDarrick J. Wong	struct xfs_rmap_key		rmap;
38a1d46cffSDarrick J. Wong	struct xfs_rmap_key		__rmap_bigkey[2];
391946b91cSDarrick J. Wong	struct xfs_refcount_key		refc;
402c813ad6SDarrick J. Wong};
412c813ad6SDarrick J. Wong
42de227dd9SChristoph Hellwigunion xfs_btree_rec {
43035e00acSDarrick J. Wong	struct xfs_bmbt_rec		bmbt;
44035e00acSDarrick J. Wong	xfs_bmdr_rec_t			bmbr;	/* bmbt root block */
45035e00acSDarrick J. Wong	struct xfs_alloc_rec		alloc;
46035e00acSDarrick J. Wong	struct xfs_inobt_rec		inobt;
47035e00acSDarrick J. Wong	struct xfs_rmap_rec		rmap;
481946b91cSDarrick J. Wong	struct xfs_refcount_rec		refc;
49de227dd9SChristoph Hellwig};
50de227dd9SChristoph Hellwig
51a4fbe6abSDave Chinner/*
52a4fbe6abSDave Chinner * This nonsense is to make -wlint happy.
53a4fbe6abSDave Chinner */
54a4fbe6abSDave Chinner#define	XFS_LOOKUP_EQ	((xfs_lookup_t)XFS_LOOKUP_EQi)
55a4fbe6abSDave Chinner#define	XFS_LOOKUP_LE	((xfs_lookup_t)XFS_LOOKUP_LEi)
56a4fbe6abSDave Chinner#define	XFS_LOOKUP_GE	((xfs_lookup_t)XFS_LOOKUP_GEi)
57a4fbe6abSDave Chinner
58a4fbe6abSDave Chinner#define	XFS_BTNUM_BNO	((xfs_btnum_t)XFS_BTNUM_BNOi)
59a4fbe6abSDave Chinner#define	XFS_BTNUM_CNT	((xfs_btnum_t)XFS_BTNUM_CNTi)
60a4fbe6abSDave Chinner#define	XFS_BTNUM_BMAP	((xfs_btnum_t)XFS_BTNUM_BMAPi)
61a4fbe6abSDave Chinner#define	XFS_BTNUM_INO	((xfs_btnum_t)XFS_BTNUM_INOi)
62aafc3c24SBrian Foster#define	XFS_BTNUM_FINO	((xfs_btnum_t)XFS_BTNUM_FINOi)
63b8704944SDarrick J. Wong#define	XFS_BTNUM_RMAP	((xfs_btnum_t)XFS_BTNUM_RMAPi)
6446eeb521SDarrick J. Wong#define	XFS_BTNUM_REFC	((xfs_btnum_t)XFS_BTNUM_REFCi)
65a4fbe6abSDave Chinner
66c8ce540dSDarrick J. Wonguint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
67af7d20fdSEric Sandeen
681da177e4SLinus Torvalds/*
691da177e4SLinus Torvalds * For logging record fields.
701da177e4SLinus Torvalds */
7121b5c978SDave Chinner#define	XFS_BB_MAGIC		(1 << 0)
7221b5c978SDave Chinner#define	XFS_BB_LEVEL		(1 << 1)
7321b5c978SDave Chinner#define	XFS_BB_NUMRECS		(1 << 2)
7421b5c978SDave Chinner#define	XFS_BB_LEFTSIB		(1 << 3)
7521b5c978SDave Chinner#define	XFS_BB_RIGHTSIB		(1 << 4)
7621b5c978SDave Chinner#define	XFS_BB_BLKNO		(1 << 5)
7721b5c978SDave Chinner#define	XFS_BB_LSN		(1 << 6)
7821b5c978SDave Chinner#define	XFS_BB_UUID		(1 << 7)
7921b5c978SDave Chinner#define	XFS_BB_OWNER		(1 << 8)
801da177e4SLinus Torvalds#define	XFS_BB_NUM_BITS		5
811da177e4SLinus Torvalds#define	XFS_BB_ALL_BITS		((1 << XFS_BB_NUM_BITS) - 1)
8221b5c978SDave Chinner#define	XFS_BB_NUM_BITS_CRC	9
83ee1a47abSChristoph Hellwig#define	XFS_BB_ALL_BITS_CRC	((1 << XFS_BB_NUM_BITS_CRC) - 1)
841da177e4SLinus Torvalds
85854929f0SDavid Chinner/*
86854929f0SDavid Chinner * Generic stats interface
87854929f0SDavid Chinner */
88ff6d6af2SBill O'Donnell#define XFS_BTREE_STATS_INC(cur, stat)	\
8911ef38afSDave Chinner	XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
9011ef38afSDave Chinner#define XFS_BTREE_STATS_ADD(cur, stat, val)	\
9111ef38afSDave Chinner	XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
921da177e4SLinus Torvalds
93561f7d17SChristoph Hellwigstruct xfs_btree_ops {
9465f1eaeaSChristoph Hellwig	/* size of the key and record structures */
9565f1eaeaSChristoph Hellwig	size_t	key_len;
9665f1eaeaSChristoph Hellwig	size_t	rec_len;
9765f1eaeaSChristoph Hellwig
98561f7d17SChristoph Hellwig	/* cursor operations */
99561f7d17SChristoph Hellwig	struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
1004b22a571SChristoph Hellwig	void	(*update_cursor)(struct xfs_btree_cur *src,
1014b22a571SChristoph Hellwig				 struct xfs_btree_cur *dst);
1028c4ed633SChristoph Hellwig
103344207ceSChristoph Hellwig	/* update btree root pointer */
104344207ceSChristoph Hellwig	void	(*set_root)(struct xfs_btree_cur *cur,
105b5a6e5feSDarrick J. Wong			    const union xfs_btree_ptr *nptr, int level_change);
106344207ceSChristoph Hellwig
107f5eb8e7cSChristoph Hellwig	/* block allocation / freeing */
108f5eb8e7cSChristoph Hellwig	int	(*alloc_block)(struct xfs_btree_cur *cur,
109deb06b9aSDarrick J. Wong			       const union xfs_btree_ptr *start_bno,
110f5eb8e7cSChristoph Hellwig			       union xfs_btree_ptr *new_bno,
1116f8950cdSEric Sandeen			       int *stat);
112d4b3a4b7SChristoph Hellwig	int	(*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
113f5eb8e7cSChristoph Hellwig
114278d0ca1SChristoph Hellwig	/* update last record information */
115278d0ca1SChristoph Hellwig	void	(*update_lastrec)(struct xfs_btree_cur *cur,
11660e265f7SDarrick J. Wong				  const struct xfs_btree_block *block,
11760e265f7SDarrick J. Wong				  const union xfs_btree_rec *rec,
118278d0ca1SChristoph Hellwig				  int ptr, int reason);
119278d0ca1SChristoph Hellwig
120ce5e42dbSChristoph Hellwig	/* records in block/level */
12191cca5dfSChristoph Hellwig	int	(*get_minrecs)(struct xfs_btree_cur *cur, int level);
122ce5e42dbSChristoph Hellwig	int	(*get_maxrecs)(struct xfs_btree_cur *cur, int level);
123ce5e42dbSChristoph Hellwig
1244b22a571SChristoph Hellwig	/* records on disk.  Matter for the root in inode case. */
1254b22a571SChristoph Hellwig	int	(*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
1264b22a571SChristoph Hellwig
127fe033cc8SChristoph Hellwig	/* init values of btree structures */
128fe033cc8SChristoph Hellwig	void	(*init_key_from_rec)(union xfs_btree_key *key,
12923825cd1SDarrick J. Wong				     const union xfs_btree_rec *rec);
1304b22a571SChristoph Hellwig	void	(*init_rec_from_cur)(struct xfs_btree_cur *cur,
1314b22a571SChristoph Hellwig				     union xfs_btree_rec *rec);
132fe033cc8SChristoph Hellwig	void	(*init_ptr_from_cur)(struct xfs_btree_cur *cur,
133fe033cc8SChristoph Hellwig				     union xfs_btree_ptr *ptr);
1342c813ad6SDarrick J. Wong	void	(*init_high_key_from_rec)(union xfs_btree_key *key,
13523825cd1SDarrick J. Wong					  const union xfs_btree_rec *rec);
136fe033cc8SChristoph Hellwig
137fe033cc8SChristoph Hellwig	/* difference between key value and cursor value */
138c8ce540dSDarrick J. Wong	int64_t (*key_diff)(struct xfs_btree_cur *cur,
139d29d5577SDarrick J. Wong			    const union xfs_btree_key *key);
140fe033cc8SChristoph Hellwig
1412c813ad6SDarrick J. Wong	/*
1422c813ad6SDarrick J. Wong	 * Difference between key2 and key1 -- positive if key1 > key2,
1432c813ad6SDarrick J. Wong	 * negative if key1 < key2, and zero if equal.
1442c813ad6SDarrick J. Wong	 */
145c8ce540dSDarrick J. Wong	int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
146d29d5577SDarrick J. Wong				 const union xfs_btree_key *key1,
147d29d5577SDarrick J. Wong				 const union xfs_btree_key *key2);
1482c813ad6SDarrick J. Wong
1491813dd64SDave Chinner	const struct xfs_buf_ops	*buf_ops;
150b0f539deSDave Chinner
1514a26e66eSChristoph Hellwig	/* check that k1 is lower than k2 */
1524a26e66eSChristoph Hellwig	int	(*keys_inorder)(struct xfs_btree_cur *cur,
1538e38dc88SDarrick J. Wong				const union xfs_btree_key *k1,
1548e38dc88SDarrick J. Wong				const union xfs_btree_key *k2);
1554a26e66eSChristoph Hellwig
1564a26e66eSChristoph Hellwig	/* check that r1 is lower than r2 */
1574a26e66eSChristoph Hellwig	int	(*recs_inorder)(struct xfs_btree_cur *cur,
1588e38dc88SDarrick J. Wong				const union xfs_btree_rec *r1,
1598e38dc88SDarrick J. Wong				const union xfs_btree_rec *r2);
160561f7d17SChristoph Hellwig};
161561f7d17SChristoph Hellwig
162278d0ca1SChristoph Hellwig/*
163278d0ca1SChristoph Hellwig * Reasons for the update_lastrec method to be called.
164278d0ca1SChristoph Hellwig */
165278d0ca1SChristoph Hellwig#define LASTREC_UPDATE	0
1664b22a571SChristoph Hellwig#define LASTREC_INSREC	1
16791cca5dfSChristoph Hellwig#define LASTREC_DELREC	2
168278d0ca1SChristoph Hellwig
169278d0ca1SChristoph Hellwig
170105f7d83SDarrick J. Wongunion xfs_btree_irec {
171105f7d83SDarrick J. Wong	struct xfs_alloc_rec_incore	a;
172105f7d83SDarrick J. Wong	struct xfs_bmbt_irec		b;
173105f7d83SDarrick J. Wong	struct xfs_inobt_rec_incore	i;
1744b8ed677SDarrick J. Wong	struct xfs_rmap_irec		r;
1751946b91cSDarrick J. Wong	struct xfs_refcount_irec	rc;
1761946b91cSDarrick J. Wong};
1771946b91cSDarrick J. Wong
17868422d90SDave Chinner/* Per-AG btree information. */
17968422d90SDave Chinnerstruct xfs_btree_cur_ag {
180efb79ea3SDarrick J. Wong	struct xfs_perag		*pag;
181e06536a6SDarrick J. Wong	union {
182e06536a6SDarrick J. Wong		struct xfs_buf		*agbp;
183e06536a6SDarrick J. Wong		struct xbtree_afakeroot	*afake;	/* for staging cursor */
184e06536a6SDarrick J. Wong	};
185c4aa10d0SDave Chinner	union {
186c4aa10d0SDave Chinner		struct {
187efb79ea3SDarrick J. Wong			unsigned int	nr_ops;	/* # record updates */
188efb79ea3SDarrick J. Wong			unsigned int	shape_changes;	/* # of extent splits */
189c4aa10d0SDave Chinner		} refc;
190c4aa10d0SDave Chinner		struct {
191efb79ea3SDarrick J. Wong			bool		active;	/* allocation cursor state */
192c4aa10d0SDave Chinner		} abt;
193c4aa10d0SDave Chinner	};
19468422d90SDave Chinner};
19568422d90SDave Chinner
19668422d90SDave Chinner/* Btree-in-inode cursor information */
19768422d90SDave Chinnerstruct xfs_btree_cur_ino {
19868422d90SDave Chinner	struct xfs_inode		*ip;
199349e1c03SDarrick J. Wong	struct xbtree_ifakeroot		*ifake;	/* for staging cursor */
20068422d90SDave Chinner	int				allocated;
20168422d90SDave Chinner	short				forksize;
20268422d90SDave Chinner	char				whichfork;
20368422d90SDave Chinner	char				flags;
20468422d90SDave Chinner/* We are converting a delalloc reservation */
20568422d90SDave Chinner#define	XFS_BTCUR_BMBT_WASDEL		(1 << 0)
20668422d90SDave Chinner
20768422d90SDave Chinner/* For extent swap, ignore owner check in verifier */
20868422d90SDave Chinner#define	XFS_BTCUR_BMBT_INVALID_OWNER	(1 << 1)
20968422d90SDave Chinner};
21068422d90SDave Chinner
2116ca444cfSDarrick J. Wongstruct xfs_btree_level {
2126ca444cfSDarrick J. Wong	/* buffer pointer */
2136ca444cfSDarrick J. Wong	struct xfs_buf		*bp;
2146ca444cfSDarrick J. Wong
2156ca444cfSDarrick J. Wong	/* key/record number */
2166ca444cfSDarrick J. Wong	uint16_t		ptr;
2176ca444cfSDarrick J. Wong
2186ca444cfSDarrick J. Wong	/* readahead info */
2196ca444cfSDarrick J. Wong#define XFS_BTCUR_LEFTRA	(1 << 0) /* left sibling has been read-ahead */
2206ca444cfSDarrick J. Wong#define XFS_BTCUR_RIGHTRA	(1 << 1) /* right sibling has been read-ahead */
2216ca444cfSDarrick J. Wong	uint16_t		ra;
2226ca444cfSDarrick J. Wong};
2236ca444cfSDarrick J. Wong
2241da177e4SLinus Torvalds/*
2251da177e4SLinus Torvalds * Btree cursor structure.
2261da177e4SLinus Torvalds * This collects all information needed by the btree code in one place.
2271da177e4SLinus Torvalds */
228ae127f08SDarrick J. Wongstruct xfs_btree_cur
2291da177e4SLinus Torvalds{
2301da177e4SLinus Torvalds	struct xfs_trans	*bc_tp;	/* transaction we're in, if any */
2311da177e4SLinus Torvalds	struct xfs_mount	*bc_mp;	/* file system mount struct */
232561f7d17SChristoph Hellwig	const struct xfs_btree_ops *bc_ops;
233e7720afaSDarrick J. Wong	struct kmem_cache	*bc_cache; /* cursor cache */
23469724d92SDarrick J. Wong	unsigned int		bc_flags; /* btree features - below */
23569724d92SDarrick J. Wong	xfs_btnum_t		bc_btnum; /* identifies which btree type */
236105f7d83SDarrick J. Wong	union xfs_btree_irec	bc_rec;	/* current insert/search record value */
23769724d92SDarrick J. Wong	uint8_t			bc_nlevels; /* number of levels in the tree */
238c0643f6fSDarrick J. Wong	uint8_t			bc_maxlevels; /* maximum levels for this btree type */
23969724d92SDarrick J. Wong	int			bc_statoff; /* offset of btree stats array */
240be9fb17dSDave Chinner
241be9fb17dSDave Chinner	/*
242be9fb17dSDave Chinner	 * Short btree pointers need an agno to be able to turn the pointers
243be9fb17dSDave Chinner	 * into physical addresses for IO, so the btree cursor switches between
244be9fb17dSDave Chinner	 * bc_ino and bc_ag based on whether XFS_BTREE_LONG_PTRS is set for the
245be9fb17dSDave Chinner	 * cursor.
246be9fb17dSDave Chinner	 */
2471da177e4SLinus Torvalds	union {
24868422d90SDave Chinner		struct xfs_btree_cur_ag	bc_ag;
24968422d90SDave Chinner		struct xfs_btree_cur_ino bc_ino;
25068422d90SDave Chinner	};
2516ca444cfSDarrick J. Wong
2526ca444cfSDarrick J. Wong	/* Must be at the end of the struct! */
2536ca444cfSDarrick J. Wong	struct xfs_btree_level	bc_levels[];
254ae127f08SDarrick J. Wong};
2551da177e4SLinus Torvalds
2566ca444cfSDarrick J. Wong/*
2576ca444cfSDarrick J. Wong * Compute the size of a btree cursor that can handle a btree of a given
2586ca444cfSDarrick J. Wong * height.  The bc_levels array handles node and leaf blocks, so its size
2596ca444cfSDarrick J. Wong * is exactly nlevels.
2606ca444cfSDarrick J. Wong */
2616ca444cfSDarrick J. Wongstatic inline size_t
2626ca444cfSDarrick J. Wongxfs_btree_cur_sizeof(unsigned int nlevels)
2636ca444cfSDarrick J. Wong{
2646ca444cfSDarrick J. Wong	return struct_size((struct xfs_btree_cur *)NULL, bc_levels, nlevels);
2656ca444cfSDarrick J. Wong}
2666ca444cfSDarrick J. Wong
2678186e517SChristoph Hellwig/* cursor flags */
268e99ab90dSChristoph Hellwig#define XFS_BTREE_LONG_PTRS		(1<<0)	/* pointers are 64bits long */
2698186e517SChristoph Hellwig#define XFS_BTREE_ROOT_IN_INODE		(1<<1)	/* root may be variable size */
270278d0ca1SChristoph Hellwig#define XFS_BTREE_LASTREC_UPDATE	(1<<2)	/* track last rec externally */
271ee1a47abSChristoph Hellwig#define XFS_BTREE_CRC_BLOCKS		(1<<3)	/* uses extended btree blocks */
2722c813ad6SDarrick J. Wong#define XFS_BTREE_OVERLAPPING		(1<<4)	/* overlapping intervals */
273e06536a6SDarrick J. Wong/*
274e06536a6SDarrick J. Wong * The root of this btree is a fakeroot structure so that we can stage a btree
275e06536a6SDarrick J. Wong * rebuild without leaving it accessible via primary metadata.  The ops struct
276e06536a6SDarrick J. Wong * is dynamically allocated and must be freed when the cursor is deleted.
277e06536a6SDarrick J. Wong */
278e06536a6SDarrick J. Wong#define XFS_BTREE_STAGING		(1<<5)
2798186e517SChristoph Hellwig
2801da177e4SLinus Torvalds#define	XFS_BTREE_NOERROR	0
2811da177e4SLinus Torvalds#define	XFS_BTREE_ERROR		1
2821da177e4SLinus Torvalds
2831da177e4SLinus Torvalds/*
2841da177e4SLinus Torvalds * Convert from buffer to btree block header.
2851da177e4SLinus Torvalds */
28662926044SChandra Seetharaman#define	XFS_BUF_TO_BLOCK(bp)	((struct xfs_btree_block *)((bp)->b_addr))
2871da177e4SLinus Torvalds
28852c732eeSDarrick J. Wong/*
28952c732eeSDarrick J. Wong * Internal long and short btree block checks.  They return NULL if the
29052c732eeSDarrick J. Wong * block is ok or the address of the failed check otherwise.
29152c732eeSDarrick J. Wong */
29252c732eeSDarrick J. Wongxfs_failaddr_t __xfs_btree_check_lblock(struct xfs_btree_cur *cur,
29352c732eeSDarrick J. Wong		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
29452c732eeSDarrick J. Wongxfs_failaddr_t __xfs_btree_check_sblock(struct xfs_btree_cur *cur,
29552c732eeSDarrick J. Wong		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
2961da177e4SLinus Torvalds
2971da177e4SLinus Torvalds/*
298a23f6ef8SChristoph Hellwig * Check that block header is ok.
2991da177e4SLinus Torvalds */
300a23f6ef8SChristoph Hellwigint
301a23f6ef8SChristoph Hellwigxfs_btree_check_block(
302a23f6ef8SChristoph Hellwig	struct xfs_btree_cur	*cur,	/* btree cursor */
303a23f6ef8SChristoph Hellwig	struct xfs_btree_block	*block,	/* generic btree block pointer */
3041da177e4SLinus Torvalds	int			level,	/* level of the btree block */
3051da177e4SLinus Torvalds	struct xfs_buf		*bp);	/* buffer containing block, if any */
3061da177e4SLinus Torvalds
3071da177e4SLinus Torvalds/*
308a23f6ef8SChristoph Hellwig * Check that (long) pointer is ok.
3091da177e4SLinus Torvalds */
310f135761aSDarrick J. Wongbool					/* error (0 or EFSCORRUPTED) */
3111da177e4SLinus Torvaldsxfs_btree_check_lptr(
312a23f6ef8SChristoph Hellwig	struct xfs_btree_cur	*cur,	/* btree cursor */
313f135761aSDarrick J. Wong	xfs_fsblock_t		fsbno,	/* btree block disk address */
314f135761aSDarrick J. Wong	int			level);	/* btree block level */
315f135761aSDarrick J. Wong
316f135761aSDarrick J. Wong/*
317f135761aSDarrick J. Wong * Check that (short) pointer is ok.
318f135761aSDarrick J. Wong */
319f135761aSDarrick J. Wongbool					/* error (0 or EFSCORRUPTED) */
320f135761aSDarrick J. Wongxfs_btree_check_sptr(
321f135761aSDarrick J. Wong	struct xfs_btree_cur	*cur,	/* btree cursor */
322f135761aSDarrick J. Wong	xfs_agblock_t		agbno,	/* btree block disk address */
3231da177e4SLinus Torvalds	int			level);	/* btree block level */
3241da177e4SLinus Torvalds
3251da177e4SLinus Torvalds/*
3261da177e4SLinus Torvalds * Delete the btree cursor.
3271da177e4SLinus Torvalds */
3281da177e4SLinus Torvaldsvoid
3291da177e4SLinus Torvaldsxfs_btree_del_cursor(
330ae127f08SDarrick J. Wong	struct xfs_btree_cur	*cur,	/* btree cursor */
3311da177e4SLinus Torvalds	int			error);	/* del because of error */
3321da177e4SLinus Torvalds
3331da177e4SLinus Torvalds/*
3341da177e4SLinus Torvalds * Duplicate the btree cursor.
3351da177e4SLinus Torvalds * Allocate a new one, copy the record, re-get the buffers.
3361da177e4SLinus Torvalds */
3371da177e4SLinus Torvaldsint					/* error */
3381da177e4SLinus Torvaldsxfs_btree_dup_cursor(
339ae127f08SDarrick J. Wong	struct xfs_btree_cur		*cur,	/* input cursor */
340ae127f08SDarrick J. Wong	struct xfs_btree_cur		**ncur);/* output cursor */
3411da177e4SLinus Torvalds
3421da177e4SLinus Torvalds/*
3431da177e4SLinus Torvalds * Compute first and last byte offsets for the fields given.
3441da177e4SLinus Torvalds * Interprets the offsets table, which contains struct field offsets.
3451da177e4SLinus Torvalds */
3461da177e4SLinus Torvaldsvoid
3471da177e4SLinus Torvaldsxfs_btree_offsets(
348c8ce540dSDarrick J. Wong	int64_t			fields,	/* bitmask of fields */
3491da177e4SLinus Torvalds	const short		*offsets,/* table of field offsets */
3501da177e4SLinus Torvalds	int			nbits,	/* number of bits to inspect */
3511da177e4SLinus Torvalds	int			*first,	/* output: first byte offset */
3521da177e4SLinus Torvalds	int			*last);	/* output: last byte offset */
3531da177e4SLinus Torvalds
3541da177e4SLinus Torvalds/*
3551da177e4SLinus Torvalds * Get a buffer for the block, return it read in.
3561da177e4SLinus Torvalds * Long-form addressing.
3571da177e4SLinus Torvalds */
3581da177e4SLinus Torvaldsint					/* error */
3591da177e4SLinus Torvaldsxfs_btree_read_bufl(
3601da177e4SLinus Torvalds	struct xfs_mount	*mp,	/* file system mount point */
3611da177e4SLinus Torvalds	struct xfs_trans	*tp,	/* transaction pointer */
3621da177e4SLinus Torvalds	xfs_fsblock_t		fsbno,	/* file system block number */
3631da177e4SLinus Torvalds	struct xfs_buf		**bpp,	/* buffer for fsbno */
3643d3e6f64SDave Chinner	int			refval,	/* ref count value for buffer */
3651813dd64SDave Chinner	const struct xfs_buf_ops *ops);
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds/*
3681da177e4SLinus Torvalds * Read-ahead the block, don't wait for it, don't return a buffer.
3691da177e4SLinus Torvalds * Long-form addressing.
3701da177e4SLinus Torvalds */
3711da177e4SLinus Torvaldsvoid					/* error */
3721da177e4SLinus Torvaldsxfs_btree_reada_bufl(
3731da177e4SLinus Torvalds	struct xfs_mount	*mp,	/* file system mount point */
3741da177e4SLinus Torvalds	xfs_fsblock_t		fsbno,	/* file system block number */
3753d3e6f64SDave Chinner	xfs_extlen_t		count,	/* count of filesystem blocks */
3761813dd64SDave Chinner	const struct xfs_buf_ops *ops);
3771da177e4SLinus Torvalds
3781da177e4SLinus Torvalds/*
3791da177e4SLinus Torvalds * Read-ahead the block, don't wait for it, don't return a buffer.
3801da177e4SLinus Torvalds * Short-form addressing.
3811da177e4SLinus Torvalds */
3821da177e4SLinus Torvaldsvoid					/* error */
3831da177e4SLinus Torvaldsxfs_btree_reada_bufs(
3841da177e4SLinus Torvalds	struct xfs_mount	*mp,	/* file system mount point */
3851da177e4SLinus Torvalds	xfs_agnumber_t		agno,	/* allocation group number */
3861da177e4SLinus Torvalds	xfs_agblock_t		agbno,	/* allocation group block number */
3873d3e6f64SDave Chinner	xfs_extlen_t		count,	/* count of filesystem blocks */
3881813dd64SDave Chinner	const struct xfs_buf_ops *ops);
3891da177e4SLinus Torvalds
390b64f3a39SDave Chinner/*
391b64f3a39SDave Chinner * Initialise a new btree block header
392b64f3a39SDave Chinner */
393b64f3a39SDave Chinnervoid
394b64f3a39SDave Chinnerxfs_btree_init_block(
395b64f3a39SDave Chinner	struct xfs_mount *mp,
396b64f3a39SDave Chinner	struct xfs_buf	*bp,
397b6f41e44SEric Sandeen	xfs_btnum_t	btnum,
398b64f3a39SDave Chinner	__u16		level,
399b64f3a39SDave Chinner	__u16		numrecs,
400f5b999c0SEric Sandeen	__u64		owner);
40165f1eaeaSChristoph Hellwig
402ee1a47abSChristoph Hellwigvoid
403ee1a47abSChristoph Hellwigxfs_btree_init_block_int(
404ee1a47abSChristoph Hellwig	struct xfs_mount	*mp,
405ee1a47abSChristoph Hellwig	struct xfs_btree_block	*buf,
406ee1a47abSChristoph Hellwig	xfs_daddr_t		blkno,
407b6f41e44SEric Sandeen	xfs_btnum_t		btnum,
408ee1a47abSChristoph Hellwig	__u16			level,
409ee1a47abSChristoph Hellwig	__u16			numrecs,
410ee1a47abSChristoph Hellwig	__u64			owner,
411ee1a47abSChristoph Hellwig	unsigned int		flags);
412ee1a47abSChristoph Hellwig
413637aa50fSChristoph Hellwig/*
414637aa50fSChristoph Hellwig * Common btree core entry points.
415637aa50fSChristoph Hellwig */
416637aa50fSChristoph Hellwigint xfs_btree_increment(struct xfs_btree_cur *, int, int *);
4178df4da4aSChristoph Hellwigint xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
418fe033cc8SChristoph Hellwigint xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
419278d0ca1SChristoph Hellwigint xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
420ea77b0a6SChristoph Hellwigint xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
4214b22a571SChristoph Hellwigint xfs_btree_insert(struct xfs_btree_cur *, int *);
42291cca5dfSChristoph Hellwigint xfs_btree_delete(struct xfs_btree_cur *, int *);
4238cc938feSChristoph Hellwigint xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
424c8ce540dSDarrick J. Wongint xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
425638f4416SDave Chinner			   struct list_head *buffer_list);
426637aa50fSChristoph Hellwig
427ee1a47abSChristoph Hellwig/*
428ee1a47abSChristoph Hellwig * btree block CRC helpers
429ee1a47abSChristoph Hellwig */
430ee1a47abSChristoph Hellwigvoid xfs_btree_lblock_calc_crc(struct xfs_buf *);
431ee1a47abSChristoph Hellwigbool xfs_btree_lblock_verify_crc(struct xfs_buf *);
432ee1a47abSChristoph Hellwigvoid xfs_btree_sblock_calc_crc(struct xfs_buf *);
433ee1a47abSChristoph Hellwigbool xfs_btree_sblock_verify_crc(struct xfs_buf *);
434ee1a47abSChristoph Hellwig
435fd6bcc5bSChristoph Hellwig/*
436fd6bcc5bSChristoph Hellwig * Internal btree helpers also used by xfs_bmap.c.
437fd6bcc5bSChristoph Hellwig */
438fd6bcc5bSChristoph Hellwigvoid xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
439fd6bcc5bSChristoph Hellwigvoid xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
440fd6bcc5bSChristoph Hellwig
44165f1eaeaSChristoph Hellwig/*
44265f1eaeaSChristoph Hellwig * Helpers.
44365f1eaeaSChristoph Hellwig */
44460e265f7SDarrick J. Wongstatic inline int xfs_btree_get_numrecs(const struct xfs_btree_block *block)
445637aa50fSChristoph Hellwig{
446637aa50fSChristoph Hellwig	return be16_to_cpu(block->bb_numrecs);
447637aa50fSChristoph Hellwig}
448637aa50fSChristoph Hellwig
4499eaead51SChristoph Hellwigstatic inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
450c8ce540dSDarrick J. Wong		uint16_t numrecs)
4519eaead51SChristoph Hellwig{
4529eaead51SChristoph Hellwig	block->bb_numrecs = cpu_to_be16(numrecs);
4539eaead51SChristoph Hellwig}
4549eaead51SChristoph Hellwig
45560e265f7SDarrick J. Wongstatic inline int xfs_btree_get_level(const struct xfs_btree_block *block)
45665f1eaeaSChristoph Hellwig{
45765f1eaeaSChristoph Hellwig	return be16_to_cpu(block->bb_level);
45865f1eaeaSChristoph Hellwig}
45965f1eaeaSChristoph Hellwig
4601da177e4SLinus Torvalds
4611da177e4SLinus Torvalds/*
4621da177e4SLinus Torvalds * Min and max functions for extlen, agblock, fileoff, and filblks types.
4631da177e4SLinus Torvalds */
46454aa8e26SDavid Chinner#define	XFS_EXTLEN_MIN(a,b)	min_t(xfs_extlen_t, (a), (b))
46554aa8e26SDavid Chinner#define	XFS_EXTLEN_MAX(a,b)	max_t(xfs_extlen_t, (a), (b))
46654aa8e26SDavid Chinner#define	XFS_AGBLOCK_MIN(a,b)	min_t(xfs_agblock_t, (a), (b))
46754aa8e26SDavid Chinner#define	XFS_AGBLOCK_MAX(a,b)	max_t(xfs_agblock_t, (a), (b))
46854aa8e26SDavid Chinner#define	XFS_FILEOFF_MIN(a,b)	min_t(xfs_fileoff_t, (a), (b))
46954aa8e26SDavid Chinner#define	XFS_FILEOFF_MAX(a,b)	max_t(xfs_fileoff_t, (a), (b))
47054aa8e26SDavid Chinner#define	XFS_FILBLKS_MIN(a,b)	min_t(xfs_filblks_t, (a), (b))
47154aa8e26SDavid Chinner#define	XFS_FILBLKS_MAX(a,b)	max_t(xfs_filblks_t, (a), (b))
472a844f451SNathan Scott
473a6a781a5SDarrick J. Wongxfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
474a6a781a5SDarrick J. Wongxfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
475a6a781a5SDarrick J. Wong		unsigned int max_recs);
476a6a781a5SDarrick J. Wongxfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
477a6a781a5SDarrick J. Wong		uint64_t owner);
478a6a781a5SDarrick J. Wongxfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
479a6a781a5SDarrick J. Wong		unsigned int max_recs);
4808368a601SDarrick J. Wong
4811b236ad7SDarrick J. Wongunsigned int xfs_btree_compute_maxlevels(const unsigned int *limits,
4821b236ad7SDarrick J. Wong		unsigned long long records);
4831b236ad7SDarrick J. Wongunsigned long long xfs_btree_calc_size(const unsigned int *limits,
4841b236ad7SDarrick J. Wong		unsigned long long records);
4859ec69120SDarrick J. Wongunsigned int xfs_btree_space_to_height(const unsigned int *limits,
4869ec69120SDarrick J. Wong		unsigned long long blocks);
487c5ab131bSDarrick J. Wong
488e7ee96dfSDarrick J. Wong/*
489e7ee96dfSDarrick J. Wong * Return codes for the query range iterator function are 0 to continue
490e7ee96dfSDarrick J. Wong * iterating, and non-zero to stop iterating.  Any non-zero value will be
491e7ee96dfSDarrick J. Wong * passed up to the _query_range caller.  The special value -ECANCELED can be
492e7ee96dfSDarrick J. Wong * used to stop iteration, because _query_range never generates that error
493e7ee96dfSDarrick J. Wong * code on its own.
494e7ee96dfSDarrick J. Wong */
495105f7d83SDarrick J. Wongtypedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
496159eb69dSDarrick J. Wong		const union xfs_btree_rec *rec, void *priv);
497105f7d83SDarrick J. Wong
498105f7d83SDarrick J. Wongint xfs_btree_query_range(struct xfs_btree_cur *cur,
49904dcb474SDarrick J. Wong		const union xfs_btree_irec *low_rec,
50004dcb474SDarrick J. Wong		const union xfs_btree_irec *high_rec,
501105f7d83SDarrick J. Wong		xfs_btree_query_range_fn fn, void *priv);
502e9a2599aSDarrick J. Wongint xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
503e9a2599aSDarrick J. Wong		void *priv);
504105f7d83SDarrick J. Wong
50528a89567SDarrick J. Wongtypedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
50628a89567SDarrick J. Wong		void *data);
507e992ae8aSDarrick J. Wong/* Visit record blocks. */
508e992ae8aSDarrick J. Wong#define XFS_BTREE_VISIT_RECORDS		(1 << 0)
509e992ae8aSDarrick J. Wong/* Visit leaf blocks. */
510e992ae8aSDarrick J. Wong#define XFS_BTREE_VISIT_LEAVES		(1 << 1)
511e992ae8aSDarrick J. Wong/* Visit all blocks. */
512e992ae8aSDarrick J. Wong#define XFS_BTREE_VISIT_ALL		(XFS_BTREE_VISIT_RECORDS | \
513e992ae8aSDarrick J. Wong					 XFS_BTREE_VISIT_LEAVES)
51428a89567SDarrick J. Wongint xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
515e992ae8aSDarrick J. Wong		xfs_btree_visit_blocks_fn fn, unsigned int flags, void *data);
51628a89567SDarrick J. Wong
517c611cc03SDarrick J. Wongint xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
518c611cc03SDarrick J. Wong
51926788097SDarrick J. Wongunion xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
52026788097SDarrick J. Wong		struct xfs_btree_block *block);
52126788097SDarrick J. Wongunion xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
52226788097SDarrick J. Wong		struct xfs_btree_block *block);
52326788097SDarrick J. Wongunion xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
52426788097SDarrick J. Wong		struct xfs_btree_block *block);
52526788097SDarrick J. Wongunion xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
52626788097SDarrick J. Wong		struct xfs_btree_block *block);
52726788097SDarrick J. Wongint xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
52832816fd7SDarrick J. Wong		const union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
52926788097SDarrick J. Wongstruct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
53026788097SDarrick J. Wong		int level, struct xfs_buf **bpp);
53132816fd7SDarrick J. Wongbool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur,
53232816fd7SDarrick J. Wong		const union xfs_btree_ptr *ptr);
533cc3e0948SDarrick J. Wongint64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur *cur,
534cc3e0948SDarrick J. Wong				const union xfs_btree_ptr *a,
535cc3e0948SDarrick J. Wong				const union xfs_btree_ptr *b);
536cc3e0948SDarrick J. Wongvoid xfs_btree_get_sibling(struct xfs_btree_cur *cur,
537cc3e0948SDarrick J. Wong			   struct xfs_btree_block *block,
538cc3e0948SDarrick J. Wong			   union xfs_btree_ptr *ptr, int lr);
5392fdbec5cSDarrick J. Wongvoid xfs_btree_get_keys(struct xfs_btree_cur *cur,
5402fdbec5cSDarrick J. Wong		struct xfs_btree_block *block, union xfs_btree_key *key);
5412fdbec5cSDarrick J. Wongunion xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
5422fdbec5cSDarrick J. Wong		union xfs_btree_key *key);
54332816fd7SDarrick J. Wongint xfs_btree_has_record(struct xfs_btree_cur *cur,
54432816fd7SDarrick J. Wong		const union xfs_btree_irec *low,
54532816fd7SDarrick J. Wong		const union xfs_btree_irec *high, bool *exists);
54608daa3ccSDarrick J. Wongbool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
547349e1c03SDarrick J. Wongstruct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
54826788097SDarrick J. Wong
54927d9ee57SDarrick J. Wong/* Does this cursor point to the last block in the given level? */
55027d9ee57SDarrick J. Wongstatic inline bool
55127d9ee57SDarrick J. Wongxfs_btree_islastblock(
552ae127f08SDarrick J. Wong	struct xfs_btree_cur	*cur,
55327d9ee57SDarrick J. Wong	int			level)
55427d9ee57SDarrick J. Wong{
55527d9ee57SDarrick J. Wong	struct xfs_btree_block	*block;
55627d9ee57SDarrick J. Wong	struct xfs_buf		*bp;
55727d9ee57SDarrick J. Wong
55827d9ee57SDarrick J. Wong	block = xfs_btree_get_block(cur, level, &bp);
55927d9ee57SDarrick J. Wong	ASSERT(block && xfs_btree_check_block(cur, block, level, bp) == 0);
56027d9ee57SDarrick J. Wong
56127d9ee57SDarrick J. Wong	if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
56227d9ee57SDarrick J. Wong		return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
56327d9ee57SDarrick J. Wong	return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
56427d9ee57SDarrick J. Wong}
56527d9ee57SDarrick J. Wong
56660e3d707SDarrick J. Wongvoid xfs_btree_set_ptr_null(struct xfs_btree_cur *cur,
56760e3d707SDarrick J. Wong		union xfs_btree_ptr *ptr);
56832816fd7SDarrick J. Wongint xfs_btree_get_buf_block(struct xfs_btree_cur *cur,
56932816fd7SDarrick J. Wong		const union xfs_btree_ptr *ptr, struct xfs_btree_block **block,
57032816fd7SDarrick J. Wong		struct xfs_buf **bpp);
57160e3d707SDarrick J. Wongvoid xfs_btree_set_sibling(struct xfs_btree_cur *cur,
57232816fd7SDarrick J. Wong		struct xfs_btree_block *block, const union xfs_btree_ptr *ptr,
57360e3d707SDarrick J. Wong		int lr);
57460e3d707SDarrick J. Wongvoid xfs_btree_init_block_cur(struct xfs_btree_cur *cur,
57560e3d707SDarrick J. Wong		struct xfs_buf *bp, int level, int numrecs);
57660e3d707SDarrick J. Wongvoid xfs_btree_copy_ptrs(struct xfs_btree_cur *cur,
57760e3d707SDarrick J. Wong		union xfs_btree_ptr *dst_ptr,
57860e3d707SDarrick J. Wong		const union xfs_btree_ptr *src_ptr, int numptrs);
57960e3d707SDarrick J. Wongvoid xfs_btree_copy_keys(struct xfs_btree_cur *cur,
58032816fd7SDarrick J. Wong		union xfs_btree_key *dst_key,
58132816fd7SDarrick J. Wong		const union xfs_btree_key *src_key, int numkeys);
58260e3d707SDarrick J. Wong
58356370ea6SDarrick J. Wongstatic inline struct xfs_btree_cur *
58456370ea6SDarrick J. Wongxfs_btree_alloc_cursor(
58556370ea6SDarrick J. Wong	struct xfs_mount	*mp,
58656370ea6SDarrick J. Wong	struct xfs_trans	*tp,
587c940a0c5SDarrick J. Wong	xfs_btnum_t		btnum,
5889fa47bdcSDarrick J. Wong	uint8_t			maxlevels,
589e7720afaSDarrick J. Wong	struct kmem_cache	*cache)
59056370ea6SDarrick J. Wong{
59156370ea6SDarrick J. Wong	struct xfs_btree_cur	*cur;
59256370ea6SDarrick J. Wong
5939fa47bdcSDarrick J. Wong	cur = kmem_cache_zalloc(cache, GFP_NOFS | __GFP_NOFAIL);
59456370ea6SDarrick J. Wong	cur->bc_tp = tp;
59556370ea6SDarrick J. Wong	cur->bc_mp = mp;
59656370ea6SDarrick J. Wong	cur->bc_btnum = btnum;
597c940a0c5SDarrick J. Wong	cur->bc_maxlevels = maxlevels;
5989fa47bdcSDarrick J. Wong	cur->bc_cache = cache;
59956370ea6SDarrick J. Wong
60056370ea6SDarrick J. Wong	return cur;
60156370ea6SDarrick J. Wong}
60256370ea6SDarrick J. Wong
6039fa47bdcSDarrick J. Wongint __init xfs_btree_init_cur_caches(void);
6049fa47bdcSDarrick J. Wongvoid xfs_btree_destroy_cur_caches(void);
6059fa47bdcSDarrick J. Wong
6061da177e4SLinus Torvalds#endif	/* __XFS_BTREE_H__ */