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 --- linux-chaos-2.4.20-6/fs/ext3/Makefile~ext-2.4-patch-1-chaos 2003-04-09 16:10:38.000000000 -0600
15 +++ linux-chaos-2.4.20-6-braam/fs/ext3/Makefile 2003-04-09 16:18:55.000000000 -0600
16 @@ -12,7 +12,7 @@ O_TARGET := ext3.o
17 export-objs := super.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-chaos-2.4.20-6/fs/ext3/dir.c~ext-2.4-patch-1-chaos 2002-05-07 15:53:46.000000000 -0600
26 +++ linux-chaos-2.4.20-6-braam/fs/ext3/dir.c 2003-04-09 16:18:55.000000000 -0600
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-chaos-2.4.20-6/fs/ext3/file.c~ext-2.4-patch-1-chaos 2003-02-14 15:59:09.000000000 -0700
370 +++ linux-chaos-2.4.20-6-braam/fs/ext3/file.c 2003-04-09 16:18:55.000000000 -0600
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 2003-01-30 03:24:37.000000000 -0700
382 +++ linux-chaos-2.4.20-6-braam/fs/ext3/hash.c 2003-04-09 16:18:55.000000000 -0600
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-chaos-2.4.20-6/fs/ext3/namei.c~ext-2.4-patch-1-chaos 2003-03-12 12:51:02.000000000 -0700
600 +++ linux-chaos-2.4.20-6-braam/fs/ext3/namei.c 2003-04-09 16:26:04.000000000 -0600
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 @@ -166,7 +819,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 @@ -197,6 +850,66 @@ 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 + if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
1329 + hash = hinfo.hash;
1331 + block = dx_get_block(frame->at);
1332 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1334 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1335 + top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
1336 + EXT3_DIR_REC_LEN(0));
1337 + for (; de < top; de = ext3_next_entry(de))
1338 + if (ext3_match (namelen, name, de)) {
1339 + if (!ext3_check_dir_entry("ext3_find_entry",
1341 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1342 + +((char *)de - bh->b_data))) {
1347 + dx_release (frames);
1351 + /* Check to see if we should continue to search */
1352 + retval = ext3_htree_next_block(dir, hash, frame,
1354 + if (retval == -1) {
1355 + ext3_warning(sb, __FUNCTION__,
1356 + "error reading index page in directory #%lu",
1360 + } while (retval == 1);
1364 + dxtrace(printk("%s not found\n", name));
1365 + dx_release (frames);
1370 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1372 struct inode * inode;
1373 @@ -213,8 +926,9 @@ static struct dentry *ext3_lookup(struct
1375 inode = iget(dir->i_sb, ino);
1379 return ERR_PTR(-EACCES);
1382 d_add(dentry, inode);
1384 @@ -238,6 +952,301 @@ static inline void ext3_set_de_type(stru
1385 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1388 +#ifdef CONFIG_EXT3_INDEX
1389 +static struct ext3_dir_entry_2 *
1390 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1392 + unsigned rec_len = 0;
1395 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1396 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1397 + memcpy (to, de, rec_len);
1398 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1403 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1406 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1408 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1409 + unsigned rec_len = 0;
1412 + while ((char*)de < base + size) {
1413 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1414 + le16_to_cpu(de->rec_len));
1415 + if (de->inode && de->name_len) {
1416 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1418 + memmove(to, de, rec_len);
1419 + to->rec_len = cpu_to_le16(rec_len);
1421 + to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1428 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1429 + struct buffer_head **bh,struct dx_frame *frame,
1430 + struct dx_hash_info *hinfo, int *error)
1432 + unsigned blocksize = dir->i_sb->s_blocksize;
1433 + unsigned count, continued;
1434 + struct buffer_head *bh2;
1437 + struct dx_map_entry *map;
1438 + char *data1 = (*bh)->b_data, *data2;
1440 + struct ext3_dir_entry_2 *de = NULL, *de2;
1443 + bh2 = ext3_append (handle, dir, &newblock, error);
1450 + BUFFER_TRACE(*bh, "get_write_access");
1451 + err = ext3_journal_get_write_access(handle, *bh);
1457 + ext3_std_error(dir->i_sb, err);
1460 + BUFFER_TRACE(frame->bh, "get_write_access");
1461 + err = ext3_journal_get_write_access(handle, frame->bh);
1463 + goto journal_error;
1465 + data2 = bh2->b_data;
1467 + /* create map in the end of data2 block */
1468 + map = (struct dx_map_entry *) (data2 + blocksize);
1469 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1470 + blocksize, hinfo, map);
1472 + split = count/2; // need to adjust to actual middle
1473 + dx_sort_map (map, count);
1474 + hash2 = map[split].hash;
1475 + continued = hash2 == map[split - 1].hash;
1476 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1477 + dx_get_block(frame->at), hash2, split, count-split));
1479 + /* Fancy dance to stay within two buffers */
1480 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1481 + de = dx_pack_dirents(data1,blocksize);
1482 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1483 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1484 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1485 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1487 + /* Which block gets the new entry? */
1488 + if (hinfo->hash >= hash2)
1493 + dx_insert_block (frame, hash2 + continued, newblock);
1494 + err = ext3_journal_dirty_metadata (handle, bh2);
1496 + goto journal_error;
1497 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1499 + goto journal_error;
1501 + dxtrace(dx_show_index ("frame", frame->entries));
1509 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1510 + * it points to a directory entry which is guaranteed to be large
1511 + * enough for new directory entry. If de is NULL, then
1512 + * add_dirent_to_buf will attempt search the directory block for
1513 + * space. It will return -ENOSPC if no space is available, and -EIO
1514 + * and -EEXIST if directory entry already exists.
1516 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1517 + * all other cases bh is released.
1519 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1520 + struct inode *inode, struct ext3_dir_entry_2 *de,
1521 + struct buffer_head * bh)
1523 + struct inode *dir = dentry->d_parent->d_inode;
1524 + const char *name = dentry->d_name.name;
1525 + int namelen = dentry->d_name.len;
1526 + unsigned long offset = 0;
1527 + unsigned short reclen;
1528 + int nlen, rlen, err;
1531 + reclen = EXT3_DIR_REC_LEN(namelen);
1533 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1534 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1535 + while ((char *) de <= top) {
1536 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1541 + if (ext3_match (namelen, name, de)) {
1545 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1546 + rlen = le16_to_cpu(de->rec_len);
1547 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1549 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1552 + if ((char *) de > top)
1555 + BUFFER_TRACE(bh, "get_write_access");
1556 + err = ext3_journal_get_write_access(handle, bh);
1558 + ext3_std_error(dir->i_sb, err);
1563 + /* By now the buffer is marked for journaling */
1564 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1565 + rlen = le16_to_cpu(de->rec_len);
1567 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1568 + de1->rec_len = cpu_to_le16(rlen - nlen);
1569 + de->rec_len = cpu_to_le16(nlen);
1572 + de->file_type = EXT3_FT_UNKNOWN;
1574 + de->inode = cpu_to_le32(inode->i_ino);
1575 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1578 + de->name_len = namelen;
1579 + memcpy (de->name, name, namelen);
1581 + * XXX shouldn't update any times until successful
1582 + * completion of syscall, but too many callers depend
1585 + * XXX similarly, too many callers depend on
1586 + * ext3_new_inode() setting the times, but error
1587 + * recovery deletes the inode, so the worst that can
1588 + * happen is that the times are slightly out of date
1589 + * and/or different from the directory change time.
1591 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1592 + ext3_update_dx_flag(dir);
1593 + dir->i_version = ++event;
1594 + ext3_mark_inode_dirty(handle, dir);
1595 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1596 + err = ext3_journal_dirty_metadata(handle, bh);
1598 + ext3_std_error(dir->i_sb, err);
1603 +#ifdef CONFIG_EXT3_INDEX
1605 + * This converts a one block unindexed directory to a 3 block indexed
1606 + * directory, and adds the dentry to the indexed directory.
1608 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1609 + struct inode *inode, struct buffer_head *bh)
1611 + struct inode *dir = dentry->d_parent->d_inode;
1612 + const char *name = dentry->d_name.name;
1613 + int namelen = dentry->d_name.len;
1614 + struct buffer_head *bh2;
1615 + struct dx_root *root;
1616 + struct dx_frame frames[2], *frame;
1617 + struct dx_entry *entries;
1618 + struct ext3_dir_entry_2 *de, *de2;
1619 + char *data1, *top;
1622 + unsigned blocksize;
1623 + struct dx_hash_info hinfo;
1626 + blocksize = dir->i_sb->s_blocksize;
1627 + dxtrace(printk("Creating index\n"));
1628 + retval = ext3_journal_get_write_access(handle, bh);
1630 + ext3_std_error(dir->i_sb, retval);
1634 + root = (struct dx_root *) bh->b_data;
1636 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1637 + bh2 = ext3_append (handle, dir, &block, &retval);
1642 + data1 = bh2->b_data;
1644 + /* The 0th block becomes the root, move the dirents out */
1645 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1646 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1647 + len = ((char *) root) + blocksize - (char *) de;
1648 + memcpy (data1, de, len);
1649 + de = (struct ext3_dir_entry_2 *) data1;
1650 + top = data1 + len;
1651 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1653 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1654 + /* Initialize the root; the dot dirents already exist */
1655 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1656 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1657 + memset (&root->info, 0, sizeof(root->info));
1658 + root->info.info_length = sizeof(root->info);
1659 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1660 + entries = root->entries;
1661 + dx_set_block (entries, 1);
1662 + dx_set_count (entries, 1);
1663 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1665 + /* Initialize as for dx_probe */
1666 + hinfo.hash_version = root->info.hash_version;
1667 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1668 + ext3fs_dirhash(name, namelen, &hinfo);
1670 + frame->entries = entries;
1671 + frame->at = entries;
1674 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1675 + dx_release (frames);
1679 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1686 @@ -248,127 +1256,198 @@ static inline void ext3_set_de_type(stru
1687 * may not sleep between calling this and putting something into
1688 * the entry, as someone else might have used it while you slept.
1692 - * AKPM: the journalling code here looks wrong on the error paths
1694 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1695 struct inode *inode)
1697 struct inode *dir = dentry->d_parent->d_inode;
1698 - const char *name = dentry->d_name.name;
1699 - int namelen = dentry->d_name.len;
1700 unsigned long offset;
1701 - unsigned short rec_len;
1702 struct buffer_head * bh;
1703 - struct ext3_dir_entry_2 * de, * de1;
1704 + struct ext3_dir_entry_2 *de;
1705 struct super_block * sb;
1707 +#ifdef CONFIG_EXT3_INDEX
1708 + int dx_fallback=0;
1710 + unsigned blocksize;
1711 + unsigned nlen, rlen;
1712 + u32 block, blocks;
1717 + blocksize = sb->s_blocksize;
1718 + if (!dentry->d_name.len)
1720 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1721 +#ifdef CONFIG_EXT3_INDEX
1723 + retval = ext3_dx_add_entry(handle, dentry, inode);
1724 + if (!retval || (retval != ERR_BAD_DX_DIR))
1726 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1728 + ext3_mark_inode_dirty(handle, dir);
1731 + blocks = dir->i_size >> sb->s_blocksize_bits;
1732 + for (block = 0, offset = 0; block < blocks; block++) {
1733 + bh = ext3_bread(handle, dir, block, 0, &retval);
1736 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1737 + if (retval != -ENOSPC)
1740 +#ifdef CONFIG_EXT3_INDEX
1741 + if (blocks == 1 && !dx_fallback &&
1742 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1743 + return make_indexed_dir(handle, dentry, inode, bh);
1747 + bh = ext3_append(handle, dir, &block, &retval);
1750 - rec_len = EXT3_DIR_REC_LEN(namelen);
1752 de = (struct ext3_dir_entry_2 *) bh->b_data;
1754 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1757 - bh = ext3_bread (handle, dir,
1758 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1761 - if (dir->i_size <= offset) {
1762 - if (dir->i_size == 0) {
1767 + de->rec_len = cpu_to_le16(rlen = blocksize);
1769 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1772 - ext3_debug ("creating next block\n");
1773 +#ifdef CONFIG_EXT3_INDEX
1775 + * Returns 0 for success, or a negative error value
1777 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1778 + struct inode *inode)
1780 + struct dx_frame frames[2], *frame;
1781 + struct dx_entry *entries, *at;
1782 + struct dx_hash_info hinfo;
1783 + struct buffer_head * bh;
1784 + struct inode *dir = dentry->d_parent->d_inode;
1785 + struct super_block * sb = dir->i_sb;
1786 + struct ext3_dir_entry_2 *de;
1789 - BUFFER_TRACE(bh, "get_write_access");
1790 - ext3_journal_get_write_access(handle, bh);
1791 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1793 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1794 - dir->u.ext3_i.i_disksize =
1795 - dir->i_size = offset + sb->s_blocksize;
1796 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1797 - ext3_mark_inode_dirty(handle, dir);
1799 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1802 + entries = frame->entries;
1805 - ext3_debug ("skipping to next block\n");
1806 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1809 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1812 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1817 - if (ext3_match (namelen, name, de)) {
1820 + BUFFER_TRACE(bh, "get_write_access");
1821 + err = ext3_journal_get_write_access(handle, bh);
1823 + goto journal_error;
1825 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1826 + if (err != -ENOSPC) {
1831 + /* Block full, should compress but for now just split */
1832 + dxtrace(printk("using %u of %u node entries\n",
1833 + dx_get_count(entries), dx_get_limit(entries)));
1834 + /* Need to split index? */
1835 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1837 + unsigned icount = dx_get_count(entries);
1838 + int levels = frame - frames;
1839 + struct dx_entry *entries2;
1840 + struct dx_node *node2;
1841 + struct buffer_head *bh2;
1843 + if (levels && (dx_get_count(frames->entries) ==
1844 + dx_get_limit(frames->entries))) {
1845 + ext3_warning(sb, __FUNCTION__,
1846 + "Directory index full!\n");
1850 - if ((le32_to_cpu(de->inode) == 0 &&
1851 - le16_to_cpu(de->rec_len) >= rec_len) ||
1852 - (le16_to_cpu(de->rec_len) >=
1853 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1854 - BUFFER_TRACE(bh, "get_write_access");
1855 - ext3_journal_get_write_access(handle, bh);
1856 - /* By now the buffer is marked for journaling */
1857 - offset += le16_to_cpu(de->rec_len);
1858 - if (le32_to_cpu(de->inode)) {
1859 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1860 - EXT3_DIR_REC_LEN(de->name_len));
1862 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1863 - EXT3_DIR_REC_LEN(de->name_len));
1864 - de->rec_len = cpu_to_le16(
1865 - EXT3_DIR_REC_LEN(de->name_len));
1867 + bh2 = ext3_append (handle, dir, &newblock, &err);
1870 + node2 = (struct dx_node *)(bh2->b_data);
1871 + entries2 = node2->entries;
1872 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1873 + node2->fake.inode = 0;
1874 + BUFFER_TRACE(frame->bh, "get_write_access");
1875 + err = ext3_journal_get_write_access(handle, frame->bh);
1877 + goto journal_error;
1879 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1880 + unsigned hash2 = dx_get_hash(entries + icount1);
1881 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1883 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1884 + err = ext3_journal_get_write_access(handle,
1887 + goto journal_error;
1889 + memcpy ((char *) entries2, (char *) (entries + icount1),
1890 + icount2 * sizeof(struct dx_entry));
1891 + dx_set_count (entries, icount1);
1892 + dx_set_count (entries2, icount2);
1893 + dx_set_limit (entries2, dx_node_limit(dir));
1895 + /* Which index block gets the new entry? */
1896 + if (at - entries >= icount1) {
1897 + frame->at = at = at - entries - icount1 + entries2;
1898 + frame->entries = entries = entries2;
1899 + swap(frame->bh, bh2);
1901 - de->file_type = EXT3_FT_UNKNOWN;
1903 - de->inode = cpu_to_le32(inode->i_ino);
1904 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1907 - de->name_len = namelen;
1908 - memcpy (de->name, name, namelen);
1910 - * XXX shouldn't update any times until successful
1911 - * completion of syscall, but too many callers depend
1914 - * XXX similarly, too many callers depend on
1915 - * ext3_new_inode() setting the times, but error
1916 - * recovery deletes the inode, so the worst that can
1917 - * happen is that the times are slightly out of date
1918 - * and/or different from the directory change time.
1920 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1921 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1922 - dir->i_version = ++event;
1923 - ext3_mark_inode_dirty(handle, dir);
1924 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1925 - ext3_journal_dirty_metadata(handle, bh);
1928 + dx_insert_block (frames + 0, hash2, newblock);
1929 + dxtrace(dx_show_index ("node", frames[1].entries));
1930 + dxtrace(dx_show_index ("node",
1931 + ((struct dx_node *) bh2->b_data)->entries));
1932 + err = ext3_journal_dirty_metadata(handle, bh2);
1934 + goto journal_error;
1937 + dxtrace(printk("Creating second level index...\n"));
1938 + memcpy((char *) entries2, (char *) entries,
1939 + icount * sizeof(struct dx_entry));
1940 + dx_set_limit(entries2, dx_node_limit(dir));
1943 + dx_set_count(entries, 1);
1944 + dx_set_block(entries + 0, newblock);
1945 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1947 + /* Add new access path frame */
1948 + frame = frames + 1;
1949 + frame->at = at = at - entries + entries2;
1950 + frame->entries = entries = entries2;
1952 + err = ext3_journal_get_write_access(handle,
1955 + goto journal_error;
1957 - offset += le16_to_cpu(de->rec_len);
1958 - de = (struct ext3_dir_entry_2 *)
1959 - ((char *) de + le16_to_cpu(de->rec_len));
1960 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1964 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1967 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1972 + ext3_std_error(dir->i_sb, err);
1976 + dx_release(frames);
1982 * ext3_delete_entry deletes a directory entry by merging it with the
1983 @@ -455,9 +1534,11 @@ static int ext3_create (struct inode * d
1984 struct inode * inode;
1987 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
1988 - if (IS_ERR(handle))
1989 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1990 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
1991 + if (IS_ERR(handle)) {
1992 return PTR_ERR(handle);
1997 @@ -481,9 +1562,11 @@ static int ext3_mknod (struct inode * di
1998 struct inode *inode;
2001 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2002 - if (IS_ERR(handle))
2003 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2004 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2005 + if (IS_ERR(handle)) {
2006 return PTR_ERR(handle);
2011 @@ -509,9 +1592,11 @@ static int ext3_mkdir(struct inode * dir
2012 if (dir->i_nlink >= EXT3_LINK_MAX)
2015 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2016 - if (IS_ERR(handle))
2017 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2018 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2019 + if (IS_ERR(handle)) {
2020 return PTR_ERR(handle);
2025 @@ -523,7 +1608,7 @@ static int ext3_mkdir(struct inode * dir
2027 inode->i_op = &ext3_dir_inode_operations;
2028 inode->i_fop = &ext3_dir_operations;
2029 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2030 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2031 inode->i_blocks = 0;
2032 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2034 @@ -556,21 +1641,19 @@ static int ext3_mkdir(struct inode * dir
2035 inode->i_mode |= S_ISGID;
2036 ext3_mark_inode_dirty(handle, inode);
2037 err = ext3_add_entry (handle, dentry, inode);
2039 - goto out_no_entry;
2041 + inode->i_nlink = 0;
2042 + ext3_mark_inode_dirty(handle, inode);
2047 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2048 + ext3_update_dx_flag(dir);
2049 ext3_mark_inode_dirty(handle, dir);
2050 d_instantiate(dentry, inode);
2052 ext3_journal_stop(handle, dir);
2056 - inode->i_nlink = 0;
2057 - ext3_mark_inode_dirty(handle, inode);
2063 @@ -657,7 +1740,7 @@ int ext3_orphan_add(handle_t *handle, st
2067 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2068 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2071 /* Orphan handling is only valid for files with data blocks
2072 @@ -698,7 +1781,7 @@ int ext3_orphan_add(handle_t *handle, st
2073 * This is safe: on error we're going to ignore the orphan list
2074 * anyway on the next recovery. */
2076 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2077 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2079 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2080 jbd_debug(4, "orphan inode %ld will point to %d\n",
2081 @@ -716,25 +1799,26 @@ out_unlock:
2082 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2084 struct list_head *prev;
2085 + struct ext3_inode_info *ei = EXT3_I(inode);
2086 struct ext3_sb_info *sbi;
2087 unsigned long ino_next;
2088 struct ext3_iloc iloc;
2091 lock_super(inode->i_sb);
2092 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2093 + if (list_empty(&ei->i_orphan)) {
2094 unlock_super(inode->i_sb);
2098 ino_next = NEXT_ORPHAN(inode);
2099 - prev = inode->u.ext3_i.i_orphan.prev;
2100 + prev = ei->i_orphan.prev;
2101 sbi = EXT3_SB(inode->i_sb);
2103 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2105 - list_del(&inode->u.ext3_i.i_orphan);
2106 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2107 + list_del(&ei->i_orphan);
2108 + INIT_LIST_HEAD(&ei->i_orphan);
2110 /* If we're on an error path, we may not have a valid
2111 * transaction handle with which to update the orphan list on
2112 @@ -795,8 +1879,9 @@ static int ext3_rmdir (struct inode * di
2115 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2116 - if (IS_ERR(handle))
2117 + if (IS_ERR(handle)) {
2118 return PTR_ERR(handle);
2122 bh = ext3_find_entry (dentry, &de);
2123 @@ -834,7 +1919,7 @@ static int ext3_rmdir (struct inode * di
2125 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2126 ext3_mark_inode_dirty(handle, inode);
2127 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2128 + ext3_update_dx_flag(dir);
2129 ext3_mark_inode_dirty(handle, dir);
2132 @@ -852,8 +1937,9 @@ static int ext3_unlink(struct inode * di
2135 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2136 - if (IS_ERR(handle))
2137 + if (IS_ERR(handle)) {
2138 return PTR_ERR(handle);
2143 @@ -880,7 +1966,7 @@ static int ext3_unlink(struct inode * di
2146 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2147 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2148 + ext3_update_dx_flag(dir);
2149 ext3_mark_inode_dirty(handle, dir);
2151 if (!inode->i_nlink)
2152 @@ -906,9 +1992,11 @@ static int ext3_symlink (struct inode *
2153 if (l > dir->i_sb->s_blocksize)
2154 return -ENAMETOOLONG;
2156 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2157 - if (IS_ERR(handle))
2158 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2159 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2160 + if (IS_ERR(handle)) {
2161 return PTR_ERR(handle);
2166 @@ -918,7 +2006,7 @@ static int ext3_symlink (struct inode *
2170 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2171 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2172 inode->i_op = &page_symlink_inode_operations;
2173 inode->i_mapping->a_ops = &ext3_aops;
2175 @@ -927,24 +2015,23 @@ static int ext3_symlink (struct inode *
2176 * i_size in generic_commit_write().
2178 err = block_symlink(inode, symname, l);
2180 - goto out_no_entry;
2182 + ext3_dec_count(handle, inode);
2183 + ext3_mark_inode_dirty(handle, inode);
2188 inode->i_op = &ext3_fast_symlink_inode_operations;
2189 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2190 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2191 inode->i_size = l-1;
2193 - inode->u.ext3_i.i_disksize = inode->i_size;
2194 + EXT3_I(inode)->i_disksize = inode->i_size;
2195 err = ext3_add_nondir(handle, dentry, inode);
2196 + ext3_mark_inode_dirty(handle, inode);
2198 ext3_journal_stop(handle, dir);
2202 - ext3_dec_count(handle, inode);
2203 - ext3_mark_inode_dirty(handle, inode);
2208 static int ext3_link (struct dentry * old_dentry,
2209 @@ -957,12 +2044,15 @@ static int ext3_link (struct dentry * ol
2210 if (S_ISDIR(inode->i_mode))
2213 - if (inode->i_nlink >= EXT3_LINK_MAX)
2214 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2218 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2219 - if (IS_ERR(handle))
2220 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2221 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2222 + if (IS_ERR(handle)) {
2223 return PTR_ERR(handle);
2228 @@ -995,9 +2085,11 @@ static int ext3_rename (struct inode * o
2230 old_bh = new_bh = dir_bh = NULL;
2232 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2233 - if (IS_ERR(handle))
2234 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2235 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2236 + if (IS_ERR(handle)) {
2237 return PTR_ERR(handle);
2240 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2242 @@ -1077,7 +2169,7 @@ static int ext3_rename (struct inode * o
2243 new_inode->i_ctime = CURRENT_TIME;
2245 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2246 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2247 + ext3_update_dx_flag(old_dir);
2249 BUFFER_TRACE(dir_bh, "get_write_access");
2250 ext3_journal_get_write_access(handle, dir_bh);
2251 @@ -1089,7 +2181,7 @@ static int ext3_rename (struct inode * o
2252 new_inode->i_nlink--;
2255 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2256 + ext3_update_dx_flag(new_dir);
2257 ext3_mark_inode_dirty(handle, new_dir);
2260 --- linux-chaos-2.4.20-6/fs/ext3/super.c~ext-2.4-patch-1-chaos 2003-04-09 16:10:38.000000000 -0600
2261 +++ linux-chaos-2.4.20-6-braam/fs/ext3/super.c 2003-04-09 16:18:55.000000000 -0600
2262 @@ -710,6 +710,7 @@ static int ext3_setup_super(struct super
2263 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2264 ext3_update_dynamic_rev(sb);
2265 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2267 ext3_commit_super (sb, es, 1);
2268 if (test_opt (sb, DEBUG))
2270 @@ -720,6 +721,7 @@ static int ext3_setup_super(struct super
2271 EXT3_BLOCKS_PER_GROUP(sb),
2272 EXT3_INODES_PER_GROUP(sb),
2275 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2276 bdevname(sb->s_dev));
2277 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2278 @@ -893,6 +895,7 @@ static loff_t ext3_max_size(int bits)
2283 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2286 @@ -1069,6 +1072,9 @@ struct super_block * ext3_read_super (st
2287 sbi->s_mount_state = le16_to_cpu(es->s_state);
2288 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2289 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2290 + for (i=0; i < 4; i++)
2291 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2292 + sbi->s_def_hash_version = es->s_def_hash_version;
2294 if (sbi->s_blocks_per_group > blocksize * 8) {
2296 @@ -1770,6 +1776,7 @@ static void __exit exit_ext3_fs(void)
2297 unregister_filesystem(&ext3_fs_type);
2300 +EXPORT_SYMBOL(ext3_force_commit);
2301 EXPORT_SYMBOL(ext3_bread);
2303 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2304 --- linux-chaos-2.4.20-6/include/linux/ext3_fs.h~ext-2.4-patch-1-chaos 2003-03-12 12:51:27.000000000 -0700
2305 +++ linux-chaos-2.4.20-6-braam/include/linux/ext3_fs.h 2003-04-09 16:18:55.000000000 -0600
2307 #define EXT3FS_VERSION "2.4-0.9.19"
2310 + * Always enable hashed directories
2312 +#define CONFIG_EXT3_INDEX
2318 @@ -437,8 +442,11 @@ struct ext3_super_block {
2319 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2320 __u32 s_journal_dev; /* device number of journal file */
2321 __u32 s_last_orphan; /* start of list of inodes to delete */
2323 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2324 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2325 + __u8 s_def_hash_version; /* Default hash version to use */
2326 + __u8 s_reserved_char_pad;
2327 + __u16 s_reserved_word_pad;
2328 + __u32 s_reserved[192]; /* Padding to the end of the block */
2332 @@ -575,9 +583,46 @@ struct ext3_dir_entry_2 {
2333 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2334 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2337 + * Hash Tree Directory indexing
2338 + * (c) Daniel Phillips, 2001
2341 +#ifdef CONFIG_EXT3_INDEX
2342 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2343 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2344 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2345 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2346 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2348 + #define is_dx(dir) 0
2349 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2350 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2353 +/* Legal values for the dx_root hash_version field: */
2355 +#define DX_HASH_LEGACY 0
2356 +#define DX_HASH_HALF_MD4 1
2357 +#define DX_HASH_TEA 2
2359 +/* hash info structure used by the directory hash */
2360 +struct dx_hash_info
2370 + * Control parameters used by ext3_htree_next_block
2372 +#define HASH_NB_ALWAYS 1
2376 * Describe an inode's exact location on disk and in memory
2379 @@ -587,6 +632,27 @@ struct ext3_iloc
2380 unsigned long block_group;
2385 + * This structure is stuffed into the struct file's private_data field
2386 + * for directories. It is where we put information so that we can do
2387 + * readdir operations in hash tree order.
2389 +struct dir_private_info {
2391 + rb_node_t *curr_node;
2392 + struct fname *extra_fname;
2395 + __u32 curr_minor_hash;
2400 + * Special error return code only used by dx_probe() and its callers.
2402 +#define ERR_BAD_DX_DIR -75000
2405 * Function prototypes
2407 @@ -614,11 +680,20 @@ extern struct ext3_group_desc * ext3_get
2410 extern int ext3_check_dir_entry(const char *, struct inode *,
2411 - struct ext3_dir_entry_2 *, struct buffer_head *,
2413 + struct ext3_dir_entry_2 *,
2414 + struct buffer_head *, unsigned long);
2415 +extern void ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2417 + struct ext3_dir_entry_2 *dirent);
2418 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2421 extern int ext3_sync_file (struct file *, struct dentry *, int);
2424 +extern int ext3fs_dirhash(const char *name, int len, struct
2425 + dx_hash_info *hinfo);
2428 extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2429 extern void ext3_free_inode (handle_t *, struct inode *);
2430 @@ -650,6 +725,8 @@ extern int ext3_ioctl (struct inode *, s
2432 extern int ext3_orphan_add(handle_t *, struct inode *);
2433 extern int ext3_orphan_del(handle_t *, struct inode *);
2434 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2435 + __u32 start_minor_hash, __u32 *next_hash);
2438 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2439 --- linux-chaos-2.4.20-6/include/linux/ext3_fs_sb.h~ext-2.4-patch-1-chaos 2003-03-12 12:51:27.000000000 -0700
2440 +++ linux-chaos-2.4.20-6-braam/include/linux/ext3_fs_sb.h 2003-04-09 16:18:55.000000000 -0600
2441 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2444 u32 s_next_generation;
2445 + u32 s_hash_seed[4];
2446 + int s_def_hash_version;
2449 struct inode * s_journal_inode;
2450 --- linux-chaos-2.4.20-6/include/linux/ext3_jbd.h~ext-2.4-patch-1-chaos 2003-03-12 12:51:27.000000000 -0700
2451 +++ linux-chaos-2.4.20-6-braam/include/linux/ext3_jbd.h 2003-04-09 16:18:55.000000000 -0600
2452 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2454 #define EXT3_RESERVE_TRANS_BLOCKS 12U
2456 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2459 ext3_mark_iloc_dirty(handle_t *handle,
2460 struct inode *inode,
2461 --- linux-chaos-2.4.20-6/include/linux/rbtree.h~ext-2.4-patch-1-chaos 2002-05-07 15:53:47.000000000 -0600
2462 +++ linux-chaos-2.4.20-6-braam/include/linux/rbtree.h 2003-04-09 16:18:55.000000000 -0600
2463 @@ -120,6 +120,8 @@ rb_root_t;
2465 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2466 extern void rb_erase(rb_node_t *, rb_root_t *);
2467 +extern rb_node_t *rb_get_first(rb_root_t *root);
2468 +extern rb_node_t *rb_get_next(rb_node_t *n);
2470 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2472 --- linux-chaos-2.4.20-6/lib/rbtree.c~ext-2.4-patch-1-chaos 2002-09-25 11:14:03.000000000 -0600
2473 +++ linux-chaos-2.4.20-6-braam/lib/rbtree.c 2003-04-09 16:18:55.000000000 -0600
2475 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2479 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2482 #include <linux/rbtree.h>
2483 @@ -294,3 +296,43 @@ void rb_erase(rb_node_t * node, rb_root_
2484 __rb_erase_color(child, parent, root);
2486 EXPORT_SYMBOL(rb_erase);
2489 + * This function returns the first node (in sort order) of the tree.
2491 +rb_node_t *rb_get_first(rb_root_t *root)
2495 + n = root->rb_node;
2498 + while (n->rb_left)
2502 +EXPORT_SYMBOL(rb_get_first);
2505 + * Given a node, this function will return the next node in the tree.
2507 +rb_node_t *rb_get_next(rb_node_t *n)
2509 + rb_node_t *parent;
2511 + if (n->rb_right) {
2513 + while (n->rb_left)
2517 + while ((parent = n->rb_parent)) {
2518 + if (n == parent->rb_left)
2525 +EXPORT_SYMBOL(rb_get_next);