xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_dir2.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #ifndef __XFS_DIR2_H__
7*4882a593Smuzhiyun #define __XFS_DIR2_H__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include "xfs_da_format.h"
10*4882a593Smuzhiyun #include "xfs_da_btree.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct xfs_da_args;
13*4882a593Smuzhiyun struct xfs_inode;
14*4882a593Smuzhiyun struct xfs_mount;
15*4882a593Smuzhiyun struct xfs_trans;
16*4882a593Smuzhiyun struct xfs_dir2_sf_hdr;
17*4882a593Smuzhiyun struct xfs_dir2_sf_entry;
18*4882a593Smuzhiyun struct xfs_dir2_data_hdr;
19*4882a593Smuzhiyun struct xfs_dir2_data_entry;
20*4882a593Smuzhiyun struct xfs_dir2_data_unused;
21*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr;
22*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun extern struct xfs_name	xfs_name_dotdot;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Convert inode mode to directory entry filetype
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun extern unsigned char xfs_mode_to_ftype(int mode);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun  * Generic directory interface routines
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun extern void xfs_dir_startup(void);
35*4882a593Smuzhiyun extern int xfs_da_mount(struct xfs_mount *mp);
36*4882a593Smuzhiyun extern void xfs_da_unmount(struct xfs_mount *mp);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun extern int xfs_dir_isempty(struct xfs_inode *dp);
39*4882a593Smuzhiyun extern int xfs_dir_init(struct xfs_trans *tp, struct xfs_inode *dp,
40*4882a593Smuzhiyun 				struct xfs_inode *pdp);
41*4882a593Smuzhiyun extern int xfs_dir_createname(struct xfs_trans *tp, struct xfs_inode *dp,
42*4882a593Smuzhiyun 				struct xfs_name *name, xfs_ino_t inum,
43*4882a593Smuzhiyun 				xfs_extlen_t tot);
44*4882a593Smuzhiyun extern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp,
45*4882a593Smuzhiyun 				struct xfs_name *name, xfs_ino_t *inum,
46*4882a593Smuzhiyun 				struct xfs_name *ci_name);
47*4882a593Smuzhiyun extern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp,
48*4882a593Smuzhiyun 				struct xfs_name *name, xfs_ino_t ino,
49*4882a593Smuzhiyun 				xfs_extlen_t tot);
50*4882a593Smuzhiyun extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
51*4882a593Smuzhiyun 				struct xfs_name *name, xfs_ino_t inum,
52*4882a593Smuzhiyun 				xfs_extlen_t tot);
53*4882a593Smuzhiyun extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
54*4882a593Smuzhiyun 				struct xfs_name *name);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun  * Direct call from the bmap code, bypassing the generic directory layer.
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun extern int xfs_dir2_sf_to_block(struct xfs_da_args *args);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * Interface routines used by userspace utilities
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun extern int xfs_dir2_isblock(struct xfs_da_args *args, int *r);
65*4882a593Smuzhiyun extern int xfs_dir2_isleaf(struct xfs_da_args *args, int *r);
66*4882a593Smuzhiyun extern int xfs_dir2_shrink_inode(struct xfs_da_args *args, xfs_dir2_db_t db,
67*4882a593Smuzhiyun 				struct xfs_buf *bp);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun extern void xfs_dir2_data_freescan(struct xfs_mount *mp,
70*4882a593Smuzhiyun 		struct xfs_dir2_data_hdr *hdr, int *loghead);
71*4882a593Smuzhiyun extern void xfs_dir2_data_log_entry(struct xfs_da_args *args,
72*4882a593Smuzhiyun 		struct xfs_buf *bp, struct xfs_dir2_data_entry *dep);
73*4882a593Smuzhiyun extern void xfs_dir2_data_log_header(struct xfs_da_args *args,
74*4882a593Smuzhiyun 		struct xfs_buf *bp);
75*4882a593Smuzhiyun extern void xfs_dir2_data_log_unused(struct xfs_da_args *args,
76*4882a593Smuzhiyun 		struct xfs_buf *bp, struct xfs_dir2_data_unused *dup);
77*4882a593Smuzhiyun extern void xfs_dir2_data_make_free(struct xfs_da_args *args,
78*4882a593Smuzhiyun 		struct xfs_buf *bp, xfs_dir2_data_aoff_t offset,
79*4882a593Smuzhiyun 		xfs_dir2_data_aoff_t len, int *needlogp, int *needscanp);
80*4882a593Smuzhiyun extern int xfs_dir2_data_use_free(struct xfs_da_args *args,
81*4882a593Smuzhiyun 		struct xfs_buf *bp, struct xfs_dir2_data_unused *dup,
82*4882a593Smuzhiyun 		xfs_dir2_data_aoff_t offset, xfs_dir2_data_aoff_t len,
83*4882a593Smuzhiyun 		int *needlogp, int *needscanp);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun extern struct xfs_dir2_data_free *xfs_dir2_data_freefind(
86*4882a593Smuzhiyun 		struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
87*4882a593Smuzhiyun 		struct xfs_dir2_data_unused *dup);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun extern const struct xfs_buf_ops xfs_dir3_block_buf_ops;
92*4882a593Smuzhiyun extern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops;
93*4882a593Smuzhiyun extern const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops;
94*4882a593Smuzhiyun extern const struct xfs_buf_ops xfs_dir3_free_buf_ops;
95*4882a593Smuzhiyun extern const struct xfs_buf_ops xfs_dir3_data_buf_ops;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * Directory offset/block conversion functions.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * DB blocks here are logical directory block numbers, not filesystem blocks.
101*4882a593Smuzhiyun  */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun  * Convert dataptr to byte in file space
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun static inline xfs_dir2_off_t
xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)107*4882a593Smuzhiyun xfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun  * Convert byte in file space to dataptr.  It had better be aligned.
114*4882a593Smuzhiyun  */
115*4882a593Smuzhiyun static inline xfs_dir2_dataptr_t
xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)116*4882a593Smuzhiyun xfs_dir2_byte_to_dataptr(xfs_dir2_off_t by)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun  * Convert byte in space to (DB) block
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun static inline xfs_dir2_db_t
xfs_dir2_byte_to_db(struct xfs_da_geometry * geo,xfs_dir2_off_t by)125*4882a593Smuzhiyun xfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	return (xfs_dir2_db_t)(by >> geo->blklog);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * Convert dataptr to a block number
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun static inline xfs_dir2_db_t
xfs_dir2_dataptr_to_db(struct xfs_da_geometry * geo,xfs_dir2_dataptr_t dp)134*4882a593Smuzhiyun xfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp));
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun  * Convert byte in space to offset in a block
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun static inline xfs_dir2_data_aoff_t
xfs_dir2_byte_to_off(struct xfs_da_geometry * geo,xfs_dir2_off_t by)143*4882a593Smuzhiyun xfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1));
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun  * Convert dataptr to a byte offset in a block
150*4882a593Smuzhiyun  */
151*4882a593Smuzhiyun static inline xfs_dir2_data_aoff_t
xfs_dir2_dataptr_to_off(struct xfs_da_geometry * geo,xfs_dir2_dataptr_t dp)152*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp));
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun  * Convert block and offset to byte in space
159*4882a593Smuzhiyun  */
160*4882a593Smuzhiyun static inline xfs_dir2_off_t
xfs_dir2_db_off_to_byte(struct xfs_da_geometry * geo,xfs_dir2_db_t db,xfs_dir2_data_aoff_t o)161*4882a593Smuzhiyun xfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
162*4882a593Smuzhiyun 			xfs_dir2_data_aoff_t o)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	return ((xfs_dir2_off_t)db << geo->blklog) + o;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun  * Convert block (DB) to block (dablk)
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun static inline xfs_dablk_t
xfs_dir2_db_to_da(struct xfs_da_geometry * geo,xfs_dir2_db_t db)171*4882a593Smuzhiyun xfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog));
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  * Convert byte in space to (DA) block
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun static inline xfs_dablk_t
xfs_dir2_byte_to_da(struct xfs_da_geometry * geo,xfs_dir2_off_t by)180*4882a593Smuzhiyun xfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by));
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun  * Convert block and offset to dataptr
187*4882a593Smuzhiyun  */
188*4882a593Smuzhiyun static inline xfs_dir2_dataptr_t
xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry * geo,xfs_dir2_db_t db,xfs_dir2_data_aoff_t o)189*4882a593Smuzhiyun xfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db,
190*4882a593Smuzhiyun 			   xfs_dir2_data_aoff_t o)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o));
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun  * Convert block (dablk) to block (DB)
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun static inline xfs_dir2_db_t
xfs_dir2_da_to_db(struct xfs_da_geometry * geo,xfs_dablk_t da)199*4882a593Smuzhiyun xfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog));
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun  * Convert block (dablk) to byte offset in space
206*4882a593Smuzhiyun  */
207*4882a593Smuzhiyun static inline xfs_dir2_off_t
xfs_dir2_da_to_byte(struct xfs_da_geometry * geo,xfs_dablk_t da)208*4882a593Smuzhiyun xfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun  * Directory tail pointer accessor functions. Based on block geometry.
215*4882a593Smuzhiyun  */
216*4882a593Smuzhiyun static inline struct xfs_dir2_block_tail *
xfs_dir2_block_tail_p(struct xfs_da_geometry * geo,struct xfs_dir2_data_hdr * hdr)217*4882a593Smuzhiyun xfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	return ((struct xfs_dir2_block_tail *)
220*4882a593Smuzhiyun 		((char *)hdr + geo->blksize)) - 1;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun static inline struct xfs_dir2_leaf_tail *
xfs_dir2_leaf_tail_p(struct xfs_da_geometry * geo,struct xfs_dir2_leaf * lp)224*4882a593Smuzhiyun xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	return (struct xfs_dir2_leaf_tail *)
227*4882a593Smuzhiyun 		((char *)lp + geo->blksize -
228*4882a593Smuzhiyun 		  sizeof(struct xfs_dir2_leaf_tail));
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun  * The Linux API doesn't pass down the total size of the buffer
233*4882a593Smuzhiyun  * we read into down to the filesystem.  With the filldir concept
234*4882a593Smuzhiyun  * it's not needed for correct information, but the XFS dir2 leaf
235*4882a593Smuzhiyun  * code wants an estimate of the buffer size to calculate it's
236*4882a593Smuzhiyun  * readahead window and size the buffers used for mapping to
237*4882a593Smuzhiyun  * physical blocks.
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  * Try to give it an estimate that's good enough, maybe at some
240*4882a593Smuzhiyun  * point we can change the ->readdir prototype to include the
241*4882a593Smuzhiyun  * buffer size.  For now we use the current glibc buffer size.
242*4882a593Smuzhiyun  * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE.
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun #define XFS_READDIR_BUFSIZE	(32768)
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun unsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype);
247*4882a593Smuzhiyun unsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo,
248*4882a593Smuzhiyun 		struct xfs_dir2_data_hdr *hdr);
249*4882a593Smuzhiyun bool xfs_dir2_namecheck(const void *name, size_t length);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun #endif	/* __XFS_DIR2_H__ */
252