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