Whamcloud - gitweb
- configurable stack size fo x86_64
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-htree.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            | 1420 ++++++++++++++++++++++++++++++++++++++++-----
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, 1921 insertions(+), 161 deletions(-)
13
14 --- linux-2.4.18-chaos-pdirops/fs/ext3/dir.c~ext3-htree 2001-11-10 01:25:04.000000000 +0300
15 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/dir.c     2003-09-23 12:12:04.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.18-chaos-pdirops/fs/ext3/file.c~ext3-htree        2003-07-28 17:52:04.000000000 +0400
362 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/file.c    2003-09-23 12:11:54.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.18-chaos-pdirops-alexey/fs/ext3/hash.c    2003-09-23 12:11:54.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.18-chaos-pdirops/fs/ext3/Makefile~ext3-htree      2003-09-23 11:52:24.000000000 +0400
592 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/Makefile  2003-09-23 12:11:54.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.18-chaos-pdirops/fs/ext3/namei.c~ext3-htree       2003-09-23 11:52:34.000000000 +0400
603 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/namei.c   2003-09-23 12:12:09.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,66 @@ 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 +       if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
1343 +               return NULL;
1344 +       hash = hinfo.hash;
1345 +       do {
1346 +               block = dx_get_block(frame->at);
1347 +               if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1348 +                       goto errout;
1349 +               de = (struct ext3_dir_entry_2 *) bh->b_data;
1350 +               top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
1351 +                                      EXT3_DIR_REC_LEN(0));
1352 +               for (; de < top; de = ext3_next_entry(de))
1353 +               if (ext3_match (namelen, name, de)) {
1354 +                       if (!ext3_check_dir_entry("ext3_find_entry",
1355 +                                                 dir, de, bh,
1356 +                                 (block<<EXT3_BLOCK_SIZE_BITS(sb))
1357 +                                         +((char *)de - bh->b_data))) {
1358 +                               brelse (bh);
1359 +                               goto errout;
1360 +                       }
1361 +                       *res_dir = de;
1362 +                       dx_release (frames);
1363 +                       return bh;
1364 +               }
1365 +               brelse (bh);
1366 +               /* Check to see if we should continue to search */
1367 +               retval = ext3_htree_next_block(dir, hash, frame,
1368 +                                              frames, err, 0);
1369 +               if (retval == -1) {
1370 +                       ext3_warning(sb, __FUNCTION__,
1371 +                            "error reading index page in directory #%lu",
1372 +                            dir->i_ino);
1373 +                       goto errout;
1374 +               }
1375 +       } while (retval == 1);
1376 +       
1377 +       *err = -ENOENT;
1378 +errout:
1379 +       dxtrace(printk("%s not found\n", name));
1380 +       dx_release (frames);
1381 +       return NULL;
1382 +}
1383 +#endif
1384 +
1385  static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1386  {
1387         struct inode * inode;
1388 @@ -213,8 +938,9 @@ static struct dentry *ext3_lookup(struct
1389                 brelse (bh);
1390                 inode = iget(dir->i_sb, ino);
1391  
1392 -               if (!inode)
1393 +               if (!inode) {
1394                         return ERR_PTR(-EACCES);
1395 +               }
1396         }
1397         d_add(dentry, inode);
1398         return NULL;
1399 @@ -238,6 +964,301 @@ static inline void ext3_set_de_type(stru
1400                 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1401  }
1402  
1403 +#ifdef CONFIG_EXT3_INDEX
1404 +static struct ext3_dir_entry_2 *
1405 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1406 +{
1407 +       unsigned rec_len = 0;
1408 +
1409 +       while (count--) {
1410 +               struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1411 +               rec_len = EXT3_DIR_REC_LEN(de->name_len);
1412 +               memcpy (to, de, rec_len);
1413 +               ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1414 +               de->inode = 0;
1415 +               map++;
1416 +               to += rec_len;
1417 +       }
1418 +       return (struct ext3_dir_entry_2 *) (to - rec_len);
1419 +}
1420 +
1421 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1422 +{
1423 +       struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1424 +       unsigned rec_len = 0;
1425 +
1426 +       prev = to = de;
1427 +       while ((char*)de < base + size) {
1428 +               next = (struct ext3_dir_entry_2 *) ((char *) de +
1429 +                                                   le16_to_cpu(de->rec_len));
1430 +               if (de->inode && de->name_len) {
1431 +                       rec_len = EXT3_DIR_REC_LEN(de->name_len);
1432 +                       if (de > to)
1433 +                               memmove(to, de, rec_len);
1434 +                       to->rec_len = cpu_to_le16(rec_len);
1435 +                       prev = to;
1436 +                       to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1437 +               }
1438 +               de = next;
1439 +       }
1440 +       return prev;
1441 +}
1442 +
1443 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1444 +                       struct buffer_head **bh,struct dx_frame *frame,
1445 +                       struct dx_hash_info *hinfo, int *error)
1446 +{
1447 +       unsigned blocksize = dir->i_sb->s_blocksize;
1448 +       unsigned count, continued;
1449 +       struct buffer_head *bh2;
1450 +       u32 newblock;
1451 +       u32 hash2;
1452 +       struct dx_map_entry *map;
1453 +       char *data1 = (*bh)->b_data, *data2;
1454 +       unsigned split;
1455 +       struct ext3_dir_entry_2 *de = NULL, *de2;
1456 +       int     err;
1457 +
1458 +       bh2 = ext3_append (handle, dir, &newblock, error);
1459 +       if (!(bh2)) {
1460 +               brelse(*bh);
1461 +               *bh = NULL;
1462 +               goto errout;
1463 +       }
1464 +
1465 +       BUFFER_TRACE(*bh, "get_write_access");
1466 +       err = ext3_journal_get_write_access(handle, *bh);
1467 +       if (err) {
1468 +       journal_error:
1469 +               brelse(*bh);
1470 +               brelse(bh2);
1471 +               *bh = NULL;
1472 +               ext3_std_error(dir->i_sb, err);
1473 +               goto errout;
1474 +       }
1475 +       BUFFER_TRACE(frame->bh, "get_write_access");
1476 +       err = ext3_journal_get_write_access(handle, frame->bh);
1477 +       if (err)
1478 +               goto journal_error;
1479 +
1480 +       data2 = bh2->b_data;
1481 +
1482 +       /* create map in the end of data2 block */
1483 +       map = (struct dx_map_entry *) (data2 + blocksize);
1484 +       count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1485 +                            blocksize, hinfo, map);
1486 +       map -= count;
1487 +       split = count/2; // need to adjust to actual middle
1488 +       dx_sort_map (map, count);
1489 +       hash2 = map[split].hash;
1490 +       continued = hash2 == map[split - 1].hash;
1491 +       dxtrace(printk("Split block %i at %x, %i/%i\n",
1492 +               dx_get_block(frame->at), hash2, split, count-split));
1493 +
1494 +       /* Fancy dance to stay within two buffers */
1495 +       de2 = dx_move_dirents(data1, data2, map + split, count - split);
1496 +       de = dx_pack_dirents(data1,blocksize);
1497 +       de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1498 +       de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1499 +       dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1500 +       dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1501 +
1502 +       /* Which block gets the new entry? */
1503 +       if (hinfo->hash >= hash2)
1504 +       {
1505 +               swap(*bh, bh2);
1506 +               de = de2;
1507 +       }
1508 +       dx_insert_block (frame, hash2 + continued, newblock);
1509 +       err = ext3_journal_dirty_metadata (handle, bh2);
1510 +       if (err)
1511 +               goto journal_error;
1512 +       err = ext3_journal_dirty_metadata (handle, frame->bh);
1513 +       if (err)
1514 +               goto journal_error;
1515 +       brelse (bh2);
1516 +       dxtrace(dx_show_index ("frame", frame->entries));
1517 +errout:
1518 +       return de;
1519 +}
1520 +#endif
1521 +
1522 +
1523 +/*
1524 + * Add a new entry into a directory (leaf) block.  If de is non-NULL,
1525 + * it points to a directory entry which is guaranteed to be large
1526 + * enough for new directory entry.  If de is NULL, then
1527 + * add_dirent_to_buf will attempt search the directory block for
1528 + * space.  It will return -ENOSPC if no space is available, and -EIO
1529 + * and -EEXIST if directory entry already exists.
1530 + * 
1531 + * NOTE!  bh is NOT released in the case where ENOSPC is returned.  In
1532 + * all other cases bh is released.
1533 + */
1534 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1535 +                            struct inode *inode, struct ext3_dir_entry_2 *de,
1536 +                            struct buffer_head * bh)
1537 +{
1538 +       struct inode    *dir = dentry->d_parent->d_inode;
1539 +       const char      *name = dentry->d_name.name;
1540 +       int             namelen = dentry->d_name.len;
1541 +       unsigned long   offset = 0;
1542 +       unsigned short  reclen;
1543 +       int             nlen, rlen, err;
1544 +       char            *top;
1545 +       
1546 +       reclen = EXT3_DIR_REC_LEN(namelen);
1547 +       if (!de) {
1548 +               de = (struct ext3_dir_entry_2 *)bh->b_data;
1549 +               top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1550 +               while ((char *) de <= top) {
1551 +                       if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1552 +                                                 bh, offset)) {
1553 +                               brelse (bh);
1554 +                               return -EIO;
1555 +                       }
1556 +                       if (ext3_match (namelen, name, de)) {
1557 +                               brelse (bh);
1558 +                               return -EEXIST;
1559 +                       }
1560 +                       nlen = EXT3_DIR_REC_LEN(de->name_len);
1561 +                       rlen = le16_to_cpu(de->rec_len);
1562 +                       if ((de->inode? rlen - nlen: rlen) >= reclen)
1563 +                               break;
1564 +                       de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1565 +                       offset += rlen;
1566 +               }
1567 +               if ((char *) de > top)
1568 +                       return -ENOSPC;
1569 +       }
1570 +       BUFFER_TRACE(bh, "get_write_access");
1571 +       err = ext3_journal_get_write_access(handle, bh);
1572 +       if (err) {
1573 +               ext3_std_error(dir->i_sb, err);
1574 +               brelse(bh);
1575 +               return err;
1576 +       }
1577 +       
1578 +       /* By now the buffer is marked for journaling */
1579 +       nlen = EXT3_DIR_REC_LEN(de->name_len);
1580 +       rlen = le16_to_cpu(de->rec_len);
1581 +       if (de->inode) {
1582 +               struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1583 +               de1->rec_len = cpu_to_le16(rlen - nlen);
1584 +               de->rec_len = cpu_to_le16(nlen);
1585 +               de = de1;
1586 +       }
1587 +       de->file_type = EXT3_FT_UNKNOWN;
1588 +       if (inode) {
1589 +               de->inode = cpu_to_le32(inode->i_ino);
1590 +               ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1591 +       } else
1592 +               de->inode = 0;
1593 +       de->name_len = namelen;
1594 +       memcpy (de->name, name, namelen);
1595 +       /*
1596 +        * XXX shouldn't update any times until successful
1597 +        * completion of syscall, but too many callers depend
1598 +        * on this.
1599 +        *
1600 +        * XXX similarly, too many callers depend on
1601 +        * ext3_new_inode() setting the times, but error
1602 +        * recovery deletes the inode, so the worst that can
1603 +        * happen is that the times are slightly out of date
1604 +        * and/or different from the directory change time.
1605 +        */
1606 +       dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1607 +       ext3_update_dx_flag(dir);
1608 +       dir->i_version = ++event;
1609 +       ext3_mark_inode_dirty(handle, dir);
1610 +       BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1611 +       err = ext3_journal_dirty_metadata(handle, bh);
1612 +       if (err)
1613 +               ext3_std_error(dir->i_sb, err);
1614 +       brelse(bh);
1615 +       return 0;
1616 +}
1617 +
1618 +#ifdef CONFIG_EXT3_INDEX
1619 +/*
1620 + * This converts a one block unindexed directory to a 3 block indexed
1621 + * directory, and adds the dentry to the indexed directory.
1622 + */
1623 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1624 +                           struct inode *inode, struct buffer_head *bh)
1625 +{
1626 +       struct inode    *dir = dentry->d_parent->d_inode;
1627 +       const char      *name = dentry->d_name.name;
1628 +       int             namelen = dentry->d_name.len;
1629 +       struct buffer_head *bh2;
1630 +       struct dx_root  *root;
1631 +       struct dx_frame frames[2], *frame;
1632 +       struct dx_entry *entries;
1633 +       struct ext3_dir_entry_2 *de, *de2;
1634 +       char            *data1, *top;
1635 +       unsigned        len;
1636 +       int             retval;
1637 +       unsigned        blocksize;
1638 +       struct dx_hash_info hinfo;
1639 +       u32             block;
1640 +               
1641 +       blocksize =  dir->i_sb->s_blocksize;
1642 +       dxtrace(printk("Creating index\n"));
1643 +       retval = ext3_journal_get_write_access(handle, bh);
1644 +       if (retval) {
1645 +               ext3_std_error(dir->i_sb, retval);
1646 +               brelse(bh);
1647 +               return retval;
1648 +       }
1649 +       root = (struct dx_root *) bh->b_data;
1650 +               
1651 +       EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1652 +       bh2 = ext3_append (handle, dir, &block, &retval);
1653 +       if (!(bh2)) {
1654 +               brelse(bh);
1655 +               return retval;
1656 +       }
1657 +       data1 = bh2->b_data;
1658 +
1659 +       /* The 0th block becomes the root, move the dirents out */
1660 +       de = (struct ext3_dir_entry_2 *)&root->dotdot;
1661 +       de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1662 +       len = ((char *) root) + blocksize - (char *) de;
1663 +       memcpy (data1, de, len);
1664 +       de = (struct ext3_dir_entry_2 *) data1;
1665 +       top = data1 + len;
1666 +       while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1667 +               de = de2;
1668 +       de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1669 +       /* Initialize the root; the dot dirents already exist */
1670 +       de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1671 +       de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1672 +       memset (&root->info, 0, sizeof(root->info));
1673 +       root->info.info_length = sizeof(root->info);
1674 +       root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1675 +       entries = root->entries;
1676 +       dx_set_block (entries, 1);
1677 +       dx_set_count (entries, 1);
1678 +       dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1679 +
1680 +       /* Initialize as for dx_probe */
1681 +       hinfo.hash_version = root->info.hash_version;
1682 +       hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1683 +       ext3fs_dirhash(name, namelen, &hinfo);
1684 +       frame = frames;
1685 +       frame->entries = entries;
1686 +       frame->at = entries;
1687 +       frame->bh = bh;
1688 +       bh = bh2;
1689 +       de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1690 +       dx_release (frames);
1691 +       if (!(de))
1692 +               return retval;
1693 +
1694 +       return add_dirent_to_buf(handle, dentry, inode, de, bh);
1695 +}
1696 +#endif
1697 +
1698  /*
1699   *     ext3_add_entry()
1700   *
1701 @@ -248,127 +1268,198 @@ static inline void ext3_set_de_type(stru
1702   * may not sleep between calling this and putting something into
1703   * the entry, as someone else might have used it while you slept.
1704   */
1705 -
1706 -/*
1707 - * AKPM: the journalling code here looks wrong on the error paths
1708 - */
1709  static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1710         struct inode *inode)
1711  {
1712         struct inode *dir = dentry->d_parent->d_inode;
1713 -       const char *name = dentry->d_name.name;
1714 -       int namelen = dentry->d_name.len;
1715         unsigned long offset;
1716 -       unsigned short rec_len;
1717         struct buffer_head * bh;
1718 -       struct ext3_dir_entry_2 * de, * de1;
1719 +       struct ext3_dir_entry_2 *de;
1720         struct super_block * sb;
1721         int     retval;
1722 +#ifdef CONFIG_EXT3_INDEX
1723 +       int     dx_fallback=0;
1724 +#endif
1725 +       unsigned blocksize;
1726 +       unsigned nlen, rlen;
1727 +       u32 block, blocks;
1728  
1729         sb = dir->i_sb;
1730 -
1731 -       if (!namelen)
1732 +       blocksize = sb->s_blocksize;
1733 +       if (!dentry->d_name.len)
1734                 return -EINVAL;
1735 -       bh = ext3_bread (handle, dir, 0, 0, &retval);
1736 +#ifdef CONFIG_EXT3_INDEX
1737 +       if (is_dx(dir)) {
1738 +               retval = ext3_dx_add_entry(handle, dentry, inode);
1739 +               if (!retval || (retval != ERR_BAD_DX_DIR))
1740 +                       return retval;
1741 +               EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1742 +               dx_fallback++;
1743 +               ext3_mark_inode_dirty(handle, dir);
1744 +       }
1745 +#endif
1746 +       blocks = dir->i_size >> sb->s_blocksize_bits;
1747 +       for (block = 0, offset = 0; block < blocks; block++) {
1748 +               bh = ext3_bread(handle, dir, block, 0, &retval);
1749 +               if(!bh)
1750 +                       return retval;
1751 +               retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1752 +               if (retval != -ENOSPC)
1753 +                       return retval;
1754 +
1755 +#ifdef CONFIG_EXT3_INDEX
1756 +               if (blocks == 1 && !dx_fallback &&
1757 +                   EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1758 +                       return make_indexed_dir(handle, dentry, inode, bh);
1759 +#endif
1760 +               brelse(bh);
1761 +       }
1762 +       bh = ext3_append(handle, dir, &block, &retval);
1763         if (!bh)
1764                 return retval;
1765 -       rec_len = EXT3_DIR_REC_LEN(namelen);
1766 -       offset = 0;
1767         de = (struct ext3_dir_entry_2 *) bh->b_data;
1768 -       while (1) {
1769 -               if ((char *)de >= sb->s_blocksize + bh->b_data) {
1770 -                       brelse (bh);
1771 -                       bh = NULL;
1772 -                       bh = ext3_bread (handle, dir,
1773 -                               offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1774 -                       if (!bh)
1775 -                               return retval;
1776 -                       if (dir->i_size <= offset) {
1777 -                               if (dir->i_size == 0) {
1778 -                                       brelse(bh);
1779 -                                       return -ENOENT;
1780 -                               }
1781 +       de->inode = 0;
1782 +       de->rec_len = cpu_to_le16(rlen = blocksize);
1783 +       nlen = 0;
1784 +       return add_dirent_to_buf(handle, dentry, inode, de, bh);
1785 +}
1786  
1787 -                               ext3_debug ("creating next block\n");
1788 +#ifdef CONFIG_EXT3_INDEX
1789 +/*
1790 + * Returns 0 for success, or a negative error value
1791 + */
1792 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1793 +                            struct inode *inode)
1794 +{
1795 +       struct dx_frame frames[2], *frame;
1796 +       struct dx_entry *entries, *at;
1797 +       struct dx_hash_info hinfo;
1798 +       struct buffer_head * bh;
1799 +       struct inode *dir = dentry->d_parent->d_inode;
1800 +       struct super_block * sb = dir->i_sb;
1801 +       struct ext3_dir_entry_2 *de;
1802 +       int err;
1803  
1804 -                               BUFFER_TRACE(bh, "get_write_access");
1805 -                               ext3_journal_get_write_access(handle, bh);
1806 -                               de = (struct ext3_dir_entry_2 *) bh->b_data;
1807 -                               de->inode = 0;
1808 -                               de->rec_len = le16_to_cpu(sb->s_blocksize);
1809 -                               dir->u.ext3_i.i_disksize =
1810 -                                       dir->i_size = offset + sb->s_blocksize;
1811 -                               dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1812 -                               ext3_mark_inode_dirty(handle, dir);
1813 -                       } else {
1814 +       frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1815 +       if (!frame)
1816 +               return err;
1817 +       entries = frame->entries;
1818 +       at = frame->at;
1819  
1820 -                               ext3_debug ("skipping to next block\n");
1821 +       if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1822 +               goto cleanup;
1823  
1824 -                               de = (struct ext3_dir_entry_2 *) bh->b_data;
1825 -                       }
1826 -               }
1827 -               if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1828 -                                          offset)) {
1829 -                       brelse (bh);
1830 -                       return -ENOENT;
1831 -               }
1832 -               if (ext3_match (namelen, name, de)) {
1833 -                               brelse (bh);
1834 -                               return -EEXIST;
1835 +       BUFFER_TRACE(bh, "get_write_access");
1836 +       err = ext3_journal_get_write_access(handle, bh);
1837 +       if (err)
1838 +               goto journal_error;
1839 +
1840 +       err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1841 +       if (err != -ENOSPC) {
1842 +               bh = 0;
1843 +               goto cleanup;
1844 +       }
1845 +
1846 +       /* Block full, should compress but for now just split */
1847 +       dxtrace(printk("using %u of %u node entries\n",
1848 +                      dx_get_count(entries), dx_get_limit(entries)));
1849 +       /* Need to split index? */
1850 +       if (dx_get_count(entries) == dx_get_limit(entries)) {
1851 +               u32 newblock;
1852 +               unsigned icount = dx_get_count(entries);
1853 +               int levels = frame - frames;
1854 +               struct dx_entry *entries2;
1855 +               struct dx_node *node2;
1856 +               struct buffer_head *bh2;
1857 +
1858 +               if (levels && (dx_get_count(frames->entries) ==
1859 +                              dx_get_limit(frames->entries))) {
1860 +                       ext3_warning(sb, __FUNCTION__,
1861 +                                    "Directory index full!\n");
1862 +                       err = -ENOSPC;
1863 +                       goto cleanup;
1864                 }
1865 -               if ((le32_to_cpu(de->inode) == 0 &&
1866 -                               le16_to_cpu(de->rec_len) >= rec_len) ||
1867 -                   (le16_to_cpu(de->rec_len) >=
1868 -                               EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1869 -                       BUFFER_TRACE(bh, "get_write_access");
1870 -                       ext3_journal_get_write_access(handle, bh);
1871 -                       /* By now the buffer is marked for journaling */
1872 -                       offset += le16_to_cpu(de->rec_len);
1873 -                       if (le32_to_cpu(de->inode)) {
1874 -                               de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1875 -                                       EXT3_DIR_REC_LEN(de->name_len));
1876 -                               de1->rec_len =
1877 -                                       cpu_to_le16(le16_to_cpu(de->rec_len) -
1878 -                                       EXT3_DIR_REC_LEN(de->name_len));
1879 -                               de->rec_len = cpu_to_le16(
1880 -                                               EXT3_DIR_REC_LEN(de->name_len));
1881 -                               de = de1;
1882 +               bh2 = ext3_append (handle, dir, &newblock, &err);
1883 +               if (!(bh2))
1884 +                       goto cleanup;
1885 +               node2 = (struct dx_node *)(bh2->b_data);
1886 +               entries2 = node2->entries;
1887 +               node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1888 +               node2->fake.inode = 0;
1889 +               BUFFER_TRACE(frame->bh, "get_write_access");
1890 +               err = ext3_journal_get_write_access(handle, frame->bh);
1891 +               if (err)
1892 +                       goto journal_error;
1893 +               if (levels) {
1894 +                       unsigned icount1 = icount/2, icount2 = icount - icount1;
1895 +                       unsigned hash2 = dx_get_hash(entries + icount1);
1896 +                       dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1897 +                               
1898 +                       BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1899 +                       err = ext3_journal_get_write_access(handle,
1900 +                                                            frames[0].bh);
1901 +                       if (err)
1902 +                               goto journal_error;
1903 +                               
1904 +                       memcpy ((char *) entries2, (char *) (entries + icount1),
1905 +                               icount2 * sizeof(struct dx_entry));
1906 +                       dx_set_count (entries, icount1);
1907 +                       dx_set_count (entries2, icount2);
1908 +                       dx_set_limit (entries2, dx_node_limit(dir));
1909 +
1910 +                       /* Which index block gets the new entry? */
1911 +                       if (at - entries >= icount1) {
1912 +                               frame->at = at = at - entries - icount1 + entries2;
1913 +                               frame->entries = entries = entries2;
1914 +                               swap(frame->bh, bh2);
1915                         }
1916 -                       de->file_type = EXT3_FT_UNKNOWN;
1917 -                       if (inode) {
1918 -                               de->inode = cpu_to_le32(inode->i_ino);
1919 -                               ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1920 -                       } else
1921 -                               de->inode = 0;
1922 -                       de->name_len = namelen;
1923 -                       memcpy (de->name, name, namelen);
1924 -                       /*
1925 -                        * XXX shouldn't update any times until successful
1926 -                        * completion of syscall, but too many callers depend
1927 -                        * on this.
1928 -                        *
1929 -                        * XXX similarly, too many callers depend on
1930 -                        * ext3_new_inode() setting the times, but error
1931 -                        * recovery deletes the inode, so the worst that can
1932 -                        * happen is that the times are slightly out of date
1933 -                        * and/or different from the directory change time.
1934 -                        */
1935 -                       dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1936 -                       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1937 -                       dir->i_version = ++event;
1938 -                       ext3_mark_inode_dirty(handle, dir);
1939 -                       BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1940 -                       ext3_journal_dirty_metadata(handle, bh);
1941 -                       brelse(bh);
1942 -                       return 0;
1943 +                       dx_insert_block (frames + 0, hash2, newblock);
1944 +                       dxtrace(dx_show_index ("node", frames[1].entries));
1945 +                       dxtrace(dx_show_index ("node",
1946 +                              ((struct dx_node *) bh2->b_data)->entries));
1947 +                       err = ext3_journal_dirty_metadata(handle, bh2);
1948 +                       if (err)
1949 +                               goto journal_error;
1950 +                       brelse (bh2);
1951 +               } else {
1952 +                       dxtrace(printk("Creating second level index...\n"));
1953 +                       memcpy((char *) entries2, (char *) entries,
1954 +                              icount * sizeof(struct dx_entry));
1955 +                       dx_set_limit(entries2, dx_node_limit(dir));
1956 +
1957 +                       /* Set up root */
1958 +                       dx_set_count(entries, 1);
1959 +                       dx_set_block(entries + 0, newblock);
1960 +                       ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1961 +
1962 +                       /* Add new access path frame */
1963 +                       frame = frames + 1;
1964 +                       frame->at = at = at - entries + entries2;
1965 +                       frame->entries = entries = entries2;
1966 +                       frame->bh = bh2;
1967 +                       err = ext3_journal_get_write_access(handle,
1968 +                                                            frame->bh);
1969 +                       if (err)
1970 +                               goto journal_error;
1971                 }
1972 -               offset += le16_to_cpu(de->rec_len);
1973 -               de = (struct ext3_dir_entry_2 *)
1974 -                       ((char *) de + le16_to_cpu(de->rec_len));
1975 +               ext3_journal_dirty_metadata(handle, frames[0].bh);
1976         }
1977 -       brelse (bh);
1978 -       return -ENOSPC;
1979 +       de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1980 +       if (!de)
1981 +               goto cleanup;
1982 +       err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1983 +       bh = 0;
1984 +       goto cleanup;
1985 +       
1986 +journal_error:
1987 +       ext3_std_error(dir->i_sb, err);
1988 +cleanup:
1989 +       if (bh)
1990 +               brelse(bh);
1991 +       dx_release(frames);
1992 +       return err;
1993  }
1994 +#endif
1995  
1996  /*
1997   * ext3_delete_entry deletes a directory entry by merging it with the
1998 @@ -452,9 +1543,11 @@ static int ext3_create (struct inode * d
1999         struct inode * inode;
2000         int err;
2001  
2002 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2003 -       if (IS_ERR(handle))
2004 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2005 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2006 +       if (IS_ERR(handle)) {
2007                 return PTR_ERR(handle);
2008 +       }
2009  
2010         if (IS_SYNC(dir))
2011                 handle->h_sync = 1;
2012 @@ -479,9 +1572,11 @@ static int ext3_mknod (struct inode * di
2013         struct inode *inode;
2014         int err;
2015  
2016 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2017 -       if (IS_ERR(handle))
2018 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2019 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2020 +       if (IS_ERR(handle)) {
2021                 return PTR_ERR(handle);
2022 +       }
2023  
2024         if (IS_SYNC(dir))
2025                 handle->h_sync = 1;
2026 @@ -508,9 +1603,11 @@ static int ext3_mkdir(struct inode * dir
2027         if (dir->i_nlink >= EXT3_LINK_MAX)
2028                 return -EMLINK;
2029  
2030 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2031 -       if (IS_ERR(handle))
2032 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2033 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2034 +       if (IS_ERR(handle)) {
2035                 return PTR_ERR(handle);
2036 +       }
2037  
2038         if (IS_SYNC(dir))
2039                 handle->h_sync = 1;
2040 @@ -522,7 +1619,7 @@ static int ext3_mkdir(struct inode * dir
2041  
2042         inode->i_op = &ext3_dir_inode_operations;
2043         inode->i_fop = &ext3_dir_operations;
2044 -       inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2045 +       inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2046         inode->i_blocks = 0;    
2047         dir_block = ext3_bread (handle, inode, 0, 1, &err);
2048         if (!dir_block) {
2049 @@ -555,21 +1652,19 @@ static int ext3_mkdir(struct inode * dir
2050                 inode->i_mode |= S_ISGID;
2051         ext3_mark_inode_dirty(handle, inode);
2052         err = ext3_add_entry (handle, dentry, inode);
2053 -       if (err)
2054 -               goto out_no_entry;
2055 +       if (err) {
2056 +               inode->i_nlink = 0;
2057 +               ext3_mark_inode_dirty(handle, inode);
2058 +               iput (inode);
2059 +               goto out_stop;
2060 +       }
2061         dir->i_nlink++;
2062 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2063 +       ext3_update_dx_flag(dir);
2064         ext3_mark_inode_dirty(handle, dir);
2065         d_instantiate(dentry, inode);
2066  out_stop:
2067         ext3_journal_stop(handle, dir);
2068         return err;
2069 -
2070 -out_no_entry:
2071 -       inode->i_nlink = 0;
2072 -       ext3_mark_inode_dirty(handle, inode);
2073 -       iput (inode);
2074 -       goto out_stop;
2075  }
2076  
2077  /*
2078 @@ -656,7 +1751,7 @@ int ext3_orphan_add(handle_t *handle, st
2079         int err = 0, rc;
2080         
2081         lock_super(sb);
2082 -       if (!list_empty(&inode->u.ext3_i.i_orphan))
2083 +       if (!list_empty(&EXT3_I(inode)->i_orphan))
2084                 goto out_unlock;
2085  
2086         /* Orphan handling is only valid for files with data blocks
2087 @@ -697,7 +1792,7 @@ int ext3_orphan_add(handle_t *handle, st
2088          * This is safe: on error we're going to ignore the orphan list
2089          * anyway on the next recovery. */
2090         if (!err)
2091 -               list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2092 +               list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2093  
2094         jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2095         jbd_debug(4, "orphan inode %ld will point to %d\n",
2096 @@ -715,25 +1810,26 @@ out_unlock:
2097  int ext3_orphan_del(handle_t *handle, struct inode *inode)
2098  {
2099         struct list_head *prev;
2100 +       struct ext3_inode_info *ei = EXT3_I(inode);
2101         struct ext3_sb_info *sbi;
2102         ino_t ino_next; 
2103         struct ext3_iloc iloc;
2104         int err = 0;
2105         
2106         lock_super(inode->i_sb);
2107 -       if (list_empty(&inode->u.ext3_i.i_orphan)) {
2108 +       if (list_empty(&ei->i_orphan)) {
2109                 unlock_super(inode->i_sb);
2110                 return 0;
2111         }
2112  
2113         ino_next = NEXT_ORPHAN(inode);
2114 -       prev = inode->u.ext3_i.i_orphan.prev;
2115 +       prev = ei->i_orphan.prev;
2116         sbi = EXT3_SB(inode->i_sb);
2117  
2118         jbd_debug(4, "remove inode %ld from orphan list\n", inode->i_ino);
2119  
2120 -       list_del(&inode->u.ext3_i.i_orphan);
2121 -       INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2122 +       list_del(&ei->i_orphan);
2123 +       INIT_LIST_HEAD(&ei->i_orphan);
2124  
2125         /* If we're on an error path, we may not have a valid
2126          * transaction handle with which to update the orphan list on
2127 @@ -794,8 +1890,9 @@ static int ext3_rmdir (struct inode * di
2128         handle_t *handle;
2129  
2130         handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2131 -       if (IS_ERR(handle))
2132 +       if (IS_ERR(handle)) {
2133                 return PTR_ERR(handle);
2134 +       }
2135  
2136         retval = -ENOENT;
2137         bh = ext3_find_entry (dentry, &de);
2138 @@ -833,7 +1930,7 @@ static int ext3_rmdir (struct inode * di
2139         dir->i_nlink--;
2140         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2141         ext3_mark_inode_dirty(handle, inode);
2142 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2143 +       ext3_update_dx_flag(dir);
2144         ext3_mark_inode_dirty(handle, dir);
2145  
2146  end_rmdir:
2147 @@ -851,8 +1948,9 @@ static int ext3_unlink(struct inode * di
2148         handle_t *handle;
2149  
2150         handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2151 -       if (IS_ERR(handle))
2152 +       if (IS_ERR(handle)) {
2153                 return PTR_ERR(handle);
2154 +       }
2155  
2156         if (IS_SYNC(dir))
2157                 handle->h_sync = 1;
2158 @@ -879,7 +1977,7 @@ static int ext3_unlink(struct inode * di
2159         if (retval)
2160                 goto end_unlink;
2161         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2162 -       dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2163 +       ext3_update_dx_flag(dir);
2164         ext3_mark_inode_dirty(handle, dir);
2165         inode->i_nlink--;
2166         if (!inode->i_nlink)
2167 @@ -905,9 +2003,11 @@ static int ext3_symlink (struct inode * 
2168         if (l > dir->i_sb->s_blocksize)
2169                 return -ENAMETOOLONG;
2170  
2171 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2172 -       if (IS_ERR(handle))
2173 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2174 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2175 +       if (IS_ERR(handle)) {
2176                 return PTR_ERR(handle);
2177 +       }
2178  
2179         if (IS_SYNC(dir))
2180                 handle->h_sync = 1;
2181 @@ -917,7 +2017,7 @@ static int ext3_symlink (struct inode * 
2182         if (IS_ERR(inode))
2183                 goto out_stop;
2184  
2185 -       if (l > sizeof (inode->u.ext3_i.i_data)) {
2186 +       if (l > sizeof (EXT3_I(inode)->i_data)) {
2187                 inode->i_op = &page_symlink_inode_operations;
2188                 inode->i_mapping->a_ops = &ext3_aops;
2189                 /*
2190 @@ -926,25 +2026,23 @@ static int ext3_symlink (struct inode * 
2191                  * i_size in generic_commit_write().
2192                  */
2193                 err = block_symlink(inode, symname, l);
2194 -               if (err)
2195 -                       goto out_no_entry;
2196 +               if (err) {
2197 +                       ext3_dec_count(handle, inode);
2198 +                       ext3_mark_inode_dirty(handle, inode);
2199 +                       iput (inode);
2200 +                       goto out_stop;
2201 +               }
2202         } else {
2203                 inode->i_op = &ext3_fast_symlink_inode_operations;
2204 -               memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2205 +               memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2206                 inode->i_size = l-1;
2207         }
2208 -       inode->u.ext3_i.i_disksize = inode->i_size;
2209 +       EXT3_I(inode)->i_disksize = inode->i_size;
2210         err = ext3_add_nondir(handle, dentry, inode);
2211         ext3_mark_inode_dirty(handle, inode);
2212  out_stop:
2213         ext3_journal_stop(handle, dir);
2214         return err;
2215 -
2216 -out_no_entry:
2217 -       ext3_dec_count(handle, inode);
2218 -       ext3_mark_inode_dirty(handle, inode);
2219 -       iput (inode);
2220 -       goto out_stop;
2221  }
2222  
2223  static int ext3_link (struct dentry * old_dentry,
2224 @@ -957,12 +2055,15 @@ static int ext3_link (struct dentry * ol
2225         if (S_ISDIR(inode->i_mode))
2226                 return -EPERM;
2227  
2228 -       if (inode->i_nlink >= EXT3_LINK_MAX)
2229 +       if (inode->i_nlink >= EXT3_LINK_MAX) {
2230                 return -EMLINK;
2231 +       }
2232  
2233 -       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2234 -       if (IS_ERR(handle))
2235 +       handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2236 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2237 +       if (IS_ERR(handle)) {
2238                 return PTR_ERR(handle);
2239 +       }
2240  
2241         if (IS_SYNC(dir))
2242                 handle->h_sync = 1;
2243 @@ -996,9 +2097,11 @@ static int ext3_rename (struct inode * o
2244  
2245         old_bh = new_bh = dir_bh = NULL;
2246  
2247 -       handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2248 -       if (IS_ERR(handle))
2249 +       handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2250 +                                       EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2251 +       if (IS_ERR(handle)) {
2252                 return PTR_ERR(handle);
2253 +       }
2254  
2255         if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2256                 handle->h_sync = 1;
2257 @@ -1071,14 +2174,37 @@ static int ext3_rename (struct inode * o
2258         /*
2259          * ok, that's it
2260          */
2261 -       ext3_delete_entry(handle, old_dir, old_de, old_bh);
2262 +       if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2263 +           old_de->name_len != old_dentry->d_name.len ||
2264 +           strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2265 +           (retval = ext3_delete_entry(handle, old_dir,
2266 +                                       old_de, old_bh)) == -ENOENT) {
2267 +               /* old_de could have moved from under us during htree split, so
2268 +                * make sure that we are deleting the right entry.  We might
2269 +                * also be pointing to a stale entry in the unused part of
2270 +                * old_bh so just checking inum and the name isn't enough. */
2271 +               struct buffer_head *old_bh2;
2272 +               struct ext3_dir_entry_2 *old_de2;
2273 +
2274 +               old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2275 +               if (old_bh2) {
2276 +                       retval = ext3_delete_entry(handle, old_dir,
2277 +                                                  old_de2, old_bh2);
2278 +                       brelse(old_bh2);
2279 +               }
2280 +       }
2281 +       if (retval) {
2282 +               ext3_warning(old_dir->i_sb, "ext3_rename",
2283 +                               "Deleting old file (%lu), %d, error=%d",
2284 +                               old_dir->i_ino, old_dir->i_nlink, retval);
2285 +       }
2286  
2287         if (new_inode) {
2288                 new_inode->i_nlink--;
2289                 new_inode->i_ctime = CURRENT_TIME;
2290         }
2291         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2292 -       old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2293 +       ext3_update_dx_flag(old_dir);
2294         if (dir_bh) {
2295                 BUFFER_TRACE(dir_bh, "get_write_access");
2296                 ext3_journal_get_write_access(handle, dir_bh);
2297 @@ -1090,7 +2212,7 @@ static int ext3_rename (struct inode * o
2298                         new_inode->i_nlink--;
2299                 } else {
2300                         new_dir->i_nlink++;
2301 -                       new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2302 +                       ext3_update_dx_flag(new_dir);
2303                         ext3_mark_inode_dirty(handle, new_dir);
2304                 }
2305         }
2306 --- linux-2.4.18-chaos-pdirops/fs/ext3/super.c~ext3-htree       2003-09-23 11:52:24.000000000 +0400
2307 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/super.c   2003-09-23 12:11:54.000000000 +0400
2308 @@ -705,6 +705,7 @@ static int ext3_setup_super(struct super
2309         es->s_mtime = cpu_to_le32(CURRENT_TIME);
2310         ext3_update_dynamic_rev(sb);
2311         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2312 +
2313         ext3_commit_super (sb, es, 1);
2314         if (test_opt (sb, DEBUG))
2315                 printk (KERN_INFO
2316 @@ -715,6 +716,7 @@ static int ext3_setup_super(struct super
2317                         EXT3_BLOCKS_PER_GROUP(sb),
2318                         EXT3_INODES_PER_GROUP(sb),
2319                         sbi->s_mount_opt);
2320 +
2321         printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2322                                 bdevname(sb->s_dev));
2323         if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2324 @@ -889,6 +891,7 @@ static loff_t ext3_max_size(int bits)
2325         return res;
2326  }
2327  
2328 +
2329  struct super_block * ext3_read_super (struct super_block * sb, void * data,
2330                                       int silent)
2331  {
2332 @@ -1065,6 +1068,9 @@ struct super_block * ext3_read_super (st
2333         sbi->s_mount_state = le16_to_cpu(es->s_state);
2334         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2335         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2336 +       for (i=0; i < 4; i++)
2337 +               sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2338 +       sbi->s_def_hash_version = es->s_def_hash_version;
2339  
2340         if (sbi->s_blocks_per_group > blocksize * 8) {
2341                 printk (KERN_ERR
2342 @@ -1747,6 +1753,7 @@ static void __exit exit_ext3_fs(void)
2343         unregister_filesystem(&ext3_fs_type);
2344  }
2345  
2346 +EXPORT_SYMBOL(ext3_force_commit);
2347  EXPORT_SYMBOL(ext3_bread);
2348  
2349  MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2350 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_fs.h~ext3-htree       2003-07-28 17:52:17.000000000 +0400
2351 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_fs.h   2003-09-23 12:12:04.000000000 +0400
2352 @@ -40,6 +40,11 @@
2353  #define EXT3FS_VERSION         "2.4-0.9.18"
2354  
2355  /*
2356 + * Always enable hashed directories
2357 + */
2358 +#define CONFIG_EXT3_INDEX
2359 +
2360 +/*
2361   * Debug code
2362   */
2363  #ifdef EXT3FS_DEBUG
2364 @@ -437,8 +442,11 @@ struct ext3_super_block {
2365  /*E0*/ __u32   s_journal_inum;         /* inode number of journal file */
2366         __u32   s_journal_dev;          /* device number of journal file */
2367         __u32   s_last_orphan;          /* start of list of inodes to delete */
2368 -
2369 -/*EC*/ __u32   s_reserved[197];        /* Padding to the end of the block */
2370 +       __u32   s_hash_seed[4];         /* HTREE hash seed */
2371 +       __u8    s_def_hash_version;     /* Default hash version to use */
2372 +       __u8    s_reserved_char_pad;
2373 +       __u16   s_reserved_word_pad;
2374 +       __u32   s_reserved[192];        /* Padding to the end of the block */
2375  };
2376  
2377  #ifdef __KERNEL__
2378 @@ -575,9 +583,46 @@ struct ext3_dir_entry_2 {
2379  #define EXT3_DIR_ROUND                 (EXT3_DIR_PAD - 1)
2380  #define EXT3_DIR_REC_LEN(name_len)     (((name_len) + 8 + EXT3_DIR_ROUND) & \
2381                                          ~EXT3_DIR_ROUND)
2382 +/*
2383 + * Hash Tree Directory indexing
2384 + * (c) Daniel Phillips, 2001
2385 + */
2386 +
2387 +#ifdef CONFIG_EXT3_INDEX
2388 +  #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2389 +                                             EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2390 +                     (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2391 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2392 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2393 +#else
2394 +  #define is_dx(dir) 0
2395 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2396 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2397 +#endif
2398 +
2399 +/* Legal values for the dx_root hash_version field: */
2400 +
2401 +#define DX_HASH_LEGACY         0
2402 +#define DX_HASH_HALF_MD4       1
2403 +#define DX_HASH_TEA            2
2404 +
2405 +/* hash info structure used by the directory hash */
2406 +struct dx_hash_info
2407 +{
2408 +       u32             hash;
2409 +       u32             minor_hash;
2410 +       int             hash_version;
2411 +       u32             *seed;
2412 +};
2413  
2414  #ifdef __KERNEL__
2415  /*
2416 + * Control parameters used by ext3_htree_next_block
2417 + */
2418 +#define HASH_NB_ALWAYS         1
2419 +
2420 +
2421 +/*
2422   * Describe an inode's exact location on disk and in memory
2423   */
2424  struct ext3_iloc
2425 @@ -587,6 +632,27 @@ struct ext3_iloc
2426         unsigned long block_group;
2427  };
2428  
2429 +
2430 +/*
2431 + * This structure is stuffed into the struct file's private_data field
2432 + * for directories.  It is where we put information so that we can do
2433 + * readdir operations in hash tree order.
2434 + */
2435 +struct dir_private_info {
2436 +       rb_root_t       root;
2437 +       rb_node_t       *curr_node;
2438 +       struct fname    *extra_fname;
2439 +       loff_t          last_pos;
2440 +       __u32           curr_hash;
2441 +       __u32           curr_minor_hash;
2442 +       __u32           next_hash;
2443 +};
2444 +
2445 +/*
2446 + * Special error return code only used by dx_probe() and its callers.
2447 + */
2448 +#define ERR_BAD_DX_DIR -75000
2449 +
2450  /*
2451   * Function prototypes
2452   */
2453 @@ -614,11 +680,20 @@ extern struct ext3_group_desc * ext3_get
2454  
2455  /* dir.c */
2456  extern int ext3_check_dir_entry(const char *, struct inode *,
2457 -                               struct ext3_dir_entry_2 *, struct buffer_head *,
2458 -                               unsigned long);
2459 +                               struct ext3_dir_entry_2 *,
2460 +                               struct buffer_head *, unsigned long);
2461 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2462 +                                   __u32 minor_hash,
2463 +                                   struct ext3_dir_entry_2 *dirent);
2464 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2465 +
2466  /* fsync.c */
2467  extern int ext3_sync_file (struct file *, struct dentry *, int);
2468  
2469 +/* hash.c */
2470 +extern int ext3fs_dirhash(const char *name, int len, struct
2471 +                         dx_hash_info *hinfo);
2472 +
2473  /* ialloc.c */
2474  extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2475  extern void ext3_free_inode (handle_t *, struct inode *);
2476 @@ -650,6 +725,8 @@ extern int ext3_ioctl (struct inode *, s
2477  /* namei.c */
2478  extern int ext3_orphan_add(handle_t *, struct inode *);
2479  extern int ext3_orphan_del(handle_t *, struct inode *);
2480 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2481 +                               __u32 start_minor_hash, __u32 *next_hash);
2482  
2483  /* super.c */
2484  extern void ext3_error (struct super_block *, const char *, const char *, ...)
2485 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_fs_sb.h~ext3-htree    2003-07-28 17:52:17.000000000 +0400
2486 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_fs_sb.h        2003-09-23 12:11:54.000000000 +0400
2487 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2488         int s_inode_size;
2489         int s_first_ino;
2490         u32 s_next_generation;
2491 +       u32 s_hash_seed[4];
2492 +       int s_def_hash_version;
2493  
2494         /* Journaling */
2495         struct inode * s_journal_inode;
2496 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_jbd.h~ext3-htree      2001-12-21 20:42:03.000000000 +0300
2497 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_jbd.h  2003-09-23 12:11:54.000000000 +0400
2498 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2499  
2500  #define EXT3_RESERVE_TRANS_BLOCKS      12
2501  
2502 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS  8
2503 +
2504  int
2505  ext3_mark_iloc_dirty(handle_t *handle, 
2506                      struct inode *inode,
2507 --- linux-2.4.18-chaos-pdirops/include/linux/rbtree.h~ext3-htree        2001-11-22 22:46:18.000000000 +0300
2508 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/rbtree.h    2003-09-23 12:11:54.000000000 +0400
2509 @@ -120,6 +120,8 @@ rb_root_t;
2510  
2511  extern void rb_insert_color(rb_node_t *, rb_root_t *);
2512  extern void rb_erase(rb_node_t *, rb_root_t *);
2513 +extern rb_node_t *rb_get_first(rb_root_t *root);
2514 +extern rb_node_t *rb_get_next(rb_node_t *n);
2515  
2516  static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2517  {
2518 --- linux-2.4.18-chaos-pdirops/lib/rbtree.c~ext3-htree  2003-09-23 11:52:34.000000000 +0400
2519 +++ linux-2.4.18-chaos-pdirops-alexey/lib/rbtree.c      2003-09-23 12:11:54.000000000 +0400
2520 @@ -17,6 +17,8 @@
2521    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2522  
2523    linux/lib/rbtree.c
2524 +
2525 +  rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2526  */
2527  
2528  #include <linux/rbtree.h>
2529 @@ -295,3 +297,43 @@ void rb_erase(rb_node_t * node, rb_root_
2530                 __rb_erase_color(child, parent, root);
2531  }
2532  EXPORT_SYMBOL(rb_erase);
2533 +
2534 +/*
2535 + * This function returns the first node (in sort order) of the tree.
2536 + */
2537 +rb_node_t *rb_get_first(rb_root_t *root)
2538 +{
2539 +       rb_node_t       *n;
2540 +
2541 +       n = root->rb_node;
2542 +       if (!n)
2543 +               return 0;
2544 +       while (n->rb_left)
2545 +               n = n->rb_left;
2546 +       return n;
2547 +}
2548 +EXPORT_SYMBOL(rb_get_first);
2549 +
2550 +/*
2551 + * Given a node, this function will return the next node in the tree.
2552 + */
2553 +rb_node_t *rb_get_next(rb_node_t *n)
2554 +{
2555 +       rb_node_t       *parent;
2556 +
2557 +       if (n->rb_right) {
2558 +               n = n->rb_right;
2559 +               while (n->rb_left)
2560 +                       n = n->rb_left;
2561 +               return n;
2562 +       } else {
2563 +               while ((parent = n->rb_parent)) {
2564 +                       if (n == parent->rb_left)
2565 +                               return parent;
2566 +                       n = parent;
2567 +               }
2568 +               return 0;
2569 +       }
2570 +}
2571 +EXPORT_SYMBOL(rb_get_next);
2572 +
2573
2574 _