xfs_dir2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. extern const struct xfs_name xfs_name_dot;
  23. static inline bool
  24. xfs_dir2_samename(
  25. const struct xfs_name *n1,
  26. const struct xfs_name *n2)
  27. {
  28. if (n1 == n2)
  29. return true;
  30. if (n1->len != n2->len)
  31. return false;
  32. return !memcmp(n1->name, n2->name, n1->len);
  33. }
  34. enum xfs_dir2_fmt {
  35. XFS_DIR2_FMT_SF,
  36. XFS_DIR2_FMT_BLOCK,
  37. XFS_DIR2_FMT_LEAF,
  38. XFS_DIR2_FMT_NODE,
  39. XFS_DIR2_FMT_ERROR,
  40. };
  41. enum xfs_dir2_fmt xfs_dir2_format(struct xfs_da_args *args, int *error);
  42. /*
  43. * Convert inode mode to directory entry filetype
  44. */
  45. extern unsigned char xfs_mode_to_ftype(int mode);
  46. /*
  47. * Generic directory interface routines
  48. */
  49. extern void xfs_dir_startup(void);
  50. extern int xfs_da_mount(struct xfs_mount *mp);
  51. extern void xfs_da_unmount(struct xfs_mount *mp);
  52. extern int xfs_dir_isempty(struct xfs_inode *dp);
  53. extern int xfs_dir_init(struct xfs_trans *tp, struct xfs_inode *dp,
  54. struct xfs_inode *pdp);
  55. extern int xfs_dir_createname(struct xfs_trans *tp, struct xfs_inode *dp,
  56. const struct xfs_name *name, xfs_ino_t inum,
  57. xfs_extlen_t tot);
  58. extern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp,
  59. const struct xfs_name *name, xfs_ino_t *inum,
  60. struct xfs_name *ci_name);
  61. extern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp,
  62. const struct xfs_name *name, xfs_ino_t ino,
  63. xfs_extlen_t tot);
  64. extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
  65. const struct xfs_name *name, xfs_ino_t inum,
  66. xfs_extlen_t tot);
  67. extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
  68. const struct xfs_name *name);
  69. int xfs_dir_lookup_args(struct xfs_da_args *args);
  70. int xfs_dir_createname_args(struct xfs_da_args *args);
  71. int xfs_dir_removename_args(struct xfs_da_args *args);
  72. int xfs_dir_replace_args(struct xfs_da_args *args);
  73. /*
  74. * Direct call from the bmap code, bypassing the generic directory layer.
  75. */
  76. extern int xfs_dir2_sf_to_block(struct xfs_da_args *args);
  77. /*
  78. * Interface routines used by userspace utilities
  79. */
  80. extern int xfs_dir2_shrink_inode(struct xfs_da_args *args, xfs_dir2_db_t db,
  81. struct xfs_buf *bp);
  82. extern void xfs_dir2_data_freescan(struct xfs_mount *mp,
  83. struct xfs_dir2_data_hdr *hdr, int *loghead);
  84. extern void xfs_dir2_data_log_entry(struct xfs_da_args *args,
  85. struct xfs_buf *bp, struct xfs_dir2_data_entry *dep);
  86. extern void xfs_dir2_data_log_header(struct xfs_da_args *args,
  87. struct xfs_buf *bp);
  88. extern void xfs_dir2_data_log_unused(struct xfs_da_args *args,
  89. struct xfs_buf *bp, struct xfs_dir2_data_unused *dup);
  90. extern void xfs_dir2_data_make_free(struct xfs_da_args *args,
  91. struct xfs_buf *bp, xfs_dir2_data_aoff_t offset,
  92. xfs_dir2_data_aoff_t len, int *needlogp, int *needscanp);
  93. extern int xfs_dir2_data_use_free(struct xfs_da_args *args,
  94. struct xfs_buf *bp, struct xfs_dir2_data_unused *dup,
  95. xfs_dir2_data_aoff_t offset, xfs_dir2_data_aoff_t len,
  96. int *needlogp, int *needscanp);
  97. extern struct xfs_dir2_data_free *xfs_dir2_data_freefind(
  98. struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
  99. struct xfs_dir2_data_unused *dup);
  100. extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
  101. xfs_failaddr_t xfs_dir3_leaf_header_check(struct xfs_buf *bp, xfs_ino_t owner);
  102. xfs_failaddr_t xfs_dir3_data_header_check(struct xfs_buf *bp, xfs_ino_t owner);
  103. xfs_failaddr_t xfs_dir3_block_header_check(struct xfs_buf *bp, xfs_ino_t owner);
  104. extern const struct xfs_buf_ops xfs_dir3_block_buf_ops;
  105. extern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops;
  106. extern const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops;
  107. extern const struct xfs_buf_ops xfs_dir3_free_buf_ops;
  108. extern const struct xfs_buf_ops xfs_dir3_data_buf_ops;
  109. /*
  110. * Directory offset/block conversion functions.
  111. *
  112. * DB blocks here are logical directory block numbers, not filesystem blocks.
  113. */
  114. /*
  115. * Convert dataptr to byte in file space
  116. */
  117. static inline xfs_dir2_off_t
  118. xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
  119. {
  120. return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
  121. }
  122. /*
  123. * Convert byte in file space to dataptr. It had better be aligned.
  124. */
  125. static inline xfs_dir2_dataptr_t
  126. xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
  127. {
  128. return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
  129. }
  130. /*
  131. * Convert byte in space to (DB) block
  132. */
  133. static inline xfs_dir2_db_t
  134. xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  135. {
  136. return (xfs_dir2_db_t)(by >> geo->blklog);
  137. }
  138. /*
  139. * Convert dataptr to a block number
  140. */
  141. static inline xfs_dir2_db_t
  142. xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
  143. {
  144. return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
  145. }
  146. /*
  147. * Convert byte in space to offset in a block
  148. */
  149. static inline xfs_dir2_data_aoff_t
  150. xfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  151. {
  152. return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
  153. }
  154. /*
  155. * Convert dataptr to a byte offset in a block
  156. */
  157. static inline xfs_dir2_data_aoff_t
  158. xfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
  159. {
  160. return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
  161. }
  162. /*
  163. * Convert block and offset to byte in space
  164. */
  165. static inline xfs_dir2_off_t
  166. xfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
  167. xfs_dir2_data_aoff_t o)
  168. {
  169. return ((xfs_dir2_off_t)db << geo->blklog) + o;
  170. }
  171. /*
  172. * Convert block (DB) to block (dablk)
  173. */
  174. static inline xfs_dablk_t
  175. xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  176. {
  177. return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
  178. }
  179. /*
  180. * Convert byte in space to (DA) block
  181. */
  182. static inline xfs_dablk_t
  183. xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
  184. {
  185. return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
  186. }
  187. /*
  188. * Convert block and offset to dataptr
  189. */
  190. static inline xfs_dir2_dataptr_t
  191. xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
  192. xfs_dir2_data_aoff_t o)
  193. {
  194. return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
  195. }
  196. /*
  197. * Convert block (dablk) to block (DB)
  198. */
  199. static inline xfs_dir2_db_t
  200. xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
  201. {
  202. return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
  203. }
  204. /*
  205. * Convert block (dablk) to byte offset in space
  206. */
  207. static inline xfs_dir2_off_t
  208. xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
  209. {
  210. return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
  211. }
  212. /*
  213. * Directory tail pointer accessor functions. Based on block geometry.
  214. */
  215. static inline struct xfs_dir2_block_tail *
  216. xfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr)
  217. {
  218. return ((struct xfs_dir2_block_tail *)
  219. ((char *)hdr + geo->blksize)) - 1;
  220. }
  221. static inline struct xfs_dir2_leaf_tail *
  222. xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
  223. {
  224. return (struct xfs_dir2_leaf_tail *)
  225. ((char *)lp + geo->blksize -
  226. sizeof(struct xfs_dir2_leaf_tail));
  227. }
  228. /*
  229. * The Linux API doesn't pass down the total size of the buffer
  230. * we read into down to the filesystem. With the filldir concept
  231. * it's not needed for correct information, but the XFS dir2 leaf
  232. * code wants an estimate of the buffer size to calculate it's
  233. * readahead window and size the buffers used for mapping to
  234. * physical blocks.
  235. *
  236. * Try to give it an estimate that's good enough, maybe at some
  237. * point we can change the ->readdir prototype to include the
  238. * buffer size. For now we use the current glibc buffer size.
  239. * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE.
  240. */
  241. #define XFS_READDIR_BUFSIZE (32768)
  242. unsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype);
  243. unsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo,
  244. struct xfs_dir2_data_hdr *hdr);
  245. bool xfs_dir2_namecheck(const void *name, size_t length);
  246. /*
  247. * The "ascii-ci" feature was created to speed up case-insensitive lookups for
  248. * a Samba product. Because of the inherent problems with CI and UTF-8
  249. * encoding, etc, it was decided that Samba would be configured to export
  250. * latin1/iso 8859-1 encodings as that covered >90% of the target markets for
  251. * the product. Hence the "ascii-ci" casefolding code could be encoded into
  252. * the XFS directory operations and remove all the overhead of casefolding from
  253. * Samba.
  254. *
  255. * To provide consistent hashing behavior between the userspace and kernel,
  256. * these functions prepare names for hashing by transforming specific bytes
  257. * to other bytes. Robustness with other encodings is not guaranteed.
  258. */
  259. static inline bool xfs_ascii_ci_need_xfrm(unsigned char c)
  260. {
  261. if (c >= 0x41 && c <= 0x5a) /* A-Z */
  262. return true;
  263. if (c >= 0xc0 && c <= 0xd6) /* latin A-O with accents */
  264. return true;
  265. if (c >= 0xd8 && c <= 0xde) /* latin O-Y with accents */
  266. return true;
  267. return false;
  268. }
  269. static inline unsigned char xfs_ascii_ci_xfrm(unsigned char c)
  270. {
  271. if (xfs_ascii_ci_need_xfrm(c))
  272. c -= 'A' - 'a';
  273. return c;
  274. }
  275. struct xfs_dir_update_params {
  276. const struct xfs_inode *dp;
  277. const struct xfs_inode *ip;
  278. const struct xfs_name *name;
  279. int delta;
  280. };
  281. #ifdef CONFIG_XFS_LIVE_HOOKS
  282. void xfs_dir_update_hook(struct xfs_inode *dp, struct xfs_inode *ip,
  283. int delta, const struct xfs_name *name);
  284. struct xfs_dir_hook {
  285. struct xfs_hook dirent_hook;
  286. };
  287. void xfs_dir_hook_disable(void);
  288. void xfs_dir_hook_enable(void);
  289. int xfs_dir_hook_add(struct xfs_mount *mp, struct xfs_dir_hook *hook);
  290. void xfs_dir_hook_del(struct xfs_mount *mp, struct xfs_dir_hook *hook);
  291. void xfs_dir_hook_setup(struct xfs_dir_hook *hook, notifier_fn_t mod_fn);
  292. #else
  293. # define xfs_dir_update_hook(dp, ip, delta, name) ((void)0)
  294. #endif /* CONFIG_XFS_LIVE_HOOKS */
  295. struct xfs_parent_args;
  296. struct xfs_dir_update {
  297. struct xfs_inode *dp;
  298. const struct xfs_name *name;
  299. struct xfs_inode *ip;
  300. struct xfs_parent_args *ppargs;
  301. };
  302. int xfs_dir_create_child(struct xfs_trans *tp, unsigned int resblks,
  303. struct xfs_dir_update *du);
  304. int xfs_dir_add_child(struct xfs_trans *tp, unsigned int resblks,
  305. struct xfs_dir_update *du);
  306. int xfs_dir_remove_child(struct xfs_trans *tp, unsigned int resblks,
  307. struct xfs_dir_update *du);
  308. int xfs_dir_exchange_children(struct xfs_trans *tp, struct xfs_dir_update *du1,
  309. struct xfs_dir_update *du2, unsigned int spaceres);
  310. int xfs_dir_rename_children(struct xfs_trans *tp, struct xfs_dir_update *du_src,
  311. struct xfs_dir_update *du_tgt, unsigned int spaceres,
  312. struct xfs_dir_update *du_wip);
  313. #endif /* __XFS_DIR2_H__ */