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