blob: 7440b78f9cec3b6d5c722e804b3fbc5acca756c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_BTREE_H__
19#define __XFS_BTREE_H__
20
21struct xfs_buf;
22struct xfs_bmap_free;
23struct xfs_inode;
24struct xfs_mount;
25struct xfs_trans;
26
David Chinnera8272ce2007-11-23 16:28:09 +110027extern kmem_zone_t *xfs_btree_cur_zone;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * This nonsense is to make -wlint happy.
31 */
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)
35
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)
40
41/*
42 * Short form header: space allocation btrees.
43 */
Christoph Hellwig16259e72005-11-02 15:11:25 +110044typedef 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050} xfs_btree_sblock_t;
51
52/*
53 * Long form header: bmap btrees.
54 */
Christoph Hellwig16259e72005-11-02 15:11:25 +110055typedef 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061} xfs_btree_lblock_t;
62
63/*
64 * Combined header and structure, used by common code.
65 */
66typedef struct xfs_btree_hdr
67{
Christoph Hellwig16259e72005-11-02 15:11:25 +110068 __be32 bb_magic; /* magic number for block type */
69 __be16 bb_level; /* 0 is a leaf */
70 __be16 bb_numrecs; /* current # of data records */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071} xfs_btree_hdr_t;
72
Christoph Hellwig16259e72005-11-02 15:11:25 +110073typedef struct xfs_btree_block {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 xfs_btree_hdr_t bb_h; /* header */
Christoph Hellwig16259e72005-11-02 15:11:25 +110075 union {
76 struct {
77 __be32 bb_leftsib;
78 __be32 bb_rightsib;
79 } s; /* short form pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct {
Christoph Hellwig16259e72005-11-02 15:11:25 +110081 __be64 bb_leftsib;
82 __be64 bb_rightsib;
83 } l; /* long form pointers */
84 } bb_u; /* rest */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085} xfs_btree_block_t;
86
87/*
88 * For logging record fields.
89 */
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)
97
98/*
99 * Boolean to select which form of xfs_btree_block_t.bb_u to use.
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Magic numbers for btree blocks.
105 */
106extern const __uint32_t xfs_magics[];
107
108/*
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
112 * compiler warnings.
113 */
114#define XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) \
115 ((int)(((bsz) - (uint)sizeof(t ## _block_t)) / \
116 (((lf) * (uint)sizeof(t ## _rec_t)) + \
117 ((1 - (lf)) * \
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)
121
122/*
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).
126 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100127#define XFS_BTREE_REC_ADDR(t,bb,i) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ((t ## _rec_t *)((char *)(bb) + sizeof(t ## _block_t) + \
129 ((i) - 1) * sizeof(t ## _rec_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100130#define XFS_BTREE_KEY_ADDR(t,bb,i) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 ((t ## _key_t *)((char *)(bb) + sizeof(t ## _block_t) + \
132 ((i) - 1) * sizeof(t ## _key_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100133#define XFS_BTREE_PTR_ADDR(t,bb,i,mxr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ((t ## _ptr_t *)((char *)(bb) + sizeof(t ## _block_t) + \
135 (mxr) * sizeof(t ## _key_t) + ((i) - 1) * sizeof(t ## _ptr_t)))
136
137#define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
138
139/*
140 * Btree cursor structure.
141 * This collects all information needed by the btree code in one place.
142 */
143typedef struct xfs_btree_cur
144{
145 struct xfs_trans *bc_tp; /* transaction we're in, if any */
146 struct xfs_mount *bc_mp; /* file system mount struct */
147 union {
Christoph Hellwig16259e72005-11-02 15:11:25 +1100148 xfs_alloc_rec_incore_t a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xfs_bmbt_irec_t b;
Christoph Hellwig61a25842006-09-28 10:57:04 +1000150 xfs_inobt_rec_incore_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } 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 */
160 union {
161 struct { /* needed for BNO, CNT */
162 struct xfs_buf *agbp; /* agf buffer pointer */
163 xfs_agnumber_t agno; /* ag number */
164 } a;
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 */
174 } b;
175 struct { /* needed for INO */
176 struct xfs_buf *agbp; /* agi buffer pointer */
177 xfs_agnumber_t agno; /* ag number */
178 } i;
179 } bc_private; /* per-btree type data */
180} xfs_btree_cur_t;
181
182#define XFS_BTREE_NOERROR 0
183#define XFS_BTREE_ERROR 1
184
185/*
186 * Convert from buffer to btree block header.
187 */
Nathan Scotta844f452005-11-02 14:38:42 +1100188#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))
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#ifdef __KERNEL__
194
195#ifdef DEBUG
196/*
197 * Debug routine: check that block header is ok.
198 */
199void
200xfs_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 */
205
206/*
207 * Debug routine: check that keys are in the right order.
208 */
209void
210xfs_btree_check_key(
211 xfs_btnum_t btnum, /* btree identifier */
212 void *ak1, /* pointer to left (lower) key */
213 void *ak2); /* pointer to right (higher) key */
214
215/*
216 * Debug routine: check that records are in the right order.
217 */
218void
219xfs_btree_check_rec(
220 xfs_btnum_t btnum, /* btree identifier */
221 void *ar1, /* pointer to left (lower) record */
222 void *ar2); /* pointer to right (higher) record */
223#else
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)
227#endif /* DEBUG */
228
229/*
230 * Checking routine: check that long form block header is ok.
231 */
232int /* error (0 or EFSCORRUPTED) */
233xfs_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 */
238
239/*
240 * Checking routine: check that (long) pointer is ok.
241 */
242int /* error (0 or EFSCORRUPTED) */
243xfs_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 */
247
Christoph Hellwigb113bcb2006-09-28 10:57:42 +1000248#define xfs_btree_check_lptr_disk(cur, ptr, level) \
Christoph Hellwig576039c2006-09-28 10:58:06 +1000249 xfs_btree_check_lptr(cur, be64_to_cpu(ptr), level)
Christoph Hellwigb113bcb2006-09-28 10:57:42 +1000250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Checking routine: check that short form block header is ok.
253 */
254int /* error (0 or EFSCORRUPTED) */
255xfs_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 */
260
261/*
262 * Checking routine: check that (short) pointer is ok.
263 */
264int /* error (0 or EFSCORRUPTED) */
265xfs_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 */
269
270/*
271 * Delete the btree cursor.
272 */
273void
274xfs_btree_del_cursor(
275 xfs_btree_cur_t *cur, /* btree cursor */
276 int error); /* del because of error */
277
278/*
279 * Duplicate the btree cursor.
280 * Allocate a new one, copy the record, re-get the buffers.
281 */
282int /* error */
283xfs_btree_dup_cursor(
284 xfs_btree_cur_t *cur, /* input cursor */
285 xfs_btree_cur_t **ncur);/* output cursor */
286
287/*
288 * Change the cursor to point to the first record in the current block
289 * at the given level. Other levels are unaffected.
290 */
291int /* success=1, failure=0 */
292xfs_btree_firstrec(
293 xfs_btree_cur_t *cur, /* btree cursor */
294 int level); /* level to change */
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Get a buffer for the block, return it with no data read.
298 * Long-form addressing.
299 */
300struct xfs_buf * /* buffer for fsbno */
301xfs_btree_get_bufl(
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 */
306
307/*
308 * Get a buffer for the block, return it with no data read.
309 * Short-form addressing.
310 */
311struct xfs_buf * /* buffer for agno/agbno */
312xfs_btree_get_bufs(
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 */
318
319/*
320 * Allocate a new btree cursor.
321 * The cursor is either for allocation (A) or bmap (B).
322 */
323xfs_btree_cur_t * /* new btree cursor */
324xfs_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 */
332
333/*
334 * Check for the cursor referring to the last block at the given level.
335 */
336int /* 1=is last block, 0=not last block */
337xfs_btree_islastblock(
338 xfs_btree_cur_t *cur, /* btree cursor */
339 int level); /* level to check */
340
341/*
342 * Change the cursor to point to the last record in the current block
343 * at the given level. Other levels are unaffected.
344 */
345int /* success=1, failure=0 */
346xfs_btree_lastrec(
347 xfs_btree_cur_t *cur, /* btree cursor */
348 int level); /* level to change */
349
350/*
351 * Compute first and last byte offsets for the fields given.
352 * Interprets the offsets table, which contains struct field offsets.
353 */
354void
355xfs_btree_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 */
361
362/*
363 * Get a buffer for the block, return it read in.
364 * Long-form addressing.
365 */
366int /* error */
367xfs_btree_read_bufl(
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 */
374
375/*
376 * Get a buffer for the block, return it read in.
377 * Short-form addressing.
378 */
379int /* error */
380xfs_btree_read_bufs(
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 */
388
389/*
390 * Read-ahead the block, don't wait for it, don't return a buffer.
391 * Long-form addressing.
392 */
393void /* error */
394xfs_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 */
398
399/*
400 * Read-ahead the block, don't wait for it, don't return a buffer.
401 * Short-form addressing.
402 */
403void /* error */
404xfs_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 */
409
410/*
411 * Read-ahead btree blocks, at the given level.
412 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
413 */
414int /* readahead block count */
415xfs_btree_readahead_core(
416 xfs_btree_cur_t *cur, /* btree cursor */
417 int lev, /* level in btree */
418 int lr); /* left/right bits */
419
420static inline int /* readahead block count */
421xfs_btree_readahead(
422 xfs_btree_cur_t *cur, /* btree cursor */
423 int lev, /* level in btree */
424 int lr) /* left/right bits */
425{
426 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
427 return 0;
428
429 return xfs_btree_readahead_core(cur, lev, lr);
430}
431
432
433/*
434 * Set the buffer for level "lev" in the cursor to bp, releasing
435 * any previous buffer.
436 */
437void
438xfs_btree_setbuf(
439 xfs_btree_cur_t *cur, /* btree cursor */
440 int lev, /* level in btree */
441 struct xfs_buf *bp); /* new buffer to set */
442
443#endif /* __KERNEL__ */
444
445
446/*
447 * Min and max functions for extlen, agblock, fileoff, and filblks types.
448 */
David Chinner54aa8e22007-06-28 16:43:39 +1000449#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))
Nathan Scotta844f452005-11-02 14:38:42 +1100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#define XFS_FSB_SANITY_CHECK(mp,fsb) \
459 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
Nathan Scotta844f452005-11-02 14:38:42 +1100460 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462#endif /* __XFS_BTREE_H__ */