xfs_dir2_leaf.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2000-2001,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_DIR2_LEAF_H__
  19. #define __XFS_DIR2_LEAF_H__
  20. struct uio;
  21. struct xfs_dabuf;
  22. struct xfs_da_args;
  23. struct xfs_inode;
  24. struct xfs_mount;
  25. struct xfs_trans;
  26. /*
  27. * Offset of the leaf/node space. First block in this space
  28. * is the btree root.
  29. */
  30. #define XFS_DIR2_LEAF_SPACE 1
  31. #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
  32. #define XFS_DIR2_LEAF_FIRSTDB(mp) \
  33. XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_LEAF_OFFSET)
  34. /*
  35. * Offset in data space of a data entry.
  36. */
  37. typedef __uint32_t xfs_dir2_dataptr_t;
  38. #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
  39. #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
  40. /*
  41. * Leaf block header.
  42. */
  43. typedef struct xfs_dir2_leaf_hdr {
  44. xfs_da_blkinfo_t info; /* header for da routines */
  45. __uint16_t count; /* count of entries */
  46. __uint16_t stale; /* count of stale entries */
  47. } xfs_dir2_leaf_hdr_t;
  48. /*
  49. * Leaf block entry.
  50. */
  51. typedef struct xfs_dir2_leaf_entry {
  52. xfs_dahash_t hashval; /* hash value of name */
  53. xfs_dir2_dataptr_t address; /* address of data entry */
  54. } xfs_dir2_leaf_entry_t;
  55. /*
  56. * Leaf block tail.
  57. */
  58. typedef struct xfs_dir2_leaf_tail {
  59. __uint32_t bestcount;
  60. } xfs_dir2_leaf_tail_t;
  61. /*
  62. * Leaf block.
  63. * bests and tail are at the end of the block for single-leaf only
  64. * (magic = XFS_DIR2_LEAF1_MAGIC not XFS_DIR2_LEAFN_MAGIC).
  65. */
  66. typedef struct xfs_dir2_leaf {
  67. xfs_dir2_leaf_hdr_t hdr; /* leaf header */
  68. xfs_dir2_leaf_entry_t ents[1]; /* entries */
  69. /* ... */
  70. xfs_dir2_data_off_t bests[1]; /* best free counts */
  71. xfs_dir2_leaf_tail_t tail; /* leaf tail */
  72. } xfs_dir2_leaf_t;
  73. /*
  74. * DB blocks here are logical directory block numbers, not filesystem blocks.
  75. */
  76. #define XFS_DIR2_MAX_LEAF_ENTS(mp) xfs_dir2_max_leaf_ents(mp)
  77. static inline int xfs_dir2_max_leaf_ents(struct xfs_mount *mp)
  78. {
  79. return (int)(((mp)->m_dirblksize - (uint)sizeof(xfs_dir2_leaf_hdr_t)) /
  80. (uint)sizeof(xfs_dir2_leaf_entry_t));
  81. }
  82. /*
  83. * Get address of the bestcount field in the single-leaf block.
  84. */
  85. #define XFS_DIR2_LEAF_TAIL_P(mp,lp) xfs_dir2_leaf_tail_p(mp, lp)
  86. static inline xfs_dir2_leaf_tail_t *
  87. xfs_dir2_leaf_tail_p(struct xfs_mount *mp, xfs_dir2_leaf_t *lp)
  88. {
  89. return (xfs_dir2_leaf_tail_t *)
  90. ((char *)(lp) + (mp)->m_dirblksize -
  91. (uint)sizeof(xfs_dir2_leaf_tail_t));
  92. }
  93. /*
  94. * Get address of the bests array in the single-leaf block.
  95. */
  96. #define XFS_DIR2_LEAF_BESTS_P(ltp) xfs_dir2_leaf_bests_p(ltp)
  97. static inline xfs_dir2_data_off_t *
  98. xfs_dir2_leaf_bests_p(xfs_dir2_leaf_tail_t *ltp)
  99. {
  100. return (xfs_dir2_data_off_t *)
  101. (ltp) - INT_GET((ltp)->bestcount, ARCH_CONVERT);
  102. }
  103. /*
  104. * Convert dataptr to byte in file space
  105. */
  106. #define XFS_DIR2_DATAPTR_TO_BYTE(mp,dp) xfs_dir2_dataptr_to_byte(mp, dp)
  107. static inline xfs_dir2_off_t
  108. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  109. {
  110. return (xfs_dir2_off_t)(dp) << XFS_DIR2_DATA_ALIGN_LOG;
  111. }
  112. /*
  113. * Convert byte in file space to dataptr. It had better be aligned.
  114. */
  115. #define XFS_DIR2_BYTE_TO_DATAPTR(mp,by) xfs_dir2_byte_to_dataptr(mp,by)
  116. static inline xfs_dir2_dataptr_t
  117. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  118. {
  119. return (xfs_dir2_dataptr_t)((by) >> XFS_DIR2_DATA_ALIGN_LOG);
  120. }
  121. /*
  122. * Convert byte in space to (DB) block
  123. */
  124. #define XFS_DIR2_BYTE_TO_DB(mp,by) xfs_dir2_byte_to_db(mp, by)
  125. static inline xfs_dir2_db_t
  126. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  127. {
  128. return (xfs_dir2_db_t)((by) >> \
  129. ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog));
  130. }
  131. /*
  132. * Convert dataptr to a block number
  133. */
  134. #define XFS_DIR2_DATAPTR_TO_DB(mp,dp) xfs_dir2_dataptr_to_db(mp, dp)
  135. static inline xfs_dir2_db_t
  136. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  137. {
  138. return XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
  139. }
  140. /*
  141. * Convert byte in space to offset in a block
  142. */
  143. #define XFS_DIR2_BYTE_TO_OFF(mp,by) xfs_dir2_byte_to_off(mp, by)
  144. static inline xfs_dir2_data_aoff_t
  145. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  146. {
  147. return (xfs_dir2_data_aoff_t)((by) & \
  148. ((1 << ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) - 1));
  149. }
  150. /*
  151. * Convert dataptr to a byte offset in a block
  152. */
  153. #define XFS_DIR2_DATAPTR_TO_OFF(mp,dp) xfs_dir2_dataptr_to_off(mp, dp)
  154. static inline xfs_dir2_data_aoff_t
  155. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  156. {
  157. return XFS_DIR2_BYTE_TO_OFF(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
  158. }
  159. /*
  160. * Convert block and offset to byte in space
  161. */
  162. #define XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o) \
  163. xfs_dir2_db_off_to_byte(mp, db, o)
  164. static inline xfs_dir2_off_t
  165. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  166. xfs_dir2_data_aoff_t o)
  167. {
  168. return ((xfs_dir2_off_t)(db) << \
  169. ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) + (o);
  170. }
  171. /*
  172. * Convert block (DB) to block (dablk)
  173. */
  174. #define XFS_DIR2_DB_TO_DA(mp,db) xfs_dir2_db_to_da(mp, db)
  175. static inline xfs_dablk_t
  176. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  177. {
  178. return (xfs_dablk_t)((db) << (mp)->m_sb.sb_dirblklog);
  179. }
  180. /*
  181. * Convert byte in space to (DA) block
  182. */
  183. #define XFS_DIR2_BYTE_TO_DA(mp,by) xfs_dir2_byte_to_da(mp, by)
  184. static inline xfs_dablk_t
  185. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  186. {
  187. return XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, by));
  188. }
  189. /*
  190. * Convert block and offset to dataptr
  191. */
  192. #define XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o) \
  193. xfs_dir2_db_off_to_dataptr(mp, db, o)
  194. static inline xfs_dir2_dataptr_t
  195. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  196. xfs_dir2_data_aoff_t o)
  197. {
  198. return XFS_DIR2_BYTE_TO_DATAPTR(mp, XFS_DIR2_DB_OFF_TO_BYTE(mp, db, o));
  199. }
  200. /*
  201. * Convert block (dablk) to block (DB)
  202. */
  203. #define XFS_DIR2_DA_TO_DB(mp,da) xfs_dir2_da_to_db(mp, da)
  204. static inline xfs_dir2_db_t
  205. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  206. {
  207. return (xfs_dir2_db_t)((da) >> (mp)->m_sb.sb_dirblklog);
  208. }
  209. /*
  210. * Convert block (dablk) to byte offset in space
  211. */
  212. #define XFS_DIR2_DA_TO_BYTE(mp,da) xfs_dir2_da_to_byte(mp, da)
  213. static inline xfs_dir2_off_t
  214. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  215. {
  216. return XFS_DIR2_DB_OFF_TO_BYTE(mp, XFS_DIR2_DA_TO_DB(mp, da), 0);
  217. }
  218. /*
  219. * Function declarations.
  220. */
  221. extern int xfs_dir2_block_to_leaf(struct xfs_da_args *args,
  222. struct xfs_dabuf *dbp);
  223. extern int xfs_dir2_leaf_addname(struct xfs_da_args *args);
  224. extern void xfs_dir2_leaf_compact(struct xfs_da_args *args,
  225. struct xfs_dabuf *bp);
  226. extern void xfs_dir2_leaf_compact_x1(struct xfs_dabuf *bp, int *indexp,
  227. int *lowstalep, int *highstalep,
  228. int *lowlogp, int *highlogp);
  229. extern int xfs_dir2_leaf_getdents(struct xfs_trans *tp, struct xfs_inode *dp,
  230. struct uio *uio, int *eofp,
  231. struct xfs_dirent *dbp, xfs_dir2_put_t put);
  232. extern int xfs_dir2_leaf_init(struct xfs_da_args *args, xfs_dir2_db_t bno,
  233. struct xfs_dabuf **bpp, int magic);
  234. extern void xfs_dir2_leaf_log_ents(struct xfs_trans *tp, struct xfs_dabuf *bp,
  235. int first, int last);
  236. extern void xfs_dir2_leaf_log_header(struct xfs_trans *tp,
  237. struct xfs_dabuf *bp);
  238. extern int xfs_dir2_leaf_lookup(struct xfs_da_args *args);
  239. extern int xfs_dir2_leaf_removename(struct xfs_da_args *args);
  240. extern int xfs_dir2_leaf_replace(struct xfs_da_args *args);
  241. extern int xfs_dir2_leaf_search_hash(struct xfs_da_args *args,
  242. struct xfs_dabuf *lbp);
  243. extern int xfs_dir2_leaf_trim_data(struct xfs_da_args *args,
  244. struct xfs_dabuf *lbp, xfs_dir2_db_t db);
  245. extern int xfs_dir2_node_to_leaf(struct xfs_da_state *state);
  246. #endif /* __XFS_DIR2_LEAF_H__ */