[XFS] implement generic xfs_btree_increment
[deliverable/linux.git] / fs / xfs / xfs_bmap_btree.h
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
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.
1da177e4 13 *
7b718769
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
1da177e4
LT
17 */
18#ifndef __XFS_BMAP_BTREE_H__
19#define __XFS_BMAP_BTREE_H__
20
21#define XFS_BMAP_MAGIC 0x424d4150 /* 'BMAP' */
22
23struct xfs_btree_cur;
24struct xfs_btree_lblock;
25struct xfs_mount;
26struct xfs_inode;
561f7d17 27struct xfs_trans;
1da177e4
LT
28
29/*
30 * Bmap root header, on-disk form only.
31 */
16259e7d
CH
32typedef struct xfs_bmdr_block {
33 __be16 bb_level; /* 0 is a leaf */
34 __be16 bb_numrecs; /* current # of data records */
1da177e4
LT
35} xfs_bmdr_block_t;
36
37/*
38 * Bmap btree record and extent descriptor.
1da177e4
LT
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.
43 */
1da177e4 44#define BMBT_EXNTFLAG_BITLEN 1
1da177e4 45#define BMBT_STARTOFF_BITLEN 54
1da177e4 46#define BMBT_STARTBLOCK_BITLEN 52
1da177e4
LT
47#define BMBT_BLOCKCOUNT_BITLEN 21
48
1da177e4
LT
49
50#define BMBT_USE_64 1
51
52typedef struct xfs_bmbt_rec_32
53{
54 __uint32_t l0, l1, l2, l3;
55} xfs_bmbt_rec_32_t;
56typedef struct xfs_bmbt_rec_64
57{
cd8b0a97 58 __be64 l0, l1;
1da177e4
LT
59} xfs_bmbt_rec_64_t;
60
61typedef __uint64_t xfs_bmbt_rec_base_t; /* use this for casts */
62typedef xfs_bmbt_rec_64_t xfs_bmbt_rec_t, xfs_bmdr_rec_t;
63
a6f64d4a
CH
64typedef struct xfs_bmbt_rec_host {
65 __uint64_t l0, l1;
66} xfs_bmbt_rec_host_t;
67
1da177e4
LT
68/*
69 * Values and macros for delayed-allocation startblock fields.
70 */
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)
a844f451 78
1da177e4 79#define ISNULLSTARTBLOCK(x) isnullstartblock(x)
a844f451
NS
80static inline int isnullstartblock(xfs_fsblock_t x)
81{
82 return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
83}
84
1da177e4 85#define ISNULLDSTARTBLOCK(x) isnulldstartblock(x)
a844f451
NS
86static inline int isnulldstartblock(xfs_dfsbno_t x)
87{
88 return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
89}
90
1da177e4 91#define NULLSTARTBLOCK(k) nullstartblock(k)
a844f451
NS
92static inline xfs_fsblock_t nullstartblock(int k)
93{
94 ASSERT(k < (1 << STARTBLOCKVALBITS));
95 return STARTBLOCKMASK | (k);
96}
97
1da177e4 98#define STARTBLOCKVAL(x) startblockval(x)
a844f451
NS
99static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
100{
101 return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
102}
1da177e4
LT
103
104/*
105 * Possible extent formats.
106 */
107typedef enum {
108 XFS_EXTFMT_NOSTATE = 0,
109 XFS_EXTFMT_HASSTATE
110} xfs_exntfmt_t;
111
112/*
113 * Possible extent states.
114 */
115typedef enum {
116 XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
117 XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
118} xfs_exntst_t;
119
120/*
121 * Extent state and extent format macros.
122 */
a844f451 123#define XFS_EXTFMT_INODE(x) \
62118709 124 (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
a844f451 125 XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE)
1da177e4
LT
126#define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN)
127
128/*
129 * Incore version of above.
130 */
131typedef struct xfs_bmbt_irec
132{
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 */
137} xfs_bmbt_irec_t;
138
139/*
140 * Key structure for non-leaf levels of the tree.
141 */
8801bb99
CH
142typedef struct xfs_bmbt_key {
143 __be64 br_startoff; /* starting file offset */
1da177e4
LT
144} xfs_bmbt_key_t, xfs_bmdr_key_t;
145
397b5208
CH
146/* btree pointer type */
147typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
148
149/* btree block header type */
1da177e4
LT
150typedef struct xfs_btree_lblock xfs_bmbt_block_t;
151
a844f451 152#define XFS_BUF_TO_BMBT_BLOCK(bp) ((xfs_bmbt_block_t *)XFS_BUF_PTR(bp))
1da177e4 153
a844f451
NS
154#define XFS_BMAP_RBLOCK_DSIZE(lev,cur) ((cur)->bc_private.b.forksize)
155#define XFS_BMAP_RBLOCK_ISIZE(lev,cur) \
1da177e4 156 ((int)XFS_IFORK_PTR((cur)->bc_private.b.ip, \
a844f451 157 (cur)->bc_private.b.whichfork)->if_broot_bytes)
1da177e4 158
1da177e4 159#define XFS_BMAP_BLOCK_DMAXRECS(lev,cur) \
a844f451 160 (((lev) == (cur)->bc_nlevels - 1 ? \
1da177e4
LT
161 XFS_BTREE_BLOCK_MAXRECS(XFS_BMAP_RBLOCK_DSIZE(lev,cur), \
162 xfs_bmdr, (lev) == 0) : \
a844f451 163 ((cur)->bc_mp->m_bmap_dmxr[(lev) != 0])))
1da177e4 164#define XFS_BMAP_BLOCK_IMAXRECS(lev,cur) \
a844f451
NS
165 (((lev) == (cur)->bc_nlevels - 1 ? \
166 XFS_BTREE_BLOCK_MAXRECS(XFS_BMAP_RBLOCK_ISIZE(lev,cur),\
167 xfs_bmbt, (lev) == 0) : \
168 ((cur)->bc_mp->m_bmap_dmxr[(lev) != 0])))
1da177e4 169
1da177e4 170#define XFS_BMAP_BLOCK_DMINRECS(lev,cur) \
a844f451
NS
171 (((lev) == (cur)->bc_nlevels - 1 ? \
172 XFS_BTREE_BLOCK_MINRECS(XFS_BMAP_RBLOCK_DSIZE(lev,cur),\
173 xfs_bmdr, (lev) == 0) : \
174 ((cur)->bc_mp->m_bmap_dmnr[(lev) != 0])))
1da177e4 175#define XFS_BMAP_BLOCK_IMINRECS(lev,cur) \
a844f451
NS
176 (((lev) == (cur)->bc_nlevels - 1 ? \
177 XFS_BTREE_BLOCK_MINRECS(XFS_BMAP_RBLOCK_ISIZE(lev,cur),\
178 xfs_bmbt, (lev) == 0) : \
179 ((cur)->bc_mp->m_bmap_dmnr[(lev) != 0])))
180
2c36dded
ES
181#define XFS_BMAP_REC_DADDR(bb,i,cur) (XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))
182
183#define XFS_BMAP_REC_IADDR(bb,i,cur) (XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))
a844f451
NS
184
185#define XFS_BMAP_KEY_DADDR(bb,i,cur) \
2c36dded
ES
186 (XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))
187
a844f451 188#define XFS_BMAP_KEY_IADDR(bb,i,cur) \
2c36dded 189 (XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))
a844f451
NS
190
191#define XFS_BMAP_PTR_DADDR(bb,i,cur) \
2c36dded 192 (XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, XFS_BMAP_BLOCK_DMAXRECS( \
16259e7d 193 be16_to_cpu((bb)->bb_level), cur)))
a844f451 194#define XFS_BMAP_PTR_IADDR(bb,i,cur) \
2c36dded 195 (XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, XFS_BMAP_BLOCK_IMAXRECS( \
16259e7d 196 be16_to_cpu((bb)->bb_level), cur)))
1da177e4
LT
197
198/*
199 * These are to be used when we know the size of the block and
200 * we don't have a cursor.
201 */
1da177e4 202#define XFS_BMAP_BROOT_REC_ADDR(bb,i,sz) \
2c36dded 203 (XFS_BTREE_REC_ADDR(xfs_bmbt,bb,i))
1da177e4 204#define XFS_BMAP_BROOT_KEY_ADDR(bb,i,sz) \
2c36dded 205 (XFS_BTREE_KEY_ADDR(xfs_bmbt,bb,i))
1da177e4 206#define XFS_BMAP_BROOT_PTR_ADDR(bb,i,sz) \
2c36dded 207 (XFS_BTREE_PTR_ADDR(xfs_bmbt,bb,i,XFS_BMAP_BROOT_MAXRECS(sz)))
a844f451 208
16259e7d 209#define XFS_BMAP_BROOT_NUMRECS(bb) be16_to_cpu((bb)->bb_numrecs)
a844f451 210#define XFS_BMAP_BROOT_MAXRECS(sz) XFS_BTREE_BLOCK_MAXRECS(sz,xfs_bmbt,0)
1da177e4 211
1da177e4 212#define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
a844f451
NS
213 (int)(sizeof(xfs_bmbt_block_t) + \
214 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
215
1da177e4 216#define XFS_BMAP_BROOT_SPACE(bb) \
16259e7d 217 (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
a844f451
NS
218#define XFS_BMDR_SPACE_CALC(nrecs) \
219 (int)(sizeof(xfs_bmdr_block_t) + \
220 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
1da177e4
LT
221
222/*
223 * Maximum number of bmap btree levels.
224 */
a844f451 225#define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
1da177e4 226
a844f451 227#define XFS_BMAP_SANITY_CHECK(mp,bb,level) \
16259e7d
CH
228 (be32_to_cpu((bb)->bb_magic) == XFS_BMAP_MAGIC && \
229 be16_to_cpu((bb)->bb_level) == level && \
230 be16_to_cpu((bb)->bb_numrecs) > 0 && \
231 be16_to_cpu((bb)->bb_numrecs) <= (mp)->m_bmap_dmxr[(level) != 0])
1da177e4
LT
232
233
234#ifdef __KERNEL__
235
1da177e4
LT
236/*
237 * Prototypes for xfs_bmap.c to call.
238 */
a844f451
NS
239extern void xfs_bmdr_to_bmbt(xfs_bmdr_block_t *, int, xfs_bmbt_block_t *, int);
240extern int xfs_bmbt_decrement(struct xfs_btree_cur *, int, int *);
241extern int xfs_bmbt_delete(struct xfs_btree_cur *, int *);
a6f64d4a 242extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
a844f451
NS
243extern xfs_bmbt_block_t *xfs_bmbt_get_block(struct xfs_btree_cur *cur,
244 int, struct xfs_buf **bpp);
a6f64d4a
CH
245extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
246extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
247extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
248extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
1da177e4 249
a844f451 250extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
a844f451 251extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
a844f451 252extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
1da177e4 253
a844f451
NS
254extern int xfs_bmbt_insert(struct xfs_btree_cur *, int *);
255extern void xfs_bmbt_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
256extern void xfs_bmbt_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int,
257 int);
258extern int xfs_bmbt_lookup_eq(struct xfs_btree_cur *, xfs_fileoff_t,
259 xfs_fsblock_t, xfs_filblks_t, int *);
260extern int xfs_bmbt_lookup_ge(struct xfs_btree_cur *, xfs_fileoff_t,
261 xfs_fsblock_t, xfs_filblks_t, int *);
1da177e4 262
1da177e4
LT
263/*
264 * Give the bmap btree a new root block. Copy the old broot contents
265 * down into a real block and make the broot point to it.
266 */
a844f451
NS
267extern int xfs_bmbt_newroot(struct xfs_btree_cur *cur, int *lflags, int *stat);
268
a6f64d4a
CH
269extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
270extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
a844f451 271 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
a6f64d4a
CH
272extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
273extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
274extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
275extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
1da177e4 276
a844f451
NS
277extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
278extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
279 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
1da177e4 280
a844f451
NS
281extern void xfs_bmbt_to_bmdr(xfs_bmbt_block_t *, int, xfs_bmdr_block_t *, int);
282extern int xfs_bmbt_update(struct xfs_btree_cur *, xfs_fileoff_t,
283 xfs_fsblock_t, xfs_filblks_t, xfs_exntst_t);
1da177e4 284
561f7d17
CH
285extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
286 struct xfs_trans *, struct xfs_inode *, int);
287
1da177e4
LT
288#endif /* __KERNEL__ */
289
290#endif /* __XFS_BMAP_BTREE_H__ */
This page took 0.448956 seconds and 5 git commands to generate.