1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #ifndef __XFS_DIR2_LEAF_H__
33 #define	__XFS_DIR2_LEAF_H__
34 
35 /*
36  * Directory version 2, leaf block structures.
37  */
38 
39 struct uio;
40 struct xfs_dabuf;
41 struct xfs_da_args;
42 struct xfs_inode;
43 struct xfs_mount;
44 struct xfs_trans;
45 
46 /*
47  * Constants.
48  */
49 
50 /*
51  * Offset of the leaf/node space.  First block in this space
52  * is the btree root.
53  */
54 #define	XFS_DIR2_LEAF_SPACE	1
55 #define	XFS_DIR2_LEAF_OFFSET	(XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
56 #define	XFS_DIR2_LEAF_FIRSTDB(mp)	\
57 	XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_LEAF_OFFSET)
58 
59 /*
60  * Types.
61  */
62 
63 /*
64  * Offset in data space of a data entry.
65  */
66 typedef	__uint32_t	xfs_dir2_dataptr_t;
67 #define	XFS_DIR2_MAX_DATAPTR	((xfs_dir2_dataptr_t)0xffffffff)
68 #define	XFS_DIR2_NULL_DATAPTR	((xfs_dir2_dataptr_t)0)
69 
70 /*
71  * Structures.
72  */
73 
74 /*
75  * Leaf block header.
76  */
77 typedef struct xfs_dir2_leaf_hdr {
78 	xfs_da_blkinfo_t	info;		/* header for da routines */
79 	__uint16_t		count;		/* count of entries */
80 	__uint16_t		stale;		/* count of stale entries */
81 } xfs_dir2_leaf_hdr_t;
82 
83 /*
84  * Leaf block entry.
85  */
86 typedef struct xfs_dir2_leaf_entry {
87 	xfs_dahash_t		hashval;	/* hash value of name */
88 	xfs_dir2_dataptr_t	address;	/* address of data entry */
89 } xfs_dir2_leaf_entry_t;
90 
91 /*
92  * Leaf block tail.
93  */
94 typedef struct xfs_dir2_leaf_tail {
95 	__uint32_t		bestcount;
96 } xfs_dir2_leaf_tail_t;
97 
98 /*
99  * Leaf block.
100  * bests and tail are at the end of the block for single-leaf only
101  * (magic = XFS_DIR2_LEAF1_MAGIC not XFS_DIR2_LEAFN_MAGIC).
102  */
103 typedef struct xfs_dir2_leaf {
104 	xfs_dir2_leaf_hdr_t	hdr;		/* leaf header */
105 	xfs_dir2_leaf_entry_t	ents[1];	/* entries */
106 						/* ... */
107 	xfs_dir2_data_off_t	bests[1];	/* best free counts */
108 	xfs_dir2_leaf_tail_t	tail;		/* leaf tail */
109 } xfs_dir2_leaf_t;
110 
111 /*
112  * Macros.
113  * The DB blocks are logical directory block numbers, not filesystem blocks.
114  */
115 
116 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_MAX_LEAF_ENTS)
117 int
118 xfs_dir2_max_leaf_ents(struct xfs_mount *mp);
119 #define	XFS_DIR2_MAX_LEAF_ENTS(mp)	\
120 	xfs_dir2_max_leaf_ents(mp)
121 #else
122 #define	XFS_DIR2_MAX_LEAF_ENTS(mp)	\
123 	((int)(((mp)->m_dirblksize - (uint)sizeof(xfs_dir2_leaf_hdr_t)) / \
124 	       (uint)sizeof(xfs_dir2_leaf_entry_t)))
125 #endif
126 
127 /*
128  * Get address of the bestcount field in the single-leaf block.
129  */
130 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_LEAF_TAIL_P)
131 xfs_dir2_leaf_tail_t *
132 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, xfs_dir2_leaf_t *lp);
133 #define	XFS_DIR2_LEAF_TAIL_P(mp,lp)	\
134 	xfs_dir2_leaf_tail_p(mp, lp)
135 #else
136 #define	XFS_DIR2_LEAF_TAIL_P(mp,lp)	\
137 	((xfs_dir2_leaf_tail_t *)\
138 	 ((char *)(lp) + (mp)->m_dirblksize - \
139 	  (uint)sizeof(xfs_dir2_leaf_tail_t)))
140 #endif
141 
142 /*
143  * Get address of the bests array in the single-leaf block.
144  */
145 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_LEAF_BESTS_P)
146 xfs_dir2_data_off_t *
147 xfs_dir2_leaf_bests_p_arch(xfs_dir2_leaf_tail_t *ltp, xfs_arch_t arch);
148 #define	XFS_DIR2_LEAF_BESTS_P_ARCH(ltp,arch)	xfs_dir2_leaf_bests_p_arch(ltp,arch)
149 #else
150 #define	XFS_DIR2_LEAF_BESTS_P_ARCH(ltp,arch)	\
151 	((xfs_dir2_data_off_t *)(ltp) - INT_GET((ltp)->bestcount, arch))
152 #endif
153 
154 /*
155  * Convert dataptr to byte in file space
156  */
157 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DATAPTR_TO_BYTE)
158 xfs_dir2_off_t
159 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp);
160 #define	XFS_DIR2_DATAPTR_TO_BYTE(mp,dp)	xfs_dir2_dataptr_to_byte(mp, dp)
161 #else
162 #define	XFS_DIR2_DATAPTR_TO_BYTE(mp,dp)	\
163 	((xfs_dir2_off_t)(dp) << XFS_DIR2_DATA_ALIGN_LOG)
164 #endif
165 
166 /*
167  * Convert byte in file space to dataptr.  It had better be aligned.
168  */
169 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_BYTE_TO_DATAPTR)
170 xfs_dir2_dataptr_t
171 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by);
172 #define	XFS_DIR2_BYTE_TO_DATAPTR(mp,by)	xfs_dir2_byte_to_dataptr(mp,by)
173 #else
174 #define	XFS_DIR2_BYTE_TO_DATAPTR(mp,by)	\
175 	((xfs_dir2_dataptr_t)((by) >> XFS_DIR2_DATA_ALIGN_LOG))
176 #endif
177 
178 /*
179  * Convert dataptr to a block number
180  */
181 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DATAPTR_TO_DB)
182 xfs_dir2_db_t
183 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp);
184 #define	XFS_DIR2_DATAPTR_TO_DB(mp,dp)	xfs_dir2_dataptr_to_db(mp, dp)
185 #else
186 #define	XFS_DIR2_DATAPTR_TO_DB(mp,dp)	\
187 	XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp))
188 #endif
189 
190 /*
191  * Convert dataptr to a byte offset in a block
192  */
193 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DATAPTR_TO_OFF)
194 xfs_dir2_data_aoff_t
195 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp);
196 #define	XFS_DIR2_DATAPTR_TO_OFF(mp,dp)	xfs_dir2_dataptr_to_off(mp, dp)
197 #else
198 #define	XFS_DIR2_DATAPTR_TO_OFF(mp,dp)	\
199 	XFS_DIR2_BYTE_TO_OFF(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp))
200 #endif
201 
202 /*
203  * Convert block and offset to byte in space
204  */
205 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DB_OFF_TO_BYTE)
206 xfs_dir2_off_t
207 xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
208 			xfs_dir2_data_aoff_t o);
209 #define	XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o)	\
210 	xfs_dir2_db_off_to_byte(mp, db, o)
211 #else
212 #define	XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o)	\
213 	(((xfs_dir2_off_t)(db) << \
214 	 ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) + (o))
215 #endif
216 
217 /*
218  * Convert byte in space to (DB) block
219  */
220 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_BYTE_TO_DB)
221 xfs_dir2_db_t xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by);
222 #define	XFS_DIR2_BYTE_TO_DB(mp,by)	xfs_dir2_byte_to_db(mp, by)
223 #else
224 #define	XFS_DIR2_BYTE_TO_DB(mp,by)	\
225 	((xfs_dir2_db_t)((by) >> \
226 			 ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)))
227 #endif
228 
229 /*
230  * Convert byte in space to (DA) block
231  */
232 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_BYTE_TO_DA)
233 xfs_dablk_t xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by);
234 #define	XFS_DIR2_BYTE_TO_DA(mp,by)	xfs_dir2_byte_to_da(mp, by)
235 #else
236 #define	XFS_DIR2_BYTE_TO_DA(mp,by)	\
237 	XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, by))
238 #endif
239 
240 /*
241  * Convert byte in space to offset in a block
242  */
243 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_BYTE_TO_OFF)
244 xfs_dir2_data_aoff_t
245 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by);
246 #define	XFS_DIR2_BYTE_TO_OFF(mp,by)	xfs_dir2_byte_to_off(mp, by)
247 #else
248 #define	XFS_DIR2_BYTE_TO_OFF(mp,by)	\
249 	((xfs_dir2_data_aoff_t)((by) & \
250 				((1 << ((mp)->m_sb.sb_blocklog + \
251 					(mp)->m_sb.sb_dirblklog)) - 1)))
252 #endif
253 
254 /*
255  * Convert block and offset to dataptr
256  */
257 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DB_OFF_TO_DATAPTR)
258 xfs_dir2_dataptr_t
259 xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
260 			   xfs_dir2_data_aoff_t o);
261 #define	XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o)	\
262 	xfs_dir2_db_off_to_dataptr(mp, db, o)
263 #else
264 #define	XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o)	\
265 	XFS_DIR2_BYTE_TO_DATAPTR(mp, XFS_DIR2_DB_OFF_TO_BYTE(mp, db, o))
266 #endif
267 
268 /*
269  * Convert block (DB) to block (dablk)
270  */
271 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DB_TO_DA)
272 xfs_dablk_t xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db);
273 #define	XFS_DIR2_DB_TO_DA(mp,db)	xfs_dir2_db_to_da(mp, db)
274 #else
275 #define	XFS_DIR2_DB_TO_DA(mp,db)	\
276 	((xfs_dablk_t)((db) << (mp)->m_sb.sb_dirblklog))
277 #endif
278 
279 /*
280  * Convert block (dablk) to block (DB)
281  */
282 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DA_TO_DB)
283 xfs_dir2_db_t xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da);
284 #define	XFS_DIR2_DA_TO_DB(mp,da)	xfs_dir2_da_to_db(mp, da)
285 #else
286 #define	XFS_DIR2_DA_TO_DB(mp,da)	\
287 	((xfs_dir2_db_t)((da) >> (mp)->m_sb.sb_dirblklog))
288 #endif
289 
290 /*
291  * Convert block (dablk) to byte offset in space
292  */
293 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR2_DA_TO_BYTE)
294 xfs_dir2_off_t xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da);
295 #define XFS_DIR2_DA_TO_BYTE(mp,da)	xfs_dir2_da_to_byte(mp, da)
296 #else
297 #define	XFS_DIR2_DA_TO_BYTE(mp,da)	\
298 	XFS_DIR2_DB_OFF_TO_BYTE(mp, XFS_DIR2_DA_TO_DB(mp, da), 0)
299 #endif
300 
301 /*
302  * Function declarations.
303  */
304 
305 extern int
306 	xfs_dir2_block_to_leaf(struct xfs_da_args *args, struct xfs_dabuf *dbp);
307 
308 extern int
309 	xfs_dir2_leaf_addname(struct xfs_da_args *args);
310 
311 extern void
312 	xfs_dir2_leaf_compact(struct xfs_da_args *args, struct xfs_dabuf *bp);
313 
314 extern void
315 	xfs_dir2_leaf_compact_x1(struct xfs_dabuf *bp, int *indexp,
316 				 int *lowstalep, int *highstalep, int *lowlogp,
317 				 int *highlogp);
318 
319 extern int
320 	xfs_dir2_leaf_getdents(struct xfs_trans *tp, struct xfs_inode *dp,
321 			       struct uio *uio, int *eofp, struct xfs_dirent *dbp,
322 			       xfs_dir2_put_t put);
323 
324 extern int
325 	xfs_dir2_leaf_init(struct xfs_da_args *args, xfs_dir2_db_t bno,
326 			   struct xfs_dabuf **bpp, int magic);
327 
328 extern void
329 	xfs_dir2_leaf_log_ents(struct xfs_trans *tp, struct xfs_dabuf *bp,
330 			       int first, int last);
331 
332 extern void
333 	xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
334 				int first, int last);
335 
336 extern void
337 	xfs_dir2_leaf_log_header(struct xfs_trans *tp, struct xfs_dabuf *bp);
338 
339 extern void
340 	xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
341 
342 extern int
343 	xfs_dir2_leaf_lookup(struct xfs_da_args *args);
344 
345 extern int
346 	xfs_dir2_leaf_removename(struct xfs_da_args *args);
347 
348 extern int
349 	xfs_dir2_leaf_replace(struct xfs_da_args *args);
350 
351 extern int
352 	xfs_dir2_leaf_search_hash(struct xfs_da_args *args,
353 				  struct xfs_dabuf *lbp);
354 extern int
355 	xfs_dir2_leaf_trim_data(struct xfs_da_args *args, struct xfs_dabuf *lbp,				xfs_dir2_db_t db);
356 
357 extern int
358 	xfs_dir2_node_to_leaf(struct xfs_da_state *state);
359 
360 #endif	/* __XFS_DIR2_LEAF_H__ */
361