xfs_bmap_btree.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  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.
  13. *
  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
  17. */
  18. #ifndef __XFS_BMAP_BTREE_H__
  19. #define __XFS_BMAP_BTREE_H__
  20. #define XFS_BMAP_MAGIC 0x424d4150 /* 'BMAP' */
  21. struct xfs_btree_cur;
  22. struct xfs_btree_block;
  23. struct xfs_mount;
  24. struct xfs_inode;
  25. struct xfs_trans;
  26. /*
  27. * Bmap root header, on-disk form only.
  28. */
  29. typedef struct xfs_bmdr_block {
  30. __be16 bb_level; /* 0 is a leaf */
  31. __be16 bb_numrecs; /* current # of data records */
  32. } xfs_bmdr_block_t;
  33. /*
  34. * Bmap btree record and extent descriptor.
  35. * l0:63 is an extent flag (value 1 indicates non-normal).
  36. * l0:9-62 are startoff.
  37. * l0:0-8 and l1:21-63 are startblock.
  38. * l1:0-20 are blockcount.
  39. */
  40. #define BMBT_EXNTFLAG_BITLEN 1
  41. #define BMBT_STARTOFF_BITLEN 54
  42. #define BMBT_STARTBLOCK_BITLEN 52
  43. #define BMBT_BLOCKCOUNT_BITLEN 21
  44. typedef struct xfs_bmbt_rec {
  45. __be64 l0, l1;
  46. } xfs_bmbt_rec_t;
  47. typedef __uint64_t xfs_bmbt_rec_base_t; /* use this for casts */
  48. typedef xfs_bmbt_rec_t xfs_bmdr_rec_t;
  49. typedef struct xfs_bmbt_rec_host {
  50. __uint64_t l0, l1;
  51. } xfs_bmbt_rec_host_t;
  52. /*
  53. * Values and macros for delayed-allocation startblock fields.
  54. */
  55. #define STARTBLOCKVALBITS 17
  56. #define STARTBLOCKMASKBITS (15 + XFS_BIG_BLKNOS * 20)
  57. #define DSTARTBLOCKMASKBITS (15 + 20)
  58. #define STARTBLOCKMASK \
  59. (((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
  60. #define DSTARTBLOCKMASK \
  61. (((((xfs_dfsbno_t)1) << DSTARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
  62. static inline int isnullstartblock(xfs_fsblock_t x)
  63. {
  64. return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
  65. }
  66. static inline int isnulldstartblock(xfs_dfsbno_t x)
  67. {
  68. return ((x) & DSTARTBLOCKMASK) == DSTARTBLOCKMASK;
  69. }
  70. static inline xfs_fsblock_t nullstartblock(int k)
  71. {
  72. ASSERT(k < (1 << STARTBLOCKVALBITS));
  73. return STARTBLOCKMASK | (k);
  74. }
  75. static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
  76. {
  77. return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
  78. }
  79. /*
  80. * Possible extent formats.
  81. */
  82. typedef enum {
  83. XFS_EXTFMT_NOSTATE = 0,
  84. XFS_EXTFMT_HASSTATE
  85. } xfs_exntfmt_t;
  86. /*
  87. * Possible extent states.
  88. */
  89. typedef enum {
  90. XFS_EXT_NORM, XFS_EXT_UNWRITTEN,
  91. XFS_EXT_DMAPI_OFFLINE, XFS_EXT_INVALID
  92. } xfs_exntst_t;
  93. /*
  94. * Extent state and extent format macros.
  95. */
  96. #define XFS_EXTFMT_INODE(x) \
  97. (xfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
  98. XFS_EXTFMT_HASSTATE : XFS_EXTFMT_NOSTATE)
  99. #define ISUNWRITTEN(x) ((x)->br_state == XFS_EXT_UNWRITTEN)
  100. /*
  101. * Incore version of above.
  102. */
  103. typedef struct xfs_bmbt_irec
  104. {
  105. xfs_fileoff_t br_startoff; /* starting file offset */
  106. xfs_fsblock_t br_startblock; /* starting block number */
  107. xfs_filblks_t br_blockcount; /* number of blocks */
  108. xfs_exntst_t br_state; /* extent state */
  109. } xfs_bmbt_irec_t;
  110. /*
  111. * Key structure for non-leaf levels of the tree.
  112. */
  113. typedef struct xfs_bmbt_key {
  114. __be64 br_startoff; /* starting file offset */
  115. } xfs_bmbt_key_t, xfs_bmdr_key_t;
  116. /* btree pointer type */
  117. typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
  118. /*
  119. * Btree block header size depends on a superblock flag.
  120. *
  121. * (not quite yet, but soon)
  122. */
  123. #define XFS_BMBT_BLOCK_LEN(mp) XFS_BTREE_LBLOCK_LEN
  124. #define XFS_BMBT_REC_ADDR(mp, block, index) \
  125. ((xfs_bmbt_rec_t *) \
  126. ((char *)(block) + \
  127. XFS_BMBT_BLOCK_LEN(mp) + \
  128. ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
  129. #define XFS_BMBT_KEY_ADDR(mp, block, index) \
  130. ((xfs_bmbt_key_t *) \
  131. ((char *)(block) + \
  132. XFS_BMBT_BLOCK_LEN(mp) + \
  133. ((index) - 1) * sizeof(xfs_bmbt_key_t)))
  134. #define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
  135. ((xfs_bmbt_ptr_t *) \
  136. ((char *)(block) + \
  137. XFS_BMBT_BLOCK_LEN(mp) + \
  138. (maxrecs) * sizeof(xfs_bmbt_key_t) + \
  139. ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
  140. #define XFS_BMDR_REC_ADDR(block, index) \
  141. ((xfs_bmdr_rec_t *) \
  142. ((char *)(block) + \
  143. sizeof(struct xfs_bmdr_block) + \
  144. ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
  145. #define XFS_BMDR_KEY_ADDR(block, index) \
  146. ((xfs_bmdr_key_t *) \
  147. ((char *)(block) + \
  148. sizeof(struct xfs_bmdr_block) + \
  149. ((index) - 1) * sizeof(xfs_bmdr_key_t)))
  150. #define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
  151. ((xfs_bmdr_ptr_t *) \
  152. ((char *)(block) + \
  153. sizeof(struct xfs_bmdr_block) + \
  154. (maxrecs) * sizeof(xfs_bmdr_key_t) + \
  155. ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
  156. /*
  157. * These are to be used when we know the size of the block and
  158. * we don't have a cursor.
  159. */
  160. #define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
  161. XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
  162. #define XFS_BMAP_BROOT_SPACE_CALC(nrecs) \
  163. (int)(XFS_BTREE_LBLOCK_LEN + \
  164. ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
  165. #define XFS_BMAP_BROOT_SPACE(bb) \
  166. (XFS_BMAP_BROOT_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
  167. #define XFS_BMDR_SPACE_CALC(nrecs) \
  168. (int)(sizeof(xfs_bmdr_block_t) + \
  169. ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
  170. /*
  171. * Maximum number of bmap btree levels.
  172. */
  173. #define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
  174. /*
  175. * Prototypes for xfs_bmap.c to call.
  176. */
  177. extern void xfs_bmdr_to_bmbt(struct xfs_mount *, xfs_bmdr_block_t *, int,
  178. struct xfs_btree_block *, int);
  179. extern void xfs_bmbt_get_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
  180. extern xfs_filblks_t xfs_bmbt_get_blockcount(xfs_bmbt_rec_host_t *r);
  181. extern xfs_fsblock_t xfs_bmbt_get_startblock(xfs_bmbt_rec_host_t *r);
  182. extern xfs_fileoff_t xfs_bmbt_get_startoff(xfs_bmbt_rec_host_t *r);
  183. extern xfs_exntst_t xfs_bmbt_get_state(xfs_bmbt_rec_host_t *r);
  184. extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
  185. extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
  186. extern void xfs_bmbt_set_all(xfs_bmbt_rec_host_t *r, xfs_bmbt_irec_t *s);
  187. extern void xfs_bmbt_set_allf(xfs_bmbt_rec_host_t *r, xfs_fileoff_t o,
  188. xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
  189. extern void xfs_bmbt_set_blockcount(xfs_bmbt_rec_host_t *r, xfs_filblks_t v);
  190. extern void xfs_bmbt_set_startblock(xfs_bmbt_rec_host_t *r, xfs_fsblock_t v);
  191. extern void xfs_bmbt_set_startoff(xfs_bmbt_rec_host_t *r, xfs_fileoff_t v);
  192. extern void xfs_bmbt_set_state(xfs_bmbt_rec_host_t *r, xfs_exntst_t v);
  193. extern void xfs_bmbt_disk_set_allf(xfs_bmbt_rec_t *r, xfs_fileoff_t o,
  194. xfs_fsblock_t b, xfs_filblks_t c, xfs_exntst_t v);
  195. extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
  196. xfs_bmdr_block_t *, int);
  197. extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
  198. extern int xfs_bmdr_maxrecs(struct xfs_mount *, int blocklen, int leaf);
  199. extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
  200. extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
  201. struct xfs_trans *, struct xfs_inode *, int);
  202. #endif /* __XFS_BMAP_BTREE_H__ */