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 * This nonsense is to make -wlint happy.
32 #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
33 #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
34 #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
36 #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
37 #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
38 #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
39 #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
42 * Short form header: space allocation btrees.
44 typedef struct xfs_btree_sblock
{
45 __be32 bb_magic
; /* magic number for block type */
46 __be16 bb_level
; /* 0 is a leaf */
47 __be16 bb_numrecs
; /* current # of data records */
48 __be32 bb_leftsib
; /* left sibling block or NULLAGBLOCK */
49 __be32 bb_rightsib
; /* right sibling block or NULLAGBLOCK */
53 * Long form header: bmap btrees.
55 typedef struct xfs_btree_lblock
{
56 __be32 bb_magic
; /* magic number for block type */
57 __be16 bb_level
; /* 0 is a leaf */
58 __be16 bb_numrecs
; /* current # of data records */
59 __be64 bb_leftsib
; /* left sibling block or NULLDFSBNO */
60 __be64 bb_rightsib
; /* right sibling block or NULLDFSBNO */
64 * Combined header and structure, used by common code.
66 typedef struct xfs_btree_hdr
68 __be32 bb_magic
; /* magic number for block type */
69 __be16 bb_level
; /* 0 is a leaf */
70 __be16 bb_numrecs
; /* current # of data records */
73 typedef struct xfs_btree_block
{
74 xfs_btree_hdr_t bb_h
; /* header */
79 } s
; /* short form pointers */
83 } l
; /* long form pointers */
88 * For logging record fields.
90 #define XFS_BB_MAGIC 0x01
91 #define XFS_BB_LEVEL 0x02
92 #define XFS_BB_NUMRECS 0x04
93 #define XFS_BB_LEFTSIB 0x08
94 #define XFS_BB_RIGHTSIB 0x10
95 #define XFS_BB_NUM_BITS 5
96 #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
99 * Boolean to select which form of xfs_btree_block_t.bb_u to use.
101 #define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
104 * Magic numbers for btree blocks.
106 extern const __uint32_t xfs_magics
[];
109 * Maximum and minimum records in a btree block.
110 * Given block size, type prefix, and leaf flag (0 or 1).
111 * The divisor below is equivalent to lf ? (e1) : (e2) but that produces
114 #define XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) \
115 ((int)(((bsz) - (uint)sizeof(t ## _block_t)) / \
116 (((lf) * (uint)sizeof(t ## _rec_t)) + \
118 ((uint)sizeof(t ## _key_t) + (uint)sizeof(t ## _ptr_t))))))
119 #define XFS_BTREE_BLOCK_MINRECS(bsz,t,lf) \
120 (XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) / 2)
123 * Record, key, and pointer address calculation macros.
124 * Given block size, type prefix, block pointer, and index of requested entry
125 * (first entry numbered 1).
127 #define XFS_BTREE_REC_ADDR(t,bb,i) \
128 ((t ## _rec_t *)((char *)(bb) + sizeof(t ## _block_t) + \
129 ((i) - 1) * sizeof(t ## _rec_t)))
130 #define XFS_BTREE_KEY_ADDR(t,bb,i) \
131 ((t ## _key_t *)((char *)(bb) + sizeof(t ## _block_t) + \
132 ((i) - 1) * sizeof(t ## _key_t)))
133 #define XFS_BTREE_PTR_ADDR(t,bb,i,mxr) \
134 ((t ## _ptr_t *)((char *)(bb) + sizeof(t ## _block_t) + \
135 (mxr) * sizeof(t ## _key_t) + ((i) - 1) * sizeof(t ## _ptr_t)))
137 #define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
140 * Btree cursor structure.
141 * This collects all information needed by the btree code in one place.
143 typedef struct xfs_btree_cur
145 struct xfs_trans
*bc_tp
; /* transaction we're in, if any */
146 struct xfs_mount
*bc_mp
; /* file system mount struct */
148 xfs_alloc_rec_incore_t a
;
150 xfs_inobt_rec_incore_t i
;
151 } bc_rec
; /* current insert/search record value */
152 struct xfs_buf
*bc_bufs
[XFS_BTREE_MAXLEVELS
]; /* buf ptr per level */
153 int bc_ptrs
[XFS_BTREE_MAXLEVELS
]; /* key/record # */
154 __uint8_t bc_ra
[XFS_BTREE_MAXLEVELS
]; /* readahead bits */
155 #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
156 #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
157 __uint8_t bc_nlevels
; /* number of levels in the tree */
158 __uint8_t bc_blocklog
; /* log2(blocksize) of btree blocks */
159 xfs_btnum_t bc_btnum
; /* identifies which btree type */
161 struct { /* needed for BNO, CNT */
162 struct xfs_buf
*agbp
; /* agf buffer pointer */
163 xfs_agnumber_t agno
; /* ag number */
165 struct { /* needed for BMAP */
166 struct xfs_inode
*ip
; /* pointer to our inode */
167 struct xfs_bmap_free
*flist
; /* list to free after */
168 xfs_fsblock_t firstblock
; /* 1st blk allocated */
169 int allocated
; /* count of alloced */
170 short forksize
; /* fork's inode space */
171 char whichfork
; /* data or attr fork */
172 char flags
; /* flags */
173 #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
175 struct { /* needed for INO */
176 struct xfs_buf
*agbp
; /* agi buffer pointer */
177 xfs_agnumber_t agno
; /* ag number */
179 } bc_private
; /* per-btree type data */
182 #define XFS_BTREE_NOERROR 0
183 #define XFS_BTREE_ERROR 1
186 * Convert from buffer to btree block header.
188 #define XFS_BUF_TO_BLOCK(bp) ((xfs_btree_block_t *)XFS_BUF_PTR(bp))
189 #define XFS_BUF_TO_LBLOCK(bp) ((xfs_btree_lblock_t *)XFS_BUF_PTR(bp))
190 #define XFS_BUF_TO_SBLOCK(bp) ((xfs_btree_sblock_t *)XFS_BUF_PTR(bp))
197 * Debug routine: check that block header is ok.
200 xfs_btree_check_block(
201 xfs_btree_cur_t
*cur
, /* btree cursor */
202 xfs_btree_block_t
*block
, /* generic btree block pointer */
203 int level
, /* level of the btree block */
204 struct xfs_buf
*bp
); /* buffer containing block, if any */
207 * Debug routine: check that keys are in the right order.
211 xfs_btnum_t btnum
, /* btree identifier */
212 void *ak1
, /* pointer to left (lower) key */
213 void *ak2
); /* pointer to right (higher) key */
216 * Debug routine: check that records are in the right order.
220 xfs_btnum_t btnum
, /* btree identifier */
221 void *ar1
, /* pointer to left (lower) record */
222 void *ar2
); /* pointer to right (higher) record */
224 #define xfs_btree_check_block(a,b,c,d)
225 #define xfs_btree_check_key(a,b,c)
226 #define xfs_btree_check_rec(a,b,c)
230 * Checking routine: check that long form block header is ok.
232 int /* error (0 or EFSCORRUPTED) */
233 xfs_btree_check_lblock(
234 xfs_btree_cur_t
*cur
, /* btree cursor */
235 xfs_btree_lblock_t
*block
, /* btree long form block pointer */
236 int level
, /* level of the btree block */
237 struct xfs_buf
*bp
); /* buffer containing block, if any */
240 * Checking routine: check that (long) pointer is ok.
242 int /* error (0 or EFSCORRUPTED) */
243 xfs_btree_check_lptr(
244 xfs_btree_cur_t
*cur
, /* btree cursor */
245 xfs_dfsbno_t ptr
, /* btree block disk address */
246 int level
); /* btree block level */
248 #define xfs_btree_check_lptr_disk(cur, ptr, level) \
249 xfs_btree_check_lptr(cur, be64_to_cpu(ptr), level)
252 * Checking routine: check that short form block header is ok.
254 int /* error (0 or EFSCORRUPTED) */
255 xfs_btree_check_sblock(
256 xfs_btree_cur_t
*cur
, /* btree cursor */
257 xfs_btree_sblock_t
*block
, /* btree short form block pointer */
258 int level
, /* level of the btree block */
259 struct xfs_buf
*bp
); /* buffer containing block */
262 * Checking routine: check that (short) pointer is ok.
264 int /* error (0 or EFSCORRUPTED) */
265 xfs_btree_check_sptr(
266 xfs_btree_cur_t
*cur
, /* btree cursor */
267 xfs_agblock_t ptr
, /* btree block disk address */
268 int level
); /* btree block level */
271 * Delete the btree cursor.
274 xfs_btree_del_cursor(
275 xfs_btree_cur_t
*cur
, /* btree cursor */
276 int error
); /* del because of error */
279 * Duplicate the btree cursor.
280 * Allocate a new one, copy the record, re-get the buffers.
283 xfs_btree_dup_cursor(
284 xfs_btree_cur_t
*cur
, /* input cursor */
285 xfs_btree_cur_t
**ncur
);/* output cursor */
288 * Change the cursor to point to the first record in the current block
289 * at the given level. Other levels are unaffected.
291 int /* success=1, failure=0 */
293 xfs_btree_cur_t
*cur
, /* btree cursor */
294 int level
); /* level to change */
297 * Get a buffer for the block, return it with no data read.
298 * Long-form addressing.
300 struct xfs_buf
* /* buffer for fsbno */
302 struct xfs_mount
*mp
, /* file system mount point */
303 struct xfs_trans
*tp
, /* transaction pointer */
304 xfs_fsblock_t fsbno
, /* file system block number */
305 uint lock
); /* lock flags for get_buf */
308 * Get a buffer for the block, return it with no data read.
309 * Short-form addressing.
311 struct xfs_buf
* /* buffer for agno/agbno */
313 struct xfs_mount
*mp
, /* file system mount point */
314 struct xfs_trans
*tp
, /* transaction pointer */
315 xfs_agnumber_t agno
, /* allocation group number */
316 xfs_agblock_t agbno
, /* allocation group block number */
317 uint lock
); /* lock flags for get_buf */
320 * Allocate a new btree cursor.
321 * The cursor is either for allocation (A) or bmap (B).
323 xfs_btree_cur_t
* /* new btree cursor */
324 xfs_btree_init_cursor(
325 struct xfs_mount
*mp
, /* file system mount point */
326 struct xfs_trans
*tp
, /* transaction pointer */
327 struct xfs_buf
*agbp
, /* (A only) buffer for agf structure */
328 xfs_agnumber_t agno
, /* (A only) allocation group number */
329 xfs_btnum_t btnum
, /* btree identifier */
330 struct xfs_inode
*ip
, /* (B only) inode owning the btree */
331 int whichfork
); /* (B only) data/attr fork */
334 * Check for the cursor referring to the last block at the given level.
336 int /* 1=is last block, 0=not last block */
337 xfs_btree_islastblock(
338 xfs_btree_cur_t
*cur
, /* btree cursor */
339 int level
); /* level to check */
342 * Change the cursor to point to the last record in the current block
343 * at the given level. Other levels are unaffected.
345 int /* success=1, failure=0 */
347 xfs_btree_cur_t
*cur
, /* btree cursor */
348 int level
); /* level to change */
351 * Compute first and last byte offsets for the fields given.
352 * Interprets the offsets table, which contains struct field offsets.
356 __int64_t fields
, /* bitmask of fields */
357 const short *offsets
,/* table of field offsets */
358 int nbits
, /* number of bits to inspect */
359 int *first
, /* output: first byte offset */
360 int *last
); /* output: last byte offset */
363 * Get a buffer for the block, return it read in.
364 * Long-form addressing.
368 struct xfs_mount
*mp
, /* file system mount point */
369 struct xfs_trans
*tp
, /* transaction pointer */
370 xfs_fsblock_t fsbno
, /* file system block number */
371 uint lock
, /* lock flags for read_buf */
372 struct xfs_buf
**bpp
, /* buffer for fsbno */
373 int refval
);/* ref count value for buffer */
376 * Get a buffer for the block, return it read in.
377 * Short-form addressing.
381 struct xfs_mount
*mp
, /* file system mount point */
382 struct xfs_trans
*tp
, /* transaction pointer */
383 xfs_agnumber_t agno
, /* allocation group number */
384 xfs_agblock_t agbno
, /* allocation group block number */
385 uint lock
, /* lock flags for read_buf */
386 struct xfs_buf
**bpp
, /* buffer for agno/agbno */
387 int refval
);/* ref count value for buffer */
390 * Read-ahead the block, don't wait for it, don't return a buffer.
391 * Long-form addressing.
394 xfs_btree_reada_bufl(
395 struct xfs_mount
*mp
, /* file system mount point */
396 xfs_fsblock_t fsbno
, /* file system block number */
397 xfs_extlen_t count
); /* count of filesystem blocks */
400 * Read-ahead the block, don't wait for it, don't return a buffer.
401 * Short-form addressing.
404 xfs_btree_reada_bufs(
405 struct xfs_mount
*mp
, /* file system mount point */
406 xfs_agnumber_t agno
, /* allocation group number */
407 xfs_agblock_t agbno
, /* allocation group block number */
408 xfs_extlen_t count
); /* count of filesystem blocks */
411 * Read-ahead btree blocks, at the given level.
412 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
414 int /* readahead block count */
415 xfs_btree_readahead_core(
416 xfs_btree_cur_t
*cur
, /* btree cursor */
417 int lev
, /* level in btree */
418 int lr
); /* left/right bits */
420 static inline int /* readahead block count */
422 xfs_btree_cur_t
*cur
, /* btree cursor */
423 int lev
, /* level in btree */
424 int lr
) /* left/right bits */
426 if ((cur
->bc_ra
[lev
] | lr
) == cur
->bc_ra
[lev
])
429 return xfs_btree_readahead_core(cur
, lev
, lr
);
434 * Set the buffer for level "lev" in the cursor to bp, releasing
435 * any previous buffer.
439 xfs_btree_cur_t
*cur
, /* btree cursor */
440 int lev
, /* level in btree */
441 struct xfs_buf
*bp
); /* new buffer to set */
443 #endif /* __KERNEL__ */
447 * Min and max functions for extlen, agblock, fileoff, and filblks types.
449 #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
450 #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
451 #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
452 #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
453 #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
454 #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
455 #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
456 #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
458 #define XFS_FSB_SANITY_CHECK(mp,fsb) \
459 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
460 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
462 #endif /* __XFS_BTREE_H__ */