blob: ab6b09eef9ab840079fba4005e9faec83861a658 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_DIR_LEAF_H__
19#define __XFS_DIR_LEAF_H__
20
21/*
22 * Directory layout, internal structure, access macros, etc.
23 *
24 * Large directories are structured around Btrees where all the data
25 * elements are in the leaf nodes. Filenames are hashed into an int,
26 * then that int is used as the index into the Btree. Since the hashval
27 * of a filename may not be unique, we may have duplicate keys. The
28 * internal links in the Btree are logical block offsets into the file.
29 */
30
31struct uio;
32struct xfs_bmap_free;
33struct xfs_dabuf;
34struct xfs_da_args;
35struct xfs_da_state;
36struct xfs_da_state_blk;
37struct xfs_dir_put_args;
38struct xfs_inode;
39struct xfs_mount;
40struct xfs_trans;
41
42/*========================================================================
43 * Directory Structure when equal to XFS_LBSIZE(mp) bytes.
44 *========================================================================*/
45
46/*
47 * This is the structure of the leaf nodes in the Btree.
48 *
49 * Struct leaf_entry's are packed from the top. Names grow from the bottom
50 * but are not packed. The freemap contains run-length-encoded entries
51 * for the free bytes after the leaf_entry's, but only the N largest such,
52 * smaller runs are dropped. When the freemap doesn't show enough space
53 * for an allocation, we compact the namelist area and try again. If we
54 * still don't have enough space, then we have to split the block.
55 *
56 * Since we have duplicate hash keys, for each key that matches, compare
57 * the actual string. The root and intermediate node search always takes
58 * the first-in-the-block key match found, so we should only have to work
59 * "forw"ard. If none matches, continue with the "forw"ard leaf nodes
60 * until the hash key changes or the filename is found.
61 *
62 * The parent directory and the self-pointer are explicitly represented
63 * (ie: there are entries for "." and "..").
64 *
65 * Note that the count being a __uint16_t limits us to something like a
66 * blocksize of 1.3MB in the face of worst case (short) filenames.
67 */
68#define XFS_DIR_LEAF_MAPSIZE 3 /* how many freespace slots */
69
70typedef struct xfs_dir_leafblock {
71 struct xfs_dir_leaf_hdr { /* constant-structure header block */
72 xfs_da_blkinfo_t info; /* block type, links, etc. */
73 __uint16_t count; /* count of active leaf_entry's */
74 __uint16_t namebytes; /* num bytes of name strings stored */
75 __uint16_t firstused; /* first used byte in name area */
76 __uint8_t holes; /* != 0 if blk needs compaction */
77 __uint8_t pad1;
78 struct xfs_dir_leaf_map {/* RLE map of free bytes */
79 __uint16_t base; /* base of free region */
80 __uint16_t size; /* run length of free region */
81 } freemap[XFS_DIR_LEAF_MAPSIZE]; /* N largest free regions */
82 } hdr;
83 struct xfs_dir_leaf_entry { /* sorted on key, not name */
84 xfs_dahash_t hashval; /* hash value of name */
85 __uint16_t nameidx; /* index into buffer of name */
86 __uint8_t namelen; /* length of name string */
87 __uint8_t pad2;
88 } entries[1]; /* var sized array */
89 struct xfs_dir_leaf_name {
90 xfs_dir_ino_t inumber; /* inode number for this key */
91 __uint8_t name[1]; /* name string itself */
92 } namelist[1]; /* grows from bottom of buf */
93} xfs_dir_leafblock_t;
94typedef struct xfs_dir_leaf_hdr xfs_dir_leaf_hdr_t;
95typedef struct xfs_dir_leaf_map xfs_dir_leaf_map_t;
96typedef struct xfs_dir_leaf_entry xfs_dir_leaf_entry_t;
97typedef struct xfs_dir_leaf_name xfs_dir_leaf_name_t;
98
99/*
100 * Length of name for which a 512-byte block filesystem
101 * can get a double split.
102 */
103#define XFS_DIR_LEAF_CAN_DOUBLE_SPLIT_LEN \
104 (512 - (uint)sizeof(xfs_dir_leaf_hdr_t) - \
105 (uint)sizeof(xfs_dir_leaf_entry_t) * 2 - \
106 (uint)sizeof(xfs_dir_leaf_name_t) * 2 - (MAXNAMELEN - 2) + 1 + 1)
107
108typedef int (*xfs_dir_put_t)(struct xfs_dir_put_args *pa);
109
110typedef union {
111 xfs_off_t o; /* offset (cookie) */
112 /*
113 * Watch the order here (endian-ness dependent).
114 */
115 struct {
Nathan Scottf016bad2005-09-08 15:30:05 +1000116#ifndef XFS_NATIVE_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xfs_dahash_t h; /* hash value */
118 __uint32_t be; /* block and entry */
Nathan Scottf016bad2005-09-08 15:30:05 +1000119#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 __uint32_t be; /* block and entry */
121 xfs_dahash_t h; /* hash value */
Nathan Scottf016bad2005-09-08 15:30:05 +1000122#endif /* XFS_NATIVE_HOST */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } s;
124} xfs_dircook_t;
125
126#define XFS_PUT_COOKIE(c,mp,bno,entry,hash) \
127 ((c).s.be = XFS_DA_MAKE_BNOENTRY(mp, bno, entry), (c).s.h = (hash))
128
129typedef struct xfs_dir_put_args
130{
131 xfs_dircook_t cook; /* cookie of (next) entry */
132 xfs_intino_t ino; /* inode number */
133 struct xfs_dirent *dbp; /* buffer pointer */
134 char *name; /* directory entry name */
135 int namelen; /* length of name */
136 int done; /* output: set if value was stored */
137 xfs_dir_put_t put; /* put function ptr (i/o) */
138 struct uio *uio; /* uio control structure */
139} xfs_dir_put_args_t;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define XFS_DIR_LEAF_ENTSIZE_BYNAME(len) xfs_dir_leaf_entsize_byname(len)
Nathan Scotta844f452005-11-02 14:38:42 +1100142static inline int xfs_dir_leaf_entsize_byname(int len)
143{
144 return (uint)sizeof(xfs_dir_leaf_name_t)-1 + len;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry) \
148 xfs_dir_leaf_entsize_byentry(entry)
Nathan Scotta844f452005-11-02 14:38:42 +1100149static inline int xfs_dir_leaf_entsize_byentry(xfs_dir_leaf_entry_t *entry)
150{
151 return (uint)sizeof(xfs_dir_leaf_name_t)-1 + (entry)->namelen;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset) \
155 xfs_dir_leaf_namestruct(leafp,offset)
Nathan Scotta844f452005-11-02 14:38:42 +1100156static inline xfs_dir_leaf_name_t *
157xfs_dir_leaf_namestruct(xfs_dir_leafblock_t *leafp, int offset)
158{
159 return (xfs_dir_leaf_name_t *)&((char *)(leafp))[offset];
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/*========================================================================
163 * Function prototypes for the kernel.
164 *========================================================================*/
165
166/*
167 * Internal routines when dirsize < XFS_LITINO(mp).
168 */
169int xfs_dir_shortform_create(struct xfs_da_args *args, xfs_ino_t parent);
170int xfs_dir_shortform_addname(struct xfs_da_args *args);
171int xfs_dir_shortform_lookup(struct xfs_da_args *args);
172int xfs_dir_shortform_to_leaf(struct xfs_da_args *args);
173int xfs_dir_shortform_removename(struct xfs_da_args *args);
174int xfs_dir_shortform_getdents(struct xfs_inode *dp, struct uio *uio, int *eofp,
Nathan Scotta844f452005-11-02 14:38:42 +1100175 struct xfs_dirent *dbp, xfs_dir_put_t put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176int xfs_dir_shortform_replace(struct xfs_da_args *args);
177
178/*
179 * Internal routines when dirsize == XFS_LBSIZE(mp).
180 */
181int xfs_dir_leaf_to_node(struct xfs_da_args *args);
182int xfs_dir_leaf_to_shortform(struct xfs_da_args *args);
183
184/*
185 * Routines used for growing the Btree.
186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187int xfs_dir_leaf_split(struct xfs_da_state *state,
188 struct xfs_da_state_blk *oldblk,
189 struct xfs_da_state_blk *newblk);
190int xfs_dir_leaf_add(struct xfs_dabuf *leaf_buffer,
191 struct xfs_da_args *args, int insertion_index);
192int xfs_dir_leaf_addname(struct xfs_da_args *args);
193int xfs_dir_leaf_lookup_int(struct xfs_dabuf *leaf_buffer,
194 struct xfs_da_args *args,
195 int *index_found_at);
196int xfs_dir_leaf_remove(struct xfs_trans *trans,
197 struct xfs_dabuf *leaf_buffer,
198 int index_to_remove);
199int xfs_dir_leaf_getdents_int(struct xfs_dabuf *bp, struct xfs_inode *dp,
200 xfs_dablk_t bno, struct uio *uio,
201 int *eobp, struct xfs_dirent *dbp,
202 xfs_dir_put_t put, xfs_daddr_t nextda);
203
204/*
205 * Routines used for shrinking the Btree.
206 */
207int xfs_dir_leaf_toosmall(struct xfs_da_state *state, int *retval);
208void xfs_dir_leaf_unbalance(struct xfs_da_state *state,
209 struct xfs_da_state_blk *drop_blk,
210 struct xfs_da_state_blk *save_blk);
211
212/*
213 * Utility routines.
214 */
215uint xfs_dir_leaf_lasthash(struct xfs_dabuf *bp, int *count);
216int xfs_dir_leaf_order(struct xfs_dabuf *leaf1_bp,
217 struct xfs_dabuf *leaf2_bp);
218int xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa);
219int xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa);
220int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * Global data.
224 */
225extern xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
226
227#endif /* __XFS_DIR_LEAF_H__ */