xfs_dir2_leaf.h 7.8 KB

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