xfs_bmap_btree.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_BMAP_BTREE_H__
  7. #define __XFS_BMAP_BTREE_H__
  8. struct xfs_btree_cur;
  9. struct xfs_btree_block;
  10. struct xfs_mount;
  11. struct xfs_inode;
  12. struct xfs_trans;
  13. struct xbtree_ifakeroot;
  14. /*
  15. * Maximum number of bmap btree levels.
  16. */
  17. #define XFS_BM_MAXLEVELS(mp,w) ((mp)->m_bm_maxlevels[(w)])
  18. /*
  19. * Prototypes for xfs_bmap.c to call.
  20. */
  21. extern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
  22. struct xfs_btree_block *, int);
  23. void xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
  24. extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(const struct xfs_bmbt_rec *r);
  25. extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(const struct xfs_bmbt_rec *r);
  26. void xfs_bmbt_disk_get_all(const struct xfs_bmbt_rec *r,
  27. struct xfs_bmbt_irec *s);
  28. extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
  29. xfs_bmdr_block_t *, int);
  30. extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
  31. extern int xfs_bmdr_maxrecs(int blocklen, int leaf);
  32. unsigned int xfs_bmbt_maxrecs(struct xfs_mount *mp, unsigned int blocklen,
  33. bool leaf);
  34. extern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
  35. int whichfork, xfs_ino_t new_owner,
  36. struct list_head *buffer_list);
  37. extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
  38. struct xfs_trans *, struct xfs_inode *, int);
  39. void xfs_bmbt_commit_staged_btree(struct xfs_btree_cur *cur,
  40. struct xfs_trans *tp, int whichfork);
  41. extern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
  42. unsigned long long len);
  43. unsigned int xfs_bmbt_maxlevels_ondisk(void);
  44. int __init xfs_bmbt_init_cur_cache(void);
  45. void xfs_bmbt_destroy_cur_cache(void);
  46. void xfs_bmbt_init_block(struct xfs_inode *ip, struct xfs_btree_block *buf,
  47. struct xfs_buf *bp, __u16 level, __u16 numrecs);
  48. /*
  49. * Btree block header size depends on a superblock flag.
  50. */
  51. static inline size_t
  52. xfs_bmbt_block_len(struct xfs_mount *mp)
  53. {
  54. return xfs_has_crc(mp) ?
  55. XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN;
  56. }
  57. /* Addresses of key, pointers, and records within an incore bmbt block. */
  58. static inline struct xfs_bmbt_rec *
  59. xfs_bmbt_rec_addr(
  60. struct xfs_mount *mp,
  61. struct xfs_btree_block *block,
  62. unsigned int index)
  63. {
  64. return (struct xfs_bmbt_rec *)
  65. ((char *)block + xfs_bmbt_block_len(mp) +
  66. (index - 1) * sizeof(struct xfs_bmbt_rec));
  67. }
  68. static inline struct xfs_bmbt_key *
  69. xfs_bmbt_key_addr(
  70. struct xfs_mount *mp,
  71. struct xfs_btree_block *block,
  72. unsigned int index)
  73. {
  74. return (struct xfs_bmbt_key *)
  75. ((char *)block + xfs_bmbt_block_len(mp) +
  76. (index - 1) * sizeof(struct xfs_bmbt_key *));
  77. }
  78. static inline xfs_bmbt_ptr_t *
  79. xfs_bmbt_ptr_addr(
  80. struct xfs_mount *mp,
  81. struct xfs_btree_block *block,
  82. unsigned int index,
  83. unsigned int maxrecs)
  84. {
  85. return (xfs_bmbt_ptr_t *)
  86. ((char *)block + xfs_bmbt_block_len(mp) +
  87. maxrecs * sizeof(struct xfs_bmbt_key) +
  88. (index - 1) * sizeof(xfs_bmbt_ptr_t));
  89. }
  90. /* Addresses of key, pointers, and records within an ondisk bmbt block. */
  91. static inline struct xfs_bmbt_rec *
  92. xfs_bmdr_rec_addr(
  93. struct xfs_bmdr_block *block,
  94. unsigned int index)
  95. {
  96. return (struct xfs_bmbt_rec *)
  97. ((char *)(block + 1) +
  98. (index - 1) * sizeof(struct xfs_bmbt_rec));
  99. }
  100. static inline struct xfs_bmbt_key *
  101. xfs_bmdr_key_addr(
  102. struct xfs_bmdr_block *block,
  103. unsigned int index)
  104. {
  105. return (struct xfs_bmbt_key *)
  106. ((char *)(block + 1) +
  107. (index - 1) * sizeof(struct xfs_bmbt_key));
  108. }
  109. static inline xfs_bmbt_ptr_t *
  110. xfs_bmdr_ptr_addr(
  111. struct xfs_bmdr_block *block,
  112. unsigned int index,
  113. unsigned int maxrecs)
  114. {
  115. return (xfs_bmbt_ptr_t *)
  116. ((char *)(block + 1) +
  117. maxrecs * sizeof(struct xfs_bmbt_key) +
  118. (index - 1) * sizeof(xfs_bmbt_ptr_t));
  119. }
  120. /*
  121. * Address of pointers within the incore btree root.
  122. *
  123. * These are to be used when we know the size of the block and
  124. * we don't have a cursor.
  125. */
  126. static inline xfs_bmbt_ptr_t *
  127. xfs_bmap_broot_ptr_addr(
  128. struct xfs_mount *mp,
  129. struct xfs_btree_block *bb,
  130. unsigned int i,
  131. unsigned int sz)
  132. {
  133. return xfs_bmbt_ptr_addr(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, false));
  134. }
  135. /*
  136. * Compute the space required for the incore btree root containing the given
  137. * number of records.
  138. */
  139. static inline size_t
  140. xfs_bmap_broot_space_calc(
  141. struct xfs_mount *mp,
  142. unsigned int nrecs)
  143. {
  144. return xfs_bmbt_block_len(mp) +
  145. (nrecs * (sizeof(struct xfs_bmbt_key) + sizeof(xfs_bmbt_ptr_t)));
  146. }
  147. /*
  148. * Compute the space required for the incore btree root given the ondisk
  149. * btree root block.
  150. */
  151. static inline size_t
  152. xfs_bmap_broot_space(
  153. struct xfs_mount *mp,
  154. struct xfs_bmdr_block *bb)
  155. {
  156. return xfs_bmap_broot_space_calc(mp, be16_to_cpu(bb->bb_numrecs));
  157. }
  158. /* Compute the space required for the ondisk root block. */
  159. static inline size_t
  160. xfs_bmdr_space_calc(unsigned int nrecs)
  161. {
  162. return sizeof(struct xfs_bmdr_block) +
  163. (nrecs * (sizeof(struct xfs_bmbt_key) + sizeof(xfs_bmbt_ptr_t)));
  164. }
  165. /*
  166. * Compute the space required for the ondisk root block given an incore root
  167. * block.
  168. */
  169. static inline size_t
  170. xfs_bmap_bmdr_space(struct xfs_btree_block *bb)
  171. {
  172. return xfs_bmdr_space_calc(be16_to_cpu(bb->bb_numrecs));
  173. }
  174. #endif /* __XFS_BMAP_BTREE_H__ */