blob: 43f0ec2131a910efa0466a78571fbc18e80c8878 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
39struct uio;
40struct xfs_dabuf;
41struct xfs_da_args;
42struct xfs_inode;
43struct xfs_mount;
44struct xfs_trans;
45
46/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * Offset of the leaf/node space. First block in this space
48 * is the btree root.
49 */
50#define XFS_DIR2_LEAF_SPACE 1
51#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
52#define XFS_DIR2_LEAF_FIRSTDB(mp) \
53 XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_LEAF_OFFSET)
54
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Offset in data space of a data entry.
57 */
58typedef __uint32_t xfs_dir2_dataptr_t;
59#define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
60#define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * Leaf block header.
64 */
65typedef struct xfs_dir2_leaf_hdr {
66 xfs_da_blkinfo_t info; /* header for da routines */
67 __uint16_t count; /* count of entries */
68 __uint16_t stale; /* count of stale entries */
69} xfs_dir2_leaf_hdr_t;
70
71/*
72 * Leaf block entry.
73 */
74typedef struct xfs_dir2_leaf_entry {
75 xfs_dahash_t hashval; /* hash value of name */
76 xfs_dir2_dataptr_t address; /* address of data entry */
77} xfs_dir2_leaf_entry_t;
78
79/*
80 * Leaf block tail.
81 */
82typedef struct xfs_dir2_leaf_tail {
83 __uint32_t bestcount;
84} xfs_dir2_leaf_tail_t;
85
86/*
87 * Leaf block.
88 * bests and tail are at the end of the block for single-leaf only
89 * (magic = XFS_DIR2_LEAF1_MAGIC not XFS_DIR2_LEAFN_MAGIC).
90 */
91typedef struct xfs_dir2_leaf {
92 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
93 xfs_dir2_leaf_entry_t ents[1]; /* entries */
94 /* ... */
95 xfs_dir2_data_off_t bests[1]; /* best free counts */
96 xfs_dir2_leaf_tail_t tail; /* leaf tail */
97} xfs_dir2_leaf_t;
98
99/*
Nathan Scotta844f452005-11-02 14:38:42 +1100100 * DB blocks here are logical directory block numbers, not filesystem blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
Nathan Scotta844f452005-11-02 14:38:42 +1100103#define XFS_DIR2_MAX_LEAF_ENTS(mp) xfs_dir2_max_leaf_ents(mp)
104static inline int xfs_dir2_max_leaf_ents(struct xfs_mount *mp)
105{
106 return (int)(((mp)->m_dirblksize - (uint)sizeof(xfs_dir2_leaf_hdr_t)) /
107 (uint)sizeof(xfs_dir2_leaf_entry_t));
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
111 * Get address of the bestcount field in the single-leaf block.
112 */
Nathan Scotta844f452005-11-02 14:38:42 +1100113#define XFS_DIR2_LEAF_TAIL_P(mp,lp) xfs_dir2_leaf_tail_p(mp, lp)
114static inline xfs_dir2_leaf_tail_t *
115xfs_dir2_leaf_tail_p(struct xfs_mount *mp, xfs_dir2_leaf_t *lp)
116{
117 return (xfs_dir2_leaf_tail_t *)
118 ((char *)(lp) + (mp)->m_dirblksize -
119 (uint)sizeof(xfs_dir2_leaf_tail_t));
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * Get address of the bests array in the single-leaf block.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define XFS_DIR2_LEAF_BESTS_P(ltp) xfs_dir2_leaf_bests_p(ltp)
Nathan Scotta844f452005-11-02 14:38:42 +1100126static inline xfs_dir2_data_off_t *
127xfs_dir2_leaf_bests_p(xfs_dir2_leaf_tail_t *ltp)
128{
129 return (xfs_dir2_data_off_t *)
130 (ltp) - INT_GET((ltp)->bestcount, ARCH_CONVERT);
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * Convert dataptr to byte in file space
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define XFS_DIR2_DATAPTR_TO_BYTE(mp,dp) xfs_dir2_dataptr_to_byte(mp, dp)
Nathan Scotta844f452005-11-02 14:38:42 +1100137static inline xfs_dir2_off_t
138xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
139{
140 return (xfs_dir2_off_t)(dp) << XFS_DIR2_DATA_ALIGN_LOG;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * Convert byte in file space to dataptr. It had better be aligned.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define XFS_DIR2_BYTE_TO_DATAPTR(mp,by) xfs_dir2_byte_to_dataptr(mp,by)
Nathan Scotta844f452005-11-02 14:38:42 +1100147static inline xfs_dir2_dataptr_t
148xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
149{
150 return (xfs_dir2_dataptr_t)((by) >> XFS_DIR2_DATA_ALIGN_LOG);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * Convert byte in space to (DB) block
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define XFS_DIR2_BYTE_TO_DB(mp,by) xfs_dir2_byte_to_db(mp, by)
Nathan Scotta844f452005-11-02 14:38:42 +1100157static inline xfs_dir2_db_t
158xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
159{
160 return (xfs_dir2_db_t)((by) >> \
161 ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog));
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*
Nathan Scotta844f452005-11-02 14:38:42 +1100165 * Convert dataptr to a block number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Nathan Scotta844f452005-11-02 14:38:42 +1100167#define XFS_DIR2_DATAPTR_TO_DB(mp,dp) xfs_dir2_dataptr_to_db(mp, dp)
168static inline xfs_dir2_db_t
169xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
170{
171 return XFS_DIR2_BYTE_TO_DB(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/*
175 * Convert byte in space to offset in a block
176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define XFS_DIR2_BYTE_TO_OFF(mp,by) xfs_dir2_byte_to_off(mp, by)
Nathan Scotta844f452005-11-02 14:38:42 +1100178static inline xfs_dir2_data_aoff_t
179xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
180{
181 return (xfs_dir2_data_aoff_t)((by) & \
182 ((1 << ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) - 1));
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/*
Nathan Scotta844f452005-11-02 14:38:42 +1100186 * Convert dataptr to a byte offset in a block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Nathan Scotta844f452005-11-02 14:38:42 +1100188#define XFS_DIR2_DATAPTR_TO_OFF(mp,dp) xfs_dir2_dataptr_to_off(mp, dp)
189static inline xfs_dir2_data_aoff_t
190xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
191{
192 return XFS_DIR2_BYTE_TO_OFF(mp, XFS_DIR2_DATAPTR_TO_BYTE(mp, dp));
193}
194
195/*
196 * Convert block and offset to byte in space
197 */
198#define XFS_DIR2_DB_OFF_TO_BYTE(mp,db,o) \
199 xfs_dir2_db_off_to_byte(mp, db, o)
200static inline xfs_dir2_off_t
201xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
202 xfs_dir2_data_aoff_t o)
203{
204 return ((xfs_dir2_off_t)(db) << \
205 ((mp)->m_sb.sb_blocklog + (mp)->m_sb.sb_dirblklog)) + (o);
206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/*
209 * Convert block (DB) to block (dablk)
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define XFS_DIR2_DB_TO_DA(mp,db) xfs_dir2_db_to_da(mp, db)
Nathan Scotta844f452005-11-02 14:38:42 +1100212static inline xfs_dablk_t
213xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
214{
215 return (xfs_dablk_t)((db) << (mp)->m_sb.sb_dirblklog);
216}
217
218/*
219 * Convert byte in space to (DA) block
220 */
221#define XFS_DIR2_BYTE_TO_DA(mp,by) xfs_dir2_byte_to_da(mp, by)
222static inline xfs_dablk_t
223xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
224{
225 return XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, by));
226}
227
228/*
229 * Convert block and offset to dataptr
230 */
231#define XFS_DIR2_DB_OFF_TO_DATAPTR(mp,db,o) \
232 xfs_dir2_db_off_to_dataptr(mp, db, o)
233static inline xfs_dir2_dataptr_t
234xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
235 xfs_dir2_data_aoff_t o)
236{
237 return XFS_DIR2_BYTE_TO_DATAPTR(mp, XFS_DIR2_DB_OFF_TO_BYTE(mp, db, o));
238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/*
241 * Convert block (dablk) to block (DB)
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define XFS_DIR2_DA_TO_DB(mp,da) xfs_dir2_da_to_db(mp, da)
Nathan Scotta844f452005-11-02 14:38:42 +1100244static inline xfs_dir2_db_t
245xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
246{
247 return (xfs_dir2_db_t)((da) >> (mp)->m_sb.sb_dirblklog);
248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*
251 * Convert block (dablk) to byte offset in space
252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#define XFS_DIR2_DA_TO_BYTE(mp,da) xfs_dir2_da_to_byte(mp, da)
Nathan Scotta844f452005-11-02 14:38:42 +1100254static inline xfs_dir2_off_t
255xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
256{
257 return XFS_DIR2_DB_OFF_TO_BYTE(mp, XFS_DIR2_DA_TO_DB(mp, da), 0);
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/*
261 * Function declarations.
262 */
Nathan Scotta844f452005-11-02 14:38:42 +1100263extern int xfs_dir2_block_to_leaf(struct xfs_da_args *args,
264 struct xfs_dabuf *dbp);
265extern int xfs_dir2_leaf_addname(struct xfs_da_args *args);
266extern void xfs_dir2_leaf_compact(struct xfs_da_args *args,
267 struct xfs_dabuf *bp);
268extern void xfs_dir2_leaf_compact_x1(struct xfs_dabuf *bp, int *indexp,
269 int *lowstalep, int *highstalep,
270 int *lowlogp, int *highlogp);
271extern int xfs_dir2_leaf_getdents(struct xfs_trans *tp, struct xfs_inode *dp,
272 struct uio *uio, int *eofp,
273 struct xfs_dirent *dbp, xfs_dir2_put_t put);
274extern int xfs_dir2_leaf_init(struct xfs_da_args *args, xfs_dir2_db_t bno,
275 struct xfs_dabuf **bpp, int magic);
276extern void xfs_dir2_leaf_log_ents(struct xfs_trans *tp, struct xfs_dabuf *bp,
277 int first, int last);
278extern void xfs_dir2_leaf_log_header(struct xfs_trans *tp,
279 struct xfs_dabuf *bp);
280extern int xfs_dir2_leaf_lookup(struct xfs_da_args *args);
281extern int xfs_dir2_leaf_removename(struct xfs_da_args *args);
282extern int xfs_dir2_leaf_replace(struct xfs_da_args *args);
283extern int xfs_dir2_leaf_search_hash(struct xfs_da_args *args,
284 struct xfs_dabuf *lbp);
285extern int xfs_dir2_leaf_trim_data(struct xfs_da_args *args,
286 struct xfs_dabuf *lbp, xfs_dir2_db_t db);
287extern int xfs_dir2_node_to_leaf(struct xfs_da_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289#endif /* __XFS_DIR2_LEAF_H__ */