1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #ifndef __XFS_DIR2_LEAF_H__
19 #define	__XFS_DIR2_LEAF_H__
20 
21 struct uio;
22 struct xfs_dabuf;
23 struct xfs_da_args;
24 struct xfs_inode;
25 struct xfs_mount;
26 struct xfs_trans;
27 
28 /*
29  * Offset of the leaf/node space.  First block in this space
30  * is the btree root.
31  */
32 #define	XFS_DIR2_LEAF_SPACE	1
33 #define	XFS_DIR2_LEAF_OFFSET	(XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
34 #define	XFS_DIR2_LEAF_FIRSTDB(mp)	\
35 	XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_LEAF_OFFSET)
36 
37 /*
38  * Offset in data space of a data entry.
39  */
40 typedef	__uint32_t	xfs_dir2_dataptr_t;
41 #define	XFS_DIR2_MAX_DATAPTR	((xfs_dir2_dataptr_t)0xffffffff)
42 #define	XFS_DIR2_NULL_DATAPTR	((xfs_dir2_dataptr_t)0)
43 
44 /*
45  * Leaf block header.
46  */
47 typedef struct xfs_dir2_leaf_hdr {
48 	xfs_da_blkinfo_t	info;		/* header for da routines */
49 	__be16			count;		/* count of entries */
50 	__be16			stale;		/* count of stale entries */
51 } xfs_dir2_leaf_hdr_t;
52 
53 /*
54  * Leaf block entry.
55  */
56 typedef struct xfs_dir2_leaf_entry {
57 	__be32			hashval;	/* hash value of name */
58 	__be32			address;	/* address of data entry */
59 } xfs_dir2_leaf_entry_t;
60 
61 /*
62  * Leaf block tail.
63  */
64 typedef struct xfs_dir2_leaf_tail {
65 	__be32			bestcount;
66 } xfs_dir2_leaf_tail_t;
67 
68 /*
69  * Leaf block.
70  * bests and tail are at the end of the block for single-leaf only
71  * (magic = XFS_DIR2_LEAF1_MAGIC not XFS_DIR2_LEAFN_MAGIC).
72  */
73 typedef struct xfs_dir2_leaf {
74 	xfs_dir2_leaf_hdr_t	hdr;		/* leaf header */
75 	xfs_dir2_leaf_entry_t	ents[1];	/* entries */
76 						/* ... */
77 	xfs_dir2_data_off_t	bests[1];	/* best free counts */
78 	xfs_dir2_leaf_tail_t	tail;		/* leaf tail */
79 } xfs_dir2_leaf_t;
80 
81 /*
82  * DB blocks here are logical directory block numbers, not filesystem blocks.
83  */
84 
85 #define	XFS_DIR2_MAX_LEAF_ENTS(mp)	xfs_dir2_max_leaf_ents(mp)
xfs_dir2_max_leaf_ents(struct xfs_mount * mp)86 static inline int xfs_dir2_max_leaf_ents(struct xfs_mount *mp)
87 {
88 	return (int)(((mp)->m_dirblksize - (uint)sizeof(xfs_dir2_leaf_hdr_t)) /
89 	       (uint)sizeof(xfs_dir2_leaf_entry_t));
90 }
91 
92 /*
93  * Get address of the bestcount field in the single-leaf block.
94  */
95 #define	XFS_DIR2_LEAF_TAIL_P(mp,lp)	xfs_dir2_leaf_tail_p(mp, lp)
96 static inline xfs_dir2_leaf_tail_t *
xfs_dir2_leaf_tail_p(struct xfs_mount * mp,xfs_dir2_leaf_t * lp)97 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, xfs_dir2_leaf_t *lp)
98 {
99 	return (xfs_dir2_leaf_tail_t *)
100 		((char *)(lp) + (mp)->m_dirblksize -
101 		  (uint)sizeof(xfs_dir2_leaf_tail_t));
102 }
103 
104 /*
105  * Get address of the bests array in the single-leaf block.
106  */
107 #define	XFS_DIR2_LEAF_BESTS_P(ltp)	xfs_dir2_leaf_bests_p(ltp)
108 static inline __be16 *
xfs_dir2_leaf_bests_p(xfs_dir2_leaf_tail_t * ltp)109 xfs_dir2_leaf_bests_p(xfs_dir2_leaf_tail_t *ltp)
110 {
111 	return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
112 }
113 
114 /*
115  * Convert dataptr to byte in file space
116  */
117 #define	XFS_DIR2_DATAPTR_TO_BYTE(mp,dp)	xfs_dir2_dataptr_to_byte(mp, dp)
118 static inline xfs_dir2_off_t
xfs_dir2_dataptr_to_byte(struct xfs_mount * mp,xfs_dir2_dataptr_t dp)119 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
120 {
121 	return (xfs_dir2_off_t)(dp) << XFS_DIR2_DATA_ALIGN_LOG;
122 }
123 
124 /*
125  * Convert byte in file space to dataptr.  It had better be aligned.
126  */
127 #define	XFS_DIR2_BYTE_TO_DATAPTR(mp,by)	xfs_dir2_byte_to_dataptr(mp,by)
128 static inline xfs_dir2_dataptr_t
xfs_dir2_byte_to_dataptr(struct xfs_mount * mp,xfs_dir2_off_t by)129 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, 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 #define	XFS_DIR2_BYTE_TO_DB(mp,by)	xfs_dir2_byte_to_db(mp, by)
138 static inline xfs_dir2_db_t
xfs_dir2_byte_to_db(struct xfs_mount * mp,xfs_dir2_off_t by)139 xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
140 {
141 	return (xfs_dir2_db_t)((by) >> \
142 		 ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog));
143 }
144 
145 /*
146  * Convert dataptr to a block number
147  */
148 #define	XFS_DIR2_DATAPTR_TO_DB(mp,dp)	xfs_dir2_dataptr_to_db(mp, dp)
149 static inline xfs_dir2_db_t
xfs_dir2_dataptr_to_db(struct xfs_mount * mp,xfs_dir2_dataptr_t dp)150 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
151 {
152 	return XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
153 }
154 
155 /*
156  * Convert byte in space to offset in a block
157  */
158 #define	XFS_DIR2_BYTE_TO_OFF(mp,by)	xfs_dir2_byte_to_off(mp, by)
159 static inline xfs_dir2_data_aoff_t
xfs_dir2_byte_to_off(struct xfs_mount * mp,xfs_dir2_off_t by)160 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
161 {
162 	return (xfs_dir2_data_aoff_t)((by) & \
163 		((1 << ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) - 1));
164 }
165 
166 /*
167  * Convert dataptr to a byte offset in a block
168  */
169 #define	XFS_DIR2_DATAPTR_TO_OFF(mp,dp)	xfs_dir2_dataptr_to_off(mp, dp)
170 static inline xfs_dir2_data_aoff_t
xfs_dir2_dataptr_to_off(struct xfs_mount * mp,xfs_dir2_dataptr_t dp)171 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
172 {
173 	return XFS_DIR2_BYTE_TO_OFF(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
174 }
175 
176 /*
177  * Convert block and offset to byte in space
178  */
179 #define	XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o)	\
180 	xfs_dir2_db_off_to_byte(mp, db, o)
181 static inline xfs_dir2_off_t
xfs_dir2_db_off_to_byte(struct xfs_mount * mp,xfs_dir2_db_t db,xfs_dir2_data_aoff_t o)182 xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
183 			xfs_dir2_data_aoff_t o)
184 {
185 	return ((xfs_dir2_off_t)(db) << \
186 		((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) + (o);
187 }
188 
189 /*
190  * Convert block (DB) to block (dablk)
191  */
192 #define	XFS_DIR2_DB_TO_DA(mp,db)	xfs_dir2_db_to_da(mp, db)
193 static inline xfs_dablk_t
xfs_dir2_db_to_da(struct xfs_mount * mp,xfs_dir2_db_t db)194 xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
195 {
196 	return (xfs_dablk_t)((db) << (mp)->m_sb.sb_dirblklog);
197 }
198 
199 /*
200  * Convert byte in space to (DA) block
201  */
202 #define	XFS_DIR2_BYTE_TO_DA(mp,by)	xfs_dir2_byte_to_da(mp, by)
203 static inline xfs_dablk_t
xfs_dir2_byte_to_da(struct xfs_mount * mp,xfs_dir2_off_t by)204 xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
205 {
206 	return XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, by));
207 }
208 
209 /*
210  * Convert block and offset to dataptr
211  */
212 #define	XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o)	\
213 	xfs_dir2_db_off_to_dataptr(mp, db, o)
214 static inline xfs_dir2_dataptr_t
xfs_dir2_db_off_to_dataptr(struct xfs_mount * mp,xfs_dir2_db_t db,xfs_dir2_data_aoff_t o)215 xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
216 			   xfs_dir2_data_aoff_t o)
217 {
218 	return XFS_DIR2_BYTE_TO_DATAPTR(mp, XFS_DIR2_DB_OFF_TO_BYTE(mp, db, o));
219 }
220 
221 /*
222  * Convert block (dablk) to block (DB)
223  */
224 #define	XFS_DIR2_DA_TO_DB(mp,da)	xfs_dir2_da_to_db(mp, da)
225 static inline xfs_dir2_db_t
xfs_dir2_da_to_db(struct xfs_mount * mp,xfs_dablk_t da)226 xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
227 {
228 	return (xfs_dir2_db_t)((da) >> (mp)->m_sb.sb_dirblklog);
229 }
230 
231 /*
232  * Convert block (dablk) to byte offset in space
233  */
234 #define XFS_DIR2_DA_TO_BYTE(mp,da)	xfs_dir2_da_to_byte(mp, da)
235 static inline xfs_dir2_off_t
xfs_dir2_da_to_byte(struct xfs_mount * mp,xfs_dablk_t da)236 xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
237 {
238 	return XFS_DIR2_DB_OFF_TO_BYTE(mp, XFS_DIR2_DA_TO_DB(mp, da), 0);
239 }
240 
241 /*
242  * Function declarations.
243  */
244 extern int xfs_dir2_block_to_leaf(struct xfs_da_args *args,
245 				  struct xfs_dabuf *dbp);
246 extern int xfs_dir2_leaf_addname(struct xfs_da_args *args);
247 extern void xfs_dir2_leaf_compact(struct xfs_da_args *args,
248 				  struct xfs_dabuf *bp);
249 extern void xfs_dir2_leaf_compact_x1(struct xfs_dabuf *bp, int *indexp,
250 				     int *lowstalep, int *highstalep,
251 				     int *lowlogp, int *highlogp);
252 extern int xfs_dir2_leaf_getdents(struct xfs_trans *tp, struct xfs_inode *dp,
253 				  struct uio *uio, int *eofp,
254 				  struct xfs_dirent *dbp, xfs_dir2_put_t put);
255 extern int xfs_dir2_leaf_init(struct xfs_da_args *args, xfs_dir2_db_t bno,
256 			      struct xfs_dabuf **bpp, int magic);
257 extern void xfs_dir2_leaf_log_ents(struct xfs_trans *tp, struct xfs_dabuf *bp,
258 				   int first, int last);
259 extern void xfs_dir2_leaf_log_header(struct xfs_trans *tp,
260 				     struct xfs_dabuf *bp);
261 extern int xfs_dir2_leaf_lookup(struct xfs_da_args *args);
262 extern int xfs_dir2_leaf_removename(struct xfs_da_args *args);
263 extern int xfs_dir2_leaf_replace(struct xfs_da_args *args);
264 extern int xfs_dir2_leaf_search_hash(struct xfs_da_args *args,
265 				     struct xfs_dabuf *lbp);
266 extern int xfs_dir2_leaf_trim_data(struct xfs_da_args *args,
267 				   struct xfs_dabuf *lbp, xfs_dir2_db_t db);
268 extern int xfs_dir2_node_to_leaf(struct xfs_da_state *state);
269 
270 #endif	/* __XFS_DIR2_LEAF_H__ */
271