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