blob: 7f001072db47f6cb4a942f032593a9b87a2e7334 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2002-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_BMAP_BTREE_H__
19#define __XFS_BMAP_BTREE_H__
20
21#define XFS_BMAP_MAGIC 0x424d4150 /* 'BMAP' */
22
23struct xfs_btree_cur;
Christoph Hellwig136341b2008-10-30 17:11:40 +110024struct xfs_btree_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct xfs_btree_lblock;
26struct xfs_mount;
27struct xfs_inode;
Christoph Hellwig561f7d12008-10-30 16:53:59 +110028struct xfs_trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * Bmap root header, on-disk form only.
32 */
Christoph Hellwig16259e72005-11-02 15:11:25 +110033typedef struct xfs_bmdr_block {
34 __be16 bb_level; /* 0 is a leaf */
35 __be16 bb_numrecs; /* current # of data records */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036} xfs_bmdr_block_t;
37
38/*
39 * Bmap btree record and extent descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * l0:63 is an extent flag (value 1 indicates non-normal).
41 * l0:9-62 are startoff.
42 * l0:0-8 and l1:21-63 are startblock.
43 * l1:0-20 are blockcount.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define BMBT_EXNTFLAG_BITLEN 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define BMBT_STARTOFF_BITLEN 54
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define BMBT_STARTBLOCK_BITLEN 52
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define BMBT_BLOCKCOUNT_BITLEN 21
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define BMBT_USE_64 1
52
53typedef struct xfs_bmbt_rec_32
54{
55 __uint32_t l0, l1, l2, l3;
56} xfs_bmbt_rec_32_t;
57typedef struct xfs_bmbt_rec_64
58{
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +100059 __be64 l0, l1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060} xfs_bmbt_rec_64_t;
61
62typedef __uint64_t xfs_bmbt_rec_base_t; /* use this for casts */
63typedef xfs_bmbt_rec_64_t xfs_bmbt_rec_t, xfs_bmdr_rec_t;
64
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100065typedef struct xfs_bmbt_rec_host {
66 __uint64_t l0, l1;
67} xfs_bmbt_rec_host_t;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Values and macros for delayed-allocation startblock fields.
71 */
72#define STARTBLOCKVALBITS 17
73#define STARTBLOCKMASKBITS (15 + XFS_BIG_BLKNOS * 20)
74#define DSTARTBLOCKMASKBITS (15 + 20)
75#define STARTBLOCKMASK \
76 (((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
77#define DSTARTBLOCKMASK \
78 (((((xfs_dfsbno_t)1) << DSTARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
Nathan Scotta844f452005-11-02 14:38:42 +110079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define ISNULLSTARTBLOCK(x) isnullstartblock(x)
Nathan Scotta844f452005-11-02 14:38:42 +110081static inline int isnullstartblock(xfs_fsblock_t x)
82{
83 return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define ISNULLDSTARTBLOCK(x) isnulldstartblock(x)
Nathan Scotta844f452005-11-02 14:38:42 +110087static inline int isnulldstartblock(xfs_dfsbno_t x)
88{
89 return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define NULLSTARTBLOCK(k) nullstartblock(k)
Nathan Scotta844f452005-11-02 14:38:42 +110093static inline xfs_fsblock_t nullstartblock(int k)
94{
95 ASSERT(k < (1 << STARTBLOCKVALBITS));
96 return STARTBLOCKMASK | (k);
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define STARTBLOCKVAL(x) startblockval(x)
Nathan Scotta844f452005-11-02 14:38:42 +1100100static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
101{
102 return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * Possible extent formats.
107 */
108typedef enum {
109 XFS_EXTFMT_NOSTATE = 0,
110 XFS_EXTFMT_HASSTATE
111} xfs_exntfmt_t;
112
113/*
114 * Possible extent states.
115 */
116typedef enum {
117 XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
118 XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
119} xfs_exntst_t;
120
121/*
122 * Extent state and extent format macros.
123 */
Nathan Scotta844f452005-11-02 14:38:42 +1100124#define XFS_EXTFMT_INODE(x) \
Eric Sandeen62118702008-03-06 13:44:28 +1100125 (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
Nathan Scotta844f452005-11-02 14:38:42 +1100126 XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN)
128
129/*
130 * Incore version of above.
131 */
132typedef struct xfs_bmbt_irec
133{
134 xfs_fileoff_t br_startoff; /* starting file offset */
135 xfs_fsblock_t br_startblock; /* starting block number */
136 xfs_filblks_t br_blockcount; /* number of blocks */
137 xfs_exntst_t br_state; /* extent state */
138} xfs_bmbt_irec_t;
139
140/*
141 * Key structure for non-leaf levels of the tree.
142 */
Christoph Hellwig8801bb92006-09-28 10:58:17 +1000143typedef struct xfs_bmbt_key {
144 __be64 br_startoff; /* starting file offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145} xfs_bmbt_key_t, xfs_bmdr_key_t;
146
Christoph Hellwig397b5202006-09-28 10:57:52 +1000147/* btree pointer type */
148typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
149
150/* btree block header type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151typedef struct xfs_btree_lblock xfs_bmbt_block_t;
152
Nathan Scotta844f452005-11-02 14:38:42 +1100153#define XFS_BUF_TO_BMBT_BLOCK(bp) ((xfs_bmbt_block_t *)XFS_BUF_PTR(bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Hellwig136341b2008-10-30 17:11:40 +1100155#define XFS_BMBT_REC_ADDR(mp, block, index) \
156 ((xfs_bmbt_rec_t *) \
157 ((char *)(block) + \
158 sizeof(struct xfs_btree_lblock) + \
159 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100160
Christoph Hellwig136341b2008-10-30 17:11:40 +1100161#define XFS_BMBT_KEY_ADDR(mp, block, index) \
162 ((xfs_bmbt_key_t *) \
163 ((char *)(block) + \
164 sizeof(struct xfs_btree_lblock) + \
165 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig136341b2008-10-30 17:11:40 +1100167#define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
168 ((xfs_bmbt_ptr_t *) \
169 ((char *)(block) + \
170 sizeof(struct xfs_btree_lblock) + \
171 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
172 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100173
Christoph Hellwig136341b2008-10-30 17:11:40 +1100174#define XFS_BMDR_REC_ADDR(block, index) \
175 ((xfs_bmdr_rec_t *) \
176 ((char *)(block) + \
177 sizeof(struct xfs_bmdr_block) + \
178 ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Christoph Hellwig136341b2008-10-30 17:11:40 +1100180#define XFS_BMDR_KEY_ADDR(block, index) \
181 ((xfs_bmdr_key_t *) \
182 ((char *)(block) + \
183 sizeof(struct xfs_bmdr_block) + \
184 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
185
186#define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
187 ((xfs_bmdr_ptr_t *) \
188 ((char *)(block) + \
189 sizeof(struct xfs_bmdr_block) + \
190 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
191 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/*
194 * These are to be used when we know the size of the block and
195 * we don't have a cursor.
196 */
Christoph Hellwig136341b2008-10-30 17:11:40 +1100197#define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
198 XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
Nathan Scotta844f452005-11-02 14:38:42 +1100199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
Nathan Scotta844f452005-11-02 14:38:42 +1100201 (int)(sizeof(xfs_bmbt_block_t) + \
202 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#define XFS_BMAP_BROOT_SPACE(bb) \
Christoph Hellwig16259e72005-11-02 15:11:25 +1100205 (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
Nathan Scotta844f452005-11-02 14:38:42 +1100206#define XFS_BMDR_SPACE_CALC(nrecs) \
207 (int)(sizeof(xfs_bmdr_block_t) + \
208 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/*
211 * Maximum number of bmap btree levels.
212 */
Nathan Scotta844f452005-11-02 14:38:42 +1100213#define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nathan Scotta844f452005-11-02 14:38:42 +1100215#define XFS_BMAP_SANITY_CHECK(mp,bb,level) \
Christoph Hellwig16259e72005-11-02 15:11:25 +1100216 (be32_to_cpu((bb)->bb_magic) == XFS_BMAP_MAGIC && \
217 be16_to_cpu((bb)->bb_level) == level && \
218 be16_to_cpu((bb)->bb_numrecs) > 0 && \
219 be16_to_cpu((bb)->bb_numrecs) <= (mp)->m_bmap_dmxr[(level) != 0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * Prototypes for xfs_bmap.c to call.
224 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100225extern void xfs_bmdr_to_bmbt(struct xfs_mount *, xfs_bmdr_block_t *, int,
226 xfs_bmbt_block_t *, int);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000227extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000228extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
229extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
230extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
231extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Nathan Scotta844f452005-11-02 14:38:42 +1100233extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
Nathan Scotta844f452005-11-02 14:38:42 +1100234extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
Nathan Scotta844f452005-11-02 14:38:42 +1100235extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000237extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
238extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
Nathan Scotta844f452005-11-02 14:38:42 +1100239 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000240extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
241extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
242extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
243extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Nathan Scotta844f452005-11-02 14:38:42 +1100245extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
246extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
247 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Christoph Hellwig60197e82008-10-30 17:11:19 +1100249extern void xfs_bmbt_to_bmdr(struct xfs_mount *, xfs_bmbt_block_t *, int,
250 xfs_bmdr_block_t *, int);
251
252extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
253extern int xfs_bmdr_maxrecs(struct xfs_mount *, int blocklen, int leaf);
254extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100256extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
257 struct xfs_trans *, struct xfs_inode *, int);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260#endif /* __XFS_BMAP_BTREE_H__ */