1153323Srodrigc/*
2159451Srodrigc * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3159451Srodrigc * All Rights Reserved.
4153323Srodrigc *
5159451Srodrigc * This program is free software; you can redistribute it and/or
6159451Srodrigc * modify it under the terms of the GNU General Public License as
7153323Srodrigc * published by the Free Software Foundation.
8153323Srodrigc *
9159451Srodrigc * This program is distributed in the hope that it would be useful,
10159451Srodrigc * but WITHOUT ANY WARRANTY; without even the implied warranty of
11159451Srodrigc * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12159451Srodrigc * GNU General Public License for more details.
13153323Srodrigc *
14159451Srodrigc * You should have received a copy of the GNU General Public License
15159451Srodrigc * along with this program; if not, write the Free Software Foundation,
16159451Srodrigc * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17153323Srodrigc */
18153323Srodrigc#ifndef __XFS_DIR_LEAF_H__
19153323Srodrigc#define	__XFS_DIR_LEAF_H__
20153323Srodrigc
21153323Srodrigc/*
22153323Srodrigc * Directory layout, internal structure, access macros, etc.
23153323Srodrigc *
24153323Srodrigc * Large directories are structured around Btrees where all the data
25153323Srodrigc * elements are in the leaf nodes.  Filenames are hashed into an int,
26153323Srodrigc * then that int is used as the index into the Btree.  Since the hashval
27153323Srodrigc * of a filename may not be unique, we may have duplicate keys.  The
28153323Srodrigc * internal links in the Btree are logical block offsets into the file.
29153323Srodrigc */
30153323Srodrigc
31153323Srodrigcstruct uio;
32153323Srodrigcstruct xfs_bmap_free;
33153323Srodrigcstruct xfs_dabuf;
34153323Srodrigcstruct xfs_da_args;
35153323Srodrigcstruct xfs_da_state;
36153323Srodrigcstruct xfs_da_state_blk;
37153323Srodrigcstruct xfs_dir_put_args;
38153323Srodrigcstruct xfs_inode;
39153323Srodrigcstruct xfs_mount;
40153323Srodrigcstruct xfs_trans;
41153323Srodrigc
42153323Srodrigc/*========================================================================
43153323Srodrigc * Directory Structure when equal to XFS_LBSIZE(mp) bytes.
44153323Srodrigc *========================================================================*/
45153323Srodrigc
46153323Srodrigc/*
47153323Srodrigc * This is the structure of the leaf nodes in the Btree.
48153323Srodrigc *
49153323Srodrigc * Struct leaf_entry's are packed from the top.  Names grow from the bottom
50153323Srodrigc * but are not packed.  The freemap contains run-length-encoded entries
51153323Srodrigc * for the free bytes after the leaf_entry's, but only the N largest such,
52153323Srodrigc * smaller runs are dropped.  When the freemap doesn't show enough space
53153323Srodrigc * for an allocation, we compact the namelist area and try again.  If we
54153323Srodrigc * still don't have enough space, then we have to split the block.
55153323Srodrigc *
56153323Srodrigc * Since we have duplicate hash keys, for each key that matches, compare
57153323Srodrigc * the actual string.  The root and intermediate node search always takes
58153323Srodrigc * the first-in-the-block key match found, so we should only have to work
59153323Srodrigc * "forw"ard.  If none matches, continue with the "forw"ard leaf nodes
60153323Srodrigc * until the hash key changes or the filename is found.
61153323Srodrigc *
62153323Srodrigc * The parent directory and the self-pointer are explicitly represented
63153323Srodrigc * (ie: there are entries for "." and "..").
64153323Srodrigc *
65153323Srodrigc * Note that the count being a __uint16_t limits us to something like a
66153323Srodrigc * blocksize of 1.3MB in the face of worst case (short) filenames.
67153323Srodrigc */
68153323Srodrigc#define XFS_DIR_LEAF_MAPSIZE	3	/* how many freespace slots */
69153323Srodrigc
70159451Srodrigctypedef struct xfs_dir_leaf_map {	/* RLE map of free bytes */
71159451Srodrigc	__uint16_t	base;	 	/* base of free region */
72159451Srodrigc	__uint16_t	size; 		/* run length of free region */
73159451Srodrigc} xfs_dir_leaf_map_t;
74159451Srodrigc
75159451Srodrigctypedef struct xfs_dir_leaf_hdr {	/* constant-structure header block */
76159451Srodrigc	xfs_da_blkinfo_t info;		/* block type, links, etc. */
77159451Srodrigc	__uint16_t	count;		/* count of active leaf_entry's */
78159451Srodrigc	__uint16_t	namebytes;	/* num bytes of name strings stored */
79159451Srodrigc	__uint16_t	firstused;	/* first used byte in name area */
80159451Srodrigc	__uint8_t	holes;		/* != 0 if blk needs compaction */
81159451Srodrigc	__uint8_t	pad1;
82159451Srodrigc	xfs_dir_leaf_map_t freemap[XFS_DIR_LEAF_MAPSIZE];
83159451Srodrigc} xfs_dir_leaf_hdr_t;
84159451Srodrigc
85159451Srodrigctypedef struct xfs_dir_leaf_entry {	/* sorted on key, not name */
86159451Srodrigc	xfs_dahash_t	hashval;	/* hash value of name */
87159451Srodrigc	__uint16_t	nameidx;	/* index into buffer of name */
88159451Srodrigc	__uint8_t	namelen;	/* length of name string */
89159451Srodrigc	__uint8_t	pad2;
90159451Srodrigc} xfs_dir_leaf_entry_t;
91159451Srodrigc
92159451Srodrigctypedef struct xfs_dir_leaf_name {
93159451Srodrigc	xfs_dir_ino_t	inumber;	/* inode number for this key */
94159451Srodrigc	__uint8_t	name[1];	/* name string itself */
95159451Srodrigc} xfs_dir_leaf_name_t;
96159451Srodrigc
97153323Srodrigctypedef struct xfs_dir_leafblock {
98159451Srodrigc	xfs_dir_leaf_hdr_t	hdr;	/* constant-structure header block */
99159451Srodrigc	xfs_dir_leaf_entry_t	entries[1];	/* var sized array */
100159451Srodrigc	xfs_dir_leaf_name_t	namelist[1];	/* grows from bottom of buf */
101153323Srodrigc} xfs_dir_leafblock_t;
102153323Srodrigc
103153323Srodrigc/*
104153323Srodrigc * Length of name for which a 512-byte block filesystem
105153323Srodrigc * can get a double split.
106153323Srodrigc */
107153323Srodrigc#define	XFS_DIR_LEAF_CAN_DOUBLE_SPLIT_LEN	\
108153323Srodrigc	(512 - (uint)sizeof(xfs_dir_leaf_hdr_t) - \
109153323Srodrigc	 (uint)sizeof(xfs_dir_leaf_entry_t) * 2 - \
110153323Srodrigc	 (uint)sizeof(xfs_dir_leaf_name_t) * 2 - (MAXNAMELEN - 2) + 1 + 1)
111153323Srodrigc
112153323Srodrigctypedef int (*xfs_dir_put_t)(struct xfs_dir_put_args *pa);
113153323Srodrigc
114153323Srodrigctypedef union {
115153323Srodrigc	xfs_off_t		o;		/* offset (cookie) */
116153323Srodrigc	/*
117153323Srodrigc	 * Watch the order here (endian-ness dependent).
118153323Srodrigc	 */
119153323Srodrigc	struct {
120159451Srodrigc#ifndef XFS_NATIVE_HOST
121153323Srodrigc		xfs_dahash_t	h;	/* hash value */
122153323Srodrigc		__uint32_t	be;	/* block and entry */
123159451Srodrigc#else
124153323Srodrigc		__uint32_t	be;	/* block and entry */
125153323Srodrigc		xfs_dahash_t	h;	/* hash value */
126159451Srodrigc#endif /* XFS_NATIVE_HOST */
127153323Srodrigc	} s;
128153323Srodrigc} xfs_dircook_t;
129153323Srodrigc
130153323Srodrigc#define	XFS_PUT_COOKIE(c,mp,bno,entry,hash)	\
131153323Srodrigc	((c).s.be = XFS_DA_MAKE_BNOENTRY(mp, bno, entry), (c).s.h = (hash))
132153323Srodrigc
133159451Srodrigctypedef struct xfs_dir_put_args {
134153323Srodrigc	xfs_dircook_t	cook;		/* cookie of (next) entry */
135153323Srodrigc	xfs_intino_t	ino;		/* inode number */
136159451Srodrigc	struct xfs_dirent *dbp;		/* buffer pointer */
137153323Srodrigc	char		*name;		/* directory entry name */
138153323Srodrigc	int		namelen;	/* length of name */
139153323Srodrigc	int		done;		/* output: set if value was stored */
140153323Srodrigc	xfs_dir_put_t	put;		/* put function ptr (i/o) */
141153323Srodrigc	struct uio	*uio;		/* uio control structure */
142153323Srodrigc} xfs_dir_put_args_t;
143153323Srodrigc
144159451Srodrigc#define XFS_DIR_LEAF_ENTSIZE_BYNAME(len)	\
145159451Srodrigc	xfs_dir_leaf_entsize_byname(len)
146159451Srodrigcstatic inline int xfs_dir_leaf_entsize_byname(int len)
147159451Srodrigc{
148159451Srodrigc	return (uint)sizeof(xfs_dir_leaf_name_t)-1 + len;
149159451Srodrigc}
150153323Srodrigc
151153323Srodrigc#define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)	\
152153323Srodrigc	xfs_dir_leaf_entsize_byentry(entry)
153159451Srodrigcstatic inline int xfs_dir_leaf_entsize_byentry(xfs_dir_leaf_entry_t *entry)
154159451Srodrigc{
155159451Srodrigc	return (uint)sizeof(xfs_dir_leaf_name_t)-1 + (entry)->namelen;
156159451Srodrigc}
157153323Srodrigc
158153323Srodrigc#define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset)	\
159153323Srodrigc	xfs_dir_leaf_namestruct(leafp,offset)
160159451Srodrigcstatic inline xfs_dir_leaf_name_t *
161159451Srodrigcxfs_dir_leaf_namestruct(xfs_dir_leafblock_t *leafp, int offset)
162159451Srodrigc{
163159451Srodrigc	return (xfs_dir_leaf_name_t *)&((char *)(leafp))[offset];
164159451Srodrigc}
165153323Srodrigc
166153323Srodrigc/*========================================================================
167153323Srodrigc * Function prototypes for the kernel.
168153323Srodrigc *========================================================================*/
169153323Srodrigc
170153323Srodrigc/*
171153323Srodrigc * Internal routines when dirsize < XFS_LITINO(mp).
172153323Srodrigc */
173153323Srodrigcint xfs_dir_shortform_create(struct xfs_da_args *args, xfs_ino_t parent);
174153323Srodrigcint xfs_dir_shortform_addname(struct xfs_da_args *args);
175153323Srodrigcint xfs_dir_shortform_lookup(struct xfs_da_args *args);
176153323Srodrigcint xfs_dir_shortform_to_leaf(struct xfs_da_args *args);
177153323Srodrigcint xfs_dir_shortform_removename(struct xfs_da_args *args);
178153323Srodrigcint xfs_dir_shortform_getdents(struct xfs_inode *dp, struct uio *uio, int *eofp,
179159451Srodrigc			       struct xfs_dirent *dbp, xfs_dir_put_t put);
180153323Srodrigcint xfs_dir_shortform_replace(struct xfs_da_args *args);
181153323Srodrigc
182153323Srodrigc/*
183153323Srodrigc * Internal routines when dirsize == XFS_LBSIZE(mp).
184153323Srodrigc */
185153323Srodrigcint xfs_dir_leaf_to_node(struct xfs_da_args *args);
186153323Srodrigcint xfs_dir_leaf_to_shortform(struct xfs_da_args *args);
187153323Srodrigc
188153323Srodrigc/*
189153323Srodrigc * Routines used for growing the Btree.
190153323Srodrigc */
191153323Srodrigcint	xfs_dir_leaf_split(struct xfs_da_state *state,
192153323Srodrigc				  struct xfs_da_state_blk *oldblk,
193153323Srodrigc				  struct xfs_da_state_blk *newblk);
194153323Srodrigcint	xfs_dir_leaf_add(struct xfs_dabuf *leaf_buffer,
195153323Srodrigc				struct xfs_da_args *args, int insertion_index);
196153323Srodrigcint	xfs_dir_leaf_addname(struct xfs_da_args *args);
197153323Srodrigcint	xfs_dir_leaf_lookup_int(struct xfs_dabuf *leaf_buffer,
198153323Srodrigc				       struct xfs_da_args *args,
199153323Srodrigc				       int *index_found_at);
200153323Srodrigcint	xfs_dir_leaf_remove(struct xfs_trans *trans,
201153323Srodrigc				   struct xfs_dabuf *leaf_buffer,
202153323Srodrigc				   int index_to_remove);
203153323Srodrigcint	xfs_dir_leaf_getdents_int(struct xfs_dabuf *bp, struct xfs_inode *dp,
204153323Srodrigc					 xfs_dablk_t bno, struct uio *uio,
205153323Srodrigc					 int *eobp, struct xfs_dirent *dbp,
206153323Srodrigc					 xfs_dir_put_t put, xfs_daddr_t nextda);
207153323Srodrigc
208153323Srodrigc/*
209153323Srodrigc * Routines used for shrinking the Btree.
210153323Srodrigc */
211153323Srodrigcint	xfs_dir_leaf_toosmall(struct xfs_da_state *state, int *retval);
212153323Srodrigcvoid	xfs_dir_leaf_unbalance(struct xfs_da_state *state,
213153323Srodrigc					     struct xfs_da_state_blk *drop_blk,
214153323Srodrigc					     struct xfs_da_state_blk *save_blk);
215153323Srodrigc
216153323Srodrigc/*
217153323Srodrigc * Utility routines.
218153323Srodrigc */
219153323Srodrigcuint	xfs_dir_leaf_lasthash(struct xfs_dabuf *bp, int *count);
220153323Srodrigcint	xfs_dir_leaf_order(struct xfs_dabuf *leaf1_bp,
221153323Srodrigc				  struct xfs_dabuf *leaf2_bp);
222153323Srodrigcint	xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa);
223153323Srodrigcint	xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa);
224153323Srodrigcint	xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
225153323Srodrigc
226153323Srodrigc/*
227153323Srodrigc * Global data.
228153323Srodrigc */
229153323Srodrigcextern xfs_dahash_t	xfs_dir_hash_dot, xfs_dir_hash_dotdot;
230153323Srodrigc
231153323Srodrigc#endif /* __XFS_DIR_LEAF_H__ */
232