Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | #ifndef __XFS_BMAP_BTREE_H__ |
| 19 | #define __XFS_BMAP_BTREE_H__ |
| 20 | |
| 21 | #define XFS_BMAP_MAGIC 0x424d4150 /* 'BMAP' */ |
| 22 | |
| 23 | struct xfs_btree_cur; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 24 | struct xfs_btree_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct xfs_mount; |
| 26 | struct xfs_inode; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 27 | struct xfs_trans; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Bmap root header, on-disk form only. |
| 31 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 32 | typedef struct xfs_bmdr_block { |
| 33 | __be16 bb_level; /* 0 is a leaf */ |
| 34 | __be16 bb_numrecs; /* current # of data records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } xfs_bmdr_block_t; |
| 36 | |
| 37 | /* |
| 38 | * Bmap btree record and extent descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define BMBT_EXNTFLAG_BITLEN 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define BMBT_STARTOFF_BITLEN 54 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #define BMBT_STARTBLOCK_BITLEN 52 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define BMBT_BLOCKCOUNT_BITLEN 21 |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #define BMBT_USE_64 1 |
| 51 | |
| 52 | typedef struct xfs_bmbt_rec_32 |
| 53 | { |
| 54 | __uint32_t l0, l1, l2, l3; |
| 55 | } xfs_bmbt_rec_32_t; |
| 56 | typedef struct xfs_bmbt_rec_64 |
| 57 | { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 58 | __be64 l0, l1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } xfs_bmbt_rec_64_t; |
| 60 | |
| 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; |
| 63 | |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 64 | typedef struct xfs_bmbt_rec_host { |
| 65 | __uint64_t l0, l1; |
| 66 | } xfs_bmbt_rec_host_t; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #define ISNULLSTARTBLOCK(x) isnullstartblock(x) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 80 | static inline int isnullstartblock(xfs_fsblock_t x) |
| 81 | { |
| 82 | return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #define ISNULLDSTARTBLOCK(x) isnulldstartblock(x) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 86 | static inline int isnulldstartblock(xfs_dfsbno_t x) |
| 87 | { |
| 88 | return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | #define NULLSTARTBLOCK(k) nullstartblock(k) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 92 | static inline xfs_fsblock_t nullstartblock(int k) |
| 93 | { |
| 94 | ASSERT(k < (1 << STARTBLOCKVALBITS)); |
| 95 | return STARTBLOCKMASK | (k); |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #define STARTBLOCKVAL(x) startblockval(x) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 99 | static inline xfs_filblks_t startblockval(xfs_fsblock_t x) |
| 100 | { |
| 101 | return (xfs_filblks_t)((x) & ~STARTBLOCKMASK); |
| 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Possible extent formats. |
| 106 | */ |
| 107 | typedef enum { |
| 108 | XFS_EXTFMT_NOSTATE = 0, |
| 109 | XFS_EXTFMT_HASSTATE |
| 110 | } xfs_exntfmt_t; |
| 111 | |
| 112 | /* |
| 113 | * Possible extent states. |
| 114 | */ |
| 115 | typedef 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 | */ |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 123 | #define XFS_EXTFMT_INODE(x) \ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 124 | (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \ |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 125 | XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | #define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN) |
| 127 | |
| 128 | /* |
| 129 | * Incore version of above. |
| 130 | */ |
| 131 | typedef 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 | */ |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 142 | typedef struct xfs_bmbt_key { |
| 143 | __be64 br_startoff; /* starting file offset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } xfs_bmbt_key_t, xfs_bmdr_key_t; |
| 145 | |
Christoph Hellwig | 397b520 | 2006-09-28 10:57:52 +1000 | [diff] [blame] | 146 | /* btree pointer type */ |
| 147 | typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t; |
| 148 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 149 | /* |
| 150 | * Btree block header size depends on a superblock flag. |
| 151 | * |
| 152 | * (not quite yet, but soon) |
| 153 | */ |
| 154 | #define XFS_BMBT_BLOCK_LEN(mp) XFS_BTREE_LBLOCK_LEN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 156 | #define XFS_BMBT_REC_ADDR(mp, block, index) \ |
| 157 | ((xfs_bmbt_rec_t *) \ |
| 158 | ((char *)(block) + \ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 159 | XFS_BMBT_BLOCK_LEN(mp) + \ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 160 | ((index) - 1) * sizeof(xfs_bmbt_rec_t))) |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 161 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 162 | #define XFS_BMBT_KEY_ADDR(mp, block, index) \ |
| 163 | ((xfs_bmbt_key_t *) \ |
| 164 | ((char *)(block) + \ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 165 | XFS_BMBT_BLOCK_LEN(mp) + \ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 166 | ((index) - 1) * sizeof(xfs_bmbt_key_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 168 | #define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \ |
| 169 | ((xfs_bmbt_ptr_t *) \ |
| 170 | ((char *)(block) + \ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 171 | XFS_BMBT_BLOCK_LEN(mp) + \ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 172 | (maxrecs) * sizeof(xfs_bmbt_key_t) + \ |
| 173 | ((index) - 1) * sizeof(xfs_bmbt_ptr_t))) |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 174 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 175 | #define XFS_BMDR_REC_ADDR(block, index) \ |
| 176 | ((xfs_bmdr_rec_t *) \ |
| 177 | ((char *)(block) + \ |
| 178 | sizeof(struct xfs_bmdr_block) + \ |
| 179 | ((index) - 1) * sizeof(xfs_bmdr_rec_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 181 | #define XFS_BMDR_KEY_ADDR(block, index) \ |
| 182 | ((xfs_bmdr_key_t *) \ |
| 183 | ((char *)(block) + \ |
| 184 | sizeof(struct xfs_bmdr_block) + \ |
| 185 | ((index) - 1) * sizeof(xfs_bmdr_key_t))) |
| 186 | |
| 187 | #define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \ |
| 188 | ((xfs_bmdr_ptr_t *) \ |
| 189 | ((char *)(block) + \ |
| 190 | sizeof(struct xfs_bmdr_block) + \ |
| 191 | (maxrecs) * sizeof(xfs_bmdr_key_t) + \ |
| 192 | ((index) - 1) * sizeof(xfs_bmdr_ptr_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * These are to be used when we know the size of the block and |
| 196 | * we don't have a cursor. |
| 197 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 198 | #define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \ |
| 199 | XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0)) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | #define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 202 | (int)(XFS_BTREE_LBLOCK_LEN + \ |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 203 | ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)))) |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | #define XFS_BMAP_BROOT_SPACE(bb) \ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 206 | (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs))) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 207 | #define XFS_BMDR_SPACE_CALC(nrecs) \ |
| 208 | (int)(sizeof(xfs_bmdr_block_t) + \ |
| 209 | ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * Maximum number of bmap btree levels. |
| 213 | */ |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 214 | #define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Prototypes for xfs_bmap.c to call. |
| 218 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 219 | extern void xfs_bmdr_to_bmbt(struct xfs_mount *, xfs_bmdr_block_t *, int, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 220 | struct xfs_btree_block *, int); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 221 | extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 222 | extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r); |
| 223 | extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r); |
| 224 | extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r); |
| 225 | extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 227 | extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s); |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 228 | extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r); |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 229 | extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 231 | extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s); |
| 232 | extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o, |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 233 | xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 234 | extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v); |
| 235 | extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v); |
| 236 | extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v); |
| 237 | extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 239 | extern void xfs_bmbt_disk_set_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s); |
| 240 | extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o, |
| 241 | xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 243 | extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int, |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 244 | xfs_bmdr_block_t *, int); |
| 245 | |
| 246 | extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level); |
| 247 | extern int xfs_bmdr_maxrecs(struct xfs_mount *, int blocklen, int leaf); |
| 248 | extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 250 | extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *, |
| 251 | struct xfs_trans *, struct xfs_inode *, int); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | #endif /* __XFS_BMAP_BTREE_H__ */ |