xfs_dir2.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_DIR2_H__
  7. #define __XFS_DIR2_H__
  8. #include "xfs_da_format.h"
  9. #include "xfs_da_btree.h"
  10. struct xfs_da_args;
  11. struct xfs_inode;
  12. struct xfs_mount;
  13. struct xfs_trans;
  14. struct xfs_dir2_sf_hdr;
  15. struct xfs_dir2_sf_entry;
  16. struct xfs_dir2_data_hdr;
  17. struct xfs_dir2_data_entry;
  18. struct xfs_dir2_data_unused;
  19. struct xfs_dir3_icfree_hdr;
  20. struct xfs_dir3_icleaf_hdr;
  21. extern const struct xfs_name xfs_name_dotdot;
  22. /*
  23. * Convert inode mode to directory entry filetype
  24. */
  25. extern unsigned char xfs_mode_to_ftype(int mode);
  26. /*
  27. * Generic directory interface routines
  28. */
  29. extern void xfs_dir_startup(void);
  30. extern int xfs_da_mount(struct xfs_mount *mp);
  31. extern void xfs_da_unmount(struct xfs_mount *mp);
  32. extern int xfs_dir_isempty(struct xfs_inode *dp);
  33. extern int xfs_dir_init(struct xfs_trans *tp, struct xfs_inode *dp,
  34. struct xfs_inode *pdp);
  35. extern int xfs_dir_createname(struct xfs_trans *tp, struct xfs_inode *dp,
  36. const struct xfs_name *name, xfs_ino_t inum,
  37. xfs_extlen_t tot);
  38. extern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp,
  39. const struct xfs_name *name, xfs_ino_t *inum,
  40. struct xfs_name *ci_name);
  41. extern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp,
  42. struct xfs_name *name, xfs_ino_t ino,
  43. xfs_extlen_t tot);
  44. extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
  45. const struct xfs_name *name, xfs_ino_t inum,
  46. xfs_extlen_t tot);
  47. extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
  48. struct xfs_name *name);
  49. /*
  50. * Direct call from the bmap code, bypassing the generic directory layer.
  51. */
  52. extern int xfs_dir2_sf_to_block(struct xfs_da_args *args);
  53. /*
  54. * Interface routines used by userspace utilities
  55. */
  56. extern int xfs_dir2_isblock(struct xfs_da_args *args, bool *isblock);
  57. extern int xfs_dir2_isleaf(struct xfs_da_args *args, bool *isleaf);
  58. extern int xfs_dir2_shrink_inode(struct xfs_da_args *args, xfs_dir2_db_t db,
  59. struct xfs_buf *bp);
  60. extern void xfs_dir2_data_freescan(struct xfs_mount *mp,
  61. struct xfs_dir2_data_hdr *hdr, int *loghead);
  62. extern void xfs_dir2_data_log_entry(struct xfs_da_args *args,
  63. struct xfs_buf *bp, struct xfs_dir2_data_entry *dep);
  64. extern void xfs_dir2_data_log_header(struct xfs_da_args *args,
  65. struct xfs_buf *bp);
  66. extern void xfs_dir2_data_log_unused(struct xfs_da_args *args,
  67. struct xfs_buf *bp, struct xfs_dir2_data_unused *dup);
  68. extern void xfs_dir2_data_make_free(struct xfs_da_args *args,
  69. struct xfs_buf *bp, xfs_dir2_data_aoff_t offset,
  70. xfs_dir2_data_aoff_t len, int *needlogp, int *needscanp);
  71. extern int xfs_dir2_data_use_free(struct xfs_da_args *args,
  72. struct xfs_buf *bp, struct xfs_dir2_data_unused *dup,
  73. xfs_dir2_data_aoff_t offset, xfs_dir2_data_aoff_t len,
  74. int *needlogp, int *needscanp);
  75. extern struct xfs_dir2_data_free *xfs_dir2_data_freefind(
  76. struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
  77. struct xfs_dir2_data_unused *dup);
  78. extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
  79. extern const struct xfs_buf_ops xfs_dir3_block_buf_ops;
  80. extern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops;
  81. extern const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops;
  82. extern const struct xfs_buf_ops xfs_dir3_free_buf_ops;
  83. extern const struct xfs_buf_ops xfs_dir3_data_buf_ops;
  84. /*
  85. * Directory offset/block conversion functions.
  86. *
  87. * DB blocks here are logical directory block numbers, not filesystem blocks.
  88. */
  89. /*
  90. * Convert dataptr to byte in file space
  91. */
  92. static inline xfs_dir2_off_t
  93. xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
  94. {
  95. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  96. }
  97. /*
  98. * Convert byte in file space to dataptr. It had better be aligned.
  99. */
  100. static inline xfs_dir2_dataptr_t
  101. xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
  102. {
  103. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  104. }
  105. /*
  106. * Convert byte in space to (DB) block
  107. */
  108. static inline xfs_dir2_db_t
  109. xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  110. {
  111. return (xfs_dir2_db_t)(by >> geo->blklog);
  112. }
  113. /*
  114. * Convert dataptr to a block number
  115. */
  116. static inline xfs_dir2_db_t
  117. xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
  118. {
  119. return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
  120. }
  121. /*
  122. * Convert byte in space to offset in a block
  123. */
  124. static inline xfs_dir2_data_aoff_t
  125. xfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  126. {
  127. return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
  128. }
  129. /*
  130. * Convert dataptr to a byte offset in a block
  131. */
  132. static inline xfs_dir2_data_aoff_t
  133. xfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
  134. {
  135. return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
  136. }
  137. /*
  138. * Convert block and offset to byte in space
  139. */
  140. static inline xfs_dir2_off_t
  141. xfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
  142. xfs_dir2_data_aoff_t o)
  143. {
  144. return ((xfs_dir2_off_t)db << geo->blklog) + o;
  145. }
  146. /*
  147. * Convert block (DB) to block (dablk)
  148. */
  149. static inline xfs_dablk_t
  150. xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  151. {
  152. return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
  153. }
  154. /*
  155. * Convert byte in space to (DA) block
  156. */
  157. static inline xfs_dablk_t
  158. xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  159. {
  160. return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
  161. }
  162. /*
  163. * Convert block and offset to dataptr
  164. */
  165. static inline xfs_dir2_dataptr_t
  166. xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
  167. xfs_dir2_data_aoff_t o)
  168. {
  169. return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
  170. }
  171. /*
  172. * Convert block (dablk) to block (DB)
  173. */
  174. static inline xfs_dir2_db_t
  175. xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
  176. {
  177. return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
  178. }
  179. /*
  180. * Convert block (dablk) to byte offset in space
  181. */
  182. static inline xfs_dir2_off_t
  183. xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
  184. {
  185. return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
  186. }
  187. /*
  188. * Directory tail pointer accessor functions. Based on block geometry.
  189. */
  190. static inline struct xfs_dir2_block_tail *
  191. xfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr)
  192. {
  193. return ((struct xfs_dir2_block_tail *)
  194. ((char *)hdr + geo->blksize)) - 1;
  195. }
  196. static inline struct xfs_dir2_leaf_tail *
  197. xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
  198. {
  199. return (struct xfs_dir2_leaf_tail *)
  200. ((char *)lp + geo->blksize -
  201. sizeof(struct xfs_dir2_leaf_tail));
  202. }
  203. /*
  204. * The Linux API doesn't pass down the total size of the buffer
  205. * we read into down to the filesystem. With the filldir concept
  206. * it's not needed for correct information, but the XFS dir2 leaf
  207. * code wants an estimate of the buffer size to calculate it's
  208. * readahead window and size the buffers used for mapping to
  209. * physical blocks.
  210. *
  211. * Try to give it an estimate that's good enough, maybe at some
  212. * point we can change the ->readdir prototype to include the
  213. * buffer size. For now we use the current glibc buffer size.
  214. * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE.
  215. */
  216. #define XFS_READDIR_BUFSIZE (32768)
  217. unsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype);
  218. unsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo,
  219. struct xfs_dir2_data_hdr *hdr);
  220. bool xfs_dir2_namecheck(const void *name, size_t length);
  221. #endif /* __XFS_DIR2_H__ */