2 fs/ext3/dir.c | 299 +++++++++
4 fs/ext3/hash.c | 215 ++++++
5 fs/ext3/namei.c | 1388 ++++++++++++++++++++++++++++++++++++++++-----
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
12 11 files changed, 1887 insertions(+), 160 deletions(-)
14 Index: linux-2.4.19/fs/ext3/Makefile
15 ===================================================================
16 --- linux-2.4.19.orig/fs/ext3/Makefile 2004-04-23 18:25:03.000000000 -0400
17 +++ linux-2.4.19/fs/ext3/Makefile 2004-04-23 18:26:27.000000000 -0400
19 export-objs := super.o inode.o
21 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
22 - ioctl.o namei.o super.o symlink.o
23 + ioctl.o namei.o super.o symlink.o hash.o
26 obj-$(CONFIG_EXT3_FS_XATTR) += xattr.o
27 Index: linux-2.4.19/fs/ext3/dir.c
28 ===================================================================
29 --- linux-2.4.19.orig/fs/ext3/dir.c 2001-11-09 17:25:04.000000000 -0500
30 +++ linux-2.4.19/fs/ext3/dir.c 2004-04-23 18:26:27.000000000 -0400
33 #include <linux/jbd.h>
34 #include <linux/ext3_fs.h>
35 +#include <linux/slab.h>
36 +#include <linux/rbtree.h>
38 static unsigned char ext3_filetype_table[] = {
39 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
42 static int ext3_readdir(struct file *, void *, filldir_t);
43 +static int ext3_dx_readdir(struct file * filp,
44 + void * dirent, filldir_t filldir);
46 struct file_operations ext3_dir_operations = {
47 read: generic_read_dir,
49 fsync: ext3_sync_file, /* BKL held */
53 +static unsigned char get_dtype(struct super_block *sb, int filetype)
55 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
56 + (filetype >= EXT3_FT_MAX))
59 + return (ext3_filetype_table[filetype]);
63 int ext3_check_dir_entry (const char * function, struct inode * dir,
64 struct ext3_dir_entry_2 * de,
65 struct buffer_head * bh,
71 + err = ext3_dx_readdir(filp, dirent, filldir);
72 + if (err != ERR_BAD_DX_DIR)
75 + * We don't set the inode dirty flag since it's not
76 + * critical that it get flushed back to the disk.
78 + EXT3_I(filp->f_dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
82 offset = filp->f_pos & (sb->s_blocksize - 1);
84 * during the copy operation.
86 unsigned long version = filp->f_version;
87 - unsigned char d_type = DT_UNKNOWN;
89 - if (EXT3_HAS_INCOMPAT_FEATURE(sb,
90 - EXT3_FEATURE_INCOMPAT_FILETYPE)
91 - && de->file_type < EXT3_FT_MAX)
93 - ext3_filetype_table[de->file_type];
94 error = filldir(dirent, de->name,
97 le32_to_cpu(de->inode),
99 + get_dtype(sb, de->file_type));
102 if (version != filp->f_version)
103 @@ -188,3 +207,269 @@
108 +#ifdef CONFIG_EXT3_INDEX
110 + * These functions convert from the major/minor hash to an f_pos
113 + * Currently we only use major hash numer. This is unfortunate, but
114 + * on 32-bit machines, the same VFS interface is used for lseek and
115 + * llseek, so if we use the 64 bit offset, then the 32-bit versions of
116 + * lseek/telldir/seekdir will blow out spectacularly, and from within
117 + * the ext2 low-level routine, we don't know if we're being called by
118 + * a 64-bit version of the system call or the 32-bit version of the
119 + * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
122 +#define hash2pos(major, minor) (major >> 1)
123 +#define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
124 +#define pos2min_hash(pos) (0)
127 + * This structure holds the nodes of the red-black tree used to store
128 + * the directory entry in hash order.
134 + struct fname *next;
142 + * This functoin implements a non-recursive way of freeing all of the
143 + * nodes in the red-black tree.
145 +static void free_rb_tree_fname(rb_root_t *root)
147 + rb_node_t *n = root->rb_node;
149 + struct fname *fname;
152 + /* Do the node's children first */
153 + if ((n)->rb_left) {
162 + * The node has no children; free it, and then zero
163 + * out parent's link to it. Finally go to the
164 + * beginning of the loop and try to free the parent
167 + parent = n->rb_parent;
168 + fname = rb_entry(n, struct fname, rb_hash);
172 + else if (parent->rb_left == n)
173 + parent->rb_left = 0;
174 + else if (parent->rb_right == n)
175 + parent->rb_right = 0;
182 +struct dir_private_info *create_dir_info(loff_t pos)
184 + struct dir_private_info *p;
186 + p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
189 + p->root.rb_node = 0;
191 + p->extra_fname = 0;
193 + p->curr_hash = pos2maj_hash(pos);
194 + p->curr_minor_hash = pos2min_hash(pos);
199 +void ext3_htree_free_dir_info(struct dir_private_info *p)
201 + free_rb_tree_fname(&p->root);
206 + * Given a directory entry, enter it into the fname rb tree.
208 +void ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
210 + struct ext3_dir_entry_2 *dirent)
212 + rb_node_t **p, *parent = NULL;
213 + struct fname * fname, *new_fn;
214 + struct dir_private_info *info;
217 + info = (struct dir_private_info *) dir_file->private_data;
218 + p = &info->root.rb_node;
220 + /* Create and allocate the fname structure */
221 + len = sizeof(struct fname) + dirent->name_len + 1;
222 + new_fn = kmalloc(len, GFP_KERNEL);
223 + memset(new_fn, 0, len);
224 + new_fn->hash = hash;
225 + new_fn->minor_hash = minor_hash;
226 + new_fn->inode = le32_to_cpu(dirent->inode);
227 + new_fn->name_len = dirent->name_len;
228 + new_fn->file_type = dirent->file_type;
229 + memcpy(new_fn->name, dirent->name, dirent->name_len);
230 + new_fn->name[dirent->name_len] = 0;
234 + fname = rb_entry(parent, struct fname, rb_hash);
237 + * If the hash and minor hash match up, then we put
238 + * them on a linked list. This rarely happens...
240 + if ((new_fn->hash == fname->hash) &&
241 + (new_fn->minor_hash == fname->minor_hash)) {
242 + new_fn->next = fname->next;
243 + fname->next = new_fn;
247 + if (new_fn->hash < fname->hash)
248 + p = &(*p)->rb_left;
249 + else if (new_fn->hash > fname->hash)
250 + p = &(*p)->rb_right;
251 + else if (new_fn->minor_hash < fname->minor_hash)
252 + p = &(*p)->rb_left;
253 + else /* if (new_fn->minor_hash > fname->minor_hash) */
254 + p = &(*p)->rb_right;
257 + rb_link_node(&new_fn->rb_hash, parent, p);
258 + rb_insert_color(&new_fn->rb_hash, &info->root);
264 + * This is a helper function for ext3_dx_readdir. It calls filldir
265 + * for all entres on the fname linked list. (Normally there is only
266 + * one entry on the linked list, unless there are 62 bit hash collisions.)
268 +static int call_filldir(struct file * filp, void * dirent,
269 + filldir_t filldir, struct fname *fname)
271 + struct dir_private_info *info = filp->private_data;
273 + struct inode *inode = filp->f_dentry->d_inode;
274 + struct super_block * sb;
280 + printk("call_filldir: called with null fname?!?\n");
283 + curr_pos = hash2pos(fname->hash, fname->minor_hash);
285 + error = filldir(dirent, fname->name,
286 + fname->name_len, curr_pos,
288 + get_dtype(sb, fname->file_type));
290 + filp->f_pos = curr_pos;
291 + info->extra_fname = fname->next;
294 + fname = fname->next;
299 +static int ext3_dx_readdir(struct file * filp,
300 + void * dirent, filldir_t filldir)
302 + struct dir_private_info *info = filp->private_data;
303 + struct inode *inode = filp->f_dentry->d_inode;
304 + struct fname *fname;
308 + info = create_dir_info(filp->f_pos);
311 + filp->private_data = info;
314 + /* Some one has messed with f_pos; reset the world */
315 + if (info->last_pos != filp->f_pos) {
316 + free_rb_tree_fname(&info->root);
317 + info->curr_node = 0;
318 + info->extra_fname = 0;
319 + info->curr_hash = pos2maj_hash(filp->f_pos);
320 + info->curr_minor_hash = pos2min_hash(filp->f_pos);
324 + * If there are any leftover names on the hash collision
325 + * chain, return them first.
327 + if (info->extra_fname &&
328 + call_filldir(filp, dirent, filldir, info->extra_fname))
331 + if (!info->curr_node)
332 + info->curr_node = rb_get_first(&info->root);
336 + * Fill the rbtree if we have no more entries,
337 + * or the inode has changed since we last read in the
340 + if ((!info->curr_node) ||
341 + (filp->f_version != inode->i_version)) {
342 + info->curr_node = 0;
343 + free_rb_tree_fname(&info->root);
344 + filp->f_version = inode->i_version;
345 + ret = ext3_htree_fill_tree(filp, info->curr_hash,
346 + info->curr_minor_hash,
352 + info->curr_node = rb_get_first(&info->root);
355 + fname = rb_entry(info->curr_node, struct fname, rb_hash);
356 + info->curr_hash = fname->hash;
357 + info->curr_minor_hash = fname->minor_hash;
358 + if (call_filldir(filp, dirent, filldir, fname))
361 + info->curr_node = rb_get_next(info->curr_node);
362 + if (!info->curr_node) {
363 + info->curr_hash = info->next_hash;
364 + info->curr_minor_hash = 0;
368 + info->last_pos = filp->f_pos;
369 + UPDATE_ATIME(inode);
373 Index: linux-2.4.19/fs/ext3/file.c
374 ===================================================================
375 --- linux-2.4.19.orig/fs/ext3/file.c 2004-04-23 17:54:02.000000000 -0400
376 +++ linux-2.4.19/fs/ext3/file.c 2004-04-23 18:26:27.000000000 -0400
379 if (filp->f_mode & FMODE_WRITE)
380 ext3_discard_prealloc (inode);
381 + if (is_dx(inode) && filp->private_data)
382 + ext3_htree_free_dir_info(filp->private_data);
387 Index: linux-2.4.19/fs/ext3/hash.c
388 ===================================================================
389 --- linux-2.4.19.orig/fs/ext3/hash.c 2003-01-30 05:24:37.000000000 -0500
390 +++ linux-2.4.19/fs/ext3/hash.c 2004-04-23 18:26:27.000000000 -0400
393 + * linux/fs/ext3/hash.c
395 + * Copyright (C) 2002 by Theodore Ts'o
397 + * This file is released under the GPL v2.
399 + * This file may be redistributed under the terms of the GNU Public
403 +#include <linux/fs.h>
404 +#include <linux/jbd.h>
405 +#include <linux/sched.h>
406 +#include <linux/ext3_fs.h>
408 +#define DELTA 0x9E3779B9
410 +static void TEA_transform(__u32 buf[4], __u32 const in[])
413 + __u32 b0 = buf[0], b1 = buf[1];
414 + __u32 a = in[0], b = in[1], c = in[2], d = in[3];
419 + b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
420 + b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
427 +/* F, G and H are basic MD4 functions: selection, majority, parity */
428 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
429 +#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
430 +#define H(x, y, z) ((x) ^ (y) ^ (z))
433 + * The generic round function. The application is so specific that
434 + * we don't bother protecting all the arguments with parens, as is generally
435 + * good macro practice, in favor of extra legibility.
436 + * Rotation is separate from addition to prevent recomputation
438 +#define ROUND(f, a, b, c, d, x, s) \
439 + (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
441 +#define K2 013240474631UL
442 +#define K3 015666365641UL
445 + * Basic cut-down MD4 transform. Returns only 32 bits of result.
447 +static void halfMD4Transform (__u32 buf[4], __u32 const in[])
449 + __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
452 + ROUND(F, a, b, c, d, in[0] + K1, 3);
453 + ROUND(F, d, a, b, c, in[1] + K1, 7);
454 + ROUND(F, c, d, a, b, in[2] + K1, 11);
455 + ROUND(F, b, c, d, a, in[3] + K1, 19);
456 + ROUND(F, a, b, c, d, in[4] + K1, 3);
457 + ROUND(F, d, a, b, c, in[5] + K1, 7);
458 + ROUND(F, c, d, a, b, in[6] + K1, 11);
459 + ROUND(F, b, c, d, a, in[7] + K1, 19);
462 + ROUND(G, a, b, c, d, in[1] + K2, 3);
463 + ROUND(G, d, a, b, c, in[3] + K2, 5);
464 + ROUND(G, c, d, a, b, in[5] + K2, 9);
465 + ROUND(G, b, c, d, a, in[7] + K2, 13);
466 + ROUND(G, a, b, c, d, in[0] + K2, 3);
467 + ROUND(G, d, a, b, c, in[2] + K2, 5);
468 + ROUND(G, c, d, a, b, in[4] + K2, 9);
469 + ROUND(G, b, c, d, a, in[6] + K2, 13);
472 + ROUND(H, a, b, c, d, in[3] + K3, 3);
473 + ROUND(H, d, a, b, c, in[7] + K3, 9);
474 + ROUND(H, c, d, a, b, in[2] + K3, 11);
475 + ROUND(H, b, c, d, a, in[6] + K3, 15);
476 + ROUND(H, a, b, c, d, in[1] + K3, 3);
477 + ROUND(H, d, a, b, c, in[5] + K3, 9);
478 + ROUND(H, c, d, a, b, in[0] + K3, 11);
479 + ROUND(H, b, c, d, a, in[4] + K3, 15);
495 +/* The old legacy hash */
496 +static __u32 dx_hack_hash (const char *name, int len)
498 + __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
500 + __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
502 + if (hash & 0x80000000) hash -= 0x7fffffff;
506 + return (hash0 << 1);
509 +static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
514 + pad = (__u32)len | ((__u32)len << 8);
520 + for (i=0; i < len; i++) {
523 + val = msg[i] + (val << 8);
524 + if ((i % 4) == 3) {
537 + * Returns the hash of a filename. If len is 0 and name is NULL, then
538 + * this function can be used to test whether or not a hash version is
541 + * The seed is an 4 longword (32 bits) "secret" which can be used to
542 + * uniquify a hash. If the seed is all zero's, then some default seed
545 + * A particular hash version specifies whether or not the seed is
546 + * represented, and whether or not the returned hash is 32 bits or 64
547 + * bits. 32 bit hashes will return 0 for the minor hash.
549 +int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
552 + __u32 minor_hash = 0;
555 + __u32 in[8], buf[4];
557 + /* Initialize the default seed for the hash checksum functions */
558 + buf[0] = 0x67452301;
559 + buf[1] = 0xefcdab89;
560 + buf[2] = 0x98badcfe;
561 + buf[3] = 0x10325476;
563 + /* Check to see if the seed is all zero's */
565 + for (i=0; i < 4; i++) {
566 + if (hinfo->seed[i])
570 + memcpy(buf, hinfo->seed, sizeof(buf));
573 + switch (hinfo->hash_version) {
574 + case DX_HASH_LEGACY:
575 + hash = dx_hack_hash(name, len);
577 + case DX_HASH_HALF_MD4:
580 + str2hashbuf(p, len, in, 8);
581 + halfMD4Transform(buf, in);
585 + minor_hash = buf[2];
591 + str2hashbuf(p, len, in, 4);
592 + TEA_transform(buf, in);
597 + minor_hash = buf[1];
603 + hinfo->hash = hash & ~1;
604 + hinfo->minor_hash = minor_hash;
607 Index: linux-2.4.19/fs/ext3/namei.c
608 ===================================================================
609 --- linux-2.4.19.orig/fs/ext3/namei.c 2004-04-23 17:53:55.000000000 -0400
610 +++ linux-2.4.19/fs/ext3/namei.c 2004-04-23 22:24:05.000000000 -0400
612 * David S. Miller (davem@caip.rutgers.edu), 1995
613 * Directory entry file type support and forward compatibility hooks
614 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
615 + * Hash Tree Directory indexing (c)
616 + * Daniel Phillips, 2001
617 + * Hash Tree Directory indexing porting
618 + * Christopher Li, 2002
619 + * Hash Tree Directory indexing cleanup
620 + * Theodore Ts'o, 2002
623 #include <linux/fs.h>
625 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
626 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
628 +static struct buffer_head *ext3_append(handle_t *handle,
629 + struct inode *inode,
630 + u32 *block, int *err)
632 + struct buffer_head *bh;
634 + *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
636 + if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
637 + inode->i_size += inode->i_sb->s_blocksize;
638 + EXT3_I(inode)->i_disksize = inode->i_size;
639 + ext3_journal_get_write_access(handle,bh);
645 +#define assert(test) J_ASSERT(test)
649 +#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
652 +typedef struct { u32 v; } le_u32;
653 +typedef struct { u16 v; } le_u16;
656 +#define dxtrace(command) command
658 +#define dxtrace(command)
669 +struct dx_countlimit
682 + * dx_root_info is laid out so that if it should somehow get overlaid by a
683 + * dirent the two low bits of the hash version will be zero. Therefore, the
684 + * hash version mod 4 should never be 0. Sincerely, the paranoia department.
689 + struct fake_dirent dot;
691 + struct fake_dirent dotdot;
692 + char dotdot_name[4];
693 + struct dx_root_info
695 + le_u32 reserved_zero;
697 + u8 info_length; /* 8 */
698 + u8 indirect_levels;
702 + struct dx_entry entries[0];
707 + struct fake_dirent fake;
708 + struct dx_entry entries[0];
714 + struct buffer_head *bh;
715 + struct dx_entry *entries;
716 + struct dx_entry *at;
725 +#ifdef CONFIG_EXT3_INDEX
726 +static inline unsigned dx_get_block (struct dx_entry *entry);
727 +static void dx_set_block (struct dx_entry *entry, unsigned value);
728 +static inline unsigned dx_get_hash (struct dx_entry *entry);
729 +static void dx_set_hash (struct dx_entry *entry, unsigned value);
730 +static unsigned dx_get_count (struct dx_entry *entries);
731 +static unsigned dx_get_limit (struct dx_entry *entries);
732 +static void dx_set_count (struct dx_entry *entries, unsigned value);
733 +static void dx_set_limit (struct dx_entry *entries, unsigned value);
734 +static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
735 +static unsigned dx_node_limit (struct inode *dir);
736 +static struct dx_frame *dx_probe(struct dentry *dentry,
738 + struct dx_hash_info *hinfo,
739 + struct dx_frame *frame,
741 +static void dx_release (struct dx_frame *frames);
742 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
743 + struct dx_hash_info *hinfo, struct dx_map_entry map[]);
744 +static void dx_sort_map(struct dx_map_entry *map, unsigned count);
745 +static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
746 + struct dx_map_entry *offsets, int count);
747 +static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
748 +static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
749 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
750 + struct dx_frame *frame,
751 + struct dx_frame *frames, int *err,
752 + __u32 *start_hash);
753 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
754 + struct ext3_dir_entry_2 **res_dir, int *err);
755 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
756 + struct inode *inode);
759 + * Future: use high four bits of block for coalesce-on-delete flags
760 + * Mask them off for now.
763 +static inline unsigned dx_get_block (struct dx_entry *entry)
765 + return le32_to_cpu(entry->block.v) & 0x00ffffff;
768 +static inline void dx_set_block (struct dx_entry *entry, unsigned value)
770 + entry->block.v = cpu_to_le32(value);
773 +static inline unsigned dx_get_hash (struct dx_entry *entry)
775 + return le32_to_cpu(entry->hash.v);
778 +static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
780 + entry->hash.v = cpu_to_le32(value);
783 +static inline unsigned dx_get_count (struct dx_entry *entries)
785 + return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
788 +static inline unsigned dx_get_limit (struct dx_entry *entries)
790 + return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
793 +static inline void dx_set_count (struct dx_entry *entries, unsigned value)
795 + ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
798 +static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
800 + ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
803 +static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
805 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
806 + EXT3_DIR_REC_LEN(2) - infosize;
807 + return 0? 20: entry_space / sizeof(struct dx_entry);
810 +static inline unsigned dx_node_limit (struct inode *dir)
812 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
813 + return 0? 22: entry_space / sizeof(struct dx_entry);
827 +static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
828 + int size, int show_names)
830 + unsigned names = 0, space = 0;
831 + char *base = (char *) de;
832 + struct dx_hash_info h = *hinfo;
835 + while ((char *) de < base + size)
841 + int len = de->name_len;
842 + char *name = de->name;
843 + while (len--) printk("%c", *name++);
844 + ext3fs_dirhash(de->name, de->name_len, &h);
845 + printk(":%x.%u ", h.hash,
846 + ((char *) de - base));
848 + space += EXT3_DIR_REC_LEN(de->name_len);
851 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
853 + printk("(%i)\n", names);
854 + return (struct stats) { names, space, 1 };
857 +struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
858 + struct dx_entry *entries, int levels)
860 + unsigned blocksize = dir->i_sb->s_blocksize;
861 + unsigned count = dx_get_count (entries), names = 0, space = 0, i;
862 + unsigned bcount = 0;
863 + struct buffer_head *bh;
865 + printk("%i indexed blocks...\n", count);
866 + for (i = 0; i < count; i++, entries++)
868 + u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
869 + u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
870 + struct stats stats;
871 + printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
872 + if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
874 + dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
875 + dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
876 + names += stats.names;
877 + space += stats.space;
878 + bcount += stats.bcount;
882 + printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
883 + names, space/bcount,(space/bcount)*100/blocksize);
884 + return (struct stats) { names, space, bcount};
886 +#endif /* DX_DEBUG */
889 + * Probe for a directory leaf block to search.
891 + * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
892 + * error in the directory index, and the caller should fall back to
893 + * searching the directory normally. The callers of dx_probe **MUST**
894 + * check for this error code, and make sure it never gets reflected
895 + * back to userspace.
897 +static struct dx_frame *
898 +dx_probe(struct dentry *dentry, struct inode *dir,
899 + struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
901 + unsigned count, indirect;
902 + struct dx_entry *at, *entries, *p, *q, *m;
903 + struct dx_root *root;
904 + struct buffer_head *bh;
905 + struct dx_frame *frame = frame_in;
910 + dir = dentry->d_parent->d_inode;
911 + if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
913 + root = (struct dx_root *) bh->b_data;
914 + if (root->info.hash_version != DX_HASH_TEA &&
915 + root->info.hash_version != DX_HASH_HALF_MD4 &&
916 + root->info.hash_version != DX_HASH_LEGACY) {
917 + ext3_warning(dir->i_sb, __FUNCTION__,
918 + "Unrecognised inode hash code %d",
919 + root->info.hash_version);
921 + *err = ERR_BAD_DX_DIR;
924 + hinfo->hash_version = root->info.hash_version;
925 + hinfo->seed = dir->i_sb->u.ext3_sb.s_hash_seed;
927 + ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
928 + hash = hinfo->hash;
930 + if (root->info.unused_flags & 1) {
931 + ext3_warning(dir->i_sb, __FUNCTION__,
932 + "Unimplemented inode hash flags: %#06x",
933 + root->info.unused_flags);
935 + *err = ERR_BAD_DX_DIR;
939 + if ((indirect = root->info.indirect_levels) > 1) {
940 + ext3_warning(dir->i_sb, __FUNCTION__,
941 + "Unimplemented inode hash depth: %#06x",
942 + root->info.indirect_levels);
944 + *err = ERR_BAD_DX_DIR;
948 + entries = (struct dx_entry *) (((char *)&root->info) +
949 + root->info.info_length);
950 + assert(dx_get_limit(entries) == dx_root_limit(dir,
951 + root->info.info_length));
952 + dxtrace (printk("Look up %x", hash));
955 + count = dx_get_count(entries);
956 + assert (count && count <= dx_get_limit(entries));
958 + q = entries + count - 1;
962 + dxtrace(printk("."));
963 + if (dx_get_hash(m) > hash)
969 + if (0) // linear search cross check
971 + unsigned n = count - 1;
975 + dxtrace(printk(","));
976 + if (dx_get_hash(++at) > hash)
982 + assert (at == p - 1);
986 + dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
988 + frame->entries = entries;
990 + if (!indirect--) return frame;
991 + if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
993 + at = entries = ((struct dx_node *) bh->b_data)->entries;
994 + assert (dx_get_limit(entries) == dx_node_limit (dir));
998 + while (frame >= frame_in) {
1006 +static void dx_release (struct dx_frame *frames)
1008 + if (frames[0].bh == NULL)
1011 + if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
1012 + brelse(frames[1].bh);
1013 + brelse(frames[0].bh);
1017 + * This function increments the frame pointer to search the next leaf
1018 + * block, and reads in the necessary intervening nodes if the search
1019 + * should be necessary. Whether or not the search is necessary is
1020 + * controlled by the hash parameter. If the hash value is even, then
1021 + * the search is only continued if the next block starts with that
1022 + * hash value. This is used if we are searching for a specific file.
1024 + * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1026 + * This function returns 1 if the caller should continue to search,
1027 + * or 0 if it should not. If there is an error reading one of the
1028 + * index blocks, it will return -1.
1030 + * If start_hash is non-null, it will be filled in with the starting
1031 + * hash of the next page.
1033 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
1034 + struct dx_frame *frame,
1035 + struct dx_frame *frames, int *err,
1036 + __u32 *start_hash)
1038 + struct dx_frame *p;
1039 + struct buffer_head *bh;
1040 + int num_frames = 0;
1046 + * Find the next leaf page by incrementing the frame pointer.
1047 + * If we run out of entries in the interior node, loop around and
1048 + * increment pointer in the parent node. When we break out of
1049 + * this loop, num_frames indicates the number of interior
1050 + * nodes need to be read.
1053 + if (++(p->at) < p->entries + dx_get_count(p->entries))
1062 + * If the hash is 1, then continue only if the next page has a
1063 + * continuation hash of any value. This is used for readdir
1064 + * handling. Otherwise, check to see if the hash matches the
1065 + * desired contiuation hash. If it doesn't, return since
1066 + * there's no point to read in the successive index pages.
1068 + bhash = dx_get_hash(p->at);
1070 + *start_hash = bhash;
1071 + if ((hash & 1) == 0) {
1072 + if ((bhash & ~1) != hash)
1076 + * If the hash is HASH_NB_ALWAYS, we always go to the next
1077 + * block so no check is necessary
1079 + while (num_frames--) {
1080 + if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
1082 + return -1; /* Failure */
1086 + p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1093 + * p is at least 6 bytes before the end of page
1095 +static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
1097 + return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
1101 + * This function fills a red-black tree with information from a
1102 + * directory. We start scanning the directory in hash order, starting
1103 + * at start_hash and start_minor_hash.
1105 + * This function returns the number of entries inserted into the tree,
1106 + * or a negative error code.
1108 +int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1109 + __u32 start_minor_hash, __u32 *next_hash)
1111 + struct dx_hash_info hinfo;
1112 + struct buffer_head *bh;
1113 + struct ext3_dir_entry_2 *de, *top;
1114 + static struct dx_frame frames[2], *frame;
1115 + struct inode *dir;
1121 + dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
1122 + start_minor_hash));
1123 + dir = dir_file->f_dentry->d_inode;
1124 + hinfo.hash = start_hash;
1125 + hinfo.minor_hash = 0;
1126 + frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
1131 + block = dx_get_block(frame->at);
1132 + dxtrace(printk("Reading block %d\n", block));
1133 + if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
1136 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1137 + top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
1138 + EXT3_DIR_REC_LEN(0));
1139 + for (; de < top; de = ext3_next_entry(de)) {
1140 + ext3fs_dirhash(de->name, de->name_len, &hinfo);
1141 + if ((hinfo.hash < start_hash) ||
1142 + ((hinfo.hash == start_hash) &&
1143 + (hinfo.minor_hash < start_minor_hash)))
1145 + ext3_htree_store_dirent(dir_file, hinfo.hash,
1146 + hinfo.minor_hash, de);
1151 + ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
1152 + frame, frames, &err, &hashval);
1154 + *next_hash = hashval;
1158 + * Stop if: (a) there are no more entries, or
1159 + * (b) we have inserted at least one entry and the
1160 + * next hash value is not a continuation
1163 + (count && ((hashval & 1) == 0)))
1166 + dx_release(frames);
1167 + dxtrace(printk("Fill tree: returned %d entries\n", count));
1170 + dx_release(frames);
1176 + * Directory block splitting, compacting
1179 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
1180 + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
1183 + char *base = (char *) de;
1184 + struct dx_hash_info h = *hinfo;
1186 + while ((char *) de < base + size)
1188 + if (de->name_len && de->inode) {
1189 + ext3fs_dirhash(de->name, de->name_len, &h);
1191 + map_tail->hash = h.hash;
1192 + map_tail->offs = (u32) ((char *) de - base);
1195 + /* XXX: do we need to check rec_len == 0 case? -Chris */
1196 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
1201 +static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1203 + struct dx_map_entry *p, *q, *top = map + count - 1;
1205 + /* Combsort until bubble sort doesn't suck */
1208 + count = count*10/13;
1209 + if (count - 9 < 2) /* 9, 10 -> 11 */
1211 + for (p = top, q = p - count; q >= map; p--, q--)
1212 + if (p->hash < q->hash)
1215 + /* Garden variety bubble sort */
1221 + if (q[1].hash >= q[0].hash)
1229 +static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
1231 + struct dx_entry *entries = frame->entries;
1232 + struct dx_entry *old = frame->at, *new = old + 1;
1233 + int count = dx_get_count(entries);
1235 + assert(count < dx_get_limit(entries));
1236 + assert(old < entries + count);
1237 + memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1238 + dx_set_hash(new, hash);
1239 + dx_set_block(new, block);
1240 + dx_set_count(entries, count + 1);
1245 +static void ext3_update_dx_flag(struct inode *inode)
1247 + if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
1248 + EXT3_FEATURE_COMPAT_DIR_INDEX))
1249 + EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
1253 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
1264 * The returned buffer_head has ->b_count elevated. The caller is expected
1265 * to brelse() it when appropriate.
1269 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
1270 struct ext3_dir_entry_2 ** res_dir)
1272 @@ -121,12 +754,32 @@
1274 int nblocks, i, err;
1275 struct inode *dir = dentry->d_parent->d_inode;
1278 + unsigned blocksize;
1283 + blocksize = sb->s_blocksize;
1284 + namelen = dentry->d_name.len;
1285 + name = dentry->d_name.name;
1286 + if (namelen > EXT3_NAME_LEN)
1288 +#ifdef CONFIG_EXT3_INDEX
1290 + bh = ext3_dx_find_entry(dentry, res_dir, &err);
1292 + * On success, or if the error was file not found,
1293 + * return. Otherwise, fall back to doing a search the
1294 + * old fashioned way.
1296 + if (bh || (err != ERR_BAD_DX_DIR))
1298 + dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
1301 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
1302 - start = dir->u.ext3_i.i_dir_start_lookup;
1303 + start = EXT3_I(dir)->i_dir_start_lookup;
1304 if (start >= nblocks)
1308 i = search_dirblock(bh, dir, dentry,
1309 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
1311 - dir->u.ext3_i.i_dir_start_lookup = block;
1312 + EXT3_I(dir)->i_dir_start_lookup = block;
1314 goto cleanup_and_exit;
1316 @@ -198,6 +851,66 @@
1320 +#ifdef CONFIG_EXT3_INDEX
1321 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
1322 + struct ext3_dir_entry_2 **res_dir, int *err)
1324 + struct super_block * sb;
1325 + struct dx_hash_info hinfo;
1327 + struct dx_frame frames[2], *frame;
1328 + struct ext3_dir_entry_2 *de, *top;
1329 + struct buffer_head *bh;
1330 + unsigned long block;
1332 + int namelen = dentry->d_name.len;
1333 + const u8 *name = dentry->d_name.name;
1334 + struct inode *dir = dentry->d_parent->d_inode;
1337 + if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
1339 + hash = hinfo.hash;
1341 + block = dx_get_block(frame->at);
1342 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1344 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1345 + top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
1346 + EXT3_DIR_REC_LEN(0));
1347 + for (; de < top; de = ext3_next_entry(de))
1348 + if (ext3_match (namelen, name, de)) {
1349 + if (!ext3_check_dir_entry("ext3_find_entry",
1351 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1352 + +((char *)de - bh->b_data))) {
1357 + dx_release (frames);
1361 + /* Check to see if we should continue to search */
1362 + retval = ext3_htree_next_block(dir, hash, frame,
1364 + if (retval == -1) {
1365 + ext3_warning(sb, __FUNCTION__,
1366 + "error reading index page in directory #%lu",
1370 + } while (retval == 1);
1374 + dxtrace(printk("%s not found\n", name));
1375 + dx_release (frames);
1380 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1382 struct inode * inode;
1385 inode = iget(dir->i_sb, ino);
1389 return ERR_PTR(-EACCES);
1392 d_add(dentry, inode);
1394 @@ -239,6 +953,301 @@
1395 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1398 +#ifdef CONFIG_EXT3_INDEX
1399 +static struct ext3_dir_entry_2 *
1400 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1402 + unsigned rec_len = 0;
1405 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1406 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1407 + memcpy (to, de, rec_len);
1408 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1413 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1416 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1418 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1419 + unsigned rec_len = 0;
1422 + while ((char*)de < base + size) {
1423 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1424 + le16_to_cpu(de->rec_len));
1425 + if (de->inode && de->name_len) {
1426 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1428 + memmove(to, de, rec_len);
1429 + to->rec_len = cpu_to_le16(rec_len);
1431 + to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
1438 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1439 + struct buffer_head **bh,struct dx_frame *frame,
1440 + struct dx_hash_info *hinfo, int *error)
1442 + unsigned blocksize = dir->i_sb->s_blocksize;
1443 + unsigned count, continued;
1444 + struct buffer_head *bh2;
1447 + struct dx_map_entry *map;
1448 + char *data1 = (*bh)->b_data, *data2;
1450 + struct ext3_dir_entry_2 *de = NULL, *de2;
1453 + bh2 = ext3_append (handle, dir, &newblock, error);
1460 + BUFFER_TRACE(*bh, "get_write_access");
1461 + err = ext3_journal_get_write_access(handle, *bh);
1467 + ext3_std_error(dir->i_sb, err);
1470 + BUFFER_TRACE(frame->bh, "get_write_access");
1471 + err = ext3_journal_get_write_access(handle, frame->bh);
1473 + goto journal_error;
1475 + data2 = bh2->b_data;
1477 + /* create map in the end of data2 block */
1478 + map = (struct dx_map_entry *) (data2 + blocksize);
1479 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1480 + blocksize, hinfo, map);
1482 + split = count/2; // need to adjust to actual middle
1483 + dx_sort_map (map, count);
1484 + hash2 = map[split].hash;
1485 + continued = hash2 == map[split - 1].hash;
1486 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1487 + dx_get_block(frame->at), hash2, split, count-split));
1489 + /* Fancy dance to stay within two buffers */
1490 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1491 + de = dx_pack_dirents(data1,blocksize);
1492 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1493 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1494 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1495 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1497 + /* Which block gets the new entry? */
1498 + if (hinfo->hash >= hash2)
1503 + dx_insert_block (frame, hash2 + continued, newblock);
1504 + err = ext3_journal_dirty_metadata (handle, bh2);
1506 + goto journal_error;
1507 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1509 + goto journal_error;
1511 + dxtrace(dx_show_index ("frame", frame->entries));
1519 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1520 + * it points to a directory entry which is guaranteed to be large
1521 + * enough for new directory entry. If de is NULL, then
1522 + * add_dirent_to_buf will attempt search the directory block for
1523 + * space. It will return -ENOSPC if no space is available, and -EIO
1524 + * and -EEXIST if directory entry already exists.
1526 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1527 + * all other cases bh is released.
1529 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1530 + struct inode *inode, struct ext3_dir_entry_2 *de,
1531 + struct buffer_head * bh)
1533 + struct inode *dir = dentry->d_parent->d_inode;
1534 + const char *name = dentry->d_name.name;
1535 + int namelen = dentry->d_name.len;
1536 + unsigned long offset = 0;
1537 + unsigned short reclen;
1538 + int nlen, rlen, err;
1541 + reclen = EXT3_DIR_REC_LEN(namelen);
1543 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1544 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1545 + while ((char *) de <= top) {
1546 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1551 + if (ext3_match (namelen, name, de)) {
1555 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1556 + rlen = le16_to_cpu(de->rec_len);
1557 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1559 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1562 + if ((char *) de > top)
1565 + BUFFER_TRACE(bh, "get_write_access");
1566 + err = ext3_journal_get_write_access(handle, bh);
1568 + ext3_std_error(dir->i_sb, err);
1573 + /* By now the buffer is marked for journaling */
1574 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1575 + rlen = le16_to_cpu(de->rec_len);
1577 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1578 + de1->rec_len = cpu_to_le16(rlen - nlen);
1579 + de->rec_len = cpu_to_le16(nlen);
1582 + de->file_type = EXT3_FT_UNKNOWN;
1584 + de->inode = cpu_to_le32(inode->i_ino);
1585 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1588 + de->name_len = namelen;
1589 + memcpy (de->name, name, namelen);
1591 + * XXX shouldn't update any times until successful
1592 + * completion of syscall, but too many callers depend
1595 + * XXX similarly, too many callers depend on
1596 + * ext3_new_inode() setting the times, but error
1597 + * recovery deletes the inode, so the worst that can
1598 + * happen is that the times are slightly out of date
1599 + * and/or different from the directory change time.
1601 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1602 + ext3_update_dx_flag(dir);
1603 + dir->i_version = ++event;
1604 + ext3_mark_inode_dirty(handle, dir);
1605 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1606 + err = ext3_journal_dirty_metadata(handle, bh);
1608 + ext3_std_error(dir->i_sb, err);
1613 +#ifdef CONFIG_EXT3_INDEX
1615 + * This converts a one block unindexed directory to a 3 block indexed
1616 + * directory, and adds the dentry to the indexed directory.
1618 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1619 + struct inode *inode, struct buffer_head *bh)
1621 + struct inode *dir = dentry->d_parent->d_inode;
1622 + const char *name = dentry->d_name.name;
1623 + int namelen = dentry->d_name.len;
1624 + struct buffer_head *bh2;
1625 + struct dx_root *root;
1626 + struct dx_frame frames[2], *frame;
1627 + struct dx_entry *entries;
1628 + struct ext3_dir_entry_2 *de, *de2;
1629 + char *data1, *top;
1632 + unsigned blocksize;
1633 + struct dx_hash_info hinfo;
1636 + blocksize = dir->i_sb->s_blocksize;
1637 + dxtrace(printk("Creating index\n"));
1638 + retval = ext3_journal_get_write_access(handle, bh);
1640 + ext3_std_error(dir->i_sb, retval);
1644 + root = (struct dx_root *) bh->b_data;
1646 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1647 + bh2 = ext3_append (handle, dir, &block, &retval);
1652 + data1 = bh2->b_data;
1654 + /* The 0th block becomes the root, move the dirents out */
1655 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1656 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1657 + len = ((char *) root) + blocksize - (char *) de;
1658 + memcpy (data1, de, len);
1659 + de = (struct ext3_dir_entry_2 *) data1;
1660 + top = data1 + len;
1661 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1663 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1664 + /* Initialize the root; the dot dirents already exist */
1665 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1666 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1667 + memset (&root->info, 0, sizeof(root->info));
1668 + root->info.info_length = sizeof(root->info);
1669 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1670 + entries = root->entries;
1671 + dx_set_block (entries, 1);
1672 + dx_set_count (entries, 1);
1673 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1675 + /* Initialize as for dx_probe */
1676 + hinfo.hash_version = root->info.hash_version;
1677 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1678 + ext3fs_dirhash(name, namelen, &hinfo);
1680 + frame->entries = entries;
1681 + frame->at = entries;
1684 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1685 + dx_release (frames);
1689 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1696 @@ -249,127 +1258,197 @@
1697 * may not sleep between calling this and putting something into
1698 * the entry, as someone else might have used it while you slept.
1702 - * AKPM: the journalling code here looks wrong on the error paths
1704 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1705 struct inode *inode)
1707 struct inode *dir = dentry->d_parent->d_inode;
1708 - const char *name = dentry->d_name.name;
1709 - int namelen = dentry->d_name.len;
1710 unsigned long offset;
1711 - unsigned short rec_len;
1712 struct buffer_head * bh;
1713 - struct ext3_dir_entry_2 * de, * de1;
1714 + struct ext3_dir_entry_2 *de;
1715 struct super_block * sb;
1717 +#ifdef CONFIG_EXT3_INDEX
1718 + int dx_fallback=0;
1720 + unsigned blocksize;
1721 + unsigned nlen, rlen;
1722 + u32 block, blocks;
1727 + blocksize = sb->s_blocksize;
1728 + if (!dentry->d_name.len)
1730 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1731 +#ifdef CONFIG_EXT3_INDEX
1733 + retval = ext3_dx_add_entry(handle, dentry, inode);
1734 + if (!retval || (retval != ERR_BAD_DX_DIR))
1736 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1738 + ext3_mark_inode_dirty(handle, dir);
1741 + blocks = dir->i_size >> sb->s_blocksize_bits;
1742 + for (block = 0, offset = 0; block < blocks; block++) {
1743 + bh = ext3_bread(handle, dir, block, 0, &retval);
1746 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1747 + if (retval != -ENOSPC)
1750 +#ifdef CONFIG_EXT3_INDEX
1751 + if (blocks == 1 && !dx_fallback &&
1752 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1753 + return make_indexed_dir(handle, dentry, inode, bh);
1757 + bh = ext3_append(handle, dir, &block, &retval);
1760 - rec_len = EXT3_DIR_REC_LEN(namelen);
1762 de = (struct ext3_dir_entry_2 *) bh->b_data;
1764 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1767 - bh = ext3_bread (handle, dir,
1768 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1771 - if (dir->i_size <= offset) {
1772 - if (dir->i_size == 0) {
1777 - ext3_debug ("creating next block\n");
1779 - BUFFER_TRACE(bh, "get_write_access");
1780 - ext3_journal_get_write_access(handle, bh);
1781 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1783 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1784 - dir->u.ext3_i.i_disksize =
1785 - dir->i_size = offset + sb->s_blocksize;
1786 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1787 - ext3_mark_inode_dirty(handle, dir);
1790 - ext3_debug ("skipping to next block\n");
1792 + de->rec_len = cpu_to_le16(rlen = blocksize);
1794 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1797 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1800 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1805 - if (ext3_match (namelen, name, de)) {
1808 +#ifdef CONFIG_EXT3_INDEX
1810 + * Returns 0 for success, or a negative error value
1812 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1813 + struct inode *inode)
1815 + struct dx_frame frames[2], *frame;
1816 + struct dx_entry *entries, *at;
1817 + struct dx_hash_info hinfo;
1818 + struct buffer_head * bh;
1819 + struct inode *dir = dentry->d_parent->d_inode;
1820 + struct super_block * sb = dir->i_sb;
1821 + struct ext3_dir_entry_2 *de;
1824 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1827 + entries = frame->entries;
1830 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1833 + BUFFER_TRACE(bh, "get_write_access");
1834 + err = ext3_journal_get_write_access(handle, bh);
1836 + goto journal_error;
1838 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1839 + if (err != -ENOSPC) {
1844 + /* Block full, should compress but for now just split */
1845 + dxtrace(printk("using %u of %u node entries\n",
1846 + dx_get_count(entries), dx_get_limit(entries)));
1847 + /* Need to split index? */
1848 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1850 + unsigned icount = dx_get_count(entries);
1851 + int levels = frame - frames;
1852 + struct dx_entry *entries2;
1853 + struct dx_node *node2;
1854 + struct buffer_head *bh2;
1856 + if (levels && (dx_get_count(frames->entries) ==
1857 + dx_get_limit(frames->entries))) {
1858 + ext3_warning(sb, __FUNCTION__,
1859 + "Directory index full!\n");
1863 - if ((le32_to_cpu(de->inode) == 0 &&
1864 - le16_to_cpu(de->rec_len) >= rec_len) ||
1865 - (le16_to_cpu(de->rec_len) >=
1866 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1867 - BUFFER_TRACE(bh, "get_write_access");
1868 - ext3_journal_get_write_access(handle, bh);
1869 - /* By now the buffer is marked for journaling */
1870 - offset += le16_to_cpu(de->rec_len);
1871 - if (le32_to_cpu(de->inode)) {
1872 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1873 - EXT3_DIR_REC_LEN(de->name_len));
1875 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1876 - EXT3_DIR_REC_LEN(de->name_len));
1877 - de->rec_len = cpu_to_le16(
1878 - EXT3_DIR_REC_LEN(de->name_len));
1881 + bh2 = ext3_append (handle, dir, &newblock, &err);
1884 + node2 = (struct dx_node *)(bh2->b_data);
1885 + entries2 = node2->entries;
1886 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1887 + node2->fake.inode = 0;
1888 + BUFFER_TRACE(frame->bh, "get_write_access");
1889 + err = ext3_journal_get_write_access(handle, frame->bh);
1891 + goto journal_error;
1893 + unsigned icount1 = icount/2, icount2 = icount - icount1;+ unsigned hash2 = dx_get_hash(entries + icount1);
1894 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1896 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1897 + err = ext3_journal_get_write_access(handle,
1900 + goto journal_error;
1902 + memcpy ((char *) entries2, (char *) (entries + icount1),+ icount2 * sizeof(struct dx_entry));
1903 + dx_set_count (entries, icount1);
1904 + dx_set_count (entries2, icount2);
1905 + dx_set_limit (entries2, dx_node_limit(dir));
1907 + /* Which index block gets the new entry? */
1908 + if (at - entries >= icount1) {
1909 + frame->at = at = at - entries - icount1 + entries2;
1910 + frame->entries = entries = entries2;
1911 + swap(frame->bh, bh2);
1913 - de->file_type = EXT3_FT_UNKNOWN;
1915 - de->inode = cpu_to_le32(inode->i_ino);
1916 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1919 - de->name_len = namelen;
1920 - memcpy (de->name, name, namelen);
1922 - * XXX shouldn't update any times until successful
1923 - * completion of syscall, but too many callers depend
1926 - * XXX similarly, too many callers depend on
1927 - * ext3_new_inode() setting the times, but error
1928 - * recovery deletes the inode, so the worst that can
1929 - * happen is that the times are slightly out of date
1930 - * and/or different from the directory change time.
1932 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1933 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1934 - ext3_mark_inode_dirty(handle, dir);
1935 - dir->i_version = ++event;
1936 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1937 - ext3_journal_dirty_metadata(handle, bh);
1940 + dx_insert_block (frames + 0, hash2, newblock);
1941 + dxtrace(dx_show_index ("node", frames[1].entries));
1942 + dxtrace(dx_show_index ("node",
1943 + ((struct dx_node *) bh2->b_data)->entries));
1944 + err = ext3_journal_dirty_metadata(handle, bh2);
1946 + goto journal_error;
1949 + dxtrace(printk("Creating second level index...\n"));
1950 + memcpy((char *) entries2, (char *) entries,
1951 + icount * sizeof(struct dx_entry));
1952 + dx_set_limit(entries2, dx_node_limit(dir));
1955 + dx_set_count(entries, 1);
1956 + dx_set_block(entries + 0, newblock);
1957 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1959 + /* Add new access path frame */
1960 + frame = frames + 1;
1961 + frame->at = at = at - entries + entries2;
1962 + frame->entries = entries = entries2;
1964 + err = ext3_journal_get_write_access(handle,
1967 + goto journal_error;
1969 - offset += le16_to_cpu(de->rec_len);
1970 - de = (struct ext3_dir_entry_2 *)
1971 - ((char *) de + le16_to_cpu(de->rec_len));
1972 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1976 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1979 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1984 + ext3_std_error(dir->i_sb, err);
1988 + dx_release(frames);
1994 * ext3_delete_entry deletes a directory entry by merging it with the
1995 @@ -453,9 +1532,11 @@
1996 struct inode * inode;
1999 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2000 - if (IS_ERR(handle))
2001 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2002 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2003 + if (IS_ERR(handle)) {
2004 return PTR_ERR(handle);
2009 @@ -480,9 +1561,11 @@
2010 struct inode *inode;
2013 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2014 - if (IS_ERR(handle))
2015 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2016 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2017 + if (IS_ERR(handle)) {
2018 return PTR_ERR(handle);
2023 @@ -512,9 +1595,11 @@
2024 if (dir->i_nlink >= EXT3_LINK_MAX)
2027 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2028 - if (IS_ERR(handle))
2029 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2030 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2031 + if (IS_ERR(handle)) {
2032 return PTR_ERR(handle);
2037 @@ -526,7 +1611,8 @@
2039 inode->i_op = &ext3_dir_inode_operations;
2040 inode->i_fop = &ext3_dir_operations;
2041 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2042 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2043 + inode->i_blocks = 0;
2044 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2046 inode->i_nlink--; /* is this nlink == 0? */
2047 @@ -555,21 +1641,19 @@
2049 ext3_mark_inode_dirty(handle, inode);
2050 err = ext3_add_entry (handle, dentry, inode);
2052 - goto out_no_entry;
2054 + inode->i_nlink = 0;
2055 + ext3_mark_inode_dirty(handle, inode);
2060 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2061 + ext3_update_dx_flag(dir);
2062 ext3_mark_inode_dirty(handle, dir);
2063 d_instantiate(dentry, inode);
2065 ext3_journal_stop(handle, dir);
2069 - inode->i_nlink = 0;
2070 - ext3_mark_inode_dirty(handle, inode);
2076 @@ -656,7 +1740,7 @@
2080 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2081 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2084 /* Orphan handling is only valid for files with data blocks
2085 @@ -697,7 +1781,7 @@
2086 * This is safe: on error we're going to ignore the orphan list
2087 * anyway on the next recovery. */
2089 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2090 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2092 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2093 jbd_debug(4, "orphan inode %ld will point to %d\n",
2094 @@ -715,25 +1799,26 @@
2095 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2097 struct list_head *prev;
2098 + struct ext3_inode_info *ei = EXT3_I(inode);
2099 struct ext3_sb_info *sbi;
2101 struct ext3_iloc iloc;
2104 lock_super(inode->i_sb);
2105 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2106 + if (list_empty(&ei->i_orphan)) {
2107 unlock_super(inode->i_sb);
2111 ino_next = NEXT_ORPHAN(inode);
2112 - prev = inode->u.ext3_i.i_orphan.prev;
2113 + prev = ei->i_orphan.prev;
2114 sbi = EXT3_SB(inode->i_sb);
2116 jbd_debug(4, "remove inode %ld from orphan list\n", inode->i_ino);
2118 - list_del(&inode->u.ext3_i.i_orphan);
2119 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2120 + list_del(&ei->i_orphan);
2121 + INIT_LIST_HEAD(&ei->i_orphan);
2123 /* If we're on an error path, we may not have a valid
2124 * transaction handle with which to update the orphan list on
2125 @@ -794,8 +1879,9 @@
2128 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2129 - if (IS_ERR(handle))
2130 + if (IS_ERR(handle)) {
2131 return PTR_ERR(handle);
2135 bh = ext3_find_entry (dentry, &de);
2136 @@ -833,7 +1919,7 @@
2137 ext3_mark_inode_dirty(handle, inode);
2139 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2140 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2141 + ext3_update_dx_flag(dir);
2142 ext3_mark_inode_dirty(handle, dir);
2145 @@ -851,8 +1937,9 @@
2148 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2149 - if (IS_ERR(handle))
2150 + if (IS_ERR(handle)) {
2151 return PTR_ERR(handle);
2156 @@ -879,7 +1966,7 @@
2159 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2160 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2161 + ext3_update_dx_flag(dir);
2162 ext3_mark_inode_dirty(handle, dir);
2164 if (!inode->i_nlink)
2165 @@ -905,9 +1992,11 @@
2166 if (l > dir->i_sb->s_blocksize)
2167 return -ENAMETOOLONG;
2169 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2170 - if (IS_ERR(handle))
2171 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2172 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2173 + if (IS_ERR(handle)) {
2174 return PTR_ERR(handle);
2179 @@ -917,7 +2006,7 @@
2183 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2184 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2185 inode->i_op = &ext3_symlink_inode_operations;
2186 inode->i_mapping->a_ops = &ext3_aops;
2188 @@ -926,25 +2015,23 @@
2189 * i_size in generic_commit_write().
2191 err = block_symlink(inode, symname, l);
2193 - goto out_no_entry;
2195 + ext3_dec_count(handle, inode);
2196 + ext3_mark_inode_dirty(handle, inode);
2201 inode->i_op = &ext3_fast_symlink_inode_operations;
2202 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2203 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2204 inode->i_size = l-1;
2206 - inode->u.ext3_i.i_disksize = inode->i_size;
2207 + EXT3_I(inode)->i_disksize = inode->i_size;
2208 ext3_mark_inode_dirty(handle, inode);
2209 err = ext3_add_nondir(handle, dentry, inode);
2211 ext3_journal_stop(handle, dir);
2215 - ext3_dec_count(handle, inode);
2216 - ext3_mark_inode_dirty(handle, inode);
2221 static int ext3_link (struct dentry * old_dentry,
2222 @@ -957,12 +2044,15 @@
2223 if (S_ISDIR(inode->i_mode))
2226 - if (inode->i_nlink >= EXT3_LINK_MAX)
2227 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2231 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2232 - if (IS_ERR(handle))
2233 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2234 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2235 + if (IS_ERR(handle)) {
2236 return PTR_ERR(handle);
2241 @@ -996,9 +2086,11 @@
2243 old_bh = new_bh = dir_bh = NULL;
2245 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2246 - if (IS_ERR(handle))
2247 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2248 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2249 + if (IS_ERR(handle)) {
2250 return PTR_ERR(handle);
2253 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2255 @@ -1078,7 +2170,7 @@
2256 new_inode->i_ctime = CURRENT_TIME;
2258 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2259 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2260 + ext3_update_dx_flag(old_dir);
2262 BUFFER_TRACE(dir_bh, "get_write_access");
2263 ext3_journal_get_write_access(handle, dir_bh);
2264 @@ -1090,7 +2182,7 @@
2265 new_inode->i_nlink--;
2268 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2269 + ext3_update_dx_flag(new_dir);
2270 ext3_mark_inode_dirty(handle, new_dir);
2273 Index: linux-2.4.19/fs/ext3/super.c
2274 ===================================================================
2275 --- linux-2.4.19.orig/fs/ext3/super.c 2004-04-23 18:25:03.000000000 -0400
2276 +++ linux-2.4.19/fs/ext3/super.c 2004-04-23 18:26:27.000000000 -0400
2278 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2279 ext3_update_dynamic_rev(sb);
2280 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2282 ext3_commit_super (sb, es, 1);
2283 if (test_opt (sb, DEBUG))
2286 EXT3_BLOCKS_PER_GROUP(sb),
2287 EXT3_INODES_PER_GROUP(sb),
2290 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2291 bdevname(sb->s_dev));
2292 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2298 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2301 @@ -1113,6 +1116,9 @@
2302 sbi->s_mount_state = le16_to_cpu(es->s_state);
2303 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2304 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2305 + for (i=0; i < 4; i++)
2306 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2307 + sbi->s_def_hash_version = es->s_def_hash_version;
2309 if (sbi->s_blocks_per_group > blocksize * 8) {
2311 @@ -1821,6 +1827,7 @@
2315 +EXPORT_SYMBOL(ext3_force_commit);
2316 EXPORT_SYMBOL(ext3_bread);
2318 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2319 Index: linux-2.4.19/include/linux/ext3_fs.h
2320 ===================================================================
2321 --- linux-2.4.19.orig/include/linux/ext3_fs.h 2004-04-23 17:53:55.000000000 -0400
2322 +++ linux-2.4.19/include/linux/ext3_fs.h 2004-04-23 18:26:27.000000000 -0400
2324 #define EXT3FS_VERSION "2.4-0.9.18"
2327 + * Always enable hashed directories
2329 +#define CONFIG_EXT3_INDEX
2335 @@ -414,8 +419,11 @@
2336 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2337 __u32 s_journal_dev; /* device number of journal file */
2338 __u32 s_last_orphan; /* start of list of inodes to delete */
2340 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2341 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2342 + __u8 s_def_hash_version; /* Default hash version to use */
2343 + __u8 s_reserved_char_pad;
2344 + __u16 s_reserved_word_pad;
2345 + __u32 s_reserved[192]; /* Padding to the end of the block */
2349 @@ -552,9 +560,46 @@
2350 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2351 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2354 + * Hash Tree Directory indexing
2355 + * (c) Daniel Phillips, 2001
2358 +#ifdef CONFIG_EXT3_INDEX
2359 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2360 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2361 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2362 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2363 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2365 + #define is_dx(dir) 0
2366 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2367 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2370 +/* Legal values for the dx_root hash_version field: */
2372 +#define DX_HASH_LEGACY 0
2373 +#define DX_HASH_HALF_MD4 1
2374 +#define DX_HASH_TEA 2
2376 +/* hash info structure used by the directory hash */
2377 +struct dx_hash_info
2387 + * Control parameters used by ext3_htree_next_block
2389 +#define HASH_NB_ALWAYS 1
2393 * Describe an inode's exact location on disk and in memory
2396 @@ -564,6 +609,27 @@
2397 unsigned long block_group;
2402 + * This structure is stuffed into the struct file's private_data field
2403 + * for directories. It is where we put information so that we can do
2404 + * readdir operations in hash tree order.
2406 +struct dir_private_info {
2408 + rb_node_t *curr_node;
2409 + struct fname *extra_fname;
2412 + __u32 curr_minor_hash;
2417 + * Special error return code only used by dx_probe() and its callers.
2419 +#define ERR_BAD_DX_DIR -75000
2422 * Function prototypes
2424 @@ -591,11 +657,20 @@
2427 extern int ext3_check_dir_entry(const char *, struct inode *,
2428 - struct ext3_dir_entry_2 *, struct buffer_head *,
2430 + struct ext3_dir_entry_2 *,
2431 + struct buffer_head *, unsigned long);
2432 +extern void ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2434 + struct ext3_dir_entry_2 *dirent);
2435 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2438 extern int ext3_sync_file (struct file *, struct dentry *, int);
2441 +extern int ext3fs_dirhash(const char *name, int len, struct
2442 + dx_hash_info *hinfo);
2445 extern struct inode * ext3_new_inode (handle_t *, struct inode *, int);
2446 extern void ext3_free_inode (handle_t *, struct inode *);
2449 extern int ext3_orphan_add(handle_t *, struct inode *);
2450 extern int ext3_orphan_del(handle_t *, struct inode *);
2451 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2452 + __u32 start_minor_hash, __u32 *next_hash);
2455 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2456 Index: linux-2.4.19/include/linux/ext3_fs_sb.h
2457 ===================================================================
2458 --- linux-2.4.19.orig/include/linux/ext3_fs_sb.h 2004-04-23 17:53:54.000000000 -0400
2459 +++ linux-2.4.19/include/linux/ext3_fs_sb.h 2004-04-23 18:26:27.000000000 -0400
2463 u32 s_next_generation;
2464 + u32 s_hash_seed[4];
2465 + int s_def_hash_version;
2468 struct inode * s_journal_inode;
2469 Index: linux-2.4.19/include/linux/ext3_jbd.h
2470 ===================================================================
2471 --- linux-2.4.19.orig/include/linux/ext3_jbd.h 2004-04-23 17:53:54.000000000 -0400
2472 +++ linux-2.4.19/include/linux/ext3_jbd.h 2004-04-23 18:26:27.000000000 -0400
2475 #define EXT3_RESERVE_TRANS_BLOCKS 12
2477 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2480 ext3_mark_iloc_dirty(handle_t *handle,
2481 struct inode *inode,
2482 Index: linux-2.4.19/include/linux/rbtree.h
2483 ===================================================================
2484 --- linux-2.4.19.orig/include/linux/rbtree.h 2001-11-22 14:46:18.000000000 -0500
2485 +++ linux-2.4.19/include/linux/rbtree.h 2004-04-23 18:26:27.000000000 -0400
2488 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2489 extern void rb_erase(rb_node_t *, rb_root_t *);
2490 +extern rb_node_t *rb_get_first(rb_root_t *root);
2491 +extern rb_node_t *rb_get_next(rb_node_t *n);
2493 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2495 Index: linux-2.4.19/lib/rbtree.c
2496 ===================================================================
2497 --- linux-2.4.19.orig/lib/rbtree.c 2002-08-02 20:39:46.000000000 -0400
2498 +++ linux-2.4.19/lib/rbtree.c 2004-04-23 18:26:27.000000000 -0400
2500 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2504 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2507 #include <linux/rbtree.h>
2508 @@ -294,3 +296,43 @@
2509 __rb_erase_color(child, parent, root);
2511 EXPORT_SYMBOL(rb_erase);
2514 + * This function returns the first node (in sort order) of the tree.
2516 +rb_node_t *rb_get_first(rb_root_t *root)
2520 + n = root->rb_node;
2523 + while (n->rb_left)
2527 +EXPORT_SYMBOL(rb_get_first);
2530 + * Given a node, this function will return the next node in the tree.
2532 +rb_node_t *rb_get_next(rb_node_t *n)
2534 + rb_node_t *parent;
2536 + if (n->rb_right) {
2538 + while (n->rb_left)
2542 + while ((parent = n->rb_parent)) {
2543 + if (n == parent->rb_left)
2550 +EXPORT_SYMBOL(rb_get_next);