]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_btree.h
xfs: use separate btree cursor cache for each btree type
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_btree.h
CommitLineData
7acf15bf 1/* SPDX-License-Identifier: GPL-2.0 */
2bd0ea18 2/*
f302e9e4
NS
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18
NS
5 */
6#ifndef __XFS_BTREE_H__
dfc130f3 7#define __XFS_BTREE_H__
2bd0ea18
NS
8
9struct xfs_buf;
2bd0ea18
NS
10struct xfs_inode;
11struct xfs_mount;
12struct xfs_trans;
cdd89807 13struct xfs_ifork;
ca7d293d 14struct xfs_perag;
2bd0ea18 15
ff105f75
DC
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 */
22union xfs_btree_ptr {
23 __be32 s; /* short form ptr */
24 __be64 l; /* long form ptr */
25};
26
13e831e0 27/*
45413937
DW
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.
13e831e0 31 */
45413937 32union xfs_btree_key {
13e831e0
DW
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;
45413937
DW
37 struct xfs_rmap_key rmap;
38 struct xfs_rmap_key __rmap_bigkey[2];
d8079fe0 39 struct xfs_refcount_key refc;
13e831e0
DW
40};
41
ff105f75 42union xfs_btree_rec {
b3a96b46
DW
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;
d8079fe0 48 struct xfs_refcount_rec refc;
ff105f75
DC
49};
50
2bd0ea18
NS
51/*
52 * This nonsense is to make -wlint happy.
53 */
dfc130f3
RC
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)
2bd0ea18 57
dfc130f3
RC
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)
c0a4c227 62#define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi)
e37838e5 63#define XFS_BTNUM_RMAP ((xfs_btnum_t)XFS_BTNUM_RMAPi)
88ce0792 64#define XFS_BTNUM_REFC ((xfs_btnum_t)XFS_BTNUM_REFCi)
2bd0ea18 65
4a492e72 66uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
4c6b3277 67
2bd0ea18
NS
68/*
69 * For logging record fields.
70 */
9c6ebc42
DC
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)
dfc130f3
RC
80#define XFS_BB_NUM_BITS 5
81#define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
9c6ebc42 82#define XFS_BB_NUM_BITS_CRC 9
5dfa5cd2 83#define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1)
2bd0ea18 84
b194c7d8
BN
85/*
86 * Generic stats interface
87 */
79896434 88#define XFS_BTREE_STATS_INC(cur, stat) \
5d8acc46 89 XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
8fb63e2a 90#define XFS_BTREE_STATS_ADD(cur, stat, val) \
5d8acc46 91 XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
2bd0ea18 92
b194c7d8
BN
93struct 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,
67e6075e 105 const union xfs_btree_ptr *nptr, int level_change);
b194c7d8
BN
106
107 /* block allocation / freeing */
108 int (*alloc_block)(struct xfs_btree_cur *cur,
43cbf380 109 const union xfs_btree_ptr *start_bno,
b194c7d8 110 union xfs_btree_ptr *new_bno,
ff105f75 111 int *stat);
b194c7d8
BN
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,
99c5a767
DW
116 const struct xfs_btree_block *block,
117 const union xfs_btree_rec *rec,
b194c7d8
BN
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,
c65978b6 129 const union xfs_btree_rec *rec);
b194c7d8
BN
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);
13e831e0 134 void (*init_high_key_from_rec)(union xfs_btree_key *key,
c65978b6 135 const union xfs_btree_rec *rec);
b194c7d8
BN
136
137 /* difference between key value and cursor value */
4a492e72 138 int64_t (*key_diff)(struct xfs_btree_cur *cur,
901acb0e 139 const union xfs_btree_key *key);
b194c7d8 140
13e831e0
DW
141 /*
142 * Difference between key2 and key1 -- positive if key1 > key2,
143 * negative if key1 < key2, and zero if equal.
144 */
4a492e72 145 int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
901acb0e
DW
146 const union xfs_btree_key *key1,
147 const union xfs_btree_key *key2);
13e831e0 148
a2ceac1f
DC
149 const struct xfs_buf_ops *buf_ops;
150
b194c7d8
BN
151 /* check that k1 is lower than k2 */
152 int (*keys_inorder)(struct xfs_btree_cur *cur,
141bbc5c
DW
153 const union xfs_btree_key *k1,
154 const union xfs_btree_key *k2);
b194c7d8
BN
155
156 /* check that r1 is lower than r2 */
157 int (*recs_inorder)(struct xfs_btree_cur *cur,
141bbc5c
DW
158 const union xfs_btree_rec *r1,
159 const union xfs_btree_rec *r2);
b194c7d8
BN
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
5d3b7fe1
DW
170union xfs_btree_irec {
171 struct xfs_alloc_rec_incore a;
172 struct xfs_bmbt_irec b;
173 struct xfs_inobt_rec_incore i;
936ca687 174 struct xfs_rmap_irec r;
d8079fe0
DW
175 struct xfs_refcount_irec rc;
176};
177
ead943ee
DC
178/* Per-AG btree information. */
179struct xfs_btree_cur_ag {
4c0ddd16 180 struct xfs_perag *pag;
af05bc9b
DW
181 union {
182 struct xfs_buf *agbp;
183 struct xbtree_afakeroot *afake; /* for staging cursor */
184 };
63789af3
DC
185 union {
186 struct {
4c0ddd16
DW
187 unsigned int nr_ops; /* # record updates */
188 unsigned int shape_changes; /* # of extent splits */
63789af3
DC
189 } refc;
190 struct {
4c0ddd16 191 bool active; /* allocation cursor state */
63789af3
DC
192 } abt;
193 };
ead943ee
DC
194};
195
196/* Btree-in-inode cursor information */
197struct xfs_btree_cur_ino {
198 struct xfs_inode *ip;
cdd89807 199 struct xbtree_ifakeroot *ifake; /* for staging cursor */
ead943ee
DC
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
5df9b067
DW
211struct 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
2bd0ea18
NS
224/*
225 * Btree cursor structure.
226 * This collects all information needed by the btree code in one place.
227 */
ec924d04 228struct xfs_btree_cur
2bd0ea18 229{
dfc130f3
RC
230 struct xfs_trans *bc_tp; /* transaction we're in, if any */
231 struct xfs_mount *bc_mp; /* file system mount struct */
b194c7d8 232 const struct xfs_btree_ops *bc_ops;
7d10d094 233 kmem_zone_t *bc_cache; /* cursor cache */
1640bbbd
DW
234 unsigned int bc_flags; /* btree features - below */
235 xfs_btnum_t bc_btnum; /* identifies which btree type */
5d3b7fe1 236 union xfs_btree_irec bc_rec; /* current insert/search record value */
1640bbbd 237 uint8_t bc_nlevels; /* number of levels in the tree */
100a1b52 238 uint8_t bc_maxlevels; /* maximum levels for this btree type */
1640bbbd 239 int bc_statoff; /* offset of btree stats array */
ca7d293d
DC
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 */
2bd0ea18 247 union {
ead943ee
DC
248 struct xfs_btree_cur_ag bc_ag;
249 struct xfs_btree_cur_ino bc_ino;
250 };
5df9b067
DW
251
252 /* Must be at the end of the struct! */
253 struct xfs_btree_level bc_levels[];
ec924d04 254};
2bd0ea18 255
5df9b067
DW
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 */
261static inline size_t
262xfs_btree_cur_sizeof(unsigned int nlevels)
263{
264 return struct_size((struct xfs_btree_cur *)NULL, bc_levels, nlevels);
265}
266
b194c7d8
BN
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 */
5dfa5cd2 271#define XFS_BTREE_CRC_BLOCKS (1<<3) /* uses extended btree blocks */
13e831e0 272#define XFS_BTREE_OVERLAPPING (1<<4) /* overlapping intervals */
af05bc9b
DW
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)
b194c7d8 279
dfc130f3
RC
280#define XFS_BTREE_NOERROR 0
281#define XFS_BTREE_ERROR 1
2bd0ea18
NS
282
283/*
284 * Convert from buffer to btree block header.
285 */
a2ceac1f 286#define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
f302e9e4 287
b47beff8
DW
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 */
292xfs_failaddr_t __xfs_btree_check_lblock(struct xfs_btree_cur *cur,
293 struct xfs_btree_block *block, int level, struct xfs_buf *bp);
294xfs_failaddr_t __xfs_btree_check_sblock(struct xfs_btree_cur *cur,
295 struct xfs_btree_block *block, int level, struct xfs_buf *bp);
2bd0ea18 296
2bd0ea18 297/*
b194c7d8 298 * Check that block header is ok.
2bd0ea18 299 */
b194c7d8
BN
300int
301xfs_btree_check_block(
302 struct xfs_btree_cur *cur, /* btree cursor */
303 struct xfs_btree_block *block, /* generic btree block pointer */
2bd0ea18
NS
304 int level, /* level of the btree block */
305 struct xfs_buf *bp); /* buffer containing block, if any */
306
307/*
b194c7d8 308 * Check that (long) pointer is ok.
2bd0ea18 309 */
d5c546df 310bool /* error (0 or EFSCORRUPTED) */
2bd0ea18 311xfs_btree_check_lptr(
b194c7d8 312 struct xfs_btree_cur *cur, /* btree cursor */
d5c546df
DW
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 */
319bool /* error (0 or EFSCORRUPTED) */
320xfs_btree_check_sptr(
321 struct xfs_btree_cur *cur, /* btree cursor */
322 xfs_agblock_t agbno, /* btree block disk address */
dfc130f3 323 int level); /* btree block level */
2bd0ea18 324
2bd0ea18
NS
325/*
326 * Delete the btree cursor.
327 */
328void
329xfs_btree_del_cursor(
ec924d04 330 struct xfs_btree_cur *cur, /* btree cursor */
dfc130f3 331 int error); /* del because of error */
2bd0ea18
NS
332
333/*
334 * Duplicate the btree cursor.
335 * Allocate a new one, copy the record, re-get the buffers.
336 */
337int /* error */
338xfs_btree_dup_cursor(
ec924d04
DW
339 struct xfs_btree_cur *cur, /* input cursor */
340 struct xfs_btree_cur **ncur);/* output cursor */
2bd0ea18 341
2bd0ea18
NS
342/*
343 * Compute first and last byte offsets for the fields given.
344 * Interprets the offsets table, which contains struct field offsets.
345 */
346void
347xfs_btree_offsets(
4a492e72 348 int64_t fields, /* bitmask of fields */
2bd0ea18
NS
349 const short *offsets,/* table of field offsets */
350 int nbits, /* number of bits to inspect */
dfc130f3
RC
351 int *first, /* output: first byte offset */
352 int *last); /* output: last byte offset */
2bd0ea18
NS
353
354/*
355 * Get a buffer for the block, return it read in.
356 * Long-form addressing.
357 */
358int /* error */
359xfs_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 */
2bd0ea18 363 struct xfs_buf **bpp, /* buffer for fsbno */
a2ceac1f
DC
364 int refval, /* ref count value for buffer */
365 const struct xfs_buf_ops *ops);
2bd0ea18 366
2bd0ea18
NS
367/*
368 * Read-ahead the block, don't wait for it, don't return a buffer.
369 * Long-form addressing.
370 */
371void /* error */
372xfs_btree_reada_bufl(
373 struct xfs_mount *mp, /* file system mount point */
374 xfs_fsblock_t fsbno, /* file system block number */
a2ceac1f
DC
375 xfs_extlen_t count, /* count of filesystem blocks */
376 const struct xfs_buf_ops *ops);
2bd0ea18
NS
377
378/*
379 * Read-ahead the block, don't wait for it, don't return a buffer.
380 * Short-form addressing.
381 */
382void /* error */
383xfs_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 */
a2ceac1f
DC
387 xfs_extlen_t count, /* count of filesystem blocks */
388 const struct xfs_buf_ops *ops);
2bd0ea18 389
a2ceac1f
DC
390/*
391 * Initialise a new btree block header
392 */
393void
394xfs_btree_init_block(
395 struct xfs_mount *mp,
396 struct xfs_buf *bp,
e394a4b1 397 xfs_btnum_t btnum,
a2ceac1f
DC
398 __u16 level,
399 __u16 numrecs,
4aa01a59 400 __u64 owner);
60ca9704 401
5dfa5cd2
DC
402void
403xfs_btree_init_block_int(
404 struct xfs_mount *mp,
405 struct xfs_btree_block *buf,
406 xfs_daddr_t blkno,
e394a4b1 407 xfs_btnum_t btnum,
5dfa5cd2
DC
408 __u16 level,
409 __u16 numrecs,
410 __u64 owner,
411 unsigned int flags);
412
b194c7d8
BN
413/*
414 * Common btree core entry points.
415 */
416int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
417int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
418int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
419int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
420int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
b194c7d8
BN
421int xfs_btree_insert(struct xfs_btree_cur *, int *);
422int xfs_btree_delete(struct xfs_btree_cur *, int *);
423int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
4a492e72 424int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
9c6ebc42 425 struct list_head *buffer_list);
60ca9704 426
5dfa5cd2
DC
427/*
428 * btree block CRC helpers
429 */
430void xfs_btree_lblock_calc_crc(struct xfs_buf *);
431bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
432void xfs_btree_sblock_calc_crc(struct xfs_buf *);
433bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
434
b194c7d8
BN
435/*
436 * Internal btree helpers also used by xfs_bmap.c.
437 */
438void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
439void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
60ca9704 440
2bd0ea18 441/*
b194c7d8 442 * Helpers.
2bd0ea18 443 */
99c5a767 444static inline int xfs_btree_get_numrecs(const struct xfs_btree_block *block)
b194c7d8
BN
445{
446 return be16_to_cpu(block->bb_numrecs);
447}
448
449static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
4a492e72 450 uint16_t numrecs)
b194c7d8
BN
451{
452 block->bb_numrecs = cpu_to_be16(numrecs);
453}
454
99c5a767 455static inline int xfs_btree_get_level(const struct xfs_btree_block *block)
b194c7d8
BN
456{
457 return be16_to_cpu(block->bb_level);
458}
2bd0ea18 459
2bd0ea18
NS
460
461/*
462 * Min and max functions for extlen, agblock, fileoff, and filblks types.
463 */
5e656dbb
BN
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))
f302e9e4 472
bc01119d
DW
473xfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
474xfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
475 unsigned int max_recs);
476xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
477 uint64_t owner);
478xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
479 unsigned int max_recs);
fb75464f 480
3024d6c9
DW
481unsigned int xfs_btree_compute_maxlevels(const unsigned int *limits,
482 unsigned long long records);
483unsigned long long xfs_btree_calc_size(const unsigned int *limits,
484 unsigned long long records);
4a9e48bf
DW
485unsigned int xfs_btree_space_to_height(const unsigned int *limits,
486 unsigned long long blocks);
dbca0167 487
a0f17dde
DW
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 */
5d3b7fe1 495typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
e62318a3 496 const union xfs_btree_rec *rec, void *priv);
5d3b7fe1
DW
497
498int xfs_btree_query_range(struct xfs_btree_cur *cur,
d34c6373
DW
499 const union xfs_btree_irec *low_rec,
500 const union xfs_btree_irec *high_rec,
5d3b7fe1 501 xfs_btree_query_range_fn fn, void *priv);
7e05e856
DW
502int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
503 void *priv);
5d3b7fe1 504
f31736bf
DW
505typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
506 void *data);
9a7ae5a1
DW
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)
f31736bf 514int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
9a7ae5a1 515 xfs_btree_visit_blocks_fn fn, unsigned int flags, void *data);
f31736bf 516
2cccc8ce
DW
517int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
518
50bb67d6
DW
519union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
520 struct xfs_btree_block *block);
521union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
522 struct xfs_btree_block *block);
523union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
524 struct xfs_btree_block *block);
525union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
526 struct xfs_btree_block *block);
527int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
36f59768 528 const union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
50bb67d6
DW
529struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
530 int level, struct xfs_buf **bpp);
36f59768
DW
531bool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur,
532 const union xfs_btree_ptr *ptr);
15c4e6e4
DW
533int64_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);
536void xfs_btree_get_sibling(struct xfs_btree_cur *cur,
537 struct xfs_btree_block *block,
538 union xfs_btree_ptr *ptr, int lr);
0ad12e7f
DW
539void xfs_btree_get_keys(struct xfs_btree_cur *cur,
540 struct xfs_btree_block *block, union xfs_btree_key *key);
541union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
542 union xfs_btree_key *key);
36f59768
DW
543int 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);
43e73fb0 546bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
cdd89807 547struct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
50bb67d6 548
85b0f9e0
DW
549/* Does this cursor point to the last block in the given level? */
550static inline bool
551xfs_btree_islastblock(
ec924d04 552 struct xfs_btree_cur *cur,
85b0f9e0
DW
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
e6d5a6f8
DW
566void xfs_btree_set_ptr_null(struct xfs_btree_cur *cur,
567 union xfs_btree_ptr *ptr);
36f59768
DW
568int 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);
e6d5a6f8 571void xfs_btree_set_sibling(struct xfs_btree_cur *cur,
36f59768 572 struct xfs_btree_block *block, const union xfs_btree_ptr *ptr,
e6d5a6f8
DW
573 int lr);
574void xfs_btree_init_block_cur(struct xfs_btree_cur *cur,
575 struct xfs_buf *bp, int level, int numrecs);
576void 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);
579void xfs_btree_copy_keys(struct xfs_btree_cur *cur,
36f59768
DW
580 union xfs_btree_key *dst_key,
581 const union xfs_btree_key *src_key, int numkeys);
e6d5a6f8 582
1586915a
DW
583static inline struct xfs_btree_cur *
584xfs_btree_alloc_cursor(
585 struct xfs_mount *mp,
586 struct xfs_trans *tp,
22f64346 587 xfs_btnum_t btnum,
7d10d094
DW
588 uint8_t maxlevels,
589 kmem_zone_t *cache)
1586915a
DW
590{
591 struct xfs_btree_cur *cur;
592
7d10d094 593 cur = kmem_cache_zalloc(cache, GFP_NOFS | __GFP_NOFAIL);
1586915a
DW
594 cur->bc_tp = tp;
595 cur->bc_mp = mp;
596 cur->bc_btnum = btnum;
22f64346 597 cur->bc_maxlevels = maxlevels;
7d10d094 598 cur->bc_cache = cache;
1586915a
DW
599
600 return cur;
601}
602
7d10d094
DW
603int __init xfs_btree_init_cur_caches(void);
604void xfs_btree_destroy_cur_caches(void);
605
2bd0ea18 606#endif /* __XFS_BTREE_H__ */