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