blob: cf07ca7c22e73e70340da1d061e09b33c1234f4f [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_mount;
26struct xfs_inode;
Christoph Hellwig561f7d12008-10-30 16:53:59 +110027struct xfs_trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Bmap root header, on-disk form only.
31 */
Christoph Hellwig16259e72005-11-02 15:11:25 +110032typedef struct xfs_bmdr_block {
33 __be16 bb_level; /* 0 is a leaf */
34 __be16 bb_numrecs; /* current # of data records */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035} xfs_bmdr_block_t;
36
37/*
38 * Bmap btree record and extent descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define BMBT_EXNTFLAG_BITLEN 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define BMBT_STARTOFF_BITLEN 54
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define BMBT_STARTBLOCK_BITLEN 52
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define BMBT_BLOCKCOUNT_BITLEN 21
48
Christoph Hellwiga5f9be52009-12-04 10:19:07 +000049typedef struct xfs_bmbt_rec {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +100050 __be64 l0, l1;
Christoph Hellwiga5f9be52009-12-04 10:19:07 +000051} xfs_bmbt_rec_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53typedef __uint64_t xfs_bmbt_rec_base_t; /* use this for casts */
Christoph Hellwiga5f9be52009-12-04 10:19:07 +000054typedef xfs_bmbt_rec_t xfs_bmdr_rec_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100056typedef struct xfs_bmbt_rec_host {
57 __uint64_t l0, l1;
58} xfs_bmbt_rec_host_t;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Values and macros for delayed-allocation startblock fields.
62 */
63#define STARTBLOCKVALBITS 17
64#define STARTBLOCKMASKBITS (15 + XFS_BIG_BLKNOS * 20)
65#define DSTARTBLOCKMASKBITS (15 + 20)
66#define STARTBLOCKMASK \
67 (((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
68#define DSTARTBLOCKMASK \
69 (((((xfs_dfsbno_t)1) << DSTARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
Nathan Scotta844f452005-11-02 14:38:42 +110070
Nathan Scotta844f452005-11-02 14:38:42 +110071static inline int isnullstartblock(xfs_fsblock_t x)
72{
73 return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
74}
75
Nathan Scotta844f452005-11-02 14:38:42 +110076static inline int isnulldstartblock(xfs_dfsbno_t x)
77{
78 return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
79}
80
Nathan Scotta844f452005-11-02 14:38:42 +110081static inline xfs_fsblock_t nullstartblock(int k)
82{
83 ASSERT(k < (1 << STARTBLOCKVALBITS));
84 return STARTBLOCKMASK | (k);
85}
86
Nathan Scotta844f452005-11-02 14:38:42 +110087static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
88{
89 return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Possible extent formats.
94 */
95typedef enum {
96 XFS_EXTFMT_NOSTATE = 0,
97 XFS_EXTFMT_HASSTATE
98} xfs_exntfmt_t;
99
100/*
101 * Possible extent states.
102 */
103typedef enum {
104 XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
105 XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
106} xfs_exntst_t;
107
108/*
109 * Extent state and extent format macros.
110 */
Nathan Scotta844f452005-11-02 14:38:42 +1100111#define XFS_EXTFMT_INODE(x) \
Eric Sandeen62118702008-03-06 13:44:28 +1100112 (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
Nathan Scotta844f452005-11-02 14:38:42 +1100113 XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN)
115
116/*
117 * Incore version of above.
118 */
119typedef struct xfs_bmbt_irec
120{
121 xfs_fileoff_t br_startoff; /* starting file offset */
122 xfs_fsblock_t br_startblock; /* starting block number */
123 xfs_filblks_t br_blockcount; /* number of blocks */
124 xfs_exntst_t br_state; /* extent state */
125} xfs_bmbt_irec_t;
126
127/*
128 * Key structure for non-leaf levels of the tree.
129 */
Christoph Hellwig8801bb92006-09-28 10:58:17 +1000130typedef struct xfs_bmbt_key {
131 __be64 br_startoff; /* starting file offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132} xfs_bmbt_key_t, xfs_bmdr_key_t;
133
Christoph Hellwig397b5202006-09-28 10:57:52 +1000134/* btree pointer type */
135typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
136
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100137/*
138 * Btree block header size depends on a superblock flag.
139 *
140 * (not quite yet, but soon)
141 */
142#define XFS_BMBT_BLOCK_LEN(mp) XFS_BTREE_LBLOCK_LEN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Christoph Hellwig136341b2008-10-30 17:11:40 +1100144#define XFS_BMBT_REC_ADDR(mp, block, index) \
145 ((xfs_bmbt_rec_t *) \
146 ((char *)(block) + \
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100147 XFS_BMBT_BLOCK_LEN(mp) + \
Christoph Hellwig136341b2008-10-30 17:11:40 +1100148 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100149
Christoph Hellwig136341b2008-10-30 17:11:40 +1100150#define XFS_BMBT_KEY_ADDR(mp, block, index) \
151 ((xfs_bmbt_key_t *) \
152 ((char *)(block) + \
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100153 XFS_BMBT_BLOCK_LEN(mp) + \
Christoph Hellwig136341b2008-10-30 17:11:40 +1100154 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Christoph Hellwig136341b2008-10-30 17:11:40 +1100156#define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
157 ((xfs_bmbt_ptr_t *) \
158 ((char *)(block) + \
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100159 XFS_BMBT_BLOCK_LEN(mp) + \
Christoph Hellwig136341b2008-10-30 17:11:40 +1100160 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
161 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
Eric Sandeen2c36dde2007-02-10 18:37:33 +1100162
Christoph Hellwig136341b2008-10-30 17:11:40 +1100163#define XFS_BMDR_REC_ADDR(block, index) \
164 ((xfs_bmdr_rec_t *) \
165 ((char *)(block) + \
166 sizeof(struct xfs_bmdr_block) + \
167 ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Christoph Hellwig136341b2008-10-30 17:11:40 +1100169#define XFS_BMDR_KEY_ADDR(block, index) \
170 ((xfs_bmdr_key_t *) \
171 ((char *)(block) + \
172 sizeof(struct xfs_bmdr_block) + \
173 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
174
175#define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
176 ((xfs_bmdr_ptr_t *) \
177 ((char *)(block) + \
178 sizeof(struct xfs_bmdr_block) + \
179 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
180 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * These are to be used when we know the size of the block and
184 * we don't have a cursor.
185 */
Christoph Hellwig136341b2008-10-30 17:11:40 +1100186#define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
187 XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
Nathan Scotta844f452005-11-02 14:38:42 +1100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100190 (int)(XFS_BTREE_LBLOCK_LEN + \
Nathan Scotta844f452005-11-02 14:38:42 +1100191 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define XFS_BMAP_BROOT_SPACE(bb) \
Christoph Hellwig16259e72005-11-02 15:11:25 +1100194 (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
Nathan Scotta844f452005-11-02 14:38:42 +1100195#define XFS_BMDR_SPACE_CALC(nrecs) \
196 (int)(sizeof(xfs_bmdr_block_t) + \
197 ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/*
200 * Maximum number of bmap btree levels.
201 */
Nathan Scotta844f452005-11-02 14:38:42 +1100202#define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * Prototypes for xfs_bmap.c to call.
206 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100207extern void xfs_bmdr_to_bmbt(struct xfs_mount *, xfs_bmdr_block_t *, int,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100208 struct xfs_btree_block *, int);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000209extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000210extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
211extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
212extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
213extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nathan Scotta844f452005-11-02 14:38:42 +1100215extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
Nathan Scotta844f452005-11-02 14:38:42 +1100216extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000218extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
219extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
Nathan Scotta844f452005-11-02 14:38:42 +1100220 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000221extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
222extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
223extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
224extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Nathan Scotta844f452005-11-02 14:38:42 +1100226extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
227extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
228 xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100230extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
Christoph Hellwig60197e82008-10-30 17:11:19 +1100231 xfs_bmdr_block_t *, int);
232
233extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
234extern int xfs_bmdr_maxrecs(struct xfs_mount *, int blocklen, int leaf);
235extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100237extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
238 struct xfs_trans *, struct xfs_inode *, int);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241#endif /* __XFS_BMAP_BTREE_H__ */