xref: /openbmc/linux/fs/xfs/libxfs/xfs_btree.h (revision dc6a81c3)
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 
14 extern kmem_zone_t	*xfs_btree_cur_zone;
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		(1 << 0)
72 #define	XFS_BB_LEVEL		(1 << 1)
73 #define	XFS_BB_NUMRECS		(1 << 2)
74 #define	XFS_BB_LEFTSIB		(1 << 3)
75 #define	XFS_BB_RIGHTSIB		(1 << 4)
76 #define	XFS_BB_BLKNO		(1 << 5)
77 #define	XFS_BB_LSN		(1 << 6)
78 #define	XFS_BB_UUID		(1 << 7)
79 #define	XFS_BB_OWNER		(1 << 8)
80 #define	XFS_BB_NUM_BITS		5
81 #define	XFS_BB_ALL_BITS		((1 << XFS_BB_NUM_BITS) - 1)
82 #define	XFS_BB_NUM_BITS_CRC	9
83 #define	XFS_BB_ALL_BITS_CRC	((1 << 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 #define	XFS_BTREE_MAXLEVELS	9	/* max of all btrees */
94 
95 struct xfs_btree_ops {
96 	/* size of the key and record structures */
97 	size_t	key_len;
98 	size_t	rec_len;
99 
100 	/* cursor operations */
101 	struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
102 	void	(*update_cursor)(struct xfs_btree_cur *src,
103 				 struct xfs_btree_cur *dst);
104 
105 	/* update btree root pointer */
106 	void	(*set_root)(struct xfs_btree_cur *cur,
107 			    union xfs_btree_ptr *nptr, int level_change);
108 
109 	/* block allocation / freeing */
110 	int	(*alloc_block)(struct xfs_btree_cur *cur,
111 			       union xfs_btree_ptr *start_bno,
112 			       union xfs_btree_ptr *new_bno,
113 			       int *stat);
114 	int	(*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
115 
116 	/* update last record information */
117 	void	(*update_lastrec)(struct xfs_btree_cur *cur,
118 				  struct xfs_btree_block *block,
119 				  union xfs_btree_rec *rec,
120 				  int ptr, int reason);
121 
122 	/* records in block/level */
123 	int	(*get_minrecs)(struct xfs_btree_cur *cur, int level);
124 	int	(*get_maxrecs)(struct xfs_btree_cur *cur, int level);
125 
126 	/* records on disk.  Matter for the root in inode case. */
127 	int	(*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
128 
129 	/* init values of btree structures */
130 	void	(*init_key_from_rec)(union xfs_btree_key *key,
131 				     union xfs_btree_rec *rec);
132 	void	(*init_rec_from_cur)(struct xfs_btree_cur *cur,
133 				     union xfs_btree_rec *rec);
134 	void	(*init_ptr_from_cur)(struct xfs_btree_cur *cur,
135 				     union xfs_btree_ptr *ptr);
136 	void	(*init_high_key_from_rec)(union xfs_btree_key *key,
137 					  union xfs_btree_rec *rec);
138 
139 	/* difference between key value and cursor value */
140 	int64_t (*key_diff)(struct xfs_btree_cur *cur,
141 			      union xfs_btree_key *key);
142 
143 	/*
144 	 * Difference between key2 and key1 -- positive if key1 > key2,
145 	 * negative if key1 < key2, and zero if equal.
146 	 */
147 	int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
148 				   union xfs_btree_key *key1,
149 				   union xfs_btree_key *key2);
150 
151 	const struct xfs_buf_ops	*buf_ops;
152 
153 	/* check that k1 is lower than k2 */
154 	int	(*keys_inorder)(struct xfs_btree_cur *cur,
155 				union xfs_btree_key *k1,
156 				union xfs_btree_key *k2);
157 
158 	/* check that r1 is lower than r2 */
159 	int	(*recs_inorder)(struct xfs_btree_cur *cur,
160 				union xfs_btree_rec *r1,
161 				union xfs_btree_rec *r2);
162 };
163 
164 /*
165  * Reasons for the update_lastrec method to be called.
166  */
167 #define LASTREC_UPDATE	0
168 #define LASTREC_INSREC	1
169 #define LASTREC_DELREC	2
170 
171 
172 union xfs_btree_irec {
173 	struct xfs_alloc_rec_incore	a;
174 	struct xfs_bmbt_irec		b;
175 	struct xfs_inobt_rec_incore	i;
176 	struct xfs_rmap_irec		r;
177 	struct xfs_refcount_irec	rc;
178 };
179 
180 /* Per-AG btree private information. */
181 union xfs_btree_cur_private {
182 	struct {
183 		unsigned long	nr_ops;		/* # record updates */
184 		int		shape_changes;	/* # of extent splits */
185 	} refc;
186 	struct {
187 		bool		active;		/* allocation cursor state */
188 	} abt;
189 };
190 
191 /*
192  * Btree cursor structure.
193  * This collects all information needed by the btree code in one place.
194  */
195 typedef struct xfs_btree_cur
196 {
197 	struct xfs_trans	*bc_tp;	/* transaction we're in, if any */
198 	struct xfs_mount	*bc_mp;	/* file system mount struct */
199 	const struct xfs_btree_ops *bc_ops;
200 	uint			bc_flags; /* btree features - below */
201 	union xfs_btree_irec	bc_rec;	/* current insert/search record value */
202 	struct xfs_buf	*bc_bufs[XFS_BTREE_MAXLEVELS];	/* buf ptr per level */
203 	int		bc_ptrs[XFS_BTREE_MAXLEVELS];	/* key/record # */
204 	uint8_t		bc_ra[XFS_BTREE_MAXLEVELS];	/* readahead bits */
205 #define	XFS_BTCUR_LEFTRA	1	/* left sibling has been read-ahead */
206 #define	XFS_BTCUR_RIGHTRA	2	/* right sibling has been read-ahead */
207 	uint8_t		bc_nlevels;	/* number of levels in the tree */
208 	uint8_t		bc_blocklog;	/* log2(blocksize) of btree blocks */
209 	xfs_btnum_t	bc_btnum;	/* identifies which btree type */
210 	int		bc_statoff;	/* offset of btre stats array */
211 	union {
212 		struct {			/* needed for BNO, CNT, INO */
213 			struct xfs_buf	*agbp;	/* agf/agi buffer pointer */
214 			xfs_agnumber_t	agno;	/* ag number */
215 			union xfs_btree_cur_private	priv;
216 		} a;
217 		struct {			/* needed for BMAP */
218 			struct xfs_inode *ip;	/* pointer to our inode */
219 			int		allocated;	/* count of alloced */
220 			short		forksize;	/* fork's inode space */
221 			char		whichfork;	/* data or attr fork */
222 			char		flags;		/* flags */
223 #define	XFS_BTCUR_BPRV_WASDEL		(1<<0)		/* was delayed */
224 #define	XFS_BTCUR_BPRV_INVALID_OWNER	(1<<1)		/* for ext swap */
225 		} b;
226 	}		bc_private;	/* per-btree type data */
227 } xfs_btree_cur_t;
228 
229 /* cursor flags */
230 #define XFS_BTREE_LONG_PTRS		(1<<0)	/* pointers are 64bits long */
231 #define XFS_BTREE_ROOT_IN_INODE		(1<<1)	/* root may be variable size */
232 #define XFS_BTREE_LASTREC_UPDATE	(1<<2)	/* track last rec externally */
233 #define XFS_BTREE_CRC_BLOCKS		(1<<3)	/* uses extended btree blocks */
234 #define XFS_BTREE_OVERLAPPING		(1<<4)	/* overlapping intervals */
235 
236 
237 #define	XFS_BTREE_NOERROR	0
238 #define	XFS_BTREE_ERROR		1
239 
240 /*
241  * Convert from buffer to btree block header.
242  */
243 #define	XFS_BUF_TO_BLOCK(bp)	((struct xfs_btree_block *)((bp)->b_addr))
244 
245 /*
246  * Internal long and short btree block checks.  They return NULL if the
247  * block is ok or the address of the failed check otherwise.
248  */
249 xfs_failaddr_t __xfs_btree_check_lblock(struct xfs_btree_cur *cur,
250 		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
251 xfs_failaddr_t __xfs_btree_check_sblock(struct xfs_btree_cur *cur,
252 		struct xfs_btree_block *block, int level, struct xfs_buf *bp);
253 
254 /*
255  * Check that block header is ok.
256  */
257 int
258 xfs_btree_check_block(
259 	struct xfs_btree_cur	*cur,	/* btree cursor */
260 	struct xfs_btree_block	*block,	/* generic btree block pointer */
261 	int			level,	/* level of the btree block */
262 	struct xfs_buf		*bp);	/* buffer containing block, if any */
263 
264 /*
265  * Check that (long) pointer is ok.
266  */
267 bool					/* error (0 or EFSCORRUPTED) */
268 xfs_btree_check_lptr(
269 	struct xfs_btree_cur	*cur,	/* btree cursor */
270 	xfs_fsblock_t		fsbno,	/* btree block disk address */
271 	int			level);	/* btree block level */
272 
273 /*
274  * Check that (short) pointer is ok.
275  */
276 bool					/* error (0 or EFSCORRUPTED) */
277 xfs_btree_check_sptr(
278 	struct xfs_btree_cur	*cur,	/* btree cursor */
279 	xfs_agblock_t		agbno,	/* btree block disk address */
280 	int			level);	/* btree block level */
281 
282 /*
283  * Delete the btree cursor.
284  */
285 void
286 xfs_btree_del_cursor(
287 	xfs_btree_cur_t		*cur,	/* btree cursor */
288 	int			error);	/* del because of error */
289 
290 /*
291  * Duplicate the btree cursor.
292  * Allocate a new one, copy the record, re-get the buffers.
293  */
294 int					/* error */
295 xfs_btree_dup_cursor(
296 	xfs_btree_cur_t		*cur,	/* input cursor */
297 	xfs_btree_cur_t		**ncur);/* output cursor */
298 
299 /*
300  * Compute first and last byte offsets for the fields given.
301  * Interprets the offsets table, which contains struct field offsets.
302  */
303 void
304 xfs_btree_offsets(
305 	int64_t			fields,	/* bitmask of fields */
306 	const short		*offsets,/* table of field offsets */
307 	int			nbits,	/* number of bits to inspect */
308 	int			*first,	/* output: first byte offset */
309 	int			*last);	/* output: last byte offset */
310 
311 /*
312  * Get a buffer for the block, return it read in.
313  * Long-form addressing.
314  */
315 int					/* error */
316 xfs_btree_read_bufl(
317 	struct xfs_mount	*mp,	/* file system mount point */
318 	struct xfs_trans	*tp,	/* transaction pointer */
319 	xfs_fsblock_t		fsbno,	/* file system block number */
320 	struct xfs_buf		**bpp,	/* buffer for fsbno */
321 	int			refval,	/* ref count value for buffer */
322 	const struct xfs_buf_ops *ops);
323 
324 /*
325  * Read-ahead the block, don't wait for it, don't return a buffer.
326  * Long-form addressing.
327  */
328 void					/* error */
329 xfs_btree_reada_bufl(
330 	struct xfs_mount	*mp,	/* file system mount point */
331 	xfs_fsblock_t		fsbno,	/* file system block number */
332 	xfs_extlen_t		count,	/* count of filesystem blocks */
333 	const struct xfs_buf_ops *ops);
334 
335 /*
336  * Read-ahead the block, don't wait for it, don't return a buffer.
337  * Short-form addressing.
338  */
339 void					/* error */
340 xfs_btree_reada_bufs(
341 	struct xfs_mount	*mp,	/* file system mount point */
342 	xfs_agnumber_t		agno,	/* allocation group number */
343 	xfs_agblock_t		agbno,	/* allocation group block number */
344 	xfs_extlen_t		count,	/* count of filesystem blocks */
345 	const struct xfs_buf_ops *ops);
346 
347 /*
348  * Initialise a new btree block header
349  */
350 void
351 xfs_btree_init_block(
352 	struct xfs_mount *mp,
353 	struct xfs_buf	*bp,
354 	xfs_btnum_t	btnum,
355 	__u16		level,
356 	__u16		numrecs,
357 	__u64		owner);
358 
359 void
360 xfs_btree_init_block_int(
361 	struct xfs_mount	*mp,
362 	struct xfs_btree_block	*buf,
363 	xfs_daddr_t		blkno,
364 	xfs_btnum_t		btnum,
365 	__u16			level,
366 	__u16			numrecs,
367 	__u64			owner,
368 	unsigned int		flags);
369 
370 /*
371  * Common btree core entry points.
372  */
373 int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
374 int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
375 int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
376 int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
377 int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
378 int xfs_btree_insert(struct xfs_btree_cur *, int *);
379 int xfs_btree_delete(struct xfs_btree_cur *, int *);
380 int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
381 int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
382 			   struct list_head *buffer_list);
383 
384 /*
385  * btree block CRC helpers
386  */
387 void xfs_btree_lblock_calc_crc(struct xfs_buf *);
388 bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
389 void xfs_btree_sblock_calc_crc(struct xfs_buf *);
390 bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
391 
392 /*
393  * Internal btree helpers also used by xfs_bmap.c.
394  */
395 void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
396 void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
397 
398 /*
399  * Helpers.
400  */
401 static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
402 {
403 	return be16_to_cpu(block->bb_numrecs);
404 }
405 
406 static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
407 		uint16_t numrecs)
408 {
409 	block->bb_numrecs = cpu_to_be16(numrecs);
410 }
411 
412 static inline int xfs_btree_get_level(struct xfs_btree_block *block)
413 {
414 	return be16_to_cpu(block->bb_level);
415 }
416 
417 
418 /*
419  * Min and max functions for extlen, agblock, fileoff, and filblks types.
420  */
421 #define	XFS_EXTLEN_MIN(a,b)	min_t(xfs_extlen_t, (a), (b))
422 #define	XFS_EXTLEN_MAX(a,b)	max_t(xfs_extlen_t, (a), (b))
423 #define	XFS_AGBLOCK_MIN(a,b)	min_t(xfs_agblock_t, (a), (b))
424 #define	XFS_AGBLOCK_MAX(a,b)	max_t(xfs_agblock_t, (a), (b))
425 #define	XFS_FILEOFF_MIN(a,b)	min_t(xfs_fileoff_t, (a), (b))
426 #define	XFS_FILEOFF_MAX(a,b)	max_t(xfs_fileoff_t, (a), (b))
427 #define	XFS_FILBLKS_MIN(a,b)	min_t(xfs_filblks_t, (a), (b))
428 #define	XFS_FILBLKS_MAX(a,b)	max_t(xfs_filblks_t, (a), (b))
429 
430 xfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
431 xfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
432 		unsigned int max_recs);
433 xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
434 		uint64_t owner);
435 xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
436 		unsigned int max_recs);
437 
438 uint xfs_btree_compute_maxlevels(uint *limits, unsigned long len);
439 unsigned long long xfs_btree_calc_size(uint *limits, unsigned long long len);
440 
441 /*
442  * Return codes for the query range iterator function are 0 to continue
443  * iterating, and non-zero to stop iterating.  Any non-zero value will be
444  * passed up to the _query_range caller.  The special value -ECANCELED can be
445  * used to stop iteration, because _query_range never generates that error
446  * code on its own.
447  */
448 typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
449 		union xfs_btree_rec *rec, void *priv);
450 
451 int xfs_btree_query_range(struct xfs_btree_cur *cur,
452 		union xfs_btree_irec *low_rec, union xfs_btree_irec *high_rec,
453 		xfs_btree_query_range_fn fn, void *priv);
454 int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
455 		void *priv);
456 
457 typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
458 		void *data);
459 /* Visit record blocks. */
460 #define XFS_BTREE_VISIT_RECORDS		(1 << 0)
461 /* Visit leaf blocks. */
462 #define XFS_BTREE_VISIT_LEAVES		(1 << 1)
463 /* Visit all blocks. */
464 #define XFS_BTREE_VISIT_ALL		(XFS_BTREE_VISIT_RECORDS | \
465 					 XFS_BTREE_VISIT_LEAVES)
466 int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
467 		xfs_btree_visit_blocks_fn fn, unsigned int flags, void *data);
468 
469 int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
470 
471 union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
472 		struct xfs_btree_block *block);
473 union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
474 		struct xfs_btree_block *block);
475 union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
476 		struct xfs_btree_block *block);
477 union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
478 		struct xfs_btree_block *block);
479 int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
480 		union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
481 struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
482 		int level, struct xfs_buf **bpp);
483 bool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur, union xfs_btree_ptr *ptr);
484 int64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur *cur,
485 				const union xfs_btree_ptr *a,
486 				const union xfs_btree_ptr *b);
487 void xfs_btree_get_sibling(struct xfs_btree_cur *cur,
488 			   struct xfs_btree_block *block,
489 			   union xfs_btree_ptr *ptr, int lr);
490 void xfs_btree_get_keys(struct xfs_btree_cur *cur,
491 		struct xfs_btree_block *block, union xfs_btree_key *key);
492 union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
493 		union xfs_btree_key *key);
494 int xfs_btree_has_record(struct xfs_btree_cur *cur, union xfs_btree_irec *low,
495 		union xfs_btree_irec *high, bool *exists);
496 bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
497 
498 /* Does this cursor point to the last block in the given level? */
499 static inline bool
500 xfs_btree_islastblock(
501 	xfs_btree_cur_t		*cur,
502 	int			level)
503 {
504 	struct xfs_btree_block	*block;
505 	struct xfs_buf		*bp;
506 
507 	block = xfs_btree_get_block(cur, level, &bp);
508 	ASSERT(block && xfs_btree_check_block(cur, block, level, bp) == 0);
509 
510 	if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
511 		return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
512 	return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
513 }
514 
515 #endif	/* __XFS_BTREE_H__ */
516