xref: /linux/fs/xfs/libxfs/xfs_dir2.h (revision 1e58a8ccf2597c9259a8e71a2bffac5e11e12ea0)
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 
9 #include "xfs_da_format.h"
10 #include "xfs_da_btree.h"
11 
12 struct xfs_da_args;
13 struct xfs_inode;
14 struct xfs_mount;
15 struct xfs_trans;
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;
23 
24 extern const struct xfs_name	xfs_name_dotdot;
25 extern const struct xfs_name	xfs_name_dot;
26 
27 static inline bool
28 xfs_dir2_samename(
29 	const struct xfs_name	*n1,
30 	const struct xfs_name	*n2)
31 {
32 	if (n1 == n2)
33 		return true;
34 	if (n1->len != n2->len)
35 		return false;
36 	return !memcmp(n1->name, n2->name, n1->len);
37 }
38 
39 /*
40  * Convert inode mode to directory entry filetype
41  */
42 extern unsigned char xfs_mode_to_ftype(int mode);
43 
44 /*
45  * Generic directory interface routines
46  */
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);
50 
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,
56 				xfs_extlen_t tot);
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,
62 				xfs_extlen_t tot);
63 extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
64 				const struct xfs_name *name, xfs_ino_t inum,
65 				xfs_extlen_t tot);
66 extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
67 				struct xfs_name *name);
68 
69 /*
70  * Direct call from the bmap code, bypassing the generic directory layer.
71  */
72 extern int xfs_dir2_sf_to_block(struct xfs_da_args *args);
73 
74 /*
75  * Interface routines used by userspace utilities
76  */
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,
80 				struct xfs_buf *bp);
81 
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 
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);
101 
102 extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
103 
104 xfs_failaddr_t xfs_dir3_leaf_header_check(struct xfs_buf *bp, xfs_ino_t owner);
105 xfs_failaddr_t xfs_dir3_data_header_check(struct xfs_buf *bp, xfs_ino_t owner);
106 xfs_failaddr_t xfs_dir3_block_header_check(struct xfs_buf *bp, xfs_ino_t owner);
107 
108 extern const struct xfs_buf_ops xfs_dir3_block_buf_ops;
109 extern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops;
110 extern const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops;
111 extern const struct xfs_buf_ops xfs_dir3_free_buf_ops;
112 extern const struct xfs_buf_ops xfs_dir3_data_buf_ops;
113 
114 /*
115  * Directory offset/block conversion functions.
116  *
117  * DB blocks here are logical directory block numbers, not filesystem blocks.
118  */
119 
120 /*
121  * Convert dataptr to byte in file space
122  */
123 static inline xfs_dir2_off_t
124 xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
125 {
126 	return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
127 }
128 
129 /*
130  * Convert byte in file space to dataptr.  It had better be aligned.
131  */
132 static inline xfs_dir2_dataptr_t
133 xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
134 {
135 	return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
136 }
137 
138 /*
139  * Convert byte in space to (DB) block
140  */
141 static inline xfs_dir2_db_t
142 xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
143 {
144 	return (xfs_dir2_db_t)(by >> geo->blklog);
145 }
146 
147 /*
148  * Convert dataptr to a block number
149  */
150 static inline xfs_dir2_db_t
151 xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
152 {
153 	return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
154 }
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_da_geometry *geo, xfs_dir2_off_t by)
161 {
162 	return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
163 }
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_da_geometry *geo, xfs_dir2_dataptr_t dp)
170 {
171 	return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
172 }
173 
174 /*
175  * Convert block and offset to byte in space
176  */
177 static inline xfs_dir2_off_t
178 xfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
179 			xfs_dir2_data_aoff_t o)
180 {
181 	return ((xfs_dir2_off_t)db << geo->blklog) + o;
182 }
183 
184 /*
185  * Convert block (DB) to block (dablk)
186  */
187 static inline xfs_dablk_t
188 xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
189 {
190 	return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
191 }
192 
193 /*
194  * Convert byte in space to (DA) block
195  */
196 static inline xfs_dablk_t
197 xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
198 {
199 	return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
200 }
201 
202 /*
203  * Convert block and offset to dataptr
204  */
205 static inline xfs_dir2_dataptr_t
206 xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
207 			   xfs_dir2_data_aoff_t o)
208 {
209 	return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
210 }
211 
212 /*
213  * Convert block (dablk) to block (DB)
214  */
215 static inline xfs_dir2_db_t
216 xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
217 {
218 	return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
219 }
220 
221 /*
222  * Convert block (dablk) to byte offset in space
223  */
224 static inline xfs_dir2_off_t
225 xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
226 {
227 	return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
228 }
229 
230 /*
231  * Directory tail pointer accessor functions. Based on block geometry.
232  */
233 static inline struct xfs_dir2_block_tail *
234 xfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr)
235 {
236 	return ((struct xfs_dir2_block_tail *)
237 		((char *)hdr + geo->blksize)) - 1;
238 }
239 
240 static inline struct xfs_dir2_leaf_tail *
241 xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
242 {
243 	return (struct xfs_dir2_leaf_tail *)
244 		((char *)lp + geo->blksize -
245 		  sizeof(struct xfs_dir2_leaf_tail));
246 }
247 
248 /*
249  * The Linux API doesn't pass down the total size of the buffer
250  * we read into down to the filesystem.  With the filldir concept
251  * it's not needed for correct information, but the XFS dir2 leaf
252  * code wants an estimate of the buffer size to calculate it's
253  * readahead window and size the buffers used for mapping to
254  * physical blocks.
255  *
256  * Try to give it an estimate that's good enough, maybe at some
257  * point we can change the ->readdir prototype to include the
258  * buffer size.  For now we use the current glibc buffer size.
259  * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE.
260  */
261 #define XFS_READDIR_BUFSIZE	(32768)
262 
263 unsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype);
264 unsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo,
265 		struct xfs_dir2_data_hdr *hdr);
266 bool xfs_dir2_namecheck(const void *name, size_t length);
267 
268 /*
269  * The "ascii-ci" feature was created to speed up case-insensitive lookups for
270  * a Samba product.  Because of the inherent problems with CI and UTF-8
271  * encoding, etc, it was decided that Samba would be configured to export
272  * latin1/iso 8859-1 encodings as that covered >90% of the target markets for
273  * the product.  Hence the "ascii-ci" casefolding code could be encoded into
274  * the XFS directory operations and remove all the overhead of casefolding from
275  * Samba.
276  *
277  * To provide consistent hashing behavior between the userspace and kernel,
278  * these functions prepare names for hashing by transforming specific bytes
279  * to other bytes.  Robustness with other encodings is not guaranteed.
280  */
281 static inline bool xfs_ascii_ci_need_xfrm(unsigned char c)
282 {
283 	if (c >= 0x41 && c <= 0x5a)	/* A-Z */
284 		return true;
285 	if (c >= 0xc0 && c <= 0xd6)	/* latin A-O with accents */
286 		return true;
287 	if (c >= 0xd8 && c <= 0xde)	/* latin O-Y with accents */
288 		return true;
289 	return false;
290 }
291 
292 static inline unsigned char xfs_ascii_ci_xfrm(unsigned char c)
293 {
294 	if (xfs_ascii_ci_need_xfrm(c))
295 		c -= 'A' - 'a';
296 	return c;
297 }
298 
299 #endif	/* __XFS_DIR2_H__ */
300