Whamcloud - gitweb
Modified ChangeLog entry.
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-htree-2.4.22-rh.patch
1  fs/ext3/Makefile           |    2 
2  fs/ext3/dir.c              |  302 +++++++++
3  fs/ext3/file.c             |    3 
4  fs/ext3/hash.c             |  215 ++++++
5  fs/ext3/namei.c            | 1421 ++++++++++++++++++++++++++++++++++++++++-----
6  fs/ext3/super.c            |    7 
7  include/linux/ext3_fs.h    |   85 ++
8  include/linux/ext3_fs_sb.h |    2 
9  include/linux/ext3_jbd.h   |    2 
10  include/linux/rbtree.h     |    2 
11  lib/rbtree.c               |   42 +
12  11 files changed, 1922 insertions(+), 161 deletions(-)
13
14 --- linux-2.4.22-ac1/fs/ext3/dir.c~ext3-htree-2.4.22-rh 2001-11-10 01:25:04.000000000 +0300
15 +++ linux-2.4.22-ac1-alexey/fs/ext3/dir.c       2003-09-25 14:58:30.000000000 +0400
16 @@ -21,12 +21,16 @@
17  #include <linux/fs.h>
18  #include <linux/jbd.h>
19  #include <linux/ext3_fs.h>
20 +#include <linux/slab.h>
21 +#include <linux/rbtree.h>
22  
23  static unsigned char ext3_filetype_table[] = {
24         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
25  };
26  
27  static int ext3_readdir(struct file *, void *, filldir_t);
28 +static int ext3_dx_readdir(struct file * filp,
29 +                          void * dirent, filldir_t filldir);
30  
31  struct file_operations ext3_dir_operations = {
32         read:           generic_read_dir,
33 @@ -35,6 +39,17 @@ struct file_operations ext3_dir_operatio
34         fsync:          ext3_sync_file,         /* BKL held */
35  };
36  
37 +
38 +static unsigned char get_dtype(struct super_block *sb, int filetype)
39 +{
40 +       if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
41 +           (filetype >= EXT3_FT_MAX))
42 +               return DT_UNKNOWN;
43 +
44 +       return (ext3_filetype_table[filetype]);
45 +}
46 +                              
47 +
48  int ext3_check_dir_entry (const char * function, struct inode * dir,
49                           struct ext3_dir_entry_2 * de,
50                           struct buffer_head * bh,
51 @@ -79,6 +94,16 @@ static int ext3_readdir(struct file * fi
52  
53         sb = inode->i_sb;
54  
55 +       if (is_dx(inode)) {
56 +               err = ext3_dx_readdir(filp, dirent, filldir);
57 +               if (err != ERR_BAD_DX_DIR)
58 +                       return err;
59 +               /*
60 +                * We don't set the inode dirty flag since it's not
61 +                * critical that it get flushed back to the disk.
62 +                */
63 +               EXT3_I(filp->f_dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
64 +       }
65         stored = 0;
66         bh = NULL;
67         offset = filp->f_pos & (sb->s_blocksize - 1);
68 @@ -162,18 +187,12 @@ revalidate:
69                                  * during the copy operation.
70                                  */
71                                 unsigned long version = filp->f_version;
72 -                               unsigned char d_type = DT_UNKNOWN;
73  
74 -                               if (EXT3_HAS_INCOMPAT_FEATURE(sb,
75 -                                               EXT3_FEATURE_INCOMPAT_FILETYPE)
76 -                                               && de->file_type < EXT3_FT_MAX)
77 -                                       d_type =
78 -                                         ext3_filetype_table[de->file_type];
79                                 error = filldir(dirent, de->name,
80                                                 de->name_len,
81                                                 filp->f_pos,
82                                                 le32_to_cpu(de->inode),
83 -                                               d_type);
84 +                                               get_dtype(sb, de->file_type));
85                                 if (error)
86                                         break;
87                                 if (version != filp->f_version)
88 @@ -188,3 +207,272 @@ revalidate:
89         UPDATE_ATIME(inode);
90         return 0;
91  }
92 +
93 +#ifdef CONFIG_EXT3_INDEX
94 +/*
95 + * These functions convert from the major/minor hash to an f_pos
96 + * value.
97 + * 
98 + * Currently we only use major hash numer.  This is unfortunate, but
99 + * on 32-bit machines, the same VFS interface is used for lseek and
100 + * llseek, so if we use the 64 bit offset, then the 32-bit versions of
101 + * lseek/telldir/seekdir will blow out spectacularly, and from within
102 + * the ext2 low-level routine, we don't know if we're being called by
103 + * a 64-bit version of the system call or the 32-bit version of the
104 + * system call.  Worse yet, NFSv2 only allows for a 32-bit readdir
105 + * cookie.  Sigh.
106 + */
107 +#define hash2pos(major, minor) (major >> 1)
108 +#define pos2maj_hash(pos)      ((pos << 1) & 0xffffffff)
109 +#define pos2min_hash(pos)      (0)
110 +
111 +/*
112 + * This structure holds the nodes of the red-black tree used to store
113 + * the directory entry in hash order.
114 + */
115 +struct fname {
116 +       __u32           hash;
117 +       __u32           minor_hash;
118 +       rb_node_t       rb_hash; 
119 +       struct fname    *next;
120 +       __u32           inode;
121 +       __u8            name_len;
122 +       __u8            file_type;
123 +       char            name[0];
124 +};
125 +
126 +/*
127 + * This functoin implements a non-recursive way of freeing all of the
128 + * nodes in the red-black tree.
129 + */
130 +static void free_rb_tree_fname(rb_root_t *root)
131 +{
132 +       rb_node_t       *n = root->rb_node;
133 +       rb_node_t       *parent;
134 +       struct fname    *fname;
135 +
136 +       while (n) {
137 +               /* Do the node's children first */
138 +               if ((n)->rb_left) {
139 +                       n = n->rb_left;
140 +                       continue;
141 +               }
142 +               if (n->rb_right) {
143 +                       n = n->rb_right;
144 +                       continue;
145 +               }
146 +               /*
147 +                * The node has no children; free it, and then zero
148 +                * out parent's link to it.  Finally go to the
149 +                * beginning of the loop and try to free the parent
150 +                * node.
151 +                */
152 +               parent = n->rb_parent;
153 +               fname = rb_entry(n, struct fname, rb_hash);
154 +               kfree(fname);
155 +               if (!parent)
156 +                       root->rb_node = 0;
157 +               else if (parent->rb_left == n)
158 +                       parent->rb_left = 0;
159 +               else if (parent->rb_right == n)
160 +                       parent->rb_right = 0;
161 +               n = parent;
162 +       }
163 +       root->rb_node = 0;
164 +}
165 +
166 +
167 +struct dir_private_info *create_dir_info(loff_t pos)
168 +{
169 +       struct dir_private_info *p;
170 +
171 +       p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
172 +       if (!p)
173 +               return NULL;
174 +       p->root.rb_node = 0;
175 +       p->curr_node = 0;
176 +       p->extra_fname = 0;
177 +       p->last_pos = 0;
178 +       p->curr_hash = pos2maj_hash(pos);
179 +       p->curr_minor_hash = pos2min_hash(pos);
180 +       p->next_hash = 0;
181 +       return p;
182 +}
183 +
184 +void ext3_htree_free_dir_info(struct dir_private_info *p)
185 +{
186 +       free_rb_tree_fname(&p->root);
187 +       kfree(p);
188 +}
189 +               
190 +/*
191 + * Given a directory entry, enter it into the fname rb tree.
192 + */
193 +int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
194 +                            __u32 minor_hash,
195 +                            struct ext3_dir_entry_2 *dirent)
196 +{
197 +       rb_node_t **p, *parent = NULL;
198 +       struct fname * fname, *new_fn;
199 +       struct dir_private_info *info;
200 +       int len;
201 +
202 +       info = (struct dir_private_info *) dir_file->private_data;
203 +       p = &info->root.rb_node;
204 +
205 +       /* Create and allocate the fname structure */
206 +       len = sizeof(struct fname) + dirent->name_len + 1;
207 +       new_fn = kmalloc(len, GFP_KERNEL);
208 +       if (!new_fn)
209 +               return -ENOMEM;
210 +       memset(new_fn, 0, len);
211 +       new_fn->hash = hash;
212 +       new_fn->minor_hash = minor_hash;
213 +       new_fn->inode = le32_to_cpu(dirent->inode);
214 +       new_fn->name_len = dirent->name_len;
215 +       new_fn->file_type = dirent->file_type;
216 +       memcpy(new_fn->name, dirent->name, dirent->name_len);
217 +       new_fn->name[dirent->name_len] = 0;
218 +       
219 +       while (*p) {
220 +               parent = *p;
221 +               fname = rb_entry(parent, struct fname, rb_hash);
222 +
223 +               /*
224 +                * If the hash and minor hash match up, then we put
225 +                * them on a linked list.  This rarely happens...
226 +                */
227 +               if ((new_fn->hash == fname->hash) &&
228 +                   (new_fn->minor_hash == fname->minor_hash)) {
229 +                       new_fn->next = fname->next;
230 +                       fname->next = new_fn;
231 +                       return 0;
232 +               }
233 +                       
234 +               if (new_fn->hash < fname->hash)
235 +                       p = &(*p)->rb_left;
236 +               else if (new_fn->hash > fname->hash)
237 +                       p = &(*p)->rb_right;
238 +               else if (new_fn->minor_hash < fname->minor_hash)
239 +                       p = &(*p)->rb_left;
240 +               else /* if (new_fn->minor_hash > fname->minor_hash) */
241 +                       p = &(*p)->rb_right;
242 +       }
243 +
244 +       rb_link_node(&new_fn->rb_hash, parent, p);
245 +       rb_insert_color(&new_fn->rb_hash, &info->root);
246 +       return 0;
247 +}
248 +
249 +
250 +
251 +/*
252 + * This is a helper function for ext3_dx_readdir.  It calls filldir
253 + * for all entres on the fname linked list.  (Normally there is only
254 + * one entry on the linked list, unless there are 62 bit hash collisions.)
255 + */
256 +static int call_filldir(struct file * filp, void * dirent,
257 +                       filldir_t filldir, struct fname *fname)
258 +{
259 +       struct dir_private_info *info = filp->private_data;
260 +       loff_t  curr_pos;
261 +       struct inode *inode = filp->f_dentry->d_inode;
262 +       struct super_block * sb;
263 +       int error;
264 +
265 +       sb = inode->i_sb;
266 +       
267 +       if (!fname) {
268 +               printk("call_filldir: called with null fname?!?\n");
269 +               return 0;
270 +       }
271 +       curr_pos = hash2pos(fname->hash, fname->minor_hash);
272 +       while (fname) {
273 +               error = filldir(dirent, fname->name,
274 +                               fname->name_len, curr_pos, 
275 +                               fname->inode,
276 +                               get_dtype(sb, fname->file_type));
277 +               if (error) {
278 +                       filp->f_pos = curr_pos;
279 +                       info->extra_fname = fname->next;
280 +                       return error;
281 +               }
282 +               fname = fname->next;
283 +       }
284 +       return 0;
285 +}
286 +
287 +static int ext3_dx_readdir(struct file * filp,
288 +                        void * dirent, filldir_t filldir)
289 +{
290 +       struct dir_private_info *info = filp->private_data;
291 +       struct inode *inode = filp->f_dentry->d_inode;
292 +       struct fname *fname;
293 +       int     ret;
294 +
295 +       if (!info) {
296 +               info = create_dir_info(filp->f_pos);
297 +               if (!info)
298 +                       return -ENOMEM;
299 +               filp->private_data = info;
300 +       }
301 +
302 +       /* Some one has messed with f_pos; reset the world */
303 +       if (info->last_pos != filp->f_pos) {
304 +               free_rb_tree_fname(&info->root);
305 +               info->curr_node = 0;
306 +               info->extra_fname = 0;
307 +               info->curr_hash = pos2maj_hash(filp->f_pos);
308 +               info->curr_minor_hash = pos2min_hash(filp->f_pos);
309 +       }
310 +
311 +       /*
312 +        * If there are any leftover names on the hash collision
313 +        * chain, return them first.
314 +        */
315 +       if (info->extra_fname &&
316 +           call_filldir(filp, dirent, filldir, info->extra_fname))
317 +               goto finished;
318 +
319 +       if (!info->curr_node)
320 +               info->curr_node = rb_get_first(&info->root);
321 +
322 +       while (1) {
323 +               /*
324 +                * Fill the rbtree if we have no more entries,
325 +                * or the inode has changed since we last read in the
326 +                * cached entries. 
327 +                */
328 +               if ((!info->curr_node) ||
329 +                   (filp->f_version != inode->i_version)) {
330 +                       info->curr_node = 0;
331 +                       free_rb_tree_fname(&info->root);
332 +                       filp->f_version = inode->i_version;
333 +                       ret = ext3_htree_fill_tree(filp, info->curr_hash,
334 +                                                  info->curr_minor_hash,
335 +                                                  &info->next_hash);
336 +                       if (ret < 0)
337 +                               return ret;
338 +                       if (ret == 0)
339 +                               break;
340 +                       info->curr_node = rb_get_first(&info->root);
341 +               }
342 +
343 +               fname = rb_entry(info->curr_node, struct fname, rb_hash);
344 +               info->curr_hash = fname->hash;
345 +               info->curr_minor_hash = fname->minor_hash;
346 +               if (call_filldir(filp, dirent, filldir, fname))
347 +                       break;
348 +
349 +               info->curr_node = rb_get_next(info->curr_node);
350 +               if (!info->curr_node) {
351 +                       info->curr_hash = info->next_hash;
352 +                       info->curr_minor_hash = 0;
353 +               }
354 +       }
355 +finished:
356 +       info->last_pos = filp->f_pos;
357 +       UPDATE_ATIME(inode);
358 +       return 0;
359 +}
360 +#endif
361 --- linux-2.4.22-ac1/fs/ext3/file.c~ext3-htree-2.4.22-rh        2003-08-25 15:44:43.000000000 +0400
362 +++ linux-2.4.22-ac1-alexey/fs/ext3/file.c      2003-09-25 14:55:12.000000000 +0400
363 @@ -35,6 +35,9 @@ static int ext3_release_file (struct ino
364  {
365         if (filp->f_mode & FMODE_WRITE)
366                 ext3_discard_prealloc (inode);
367 +       if (is_dx(inode) && filp->private_data)
368 +               ext3_htree_free_dir_info(filp->private_data);
369 +
370         return 0;
371  }
372  
373 --- /dev/null   2003-01-30 13:24:37.000000000 +0300
374 +++ linux-2.4.22-ac1-alexey/fs/ext3/hash.c      2003-09-25 14:55:12.000000000 +0400
375 @@ -0,0 +1,215 @@
376 +/*
377 + *  linux/fs/ext3/hash.c
378 + *
379 + * Copyright (C) 2002 by Theodore Ts'o
380 + *
381 + * This file is released under the GPL v2.
382 + * 
383 + * This file may be redistributed under the terms of the GNU Public
384 + * License.
385 + */
386 +
387 +#include <linux/fs.h>
388 +#include <linux/jbd.h>
389 +#include <linux/sched.h>
390 +#include <linux/ext3_fs.h>
391 +
392 +#define DELTA 0x9E3779B9
393 +
394 +static void TEA_transform(__u32 buf[4], __u32 const in[])
395 +{
396 +       __u32   sum = 0;
397 +       __u32   b0 = buf[0], b1 = buf[1];
398 +       __u32   a = in[0], b = in[1], c = in[2], d = in[3];
399 +       int     n = 16;
400 +
401 +       do {                                                    
402 +               sum += DELTA;                                   
403 +               b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); 
404 +               b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); 
405 +       } while(--n);
406 +
407 +       buf[0] += b0;
408 +       buf[1] += b1;
409 +}
410 +
411 +/* F, G and H are basic MD4 functions: selection, majority, parity */
412 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
413 +#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
414 +#define H(x, y, z) ((x) ^ (y) ^ (z))
415 +
416 +/*
417 + * The generic round function.  The application is so specific that
418 + * we don't bother protecting all the arguments with parens, as is generally
419 + * good macro practice, in favor of extra legibility.
420 + * Rotation is separate from addition to prevent recomputation
421 + */
422 +#define ROUND(f, a, b, c, d, x, s)     \
423 +       (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
424 +#define K1 0
425 +#define K2 013240474631UL
426 +#define K3 015666365641UL
427 +
428 +/*
429 + * Basic cut-down MD4 transform.  Returns only 32 bits of result.
430 + */
431 +static void halfMD4Transform (__u32 buf[4], __u32 const in[])
432 +{
433 +       __u32   a = buf[0], b = buf[1], c = buf[2], d = buf[3];
434 +
435 +       /* Round 1 */
436 +       ROUND(F, a, b, c, d, in[0] + K1,  3);
437 +       ROUND(F, d, a, b, c, in[1] + K1,  7);
438 +       ROUND(F, c, d, a, b, in[2] + K1, 11);
439 +       ROUND(F, b, c, d, a, in[3] + K1, 19);
440 +       ROUND(F, a, b, c, d, in[4] + K1,  3);
441 +       ROUND(F, d, a, b, c, in[5] + K1,  7);
442 +       ROUND(F, c, d, a, b, in[6] + K1, 11);
443 +       ROUND(F, b, c, d, a, in[7] + K1, 19);
444 +
445 +       /* Round 2 */
446 +       ROUND(G, a, b, c, d, in[1] + K2,  3);
447 +       ROUND(G, d, a, b, c, in[3] + K2,  5);
448 +       ROUND(G, c, d, a, b, in[5] + K2,  9);
449 +       ROUND(G, b, c, d, a, in[7] + K2, 13);
450 +       ROUND(G, a, b, c, d, in[0] + K2,  3);
451 +       ROUND(G, d, a, b, c, in[2] + K2,  5);
452 +       ROUND(G, c, d, a, b, in[4] + K2,  9);
453 +       ROUND(G, b, c, d, a, in[6] + K2, 13);
454 +
455 +       /* Round 3 */
456 +       ROUND(H, a, b, c, d, in[3] + K3,  3);
457 +       ROUND(H, d, a, b, c, in[7] + K3,  9);
458 +       ROUND(H, c, d, a, b, in[2] + K3, 11);
459 +       ROUND(H, b, c, d, a, in[6] + K3, 15);
460 +       ROUND(H, a, b, c, d, in[1] + K3,  3);
461 +       ROUND(H, d, a, b, c, in[5] + K3,  9);
462 +       ROUND(H, c, d, a, b, in[0] + K3, 11);
463 +       ROUND(H, b, c, d, a, in[4] + K3, 15);
464 +
465 +       buf[0] += a;
466 +       buf[1] += b;
467 +       buf[2] += c;
468 +       buf[3] += d;
469 +}
470 +
471 +#undef ROUND
472 +#undef F
473 +#undef G
474 +#undef H
475 +#undef K1
476 +#undef K2
477 +#undef K3
478 +
479 +/* The old legacy hash */
480 +static __u32 dx_hack_hash (const char *name, int len)
481 +{
482 +       __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
483 +       while (len--) {
484 +               __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
485 +               
486 +               if (hash & 0x80000000) hash -= 0x7fffffff;
487 +               hash1 = hash0;
488 +               hash0 = hash;
489 +       }
490 +       return (hash0 << 1);
491 +}
492 +
493 +static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
494 +{
495 +       __u32   pad, val;
496 +       int     i;
497 +
498 +       pad = (__u32)len | ((__u32)len << 8);
499 +       pad |= pad << 16;
500 +
501 +       val = pad;
502 +       if (len > num*4)
503 +               len = num * 4;
504 +       for (i=0; i < len; i++) {
505 +               if ((i % 4) == 0)
506 +                       val = pad;
507 +               val = msg[i] + (val << 8);
508 +               if ((i % 4) == 3) {
509 +                       *buf++ = val;
510 +                       val = pad;
511 +                       num--;
512 +               }
513 +       }
514 +       if (--num >= 0)
515 +               *buf++ = val;
516 +       while (--num >= 0)
517 +               *buf++ = pad;
518 +}
519 +
520 +/*
521 + * Returns the hash of a filename.  If len is 0 and name is NULL, then
522 + * this function can be used to test whether or not a hash version is
523 + * supported.
524 + * 
525 + * The seed is an 4 longword (32 bits) "secret" which can be used to
526 + * uniquify a hash.  If the seed is all zero's, then some default seed
527 + * may be used.
528 + * 
529 + * A particular hash version specifies whether or not the seed is
530 + * represented, and whether or not the returned hash is 32 bits or 64
531 + * bits.  32 bit hashes will return 0 for the minor hash.
532 + */
533 +int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
534 +{
535 +       __u32   hash;
536 +       __u32   minor_hash = 0;
537 +       const char      *p;
538 +       int             i;
539 +       __u32           in[8], buf[4];
540 +
541 +       /* Initialize the default seed for the hash checksum functions */
542 +       buf[0] = 0x67452301;
543 +       buf[1] = 0xefcdab89;
544 +       buf[2] = 0x98badcfe;
545 +       buf[3] = 0x10325476;
546 +
547 +       /* Check to see if the seed is all zero's */
548 +       if (hinfo->seed) {
549 +               for (i=0; i < 4; i++) {
550 +                       if (hinfo->seed[i])
551 +                               break;
552 +               }
553 +               if (i < 4)
554 +                       memcpy(buf, hinfo->seed, sizeof(buf));
555 +       }
556 +               
557 +       switch (hinfo->hash_version) {
558 +       case DX_HASH_LEGACY:
559 +               hash = dx_hack_hash(name, len);
560 +               break;
561 +       case DX_HASH_HALF_MD4:
562 +               p = name;
563 +               while (len > 0) {
564 +                       str2hashbuf(p, len, in, 8);
565 +                       halfMD4Transform(buf, in);
566 +                       len -= 32;
567 +                       p += 32;
568 +               }
569 +               minor_hash = buf[2];
570 +               hash = buf[1];
571 +               break;
572 +       case DX_HASH_TEA:
573 +               p = name;
574 +               while (len > 0) {
575 +                       str2hashbuf(p, len, in, 4);
576 +                       TEA_transform(buf, in);
577 +                       len -= 16;
578 +                       p += 16;
579 +               }
580 +               hash = buf[0];
581 +               minor_hash = buf[1];
582 +               break;
583 +       default:
584 +               hinfo->hash = 0;
585 +               return -1;
586 +       }
587 +       hinfo->hash = hash & ~1;
588 +       hinfo->minor_hash = minor_hash;
589 +       return 0;
590 +}
591 --- linux-2.4.22-ac1/fs/ext3/Makefile~ext3-htree-2.4.22-rh      2003-09-25 14:39:01.000000000 +0400
592 +++ linux-2.4.22-ac1-alexey/fs/ext3/Makefile    2003-09-25 14:55:12.000000000 +0400
593 @@ -12,7 +12,7 @@ O_TARGET := ext3.o
594  export-objs := super.o inode.o
595  
596  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
597 -               ioctl.o namei.o super.o symlink.o
598 +               ioctl.o namei.o super.o symlink.o hash.o
599  obj-m    := $(O_TARGET)
600  
601  include $(TOPDIR)/Rules.make
602 --- linux-2.4.22-ac1/fs/ext3/namei.c~ext3-htree-2.4.22-rh       2003-09-25 14:16:29.000000000 +0400
603 +++ linux-2.4.22-ac1-alexey/fs/ext3/namei.c     2003-09-25 14:58:37.000000000 +0400
604 @@ -16,6 +16,12 @@
605   *        David S. Miller (davem@caip.rutgers.edu), 1995
606   *  Directory entry file type support and forward compatibility hooks
607   *     for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
608 + *  Hash Tree Directory indexing (c)
609 + *     Daniel Phillips, 2001
610 + *  Hash Tree Directory indexing porting
611 + *     Christopher Li, 2002
612 + *  Hash Tree Directory indexing cleanup
613 + *     Theodore Ts'o, 2002
614   */
615  
616  #include <linux/fs.h>
617 @@ -38,6 +44,642 @@
618  #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
619  #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
620  
621 +static struct buffer_head *ext3_append(handle_t *handle,
622 +                                       struct inode *inode,
623 +                                       u32 *block, int *err)
624 +{
625 +       struct buffer_head *bh;
626 +
627 +       *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
628 +
629 +       if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
630 +               inode->i_size += inode->i_sb->s_blocksize;
631 +               EXT3_I(inode)->i_disksize = inode->i_size;
632 +               ext3_journal_get_write_access(handle,bh);
633 +       }
634 +       return bh;
635 +}
636 +
637 +#ifndef assert
638 +#define assert(test) J_ASSERT(test)
639 +#endif
640 +
641 +#ifndef swap
642 +#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
643 +#endif
644 +
645 +typedef struct { u32 v; } le_u32;
646 +typedef struct { u16 v; } le_u16;
647 +
648 +#ifdef DX_DEBUG
649 +#define dxtrace(command) command
650 +#else
651 +#define dxtrace(command) 
652 +#endif
653 +
654 +struct fake_dirent
655 +{
656 +       /*le*/u32 inode;
657 +       /*le*/u16 rec_len;
658 +       u8 name_len;
659 +       u8 file_type;
660 +};
661 +
662 +struct dx_countlimit
663 +{
664 +       le_u16 limit;
665 +       le_u16 count;
666 +};
667 +
668 +struct dx_entry
669 +{
670 +       le_u32 hash;
671 +       le_u32 block;
672 +};
673 +
674 +/*
675 + * dx_root_info is laid out so that if it should somehow get overlaid by a
676 + * dirent the two low bits of the hash version will be zero.  Therefore, the
677 + * hash version mod 4 should never be 0.  Sincerely, the paranoia department.
678 + */
679 +
680 +struct dx_root
681 +{
682 +       struct fake_dirent dot;
683 +       char dot_name[4];
684 +       struct fake_dirent dotdot;
685 +       char dotdot_name[4];
686 +       struct dx_root_info
687 +       {
688 +               le_u32 reserved_zero;
689 +               u8 hash_version;
690 +               u8 info_length; /* 8 */
691 +               u8 indirect_levels;
692 +               u8 unused_flags;
693 +       }
694 +       info;
695 +       struct dx_entry entries[0];
696 +};
697 +
698 +struct dx_node
699 +{
700 +       struct fake_dirent fake;
701 +       struct dx_entry entries[0];
702 +};
703 +
704 +
705 +struct dx_frame
706 +{
707 +       struct buffer_head *bh;
708 +       struct dx_entry *entries;
709 +       struct dx_entry *at;
710 +};
711 +
712 +struct dx_map_entry
713 +{
714 +       u32 hash;
715 +       u32 offs;
716 +};
717 +
718 +#ifdef CONFIG_EXT3_INDEX
719 +static inline unsigned dx_get_block (struct dx_entry *entry);
720 +static void dx_set_block (struct dx_entry *entry, unsigned value);
721 +static inline unsigned dx_get_hash (struct dx_entry *entry);
722 +static void dx_set_hash (struct dx_entry *entry, unsigned value);
723 +static unsigned dx_get_count (struct dx_entry *entries);
724 +static unsigned dx_get_limit (struct dx_entry *entries);
725 +static void dx_set_count (struct dx_entry *entries, unsigned value);
726 +static void dx_set_limit (struct dx_entry *entries, unsigned value);
727 +static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
728 +static unsigned dx_node_limit (struct inode *dir);
729 +static struct dx_frame *dx_probe(struct dentry *dentry,
730 +                                struct inode *dir,
731 +                                struct dx_hash_info *hinfo,
732 +                                struct dx_frame *frame,
733 +                                int *err);
734 +static void dx_release (struct dx_frame *frames);
735 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
736 +                       struct dx_hash_info *hinfo, struct dx_map_entry map[]);
737 +static void dx_sort_map(struct dx_map_entry *map, unsigned count);
738 +static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
739 +               struct dx_map_entry *offsets, int count);
740 +static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
741 +static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
742 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
743 +                                struct dx_frame *frame,
744 +                                struct dx_frame *frames, int *err,
745 +                                __u32 *start_hash);
746 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
747 +                      struct ext3_dir_entry_2 **res_dir, int *err);
748 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
749 +                            struct inode *inode);
750 +
751 +/*
752 + * Future: use high four bits of block for coalesce-on-delete flags
753 + * Mask them off for now.
754 + */
755 +
756 +static inline unsigned dx_get_block (struct dx_entry *entry)
757 +{
758 +       return le32_to_cpu(entry->block.v) & 0x00ffffff;
759 +}
760 +
761 +static inline void dx_set_block (struct dx_entry *entry, unsigned value)
762 +{
763 +       entry->block.v = cpu_to_le32(value);
764 +}
765 +
766 +static inline unsigned dx_get_hash (struct dx_entry *entry)
767 +{
768 +       return le32_to_cpu(entry->hash.v);
769 +}
770 +
771 +static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
772 +{
773 +       entry->hash.v = cpu_to_le32(value);
774 +}
775 +
776 +static inline unsigned dx_get_count (struct dx_entry *entries)
777 +{
778 +       return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
779 +}
780 +
781 +static inline unsigned dx_get_limit (struct dx_entry *entries)
782 +{
783 +       return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
784 +}
785 +
786 +static inline void dx_set_count (struct dx_entry *entries, unsigned value)
787 +{
788 +       ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
789 +}
790 +
791 +static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
792 +{
793 +       ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
794 +}
795 +
796 +static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
797 +{
798 +       unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
799 +               EXT3_DIR_REC_LEN(2) - infosize;
800 +       return 0? 20: entry_space / sizeof(struct dx_entry);
801 +}
802 +
803 +static inline unsigned dx_node_limit (struct inode *dir)
804 +{
805 +       unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
806 +       return 0? 22: entry_space / sizeof(struct dx_entry);
807 +}
808 +
809 +/*
810 + * Debug
811 + */
812 +#ifdef DX_DEBUG
813 +struct stats
814 +{ 
815 +       unsigned names;
816 +       unsigned space;
817 +       unsigned bcount;
818 +};
819 +
820 +static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
821 +                                int size, int show_names)
822 +{
823 +       unsigned names = 0, space = 0;
824 +       char *base = (char *) de;
825 +       struct dx_hash_info h = *hinfo;
826 +       
827 +       printk("names: ");
828 +       while ((char *) de < base + size)
829 +       {
830 +               if (de->inode)
831 +               {
832 +                       if (show_names)
833 +                       {
834 +                               int len = de->name_len;
835 +                               char *name = de->name;
836 +                               while (len--) printk("%c", *name++);
837 +                               ext3fs_dirhash(de->name, de->name_len, &h);
838 +                               printk(":%x.%u ", h.hash,
839 +                                      ((char *) de - base));
840 +                       }
841 +                       space += EXT3_DIR_REC_LEN(de->name_len);
842 +                       names++;
843 +               }
844 +               de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
845 +       }
846 +       printk("(%i)\n", names);
847 +       return (struct stats) { names, space, 1 };
848 +}
849 +
850 +struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
851 +                            struct dx_entry *entries, int levels)
852 +{
853 +       unsigned blocksize = dir->i_sb->s_blocksize;
854 +       unsigned count = dx_get_count (entries), names = 0, space = 0, i;
855 +       unsigned bcount = 0;
856 +       struct buffer_head *bh;
857 +       int err;
858 +       printk("%i indexed blocks...\n", count);
859 +       for (i = 0; i < count; i++, entries++)
860 +       {
861 +               u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
862 +               u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
863 +               struct stats stats;
864 +               printk("%s%3u:%03u hash %8x/%8x ",levels?"":"   ", i, block, hash, range);
865 +               if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
866 +               stats = levels?
867 +                  dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
868 +                  dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
869 +               names += stats.names;
870 +               space += stats.space;
871 +               bcount += stats.bcount;
872 +               brelse (bh);
873 +       }
874 +       if (bcount)
875 +               printk("%snames %u, fullness %u (%u%%)\n", levels?"":"   ",
876 +                       names, space/bcount,(space/bcount)*100/blocksize);
877 +       return (struct stats) { names, space, bcount};
878 +}
879 +#endif /* DX_DEBUG */
880 +
881 +/*
882 + * Probe for a directory leaf block to search.
883 + *
884 + * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
885 + * error in the directory index, and the caller should fall back to
886 + * searching the directory normally.  The callers of dx_probe **MUST**
887 + * check for this error code, and make sure it never gets reflected
888 + * back to userspace.
889 + */
890 +static struct dx_frame *
891 +dx_probe(struct dentry *dentry, struct inode *dir,
892 +        struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
893 +{
894 +       unsigned count, indirect;
895 +       struct dx_entry *at, *entries, *p, *q, *m;
896 +       struct dx_root *root;
897 +       struct buffer_head *bh;
898 +       struct dx_frame *frame = frame_in;
899 +       u32 hash;
900 +
901 +       frame->bh = NULL;
902 +       if (dentry)
903 +               dir = dentry->d_parent->d_inode;
904 +       if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
905 +               goto fail;
906 +       root = (struct dx_root *) bh->b_data;
907 +       if (root->info.hash_version != DX_HASH_TEA &&
908 +           root->info.hash_version != DX_HASH_HALF_MD4 &&
909 +           root->info.hash_version != DX_HASH_LEGACY) {
910 +               ext3_warning(dir->i_sb, __FUNCTION__,
911 +                            "Unrecognised inode hash code %d",
912 +                            root->info.hash_version);
913 +               brelse(bh);
914 +               *err = ERR_BAD_DX_DIR;
915 +               goto fail;
916 +       }
917 +       hinfo->hash_version = root->info.hash_version;
918 +       hinfo->seed = dir->i_sb->u.ext3_sb.s_hash_seed;
919 +       if (dentry)
920 +               ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
921 +       hash = hinfo->hash;
922 +
923 +       if (root->info.unused_flags & 1) {
924 +               ext3_warning(dir->i_sb, __FUNCTION__,
925 +                            "Unimplemented inode hash flags: %#06x",
926 +                            root->info.unused_flags);
927 +               brelse(bh);
928 +               *err = ERR_BAD_DX_DIR;
929 +               goto fail;
930 +       }
931 +
932 +       if ((indirect = root->info.indirect_levels) > 1) {
933 +               ext3_warning(dir->i_sb, __FUNCTION__,
934 +                            "Unimplemented inode hash depth: %#06x",
935 +                            root->info.indirect_levels);
936 +               brelse(bh);
937 +               *err = ERR_BAD_DX_DIR;
938 +               goto fail;
939 +       }
940 +
941 +       entries = (struct dx_entry *) (((char *)&root->info) +
942 +                                      root->info.info_length);
943 +       assert(dx_get_limit(entries) == dx_root_limit(dir,
944 +                                                     root->info.info_length));
945 +       dxtrace (printk("Look up %x", hash));
946 +       while (1)
947 +       {
948 +               count = dx_get_count(entries);
949 +               assert (count && count <= dx_get_limit(entries));
950 +               p = entries + 1;
951 +               q = entries + count - 1;
952 +               while (p <= q)
953 +               {
954 +                       m = p + (q - p)/2;
955 +                       dxtrace(printk("."));
956 +                       if (dx_get_hash(m) > hash)
957 +                               q = m - 1;
958 +                       else
959 +                               p = m + 1;
960 +               }
961 +
962 +               if (0) // linear search cross check
963 +               {
964 +                       unsigned n = count - 1;
965 +                       at = entries;
966 +                       while (n--)
967 +                       {
968 +                               dxtrace(printk(","));
969 +                               if (dx_get_hash(++at) > hash)
970 +                               {
971 +                                       at--;
972 +                                       break;
973 +                               }
974 +                       }
975 +                       assert (at == p - 1);
976 +               }
977 +
978 +               at = p - 1;
979 +               dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
980 +               frame->bh = bh;
981 +               frame->entries = entries;
982 +               frame->at = at;
983 +               if (!indirect--) return frame;
984 +               if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
985 +                       goto fail2;
986 +               at = entries = ((struct dx_node *) bh->b_data)->entries;
987 +               assert (dx_get_limit(entries) == dx_node_limit (dir));
988 +               frame++;
989 +       }
990 +fail2:
991 +       while (frame >= frame_in) {
992 +               brelse(frame->bh);
993 +               frame--;
994 +       }
995 +fail:
996 +       return NULL;
997 +}
998 +
999 +static void dx_release (struct dx_frame *frames)
1000 +{
1001 +       if (frames[0].bh == NULL)
1002 +               return;
1003 +
1004 +       if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
1005 +               brelse(frames[1].bh);
1006 +       brelse(frames[0].bh);
1007 +}
1008 +
1009 +/*
1010 + * This function increments the frame pointer to search the next leaf
1011 + * block, and reads in the necessary intervening nodes if the search
1012 + * should be necessary.  Whether or not the search is necessary is
1013 + * controlled by the hash parameter.  If the hash value is even, then
1014 + * the search is only continued if the next block starts with that
1015 + * hash value.  This is used if we are searching for a specific file.
1016 + *
1017 + * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1018 + *
1019 + * This function returns 1 if the caller should continue to search,
1020 + * or 0 if it should not.  If there is an error reading one of the
1021 + * index blocks, it will return -1.
1022 + *
1023 + * If start_hash is non-null, it will be filled in with the starting
1024 + * hash of the next page.
1025 + */
1026 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
1027 +                                struct dx_frame *frame,
1028 +                                struct dx_frame *frames, int *err,
1029 +                                __u32 *start_hash)
1030 +{
1031 +       struct dx_frame *p;
1032 +       struct buffer_head *bh;
1033 +       int num_frames = 0;
1034 +       __u32 bhash;
1035 +
1036 +       *err = ENOENT;
1037 +       p = frame;
1038 +       /*
1039 +        * Find the next leaf page by incrementing the frame pointer.
1040 +        * If we run out of entries in the interior node, loop around and
1041 +        * increment pointer in the parent node.  When we break out of
1042 +        * this loop, num_frames indicates the number of interior
1043 +        * nodes need to be read.
1044 +        */
1045 +       while (1) {
1046 +               if (++(p->at) < p->entries + dx_get_count(p->entries))
1047 +                       break;
1048 +               if (p == frames)
1049 +                       return 0;
1050 +               num_frames++;
1051 +               p--;
1052 +       }
1053 +
1054 +       /*
1055 +        * If the hash is 1, then continue only if the next page has a
1056 +        * continuation hash of any value.  This is used for readdir
1057 +        * handling.  Otherwise, check to see if the hash matches the
1058 +        * desired contiuation hash.  If it doesn't, return since
1059 +        * there's no point to read in the successive index pages.
1060 +        */
1061 +       bhash = dx_get_hash(p->at);
1062 +       if (start_hash)
1063 +               *start_hash = bhash;
1064 +       if ((hash & 1) == 0) {
1065 +               if ((bhash & ~1) != hash)
1066 +                       return 0;
1067 +       }
1068 +       /*
1069 +        * If the hash is HASH_NB_ALWAYS, we always go to the next
1070 +        * block so no check is necessary
1071 +        */
1072 +       while (num_frames--) {
1073 +               if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
1074 +                                     0, err)))
1075 +                       return -1; /* Failure */
1076 +               p++;
1077 +               brelse (p->bh);
1078 +               p->bh = bh;
1079 +               p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1080 +       }
1081 +       return 1;
1082 +}
1083 +
1084 +
1085 +/*
1086 + * p is at least 6 bytes before the end of page
1087 + */
1088 +static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
1089 +{
1090 +       return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
1091 +}
1092 +
1093 +/*
1094 + * This function fills a red-black tree with information from a
1095 + * directory.  We start scanning the directory in hash order, starting
1096 + * at start_hash and start_minor_hash.
1097 + *
1098 + * This function returns the number of entries inserted into the tree,
1099 + * or a negative error code.
1100 + */
1101 +int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1102 +                        __u32 start_minor_hash, __u32 *next_hash)
1103 +{
1104 +       struct dx_hash_info hinfo;
1105 +       struct buffer_head *bh;
1106 +       struct ext3_dir_entry_2 *de, *top;
1107 +       static struct dx_frame frames[2], *frame;
1108 +       struct inode *dir;
1109 +       int block, err;
1110 +       int count = 0;
1111 +       int ret;
1112 +       __u32 hashval;
1113 +       
1114 +       dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
1115 +                      start_minor_hash));
1116 +       dir = dir_file->f_dentry->d_inode;
1117 +       hinfo.hash = start_hash;
1118 +       hinfo.minor_hash = 0;
1119 +       frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
1120 +       if (!frame)
1121 +               return err;
1122 +
1123 +       /* Add '.' and '..' from the htree header */
1124 +       if (!start_hash && !start_minor_hash) {
1125 +               de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
1126 +               if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1127 +                       goto errout;
1128 +               de = ext3_next_entry(de);
1129 +               if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1130 +                       goto errout;
1131 +               count += 2;
1132 +       }
1133 +
1134 +       while (1) {
1135 +               block = dx_get_block(frame->at);
1136 +               dxtrace(printk("Reading block %d\n", block));
1137 +               if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
1138 +                       goto errout;
1139 +       
1140 +               de = (struct ext3_dir_entry_2 *) bh->b_data;
1141 +               top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
1142 +                                      EXT3_DIR_REC_LEN(0));
1143 +               for (; de < top; de = ext3_next_entry(de)) {
1144 +                       ext3fs_dirhash(de->name, de->name_len, &hinfo);
1145 +                       if ((hinfo.hash < start_hash) ||
1146 +                           ((hinfo.hash == start_hash) &&
1147 +                            (hinfo.minor_hash < start_minor_hash)))
1148 +                               continue;
1149 +                       if ((err = ext3_htree_store_dirent(dir_file,
1150 +                                  hinfo.hash, hinfo.minor_hash, de)) != 0)
1151 +                               goto errout;
1152 +                       count++;
1153 +               }
1154 +               brelse (bh);
1155 +               hashval = ~1;
1156 +               ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS, 
1157 +                                           frame, frames, &err, &hashval);
1158 +               if (next_hash)
1159 +                       *next_hash = hashval;
1160 +               if (ret == -1)
1161 +                       goto errout;
1162 +               /*
1163 +                * Stop if:  (a) there are no more entries, or
1164 +                * (b) we have inserted at least one entry and the
1165 +                * next hash value is not a continuation
1166 +                */
1167 +               if ((ret == 0) ||
1168 +                   (count && ((hashval & 1) == 0)))
1169 +                       break;
1170 +       }
1171 +       dx_release(frames);
1172 +       dxtrace(printk("Fill tree: returned %d entries\n", count));
1173 +       return count;
1174 +errout:
1175 +       dx_release(frames);
1176 +       return (err);
1177 +}
1178 +
1179 +
1180 +/*
1181 + * Directory block splitting, compacting
1182 + */
1183 +
1184 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
1185 +                       struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
1186 +{
1187 +       int count = 0;
1188 +       char *base = (char *) de;
1189 +       struct dx_hash_info h = *hinfo;
1190 +       
1191 +       while ((char *) de < base + size)
1192 +       {
1193 +               if (de->name_len && de->inode) {
1194 +                       ext3fs_dirhash(de->name, de->name_len, &h);
1195 +                       map_tail--;
1196 +                       map_tail->hash = h.hash;
1197 +                       map_tail->offs = (u32) ((char *) de - base);
1198 +                       count++;
1199 +               }
1200 +               /* XXX: do we need to check rec_len == 0 case? -Chris */
1201 +               de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
1202 +       }
1203 +       return count;
1204 +}
1205 +
1206 +static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1207 +{
1208 +       struct dx_map_entry *p, *q, *top = map + count - 1;
1209 +       int more;
1210 +       /* Combsort until bubble sort doesn't suck */
1211 +       while (count > 2)
1212 +       {
1213 +               count = count*10/13;
1214 +               if (count - 9 < 2) /* 9, 10 -> 11 */
1215 +                       count = 11;
1216 +               for (p = top, q = p - count; q >= map; p--, q--)
1217 +                       if (p->hash < q->hash)
1218 +                               swap(*p, *q);
1219 +       }
1220 +       /* Garden variety bubble sort */
1221 +       do {
1222 +               more = 0;
1223 +               q = top;
1224 +               while (q-- > map)
1225 +               {
1226 +                       if (q[1].hash >= q[0].hash)
1227 +                               continue;
1228 +                       swap(*(q+1), *q);
1229 +                       more = 1;
1230 +               }
1231 +       } while(more);
1232 +}
1233 +
1234 +static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
1235 +{
1236 +       struct dx_entry *entries = frame->entries;
1237 +       struct dx_entry *old = frame->at, *new = old + 1;
1238 +       int count = dx_get_count(entries);
1239 +
1240 +       assert(count < dx_get_limit(entries));
1241 +       assert(old < entries + count);
1242 +       memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1243 +       dx_set_hash(new, hash);
1244 +       dx_set_block(new, block);
1245 +       dx_set_count(entries, count + 1);
1246 +}
1247 +#endif
1248 +
1249 +
1250 +static void ext3_update_dx_flag(struct inode *inode)
1251 +{
1252 +       if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
1253 +                                    EXT3_FEATURE_COMPAT_DIR_INDEX))
1254 +               EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
1255 +}
1256 +
1257  /*
1258   * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
1259   *
1260 @@ -94,6 +736,7 @@ static int inline search_dirblock(struct
1261         return 0;
1262  }
1263  
1264 +
1265  /*
1266   *     ext3_find_entry()
1267   *
1268 @@ -105,6 +748,8 @@ static int inline search_dirblock(struct
1269   * The returned buffer_head has ->b_count elevated.  The caller is expected
1270   * to brelse() it when appropriate.
1271   */
1272 +
1273 +       
1274  static struct buffer_head * ext3_find_entry (struct dentry *dentry,
1275                                         struct ext3_dir_entry_2 ** res_dir)
1276  {
1277 @@ -119,12 +764,32 @@ static struct buffer_head * ext3_find_en
1278         int num = 0;
1279         int nblocks, i, err;
1280         struct inode *dir = dentry->d_parent->d_inode;
1281 +       int namelen;
1282 +       const u8 *name;
1283 +       unsigned blocksize;
1284  
1285         *res_dir = NULL;
1286         sb = dir->i_sb;
1287 -
1288 +       blocksize = sb->s_blocksize;
1289 +       namelen = dentry->d_name.len;
1290 +       name = dentry->d_name.name;
1291 +       if (namelen > EXT3_NAME_LEN)
1292 +               return NULL;
1293 +#ifdef CONFIG_EXT3_INDEX
1294 +       if (is_dx(dir)) {
1295 +               bh = ext3_dx_find_entry(dentry, res_dir, &err);
1296 +               /*
1297 +                * On success, or if the error was file not found,
1298 +                * return.  Otherwise, fall back to doing a search the
1299 +                * old fashioned way.
1300 +                */
1301 +               if (bh || (err != ERR_BAD_DX_DIR))
1302 +                       return bh;
1303 +               dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
1304 +       }
1305 +#endif
1306         nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
1307 -       start = dir->u.ext3_i.i_dir_start_lookup;
1308 +       start = EXT3_I(dir)->i_dir_start_lookup;
1309         if (start >= nblocks)
1310                 start = 0;
1311         block = start;
1312 @@ -166,7 +831,7 @@ restart:
1313                 i = search_dirblock(bh, dir, dentry,
1314                             block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
1315                 if (i == 1) {
1316 -                       dir->u.ext3_i.i_dir_start_lookup = block;
1317 +                       EXT3_I(dir)->i_dir_start_lookup = block;
1318                         ret = bh;
1319                         goto cleanup_and_exit;
1320                 } else {
1321 @@ -197,6 +862,74 @@ cleanup_and_exit:
1322         return ret;
1323  }
1324  
1325 +#ifdef CONFIG_EXT3_INDEX
1326 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
1327 +                      struct ext3_dir_entry_2 **res_dir, int *err)
1328 +{
1329 +       struct super_block * sb;
1330 +       struct dx_hash_info     hinfo;
1331 +       u32 hash;
1332 +       struct dx_frame frames[2], *frame;
1333 +       struct ext3_dir_entry_2 *de, *top;
1334 +       struct buffer_head *bh;
1335 +       unsigned long block;
1336 +       int retval;
1337 +       int namelen = dentry->d_name.len;
1338 +       const u8 *name = dentry->d_name.name;
1339 +       struct inode *dir = dentry->d_parent->d_inode;
1340 +
1341 +       sb = dir->i_sb;
1342 +       /* NFS may look up ".." - look at dx_root directory block */
1343 +       if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
1344 +               if (!(frame = dx_probe(dentry, 0, &hinfo, frames, err)))
1345 +                       return NULL;
1346 +       } else {
1347 +               frame = frames;
1348 +               frame->bh = NULL;                       /* for dx_release() */
1349 +               frame->at = (struct dx_entry *)frames;  /* hack for zero entry*/
1350 +               dx_set_block(frame->at, 0);             /* dx_root block is 0 */
1351 +       }
1352 +       hash = hinfo.hash;
1353 +       do {
1354 +               block = dx_get_block(frame->at);
1355 +               if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1356 +                       goto errout;
1357 +               de = (struct ext3_dir_entry_2 *) bh->b_data;
1358 +               top = (struct ext3_dir_entry_2 *)((char *)de + sb->s_blocksize -
1359 +                                      EXT3_DIR_REC_LEN(0));
1360 +               for (; de < top; de = ext3_next_entry(de))
1361 +               if (ext3_match (namelen, name, de)) {
1362 +                       if (!ext3_check_dir_entry("ext3_find_entry",
1363 +                                                 dir, de, bh,
1364 +                                 (block<<EXT3_BLOCK_SIZE_BITS(sb))
1365 +                                         +((char *)de - bh->b_data))) {
1366 +                               brelse (bh);
1367 +                               goto errout;
1368 +                       }
1369 +                       *res_dir = de;
1370 +                       dx_release (frames);
1371 +                       return bh;
1372 +               }
1373 +               brelse (bh);
1374 +               /* Check to see if we should continue to search */
1375 +               retval = ext3_htree_next_block(dir, hash, frame,
1376 +                                              frames, err, 0);
1377 +               if (retval == -1) {
1378 +                       ext3_warning(sb, __FUNCTION__,
1379 +                            "error reading index page in directory #%lu",
1380 +                            dir->i_ino);
1381 +                       goto errout;
1382 +               }
1383 +       } while (retval == 1);
1384 +       
1385 +       *err = -ENOENT;
1386 +errout:
1387 +       dxtrace(printk("%s not found\n", name));
1388 +       dx_release (frames);
1389 +       return NULL;
1390 +}
1391 +#endif
1392 +
1393  static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1394  {
1395         struct inode * inode;
1396 @@ -213,8 +938,9 @@ static struct dentry *ext3_lookup(struct
1397                 brelse (bh);
1398                 inode = iget(dir->i_sb, ino);
1399  
1400 -               if (!inode)
1401 +               if (!inode) {
1402                         return ERR_PTR(-EACCES);
1403 +               }
1404         }
1405         d_add(dentry, inode);
1406         return NULL;
1407 @@ -238,6 +964,301 @@ static inline void ext3_set_de_type(stru
1408                 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1409  }
1410  
1411 +#ifdef CONFIG_EXT3_INDEX
1412 +static struct ext3_dir_entry_2 *
1413 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1414 +{
1415 +       unsigned rec_len = 0;
1416 +
1417 +       while (count--) {
1418 +               struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1419 +               rec_len = EXT3_DIR_REC_LEN(de->name_len);
1420 +               memcpy (to, de, rec_len);
1421 +               ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1422 +               de->inode = 0;
1423 +               map++;
1424 +               to += rec_len;
1425 +       }
1426 +       return (struct ext3_dir_entry_2 *) (to - rec_len);
1427 +}
1428 +
1429 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1430 +{
1431 +       struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1432 +       unsigned rec_len = 0;
1433 +
1434 +       prev = to = de;
1435 +       while ((char*)de < base + size) {
1436 +               next = (struct ext3_dir_entry_2 *) ((char *) de +
1437 +                                                   le16_to_cpu(de->rec_len));
1438 +               if (de->inode && de->name_len) {
1439 +                       rec_len = EXT3_DIR_REC_LEN(de->name_len);
1440 +                       if (de > to)
1441 +                               memmove(to, de, rec_len);
1442 +                       to->rec_len = cpu_to_le16(rec_len);
1443 +                       prev = to;
1444 +                       to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1445 +               }
1446 +               de = next;
1447 +       }
1448 +       return prev;
1449 +}
1450 +
1451 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1452 +                       struct buffer_head **bh,struct dx_frame *frame,
1453 +                       struct dx_hash_info *hinfo, int *error)
1454 +{
1455 +       unsigned blocksize = dir->i_sb->s_blocksize;
1456 +       unsigned count, continued;
1457 +       struct buffer_head *bh2;
1458 +       u32 newblock;
1459 +       u32 hash2;
1460 +       struct dx_map_entry *map;
1461 +       char *data1 = (*bh)->b_data, *data2;
1462 +       unsigned split;
1463 +       struct ext3_dir_entry_2 *de = NULL, *de2;
1464 +       int     err;
1465 +
1466 +       bh2 = ext3_append (handle, dir, &newblock, error);
1467 +       if (!(bh2)) {
1468 +               brelse(*bh);
1469 +               *bh = NULL;
1470 +               goto errout;
1471 +       }
1472 +
1473 +       BUFFER_TRACE(*bh, "get_write_access");
1474 +       err = ext3_journal_get_write_access(handle, *bh);
1475 +       if (err) {
1476 +       journal_error:
1477 +               brelse(*bh);
1478 +               brelse(bh2);
1479 +               *bh = NULL;
1480 +               ext3_std_error(dir->i_sb, err);
1481 +               goto errout;
1482 +       }
1483 +       BUFFER_TRACE(frame->bh, "get_write_access");
1484 +       err = ext3_journal_get_write_access(handle, frame->bh);
1485 +       if (err)
1486 +               goto journal_error;
1487 +
1488 +       data2 = bh2->b_data;
1489 +
1490 +       /* create map in the end of data2 block */
1491 +       map = (struct dx_map_entry *) (data2 + blocksize);
1492 +       count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1493 +                            blocksize, hinfo, map);
1494 +       map -= count;
1495 +       split = count/2; // need to adjust to actual middle
1496 +       dx_sort_map (map, count);
1497 +       hash2 = map[split].hash;
1498 +       continued = hash2 == map[split - 1].hash;
1499 +       dxtrace(printk("Split block %i at %x, %i/%i\n",
1500 +               dx_get_block(frame->at), hash2, split, count-split));
1501 +
1502 +       /* Fancy dance to stay within two buffers */
1503 +       de2 = dx_move_dirents(data1, data2, map + split, count - split);
1504 +       de = dx_pack_dirents(data1,blocksize);
1505 +       de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1506 +       de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1507 +       dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1508 +       dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1509 +
1510 +       /* Which block gets the new entry? */
1511 +       if (hinfo->hash >= hash2)
1512 +       {
1513 +               swap(*bh, bh2);
1514 +               de = de2;
1515 +       }
1516 +       dx_insert_block (frame, hash2 + continued, newblock);
1517 +       err = ext3_journal_dirty_metadata (handle, bh2);
1518 +       if (err)
1519 +               goto journal_error;
1520 +       err = ext3_journal_dirty_metadata (handle, frame->bh);
1521 +       if (err)
1522 +               goto journal_error;
1523 +       brelse (bh2);
1524 +       dxtrace(dx_show_index ("frame", frame->entries));
1525 +errout:
1526 +       return de;
1527 +}
1528 +#endif
1529 +
1530 +
1531 +/*
1532 + * Add a new entry into a directory (leaf) block.  If de is non-NULL,
1533 + * it points to a directory entry which is guaranteed to be large
1534 + * enough for new directory entry.  If de is NULL, then
1535 + * add_dirent_to_buf will attempt search the directory block for
1536 + * space.  It will return -ENOSPC if no space is available, and -EIO
1537 + * and -EEXIST if directory entry already exists.
1538 + * 
1539 + * NOTE!  bh is NOT released in the case where ENOSPC is returned.  In
1540 + * all other cases bh is released.
1541 + */
1542 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1543 +                            struct inode *inode, struct ext3_dir_entry_2 *de,
1544 +                            struct buffer_head * bh)
1545 +{
1546 +       struct inode    *dir = dentry->d_parent->d_inode;
1547 +       const char      *name = dentry->d_name.name;
1548 +       int             namelen = dentry->d_name.len;
1549 +       unsigned long   offset = 0;
1550 +       unsigned short  reclen;
1551 +       int             nlen, rlen, err;
1552 +       char            *top;
1553 +       
1554 +       reclen = EXT3_DIR_REC_LEN(namelen);
1555 +       if (!de) {
1556 +               de = (struct ext3_dir_entry_2 *)bh->b_data;
1557 +               top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1558 +               while ((char *) de <= top) {
1559 +                       if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1560 +                                                 bh, offset)) {
1561 +                               brelse (bh);
1562 +                               return -EIO;
1563 +                       }
1564 +                       if (ext3_match (namelen, name, de)) {
1565 +                               brelse (bh);
1566 +                               return -EEXIST;
1567 +                       }
1568 +                       nlen = EXT3_DIR_REC_LEN(de->name_len);
1569 +                       rlen = le16_to_cpu(de->rec_len);
1570 +                       if ((de->inode? rlen - nlen: rlen) >= reclen)
1571 +                               break;
1572 +                       de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1573 +                       offset += rlen;
1574 +               }
1575 +               if ((char *) de > top)
1576 +                       return -ENOSPC;
1577 +       }
1578 +       BUFFER_TRACE(bh, "get_write_access");
1579 +       err = ext3_journal_get_write_access(handle, bh);
1580 +       if (err) {
1581 +               ext3_std_error(dir->i_sb, err);
1582 +               brelse(bh);
1583 +               return err;
1584 +       }
1585 +       
1586 +       /* By now the buffer is marked for journaling */
1587 +       nlen = EXT3_DIR_REC_LEN(de->name_len);
1588 +       rlen = le16_to_cpu(de->rec_len);
1589 +       if (de->inode) {
1590 +               struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1591 +               de1->rec_len = cpu_to_le16(rlen - nlen);
1592 +               de->rec_len = cpu_to_le16(nlen);
1593 +               de = de1;
1594 +       }
1595 +       de->file_type = EXT3_FT_UNKNOWN;
1596 +       if (inode) {
1597 +               de->inode = cpu_to_le32(inode->i_ino);
1598 +               ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1599 +       } else
1600 +               de->inode = 0;
1601 +       de->name_len = namelen;
1602 +       memcpy (de->name, name, namelen);
1603 +       /*
1604 +        * XXX shouldn't update any times until successful
1605 +        * completion of syscall, but too many callers depend
1606 +        * on this.
1607 +        *
1608 +        * XXX similarly, too many callers depend on
1609 +        * ext3_new_inode() setting the times, but error
1610 +        * recovery deletes the inode, so the worst that can
1611 +        * happen is that the times are slightly out of date
1612 +        * and/or different from the directory change time.
1613 +        */
1614 +       dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1615 +       ext3_update_dx_flag(dir);
1616 +       dir->i_version = ++event;
1617 +       ext3_mark_inode_dirty(handle, dir);
1618 +       BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1619 +       err = ext3_journal_dirty_metadata(handle, bh);
1620 +       if (err)
1621 +               ext3_std_error(dir->i_sb, err);
1622 +       brelse(bh);
1623 +       return 0;
1624 +}
1625 +
1626 +#ifdef CONFIG_EXT3_INDEX
1627 +/*
1628 + * This converts a one block unindexed directory to a 3 block indexed
1629 + * directory, and adds the dentry to the indexed directory.
1630 + */
1631 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1632 +                           struct inode *inode, struct buffer_head *bh)
1633 +{
1634 +       struct inode    *dir = dentry->d_parent->d_inode;
1635 +       const char      *name = dentry->d_name.name;
1636 +       int             namelen = dentry->d_name.len;
1637 +       struct buffer_head *bh2;
1638 +       struct dx_root  *root;
1639 +       struct dx_frame frames[2], *frame;
1640 +       struct dx_entry *entries;
1641 +       struct ext3_dir_entry_2 *de, *de2;
1642 +       char            *data1, *top;
1643 +       unsigned        len;
1644 +       int             retval;
1645 +       unsigned        blocksize;
1646 +       struct dx_hash_info hinfo;
1647 +       u32             block;
1648 +               
1649 +       blocksize =  dir->i_sb->s_blocksize;
1650 +       dxtrace(printk("Creating index\n"));
1651 +       retval = ext3_journal_get_write_access(handle, bh);
1652 +       if (retval) {
1653 +               ext3_std_error(dir->i_sb, retval);
1654 +               brelse(bh);
1655 +               return retval;
1656 +       }
1657 +       root = (struct dx_root *) bh->b_data;
1658 +               
1659 +       EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1660 +       bh2 = ext3_append (handle, dir, &block, &retval);
1661 +       if (!(bh2)) {
1662 +               brelse(bh);
1663 +               return retval;
1664 +       }
1665 +       data1 = bh2->b_data;
1666 +
1667 +       /* The 0th block becomes the root, move the dirents out */
1668 +       de = (struct ext3_dir_entry_2 *)&root->dotdot;
1669 +       de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1670 +       len = ((char *) root) + blocksize - (char *) de;
1671 +       memcpy (data1, de, len);
1672 +       de = (struct ext3_dir_entry_2 *) data1;
1673 +       top = data1 + len;
1674 +       while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1675 +               de = de2;
1676 +       de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1677 +       /* Initialize the root; the dot dirents already exist */
1678 +       de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1679 +       de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1680 +       memset (&root->info, 0, sizeof(root->info));
1681 +       root->info.info_length = sizeof(root->info);
1682 +       root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1683 +       entries = root->entries;
1684 +       dx_set_block (entries, 1);
1685 +       dx_set_count (entries, 1);
1686 +       dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1687 +
1688 +       /* Initialize as for dx_probe */
1689 +       hinfo.hash_version = root->info.hash_version;
1690 +       hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1691 +       ext3fs_dirhash(name, namelen, &hinfo);
1692 +       frame = frames;
1693 +       frame->entries = entries;
1694 +       frame->at = entries;
1695 +       frame->bh = bh;
1696 +       bh = bh2;
1697 +       de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1698 +       dx_release (frames);
1699 +       if (!(de))
1700 +               return retval;
1701 +
1702 +       return add_dirent_to_buf(handle, dentry, inode, de, bh);
1703 +}
1704 +#endif
1705 +
1706  /*
1707   *     ext3_add_entry()
1708   *
1709 @@ -248,127 +1269,198 @@ static inline void ext3_set_de_type(stru
1710   * may not sleep between calling this and putting something into
1711   * the entry, as someone else might have used it while you slept.
1712   */
1713 -
1714 -/*
1715 - * AKPM: the journalling code here looks wrong on the error paths
1716 - */
1717  static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1718         struct inode *inode)
1719  {
1720         struct inode *dir = dentry->d_parent->d_inode;
1721 -       const char *name = dentry->d_name.name;
1722 -       int namelen = dentry->d_name.len;
1723         unsigned long offset;
1724 -       unsigned short rec_len;
1725         struct buffer_head * bh;
1726 -       struct ext3_dir_entry_2 * de, * de1;
1727 +       struct ext3_dir_entry_2 *de;
1728         struct super_block * sb;
1729         int     retval;
1730 +#ifdef CONFIG_EXT3_INDEX
1731 +       int     dx_fallback=0;
1732 +#endif
1733 +       unsigned blocksize;
1734 +       unsigned nlen, rlen;
1735 +       u32 block, blocks;
1736  
1737         sb = dir->i_sb;
1738 -
1739 -       if (!namelen)
1740 +       blocksize = sb->s_blocksize;
1741 +       if (!dentry->d_name.len)
1742                 return -EINVAL;
1743 -       bh = ext3_bread (handle, dir, 0, 0, &retval);
1744 +#ifdef CONFIG_EXT3_INDEX
1745 +       if (is_dx(dir)) {
1746 +               retval = ext3_dx_add_entry(handle, dentry, inode);
1747 +               if (!retval || (retval != ERR_BAD_DX_DIR))
1748 +                       return retval;
1749 +               EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1750 +               dx_fallback++;
1751 +               ext3_mark_inode_dirty(handle, dir);
1752 +       }
1753 +#endif
1754 +       blocks = dir->i_size >> sb->s_blocksize_bits;
1755 +       for (block = 0, offset = 0; block < blocks; block++) {
1756 +               bh = ext3_bread(handle, dir, block, 0, &retval);
1757 +               if(!bh)
1758 +                       return retval;
1759 +               retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1760 +               if (retval != -ENOSPC)
1761 +                       return retval;
1762 +
1763 +#ifdef CONFIG_EXT3_INDEX
1764 +               if (blocks == 1 && !dx_fallback &&
1765 +                   EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1766 +                       return make_indexed_dir(handle, dentry, inode, bh);
1767 +#endif
1768 +               brelse(bh);
1769 +       }
1770 +       bh = ext3_append(handle, dir, &block, &retval);
1771         if (!bh)
1772                 return retval;
1773 -       rec_len = EXT3_DIR_REC_LEN(namelen);
1774 -       offset = 0;
1775         de = (struct ext3_dir_entry_2 *) bh->b_data;
1776 -       while (1) {
1777 -               if ((char *)de >= sb->s_blocksize + bh->b_data) {
1778 -                       brelse (bh);
1779 -                       bh = NULL;
1780 -                       bh = ext3_bread (handle, dir,
1781 -                               offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1782 -                       if (!bh)
1783 -                               return retval;
1784 -                       if (dir->i_size <= offset) {
1785 -                               if (dir->i_size == 0) {
1786 -                                       brelse(bh);
1787 -                                       return -ENOENT;
1788 -                               }
1789 +       de->inode = 0;
1790 +       de->rec_len = cpu_to_le16(rlen = blocksize);
1791 +       nlen = 0;
1792 +       return add_dirent_to_buf(handle, dentry, inode, de, bh);
1793 +}
1794  
1795 -                               ext3_debug ("creating next block\n");
1796 +#ifdef CONFIG_EXT3_INDEX
1797 +/*
1798 + * Returns 0 for success, or a negative error value
1799 + */
1800 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1801 +                            struct inode *inode)
1802 +{
1803 +       struct dx_frame frames[2], *frame;
1804 +       struct dx_entry *entries, *at;
1805 +       struct dx_hash_info hinfo;
1806 +       struct buffer_head * bh;
1807 +       struct inode *dir = dentry->d_parent->d_inode;
1808 +       struct super_block * sb = dir->i_sb;
1809 +       struct ext3_dir_entry_2 *de;
1810 +       int err;
1811  
1812 -                               BUFFER_TRACE(bh, "get_write_access");
1813 -                               ext3_journal_get_write_access(handle, bh);
1814 -                               de = (struct ext3_dir_entry_2 *) bh->b_data;
1815 -                               de->inode = 0;
1816 -                               de->rec_len = le16_to_cpu(sb->s_blocksize);
1817 -                               dir->u.ext3_i.i_disksize =
1818 -                                       dir->i_size = offset + sb->s_blocksize;
1819 -                               dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1820 -                               ext3_mark_inode_dirty(handle, dir);
1821 -                       } else {
1822 +       frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1823 +       if (!frame)
1824 +               return err;
1825 +       entries = frame->entries;
1826 +       at = frame->at;
1827  
1828 -                               ext3_debug ("skipping to next block\n");
1829 +       if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1830 +               goto cleanup;
1831  
1832 -                               de = (struct ext3_dir_entry_2 *) bh->b_data;
1833 -                       }
1834 -               }
1835 -               if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1836 -                                          offset)) {
1837 -                       brelse (bh);
1838 -                       return -ENOENT;
1839 -               }
1840 -               if (ext3_match (namelen, name, de)) {
1841 -                               brelse (bh);
1842 -                               return -EEXIST;
1843 +       BUFFER_TRACE(bh, "get_write_access");
1844 +       err = ext3_journal_get_write_access(handle, bh);
1845 +       if (err)
1846 +               goto journal_error;
1847 +
1848 +       err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1849 +       if (err != -ENOSPC) {
1850 +               bh = 0;
1851 +               goto cleanup;
1852 +       }
1853 +
1854 +       /* Block full, should compress but for now just split */
1855 +       dxtrace(printk("using %u of %u node entries\n",
1856 +                      dx_get_count(entries), dx_get_limit(entries)));
1857 +       /* Need to split index? */
1858 +       if (dx_get_count(entries) == dx_get_limit(entries)) {
1859 +               u32 newblock;
1860 +               unsigned icount = dx_get_count(entries);
1861 +               int levels = frame - frames;
1862 +               struct dx_entry *entries2;
1863 +               struct dx_node *node2;
1864 +               struct buffer_head *bh2;
1865 +
1866 +               if (levels && (dx_get_count(frames->entries) ==
1867 +                              dx_get_limit(frames->entries))) {
1868 +                       ext3_warning(sb, __FUNCTION__,
1869 +                                    "Directory index full!\n");
1870 +                       err = -ENOSPC;
1871 +                       goto cleanup;
1872                 }
1873 -               if ((le32_to_cpu(de->inode) == 0 &&
1874 -                               le16_to_cpu(de->rec_len) >= rec_len) ||
1875 -                   (le16_to_cpu(de->rec_len) >=
1876 -                               EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1877 -                       BUFFER_TRACE(bh, "get_write_access");
1878 -                       ext3_journal_get_write_access(handle, bh);
1879 -                       /* By now the buffer is marked for journaling */
1880 -                       offset += le16_to_cpu(de->rec_len);
1881 -                       if (le32_to_cpu(de->inode)) {
1882 -                               de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1883 -                                       EXT3_DIR_REC_LEN(de->name_len));
1884 -                               de1->rec_len =
1885 -                                       cpu_to_le16(le16_to_cpu(de->rec_len) -
1886 -                                       EXT3_DIR_REC_LEN(de->name_len));
1887 -                               de->rec_len = cpu_to_le16(
1888 -                                               EXT3_DIR_REC_LEN(de->name_len));
1889 -                               de = de1;
1890 +               bh2 = ext3_append (handle, dir, &newblock, &err);
1891 +               if (!(bh2))
1892 +                       goto cleanup;
1893 +               node2 = (struct dx_node *)(bh2->b_data);
1894 +               entries2 = node2->entries;
1895 +               node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1896 +               node2->fake.inode = 0;
1897 +               BUFFER_TRACE(frame->bh, "get_write_access");
1898 +               err = ext3_journal_get_write_access(handle, frame->bh);
1899 +               if (err)
1900 +                       goto journal_error;
1901 +               if (levels) {
1902 +                       unsigned icount1 = icount/2, icount2 = icount - icount1;
1903 +                       unsigned hash2 = dx_get_hash(entries + icount1);
1904 +                       dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1905 +                               
1906 +                       BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1907 +                       err = ext3_journal_get_write_access(handle,
1908 +                                                            frames[0].bh);
1909 +                       if (err)
1910 +                               goto journal_error;
1911 +                               
1912 +                       memcpy ((char *) entries2, (char *) (entries + icount1),
1913 +                               icount2 * sizeof(struct dx_entry));
1914 +                       dx_set_count (entries, icount1);
1915 +                       dx_set_count (entries2, icount2);
1916 +                       dx_set_limit (entries2, dx_node_limit(dir));
1917 +
1918 +                       /* Which index block gets the new entry? */
1919 +                       if (at - entries >= icount1) {
1920 +                               frame->at = at = at - entries - icount1 + entries2;
1921 +                               frame->entries = entries = entries2;
1922 +                               swap(frame->bh, bh2);
1923                         }
1924 -                       de->file_type = EXT3_FT_UNKNOWN;
1925 -                       if (inode) {
1926 -                               de->inode = cpu_to_le32(inode->i_ino);
1927 -                               ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1928 -                       } else
1929 -                               de->inode = 0;
1930 -                       de->name_len = namelen;
1931 -                       memcpy (de->name, name, namelen);
1932 -                       /*
1933 -                        * XXX shouldn't update any times until successful
1934 -                        * completion of syscall, but too many callers depend
1935 -                        * on this.
1936 -                        *
1937 -                        * XXX similarly, too many callers depend on
1938 -                        * ext3_new_inode() setting the times, but error
1939 -                        * recovery deletes the inode, so the worst that can
1940 -                        * happen is that the times are slightly out of date
1941 -                        * and/or different from the directory change time.
1942 -                        */
1943 -                       dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1944 -                       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1945 -                       dir->i_version = ++event;
1946 -                       ext3_mark_inode_dirty(handle, dir);
1947 -                       BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1948 -                       ext3_journal_dirty_metadata(handle, bh);
1949 -                       brelse(bh);
1950 -                       return 0;
1951 +                       dx_insert_block (frames + 0, hash2, newblock);
1952 +                       dxtrace(dx_show_index ("node", frames[1].entries));
1953 +                       dxtrace(dx_show_index ("node",
1954 +                              ((struct dx_node *) bh2->b_data)->entries));
1955 +                       err = ext3_journal_dirty_metadata(handle, bh2);
1956 +                       if (err)
1957 +                               goto journal_error;
1958 +                       brelse (bh2);
1959 +               } else {
1960 +                       dxtrace(printk("Creating second level index...\n"));
1961 +                       memcpy((char *) entries2, (char *) entries,
1962 +                              icount * sizeof(struct dx_entry));
1963 +                       dx_set_limit(entries2, dx_node_limit(dir));
1964 +
1965 +                       /* Set up root */
1966 +                       dx_set_count(entries, 1);
1967 +                       dx_set_block(entries + 0, newblock);
1968 +                       ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1969 +
1970 +                       /* Add new access path frame */
1971 +                       frame = frames + 1;
1972 +                       frame->at = at = at - entries + entries2;
1973 +                       frame->entries = entries = entries2;
1974 +                       frame->bh = bh2;
1975 +                       err = ext3_journal_get_write_access(handle,
1976 +                                                            frame->bh);
1977 +                       if (err)
1978 +                               goto journal_error;
1979                 }
1980 -               offset += le16_to_cpu(de->rec_len);
1981 -               de = (struct ext3_dir_entry_2 *)
1982 -                       ((char *) de + le16_to_cpu(de->rec_len));
1983 +               ext3_journal_dirty_metadata(handle, frames[0].bh);
1984         }
1985 -       brelse (bh);
1986 -       return -ENOSPC;
1987 +       de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1988 +       if (!de)
1989 +               goto cleanup;
1990 +       err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1991 +       bh = 0;
1992 +       goto cleanup;
1993 +       
1994 +journal_error:
1995 +       ext3_std_error(dir->i_sb, err);
1996 +cleanup:
1997 +       if (bh)
1998 +               brelse(bh);
1999 +       dx_release(frames);
2000 +       return err;
2001  }
2002 +#endif
2003  
2004  /*
2005   * ext3_delete_entry deletes a directory entry by merging it with the
2006 @@ -455,9 +1547,11 @@ static int ext3_create (struct inode * d
2007         struct inode * inode;
2008         int err;
2009  
2010 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2011 -       if (IS_ERR(handle))
2012 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2013 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2014 +       if (IS_ERR(handle)) {
2015                 return PTR_ERR(handle);
2016 +       }
2017  
2018         if (IS_SYNC(dir))
2019                 handle->h_sync = 1;
2020 @@ -481,9 +1575,11 @@ static int ext3_mknod (struct inode * di
2021         struct inode *inode;
2022         int err;
2023  
2024 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2025 -       if (IS_ERR(handle))
2026 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2027 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2028 +       if (IS_ERR(handle)) {
2029                 return PTR_ERR(handle);
2030 +       }
2031  
2032         if (IS_SYNC(dir))
2033                 handle->h_sync = 1;
2034 @@ -509,9 +1605,11 @@ static int ext3_mkdir(struct inode * dir
2035         if (dir->i_nlink >= EXT3_LINK_MAX)
2036                 return -EMLINK;
2037  
2038 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2039 -       if (IS_ERR(handle))
2040 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2041 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2042 +       if (IS_ERR(handle)) {
2043                 return PTR_ERR(handle);
2044 +       }
2045  
2046         if (IS_SYNC(dir))
2047                 handle->h_sync = 1;
2048 @@ -523,7 +1621,7 @@ static int ext3_mkdir(struct inode * dir
2049  
2050         inode->i_op = &ext3_dir_inode_operations;
2051         inode->i_fop = &ext3_dir_operations;
2052 -       inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2053 +       inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2054         inode->i_blocks = 0;    
2055         dir_block = ext3_bread (handle, inode, 0, 1, &err);
2056         if (!dir_block) {
2057 @@ -556,21 +1654,19 @@ static int ext3_mkdir(struct inode * dir
2058                 inode->i_mode |= S_ISGID;
2059         ext3_mark_inode_dirty(handle, inode);
2060         err = ext3_add_entry (handle, dentry, inode);
2061 -       if (err)
2062 -               goto out_no_entry;
2063 +       if (err) {
2064 +               inode->i_nlink = 0;
2065 +               ext3_mark_inode_dirty(handle, inode);
2066 +               iput (inode);
2067 +               goto out_stop;
2068 +       }
2069         dir->i_nlink++;
2070 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2071 +       ext3_update_dx_flag(dir);
2072         ext3_mark_inode_dirty(handle, dir);
2073         d_instantiate(dentry, inode);
2074  out_stop:
2075         ext3_journal_stop(handle, dir);
2076         return err;
2077 -
2078 -out_no_entry:
2079 -       inode->i_nlink = 0;
2080 -       ext3_mark_inode_dirty(handle, inode);
2081 -       iput (inode);
2082 -       goto out_stop;
2083  }
2084  
2085  /*
2086 @@ -657,7 +1753,7 @@ int ext3_orphan_add(handle_t *handle, st
2087         int err = 0, rc;
2088         
2089         lock_super(sb);
2090 -       if (!list_empty(&inode->u.ext3_i.i_orphan))
2091 +       if (!list_empty(&EXT3_I(inode)->i_orphan))
2092                 goto out_unlock;
2093  
2094         /* Orphan handling is only valid for files with data blocks
2095 @@ -698,7 +1794,7 @@ int ext3_orphan_add(handle_t *handle, st
2096          * This is safe: on error we're going to ignore the orphan list
2097          * anyway on the next recovery. */
2098         if (!err)
2099 -               list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2100 +               list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2101  
2102         jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2103         jbd_debug(4, "orphan inode %ld will point to %d\n",
2104 @@ -716,25 +1812,26 @@ out_unlock:
2105  int ext3_orphan_del(handle_t *handle, struct inode *inode)
2106  {
2107         struct list_head *prev;
2108 +       struct ext3_inode_info *ei = EXT3_I(inode);
2109         struct ext3_sb_info *sbi;
2110         unsigned long ino_next;
2111         struct ext3_iloc iloc;
2112         int err = 0;
2113  
2114         lock_super(inode->i_sb);
2115 -       if (list_empty(&inode->u.ext3_i.i_orphan)) {
2116 +       if (list_empty(&ei->i_orphan)) {
2117                 unlock_super(inode->i_sb);
2118                 return 0;
2119         }
2120  
2121         ino_next = NEXT_ORPHAN(inode);
2122 -       prev = inode->u.ext3_i.i_orphan.prev;
2123 +       prev = ei->i_orphan.prev;
2124         sbi = EXT3_SB(inode->i_sb);
2125  
2126         jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2127  
2128 -       list_del(&inode->u.ext3_i.i_orphan);
2129 -       INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2130 +       list_del(&ei->i_orphan);
2131 +       INIT_LIST_HEAD(&ei->i_orphan);
2132  
2133         /* If we're on an error path, we may not have a valid
2134          * transaction handle with which to update the orphan list on
2135 @@ -795,8 +1892,9 @@ static int ext3_rmdir (struct inode * di
2136         handle_t *handle;
2137  
2138         handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2139 -       if (IS_ERR(handle))
2140 +       if (IS_ERR(handle)) {
2141                 return PTR_ERR(handle);
2142 +       }
2143  
2144         retval = -ENOENT;
2145         bh = ext3_find_entry (dentry, &de);
2146 @@ -834,7 +1932,7 @@ static int ext3_rmdir (struct inode * di
2147         dir->i_nlink--;
2148         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2149         ext3_mark_inode_dirty(handle, inode);
2150 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2151 +       ext3_update_dx_flag(dir);
2152         ext3_mark_inode_dirty(handle, dir);
2153  
2154  end_rmdir:
2155 @@ -852,8 +1950,9 @@ static int ext3_unlink(struct inode * di
2156         handle_t *handle;
2157  
2158         handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2159 -       if (IS_ERR(handle))
2160 +       if (IS_ERR(handle)) {
2161                 return PTR_ERR(handle);
2162 +       }
2163  
2164         if (IS_SYNC(dir))
2165                 handle->h_sync = 1;
2166 @@ -880,7 +1979,7 @@ static int ext3_unlink(struct inode * di
2167         if (retval)
2168                 goto end_unlink;
2169         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2170 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2171 +       ext3_update_dx_flag(dir);
2172         ext3_mark_inode_dirty(handle, dir);
2173         inode->i_nlink--;
2174         if (!inode->i_nlink)
2175 @@ -906,9 +2005,11 @@ static int ext3_symlink (struct inode * 
2176         if (l > dir->i_sb->s_blocksize)
2177                 return -ENAMETOOLONG;
2178  
2179 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2180 -       if (IS_ERR(handle))
2181 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2182 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2183 +       if (IS_ERR(handle)) {
2184                 return PTR_ERR(handle);
2185 +       }
2186  
2187         if (IS_SYNC(dir))
2188                 handle->h_sync = 1;
2189 @@ -918,7 +2019,7 @@ static int ext3_symlink (struct inode * 
2190         if (IS_ERR(inode))
2191                 goto out_stop;
2192  
2193 -       if (l > sizeof (inode->u.ext3_i.i_data)) {
2194 +       if (l > sizeof (EXT3_I(inode)->i_data)) {
2195                 inode->i_op = &page_symlink_inode_operations;
2196                 inode->i_mapping->a_ops = &ext3_aops;
2197                 /*
2198 @@ -927,24 +2028,22 @@ static int ext3_symlink (struct inode * 
2199                  * i_size in generic_commit_write().
2200                  */
2201                 err = block_symlink(inode, symname, l);
2202 -               if (err)
2203 -                       goto out_no_entry;
2204 +               if (err) {
2205 +                       ext3_dec_count(handle, inode);
2206 +                       ext3_mark_inode_dirty(handle, inode);
2207 +                       iput (inode);
2208 +                       goto out_stop;
2209 +               }
2210         } else {
2211                 inode->i_op = &ext3_fast_symlink_inode_operations;
2212 -               memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2213 +               memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2214                 inode->i_size = l-1;
2215         }
2216 -       inode->u.ext3_i.i_disksize = inode->i_size;
2217 +       EXT3_I(inode)->i_disksize = inode->i_size;
2218         err = ext3_add_nondir(handle, dentry, inode);
2219  out_stop:
2220         ext3_journal_stop(handle, dir);
2221         return err;
2222 -
2223 -out_no_entry:
2224 -       ext3_dec_count(handle, inode);
2225 -       ext3_mark_inode_dirty(handle, inode);
2226 -       iput (inode);
2227 -       goto out_stop;
2228  }
2229  
2230  static int ext3_link (struct dentry * old_dentry,
2231 @@ -957,12 +2056,15 @@ static int ext3_link (struct dentry * ol
2232         if (S_ISDIR(inode->i_mode))
2233                 return -EPERM;
2234  
2235 -       if (inode->i_nlink >= EXT3_LINK_MAX)
2236 +       if (inode->i_nlink >= EXT3_LINK_MAX) {
2237                 return -EMLINK;
2238 +       }
2239  
2240 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2241 -       if (IS_ERR(handle))
2242 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2243 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2244 +       if (IS_ERR(handle)) {
2245                 return PTR_ERR(handle);
2246 +       }
2247  
2248         if (IS_SYNC(dir))
2249                 handle->h_sync = 1;
2250 @@ -995,9 +2097,11 @@ static int ext3_rename (struct inode * o
2251  
2252         old_bh = new_bh = dir_bh = NULL;
2253  
2254 -       handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2255 -       if (IS_ERR(handle))
2256 +       handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2257 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2258 +       if (IS_ERR(handle)) {
2259                 return PTR_ERR(handle);
2260 +       }
2261  
2262         if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2263                 handle->h_sync = 1;
2264 @@ -1070,14 +2174,37 @@ static int ext3_rename (struct inode * o
2265         /*
2266          * ok, that's it
2267          */
2268 -       ext3_delete_entry(handle, old_dir, old_de, old_bh);
2269 +       if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2270 +           old_de->name_len != old_dentry->d_name.len ||
2271 +           strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2272 +           (retval = ext3_delete_entry(handle, old_dir,
2273 +                                       old_de, old_bh)) == -ENOENT) {
2274 +               /* old_de could have moved from under us during htree split, so
2275 +                * make sure that we are deleting the right entry.  We might
2276 +                * also be pointing to a stale entry in the unused part of
2277 +                * old_bh so just checking inum and the name isn't enough. */
2278 +               struct buffer_head *old_bh2;
2279 +               struct ext3_dir_entry_2 *old_de2;
2280 +
2281 +               old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2282 +               if (old_bh2) {
2283 +                       retval = ext3_delete_entry(handle, old_dir,
2284 +                                                  old_de2, old_bh2);
2285 +                       brelse(old_bh2);
2286 +               }
2287 +       }
2288 +       if (retval) {
2289 +               ext3_warning(old_dir->i_sb, "ext3_rename",
2290 +                               "Deleting old file (%lu), %d, error=%d",
2291 +                               old_dir->i_ino, old_dir->i_nlink, retval);
2292 +       }
2293  
2294         if (new_inode) {
2295                 new_inode->i_nlink--;
2296                 new_inode->i_ctime = CURRENT_TIME;
2297         }
2298         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2299 -       old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2300 +       ext3_update_dx_flag(old_dir);
2301         if (dir_bh) {
2302                 BUFFER_TRACE(dir_bh, "get_write_access");
2303                 ext3_journal_get_write_access(handle, dir_bh);
2304 @@ -1089,7 +2212,7 @@ static int ext3_rename (struct inode * o
2305                         new_inode->i_nlink--;
2306                 } else {
2307                         new_dir->i_nlink++;
2308 -                       new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2309 +                       ext3_update_dx_flag(new_dir);
2310                         ext3_mark_inode_dirty(handle, new_dir);
2311                 }
2312         }
2313 --- linux-2.4.22-ac1/fs/ext3/super.c~ext3-htree-2.4.22-rh       2003-09-25 14:39:01.000000000 +0400
2314 +++ linux-2.4.22-ac1-alexey/fs/ext3/super.c     2003-09-25 14:55:12.000000000 +0400
2315 @@ -714,6 +714,7 @@ static int ext3_setup_super(struct super
2316         es->s_mtime = cpu_to_le32(CURRENT_TIME);
2317         ext3_update_dynamic_rev(sb);
2318         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2319 +
2320         ext3_commit_super (sb, es, 1);
2321         if (test_opt (sb, DEBUG))
2322                 printk (KERN_INFO
2323 @@ -724,6 +725,7 @@ static int ext3_setup_super(struct super
2324                         EXT3_BLOCKS_PER_GROUP(sb),
2325                         EXT3_INODES_PER_GROUP(sb),
2326                         sbi->s_mount_opt);
2327 +
2328         printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2329                                 bdevname(sb->s_dev));
2330         if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2331 @@ -897,6 +899,7 @@ static loff_t ext3_max_size(int bits)
2332         return res;
2333  }
2334  
2335 +
2336  struct super_block * ext3_read_super (struct super_block * sb, void * data,
2337                                       int silent)
2338  {
2339 @@ -1073,6 +1076,9 @@ struct super_block * ext3_read_super (st
2340         sbi->s_mount_state = le16_to_cpu(es->s_state);
2341         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2342         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2343 +       for (i=0; i < 4; i++)
2344 +               sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2345 +       sbi->s_def_hash_version = es->s_def_hash_version;
2346  
2347         if (sbi->s_blocks_per_group > blocksize * 8) {
2348                 printk (KERN_ERR
2349 @@ -1846,6 +1852,7 @@ static void __exit exit_ext3_fs(void)
2350         unregister_filesystem(&ext3_fs_type);
2351  }
2352  
2353 +EXPORT_SYMBOL(ext3_force_commit);
2354  EXPORT_SYMBOL(ext3_bread);
2355  
2356  MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2357 --- linux-2.4.22-ac1/include/linux/ext3_fs.h~ext3-htree-2.4.22-rh       2003-09-25 14:16:29.000000000 +0400
2358 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_fs.h     2003-09-25 14:58:30.000000000 +0400
2359 @@ -40,6 +40,11 @@
2360  #define EXT3FS_VERSION         "2.4-0.9.19"
2361  
2362  /*
2363 + * Always enable hashed directories
2364 + */
2365 +#define CONFIG_EXT3_INDEX
2366 +
2367 +/*
2368   * Debug code
2369   */
2370  #ifdef EXT3FS_DEBUG
2371 @@ -440,8 +445,11 @@ struct ext3_super_block {
2372  /*E0*/ __u32   s_journal_inum;         /* inode number of journal file */
2373         __u32   s_journal_dev;          /* device number of journal file */
2374         __u32   s_last_orphan;          /* start of list of inodes to delete */
2375 -
2376 -/*EC*/ __u32   s_reserved[197];        /* Padding to the end of the block */
2377 +       __u32   s_hash_seed[4];         /* HTREE hash seed */
2378 +       __u8    s_def_hash_version;     /* Default hash version to use */
2379 +       __u8    s_reserved_char_pad;
2380 +       __u16   s_reserved_word_pad;
2381 +       __u32   s_reserved[192];        /* Padding to the end of the block */
2382  };
2383  
2384  #ifdef __KERNEL__
2385 @@ -578,9 +586,46 @@ struct ext3_dir_entry_2 {
2386  #define EXT3_DIR_ROUND                 (EXT3_DIR_PAD - 1)
2387  #define EXT3_DIR_REC_LEN(name_len)     (((name_len) + 8 + EXT3_DIR_ROUND) & \
2388                                          ~EXT3_DIR_ROUND)
2389 +/*
2390 + * Hash Tree Directory indexing
2391 + * (c) Daniel Phillips, 2001
2392 + */
2393 +
2394 +#ifdef CONFIG_EXT3_INDEX
2395 +  #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2396 +                                             EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2397 +                     (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2398 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2399 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2400 +#else
2401 +  #define is_dx(dir) 0
2402 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2403 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2404 +#endif
2405 +
2406 +/* Legal values for the dx_root hash_version field: */
2407 +
2408 +#define DX_HASH_LEGACY         0
2409 +#define DX_HASH_HALF_MD4       1
2410 +#define DX_HASH_TEA            2
2411 +
2412 +/* hash info structure used by the directory hash */
2413 +struct dx_hash_info
2414 +{
2415 +       u32             hash;
2416 +       u32             minor_hash;
2417 +       int             hash_version;
2418 +       u32             *seed;
2419 +};
2420  
2421  #ifdef __KERNEL__
2422  /*
2423 + * Control parameters used by ext3_htree_next_block
2424 + */
2425 +#define HASH_NB_ALWAYS         1
2426 +
2427 +
2428 +/*
2429   * Describe an inode's exact location on disk and in memory
2430   */
2431  struct ext3_iloc
2432 @@ -590,6 +635,27 @@ struct ext3_iloc
2433         unsigned long block_group;
2434  };
2435  
2436 +
2437 +/*
2438 + * This structure is stuffed into the struct file's private_data field
2439 + * for directories.  It is where we put information so that we can do
2440 + * readdir operations in hash tree order.
2441 + */
2442 +struct dir_private_info {
2443 +       rb_root_t       root;
2444 +       rb_node_t       *curr_node;
2445 +       struct fname    *extra_fname;
2446 +       loff_t          last_pos;
2447 +       __u32           curr_hash;
2448 +       __u32           curr_minor_hash;
2449 +       __u32           next_hash;
2450 +};
2451 +
2452 +/*
2453 + * Special error return code only used by dx_probe() and its callers.
2454 + */
2455 +#define ERR_BAD_DX_DIR -75000
2456 +
2457  /*
2458   * Function prototypes
2459   */
2460 @@ -617,11 +683,20 @@ extern struct ext3_group_desc * ext3_get
2461  
2462  /* dir.c */
2463  extern int ext3_check_dir_entry(const char *, struct inode *,
2464 -                               struct ext3_dir_entry_2 *, struct buffer_head *,
2465 -                               unsigned long);
2466 +                               struct ext3_dir_entry_2 *,
2467 +                               struct buffer_head *, unsigned long);
2468 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2469 +                                   __u32 minor_hash,
2470 +                                   struct ext3_dir_entry_2 *dirent);
2471 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2472 +
2473  /* fsync.c */
2474  extern int ext3_sync_file (struct file *, struct dentry *, int);
2475  
2476 +/* hash.c */
2477 +extern int ext3fs_dirhash(const char *name, int len, struct
2478 +                         dx_hash_info *hinfo);
2479 +
2480  /* ialloc.c */
2481  extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2482  extern void ext3_free_inode (handle_t *, struct inode *);
2483 @@ -655,6 +730,8 @@ extern int ext3_ioctl (struct inode *, s
2484  /* namei.c */
2485  extern int ext3_orphan_add(handle_t *, struct inode *);
2486  extern int ext3_orphan_del(handle_t *, struct inode *);
2487 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2488 +                               __u32 start_minor_hash, __u32 *next_hash);
2489  
2490  /* super.c */
2491  extern void ext3_error (struct super_block *, const char *, const char *, ...)
2492 --- linux-2.4.22-ac1/include/linux/ext3_fs_sb.h~ext3-htree-2.4.22-rh    2003-09-25 14:16:34.000000000 +0400
2493 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_fs_sb.h  2003-09-25 14:55:12.000000000 +0400
2494 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2495         int s_inode_size;
2496         int s_first_ino;
2497         u32 s_next_generation;
2498 +       u32 s_hash_seed[4];
2499 +       int s_def_hash_version;
2500  
2501         unsigned long s_dir_count;
2502         u8 *s_debts;
2503 --- linux-2.4.22-ac1/include/linux/ext3_jbd.h~ext3-htree-2.4.22-rh      2003-06-13 18:51:38.000000000 +0400
2504 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_jbd.h    2003-09-25 14:55:12.000000000 +0400
2505 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2506  
2507  #define EXT3_RESERVE_TRANS_BLOCKS      12U
2508  
2509 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS  8
2510 +
2511  int
2512  ext3_mark_iloc_dirty(handle_t *handle, 
2513                      struct inode *inode,
2514 --- linux-2.4.22-ac1/include/linux/rbtree.h~ext3-htree-2.4.22-rh        2001-11-22 22:46:18.000000000 +0300
2515 +++ linux-2.4.22-ac1-alexey/include/linux/rbtree.h      2003-09-25 14:55:12.000000000 +0400
2516 @@ -120,6 +120,8 @@ rb_root_t;
2517  
2518  extern void rb_insert_color(rb_node_t *, rb_root_t *);
2519  extern void rb_erase(rb_node_t *, rb_root_t *);
2520 +extern rb_node_t *rb_get_first(rb_root_t *root);
2521 +extern rb_node_t *rb_get_next(rb_node_t *n);
2522  
2523  static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2524  {
2525 --- linux-2.4.22-ac1/lib/rbtree.c~ext3-htree-2.4.22-rh  2002-08-03 04:39:46.000000000 +0400
2526 +++ linux-2.4.22-ac1-alexey/lib/rbtree.c        2003-09-25 14:55:12.000000000 +0400
2527 @@ -17,6 +17,8 @@
2528    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2529  
2530    linux/lib/rbtree.c
2531 +
2532 +  rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2533  */
2534  
2535  #include <linux/rbtree.h>
2536 @@ -294,3 +296,43 @@ void rb_erase(rb_node_t * node, rb_root_
2537                 __rb_erase_color(child, parent, root);
2538  }
2539  EXPORT_SYMBOL(rb_erase);
2540 +
2541 +/*
2542 + * This function returns the first node (in sort order) of the tree.
2543 + */
2544 +rb_node_t *rb_get_first(rb_root_t *root)
2545 +{
2546 +       rb_node_t       *n;
2547 +
2548 +       n = root->rb_node;
2549 +       if (!n)
2550 +               return 0;
2551 +       while (n->rb_left)
2552 +               n = n->rb_left;
2553 +       return n;
2554 +}
2555 +EXPORT_SYMBOL(rb_get_first);
2556 +
2557 +/*
2558 + * Given a node, this function will return the next node in the tree.
2559 + */
2560 +rb_node_t *rb_get_next(rb_node_t *n)
2561 +{
2562 +       rb_node_t       *parent;
2563 +
2564 +       if (n->rb_right) {
2565 +               n = n->rb_right;
2566 +               while (n->rb_left)
2567 +                       n = n->rb_left;
2568 +               return n;
2569 +       } else {
2570 +               while ((parent = n->rb_parent)) {
2571 +                       if (n == parent->rb_left)
2572 +                               return parent;
2573 +                       n = parent;
2574 +               }
2575 +               return 0;
2576 +       }
2577 +}
2578 +EXPORT_SYMBOL(rb_get_next);
2579 +
2580
2581 _