2 fs/ext3/dir.c | 299 +++++++++
4 fs/ext3/hash.c | 215 ++++++
5 fs/ext3/namei.c | 1387 ++++++++++++++++++++++++++++++++++++++++-----
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, 1886 insertions(+), 160 deletions(-)
14 --- linux-2.4.20/fs/ext3/Makefile~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
15 +++ linux-2.4.20-braam/fs/ext3/Makefile Sat Apr 5 03:57:05 2003
16 @@ -12,7 +12,7 @@ O_TARGET := ext3.o
17 export-objs := super.o inode.o
19 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
20 - ioctl.o namei.o super.o symlink.o
21 + ioctl.o namei.o super.o symlink.o hash.o
24 include $(TOPDIR)/Rules.make
25 --- linux-2.4.20/fs/ext3/dir.c~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
26 +++ linux-2.4.20-braam/fs/ext3/dir.c Sat Apr 5 03:56:31 2003
29 #include <linux/jbd.h>
30 #include <linux/ext3_fs.h>
31 +#include <linux/slab.h>
32 +#include <linux/rbtree.h>
34 static unsigned char ext3_filetype_table[] = {
35 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
38 static int ext3_readdir(struct file *, void *, filldir_t);
39 +static int ext3_dx_readdir(struct file * filp,
40 + void * dirent, filldir_t filldir);
42 struct file_operations ext3_dir_operations = {
43 read: generic_read_dir,
44 @@ -35,6 +39,17 @@ struct file_operations ext3_dir_operatio
45 fsync: ext3_sync_file, /* BKL held */
49 +static unsigned char get_dtype(struct super_block *sb, int filetype)
51 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
52 + (filetype >= EXT3_FT_MAX))
55 + return (ext3_filetype_table[filetype]);
59 int ext3_check_dir_entry (const char * function, struct inode * dir,
60 struct ext3_dir_entry_2 * de,
61 struct buffer_head * bh,
62 @@ -79,6 +94,16 @@ static int ext3_readdir(struct file * fi
67 + err = ext3_dx_readdir(filp, dirent, filldir);
68 + if (err != ERR_BAD_DX_DIR)
71 + * We don't set the inode dirty flag since it's not
72 + * critical that it get flushed back to the disk.
74 + EXT3_I(filp->f_dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
78 offset = filp->f_pos & (sb->s_blocksize - 1);
79 @@ -162,18 +187,12 @@ revalidate:
80 * during the copy operation.
82 unsigned long version = filp->f_version;
83 - unsigned char d_type = DT_UNKNOWN;
85 - if (EXT3_HAS_INCOMPAT_FEATURE(sb,
86 - EXT3_FEATURE_INCOMPAT_FILETYPE)
87 - && de->file_type < EXT3_FT_MAX)
89 - ext3_filetype_table[de->file_type];
90 error = filldir(dirent, de->name,
93 le32_to_cpu(de->inode),
95 + get_dtype(sb, de->file_type));
98 if (version != filp->f_version)
99 @@ -188,3 +207,269 @@ revalidate:
104 +#ifdef CONFIG_EXT3_INDEX
106 + * These functions convert from the major/minor hash to an f_pos
109 + * Currently we only use major hash numer. This is unfortunate, but
110 + * on 32-bit machines, the same VFS interface is used for lseek and
111 + * llseek, so if we use the 64 bit offset, then the 32-bit versions of
112 + * lseek/telldir/seekdir will blow out spectacularly, and from within
113 + * the ext2 low-level routine, we don't know if we're being called by
114 + * a 64-bit version of the system call or the 32-bit version of the
115 + * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
118 +#define hash2pos(major, minor) (major >> 1)
119 +#define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
120 +#define pos2min_hash(pos) (0)
123 + * This structure holds the nodes of the red-black tree used to store
124 + * the directory entry in hash order.
130 + struct fname *next;
138 + * This functoin implements a non-recursive way of freeing all of the
139 + * nodes in the red-black tree.
141 +static void free_rb_tree_fname(rb_root_t *root)
143 + rb_node_t *n = root->rb_node;
145 + struct fname *fname;
148 + /* Do the node's children first */
149 + if ((n)->rb_left) {
158 + * The node has no children; free it, and then zero
159 + * out parent's link to it. Finally go to the
160 + * beginning of the loop and try to free the parent
163 + parent = n->rb_parent;
164 + fname = rb_entry(n, struct fname, rb_hash);
168 + else if (parent->rb_left == n)
169 + parent->rb_left = 0;
170 + else if (parent->rb_right == n)
171 + parent->rb_right = 0;
178 +struct dir_private_info *create_dir_info(loff_t pos)
180 + struct dir_private_info *p;
182 + p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
185 + p->root.rb_node = 0;
187 + p->extra_fname = 0;
189 + p->curr_hash = pos2maj_hash(pos);
190 + p->curr_minor_hash = pos2min_hash(pos);
195 +void ext3_htree_free_dir_info(struct dir_private_info *p)
197 + free_rb_tree_fname(&p->root);
202 + * Given a directory entry, enter it into the fname rb tree.
204 +void ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
206 + struct ext3_dir_entry_2 *dirent)
208 + rb_node_t **p, *parent = NULL;
209 + struct fname * fname, *new_fn;
210 + struct dir_private_info *info;
213 + info = (struct dir_private_info *) dir_file->private_data;
214 + p = &info->root.rb_node;
216 + /* Create and allocate the fname structure */
217 + len = sizeof(struct fname) + dirent->name_len + 1;
218 + new_fn = kmalloc(len, GFP_KERNEL);
219 + memset(new_fn, 0, len);
220 + new_fn->hash = hash;
221 + new_fn->minor_hash = minor_hash;
222 + new_fn->inode = le32_to_cpu(dirent->inode);
223 + new_fn->name_len = dirent->name_len;
224 + new_fn->file_type = dirent->file_type;
225 + memcpy(new_fn->name, dirent->name, dirent->name_len);
226 + new_fn->name[dirent->name_len] = 0;
230 + fname = rb_entry(parent, struct fname, rb_hash);
233 + * If the hash and minor hash match up, then we put
234 + * them on a linked list. This rarely happens...
236 + if ((new_fn->hash == fname->hash) &&
237 + (new_fn->minor_hash == fname->minor_hash)) {
238 + new_fn->next = fname->next;
239 + fname->next = new_fn;
243 + if (new_fn->hash < fname->hash)
244 + p = &(*p)->rb_left;
245 + else if (new_fn->hash > fname->hash)
246 + p = &(*p)->rb_right;
247 + else if (new_fn->minor_hash < fname->minor_hash)
248 + p = &(*p)->rb_left;
249 + else /* if (new_fn->minor_hash > fname->minor_hash) */
250 + p = &(*p)->rb_right;
253 + rb_link_node(&new_fn->rb_hash, parent, p);
254 + rb_insert_color(&new_fn->rb_hash, &info->root);
260 + * This is a helper function for ext3_dx_readdir. It calls filldir
261 + * for all entres on the fname linked list. (Normally there is only
262 + * one entry on the linked list, unless there are 62 bit hash collisions.)
264 +static int call_filldir(struct file * filp, void * dirent,
265 + filldir_t filldir, struct fname *fname)
267 + struct dir_private_info *info = filp->private_data;
269 + struct inode *inode = filp->f_dentry->d_inode;
270 + struct super_block * sb;
276 + printk("call_filldir: called with null fname?!?\n");
279 + curr_pos = hash2pos(fname->hash, fname->minor_hash);
281 + error = filldir(dirent, fname->name,
282 + fname->name_len, curr_pos,
284 + get_dtype(sb, fname->file_type));
286 + filp->f_pos = curr_pos;
287 + info->extra_fname = fname->next;
290 + fname = fname->next;
295 +static int ext3_dx_readdir(struct file * filp,
296 + void * dirent, filldir_t filldir)
298 + struct dir_private_info *info = filp->private_data;
299 + struct inode *inode = filp->f_dentry->d_inode;
300 + struct fname *fname;
304 + info = create_dir_info(filp->f_pos);
307 + filp->private_data = info;
310 + /* Some one has messed with f_pos; reset the world */
311 + if (info->last_pos != filp->f_pos) {
312 + free_rb_tree_fname(&info->root);
313 + info->curr_node = 0;
314 + info->extra_fname = 0;
315 + info->curr_hash = pos2maj_hash(filp->f_pos);
316 + info->curr_minor_hash = pos2min_hash(filp->f_pos);
320 + * If there are any leftover names on the hash collision
321 + * chain, return them first.
323 + if (info->extra_fname &&
324 + call_filldir(filp, dirent, filldir, info->extra_fname))
327 + if (!info->curr_node)
328 + info->curr_node = rb_get_first(&info->root);
332 + * Fill the rbtree if we have no more entries,
333 + * or the inode has changed since we last read in the
336 + if ((!info->curr_node) ||
337 + (filp->f_version != inode->i_version)) {
338 + info->curr_node = 0;
339 + free_rb_tree_fname(&info->root);
340 + filp->f_version = inode->i_version;
341 + ret = ext3_htree_fill_tree(filp, info->curr_hash,
342 + info->curr_minor_hash,
348 + info->curr_node = rb_get_first(&info->root);
351 + fname = rb_entry(info->curr_node, struct fname, rb_hash);
352 + info->curr_hash = fname->hash;
353 + info->curr_minor_hash = fname->minor_hash;
354 + if (call_filldir(filp, dirent, filldir, fname))
357 + info->curr_node = rb_get_next(info->curr_node);
358 + if (!info->curr_node) {
359 + info->curr_hash = info->next_hash;
360 + info->curr_minor_hash = 0;
364 + info->last_pos = filp->f_pos;
365 + UPDATE_ATIME(inode);
369 --- linux-2.4.20/fs/ext3/file.c~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
370 +++ linux-2.4.20-braam/fs/ext3/file.c Sat Apr 5 03:56:31 2003
371 @@ -35,6 +35,9 @@ static int ext3_release_file (struct ino
373 if (filp->f_mode & FMODE_WRITE)
374 ext3_discard_prealloc (inode);
375 + if (is_dx(inode) && filp->private_data)
376 + ext3_htree_free_dir_info(filp->private_data);
381 --- /dev/null Fri Aug 30 17:31:37 2002
382 +++ linux-2.4.20-braam/fs/ext3/hash.c Sat Apr 5 03:56:31 2003
385 + * linux/fs/ext3/hash.c
387 + * Copyright (C) 2002 by Theodore Ts'o
389 + * This file is released under the GPL v2.
391 + * This file may be redistributed under the terms of the GNU Public
395 +#include <linux/fs.h>
396 +#include <linux/jbd.h>
397 +#include <linux/sched.h>
398 +#include <linux/ext3_fs.h>
400 +#define DELTA 0x9E3779B9
402 +static void TEA_transform(__u32 buf[4], __u32 const in[])
405 + __u32 b0 = buf[0], b1 = buf[1];
406 + __u32 a = in[0], b = in[1], c = in[2], d = in[3];
411 + b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
412 + b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
419 +/* F, G and H are basic MD4 functions: selection, majority, parity */
420 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
421 +#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
422 +#define H(x, y, z) ((x) ^ (y) ^ (z))
425 + * The generic round function. The application is so specific that
426 + * we don't bother protecting all the arguments with parens, as is generally
427 + * good macro practice, in favor of extra legibility.
428 + * Rotation is separate from addition to prevent recomputation
430 +#define ROUND(f, a, b, c, d, x, s) \
431 + (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
433 +#define K2 013240474631UL
434 +#define K3 015666365641UL
437 + * Basic cut-down MD4 transform. Returns only 32 bits of result.
439 +static void halfMD4Transform (__u32 buf[4], __u32 const in[])
441 + __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
444 + ROUND(F, a, b, c, d, in[0] + K1, 3);
445 + ROUND(F, d, a, b, c, in[1] + K1, 7);
446 + ROUND(F, c, d, a, b, in[2] + K1, 11);
447 + ROUND(F, b, c, d, a, in[3] + K1, 19);
448 + ROUND(F, a, b, c, d, in[4] + K1, 3);
449 + ROUND(F, d, a, b, c, in[5] + K1, 7);
450 + ROUND(F, c, d, a, b, in[6] + K1, 11);
451 + ROUND(F, b, c, d, a, in[7] + K1, 19);
454 + ROUND(G, a, b, c, d, in[1] + K2, 3);
455 + ROUND(G, d, a, b, c, in[3] + K2, 5);
456 + ROUND(G, c, d, a, b, in[5] + K2, 9);
457 + ROUND(G, b, c, d, a, in[7] + K2, 13);
458 + ROUND(G, a, b, c, d, in[0] + K2, 3);
459 + ROUND(G, d, a, b, c, in[2] + K2, 5);
460 + ROUND(G, c, d, a, b, in[4] + K2, 9);
461 + ROUND(G, b, c, d, a, in[6] + K2, 13);
464 + ROUND(H, a, b, c, d, in[3] + K3, 3);
465 + ROUND(H, d, a, b, c, in[7] + K3, 9);
466 + ROUND(H, c, d, a, b, in[2] + K3, 11);
467 + ROUND(H, b, c, d, a, in[6] + K3, 15);
468 + ROUND(H, a, b, c, d, in[1] + K3, 3);
469 + ROUND(H, d, a, b, c, in[5] + K3, 9);
470 + ROUND(H, c, d, a, b, in[0] + K3, 11);
471 + ROUND(H, b, c, d, a, in[4] + K3, 15);
487 +/* The old legacy hash */
488 +static __u32 dx_hack_hash (const char *name, int len)
490 + __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
492 + __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
494 + if (hash & 0x80000000) hash -= 0x7fffffff;
498 + return (hash0 << 1);
501 +static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
506 + pad = (__u32)len | ((__u32)len << 8);
512 + for (i=0; i < len; i++) {
515 + val = msg[i] + (val << 8);
516 + if ((i % 4) == 3) {
529 + * Returns the hash of a filename. If len is 0 and name is NULL, then
530 + * this function can be used to test whether or not a hash version is
533 + * The seed is an 4 longword (32 bits) "secret" which can be used to
534 + * uniquify a hash. If the seed is all zero's, then some default seed
537 + * A particular hash version specifies whether or not the seed is
538 + * represented, and whether or not the returned hash is 32 bits or 64
539 + * bits. 32 bit hashes will return 0 for the minor hash.
541 +int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
544 + __u32 minor_hash = 0;
547 + __u32 in[8], buf[4];
549 + /* Initialize the default seed for the hash checksum functions */
550 + buf[0] = 0x67452301;
551 + buf[1] = 0xefcdab89;
552 + buf[2] = 0x98badcfe;
553 + buf[3] = 0x10325476;
555 + /* Check to see if the seed is all zero's */
557 + for (i=0; i < 4; i++) {
558 + if (hinfo->seed[i])
562 + memcpy(buf, hinfo->seed, sizeof(buf));
565 + switch (hinfo->hash_version) {
566 + case DX_HASH_LEGACY:
567 + hash = dx_hack_hash(name, len);
569 + case DX_HASH_HALF_MD4:
572 + str2hashbuf(p, len, in, 8);
573 + halfMD4Transform(buf, in);
577 + minor_hash = buf[2];
583 + str2hashbuf(p, len, in, 4);
584 + TEA_transform(buf, in);
589 + minor_hash = buf[1];
595 + hinfo->hash = hash & ~1;
596 + hinfo->minor_hash = minor_hash;
599 --- linux-2.4.20/fs/ext3/namei.c~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
600 +++ linux-2.4.20-braam/fs/ext3/namei.c Sat Apr 5 03:56:31 2003
602 * David S. Miller (davem@caip.rutgers.edu), 1995
603 * Directory entry file type support and forward compatibility hooks
604 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
605 + * Hash Tree Directory indexing (c)
606 + * Daniel Phillips, 2001
607 + * Hash Tree Directory indexing porting
608 + * Christopher Li, 2002
609 + * Hash Tree Directory indexing cleanup
610 + * Theodore Ts'o, 2002
613 #include <linux/fs.h>
615 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
616 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
618 +static struct buffer_head *ext3_append(handle_t *handle,
619 + struct inode *inode,
620 + u32 *block, int *err)
622 + struct buffer_head *bh;
624 + *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
626 + if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
627 + inode->i_size += inode->i_sb->s_blocksize;
628 + EXT3_I(inode)->i_disksize = inode->i_size;
629 + ext3_journal_get_write_access(handle,bh);
635 +#define assert(test) J_ASSERT(test)
639 +#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
642 +typedef struct { u32 v; } le_u32;
643 +typedef struct { u16 v; } le_u16;
646 +#define dxtrace(command) command
648 +#define dxtrace(command)
659 +struct dx_countlimit
672 + * dx_root_info is laid out so that if it should somehow get overlaid by a
673 + * dirent the two low bits of the hash version will be zero. Therefore, the
674 + * hash version mod 4 should never be 0. Sincerely, the paranoia department.
679 + struct fake_dirent dot;
681 + struct fake_dirent dotdot;
682 + char dotdot_name[4];
683 + struct dx_root_info
685 + le_u32 reserved_zero;
687 + u8 info_length; /* 8 */
688 + u8 indirect_levels;
692 + struct dx_entry entries[0];
697 + struct fake_dirent fake;
698 + struct dx_entry entries[0];
704 + struct buffer_head *bh;
705 + struct dx_entry *entries;
706 + struct dx_entry *at;
715 +#ifdef CONFIG_EXT3_INDEX
716 +static inline unsigned dx_get_block (struct dx_entry *entry);
717 +static void dx_set_block (struct dx_entry *entry, unsigned value);
718 +static inline unsigned dx_get_hash (struct dx_entry *entry);
719 +static void dx_set_hash (struct dx_entry *entry, unsigned value);
720 +static unsigned dx_get_count (struct dx_entry *entries);
721 +static unsigned dx_get_limit (struct dx_entry *entries);
722 +static void dx_set_count (struct dx_entry *entries, unsigned value);
723 +static void dx_set_limit (struct dx_entry *entries, unsigned value);
724 +static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
725 +static unsigned dx_node_limit (struct inode *dir);
726 +static struct dx_frame *dx_probe(struct dentry *dentry,
728 + struct dx_hash_info *hinfo,
729 + struct dx_frame *frame,
731 +static void dx_release (struct dx_frame *frames);
732 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
733 + struct dx_hash_info *hinfo, struct dx_map_entry map[]);
734 +static void dx_sort_map(struct dx_map_entry *map, unsigned count);
735 +static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
736 + struct dx_map_entry *offsets, int count);
737 +static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
738 +static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
739 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
740 + struct dx_frame *frame,
741 + struct dx_frame *frames, int *err,
742 + __u32 *start_hash);
743 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
744 + struct ext3_dir_entry_2 **res_dir, int *err);
745 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
746 + struct inode *inode);
749 + * Future: use high four bits of block for coalesce-on-delete flags
750 + * Mask them off for now.
753 +static inline unsigned dx_get_block (struct dx_entry *entry)
755 + return le32_to_cpu(entry->block.v) & 0x00ffffff;
758 +static inline void dx_set_block (struct dx_entry *entry, unsigned value)
760 + entry->block.v = cpu_to_le32(value);
763 +static inline unsigned dx_get_hash (struct dx_entry *entry)
765 + return le32_to_cpu(entry->hash.v);
768 +static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
770 + entry->hash.v = cpu_to_le32(value);
773 +static inline unsigned dx_get_count (struct dx_entry *entries)
775 + return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
778 +static inline unsigned dx_get_limit (struct dx_entry *entries)
780 + return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
783 +static inline void dx_set_count (struct dx_entry *entries, unsigned value)
785 + ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
788 +static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
790 + ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
793 +static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
795 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
796 + EXT3_DIR_REC_LEN(2) - infosize;
797 + return 0? 20: entry_space / sizeof(struct dx_entry);
800 +static inline unsigned dx_node_limit (struct inode *dir)
802 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
803 + return 0? 22: entry_space / sizeof(struct dx_entry);
817 +static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
818 + int size, int show_names)
820 + unsigned names = 0, space = 0;
821 + char *base = (char *) de;
822 + struct dx_hash_info h = *hinfo;
825 + while ((char *) de < base + size)
831 + int len = de->name_len;
832 + char *name = de->name;
833 + while (len--) printk("%c", *name++);
834 + ext3fs_dirhash(de->name, de->name_len, &h);
835 + printk(":%x.%u ", h.hash,
836 + ((char *) de - base));
838 + space += EXT3_DIR_REC_LEN(de->name_len);
841 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
843 + printk("(%i)\n", names);
844 + return (struct stats) { names, space, 1 };
847 +struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
848 + struct dx_entry *entries, int levels)
850 + unsigned blocksize = dir->i_sb->s_blocksize;
851 + unsigned count = dx_get_count (entries), names = 0, space = 0, i;
852 + unsigned bcount = 0;
853 + struct buffer_head *bh;
855 + printk("%i indexed blocks...\n", count);
856 + for (i = 0; i < count; i++, entries++)
858 + u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
859 + u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
860 + struct stats stats;
861 + printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
862 + if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
864 + dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
865 + dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
866 + names += stats.names;
867 + space += stats.space;
868 + bcount += stats.bcount;
872 + printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
873 + names, space/bcount,(space/bcount)*100/blocksize);
874 + return (struct stats) { names, space, bcount};
876 +#endif /* DX_DEBUG */
879 + * Probe for a directory leaf block to search.
881 + * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
882 + * error in the directory index, and the caller should fall back to
883 + * searching the directory normally. The callers of dx_probe **MUST**
884 + * check for this error code, and make sure it never gets reflected
885 + * back to userspace.
887 +static struct dx_frame *
888 +dx_probe(struct dentry *dentry, struct inode *dir,
889 + struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
891 + unsigned count, indirect;
892 + struct dx_entry *at, *entries, *p, *q, *m;
893 + struct dx_root *root;
894 + struct buffer_head *bh;
895 + struct dx_frame *frame = frame_in;
900 + dir = dentry->d_parent->d_inode;
901 + if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
903 + root = (struct dx_root *) bh->b_data;
904 + if (root->info.hash_version != DX_HASH_TEA &&
905 + root->info.hash_version != DX_HASH_HALF_MD4 &&
906 + root->info.hash_version != DX_HASH_LEGACY) {
907 + ext3_warning(dir->i_sb, __FUNCTION__,
908 + "Unrecognised inode hash code %d",
909 + root->info.hash_version);
911 + *err = ERR_BAD_DX_DIR;
914 + hinfo->hash_version = root->info.hash_version;
915 + hinfo->seed = dir->i_sb->u.ext3_sb.s_hash_seed;
917 + ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
918 + hash = hinfo->hash;
920 + if (root->info.unused_flags & 1) {
921 + ext3_warning(dir->i_sb, __FUNCTION__,
922 + "Unimplemented inode hash flags: %#06x",
923 + root->info.unused_flags);
925 + *err = ERR_BAD_DX_DIR;
929 + if ((indirect = root->info.indirect_levels) > 1) {
930 + ext3_warning(dir->i_sb, __FUNCTION__,
931 + "Unimplemented inode hash depth: %#06x",
932 + root->info.indirect_levels);
934 + *err = ERR_BAD_DX_DIR;
938 + entries = (struct dx_entry *) (((char *)&root->info) +
939 + root->info.info_length);
940 + assert(dx_get_limit(entries) == dx_root_limit(dir,
941 + root->info.info_length));
942 + dxtrace (printk("Look up %x", hash));
945 + count = dx_get_count(entries);
946 + assert (count && count <= dx_get_limit(entries));
948 + q = entries + count - 1;
952 + dxtrace(printk("."));
953 + if (dx_get_hash(m) > hash)
959 + if (0) // linear search cross check
961 + unsigned n = count - 1;
965 + dxtrace(printk(","));
966 + if (dx_get_hash(++at) > hash)
972 + assert (at == p - 1);
976 + dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
978 + frame->entries = entries;
980 + if (!indirect--) return frame;
981 + if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
983 + at = entries = ((struct dx_node *) bh->b_data)->entries;
984 + assert (dx_get_limit(entries) == dx_node_limit (dir));
988 + while (frame >= frame_in) {
996 +static void dx_release (struct dx_frame *frames)
998 + if (frames[0].bh == NULL)
1001 + if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
1002 + brelse(frames[1].bh);
1003 + brelse(frames[0].bh);
1007 + * This function increments the frame pointer to search the next leaf
1008 + * block, and reads in the necessary intervening nodes if the search
1009 + * should be necessary. Whether or not the search is necessary is
1010 + * controlled by the hash parameter. If the hash value is even, then
1011 + * the search is only continued if the next block starts with that
1012 + * hash value. This is used if we are searching for a specific file.
1014 + * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1016 + * This function returns 1 if the caller should continue to search,
1017 + * or 0 if it should not. If there is an error reading one of the
1018 + * index blocks, it will return -1.
1020 + * If start_hash is non-null, it will be filled in with the starting
1021 + * hash of the next page.
1023 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
1024 + struct dx_frame *frame,
1025 + struct dx_frame *frames, int *err,
1026 + __u32 *start_hash)
1028 + struct dx_frame *p;
1029 + struct buffer_head *bh;
1030 + int num_frames = 0;
1036 + * Find the next leaf page by incrementing the frame pointer.
1037 + * If we run out of entries in the interior node, loop around and
1038 + * increment pointer in the parent node. When we break out of
1039 + * this loop, num_frames indicates the number of interior
1040 + * nodes need to be read.
1043 + if (++(p->at) < p->entries + dx_get_count(p->entries))
1052 + * If the hash is 1, then continue only if the next page has a
1053 + * continuation hash of any value. This is used for readdir
1054 + * handling. Otherwise, check to see if the hash matches the
1055 + * desired contiuation hash. If it doesn't, return since
1056 + * there's no point to read in the successive index pages.
1058 + bhash = dx_get_hash(p->at);
1060 + *start_hash = bhash;
1061 + if ((hash & 1) == 0) {
1062 + if ((bhash & ~1) != hash)
1066 + * If the hash is HASH_NB_ALWAYS, we always go to the next
1067 + * block so no check is necessary
1069 + while (num_frames--) {
1070 + if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
1072 + return -1; /* Failure */
1076 + p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1083 + * p is at least 6 bytes before the end of page
1085 +static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
1087 + return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
1091 + * This function fills a red-black tree with information from a
1092 + * directory. We start scanning the directory in hash order, starting
1093 + * at start_hash and start_minor_hash.
1095 + * This function returns the number of entries inserted into the tree,
1096 + * or a negative error code.
1098 +int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1099 + __u32 start_minor_hash, __u32 *next_hash)
1101 + struct dx_hash_info hinfo;
1102 + struct buffer_head *bh;
1103 + struct ext3_dir_entry_2 *de, *top;
1104 + static struct dx_frame frames[2], *frame;
1105 + struct inode *dir;
1111 + dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
1112 + start_minor_hash));
1113 + dir = dir_file->f_dentry->d_inode;
1114 + hinfo.hash = start_hash;
1115 + hinfo.minor_hash = 0;
1116 + frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
1121 + block = dx_get_block(frame->at);
1122 + dxtrace(printk("Reading block %d\n", block));
1123 + if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
1126 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1127 + top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
1128 + EXT3_DIR_REC_LEN(0));
1129 + for (; de < top; de = ext3_next_entry(de)) {
1130 + ext3fs_dirhash(de->name, de->name_len, &hinfo);
1131 + if ((hinfo.hash < start_hash) ||
1132 + ((hinfo.hash == start_hash) &&
1133 + (hinfo.minor_hash < start_minor_hash)))
1135 + ext3_htree_store_dirent(dir_file, hinfo.hash,
1136 + hinfo.minor_hash, de);
1141 + ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
1142 + frame, frames, &err, &hashval);
1144 + *next_hash = hashval;
1148 + * Stop if: (a) there are no more entries, or
1149 + * (b) we have inserted at least one entry and the
1150 + * next hash value is not a continuation
1153 + (count && ((hashval & 1) == 0)))
1156 + dx_release(frames);
1157 + dxtrace(printk("Fill tree: returned %d entries\n", count));
1160 + dx_release(frames);
1166 + * Directory block splitting, compacting
1169 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
1170 + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
1173 + char *base = (char *) de;
1174 + struct dx_hash_info h = *hinfo;
1176 + while ((char *) de < base + size)
1178 + if (de->name_len && de->inode) {
1179 + ext3fs_dirhash(de->name, de->name_len, &h);
1181 + map_tail->hash = h.hash;
1182 + map_tail->offs = (u32) ((char *) de - base);
1185 + /* XXX: do we need to check rec_len == 0 case? -Chris */
1186 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
1191 +static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1193 + struct dx_map_entry *p, *q, *top = map + count - 1;
1195 + /* Combsort until bubble sort doesn't suck */
1198 + count = count*10/13;
1199 + if (count - 9 < 2) /* 9, 10 -> 11 */
1201 + for (p = top, q = p - count; q >= map; p--, q--)
1202 + if (p->hash < q->hash)
1205 + /* Garden variety bubble sort */
1211 + if (q[1].hash >= q[0].hash)
1219 +static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
1221 + struct dx_entry *entries = frame->entries;
1222 + struct dx_entry *old = frame->at, *new = old + 1;
1223 + int count = dx_get_count(entries);
1225 + assert(count < dx_get_limit(entries));
1226 + assert(old < entries + count);
1227 + memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1228 + dx_set_hash(new, hash);
1229 + dx_set_block(new, block);
1230 + dx_set_count(entries, count + 1);
1235 +static void ext3_update_dx_flag(struct inode *inode)
1237 + if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
1238 + EXT3_FEATURE_COMPAT_DIR_INDEX))
1239 + EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
1243 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
1245 @@ -94,6 +724,7 @@ static int inline search_dirblock(struct
1253 @@ -105,6 +736,8 @@ static int inline search_dirblock(struct
1254 * The returned buffer_head has ->b_count elevated. The caller is expected
1255 * to brelse() it when appropriate.
1259 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
1260 struct ext3_dir_entry_2 ** res_dir)
1262 @@ -119,12 +752,32 @@ static struct buffer_head * ext3_find_en
1264 int nblocks, i, err;
1265 struct inode *dir = dentry->d_parent->d_inode;
1268 + unsigned blocksize;
1273 + blocksize = sb->s_blocksize;
1274 + namelen = dentry->d_name.len;
1275 + name = dentry->d_name.name;
1276 + if (namelen > EXT3_NAME_LEN)
1278 +#ifdef CONFIG_EXT3_INDEX
1280 + bh = ext3_dx_find_entry(dentry, res_dir, &err);
1282 + * On success, or if the error was file not found,
1283 + * return. Otherwise, fall back to doing a search the
1284 + * old fashioned way.
1286 + if (bh || (err != ERR_BAD_DX_DIR))
1288 + dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
1291 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
1292 - start = dir->u.ext3_i.i_dir_start_lookup;
1293 + start = EXT3_I(dir)->i_dir_start_lookup;
1294 if (start >= nblocks)
1297 @@ -165,7 +818,7 @@ restart:
1298 i = search_dirblock(bh, dir, dentry,
1299 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
1301 - dir->u.ext3_i.i_dir_start_lookup = block;
1302 + EXT3_I(dir)->i_dir_start_lookup = block;
1304 goto cleanup_and_exit;
1306 @@ -196,6 +849,74 @@ cleanup_and_exit:
1310 +#ifdef CONFIG_EXT3_INDEX
1311 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
1312 + struct ext3_dir_entry_2 **res_dir, int *err)
1314 + struct super_block * sb;
1315 + struct dx_hash_info hinfo;
1317 + struct dx_frame frames[2], *frame;
1318 + struct ext3_dir_entry_2 *de, *top;
1319 + struct buffer_head *bh;
1320 + unsigned long block;
1322 + int namelen = dentry->d_name.len;
1323 + const u8 *name = dentry->d_name.name;
1324 + struct inode *dir = dentry->d_parent->d_inode;
1327 + /* NFS may look up ".." - look at dx_root directory block */
1328 + if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
1329 + if (!(frame = dx_probe(dentry, 0, &hinfo, frames, err)))
1333 + frame->bh = NULL; /* for dx_release() */
1334 + frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
1335 + dx_set_block(frame->at, 0); /* dx_root block is 0 */
1337 + hash = hinfo.hash;
1339 + block = dx_get_block(frame->at);
1340 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1342 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1343 + top = (struct ext3_dir_entry_2 *)((char *)de + sb->s_blocksize -
1344 + EXT3_DIR_REC_LEN(0));
1345 + for (; de < top; de = ext3_next_entry(de))
1346 + if (ext3_match (namelen, name, de)) {
1347 + if (!ext3_check_dir_entry("ext3_find_entry",
1349 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1350 + +((char *)de - bh->b_data))) {
1355 + dx_release (frames);
1359 + /* Check to see if we should continue to search */
1360 + retval = ext3_htree_next_block(dir, hash, frame,
1362 + if (retval == -1) {
1363 + ext3_warning(sb, __FUNCTION__,
1364 + "error reading index page in directory #%lu",
1368 + } while (retval == 1);
1372 + dxtrace(printk("%s not found\n", name));
1373 + dx_release (frames);
1378 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1380 struct inode * inode;
1381 @@ -212,8 +925,9 @@ static struct dentry *ext3_lookup(struct
1383 inode = iget(dir->i_sb, ino);
1387 return ERR_PTR(-EACCES);
1390 d_add(dentry, inode);
1392 @@ -237,6 +951,301 @@ static inline void ext3_set_de_type(stru
1393 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1396 +#ifdef CONFIG_EXT3_INDEX
1397 +static struct ext3_dir_entry_2 *
1398 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1400 + unsigned rec_len = 0;
1403 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1404 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1405 + memcpy (to, de, rec_len);
1406 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1411 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1414 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1416 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1417 + unsigned rec_len = 0;
1420 + while ((char*)de < base + size) {
1421 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1422 + le16_to_cpu(de->rec_len));
1423 + if (de->inode && de->name_len) {
1424 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1426 + memmove(to, de, rec_len);
1427 + to->rec_len = cpu_to_le16(rec_len);
1429 + to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1436 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1437 + struct buffer_head **bh,struct dx_frame *frame,
1438 + struct dx_hash_info *hinfo, int *error)
1440 + unsigned blocksize = dir->i_sb->s_blocksize;
1441 + unsigned count, continued;
1442 + struct buffer_head *bh2;
1445 + struct dx_map_entry *map;
1446 + char *data1 = (*bh)->b_data, *data2;
1448 + struct ext3_dir_entry_2 *de = NULL, *de2;
1451 + bh2 = ext3_append (handle, dir, &newblock, error);
1458 + BUFFER_TRACE(*bh, "get_write_access");
1459 + err = ext3_journal_get_write_access(handle, *bh);
1465 + ext3_std_error(dir->i_sb, err);
1468 + BUFFER_TRACE(frame->bh, "get_write_access");
1469 + err = ext3_journal_get_write_access(handle, frame->bh);
1471 + goto journal_error;
1473 + data2 = bh2->b_data;
1475 + /* create map in the end of data2 block */
1476 + map = (struct dx_map_entry *) (data2 + blocksize);
1477 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1478 + blocksize, hinfo, map);
1480 + split = count/2; // need to adjust to actual middle
1481 + dx_sort_map (map, count);
1482 + hash2 = map[split].hash;
1483 + continued = hash2 == map[split - 1].hash;
1484 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1485 + dx_get_block(frame->at), hash2, split, count-split));
1487 + /* Fancy dance to stay within two buffers */
1488 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1489 + de = dx_pack_dirents(data1,blocksize);
1490 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1491 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1492 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1493 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1495 + /* Which block gets the new entry? */
1496 + if (hinfo->hash >= hash2)
1501 + dx_insert_block (frame, hash2 + continued, newblock);
1502 + err = ext3_journal_dirty_metadata (handle, bh2);
1504 + goto journal_error;
1505 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1507 + goto journal_error;
1509 + dxtrace(dx_show_index ("frame", frame->entries));
1517 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1518 + * it points to a directory entry which is guaranteed to be large
1519 + * enough for new directory entry. If de is NULL, then
1520 + * add_dirent_to_buf will attempt search the directory block for
1521 + * space. It will return -ENOSPC if no space is available, and -EIO
1522 + * and -EEXIST if directory entry already exists.
1524 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1525 + * all other cases bh is released.
1527 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1528 + struct inode *inode, struct ext3_dir_entry_2 *de,
1529 + struct buffer_head * bh)
1531 + struct inode *dir = dentry->d_parent->d_inode;
1532 + const char *name = dentry->d_name.name;
1533 + int namelen = dentry->d_name.len;
1534 + unsigned long offset = 0;
1535 + unsigned short reclen;
1536 + int nlen, rlen, err;
1539 + reclen = EXT3_DIR_REC_LEN(namelen);
1541 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1542 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1543 + while ((char *) de <= top) {
1544 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1549 + if (ext3_match (namelen, name, de)) {
1553 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1554 + rlen = le16_to_cpu(de->rec_len);
1555 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1557 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1560 + if ((char *) de > top)
1563 + BUFFER_TRACE(bh, "get_write_access");
1564 + err = ext3_journal_get_write_access(handle, bh);
1566 + ext3_std_error(dir->i_sb, err);
1571 + /* By now the buffer is marked for journaling */
1572 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1573 + rlen = le16_to_cpu(de->rec_len);
1575 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1576 + de1->rec_len = cpu_to_le16(rlen - nlen);
1577 + de->rec_len = cpu_to_le16(nlen);
1580 + de->file_type = EXT3_FT_UNKNOWN;
1582 + de->inode = cpu_to_le32(inode->i_ino);
1583 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1586 + de->name_len = namelen;
1587 + memcpy (de->name, name, namelen);
1589 + * XXX shouldn't update any times until successful
1590 + * completion of syscall, but too many callers depend
1593 + * XXX similarly, too many callers depend on
1594 + * ext3_new_inode() setting the times, but error
1595 + * recovery deletes the inode, so the worst that can
1596 + * happen is that the times are slightly out of date
1597 + * and/or different from the directory change time.
1599 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1600 + ext3_update_dx_flag(dir);
1601 + dir->i_version = ++event;
1602 + ext3_mark_inode_dirty(handle, dir);
1603 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1604 + err = ext3_journal_dirty_metadata(handle, bh);
1606 + ext3_std_error(dir->i_sb, err);
1611 +#ifdef CONFIG_EXT3_INDEX
1613 + * This converts a one block unindexed directory to a 3 block indexed
1614 + * directory, and adds the dentry to the indexed directory.
1616 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1617 + struct inode *inode, struct buffer_head *bh)
1619 + struct inode *dir = dentry->d_parent->d_inode;
1620 + const char *name = dentry->d_name.name;
1621 + int namelen = dentry->d_name.len;
1622 + struct buffer_head *bh2;
1623 + struct dx_root *root;
1624 + struct dx_frame frames[2], *frame;
1625 + struct dx_entry *entries;
1626 + struct ext3_dir_entry_2 *de, *de2;
1627 + char *data1, *top;
1630 + unsigned blocksize;
1631 + struct dx_hash_info hinfo;
1634 + blocksize = dir->i_sb->s_blocksize;
1635 + dxtrace(printk("Creating index\n"));
1636 + retval = ext3_journal_get_write_access(handle, bh);
1638 + ext3_std_error(dir->i_sb, retval);
1642 + root = (struct dx_root *) bh->b_data;
1644 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1645 + bh2 = ext3_append (handle, dir, &block, &retval);
1650 + data1 = bh2->b_data;
1652 + /* The 0th block becomes the root, move the dirents out */
1653 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1654 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1655 + len = ((char *) root) + blocksize - (char *) de;
1656 + memcpy (data1, de, len);
1657 + de = (struct ext3_dir_entry_2 *) data1;
1658 + top = data1 + len;
1659 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1661 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1662 + /* Initialize the root; the dot dirents already exist */
1663 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1664 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1665 + memset (&root->info, 0, sizeof(root->info));
1666 + root->info.info_length = sizeof(root->info);
1667 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1668 + entries = root->entries;
1669 + dx_set_block (entries, 1);
1670 + dx_set_count (entries, 1);
1671 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1673 + /* Initialize as for dx_probe */
1674 + hinfo.hash_version = root->info.hash_version;
1675 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1676 + ext3fs_dirhash(name, namelen, &hinfo);
1678 + frame->entries = entries;
1679 + frame->at = entries;
1682 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1683 + dx_release (frames);
1687 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1694 @@ -247,127 +1255,198 @@ static inline void ext3_set_de_type(stru
1695 * may not sleep between calling this and putting something into
1696 * the entry, as someone else might have used it while you slept.
1700 - * AKPM: the journalling code here looks wrong on the error paths
1702 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1703 struct inode *inode)
1705 struct inode *dir = dentry->d_parent->d_inode;
1706 - const char *name = dentry->d_name.name;
1707 - int namelen = dentry->d_name.len;
1708 unsigned long offset;
1709 - unsigned short rec_len;
1710 struct buffer_head * bh;
1711 - struct ext3_dir_entry_2 * de, * de1;
1712 + struct ext3_dir_entry_2 *de;
1713 struct super_block * sb;
1715 +#ifdef CONFIG_EXT3_INDEX
1716 + int dx_fallback=0;
1718 + unsigned blocksize;
1719 + unsigned nlen, rlen;
1720 + u32 block, blocks;
1725 + blocksize = sb->s_blocksize;
1726 + if (!dentry->d_name.len)
1728 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1729 +#ifdef CONFIG_EXT3_INDEX
1731 + retval = ext3_dx_add_entry(handle, dentry, inode);
1732 + if (!retval || (retval != ERR_BAD_DX_DIR))
1734 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1736 + ext3_mark_inode_dirty(handle, dir);
1739 + blocks = dir->i_size >> sb->s_blocksize_bits;
1740 + for (block = 0, offset = 0; block < blocks; block++) {
1741 + bh = ext3_bread(handle, dir, block, 0, &retval);
1744 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1745 + if (retval != -ENOSPC)
1748 +#ifdef CONFIG_EXT3_INDEX
1749 + if (blocks == 1 && !dx_fallback &&
1750 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1751 + return make_indexed_dir(handle, dentry, inode, bh);
1755 + bh = ext3_append(handle, dir, &block, &retval);
1758 - rec_len = EXT3_DIR_REC_LEN(namelen);
1760 de = (struct ext3_dir_entry_2 *) bh->b_data;
1762 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1765 - bh = ext3_bread (handle, dir,
1766 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1769 - if (dir->i_size <= offset) {
1770 - if (dir->i_size == 0) {
1775 + de->rec_len = cpu_to_le16(rlen = blocksize);
1777 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1780 - ext3_debug ("creating next block\n");
1781 +#ifdef CONFIG_EXT3_INDEX
1783 + * Returns 0 for success, or a negative error value
1785 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1786 + struct inode *inode)
1788 + struct dx_frame frames[2], *frame;
1789 + struct dx_entry *entries, *at;
1790 + struct dx_hash_info hinfo;
1791 + struct buffer_head * bh;
1792 + struct inode *dir = dentry->d_parent->d_inode;
1793 + struct super_block * sb = dir->i_sb;
1794 + struct ext3_dir_entry_2 *de;
1797 - BUFFER_TRACE(bh, "get_write_access");
1798 - ext3_journal_get_write_access(handle, bh);
1799 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1801 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1802 - dir->u.ext3_i.i_disksize =
1803 - dir->i_size = offset + sb->s_blocksize;
1804 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1805 - ext3_mark_inode_dirty(handle, dir);
1807 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1810 + entries = frame->entries;
1813 - ext3_debug ("skipping to next block\n");
1814 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1817 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1820 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1825 - if (ext3_match (namelen, name, de)) {
1828 + BUFFER_TRACE(bh, "get_write_access");
1829 + err = ext3_journal_get_write_access(handle, bh);
1831 + goto journal_error;
1833 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1834 + if (err != -ENOSPC) {
1839 + /* Block full, should compress but for now just split */
1840 + dxtrace(printk("using %u of %u node entries\n",
1841 + dx_get_count(entries), dx_get_limit(entries)));
1842 + /* Need to split index? */
1843 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1845 + unsigned icount = dx_get_count(entries);
1846 + int levels = frame - frames;
1847 + struct dx_entry *entries2;
1848 + struct dx_node *node2;
1849 + struct buffer_head *bh2;
1851 + if (levels && (dx_get_count(frames->entries) ==
1852 + dx_get_limit(frames->entries))) {
1853 + ext3_warning(sb, __FUNCTION__,
1854 + "Directory index full!\n");
1858 - if ((le32_to_cpu(de->inode) == 0 &&
1859 - le16_to_cpu(de->rec_len) >= rec_len) ||
1860 - (le16_to_cpu(de->rec_len) >=
1861 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1862 - BUFFER_TRACE(bh, "get_write_access");
1863 - ext3_journal_get_write_access(handle, bh);
1864 - /* By now the buffer is marked for journaling */
1865 - offset += le16_to_cpu(de->rec_len);
1866 - if (le32_to_cpu(de->inode)) {
1867 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1868 - EXT3_DIR_REC_LEN(de->name_len));
1870 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1871 - EXT3_DIR_REC_LEN(de->name_len));
1872 - de->rec_len = cpu_to_le16(
1873 - EXT3_DIR_REC_LEN(de->name_len));
1875 + bh2 = ext3_append (handle, dir, &newblock, &err);
1878 + node2 = (struct dx_node *)(bh2->b_data);
1879 + entries2 = node2->entries;
1880 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1881 + node2->fake.inode = 0;
1882 + BUFFER_TRACE(frame->bh, "get_write_access");
1883 + err = ext3_journal_get_write_access(handle, frame->bh);
1885 + goto journal_error;
1887 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1888 + unsigned hash2 = dx_get_hash(entries + icount1);
1889 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1891 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1892 + err = ext3_journal_get_write_access(handle,
1895 + goto journal_error;
1897 + memcpy ((char *) entries2, (char *) (entries + icount1),
1898 + icount2 * sizeof(struct dx_entry));
1899 + dx_set_count (entries, icount1);
1900 + dx_set_count (entries2, icount2);
1901 + dx_set_limit (entries2, dx_node_limit(dir));
1903 + /* Which index block gets the new entry? */
1904 + if (at - entries >= icount1) {
1905 + frame->at = at = at - entries - icount1 + entries2;
1906 + frame->entries = entries = entries2;
1907 + swap(frame->bh, bh2);
1909 - de->file_type = EXT3_FT_UNKNOWN;
1911 - de->inode = cpu_to_le32(inode->i_ino);
1912 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1915 - de->name_len = namelen;
1916 - memcpy (de->name, name, namelen);
1918 - * XXX shouldn't update any times until successful
1919 - * completion of syscall, but too many callers depend
1922 - * XXX similarly, too many callers depend on
1923 - * ext3_new_inode() setting the times, but error
1924 - * recovery deletes the inode, so the worst that can
1925 - * happen is that the times are slightly out of date
1926 - * and/or different from the directory change time.
1928 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1929 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1930 - dir->i_version = ++event;
1931 - ext3_mark_inode_dirty(handle, dir);
1932 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1933 - ext3_journal_dirty_metadata(handle, bh);
1936 + dx_insert_block (frames + 0, hash2, newblock);
1937 + dxtrace(dx_show_index ("node", frames[1].entries));
1938 + dxtrace(dx_show_index ("node",
1939 + ((struct dx_node *) bh2->b_data)->entries));
1940 + err = ext3_journal_dirty_metadata(handle, bh2);
1942 + goto journal_error;
1945 + dxtrace(printk("Creating second level index...\n"));
1946 + memcpy((char *) entries2, (char *) entries,
1947 + icount * sizeof(struct dx_entry));
1948 + dx_set_limit(entries2, dx_node_limit(dir));
1951 + dx_set_count(entries, 1);
1952 + dx_set_block(entries + 0, newblock);
1953 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1955 + /* Add new access path frame */
1956 + frame = frames + 1;
1957 + frame->at = at = at - entries + entries2;
1958 + frame->entries = entries = entries2;
1960 + err = ext3_journal_get_write_access(handle,
1963 + goto journal_error;
1965 - offset += le16_to_cpu(de->rec_len);
1966 - de = (struct ext3_dir_entry_2 *)
1967 - ((char *) de + le16_to_cpu(de->rec_len));
1968 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1972 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1975 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1980 + ext3_std_error(dir->i_sb, err);
1984 + dx_release(frames);
1990 * ext3_delete_entry deletes a directory entry by merging it with the
1991 @@ -451,9 +1530,11 @@ static int ext3_create (struct inode * d
1992 struct inode * inode;
1995 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
1996 - if (IS_ERR(handle))
1997 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1998 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
1999 + if (IS_ERR(handle)) {
2000 return PTR_ERR(handle);
2005 @@ -478,9 +1559,11 @@ static int ext3_mknod (struct inode * di
2006 struct inode *inode;
2009 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2010 - if (IS_ERR(handle))
2011 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2012 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2013 + if (IS_ERR(handle)) {
2014 return PTR_ERR(handle);
2019 @@ -507,9 +1590,11 @@ static int ext3_mkdir(struct inode * dir
2020 if (dir->i_nlink >= EXT3_LINK_MAX)
2023 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2024 - if (IS_ERR(handle))
2025 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2026 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2027 + if (IS_ERR(handle)) {
2028 return PTR_ERR(handle);
2033 @@ -521,7 +1606,7 @@ static int ext3_mkdir(struct inode * dir
2035 inode->i_op = &ext3_dir_inode_operations;
2036 inode->i_fop = &ext3_dir_operations;
2037 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2038 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2039 inode->i_blocks = 0;
2040 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2042 @@ -554,21 +1639,19 @@ static int ext3_mkdir(struct inode * dir
2043 inode->i_mode |= S_ISGID;
2044 ext3_mark_inode_dirty(handle, inode);
2045 err = ext3_add_entry (handle, dentry, inode);
2047 - goto out_no_entry;
2049 + inode->i_nlink = 0;
2050 + ext3_mark_inode_dirty(handle, inode);
2055 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2056 + ext3_update_dx_flag(dir);
2057 ext3_mark_inode_dirty(handle, dir);
2058 d_instantiate(dentry, inode);
2060 ext3_journal_stop(handle, dir);
2064 - inode->i_nlink = 0;
2065 - ext3_mark_inode_dirty(handle, inode);
2071 @@ -655,7 +1738,7 @@ int ext3_orphan_add(handle_t *handle, st
2075 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2076 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2079 /* Orphan handling is only valid for files with data blocks
2080 @@ -696,7 +1779,7 @@ int ext3_orphan_add(handle_t *handle, st
2081 * This is safe: on error we're going to ignore the orphan list
2082 * anyway on the next recovery. */
2084 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2085 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2087 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2088 jbd_debug(4, "orphan inode %ld will point to %d\n",
2089 @@ -714,25 +1797,26 @@ out_unlock:
2090 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2092 struct list_head *prev;
2093 + struct ext3_inode_info *ei = EXT3_I(inode);
2094 struct ext3_sb_info *sbi;
2096 struct ext3_iloc iloc;
2099 lock_super(inode->i_sb);
2100 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2101 + if (list_empty(&ei->i_orphan)) {
2102 unlock_super(inode->i_sb);
2106 ino_next = NEXT_ORPHAN(inode);
2107 - prev = inode->u.ext3_i.i_orphan.prev;
2108 + prev = ei->i_orphan.prev;
2109 sbi = EXT3_SB(inode->i_sb);
2111 jbd_debug(4, "remove inode %ld from orphan list\n", inode->i_ino);
2113 - list_del(&inode->u.ext3_i.i_orphan);
2114 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2115 + list_del(&ei->i_orphan);
2116 + INIT_LIST_HEAD(&ei->i_orphan);
2118 /* If we're on an error path, we may not have a valid
2119 * transaction handle with which to update the orphan list on
2120 @@ -793,8 +1877,9 @@ static int ext3_rmdir (struct inode * di
2123 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2124 - if (IS_ERR(handle))
2125 + if (IS_ERR(handle)) {
2126 return PTR_ERR(handle);
2130 bh = ext3_find_entry (dentry, &de);
2131 @@ -832,7 +1917,7 @@ static int ext3_rmdir (struct inode * di
2133 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2134 ext3_mark_inode_dirty(handle, inode);
2135 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2136 + ext3_update_dx_flag(dir);
2137 ext3_mark_inode_dirty(handle, dir);
2140 @@ -850,8 +1935,9 @@ static int ext3_unlink(struct inode * di
2143 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2144 - if (IS_ERR(handle))
2145 + if (IS_ERR(handle)) {
2146 return PTR_ERR(handle);
2151 @@ -878,7 +1964,7 @@ static int ext3_unlink(struct inode * di
2154 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2155 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2156 + ext3_update_dx_flag(dir);
2157 ext3_mark_inode_dirty(handle, dir);
2159 if (!inode->i_nlink)
2160 @@ -904,9 +1990,11 @@ static int ext3_symlink (struct inode *
2161 if (l > dir->i_sb->s_blocksize)
2162 return -ENAMETOOLONG;
2164 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2165 - if (IS_ERR(handle))
2166 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2167 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2168 + if (IS_ERR(handle)) {
2169 return PTR_ERR(handle);
2174 @@ -916,7 +2004,7 @@ static int ext3_symlink (struct inode *
2178 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2179 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2180 inode->i_op = &page_symlink_inode_operations;
2181 inode->i_mapping->a_ops = &ext3_aops;
2183 @@ -925,25 +2013,23 @@ static int ext3_symlink (struct inode *
2184 * i_size in generic_commit_write().
2186 err = block_symlink(inode, symname, l);
2188 - goto out_no_entry;
2190 + ext3_dec_count(handle, inode);
2191 + ext3_mark_inode_dirty(handle, inode);
2196 inode->i_op = &ext3_fast_symlink_inode_operations;
2197 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2198 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2199 inode->i_size = l-1;
2201 - inode->u.ext3_i.i_disksize = inode->i_size;
2202 + EXT3_I(inode)->i_disksize = inode->i_size;
2203 err = ext3_add_nondir(handle, dentry, inode);
2204 ext3_mark_inode_dirty(handle, inode);
2206 ext3_journal_stop(handle, dir);
2210 - ext3_dec_count(handle, inode);
2211 - ext3_mark_inode_dirty(handle, inode);
2216 static int ext3_link (struct dentry * old_dentry,
2217 @@ -956,12 +2042,15 @@ static int ext3_link (struct dentry * ol
2218 if (S_ISDIR(inode->i_mode))
2221 - if (inode->i_nlink >= EXT3_LINK_MAX)
2222 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2226 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2227 - if (IS_ERR(handle))
2228 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2229 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2230 + if (IS_ERR(handle)) {
2231 return PTR_ERR(handle);
2236 @@ -995,9 +2084,11 @@ static int ext3_rename (struct inode * o
2238 old_bh = new_bh = dir_bh = NULL;
2240 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2241 - if (IS_ERR(handle))
2242 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2243 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2244 + if (IS_ERR(handle)) {
2245 return PTR_ERR(handle);
2248 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2250 @@ -1077,7 +2168,7 @@ static int ext3_rename (struct inode * o
2251 new_inode->i_ctime = CURRENT_TIME;
2253 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2254 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2255 + ext3_update_dx_flag(old_dir);
2257 BUFFER_TRACE(dir_bh, "get_write_access");
2258 ext3_journal_get_write_access(handle, dir_bh);
2259 @@ -1089,7 +2180,7 @@ static int ext3_rename (struct inode * o
2260 new_inode->i_nlink--;
2263 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2264 + ext3_update_dx_flag(new_dir);
2265 ext3_mark_inode_dirty(handle, new_dir);
2268 --- linux-2.4.20/fs/ext3/super.c~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2269 +++ linux-2.4.20-braam/fs/ext3/super.c Sat Apr 5 03:56:31 2003
2270 @@ -707,6 +707,7 @@ static int ext3_setup_super(struct super
2271 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2272 ext3_update_dynamic_rev(sb);
2273 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2275 ext3_commit_super (sb, es, 1);
2276 if (test_opt (sb, DEBUG))
2278 @@ -717,6 +718,7 @@ static int ext3_setup_super(struct super
2279 EXT3_BLOCKS_PER_GROUP(sb),
2280 EXT3_INODES_PER_GROUP(sb),
2283 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2284 bdevname(sb->s_dev));
2285 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2286 @@ -890,6 +892,7 @@ static loff_t ext3_max_size(int bits)
2291 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2294 @@ -1066,6 +1069,9 @@ struct super_block * ext3_read_super (st
2295 sbi->s_mount_state = le16_to_cpu(es->s_state);
2296 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2297 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2298 + for (i=0; i < 4; i++)
2299 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2300 + sbi->s_def_hash_version = es->s_def_hash_version;
2302 if (sbi->s_blocks_per_group > blocksize * 8) {
2304 @@ -1769,6 +1775,7 @@ static void __exit exit_ext3_fs(void)
2305 unregister_filesystem(&ext3_fs_type);
2308 +EXPORT_SYMBOL(ext3_force_commit);
2309 EXPORT_SYMBOL(ext3_bread);
2311 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2312 --- linux-2.4.20/include/linux/ext3_fs.h~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2313 +++ linux-2.4.20-braam/include/linux/ext3_fs.h Sat Apr 5 03:56:31 2003
2315 #define EXT3FS_VERSION "2.4-0.9.19"
2318 + * Always enable hashed directories
2320 +#define CONFIG_EXT3_INDEX
2326 @@ -437,8 +442,11 @@ struct ext3_super_block {
2327 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2328 __u32 s_journal_dev; /* device number of journal file */
2329 __u32 s_last_orphan; /* start of list of inodes to delete */
2331 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2332 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2333 + __u8 s_def_hash_version; /* Default hash version to use */
2334 + __u8 s_reserved_char_pad;
2335 + __u16 s_reserved_word_pad;
2336 + __u32 s_reserved[192]; /* Padding to the end of the block */
2340 @@ -575,9 +583,46 @@ struct ext3_dir_entry_2 {
2341 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2342 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2345 + * Hash Tree Directory indexing
2346 + * (c) Daniel Phillips, 2001
2349 +#ifdef CONFIG_EXT3_INDEX
2350 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2351 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2352 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2353 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2354 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2356 + #define is_dx(dir) 0
2357 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2358 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2361 +/* Legal values for the dx_root hash_version field: */
2363 +#define DX_HASH_LEGACY 0
2364 +#define DX_HASH_HALF_MD4 1
2365 +#define DX_HASH_TEA 2
2367 +/* hash info structure used by the directory hash */
2368 +struct dx_hash_info
2378 + * Control parameters used by ext3_htree_next_block
2380 +#define HASH_NB_ALWAYS 1
2384 * Describe an inode's exact location on disk and in memory
2387 @@ -587,6 +632,27 @@ struct ext3_iloc
2388 unsigned long block_group;
2393 + * This structure is stuffed into the struct file's private_data field
2394 + * for directories. It is where we put information so that we can do
2395 + * readdir operations in hash tree order.
2397 +struct dir_private_info {
2399 + rb_node_t *curr_node;
2400 + struct fname *extra_fname;
2403 + __u32 curr_minor_hash;
2408 + * Special error return code only used by dx_probe() and its callers.
2410 +#define ERR_BAD_DX_DIR -75000
2413 * Function prototypes
2415 @@ -614,11 +680,20 @@ extern struct ext3_group_desc * ext3_get
2418 extern int ext3_check_dir_entry(const char *, struct inode *,
2419 - struct ext3_dir_entry_2 *, struct buffer_head *,
2421 + struct ext3_dir_entry_2 *,
2422 + struct buffer_head *, unsigned long);
2423 +extern void ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2425 + struct ext3_dir_entry_2 *dirent);
2426 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2429 extern int ext3_sync_file (struct file *, struct dentry *, int);
2432 +extern int ext3fs_dirhash(const char *name, int len, struct
2433 + dx_hash_info *hinfo);
2436 extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2437 extern void ext3_free_inode (handle_t *, struct inode *);
2438 @@ -650,6 +725,8 @@ extern int ext3_ioctl (struct inode *, s
2440 extern int ext3_orphan_add(handle_t *, struct inode *);
2441 extern int ext3_orphan_del(handle_t *, struct inode *);
2442 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2443 + __u32 start_minor_hash, __u32 *next_hash);
2446 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2447 --- linux-2.4.20/include/linux/ext3_fs_sb.h~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2448 +++ linux-2.4.20-braam/include/linux/ext3_fs_sb.h Sat Apr 5 03:56:31 2003
2449 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2452 u32 s_next_generation;
2453 + u32 s_hash_seed[4];
2454 + int s_def_hash_version;
2457 struct inode * s_journal_inode;
2458 --- linux-2.4.20/include/linux/ext3_jbd.h~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2459 +++ linux-2.4.20-braam/include/linux/ext3_jbd.h Sat Apr 5 03:56:31 2003
2460 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2462 #define EXT3_RESERVE_TRANS_BLOCKS 12
2464 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2467 ext3_mark_iloc_dirty(handle_t *handle,
2468 struct inode *inode,
2469 --- linux-2.4.20/include/linux/rbtree.h~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2470 +++ linux-2.4.20-braam/include/linux/rbtree.h Sat Apr 5 03:56:31 2003
2471 @@ -120,6 +120,8 @@ rb_root_t;
2473 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2474 extern void rb_erase(rb_node_t *, rb_root_t *);
2475 +extern rb_node_t *rb_get_first(rb_root_t *root);
2476 +extern rb_node_t *rb_get_next(rb_node_t *n);
2478 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2480 --- linux-2.4.20/lib/rbtree.c~ext-2.4-patch-1 Sat Apr 5 03:56:31 2003
2481 +++ linux-2.4.20-braam/lib/rbtree.c Sat Apr 5 03:56:31 2003
2483 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2487 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2490 #include <linux/rbtree.h>
2491 @@ -294,3 +296,43 @@ void rb_erase(rb_node_t * node, rb_root_
2492 __rb_erase_color(child, parent, root);
2494 EXPORT_SYMBOL(rb_erase);
2497 + * This function returns the first node (in sort order) of the tree.
2499 +rb_node_t *rb_get_first(rb_root_t *root)
2503 + n = root->rb_node;
2506 + while (n->rb_left)
2510 +EXPORT_SYMBOL(rb_get_first);
2513 + * Given a node, this function will return the next node in the tree.
2515 +rb_node_t *rb_get_next(rb_node_t *n)
2517 + rb_node_t *parent;
2519 + if (n->rb_right) {
2521 + while (n->rb_left)
2525 + while ((parent = n->rb_parent)) {
2526 + if (n == parent->rb_left)
2533 +EXPORT_SYMBOL(rb_get_next);