2 * Copyright (c) 2000,2002-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_BMAP_BTREE_H__
19 #define __XFS_BMAP_BTREE_H__
21 #define XFS_BMAP_MAGIC 0x424d4150 /* 'BMAP' */
24 struct xfs_btree_lblock;
30 * Bmap root header, on-disk form only.
32 typedef struct xfs_bmdr_block {
33 __be16 bb_level; /* 0 is a leaf */
34 __be16 bb_numrecs; /* current # of data records */
38 * Bmap btree record and extent descriptor.
39 * l0:63 is an extent flag (value 1 indicates non-normal).
40 * l0:9-62 are startoff.
41 * l0:0-8 and l1:21-63 are startblock.
42 * l1:0-20 are blockcount.
44 #define BMBT_EXNTFLAG_BITLEN 1
45 #define BMBT_STARTOFF_BITLEN 54
46 #define BMBT_STARTBLOCK_BITLEN 52
47 #define BMBT_BLOCKCOUNT_BITLEN 21
52 typedef struct xfs_bmbt_rec_32
54 __uint32_t l0, l1, l2, l3;
56 typedef struct xfs_bmbt_rec_64
61 typedef __uint64_t xfs_bmbt_rec_base_t; /* use this for casts */
62 typedef xfs_bmbt_rec_64_t xfs_bmbt_rec_t, xfs_bmdr_rec_t;
64 typedef struct xfs_bmbt_rec_host {
66 } xfs_bmbt_rec_host_t;
69 * Values and macros for delayed-allocation startblock fields.
71 #define STARTBLOCKVALBITS 17
72 #define STARTBLOCKMASKBITS (15 + XFS_BIG_BLKNOS * 20)
73 #define DSTARTBLOCKMASKBITS (15 + 20)
74 #define STARTBLOCKMASK \
75 (((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
76 #define DSTARTBLOCKMASK \
77 (((((xfs_dfsbno_t)1) << DSTARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
79 #define ISNULLSTARTBLOCK(x) isnullstartblock(x)
80 static inline int isnullstartblock(xfs_fsblock_t x)
82 return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
85 #define ISNULLDSTARTBLOCK(x) isnulldstartblock(x)
86 static inline int isnulldstartblock(xfs_dfsbno_t x)
88 return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
91 #define NULLSTARTBLOCK(k) nullstartblock(k)
92 static inline xfs_fsblock_t nullstartblock(int k)
94 ASSERT(k < (1 << STARTBLOCKVALBITS));
95 return STARTBLOCKMASK | (k);
98 #define STARTBLOCKVAL(x) startblockval(x)
99 static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
101 return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
105 * Possible extent formats.
108 XFS_EXTFMT_NOSTATE = 0,
113 * Possible extent states.
116 XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
117 XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
121 * Extent state and extent format macros.
123 #define XFS_EXTFMT_INODE(x) \
124 (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
125 XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE)
126 #define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN)
129 * Incore version of above.
131 typedef struct xfs_bmbt_irec
133 xfs_fileoff_t br_startoff; /* starting file offset */
134 xfs_fsblock_t br_startblock; /* starting block number */
135 xfs_filblks_t br_blockcount; /* number of blocks */
136 xfs_exntst_t br_state; /* extent state */
140 * Key structure for non-leaf levels of the tree.
142 typedef struct xfs_bmbt_key {
143 __be64 br_startoff; /* starting file offset */
144 } xfs_bmbt_key_t, xfs_bmdr_key_t;
146 /* btree pointer type */
147 typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
149 /* btree block header type */
150 typedef struct xfs_btree_lblock xfs_bmbt_block_t;
152 #define XFS_BUF_TO_BMBT_BLOCK(bp) ((xfs_bmbt_block_t *)XFS_BUF_PTR(bp))
154 #define XFS_BMAP_REC_DADDR(bb,i,cur) (XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))
156 #define XFS_BMAP_REC_IADDR(bb,i,cur) (XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))
158 #define XFS_BMAP_KEY_DADDR(bb,i,cur) \
159 (XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))
161 #define XFS_BMAP_KEY_IADDR(bb,i,cur) \
162 (XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))
164 #define XFS_BMAP_PTR_DADDR(bb,i,cur) \
165 (XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, XFS_BMAP_BLOCK_DMAXRECS( \
166 be16_to_cpu((bb)->bb_level), cur)))
167 #define XFS_BMAP_PTR_IADDR(bb,i,cur) \
168 (XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, xfs_bmbt_get_maxrecs(cur, \
169 be16_to_cpu((bb)->bb_level))))
172 * These are to be used when we know the size of the block and
173 * we don't have a cursor.
175 #define XFS_BMAP_BROOT_REC_ADDR(bb,i,sz) \
176 (XFS_BTREE_REC_ADDR(xfs_bmbt,bb,i))
177 #define XFS_BMAP_BROOT_KEY_ADDR(bb,i,sz) \
178 (XFS_BTREE_KEY_ADDR(xfs_bmbt,bb,i))
179 #define XFS_BMAP_BROOT_PTR_ADDR(mp, bb,i,sz) \
180 (XFS_BTREE_PTR_ADDR(xfs_bmbt,bb,i,xfs_bmbt_maxrecs(mp, sz, 0)))
182 #define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
183 (int)(sizeof(xfs_bmbt_block_t) + \
184 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
186 #define XFS_BMAP_BROOT_SPACE(bb) \
187 (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
188 #define XFS_BMDR_SPACE_CALC(nrecs) \
189 (int)(sizeof(xfs_bmdr_block_t) + \
190 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
193 * Maximum number of bmap btree levels.
195 #define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
197 #define XFS_BMAP_SANITY_CHECK(mp,bb,level) \
198 (be32_to_cpu((bb)->bb_magic) == XFS_BMAP_MAGIC && \
199 be16_to_cpu((bb)->bb_level) == level && \
200 be16_to_cpu((bb)->bb_numrecs) > 0 && \
201 be16_to_cpu((bb)->bb_numrecs) <= (mp)->m_bmap_dmxr[(level) != 0])
205 * Prototypes for xfs_bmap.c to call.
207 extern void xfs_bmdr_to_bmbt(struct xfs_mount *, xfs_bmdr_block_t *, int,
208 xfs_bmbt_block_t *, int);
209 extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
210 extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
211 extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
212 extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
213 extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
215 extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
216 extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
217 extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
219 extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
220 extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
221 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
222 extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
223 extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
224 extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
225 extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
227 extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
228 extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
229 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
231 extern void xfs_bmbt_to_bmdr(struct xfs_mount *, xfs_bmbt_block_t *, int,
232 xfs_bmdr_block_t *, int);
234 extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
235 extern int xfs_bmdr_maxrecs(struct xfs_mount *, int blocklen, int leaf);
236 extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
238 extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
239 struct xfs_trans *, struct xfs_inode *, int);
242 #endif /* __XFS_BMAP_BTREE_H__ */