2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
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.
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.
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
18 #ifndef __XFS_BTREE_H__
19 #define __XFS_BTREE_H__
27 extern kmem_zone_t
*xfs_btree_cur_zone
;
30 * Generic key, ptr and record wrapper structures.
32 * These are disk format structures, and are converted where necessary
33 * by the btree specific code that needs to interpret them.
36 __be32 s
; /* short form ptr */
37 __be64 l
; /* long form ptr */
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.
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
;
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
;
65 * This nonsense is to make -wlint happy.
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)
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)
79 uint32_t xfs_btree_magic(int crc
, xfs_btnum_t btnum
);
82 * For logging record fields.
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)
99 * Generic stats interface
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)
106 #define XFS_BTREE_MAXLEVELS 9 /* max of all btrees */
108 struct xfs_btree_ops
{
109 /* size of the key and record structures */
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
);
118 /* update btree root pointer */
119 void (*set_root
)(struct xfs_btree_cur
*cur
,
120 union xfs_btree_ptr
*nptr
, int level_change
);
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
,
127 int (*free_block
)(struct xfs_btree_cur
*cur
, struct xfs_buf
*bp
);
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
);
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
);
139 /* records on disk. Matter for the root in inode case. */
140 int (*get_dmaxrecs
)(struct xfs_btree_cur
*cur
, int level
);
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
);
152 /* difference between key value and cursor value */
153 int64_t (*key_diff
)(struct xfs_btree_cur
*cur
,
154 union xfs_btree_key
*key
);
157 * Difference between key2 and key1 -- positive if key1 > key2,
158 * negative if key1 < key2, and zero if equal.
160 int64_t (*diff_two_keys
)(struct xfs_btree_cur
*cur
,
161 union xfs_btree_key
*key1
,
162 union xfs_btree_key
*key2
);
164 const struct xfs_buf_ops
*buf_ops
;
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
);
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
);
178 * Reasons for the update_lastrec method to be called.
180 #define LASTREC_UPDATE 0
181 #define LASTREC_INSREC 1
182 #define LASTREC_DELREC 2
185 union 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
;
193 /* Per-AG btree private information. */
194 union xfs_btree_cur_private
{
196 unsigned long nr_ops
; /* # record updates */
197 int shape_changes
; /* # of extent splits */
202 * Btree cursor structure.
203 * This collects all information needed by the btree code in one place.
205 typedef struct xfs_btree_cur
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 */
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
;
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 */
239 } bc_private
; /* per-btree type data */
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 */
250 #define XFS_BTREE_NOERROR 0
251 #define XFS_BTREE_ERROR 1
254 * Convert from buffer to btree block header.
256 #define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
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.
262 xfs_failaddr_t
__xfs_btree_check_lblock(struct xfs_btree_cur
*cur
,
263 struct xfs_btree_block
*block
, int level
, struct xfs_buf
*bp
);
264 xfs_failaddr_t
__xfs_btree_check_sblock(struct xfs_btree_cur
*cur
,
265 struct xfs_btree_block
*block
, int level
, struct xfs_buf
*bp
);
268 * Check that block header is ok.
271 xfs_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 */
278 * Check that (long) pointer is ok.
280 bool /* error (0 or EFSCORRUPTED) */
281 xfs_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 */
287 * Check that (short) pointer is ok.
289 bool /* error (0 or EFSCORRUPTED) */
290 xfs_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 */
296 * Delete the btree cursor.
299 xfs_btree_del_cursor(
300 xfs_btree_cur_t
*cur
, /* btree cursor */
301 int error
); /* del because of error */
304 * Duplicate the btree cursor.
305 * Allocate a new one, copy the record, re-get the buffers.
308 xfs_btree_dup_cursor(
309 xfs_btree_cur_t
*cur
, /* input cursor */
310 xfs_btree_cur_t
**ncur
);/* output cursor */
313 * Get a buffer for the block, return it with no data read.
314 * Long-form addressing.
316 struct xfs_buf
* /* buffer for fsbno */
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 */
324 * Get a buffer for the block, return it with no data read.
325 * Short-form addressing.
327 struct xfs_buf
* /* buffer for agno/agbno */
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 */
336 * Check for the cursor referring to the last block at the given level.
338 int /* 1=is last block, 0=not last block */
339 xfs_btree_islastblock(
340 xfs_btree_cur_t
*cur
, /* btree cursor */
341 int level
); /* level to check */
344 * Compute first and last byte offsets for the fields given.
345 * Interprets the offsets table, which contains struct field 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 */
356 * Get a buffer for the block, return it read in.
357 * Long-form addressing.
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
);
370 * Read-ahead the block, don't wait for it, don't return a buffer.
371 * Long-form addressing.
374 xfs_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
);
381 * Read-ahead the block, don't wait for it, don't return a buffer.
382 * Short-form addressing.
385 xfs_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
);
393 * Initialise a new btree block header
396 xfs_btree_init_block(
397 struct xfs_mount
*mp
,
406 xfs_btree_init_block_int(
407 struct xfs_mount
*mp
,
408 struct xfs_btree_block
*buf
,
417 * Common btree core entry points.
419 int xfs_btree_increment(struct xfs_btree_cur
*, int, int *);
420 int xfs_btree_decrement(struct xfs_btree_cur
*, int, int *);
421 int xfs_btree_lookup(struct xfs_btree_cur
*, xfs_lookup_t
, int *);
422 int xfs_btree_update(struct xfs_btree_cur
*, union xfs_btree_rec
*);
423 int xfs_btree_new_iroot(struct xfs_btree_cur
*, int *, int *);
424 int xfs_btree_insert(struct xfs_btree_cur
*, int *);
425 int xfs_btree_delete(struct xfs_btree_cur
*, int *);
426 int xfs_btree_get_rec(struct xfs_btree_cur
*, union xfs_btree_rec
**, int *);
427 int xfs_btree_change_owner(struct xfs_btree_cur
*cur
, uint64_t new_owner
,
428 struct list_head
*buffer_list
);
431 * btree block CRC helpers
433 void xfs_btree_lblock_calc_crc(struct xfs_buf
*);
434 bool xfs_btree_lblock_verify_crc(struct xfs_buf
*);
435 void xfs_btree_sblock_calc_crc(struct xfs_buf
*);
436 bool xfs_btree_sblock_verify_crc(struct xfs_buf
*);
439 * Internal btree helpers also used by xfs_bmap.c.
441 void xfs_btree_log_block(struct xfs_btree_cur
*, struct xfs_buf
*, int);
442 void xfs_btree_log_recs(struct xfs_btree_cur
*, struct xfs_buf
*, int, int);
447 static inline int xfs_btree_get_numrecs(struct xfs_btree_block
*block
)
449 return be16_to_cpu(block
->bb_numrecs
);
452 static inline void xfs_btree_set_numrecs(struct xfs_btree_block
*block
,
455 block
->bb_numrecs
= cpu_to_be16(numrecs
);
458 static inline int xfs_btree_get_level(struct xfs_btree_block
*block
)
460 return be16_to_cpu(block
->bb_level
);
465 * Min and max functions for extlen, agblock, fileoff, and filblks types.
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))
476 #define XFS_FSB_SANITY_CHECK(mp,fsb) \
477 (fsb && XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
478 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
481 * Trace hooks. Currently not implemented as they need to be ported
482 * over to the generic tracing functionality, which is some effort.
484 * i,j = integer (32 bit)
485 * b = btree block buffer (xfs_buf_t)
490 #define XFS_BTREE_TRACE_ARGBI(c, b, i)
491 #define XFS_BTREE_TRACE_ARGBII(c, b, i, j)
492 #define XFS_BTREE_TRACE_ARGI(c, i)
493 #define XFS_BTREE_TRACE_ARGIPK(c, i, p, s)
494 #define XFS_BTREE_TRACE_ARGIPR(c, i, p, r)
495 #define XFS_BTREE_TRACE_ARGIK(c, i, k)
496 #define XFS_BTREE_TRACE_ARGR(c, r)
497 #define XFS_BTREE_TRACE_CURSOR(c, t)
499 bool xfs_btree_sblock_v5hdr_verify(struct xfs_buf
*bp
);
500 bool xfs_btree_sblock_verify(struct xfs_buf
*bp
, unsigned int max_recs
);
501 uint
xfs_btree_compute_maxlevels(struct xfs_mount
*mp
, uint
*limits
,
503 xfs_extlen_t
xfs_btree_calc_size(struct xfs_mount
*mp
, uint
*limits
,
504 unsigned long long len
);
507 #define XFS_BTREE_QUERY_RANGE_CONTINUE 0 /* keep iterating */
508 #define XFS_BTREE_QUERY_RANGE_ABORT 1 /* stop iterating */
509 typedef int (*xfs_btree_query_range_fn
)(struct xfs_btree_cur
*cur
,
510 union xfs_btree_rec
*rec
, void *priv
);
512 int xfs_btree_query_range(struct xfs_btree_cur
*cur
,
513 union xfs_btree_irec
*low_rec
, union xfs_btree_irec
*high_rec
,
514 xfs_btree_query_range_fn fn
, void *priv
);
515 int xfs_btree_query_all(struct xfs_btree_cur
*cur
, xfs_btree_query_range_fn fn
,
518 typedef int (*xfs_btree_visit_blocks_fn
)(struct xfs_btree_cur
*cur
, int level
,
520 int xfs_btree_visit_blocks(struct xfs_btree_cur
*cur
,
521 xfs_btree_visit_blocks_fn fn
, void *data
);
523 int xfs_btree_count_blocks(struct xfs_btree_cur
*cur
, xfs_extlen_t
*blocks
);
525 union xfs_btree_rec
*xfs_btree_rec_addr(struct xfs_btree_cur
*cur
, int n
,
526 struct xfs_btree_block
*block
);
527 union xfs_btree_key
*xfs_btree_key_addr(struct xfs_btree_cur
*cur
, int n
,
528 struct xfs_btree_block
*block
);
529 union xfs_btree_key
*xfs_btree_high_key_addr(struct xfs_btree_cur
*cur
, int n
,
530 struct xfs_btree_block
*block
);
531 union xfs_btree_ptr
*xfs_btree_ptr_addr(struct xfs_btree_cur
*cur
, int n
,
532 struct xfs_btree_block
*block
);
533 int xfs_btree_lookup_get_block(struct xfs_btree_cur
*cur
, int level
,
534 union xfs_btree_ptr
*pp
, struct xfs_btree_block
**blkp
);
535 struct xfs_btree_block
*xfs_btree_get_block(struct xfs_btree_cur
*cur
,
536 int level
, struct xfs_buf
**bpp
);
537 bool xfs_btree_ptr_is_null(struct xfs_btree_cur
*cur
, union xfs_btree_ptr
*ptr
);
538 int64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur
*cur
,
539 const union xfs_btree_ptr
*a
,
540 const union xfs_btree_ptr
*b
);
541 void xfs_btree_get_sibling(struct xfs_btree_cur
*cur
,
542 struct xfs_btree_block
*block
,
543 union xfs_btree_ptr
*ptr
, int lr
);
544 void xfs_btree_get_keys(struct xfs_btree_cur
*cur
,
545 struct xfs_btree_block
*block
, union xfs_btree_key
*key
);
546 union xfs_btree_key
*xfs_btree_high_key_from_key(struct xfs_btree_cur
*cur
,
547 union xfs_btree_key
*key
);
549 #endif /* __XFS_BTREE_H__ */