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 873856429 : xfs_dir2_samename(
29 : const struct xfs_name *n1,
30 : const struct xfs_name *n2)
31 : {
32 1001708488 : return n1 == n2 || (n1->len == n2->len &&
33 127852059 : !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 : 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 5175565394 : 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 74849785109 : 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 3579395699 : xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
140 : {
141 3579395699 : 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 2811186856 : 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 2632417103 : 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 2474819598 : 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 30451737961 : 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 30451737961 : 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 1265114951 : xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
186 : {
187 1265114951 : 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 290055937 : xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
195 : {
196 290055937 : 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 30198430324 : 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 374773539 : xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
214 : {
215 374773539 : 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 257479087 : xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
223 : {
224 257479087 : 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 347583977 : return ((struct xfs_dir2_block_tail *)
234 347583977 : ((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 452843720 : return (struct xfs_dir2_leaf_tail *)
241 452843720 : ((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 330282729 : if (c >= 0x41 && c <= 0x5a) /* A-Z */
281 : return true;
282 318132115 : if (c >= 0xc0 && c <= 0xd6) /* latin A-O with accents */
283 : return true;
284 318077650 : if (c >= 0xd8 && c <= 0xde) /* latin O-Y with accents */
285 : return true;
286 : return false;
287 : }
288 :
289 330282729 : static inline unsigned char xfs_ascii_ci_xfrm(unsigned char c)
290 : {
291 330282729 : if (xfs_ascii_ci_need_xfrm(c))
292 12222673 : c -= 'A' - 'a';
293 330282729 : return c;
294 : }
295 :
296 : #endif /* __XFS_DIR2_H__ */
|