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