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