Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 LT |
17 | */ |
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 | ||
31 | struct uio; | |
32 | struct xfs_bmap_free; | |
33 | struct xfs_dabuf; | |
34 | struct xfs_da_args; | |
35 | struct xfs_da_state; | |
36 | struct xfs_da_state_blk; | |
37 | struct xfs_dir_put_args; | |
38 | struct xfs_inode; | |
39 | struct xfs_mount; | |
40 | struct 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 | ||
3762ec6b NS |
70 | typedef struct xfs_dir_leaf_map { /* RLE map of free bytes */ |
71 | __uint16_t base; /* base of free region */ | |
72 | __uint16_t size; /* run length of free region */ | |
73 | } xfs_dir_leaf_map_t; | |
74 | ||
75 | typedef struct xfs_dir_leaf_hdr { /* constant-structure header block */ | |
76 | xfs_da_blkinfo_t info; /* block type, links, etc. */ | |
77 | __uint16_t count; /* count of active leaf_entry's */ | |
78 | __uint16_t namebytes; /* num bytes of name strings stored */ | |
79 | __uint16_t firstused; /* first used byte in name area */ | |
80 | __uint8_t holes; /* != 0 if blk needs compaction */ | |
81 | __uint8_t pad1; | |
82 | xfs_dir_leaf_map_t freemap[XFS_DIR_LEAF_MAPSIZE]; | |
83 | } xfs_dir_leaf_hdr_t; | |
84 | ||
85 | typedef struct xfs_dir_leaf_entry { /* sorted on key, not name */ | |
86 | xfs_dahash_t hashval; /* hash value of name */ | |
87 | __uint16_t nameidx; /* index into buffer of name */ | |
88 | __uint8_t namelen; /* length of name string */ | |
89 | __uint8_t pad2; | |
90 | } xfs_dir_leaf_entry_t; | |
91 | ||
92 | typedef struct xfs_dir_leaf_name { | |
93 | xfs_dir_ino_t inumber; /* inode number for this key */ | |
94 | __uint8_t name[1]; /* name string itself */ | |
95 | } xfs_dir_leaf_name_t; | |
96 | ||
1da177e4 | 97 | typedef struct xfs_dir_leafblock { |
3762ec6b NS |
98 | xfs_dir_leaf_hdr_t hdr; /* constant-structure header block */ |
99 | xfs_dir_leaf_entry_t entries[1]; /* var sized array */ | |
100 | xfs_dir_leaf_name_t namelist[1]; /* grows from bottom of buf */ | |
1da177e4 | 101 | } xfs_dir_leafblock_t; |
1da177e4 LT |
102 | |
103 | /* | |
104 | * Length of name for which a 512-byte block filesystem | |
105 | * can get a double split. | |
106 | */ | |
107 | #define XFS_DIR_LEAF_CAN_DOUBLE_SPLIT_LEN \ | |
108 | (512 - (uint)sizeof(xfs_dir_leaf_hdr_t) - \ | |
109 | (uint)sizeof(xfs_dir_leaf_entry_t) * 2 - \ | |
110 | (uint)sizeof(xfs_dir_leaf_name_t) * 2 - (MAXNAMELEN - 2) + 1 + 1) | |
111 | ||
112 | typedef int (*xfs_dir_put_t)(struct xfs_dir_put_args *pa); | |
113 | ||
114 | typedef union { | |
115 | xfs_off_t o; /* offset (cookie) */ | |
116 | /* | |
117 | * Watch the order here (endian-ness dependent). | |
118 | */ | |
119 | struct { | |
f016bad6 | 120 | #ifndef XFS_NATIVE_HOST |
1da177e4 LT |
121 | xfs_dahash_t h; /* hash value */ |
122 | __uint32_t be; /* block and entry */ | |
f016bad6 | 123 | #else |
1da177e4 LT |
124 | __uint32_t be; /* block and entry */ |
125 | xfs_dahash_t h; /* hash value */ | |
f016bad6 | 126 | #endif /* XFS_NATIVE_HOST */ |
1da177e4 LT |
127 | } s; |
128 | } xfs_dircook_t; | |
129 | ||
130 | #define XFS_PUT_COOKIE(c,mp,bno,entry,hash) \ | |
131 | ((c).s.be = XFS_DA_MAKE_BNOENTRY(mp, bno, entry), (c).s.h = (hash)) | |
132 | ||
3762ec6b | 133 | typedef struct xfs_dir_put_args { |
1da177e4 LT |
134 | xfs_dircook_t cook; /* cookie of (next) entry */ |
135 | xfs_intino_t ino; /* inode number */ | |
3762ec6b | 136 | struct xfs_dirent *dbp; /* buffer pointer */ |
1da177e4 LT |
137 | char *name; /* directory entry name */ |
138 | int namelen; /* length of name */ | |
139 | int done; /* output: set if value was stored */ | |
140 | xfs_dir_put_t put; /* put function ptr (i/o) */ | |
141 | struct uio *uio; /* uio control structure */ | |
142 | } xfs_dir_put_args_t; | |
143 | ||
3762ec6b NS |
144 | #define XFS_DIR_LEAF_ENTSIZE_BYNAME(len) \ |
145 | xfs_dir_leaf_entsize_byname(len) | |
a844f451 NS |
146 | static inline int xfs_dir_leaf_entsize_byname(int len) |
147 | { | |
148 | return (uint)sizeof(xfs_dir_leaf_name_t)-1 + len; | |
149 | } | |
150 | ||
1da177e4 LT |
151 | #define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry) \ |
152 | xfs_dir_leaf_entsize_byentry(entry) | |
a844f451 NS |
153 | static inline int xfs_dir_leaf_entsize_byentry(xfs_dir_leaf_entry_t *entry) |
154 | { | |
155 | return (uint)sizeof(xfs_dir_leaf_name_t)-1 + (entry)->namelen; | |
156 | } | |
157 | ||
1da177e4 LT |
158 | #define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset) \ |
159 | xfs_dir_leaf_namestruct(leafp,offset) | |
a844f451 NS |
160 | static inline xfs_dir_leaf_name_t * |
161 | xfs_dir_leaf_namestruct(xfs_dir_leafblock_t *leafp, int offset) | |
162 | { | |
163 | return (xfs_dir_leaf_name_t *)&((char *)(leafp))[offset]; | |
164 | } | |
1da177e4 LT |
165 | |
166 | /*======================================================================== | |
167 | * Function prototypes for the kernel. | |
168 | *========================================================================*/ | |
169 | ||
170 | /* | |
171 | * Internal routines when dirsize < XFS_LITINO(mp). | |
172 | */ | |
173 | int xfs_dir_shortform_create(struct xfs_da_args *args, xfs_ino_t parent); | |
174 | int xfs_dir_shortform_addname(struct xfs_da_args *args); | |
175 | int xfs_dir_shortform_lookup(struct xfs_da_args *args); | |
176 | int xfs_dir_shortform_to_leaf(struct xfs_da_args *args); | |
177 | int xfs_dir_shortform_removename(struct xfs_da_args *args); | |
178 | int xfs_dir_shortform_getdents(struct xfs_inode *dp, struct uio *uio, int *eofp, | |
a844f451 | 179 | struct xfs_dirent *dbp, xfs_dir_put_t put); |
1da177e4 LT |
180 | int xfs_dir_shortform_replace(struct xfs_da_args *args); |
181 | ||
182 | /* | |
183 | * Internal routines when dirsize == XFS_LBSIZE(mp). | |
184 | */ | |
185 | int xfs_dir_leaf_to_node(struct xfs_da_args *args); | |
186 | int xfs_dir_leaf_to_shortform(struct xfs_da_args *args); | |
187 | ||
188 | /* | |
189 | * Routines used for growing the Btree. | |
190 | */ | |
1da177e4 LT |
191 | int xfs_dir_leaf_split(struct xfs_da_state *state, |
192 | struct xfs_da_state_blk *oldblk, | |
193 | struct xfs_da_state_blk *newblk); | |
194 | int xfs_dir_leaf_add(struct xfs_dabuf *leaf_buffer, | |
195 | struct xfs_da_args *args, int insertion_index); | |
196 | int xfs_dir_leaf_addname(struct xfs_da_args *args); | |
197 | int xfs_dir_leaf_lookup_int(struct xfs_dabuf *leaf_buffer, | |
198 | struct xfs_da_args *args, | |
199 | int *index_found_at); | |
200 | int xfs_dir_leaf_remove(struct xfs_trans *trans, | |
201 | struct xfs_dabuf *leaf_buffer, | |
202 | int index_to_remove); | |
203 | int xfs_dir_leaf_getdents_int(struct xfs_dabuf *bp, struct xfs_inode *dp, | |
204 | xfs_dablk_t bno, struct uio *uio, | |
205 | int *eobp, struct xfs_dirent *dbp, | |
206 | xfs_dir_put_t put, xfs_daddr_t nextda); | |
207 | ||
208 | /* | |
209 | * Routines used for shrinking the Btree. | |
210 | */ | |
211 | int xfs_dir_leaf_toosmall(struct xfs_da_state *state, int *retval); | |
212 | void xfs_dir_leaf_unbalance(struct xfs_da_state *state, | |
213 | struct xfs_da_state_blk *drop_blk, | |
214 | struct xfs_da_state_blk *save_blk); | |
215 | ||
216 | /* | |
217 | * Utility routines. | |
218 | */ | |
219 | uint xfs_dir_leaf_lasthash(struct xfs_dabuf *bp, int *count); | |
220 | int xfs_dir_leaf_order(struct xfs_dabuf *leaf1_bp, | |
221 | struct xfs_dabuf *leaf2_bp); | |
222 | int xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa); | |
223 | int xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa); | |
224 | int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino); | |
225 | ||
1da177e4 LT |
226 | /* |
227 | * Global data. | |
228 | */ | |
229 | extern xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; | |
230 | ||
231 | #endif /* __XFS_DIR_LEAF_H__ */ |