1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
9 #include "xfs_da_format.h"
10 #include "xfs_da_btree.h"
16 struct xfs_dir2_sf_hdr;
17 struct xfs_dir2_sf_entry;
18 struct xfs_dir2_data_hdr;
19 struct xfs_dir2_data_entry;
20 struct xfs_dir2_data_unused;
21 struct xfs_dir3_icfree_hdr;
22 struct xfs_dir3_icleaf_hdr;
24 extern const struct xfs_name xfs_name_dotdot;
25 extern const struct xfs_name xfs_name_dot;
29 const struct xfs_name *n1,
30 const struct xfs_name *n2)
34 if (n1->len != n2->len)
36 return !memcmp(n1->name, n2->name, n1->len);
40 * Convert inode mode to directory entry filetype
42 extern unsigned char xfs_mode_to_ftype(int mode);
45 * Generic directory interface routines
47 extern void xfs_dir_startup(void);
48 extern int xfs_da_mount(struct xfs_mount *mp);
49 extern void xfs_da_unmount(struct xfs_mount *mp);
51 extern int xfs_dir_isempty(struct xfs_inode *dp);
52 extern int xfs_dir_init(struct xfs_trans *tp, struct xfs_inode *dp,
53 struct xfs_inode *pdp);
54 extern int xfs_dir_createname(struct xfs_trans *tp, struct xfs_inode *dp,
55 const struct xfs_name *name, xfs_ino_t inum,
57 extern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp,
58 const struct xfs_name *name, xfs_ino_t *inum,
59 struct xfs_name *ci_name);
60 extern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp,
61 struct xfs_name *name, xfs_ino_t ino,
63 extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
64 const struct xfs_name *name, xfs_ino_t inum,
66 extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
67 struct xfs_name *name);
70 * Direct call from the bmap code, bypassing the generic directory layer.
72 extern int xfs_dir2_sf_to_block(struct xfs_da_args *args);
75 * Interface routines used by userspace utilities
77 extern int xfs_dir2_isblock(struct xfs_da_args *args, bool *isblock);
78 extern int xfs_dir2_isleaf(struct xfs_da_args *args, bool *isleaf);
79 extern int xfs_dir2_shrink_inode(struct xfs_da_args *args, xfs_dir2_db_t db,
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,
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);
98 extern struct xfs_dir2_data_free *xfs_dir2_data_freefind(
99 struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
100 struct xfs_dir2_data_unused *dup);
102 extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
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;
111 * Directory offset/block conversion functions.
113 * DB blocks here are logical directory block numbers, not filesystem blocks.
117 * Convert dataptr to byte in file space
119 static inline xfs_dir2_off_t
120 xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
122 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
126 * Convert byte in file space to dataptr. It had better be aligned.
128 static inline xfs_dir2_dataptr_t
129 xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
131 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
135 * Convert byte in space to (DB) block
137 static inline xfs_dir2_db_t
138 xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
140 return (xfs_dir2_db_t)(by >> geo->blklog);
144 * Convert dataptr to a block number
146 static inline xfs_dir2_db_t
147 xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
149 return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
153 * Convert byte in space to offset in a block
155 static inline xfs_dir2_data_aoff_t
156 xfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
158 return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
162 * Convert dataptr to a byte offset in a block
164 static inline xfs_dir2_data_aoff_t
165 xfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
167 return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
171 * Convert block and offset to byte in space
173 static inline xfs_dir2_off_t
174 xfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
175 xfs_dir2_data_aoff_t o)
177 return ((xfs_dir2_off_t)db << geo->blklog) + o;
181 * Convert block (DB) to block (dablk)
183 static inline xfs_dablk_t
184 xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
186 return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
190 * Convert byte in space to (DA) block
192 static inline xfs_dablk_t
193 xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
195 return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
199 * Convert block and offset to dataptr
201 static inline xfs_dir2_dataptr_t
202 xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
203 xfs_dir2_data_aoff_t o)
205 return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
209 * Convert block (dablk) to block (DB)
211 static inline xfs_dir2_db_t
212 xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
214 return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
218 * Convert block (dablk) to byte offset in space
220 static inline xfs_dir2_off_t
221 xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
223 return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
227 * Directory tail pointer accessor functions. Based on block geometry.
229 static inline struct xfs_dir2_block_tail *
230 xfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr)
232 return ((struct xfs_dir2_block_tail *)
233 ((char *)hdr + geo->blksize)) - 1;
236 static inline struct xfs_dir2_leaf_tail *
237 xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
239 return (struct xfs_dir2_leaf_tail *)
240 ((char *)lp + geo->blksize -
241 sizeof(struct xfs_dir2_leaf_tail));
245 * The Linux API doesn't pass down the total size of the buffer
246 * we read into down to the filesystem. With the filldir concept
247 * it's not needed for correct information, but the XFS dir2 leaf
248 * code wants an estimate of the buffer size to calculate it's
249 * readahead window and size the buffers used for mapping to
252 * Try to give it an estimate that's good enough, maybe at some
253 * point we can change the ->readdir prototype to include the
254 * buffer size. For now we use the current glibc buffer size.
255 * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE.
257 #define XFS_READDIR_BUFSIZE (32768)
259 unsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype);
260 unsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo,
261 struct xfs_dir2_data_hdr *hdr);
262 bool xfs_dir2_namecheck(const void *name, size_t length);
265 * The "ascii-ci" feature was created to speed up case-insensitive lookups for
266 * a Samba product. Because of the inherent problems with CI and UTF-8
267 * encoding, etc, it was decided that Samba would be configured to export
268 * latin1/iso 8859-1 encodings as that covered >90% of the target markets for
269 * the product. Hence the "ascii-ci" casefolding code could be encoded into
270 * the XFS directory operations and remove all the overhead of casefolding from
273 * To provide consistent hashing behavior between the userspace and kernel,
274 * these functions prepare names for hashing by transforming specific bytes
275 * to other bytes. Robustness with other encodings is not guaranteed.
277 static inline bool xfs_ascii_ci_need_xfrm(unsigned char c)
279 if (c >= 0x41 && c <= 0x5a) /* A-Z */
281 if (c >= 0xc0 && c <= 0xd6) /* latin A-O with accents */
283 if (c >= 0xd8 && c <= 0xde) /* latin O-Y with accents */
288 static inline unsigned char xfs_ascii_ci_xfrm(unsigned char c)
290 if (xfs_ascii_ci_need_xfrm(c))
295 #endif /* __XFS_DIR2_H__ */