2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #ifndef __XFS_BTREE_H__
33 #define __XFS_BTREE_H__
42 * This nonsense is to make -wlint happy.
44 #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
45 #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
46 #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
48 #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
49 #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
50 #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
51 #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
54 * Short form header: space allocation btrees.
56 typedef struct xfs_btree_sblock
58 __uint32_t bb_magic
; /* magic number for block type */
59 __uint16_t bb_level
; /* 0 is a leaf */
60 __uint16_t bb_numrecs
; /* current # of data records */
61 xfs_agblock_t bb_leftsib
; /* left sibling block or NULLAGBLOCK */
62 xfs_agblock_t bb_rightsib
; /* right sibling block or NULLAGBLOCK */
66 * Long form header: bmap btrees.
68 typedef struct xfs_btree_lblock
70 __uint32_t bb_magic
; /* magic number for block type */
71 __uint16_t bb_level
; /* 0 is a leaf */
72 __uint16_t bb_numrecs
; /* current # of data records */
73 xfs_dfsbno_t bb_leftsib
; /* left sibling block or NULLDFSBNO */
74 xfs_dfsbno_t bb_rightsib
; /* right sibling block or NULLDFSBNO */
78 * Combined header and structure, used by common code.
80 typedef struct xfs_btree_hdr
82 __uint32_t bb_magic
; /* magic number for block type */
83 __uint16_t bb_level
; /* 0 is a leaf */
84 __uint16_t bb_numrecs
; /* current # of data records */
87 typedef struct xfs_btree_block
89 xfs_btree_hdr_t bb_h
; /* header */
92 xfs_agblock_t bb_leftsib
;
93 xfs_agblock_t bb_rightsib
;
94 } s
; /* short form pointers */
96 xfs_dfsbno_t bb_leftsib
;
97 xfs_dfsbno_t bb_rightsib
;
98 } l
; /* long form pointers */
103 * For logging record fields.
105 #define XFS_BB_MAGIC 0x01
106 #define XFS_BB_LEVEL 0x02
107 #define XFS_BB_NUMRECS 0x04
108 #define XFS_BB_LEFTSIB 0x08
109 #define XFS_BB_RIGHTSIB 0x10
110 #define XFS_BB_NUM_BITS 5
111 #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
114 * Boolean to select which form of xfs_btree_block_t.bb_u to use.
116 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BTREE_LONG_PTRS)
117 int xfs_btree_long_ptrs(xfs_btnum_t btnum
);
118 #define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
120 #define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
124 * Magic numbers for btree blocks.
126 extern const __uint32_t xfs_magics
[];
129 * Maximum and minimum records in a btree block.
130 * Given block size, type prefix, and leaf flag (0 or 1).
131 * The divisor below is equivalent to lf ? (e1) : (e2) but that produces
134 #define XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) \
135 ((int)(((bsz) - (uint)sizeof(t ## _block_t)) / \
136 (((lf) * (uint)sizeof(t ## _rec_t)) + \
138 ((uint)sizeof(t ## _key_t) + (uint)sizeof(t ## _ptr_t))))))
139 #define XFS_BTREE_BLOCK_MINRECS(bsz,t,lf) \
140 (XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) / 2)
143 * Record, key, and pointer address calculation macros.
144 * Given block size, type prefix, block pointer, and index of requested entry
145 * (first entry numbered 1).
147 #define XFS_BTREE_REC_ADDR(bsz,t,bb,i,mxr) \
148 ((t ## _rec_t *)((char *)(bb) + sizeof(t ## _block_t) + \
149 ((i) - 1) * sizeof(t ## _rec_t)))
150 #define XFS_BTREE_KEY_ADDR(bsz,t,bb,i,mxr) \
151 ((t ## _key_t *)((char *)(bb) + sizeof(t ## _block_t) + \
152 ((i) - 1) * sizeof(t ## _key_t)))
153 #define XFS_BTREE_PTR_ADDR(bsz,t,bb,i,mxr) \
154 ((t ## _ptr_t *)((char *)(bb) + sizeof(t ## _block_t) + \
155 (mxr) * sizeof(t ## _key_t) + ((i) - 1) * sizeof(t ## _ptr_t)))
157 #define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
160 * Btree cursor structure.
161 * This collects all information needed by the btree code in one place.
163 typedef struct xfs_btree_cur
165 struct xfs_trans
*bc_tp
; /* transaction we're in, if any */
166 struct xfs_mount
*bc_mp
; /* file system mount struct */
171 } bc_rec
; /* current insert/search record value */
172 struct xfs_buf
*bc_bufs
[XFS_BTREE_MAXLEVELS
]; /* buf ptr per level */
173 int bc_ptrs
[XFS_BTREE_MAXLEVELS
]; /* key/record # */
174 __uint8_t bc_ra
[XFS_BTREE_MAXLEVELS
]; /* readahead bits */
175 #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
176 #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
177 __uint8_t bc_nlevels
; /* number of levels in the tree */
178 __uint8_t bc_blocklog
; /* log2(blocksize) of btree blocks */
179 xfs_btnum_t bc_btnum
; /* identifies which btree type */
181 struct { /* needed for BNO, CNT */
182 struct xfs_buf
*agbp
; /* agf buffer pointer */
183 xfs_agnumber_t agno
; /* ag number */
185 struct { /* needed for BMAP */
186 struct xfs_inode
*ip
; /* pointer to our inode */
187 struct xfs_bmap_free
*flist
; /* list to free after */
188 xfs_fsblock_t firstblock
; /* 1st blk allocated */
189 int allocated
; /* count of alloced */
190 short forksize
; /* fork's inode space */
191 char whichfork
; /* data or attr fork */
192 char flags
; /* flags */
193 #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
195 struct { /* needed for INO */
196 struct xfs_buf
*agbp
; /* agi buffer pointer */
197 xfs_agnumber_t agno
; /* ag number */
199 } bc_private
; /* per-btree type data */
202 #define XFS_BTREE_NOERROR 0
203 #define XFS_BTREE_ERROR 1
206 * Convert from buffer to btree block header.
208 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BUF_TO_BLOCK)
209 xfs_btree_block_t
*xfs_buf_to_block(struct xfs_buf
*bp
);
210 #define XFS_BUF_TO_BLOCK(bp) xfs_buf_to_block(bp)
212 #define XFS_BUF_TO_BLOCK(bp) ((xfs_btree_block_t *)(XFS_BUF_PTR(bp)))
214 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BUF_TO_LBLOCK)
215 xfs_btree_lblock_t
*xfs_buf_to_lblock(struct xfs_buf
*bp
);
216 #define XFS_BUF_TO_LBLOCK(bp) xfs_buf_to_lblock(bp)
218 #define XFS_BUF_TO_LBLOCK(bp) ((xfs_btree_lblock_t *)(XFS_BUF_PTR(bp)))
220 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BUF_TO_SBLOCK)
221 xfs_btree_sblock_t
*xfs_buf_to_sblock(struct xfs_buf
*bp
);
222 #define XFS_BUF_TO_SBLOCK(bp) xfs_buf_to_sblock(bp)
224 #define XFS_BUF_TO_SBLOCK(bp) ((xfs_btree_sblock_t *)(XFS_BUF_PTR(bp)))
231 * Debug routine: check that block header is ok.
234 xfs_btree_check_block(
235 xfs_btree_cur_t
*cur
, /* btree cursor */
236 xfs_btree_block_t
*block
, /* generic btree block pointer */
237 int level
, /* level of the btree block */
238 struct xfs_buf
*bp
); /* buffer containing block, if any */
241 * Debug routine: check that keys are in the right order.
245 xfs_btnum_t btnum
, /* btree identifier */
246 void *ak1
, /* pointer to left (lower) key */
247 void *ak2
); /* pointer to right (higher) key */
250 * Debug routine: check that records are in the right order.
254 xfs_btnum_t btnum
, /* btree identifier */
255 void *ar1
, /* pointer to left (lower) record */
256 void *ar2
); /* pointer to right (higher) record */
258 #define xfs_btree_check_block(a,b,c,d)
259 #define xfs_btree_check_key(a,b,c)
260 #define xfs_btree_check_rec(a,b,c)
264 * Checking routine: check that long form block header is ok.
266 int /* error (0 or EFSCORRUPTED) */
267 xfs_btree_check_lblock(
268 xfs_btree_cur_t
*cur
, /* btree cursor */
269 xfs_btree_lblock_t
*block
, /* btree long form block pointer */
270 int level
, /* level of the btree block */
271 struct xfs_buf
*bp
); /* buffer containing block, if any */
274 * Checking routine: check that (long) pointer is ok.
276 int /* error (0 or EFSCORRUPTED) */
277 xfs_btree_check_lptr(
278 xfs_btree_cur_t
*cur
, /* btree cursor */
279 xfs_dfsbno_t ptr
, /* btree block disk address */
280 int level
); /* btree block level */
283 * Checking routine: check that short form block header is ok.
285 int /* error (0 or EFSCORRUPTED) */
286 xfs_btree_check_sblock(
287 xfs_btree_cur_t
*cur
, /* btree cursor */
288 xfs_btree_sblock_t
*block
, /* btree short form block pointer */
289 int level
, /* level of the btree block */
290 struct xfs_buf
*bp
); /* buffer containing block */
293 * Checking routine: check that (short) pointer is ok.
295 int /* error (0 or EFSCORRUPTED) */
296 xfs_btree_check_sptr(
297 xfs_btree_cur_t
*cur
, /* btree cursor */
298 xfs_agblock_t ptr
, /* btree block disk address */
299 int level
); /* btree block level */
302 * Delete the btree cursor.
305 xfs_btree_del_cursor(
306 xfs_btree_cur_t
*cur
, /* btree cursor */
307 int error
); /* del because of error */
310 * Duplicate the btree cursor.
311 * Allocate a new one, copy the record, re-get the buffers.
314 xfs_btree_dup_cursor(
315 xfs_btree_cur_t
*cur
, /* input cursor */
316 xfs_btree_cur_t
**ncur
);/* output cursor */
319 * Change the cursor to point to the first record in the current block
320 * at the given level. Other levels are unaffected.
322 int /* success=1, failure=0 */
324 xfs_btree_cur_t
*cur
, /* btree cursor */
325 int level
); /* level to change */
328 * Get a buffer for the block, return it with no data read.
329 * Long-form addressing.
331 struct xfs_buf
* /* buffer for fsbno */
333 struct xfs_mount
*mp
, /* file system mount point */
334 struct xfs_trans
*tp
, /* transaction pointer */
335 xfs_fsblock_t fsbno
, /* file system block number */
336 uint lock
); /* lock flags for get_buf */
339 * Get a buffer for the block, return it with no data read.
340 * Short-form addressing.
342 struct xfs_buf
* /* buffer for agno/agbno */
344 struct xfs_mount
*mp
, /* file system mount point */
345 struct xfs_trans
*tp
, /* transaction pointer */
346 xfs_agnumber_t agno
, /* allocation group number */
347 xfs_agblock_t agbno
, /* allocation group block number */
348 uint lock
); /* lock flags for get_buf */
351 * Allocate a new btree cursor.
352 * The cursor is either for allocation (A) or bmap (B).
354 xfs_btree_cur_t
* /* new btree cursor */
355 xfs_btree_init_cursor(
356 struct xfs_mount
*mp
, /* file system mount point */
357 struct xfs_trans
*tp
, /* transaction pointer */
358 struct xfs_buf
*agbp
, /* (A only) buffer for agf structure */
359 xfs_agnumber_t agno
, /* (A only) allocation group number */
360 xfs_btnum_t btnum
, /* btree identifier */
361 struct xfs_inode
*ip
, /* (B only) inode owning the btree */
362 int whichfork
); /* (B only) data/attr fork */
365 * Check for the cursor referring to the last block at the given level.
367 int /* 1=is last block, 0=not last block */
368 xfs_btree_islastblock(
369 xfs_btree_cur_t
*cur
, /* btree cursor */
370 int level
); /* level to check */
373 * Change the cursor to point to the last record in the current block
374 * at the given level. Other levels are unaffected.
376 int /* success=1, failure=0 */
378 xfs_btree_cur_t
*cur
, /* btree cursor */
379 int level
); /* level to change */
382 * Compute first and last byte offsets for the fields given.
383 * Interprets the offsets table, which contains struct field offsets.
387 __int64_t fields
, /* bitmask of fields */
388 const short *offsets
,/* table of field offsets */
389 int nbits
, /* number of bits to inspect */
390 int *first
, /* output: first byte offset */
391 int *last
); /* output: last byte offset */
394 * Get a buffer for the block, return it read in.
395 * Long-form addressing.
399 struct xfs_mount
*mp
, /* file system mount point */
400 struct xfs_trans
*tp
, /* transaction pointer */
401 xfs_fsblock_t fsbno
, /* file system block number */
402 uint lock
, /* lock flags for read_buf */
403 struct xfs_buf
**bpp
, /* buffer for fsbno */
404 int refval
);/* ref count value for buffer */
407 * Get a buffer for the block, return it read in.
408 * Short-form addressing.
412 struct xfs_mount
*mp
, /* file system mount point */
413 struct xfs_trans
*tp
, /* transaction pointer */
414 xfs_agnumber_t agno
, /* allocation group number */
415 xfs_agblock_t agbno
, /* allocation group block number */
416 uint lock
, /* lock flags for read_buf */
417 struct xfs_buf
**bpp
, /* buffer for agno/agbno */
418 int refval
);/* ref count value for buffer */
421 * Read-ahead the block, don't wait for it, don't return a buffer.
422 * Long-form addressing.
425 xfs_btree_reada_bufl(
426 struct xfs_mount
*mp
, /* file system mount point */
427 xfs_fsblock_t fsbno
, /* file system block number */
428 xfs_extlen_t count
); /* count of filesystem blocks */
431 * Read-ahead the block, don't wait for it, don't return a buffer.
432 * Short-form addressing.
435 xfs_btree_reada_bufs(
436 struct xfs_mount
*mp
, /* file system mount point */
437 xfs_agnumber_t agno
, /* allocation group number */
438 xfs_agblock_t agbno
, /* allocation group block number */
439 xfs_extlen_t count
); /* count of filesystem blocks */
442 * Read-ahead btree blocks, at the given level.
443 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
445 int /* readahead block count */
446 xfs_btree_readahead_core(
447 xfs_btree_cur_t
*cur
, /* btree cursor */
448 int lev
, /* level in btree */
449 int lr
); /* left/right bits */
451 static inline int /* readahead block count */
453 xfs_btree_cur_t
*cur
, /* btree cursor */
454 int lev
, /* level in btree */
455 int lr
) /* left/right bits */
457 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
460 return xfs_btree_readahead_core(cur
, lev
, lr
);
465 * Set the buffer for level "lev" in the cursor to bp, releasing
466 * any previous buffer.
470 xfs_btree_cur_t
*cur
, /* btree cursor */
471 int lev
, /* level in btree */
472 struct xfs_buf
*bp
); /* new buffer to set */
474 #endif /* __KERNEL__ */
478 * Min and max functions for extlen, agblock, fileoff, and filblks types.
480 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_EXTLEN_MIN)
481 xfs_extlen_t
xfs_extlen_min(xfs_extlen_t a
, xfs_extlen_t b
);
482 #define XFS_EXTLEN_MIN(a,b) xfs_extlen_min(a,b)
484 #define XFS_EXTLEN_MIN(a,b) \
485 ((xfs_extlen_t)(a) < (xfs_extlen_t)(b) ? \
486 (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
488 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_EXTLEN_MAX)
489 xfs_extlen_t
xfs_extlen_max(xfs_extlen_t a
, xfs_extlen_t b
);
490 #define XFS_EXTLEN_MAX(a,b) xfs_extlen_max(a,b)
492 #define XFS_EXTLEN_MAX(a,b) \
493 ((xfs_extlen_t)(a) > (xfs_extlen_t)(b) ? \
494 (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
497 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_AGBLOCK_MIN)
498 xfs_agblock_t
xfs_agblock_min(xfs_agblock_t a
, xfs_agblock_t b
);
499 #define XFS_AGBLOCK_MIN(a,b) xfs_agblock_min(a,b)
501 #define XFS_AGBLOCK_MIN(a,b) \
502 ((xfs_agblock_t)(a) < (xfs_agblock_t)(b) ? \
503 (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
505 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_AGBLOCK_MAX)
506 xfs_agblock_t
xfs_agblock_max(xfs_agblock_t a
, xfs_agblock_t b
);
507 #define XFS_AGBLOCK_MAX(a,b) xfs_agblock_max(a,b)
509 #define XFS_AGBLOCK_MAX(a,b) \
510 ((xfs_agblock_t)(a) > (xfs_agblock_t)(b) ? \
511 (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
514 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_FILEOFF_MIN)
515 xfs_fileoff_t
xfs_fileoff_min(xfs_fileoff_t a
, xfs_fileoff_t b
);
516 #define XFS_FILEOFF_MIN(a,b) xfs_fileoff_min(a,b)
518 #define XFS_FILEOFF_MIN(a,b) \
519 ((xfs_fileoff_t)(a) < (xfs_fileoff_t)(b) ? \
520 (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
522 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_FILEOFF_MAX)
523 xfs_fileoff_t
xfs_fileoff_max(xfs_fileoff_t a
, xfs_fileoff_t b
);
524 #define XFS_FILEOFF_MAX(a,b) xfs_fileoff_max(a,b)
526 #define XFS_FILEOFF_MAX(a,b) \
527 ((xfs_fileoff_t)(a) > (xfs_fileoff_t)(b) ? \
528 (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
531 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_FILBLKS_MIN)
532 xfs_filblks_t
xfs_filblks_min(xfs_filblks_t a
, xfs_filblks_t b
);
533 #define XFS_FILBLKS_MIN(a,b) xfs_filblks_min(a,b)
535 #define XFS_FILBLKS_MIN(a,b) \
536 ((xfs_filblks_t)(a) < (xfs_filblks_t)(b) ? \
537 (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
539 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_FILBLKS_MAX)
540 xfs_filblks_t
xfs_filblks_max(xfs_filblks_t a
, xfs_filblks_t b
);
541 #define XFS_FILBLKS_MAX(a,b) xfs_filblks_max(a,b)
543 #define XFS_FILBLKS_MAX(a,b) \
544 ((xfs_filblks_t)(a) > (xfs_filblks_t)(b) ? \
545 (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
547 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_FSB_SANITY_CHECK)
548 int xfs_fsb_sanity_check(struct xfs_mount
*mp
, xfs_fsblock_t fsb
);
549 #define XFS_FSB_SANITY_CHECK(mp,fsb) xfs_fsb_sanity_check(mp,fsb)
551 #define XFS_FSB_SANITY_CHECK(mp,fsb) \
552 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
553 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
557 * Macros to set EFSCORRUPTED & return/branch.
559 #define XFS_WANT_CORRUPTED_GOTO(x,l) \
561 int fs_is_ok = (x); \
563 if (unlikely(!fs_is_ok)) { \
564 XFS_ERROR_REPORT("XFS_WANT_CORRUPTED_GOTO", \
565 XFS_ERRLEVEL_LOW, NULL); \
566 error = XFS_ERROR(EFSCORRUPTED); \
571 #define XFS_WANT_CORRUPTED_RETURN(x) \
573 int fs_is_ok = (x); \
575 if (unlikely(!fs_is_ok)) { \
576 XFS_ERROR_REPORT("XFS_WANT_CORRUPTED_RETURN", \
577 XFS_ERRLEVEL_LOW, NULL); \
578 return XFS_ERROR(EFSCORRUPTED); \
582 #endif /* __XFS_BTREE_H__ */