GNU Linux-libre 6.9-gnu
[releases.git] / fs / xfs / libxfs / xfs_dir2.h
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 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 /*
111  * Directory offset/block conversion functions.
112  *
113  * DB blocks here are logical directory block numbers, not filesystem blocks.
114  */
115
116 /*
117  * Convert dataptr to byte in file space
118  */
119 static inline xfs_dir2_off_t
120 xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
121 {
122         return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
123 }
124
125 /*
126  * Convert byte in file space to dataptr.  It had better be aligned.
127  */
128 static inline xfs_dir2_dataptr_t
129 xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
130 {
131         return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
132 }
133
134 /*
135  * Convert byte in space to (DB) block
136  */
137 static inline xfs_dir2_db_t
138 xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
139 {
140         return (xfs_dir2_db_t)(by >> geo->blklog);
141 }
142
143 /*
144  * Convert dataptr to a block number
145  */
146 static inline xfs_dir2_db_t
147 xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
148 {
149         return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
150 }
151
152 /*
153  * Convert byte in space to offset in a block
154  */
155 static inline xfs_dir2_data_aoff_t
156 xfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
157 {
158         return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
159 }
160
161 /*
162  * Convert dataptr to a byte offset in a block
163  */
164 static inline xfs_dir2_data_aoff_t
165 xfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
166 {
167         return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
168 }
169
170 /*
171  * Convert block and offset to byte in space
172  */
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)
176 {
177         return ((xfs_dir2_off_t)db << geo->blklog) + o;
178 }
179
180 /*
181  * Convert block (DB) to block (dablk)
182  */
183 static inline xfs_dablk_t
184 xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
185 {
186         return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
187 }
188
189 /*
190  * Convert byte in space to (DA) block
191  */
192 static inline xfs_dablk_t
193 xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
194 {
195         return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
196 }
197
198 /*
199  * Convert block and offset to dataptr
200  */
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)
204 {
205         return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
206 }
207
208 /*
209  * Convert block (dablk) to block (DB)
210  */
211 static inline xfs_dir2_db_t
212 xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
213 {
214         return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
215 }
216
217 /*
218  * Convert block (dablk) to byte offset in space
219  */
220 static inline xfs_dir2_off_t
221 xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
222 {
223         return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
224 }
225
226 /*
227  * Directory tail pointer accessor functions. Based on block geometry.
228  */
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)
231 {
232         return ((struct xfs_dir2_block_tail *)
233                 ((char *)hdr + geo->blksize)) - 1;
234 }
235
236 static inline struct xfs_dir2_leaf_tail *
237 xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
238 {
239         return (struct xfs_dir2_leaf_tail *)
240                 ((char *)lp + geo->blksize -
241                   sizeof(struct xfs_dir2_leaf_tail));
242 }
243
244 /*
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
250  * physical blocks.
251  *
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.
256  */
257 #define XFS_READDIR_BUFSIZE     (32768)
258
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);
263
264 /*
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
271  * Samba.
272  *
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.
276  */
277 static inline bool xfs_ascii_ci_need_xfrm(unsigned char c)
278 {
279         if (c >= 0x41 && c <= 0x5a)     /* A-Z */
280                 return true;
281         if (c >= 0xc0 && c <= 0xd6)     /* latin A-O with accents */
282                 return true;
283         if (c >= 0xd8 && c <= 0xde)     /* latin O-Y with accents */
284                 return true;
285         return false;
286 }
287
288 static inline unsigned char xfs_ascii_ci_xfrm(unsigned char c)
289 {
290         if (xfs_ascii_ci_need_xfrm(c))
291                 c -= 'A' - 'a';
292         return c;
293 }
294
295 #endif  /* __XFS_DIR2_H__ */