]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_btree.h
xfs: remove xfs_btree_cur bmbt dfops field
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_btree.h
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_defer_ops;
11 struct xfs_inode;
12 struct xfs_mount;
13 struct xfs_trans;
14
15 extern kmem_zone_t *xfs_btree_cur_zone;
16
17 /*
18 * Generic key, ptr and record wrapper structures.
19 *
20 * These are disk format structures, and are converted where necessary
21 * by the btree specific code that needs to interpret them.
22 */
23 union xfs_btree_ptr {
24 __be32 s; /* short form ptr */
25 __be64 l; /* long form ptr */
26 };
27
28 /*
29 * The in-core btree key. Overlapping btrees actually store two keys
30 * per pointer, so we reserve enough memory to hold both. The __*bigkey
31 * items should never be accessed directly.
32 */
33 union xfs_btree_key {
34 struct xfs_bmbt_key bmbt;
35 xfs_bmdr_key_t bmbr; /* bmbt root block */
36 xfs_alloc_key_t alloc;
37 struct xfs_inobt_key inobt;
38 struct xfs_rmap_key rmap;
39 struct xfs_rmap_key __rmap_bigkey[2];
40 struct xfs_refcount_key refc;
41 };
42
43 union xfs_btree_rec {
44 struct xfs_bmbt_rec bmbt;
45 xfs_bmdr_rec_t bmbr; /* bmbt root block */
46 struct xfs_alloc_rec alloc;
47 struct xfs_inobt_rec inobt;
48 struct xfs_rmap_rec rmap;
49 struct xfs_refcount_rec refc;
50 };
51
52 /*
53 * This nonsense is to make -wlint happy.
54 */
55 #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
56 #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
57 #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
58
59 #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
60 #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
61 #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
62 #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
63 #define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi)
64 #define XFS_BTNUM_RMAP ((xfs_btnum_t)XFS_BTNUM_RMAPi)
65 #define XFS_BTNUM_REFC ((xfs_btnum_t)XFS_BTNUM_REFCi)
66
67 uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
68
69 /*
70 * For logging record fields.
71 */
72 #define XFS_BB_MAGIC (1 << 0)
73 #define XFS_BB_LEVEL (1 << 1)
74 #define XFS_BB_NUMRECS (1 << 2)
75 #define XFS_BB_LEFTSIB (1 << 3)
76 #define XFS_BB_RIGHTSIB (1 << 4)
77 #define XFS_BB_BLKNO (1 << 5)
78 #define XFS_BB_LSN (1 << 6)
79 #define XFS_BB_UUID (1 << 7)
80 #define XFS_BB_OWNER (1 << 8)
81 #define XFS_BB_NUM_BITS 5
82 #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
83 #define XFS_BB_NUM_BITS_CRC 9
84 #define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1)
85
86 /*
87 * Generic stats interface
88 */
89 #define XFS_BTREE_STATS_INC(cur, stat) \
90 XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
91 #define XFS_BTREE_STATS_ADD(cur, stat, val) \
92 XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
93
94 #define XFS_BTREE_MAXLEVELS 9 /* max of all btrees */
95
96 struct xfs_btree_ops {
97 /* size of the key and record structures */
98 size_t key_len;
99 size_t rec_len;
100
101 /* cursor operations */
102 struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
103 void (*update_cursor)(struct xfs_btree_cur *src,
104 struct xfs_btree_cur *dst);
105
106 /* update btree root pointer */
107 void (*set_root)(struct xfs_btree_cur *cur,
108 union xfs_btree_ptr *nptr, int level_change);
109
110 /* block allocation / freeing */
111 int (*alloc_block)(struct xfs_btree_cur *cur,
112 union xfs_btree_ptr *start_bno,
113 union xfs_btree_ptr *new_bno,
114 int *stat);
115 int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
116
117 /* update last record information */
118 void (*update_lastrec)(struct xfs_btree_cur *cur,
119 struct xfs_btree_block *block,
120 union xfs_btree_rec *rec,
121 int ptr, int reason);
122
123 /* records in block/level */
124 int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
125 int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
126
127 /* records on disk. Matter for the root in inode case. */
128 int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
129
130 /* init values of btree structures */
131 void (*init_key_from_rec)(union xfs_btree_key *key,
132 union xfs_btree_rec *rec);
133 void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
134 union xfs_btree_rec *rec);
135 void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
136 union xfs_btree_ptr *ptr);
137 void (*init_high_key_from_rec)(union xfs_btree_key *key,
138 union xfs_btree_rec *rec);
139
140 /* difference between key value and cursor value */
141 int64_t (*key_diff)(struct xfs_btree_cur *cur,
142 union xfs_btree_key *key);
143
144 /*
145 * Difference between key2 and key1 -- positive if key1 > key2,
146 * negative if key1 < key2, and zero if equal.
147 */
148 int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
149 union xfs_btree_key *key1,
150 union xfs_btree_key *key2);
151
152 const struct xfs_buf_ops *buf_ops;
153
154 /* check that k1 is lower than k2 */
155 int (*keys_inorder)(struct xfs_btree_cur *cur,
156 union xfs_btree_key *k1,
157 union xfs_btree_key *k2);
158
159 /* check that r1 is lower than r2 */
160 int (*recs_inorder)(struct xfs_btree_cur *cur,
161 union xfs_btree_rec *r1,
162 union xfs_btree_rec *r2);
163 };
164
165 /*
166 * Reasons for the update_lastrec method to be called.
167 */
168 #define LASTREC_UPDATE 0
169 #define LASTREC_INSREC 1
170 #define LASTREC_DELREC 2
171
172
173 union xfs_btree_irec {
174 struct xfs_alloc_rec_incore a;
175 struct xfs_bmbt_irec b;
176 struct xfs_inobt_rec_incore i;
177 struct xfs_rmap_irec r;
178 struct xfs_refcount_irec rc;
179 };
180
181 /* Per-AG btree private information. */
182 union xfs_btree_cur_private {
183 struct {
184 unsigned long nr_ops; /* # record updates */
185 int shape_changes; /* # of extent splits */
186 } refc;
187 };
188
189 /*
190 * Btree cursor structure.
191 * This collects all information needed by the btree code in one place.
192 */
193 typedef struct xfs_btree_cur
194 {
195 struct xfs_trans *bc_tp; /* transaction we're in, if any */
196 struct xfs_mount *bc_mp; /* file system mount struct */
197 const struct xfs_btree_ops *bc_ops;
198 uint bc_flags; /* btree features - below */
199 union xfs_btree_irec bc_rec; /* current insert/search record value */
200 struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
201 int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
202 uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
203 #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
204 #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
205 uint8_t bc_nlevels; /* number of levels in the tree */
206 uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
207 xfs_btnum_t bc_btnum; /* identifies which btree type */
208 int bc_statoff; /* offset of btre stats array */
209 union {
210 struct { /* needed for BNO, CNT, INO */
211 struct xfs_buf *agbp; /* agf/agi buffer pointer */
212 struct xfs_defer_ops *dfops; /* deferred updates */
213 xfs_agnumber_t agno; /* ag number */
214 union xfs_btree_cur_private priv;
215 } a;
216 struct { /* needed for BMAP */
217 struct xfs_inode *ip; /* pointer to our inode */
218 xfs_fsblock_t firstblock; /* 1st blk allocated */
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 * Get a buffer for the block, return it with no data read.
301 * Long-form addressing.
302 */
303 struct xfs_buf * /* buffer for fsbno */
304 xfs_btree_get_bufl(
305 struct xfs_mount *mp, /* file system mount point */
306 struct xfs_trans *tp, /* transaction pointer */
307 xfs_fsblock_t fsbno, /* file system block number */
308 uint lock); /* lock flags for get_buf */
309
310 /*
311 * Get a buffer for the block, return it with no data read.
312 * Short-form addressing.
313 */
314 struct xfs_buf * /* buffer for agno/agbno */
315 xfs_btree_get_bufs(
316 struct xfs_mount *mp, /* file system mount point */
317 struct xfs_trans *tp, /* transaction pointer */
318 xfs_agnumber_t agno, /* allocation group number */
319 xfs_agblock_t agbno, /* allocation group block number */
320 uint lock); /* lock flags for get_buf */
321
322 /*
323 * Check for the cursor referring to the last block at the given level.
324 */
325 int /* 1=is last block, 0=not last block */
326 xfs_btree_islastblock(
327 xfs_btree_cur_t *cur, /* btree cursor */
328 int level); /* level to check */
329
330 /*
331 * Compute first and last byte offsets for the fields given.
332 * Interprets the offsets table, which contains struct field offsets.
333 */
334 void
335 xfs_btree_offsets(
336 int64_t fields, /* bitmask of fields */
337 const short *offsets,/* table of field offsets */
338 int nbits, /* number of bits to inspect */
339 int *first, /* output: first byte offset */
340 int *last); /* output: last byte offset */
341
342 /*
343 * Get a buffer for the block, return it read in.
344 * Long-form addressing.
345 */
346 int /* error */
347 xfs_btree_read_bufl(
348 struct xfs_mount *mp, /* file system mount point */
349 struct xfs_trans *tp, /* transaction pointer */
350 xfs_fsblock_t fsbno, /* file system block number */
351 uint lock, /* lock flags for read_buf */
352 struct xfs_buf **bpp, /* buffer for fsbno */
353 int refval, /* ref count value for buffer */
354 const struct xfs_buf_ops *ops);
355
356 /*
357 * Read-ahead the block, don't wait for it, don't return a buffer.
358 * Long-form addressing.
359 */
360 void /* error */
361 xfs_btree_reada_bufl(
362 struct xfs_mount *mp, /* file system mount point */
363 xfs_fsblock_t fsbno, /* file system block number */
364 xfs_extlen_t count, /* count of filesystem blocks */
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 * Short-form addressing.
370 */
371 void /* error */
372 xfs_btree_reada_bufs(
373 struct xfs_mount *mp, /* file system mount point */
374 xfs_agnumber_t agno, /* allocation group number */
375 xfs_agblock_t agbno, /* allocation group block number */
376 xfs_extlen_t count, /* count of filesystem blocks */
377 const struct xfs_buf_ops *ops);
378
379 /*
380 * Initialise a new btree block header
381 */
382 void
383 xfs_btree_init_block(
384 struct xfs_mount *mp,
385 struct xfs_buf *bp,
386 xfs_btnum_t btnum,
387 __u16 level,
388 __u16 numrecs,
389 __u64 owner,
390 unsigned int flags);
391
392 void
393 xfs_btree_init_block_int(
394 struct xfs_mount *mp,
395 struct xfs_btree_block *buf,
396 xfs_daddr_t blkno,
397 xfs_btnum_t btnum,
398 __u16 level,
399 __u16 numrecs,
400 __u64 owner,
401 unsigned int flags);
402
403 /*
404 * Common btree core entry points.
405 */
406 int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
407 int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
408 int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
409 int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
410 int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
411 int xfs_btree_insert(struct xfs_btree_cur *, int *);
412 int xfs_btree_delete(struct xfs_btree_cur *, int *);
413 int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
414 int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
415 struct list_head *buffer_list);
416
417 /*
418 * btree block CRC helpers
419 */
420 void xfs_btree_lblock_calc_crc(struct xfs_buf *);
421 bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
422 void xfs_btree_sblock_calc_crc(struct xfs_buf *);
423 bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
424
425 /*
426 * Internal btree helpers also used by xfs_bmap.c.
427 */
428 void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
429 void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
430
431 /*
432 * Helpers.
433 */
434 static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
435 {
436 return be16_to_cpu(block->bb_numrecs);
437 }
438
439 static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
440 uint16_t numrecs)
441 {
442 block->bb_numrecs = cpu_to_be16(numrecs);
443 }
444
445 static inline int xfs_btree_get_level(struct xfs_btree_block *block)
446 {
447 return be16_to_cpu(block->bb_level);
448 }
449
450
451 /*
452 * Min and max functions for extlen, agblock, fileoff, and filblks types.
453 */
454 #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
455 #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
456 #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
457 #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
458 #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
459 #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
460 #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
461 #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
462
463 xfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
464 xfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
465 unsigned int max_recs);
466 xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
467 uint64_t owner);
468 xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
469 unsigned int max_recs);
470
471 uint xfs_btree_compute_maxlevels(uint *limits, unsigned long len);
472 unsigned long long xfs_btree_calc_size(uint *limits, unsigned long long len);
473
474 /* return codes */
475 #define XFS_BTREE_QUERY_RANGE_CONTINUE 0 /* keep iterating */
476 #define XFS_BTREE_QUERY_RANGE_ABORT 1 /* stop iterating */
477 typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
478 union xfs_btree_rec *rec, void *priv);
479
480 int xfs_btree_query_range(struct xfs_btree_cur *cur,
481 union xfs_btree_irec *low_rec, union xfs_btree_irec *high_rec,
482 xfs_btree_query_range_fn fn, void *priv);
483 int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
484 void *priv);
485
486 typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
487 void *data);
488 int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
489 xfs_btree_visit_blocks_fn fn, void *data);
490
491 int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
492
493 union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
494 struct xfs_btree_block *block);
495 union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
496 struct xfs_btree_block *block);
497 union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
498 struct xfs_btree_block *block);
499 union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
500 struct xfs_btree_block *block);
501 int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
502 union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
503 struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
504 int level, struct xfs_buf **bpp);
505 bool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur, union xfs_btree_ptr *ptr);
506 int64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur *cur,
507 const union xfs_btree_ptr *a,
508 const union xfs_btree_ptr *b);
509 void xfs_btree_get_sibling(struct xfs_btree_cur *cur,
510 struct xfs_btree_block *block,
511 union xfs_btree_ptr *ptr, int lr);
512 void xfs_btree_get_keys(struct xfs_btree_cur *cur,
513 struct xfs_btree_block *block, union xfs_btree_key *key);
514 union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
515 union xfs_btree_key *key);
516 int xfs_btree_has_record(struct xfs_btree_cur *cur, union xfs_btree_irec *low,
517 union xfs_btree_irec *high, bool *exists);
518 bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
519
520 #endif /* __XFS_BTREE_H__ */