xfs_bmap_btree.h 8.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25 26
 */
#ifndef __XFS_BMAP_BTREE_H__
#define __XFS_BMAP_BTREE_H__

#define XFS_BMAP_MAGIC	0x424d4150	/* 'BMAP' */

struct xfs_btree_cur;
struct xfs_btree_lblock;
struct xfs_mount;
struct xfs_inode;
27
struct xfs_trans;
L
Linus Torvalds 已提交
28 29 30 31

/*
 * Bmap root header, on-disk form only.
 */
32 33 34
typedef struct xfs_bmdr_block {
	__be16		bb_level;	/* 0 is a leaf */
	__be16		bb_numrecs;	/* current # of data records */
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
} xfs_bmdr_block_t;

/*
 * Bmap btree record and extent descriptor.
 *  l0:63 is an extent flag (value 1 indicates non-normal).
 *  l0:9-62 are startoff.
 *  l0:0-8 and l1:21-63 are startblock.
 *  l1:0-20 are blockcount.
 */
#define BMBT_EXNTFLAG_BITLEN	1
#define BMBT_STARTOFF_BITLEN	54
#define BMBT_STARTBLOCK_BITLEN	52
#define BMBT_BLOCKCOUNT_BITLEN	21


#define BMBT_USE_64	1

typedef struct xfs_bmbt_rec_32
{
	__uint32_t		l0, l1, l2, l3;
} xfs_bmbt_rec_32_t;
typedef struct xfs_bmbt_rec_64
{
58
	__be64			l0, l1;
L
Linus Torvalds 已提交
59 60 61 62 63
} xfs_bmbt_rec_64_t;

typedef __uint64_t	xfs_bmbt_rec_base_t;	/* use this for casts */
typedef xfs_bmbt_rec_64_t xfs_bmbt_rec_t, xfs_bmdr_rec_t;

64 65 66 67
typedef struct xfs_bmbt_rec_host {
	__uint64_t		l0, l1;
} xfs_bmbt_rec_host_t;

L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77
/*
 * Values and macros for delayed-allocation startblock fields.
 */
#define STARTBLOCKVALBITS	17
#define STARTBLOCKMASKBITS	(15 + XFS_BIG_BLKNOS * 20)
#define DSTARTBLOCKMASKBITS	(15 + 20)
#define STARTBLOCKMASK		\
	(((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
#define DSTARTBLOCKMASK		\
	(((((xfs_dfsbno_t)1) << DSTARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
78

L
Linus Torvalds 已提交
79
#define ISNULLSTARTBLOCK(x)	isnullstartblock(x)
80 81 82 83 84
static inline int isnullstartblock(xfs_fsblock_t x)
{
	return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
}

L
Linus Torvalds 已提交
85
#define ISNULLDSTARTBLOCK(x)	isnulldstartblock(x)
86 87 88 89 90
static inline int isnulldstartblock(xfs_dfsbno_t x)
{
	return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
}

L
Linus Torvalds 已提交
91
#define NULLSTARTBLOCK(k)	nullstartblock(k)
92 93 94 95 96 97
static inline xfs_fsblock_t nullstartblock(int k)
{
	ASSERT(k < (1 << STARTBLOCKVALBITS));
	return STARTBLOCKMASK | (k);
}

L
Linus Torvalds 已提交
98
#define STARTBLOCKVAL(x)	startblockval(x)
99 100 101 102
static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
{
	return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
}
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/*
 * Possible extent formats.
 */
typedef enum {
	XFS_EXTFMT_NOSTATE = 0,
	XFS_EXTFMT_HASSTATE
} xfs_exntfmt_t;

/*
 * Possible extent states.
 */
typedef enum {
	XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
	XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
} xfs_exntst_t;

/*
 * 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)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#define ISUNWRITTEN(x)	((x)->br_state == XFS_EXT_UNWRITTEN)

/*
 * Incore version of above.
 */
typedef struct xfs_bmbt_irec
{
	xfs_fileoff_t	br_startoff;	/* starting file offset */
	xfs_fsblock_t	br_startblock;	/* starting block number */
	xfs_filblks_t	br_blockcount;	/* number of blocks */
	xfs_exntst_t	br_state;	/* extent state */
} xfs_bmbt_irec_t;

/*
 * Key structure for non-leaf levels of the tree.
 */
142 143
typedef struct xfs_bmbt_key {
	__be64		br_startoff;	/* starting file offset */
L
Linus Torvalds 已提交
144 145
} xfs_bmbt_key_t, xfs_bmdr_key_t;

146 147 148 149
/* btree pointer type */
typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;

/* btree block header type */
L
Linus Torvalds 已提交
150 151
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))
L
Linus Torvalds 已提交
153

154 155
#define XFS_BMAP_RBLOCK_DSIZE(lev,cur)	((cur)->bc_private.b.forksize)
#define XFS_BMAP_RBLOCK_ISIZE(lev,cur)	\
L
Linus Torvalds 已提交
156
	((int)XFS_IFORK_PTR((cur)->bc_private.b.ip, \
157
		    (cur)->bc_private.b.whichfork)->if_broot_bytes)
L
Linus Torvalds 已提交
158 159

#define XFS_BMAP_BLOCK_DMAXRECS(lev,cur) \
160
	(((lev) == (cur)->bc_nlevels - 1 ? \
L
Linus Torvalds 已提交
161 162
		XFS_BTREE_BLOCK_MAXRECS(XFS_BMAP_RBLOCK_DSIZE(lev,cur), \
			xfs_bmdr, (lev) == 0) : \
163
		((cur)->bc_mp->m_bmap_dmxr[(lev) != 0])))
L
Linus Torvalds 已提交
164
#define XFS_BMAP_BLOCK_IMAXRECS(lev,cur) \
165 166 167 168
	(((lev) == (cur)->bc_nlevels - 1 ? \
			XFS_BTREE_BLOCK_MAXRECS(XFS_BMAP_RBLOCK_ISIZE(lev,cur),\
				xfs_bmbt, (lev) == 0) : \
			((cur)->bc_mp->m_bmap_dmxr[(lev) != 0])))
L
Linus Torvalds 已提交
169 170

#define XFS_BMAP_BLOCK_DMINRECS(lev,cur) \
171 172 173 174
	(((lev) == (cur)->bc_nlevels - 1 ? \
			XFS_BTREE_BLOCK_MINRECS(XFS_BMAP_RBLOCK_DSIZE(lev,cur),\
				xfs_bmdr, (lev) == 0) : \
			((cur)->bc_mp->m_bmap_dmnr[(lev) != 0])))
L
Linus Torvalds 已提交
175
#define XFS_BMAP_BLOCK_IMINRECS(lev,cur) \
176 177 178 179 180
	(((lev) == (cur)->bc_nlevels - 1 ? \
			XFS_BTREE_BLOCK_MINRECS(XFS_BMAP_RBLOCK_ISIZE(lev,cur),\
				xfs_bmbt, (lev) == 0) : \
			((cur)->bc_mp->m_bmap_dmnr[(lev) != 0])))

181 182 183
#define XFS_BMAP_REC_DADDR(bb,i,cur)	(XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))

#define XFS_BMAP_REC_IADDR(bb,i,cur)	(XFS_BTREE_REC_ADDR(xfs_bmbt, bb, i))
184 185

#define XFS_BMAP_KEY_DADDR(bb,i,cur)	\
186 187
	(XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))

188
#define XFS_BMAP_KEY_IADDR(bb,i,cur)	\
189
	(XFS_BTREE_KEY_ADDR(xfs_bmbt, bb, i))
190 191

#define XFS_BMAP_PTR_DADDR(bb,i,cur)	\
192
	(XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, XFS_BMAP_BLOCK_DMAXRECS(	\
193
				be16_to_cpu((bb)->bb_level), cur)))
194
#define XFS_BMAP_PTR_IADDR(bb,i,cur)	\
195
	(XFS_BTREE_PTR_ADDR(xfs_bmbt, bb, i, XFS_BMAP_BLOCK_IMAXRECS(	\
196
				be16_to_cpu((bb)->bb_level), cur)))
L
Linus Torvalds 已提交
197 198 199 200 201 202

/*
 * These are to be used when we know the size of the block and
 * we don't have a cursor.
 */
#define XFS_BMAP_BROOT_REC_ADDR(bb,i,sz) \
203
	(XFS_BTREE_REC_ADDR(xfs_bmbt,bb,i))
L
Linus Torvalds 已提交
204
#define XFS_BMAP_BROOT_KEY_ADDR(bb,i,sz) \
205
	(XFS_BTREE_KEY_ADDR(xfs_bmbt,bb,i))
L
Linus Torvalds 已提交
206
#define XFS_BMAP_BROOT_PTR_ADDR(bb,i,sz) \
207
	(XFS_BTREE_PTR_ADDR(xfs_bmbt,bb,i,XFS_BMAP_BROOT_MAXRECS(sz)))
208

209
#define XFS_BMAP_BROOT_NUMRECS(bb)	be16_to_cpu((bb)->bb_numrecs)
210
#define XFS_BMAP_BROOT_MAXRECS(sz)	XFS_BTREE_BLOCK_MAXRECS(sz,xfs_bmbt,0)
L
Linus Torvalds 已提交
211 212

#define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
213 214 215
	(int)(sizeof(xfs_bmbt_block_t) + \
	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))

L
Linus Torvalds 已提交
216
#define XFS_BMAP_BROOT_SPACE(bb) \
217
	(XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
218 219 220
#define XFS_BMDR_SPACE_CALC(nrecs) \
	(int)(sizeof(xfs_bmdr_block_t) + \
	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
L
Linus Torvalds 已提交
221 222 223 224

/*
 * Maximum number of bmap btree levels.
 */
225
#define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
L
Linus Torvalds 已提交
226

227
#define XFS_BMAP_SANITY_CHECK(mp,bb,level) \
228 229 230 231
	(be32_to_cpu((bb)->bb_magic) == XFS_BMAP_MAGIC && \
	 be16_to_cpu((bb)->bb_level) == level && \
	 be16_to_cpu((bb)->bb_numrecs) > 0 && \
	 be16_to_cpu((bb)->bb_numrecs) <= (mp)->m_bmap_dmxr[(level) != 0])
L
Linus Torvalds 已提交
232 233 234 235 236


/*
 * Prototypes for xfs_bmap.c to call.
 */
237
extern void xfs_bmdr_to_bmbt(xfs_bmdr_block_t *, int, xfs_bmbt_block_t *, int);
238 239 240 241 242
extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
L
Linus Torvalds 已提交
243

244 245 246
extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
L
Linus Torvalds 已提交
247

248 249
extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
250
			xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
251 252 253 254
extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
L
Linus Torvalds 已提交
255

256 257 258
extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
			xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
L
Linus Torvalds 已提交
259

260
extern void xfs_bmbt_to_bmdr(xfs_bmbt_block_t *, int, xfs_bmdr_block_t *, int);
L
Linus Torvalds 已提交
261

262 263 264
extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
		struct xfs_trans *, struct xfs_inode *, int);

L
Linus Torvalds 已提交
265 266

#endif	/* __XFS_BMAP_BTREE_H__ */