]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - include/xfs_btree.h
xfsprogs debian changes
[thirdparty/xfsprogs-dev.git] / include / xfs_btree.h
CommitLineData
2bd0ea18 1/*
f302e9e4
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
f302e9e4
NS
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
f302e9e4
NS
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.
5000d01d 13 *
f302e9e4
NS
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
2bd0ea18
NS
17 */
18#ifndef __XFS_BTREE_H__
dfc130f3 19#define __XFS_BTREE_H__
2bd0ea18
NS
20
21struct xfs_buf;
22struct xfs_bmap_free;
23struct xfs_inode;
24struct xfs_mount;
25struct xfs_trans;
26
27/*
28 * This nonsense is to make -wlint happy.
29 */
dfc130f3
RC
30#define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
31#define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
32#define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
2bd0ea18 33
dfc130f3
RC
34#define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
35#define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
36#define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
37#define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
2bd0ea18
NS
38
39/*
40 * Short form header: space allocation btrees.
41 */
f302e9e4
NS
42typedef struct xfs_btree_sblock {
43 __be32 bb_magic; /* magic number for block type */
44 __be16 bb_level; /* 0 is a leaf */
45 __be16 bb_numrecs; /* current # of data records */
46 __be32 bb_leftsib; /* left sibling block or NULLAGBLOCK */
47 __be32 bb_rightsib; /* right sibling block or NULLAGBLOCK */
2bd0ea18
NS
48} xfs_btree_sblock_t;
49
50/*
51 * Long form header: bmap btrees.
52 */
f302e9e4
NS
53typedef struct xfs_btree_lblock {
54 __be32 bb_magic; /* magic number for block type */
55 __be16 bb_level; /* 0 is a leaf */
56 __be16 bb_numrecs; /* current # of data records */
57 __be64 bb_leftsib; /* left sibling block or NULLDFSBNO */
58 __be64 bb_rightsib; /* right sibling block or NULLDFSBNO */
2bd0ea18
NS
59} xfs_btree_lblock_t;
60
61/*
62 * Combined header and structure, used by common code.
63 */
64typedef struct xfs_btree_hdr
65{
f302e9e4
NS
66 __be32 bb_magic; /* magic number for block type */
67 __be16 bb_level; /* 0 is a leaf */
68 __be16 bb_numrecs; /* current # of data records */
2bd0ea18
NS
69} xfs_btree_hdr_t;
70
f302e9e4 71typedef struct xfs_btree_block {
dfc130f3 72 xfs_btree_hdr_t bb_h; /* header */
f302e9e4
NS
73 union {
74 struct {
75 __be32 bb_leftsib;
76 __be32 bb_rightsib;
77 } s; /* short form pointers */
2bd0ea18 78 struct {
f302e9e4
NS
79 __be64 bb_leftsib;
80 __be64 bb_rightsib;
81 } l; /* long form pointers */
82 } bb_u; /* rest */
2bd0ea18
NS
83} xfs_btree_block_t;
84
85/*
86 * For logging record fields.
87 */
dfc130f3
RC
88#define XFS_BB_MAGIC 0x01
89#define XFS_BB_LEVEL 0x02
90#define XFS_BB_NUMRECS 0x04
91#define XFS_BB_LEFTSIB 0x08
92#define XFS_BB_RIGHTSIB 0x10
93#define XFS_BB_NUM_BITS 5
94#define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
2bd0ea18
NS
95
96/*
97 * Boolean to select which form of xfs_btree_block_t.bb_u to use.
98 */
dfc130f3 99#define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
2bd0ea18
NS
100
101/*
102 * Magic numbers for btree blocks.
103 */
dfc130f3 104extern const __uint32_t xfs_magics[];
2bd0ea18
NS
105
106/*
107 * Maximum and minimum records in a btree block.
108 * Given block size, type prefix, and leaf flag (0 or 1).
109 * The divisor below is equivalent to lf ? (e1) : (e2) but that produces
110 * compiler warnings.
111 */
dfc130f3 112#define XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) \
2bd0ea18
NS
113 ((int)(((bsz) - (uint)sizeof(t ## _block_t)) / \
114 (((lf) * (uint)sizeof(t ## _rec_t)) + \
115 ((1 - (lf)) * \
116 ((uint)sizeof(t ## _key_t) + (uint)sizeof(t ## _ptr_t))))))
dfc130f3 117#define XFS_BTREE_BLOCK_MINRECS(bsz,t,lf) \
2bd0ea18
NS
118 (XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) / 2)
119
120/*
121 * Record, key, and pointer address calculation macros.
122 * Given block size, type prefix, block pointer, and index of requested entry
123 * (first entry numbered 1).
124 */
dfc130f3 125#define XFS_BTREE_REC_ADDR(bsz,t,bb,i,mxr) \
2bd0ea18
NS
126 ((t ## _rec_t *)((char *)(bb) + sizeof(t ## _block_t) + \
127 ((i) - 1) * sizeof(t ## _rec_t)))
dfc130f3 128#define XFS_BTREE_KEY_ADDR(bsz,t,bb,i,mxr) \
2bd0ea18
NS
129 ((t ## _key_t *)((char *)(bb) + sizeof(t ## _block_t) + \
130 ((i) - 1) * sizeof(t ## _key_t)))
dfc130f3 131#define XFS_BTREE_PTR_ADDR(bsz,t,bb,i,mxr) \
2bd0ea18
NS
132 ((t ## _ptr_t *)((char *)(bb) + sizeof(t ## _block_t) + \
133 (mxr) * sizeof(t ## _key_t) + ((i) - 1) * sizeof(t ## _ptr_t)))
134
dfc130f3 135#define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
2bd0ea18
NS
136
137/*
138 * Btree cursor structure.
139 * This collects all information needed by the btree code in one place.
140 */
141typedef struct xfs_btree_cur
142{
dfc130f3
RC
143 struct xfs_trans *bc_tp; /* transaction we're in, if any */
144 struct xfs_mount *bc_mp; /* file system mount struct */
2bd0ea18 145 union {
f302e9e4 146 xfs_alloc_rec_incore_t a;
2bd0ea18
NS
147 xfs_bmbt_irec_t b;
148 xfs_inobt_rec_t i;
149 } bc_rec; /* current insert/search record value */
150 struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
151 int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
152 __uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
dfc130f3
RC
153#define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
154#define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
2bd0ea18
NS
155 __uint8_t bc_nlevels; /* number of levels in the tree */
156 __uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
157 xfs_btnum_t bc_btnum; /* identifies which btree type */
158 union {
159 struct { /* needed for BNO, CNT */
160 struct xfs_buf *agbp; /* agf buffer pointer */
161 xfs_agnumber_t agno; /* ag number */
162 } a;
163 struct { /* needed for BMAP */
164 struct xfs_inode *ip; /* pointer to our inode */
165 struct xfs_bmap_free *flist; /* list to free after */
166 xfs_fsblock_t firstblock; /* 1st blk allocated */
167 int allocated; /* count of alloced */
168 short forksize; /* fork's inode space */
169 char whichfork; /* data or attr fork */
170 char flags; /* flags */
dfc130f3 171#define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
2bd0ea18
NS
172 } b;
173 struct { /* needed for INO */
174 struct xfs_buf *agbp; /* agi buffer pointer */
175 xfs_agnumber_t agno; /* ag number */
176 } i;
177 } bc_private; /* per-btree type data */
178} xfs_btree_cur_t;
179
dfc130f3
RC
180#define XFS_BTREE_NOERROR 0
181#define XFS_BTREE_ERROR 1
2bd0ea18
NS
182
183/*
184 * Convert from buffer to btree block header.
185 */
f302e9e4
NS
186#define XFS_BUF_TO_BLOCK(bp) ((xfs_btree_block_t *)XFS_BUF_PTR(bp))
187#define XFS_BUF_TO_LBLOCK(bp) ((xfs_btree_lblock_t *)XFS_BUF_PTR(bp))
188#define XFS_BUF_TO_SBLOCK(bp) ((xfs_btree_sblock_t *)XFS_BUF_PTR(bp))
189
2bd0ea18
NS
190
191#ifdef __KERNEL__
192
193#ifdef DEBUG
194/*
195 * Debug routine: check that block header is ok.
196 */
197void
198xfs_btree_check_block(
199 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 200 xfs_btree_block_t *block, /* generic btree block pointer */
2bd0ea18
NS
201 int level, /* level of the btree block */
202 struct xfs_buf *bp); /* buffer containing block, if any */
203
204/*
205 * Debug routine: check that keys are in the right order.
206 */
207void
208xfs_btree_check_key(
209 xfs_btnum_t btnum, /* btree identifier */
210 void *ak1, /* pointer to left (lower) key */
211 void *ak2); /* pointer to right (higher) key */
212
213/*
214 * Debug routine: check that records are in the right order.
215 */
216void
217xfs_btree_check_rec(
218 xfs_btnum_t btnum, /* btree identifier */
219 void *ar1, /* pointer to left (lower) record */
220 void *ar2); /* pointer to right (higher) record */
221#else
dfc130f3
RC
222#define xfs_btree_check_block(a,b,c,d)
223#define xfs_btree_check_key(a,b,c)
224#define xfs_btree_check_rec(a,b,c)
2bd0ea18
NS
225#endif /* DEBUG */
226
227/*
228 * Checking routine: check that long form block header is ok.
229 */
230int /* error (0 or EFSCORRUPTED) */
231xfs_btree_check_lblock(
232 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 233 xfs_btree_lblock_t *block, /* btree long form block pointer */
2bd0ea18
NS
234 int level, /* level of the btree block */
235 struct xfs_buf *bp); /* buffer containing block, if any */
236
237/*
238 * Checking routine: check that (long) pointer is ok.
239 */
240int /* error (0 or EFSCORRUPTED) */
241xfs_btree_check_lptr(
242 xfs_btree_cur_t *cur, /* btree cursor */
243 xfs_dfsbno_t ptr, /* btree block disk address */
dfc130f3 244 int level); /* btree block level */
2bd0ea18
NS
245
246/*
247 * Checking routine: check that short form block header is ok.
248 */
249int /* error (0 or EFSCORRUPTED) */
250xfs_btree_check_sblock(
251 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 252 xfs_btree_sblock_t *block, /* btree short form block pointer */
2bd0ea18
NS
253 int level, /* level of the btree block */
254 struct xfs_buf *bp); /* buffer containing block */
255
256/*
257 * Checking routine: check that (short) pointer is ok.
258 */
259int /* error (0 or EFSCORRUPTED) */
260xfs_btree_check_sptr(
261 xfs_btree_cur_t *cur, /* btree cursor */
262 xfs_agblock_t ptr, /* btree block disk address */
dfc130f3 263 int level); /* btree block level */
2bd0ea18
NS
264
265/*
266 * Delete the btree cursor.
267 */
268void
269xfs_btree_del_cursor(
270 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 271 int error); /* del because of error */
2bd0ea18
NS
272
273/*
274 * Duplicate the btree cursor.
275 * Allocate a new one, copy the record, re-get the buffers.
276 */
277int /* error */
278xfs_btree_dup_cursor(
279 xfs_btree_cur_t *cur, /* input cursor */
280 xfs_btree_cur_t **ncur);/* output cursor */
281
282/*
283 * Change the cursor to point to the first record in the current block
dfc130f3 284 * at the given level. Other levels are unaffected.
2bd0ea18
NS
285 */
286int /* success=1, failure=0 */
287xfs_btree_firstrec(
288 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 289 int level); /* level to change */
2bd0ea18 290
2bd0ea18
NS
291/*
292 * Get a buffer for the block, return it with no data read.
293 * Long-form addressing.
294 */
295struct xfs_buf * /* buffer for fsbno */
296xfs_btree_get_bufl(
297 struct xfs_mount *mp, /* file system mount point */
298 struct xfs_trans *tp, /* transaction pointer */
299 xfs_fsblock_t fsbno, /* file system block number */
300 uint lock); /* lock flags for get_buf */
301
302/*
303 * Get a buffer for the block, return it with no data read.
304 * Short-form addressing.
305 */
306struct xfs_buf * /* buffer for agno/agbno */
307xfs_btree_get_bufs(
308 struct xfs_mount *mp, /* file system mount point */
309 struct xfs_trans *tp, /* transaction pointer */
310 xfs_agnumber_t agno, /* allocation group number */
311 xfs_agblock_t agbno, /* allocation group block number */
312 uint lock); /* lock flags for get_buf */
313
5000d01d 314/*
2bd0ea18
NS
315 * Allocate a new btree cursor.
316 * The cursor is either for allocation (A) or bmap (B).
317 */
318xfs_btree_cur_t * /* new btree cursor */
319xfs_btree_init_cursor(
320 struct xfs_mount *mp, /* file system mount point */
321 struct xfs_trans *tp, /* transaction pointer */
322 struct xfs_buf *agbp, /* (A only) buffer for agf structure */
323 xfs_agnumber_t agno, /* (A only) allocation group number */
324 xfs_btnum_t btnum, /* btree identifier */
325 struct xfs_inode *ip, /* (B only) inode owning the btree */
326 int whichfork); /* (B only) data/attr fork */
327
328/*
329 * Check for the cursor referring to the last block at the given level.
330 */
331int /* 1=is last block, 0=not last block */
332xfs_btree_islastblock(
333 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 334 int level); /* level to check */
2bd0ea18
NS
335
336/*
337 * Change the cursor to point to the last record in the current block
dfc130f3 338 * at the given level. Other levels are unaffected.
2bd0ea18
NS
339 */
340int /* success=1, failure=0 */
341xfs_btree_lastrec(
342 xfs_btree_cur_t *cur, /* btree cursor */
dfc130f3 343 int level); /* level to change */
2bd0ea18
NS
344
345/*
346 * Compute first and last byte offsets for the fields given.
347 * Interprets the offsets table, which contains struct field offsets.
348 */
349void
350xfs_btree_offsets(
dfc130f3 351 __int64_t fields, /* bitmask of fields */
2bd0ea18
NS
352 const short *offsets,/* table of field offsets */
353 int nbits, /* number of bits to inspect */
dfc130f3
RC
354 int *first, /* output: first byte offset */
355 int *last); /* output: last byte offset */
2bd0ea18
NS
356
357/*
358 * Get a buffer for the block, return it read in.
359 * Long-form addressing.
360 */
361int /* error */
362xfs_btree_read_bufl(
363 struct xfs_mount *mp, /* file system mount point */
364 struct xfs_trans *tp, /* transaction pointer */
365 xfs_fsblock_t fsbno, /* file system block number */
366 uint lock, /* lock flags for read_buf */
367 struct xfs_buf **bpp, /* buffer for fsbno */
368 int refval);/* ref count value for buffer */
369
370/*
371 * Get a buffer for the block, return it read in.
372 * Short-form addressing.
373 */
374int /* error */
375xfs_btree_read_bufs(
376 struct xfs_mount *mp, /* file system mount point */
377 struct xfs_trans *tp, /* transaction pointer */
378 xfs_agnumber_t agno, /* allocation group number */
379 xfs_agblock_t agbno, /* allocation group block number */
380 uint lock, /* lock flags for read_buf */
381 struct xfs_buf **bpp, /* buffer for agno/agbno */
382 int refval);/* ref count value for buffer */
383
384/*
385 * Read-ahead the block, don't wait for it, don't return a buffer.
386 * Long-form addressing.
387 */
388void /* error */
389xfs_btree_reada_bufl(
390 struct xfs_mount *mp, /* file system mount point */
391 xfs_fsblock_t fsbno, /* file system block number */
dfc130f3 392 xfs_extlen_t count); /* count of filesystem blocks */
2bd0ea18
NS
393
394/*
395 * Read-ahead the block, don't wait for it, don't return a buffer.
396 * Short-form addressing.
397 */
398void /* error */
399xfs_btree_reada_bufs(
400 struct xfs_mount *mp, /* file system mount point */
401 xfs_agnumber_t agno, /* allocation group number */
402 xfs_agblock_t agbno, /* allocation group block number */
dfc130f3 403 xfs_extlen_t count); /* count of filesystem blocks */
2bd0ea18
NS
404
405/*
406 * Read-ahead btree blocks, at the given level.
407 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
408 */
409int /* readahead block count */
60ca9704 410xfs_btree_readahead_core(
2bd0ea18
NS
411 xfs_btree_cur_t *cur, /* btree cursor */
412 int lev, /* level in btree */
413 int lr); /* left/right bits */
60ca9704
NS
414
415static inline int /* readahead block count */
416xfs_btree_readahead(
417 xfs_btree_cur_t *cur, /* btree cursor */
418 int lev, /* level in btree */
5000d01d 419 int lr) /* left/right bits */
60ca9704 420{
5000d01d
SL
421 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
422 return 0;
60ca9704
NS
423
424 return xfs_btree_readahead_core(cur, lev, lr);
425}
426
427
2bd0ea18
NS
428/*
429 * Set the buffer for level "lev" in the cursor to bp, releasing
430 * any previous buffer.
431 */
432void
433xfs_btree_setbuf(
434 xfs_btree_cur_t *cur, /* btree cursor */
435 int lev, /* level in btree */
436 struct xfs_buf *bp); /* new buffer to set */
437
438#endif /* __KERNEL__ */
439
440
441/*
442 * Min and max functions for extlen, agblock, fileoff, and filblks types.
443 */
dfc130f3 444#define XFS_EXTLEN_MIN(a,b) \
2bd0ea18 445 ((xfs_extlen_t)(a) < (xfs_extlen_t)(b) ? \
f302e9e4 446 (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
dfc130f3 447#define XFS_EXTLEN_MAX(a,b) \
2bd0ea18 448 ((xfs_extlen_t)(a) > (xfs_extlen_t)(b) ? \
f302e9e4 449 (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
dfc130f3 450#define XFS_AGBLOCK_MIN(a,b) \
2bd0ea18 451 ((xfs_agblock_t)(a) < (xfs_agblock_t)(b) ? \
f302e9e4 452 (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
dfc130f3 453#define XFS_AGBLOCK_MAX(a,b) \
2bd0ea18 454 ((xfs_agblock_t)(a) > (xfs_agblock_t)(b) ? \
f302e9e4 455 (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
dfc130f3 456#define XFS_FILEOFF_MIN(a,b) \
2bd0ea18 457 ((xfs_fileoff_t)(a) < (xfs_fileoff_t)(b) ? \
f302e9e4 458 (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
dfc130f3 459#define XFS_FILEOFF_MAX(a,b) \
2bd0ea18 460 ((xfs_fileoff_t)(a) > (xfs_fileoff_t)(b) ? \
f302e9e4 461 (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
dfc130f3 462#define XFS_FILBLKS_MIN(a,b) \
2bd0ea18 463 ((xfs_filblks_t)(a) < (xfs_filblks_t)(b) ? \
f302e9e4 464 (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
dfc130f3 465#define XFS_FILBLKS_MAX(a,b) \
2bd0ea18 466 ((xfs_filblks_t)(a) > (xfs_filblks_t)(b) ? \
f302e9e4
NS
467 (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
468
dfc130f3 469#define XFS_FSB_SANITY_CHECK(mp,fsb) \
2bd0ea18 470 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
f302e9e4 471 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
2bd0ea18
NS
472
473#endif /* __XFS_BTREE_H__ */