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