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