xfs_dir2_leaf.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. __be16 count; /* count of entries */
  46. __be16 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. __be32 hashval; /* hash value of name */
  53. __be32 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. __be32 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 __be16 *
  98. xfs_dir2_leaf_bests_p(xfs_dir2_leaf_tail_t *ltp)
  99. {
  100. return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
  101. }
  102. /*
  103. * Convert dataptr to byte in file space
  104. */
  105. #define XFS_DIR2_DATAPTR_TO_BYTE(mp,dp) xfs_dir2_dataptr_to_byte(mp, dp)
  106. static inline xfs_dir2_off_t
  107. xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  108. {
  109. return (xfs_dir2_off_t)(dp) << XFS_DIR2_DATA_ALIGN_LOG;
  110. }
  111. /*
  112. * Convert byte in file space to dataptr. It had better be aligned.
  113. */
  114. #define XFS_DIR2_BYTE_TO_DATAPTR(mp,by) xfs_dir2_byte_to_dataptr(mp,by)
  115. static inline xfs_dir2_dataptr_t
  116. xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
  117. {
  118. return (xfs_dir2_dataptr_t)((by) >> XFS_DIR2_DATA_ALIGN_LOG);
  119. }
  120. /*
  121. * Convert byte in space to (DB) block
  122. */
  123. #define XFS_DIR2_BYTE_TO_DB(mp,by) xfs_dir2_byte_to_db(mp, by)
  124. static inline xfs_dir2_db_t
  125. xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
  126. {
  127. return (xfs_dir2_db_t)((by) >> \
  128. ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog));
  129. }
  130. /*
  131. * Convert dataptr to a block number
  132. */
  133. #define XFS_DIR2_DATAPTR_TO_DB(mp,dp) xfs_dir2_dataptr_to_db(mp, dp)
  134. static inline xfs_dir2_db_t
  135. xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  136. {
  137. return XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
  138. }
  139. /*
  140. * Convert byte in space to offset in a block
  141. */
  142. #define XFS_DIR2_BYTE_TO_OFF(mp,by) xfs_dir2_byte_to_off(mp, by)
  143. static inline xfs_dir2_data_aoff_t
  144. xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
  145. {
  146. return (xfs_dir2_data_aoff_t)((by) & \
  147. ((1 << ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) - 1));
  148. }
  149. /*
  150. * Convert dataptr to a byte offset in a block
  151. */
  152. #define XFS_DIR2_DATAPTR_TO_OFF(mp,dp) xfs_dir2_dataptr_to_off(mp, dp)
  153. static inline xfs_dir2_data_aoff_t
  154. xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
  155. {
  156. return XFS_DIR2_BYTE_TO_OFF(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
  157. }
  158. /*
  159. * Convert block and offset to byte in space
  160. */
  161. #define XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o) \
  162. xfs_dir2_db_off_to_byte(mp, db, o)
  163. static inline xfs_dir2_off_t
  164. xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
  165. xfs_dir2_data_aoff_t o)
  166. {
  167. return ((xfs_dir2_off_t)(db) << \
  168. ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) + (o);
  169. }
  170. /*
  171. * Convert block (DB) to block (dablk)
  172. */
  173. #define XFS_DIR2_DB_TO_DA(mp,db) xfs_dir2_db_to_da(mp, db)
  174. static inline xfs_dablk_t
  175. xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
  176. {
  177. return (xfs_dablk_t)((db) << (mp)->m_sb.sb_dirblklog);
  178. }
  179. /*
  180. * Convert byte in space to (DA) block
  181. */
  182. #define XFS_DIR2_BYTE_TO_DA(mp,by) xfs_dir2_byte_to_da(mp, by)
  183. static inline xfs_dablk_t
  184. xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
  185. {
  186. return XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, by));
  187. }
  188. /*
  189. * Convert block and offset to dataptr
  190. */
  191. #define XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o) \
  192. xfs_dir2_db_off_to_dataptr(mp, db, o)
  193. static inline xfs_dir2_dataptr_t
  194. xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
  195. xfs_dir2_data_aoff_t o)
  196. {
  197. return XFS_DIR2_BYTE_TO_DATAPTR(mp, XFS_DIR2_DB_OFF_TO_BYTE(mp, db, o));
  198. }
  199. /*
  200. * Convert block (dablk) to block (DB)
  201. */
  202. #define XFS_DIR2_DA_TO_DB(mp,da) xfs_dir2_da_to_db(mp, da)
  203. static inline xfs_dir2_db_t
  204. xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
  205. {
  206. return (xfs_dir2_db_t)((da) >> (mp)->m_sb.sb_dirblklog);
  207. }
  208. /*
  209. * Convert block (dablk) to byte offset in space
  210. */
  211. #define XFS_DIR2_DA_TO_BYTE(mp,da) xfs_dir2_da_to_byte(mp, da)
  212. static inline xfs_dir2_off_t
  213. xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
  214. {
  215. return XFS_DIR2_DB_OFF_TO_BYTE(mp, XFS_DIR2_DA_TO_DB(mp, da), 0);
  216. }
  217. /*
  218. * Function declarations.
  219. */
  220. extern int xfs_dir2_block_to_leaf(struct xfs_da_args *args,
  221. struct xfs_dabuf *dbp);
  222. extern int xfs_dir2_leaf_addname(struct xfs_da_args *args);
  223. extern void xfs_dir2_leaf_compact(struct xfs_da_args *args,
  224. struct xfs_dabuf *bp);
  225. extern void xfs_dir2_leaf_compact_x1(struct xfs_dabuf *bp, int *indexp,
  226. int *lowstalep, int *highstalep,
  227. int *lowlogp, int *highlogp);
  228. extern int xfs_dir2_leaf_getdents(struct xfs_trans *tp, struct xfs_inode *dp,
  229. struct uio *uio, int *eofp,
  230. struct xfs_dirent *dbp, xfs_dir2_put_t put);
  231. extern int xfs_dir2_leaf_init(struct xfs_da_args *args, xfs_dir2_db_t bno,
  232. struct xfs_dabuf **bpp, int magic);
  233. extern void xfs_dir2_leaf_log_ents(struct xfs_trans *tp, struct xfs_dabuf *bp,
  234. int first, int last);
  235. extern void xfs_dir2_leaf_log_header(struct xfs_trans *tp,
  236. struct xfs_dabuf *bp);
  237. extern int xfs_dir2_leaf_lookup(struct xfs_da_args *args);
  238. extern int xfs_dir2_leaf_removename(struct xfs_da_args *args);
  239. extern int xfs_dir2_leaf_replace(struct xfs_da_args *args);
  240. extern int xfs_dir2_leaf_search_hash(struct xfs_da_args *args,
  241. struct xfs_dabuf *lbp);
  242. extern int xfs_dir2_leaf_trim_data(struct xfs_da_args *args,
  243. struct xfs_dabuf *lbp, xfs_dir2_db_t db);
  244. extern int xfs_dir2_node_to_leaf(struct xfs_da_state *state);
  245. #endif /* __XFS_DIR2_LEAF_H__ */