2 fs/ext3/dir.c | 302 +++++++++
4 fs/ext3/hash.c | 215 ++++++
5 fs/ext3/namei.c | 1421 ++++++++++++++++++++++++++++++++++++++++-----
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, 1922 insertions(+), 161 deletions(-)
14 Index: linux-2.4.21-chaos/fs/ext3/dir.c
15 ===================================================================
16 --- linux-2.4.21-chaos.orig/fs/ext3/dir.c 2002-05-08 01:53:46.000000000 +0400
17 +++ linux-2.4.21-chaos/fs/ext3/dir.c 2003-12-12 16:18:17.000000000 +0300
20 #include <linux/jbd.h>
21 #include <linux/ext3_fs.h>
22 +#include <linux/slab.h>
23 +#include <linux/rbtree.h>
25 static unsigned char ext3_filetype_table[] = {
26 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
29 static int ext3_readdir(struct file *, void *, filldir_t);
30 +static int ext3_dx_readdir(struct file * filp,
31 + void * dirent, filldir_t filldir);
33 struct file_operations ext3_dir_operations = {
34 read: generic_read_dir,
36 fsync: ext3_sync_file, /* BKL held */
40 +static unsigned char get_dtype(struct super_block *sb, int filetype)
42 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
43 + (filetype >= EXT3_FT_MAX))
46 + return (ext3_filetype_table[filetype]);
50 int ext3_check_dir_entry (const char * function, struct inode * dir,
51 struct ext3_dir_entry_2 * de,
52 struct buffer_head * bh,
58 + err = ext3_dx_readdir(filp, dirent, filldir);
59 + if (err != ERR_BAD_DX_DIR)
62 + * We don't set the inode dirty flag since it's not
63 + * critical that it get flushed back to the disk.
65 + EXT3_I(filp->f_dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
69 offset = filp->f_pos & (sb->s_blocksize - 1);
71 * during the copy operation.
73 unsigned long version = filp->f_version;
74 - unsigned char d_type = DT_UNKNOWN;
76 - if (EXT3_HAS_INCOMPAT_FEATURE(sb,
77 - EXT3_FEATURE_INCOMPAT_FILETYPE)
78 - && de->file_type < EXT3_FT_MAX)
80 - ext3_filetype_table[de->file_type];
81 error = filldir(dirent, de->name,
84 le32_to_cpu(de->inode),
86 + get_dtype(sb, de->file_type));
89 if (version != filp->f_version)
95 +#ifdef CONFIG_EXT3_INDEX
97 + * These functions convert from the major/minor hash to an f_pos
100 + * Currently we only use major hash numer. This is unfortunate, but
101 + * on 32-bit machines, the same VFS interface is used for lseek and
102 + * llseek, so if we use the 64 bit offset, then the 32-bit versions of
103 + * lseek/telldir/seekdir will blow out spectacularly, and from within
104 + * the ext2 low-level routine, we don't know if we're being called by
105 + * a 64-bit version of the system call or the 32-bit version of the
106 + * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
109 +#define hash2pos(major, minor) (major >> 1)
110 +#define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
111 +#define pos2min_hash(pos) (0)
114 + * This structure holds the nodes of the red-black tree used to store
115 + * the directory entry in hash order.
121 + struct fname *next;
129 + * This functoin implements a non-recursive way of freeing all of the
130 + * nodes in the red-black tree.
132 +static void free_rb_tree_fname(rb_root_t *root)
134 + rb_node_t *n = root->rb_node;
136 + struct fname *fname;
139 + /* Do the node's children first */
140 + if ((n)->rb_left) {
149 + * The node has no children; free it, and then zero
150 + * out parent's link to it. Finally go to the
151 + * beginning of the loop and try to free the parent
154 + parent = n->rb_parent;
155 + fname = rb_entry(n, struct fname, rb_hash);
159 + else if (parent->rb_left == n)
160 + parent->rb_left = 0;
161 + else if (parent->rb_right == n)
162 + parent->rb_right = 0;
169 +struct dir_private_info *create_dir_info(loff_t pos)
171 + struct dir_private_info *p;
173 + p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
176 + p->root.rb_node = 0;
178 + p->extra_fname = 0;
180 + p->curr_hash = pos2maj_hash(pos);
181 + p->curr_minor_hash = pos2min_hash(pos);
186 +void ext3_htree_free_dir_info(struct dir_private_info *p)
188 + free_rb_tree_fname(&p->root);
193 + * Given a directory entry, enter it into the fname rb tree.
195 +int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
197 + struct ext3_dir_entry_2 *dirent)
199 + rb_node_t **p, *parent = NULL;
200 + struct fname * fname, *new_fn;
201 + struct dir_private_info *info;
204 + info = (struct dir_private_info *) dir_file->private_data;
205 + p = &info->root.rb_node;
207 + /* Create and allocate the fname structure */
208 + len = sizeof(struct fname) + dirent->name_len + 1;
209 + new_fn = kmalloc(len, GFP_KERNEL);
212 + memset(new_fn, 0, len);
213 + new_fn->hash = hash;
214 + new_fn->minor_hash = minor_hash;
215 + new_fn->inode = le32_to_cpu(dirent->inode);
216 + new_fn->name_len = dirent->name_len;
217 + new_fn->file_type = dirent->file_type;
218 + memcpy(new_fn->name, dirent->name, dirent->name_len);
219 + new_fn->name[dirent->name_len] = 0;
223 + fname = rb_entry(parent, struct fname, rb_hash);
226 + * If the hash and minor hash match up, then we put
227 + * them on a linked list. This rarely happens...
229 + if ((new_fn->hash == fname->hash) &&
230 + (new_fn->minor_hash == fname->minor_hash)) {
231 + new_fn->next = fname->next;
232 + fname->next = new_fn;
236 + if (new_fn->hash < fname->hash)
237 + p = &(*p)->rb_left;
238 + else if (new_fn->hash > fname->hash)
239 + p = &(*p)->rb_right;
240 + else if (new_fn->minor_hash < fname->minor_hash)
241 + p = &(*p)->rb_left;
242 + else /* if (new_fn->minor_hash > fname->minor_hash) */
243 + p = &(*p)->rb_right;
246 + rb_link_node(&new_fn->rb_hash, parent, p);
247 + rb_insert_color(&new_fn->rb_hash, &info->root);
254 + * This is a helper function for ext3_dx_readdir. It calls filldir
255 + * for all entres on the fname linked list. (Normally there is only
256 + * one entry on the linked list, unless there are 62 bit hash collisions.)
258 +static int call_filldir(struct file * filp, void * dirent,
259 + filldir_t filldir, struct fname *fname)
261 + struct dir_private_info *info = filp->private_data;
263 + struct inode *inode = filp->f_dentry->d_inode;
264 + struct super_block * sb;
270 + printk("call_filldir: called with null fname?!?\n");
273 + curr_pos = hash2pos(fname->hash, fname->minor_hash);
275 + error = filldir(dirent, fname->name,
276 + fname->name_len, curr_pos,
278 + get_dtype(sb, fname->file_type));
280 + filp->f_pos = curr_pos;
281 + info->extra_fname = fname->next;
284 + fname = fname->next;
289 +static int ext3_dx_readdir(struct file * filp,
290 + void * dirent, filldir_t filldir)
292 + struct dir_private_info *info = filp->private_data;
293 + struct inode *inode = filp->f_dentry->d_inode;
294 + struct fname *fname;
298 + info = create_dir_info(filp->f_pos);
301 + filp->private_data = info;
304 + /* Some one has messed with f_pos; reset the world */
305 + if (info->last_pos != filp->f_pos) {
306 + free_rb_tree_fname(&info->root);
307 + info->curr_node = 0;
308 + info->extra_fname = 0;
309 + info->curr_hash = pos2maj_hash(filp->f_pos);
310 + info->curr_minor_hash = pos2min_hash(filp->f_pos);
314 + * If there are any leftover names on the hash collision
315 + * chain, return them first.
317 + if (info->extra_fname &&
318 + call_filldir(filp, dirent, filldir, info->extra_fname))
321 + if (!info->curr_node)
322 + info->curr_node = rb_get_first(&info->root);
326 + * Fill the rbtree if we have no more entries,
327 + * or the inode has changed since we last read in the
330 + if ((!info->curr_node) ||
331 + (filp->f_version != inode->i_version)) {
332 + info->curr_node = 0;
333 + free_rb_tree_fname(&info->root);
334 + filp->f_version = inode->i_version;
335 + ret = ext3_htree_fill_tree(filp, info->curr_hash,
336 + info->curr_minor_hash,
342 + info->curr_node = rb_get_first(&info->root);
345 + fname = rb_entry(info->curr_node, struct fname, rb_hash);
346 + info->curr_hash = fname->hash;
347 + info->curr_minor_hash = fname->minor_hash;
348 + if (call_filldir(filp, dirent, filldir, fname))
351 + info->curr_node = rb_get_next(info->curr_node);
352 + if (!info->curr_node) {
353 + info->curr_hash = info->next_hash;
354 + info->curr_minor_hash = 0;
358 + info->last_pos = filp->f_pos;
359 + UPDATE_ATIME(inode);
363 Index: linux-2.4.21-chaos/fs/ext3/file.c
364 ===================================================================
365 --- linux-2.4.21-chaos.orig/fs/ext3/file.c 2003-12-05 07:55:47.000000000 +0300
366 +++ linux-2.4.21-chaos/fs/ext3/file.c 2003-12-12 16:18:17.000000000 +0300
369 if (filp->f_mode & FMODE_WRITE)
370 ext3_discard_prealloc (inode);
371 + if (is_dx(inode) && filp->private_data)
372 + ext3_htree_free_dir_info(filp->private_data);
377 Index: linux-2.4.21-chaos/fs/ext3/hash.c
378 ===================================================================
379 --- linux-2.4.21-chaos.orig/fs/ext3/hash.c 2003-01-30 13:24:37.000000000 +0300
380 +++ linux-2.4.21-chaos/fs/ext3/hash.c 2003-12-12 16:18:17.000000000 +0300
383 + * linux/fs/ext3/hash.c
385 + * Copyright (C) 2002 by Theodore Ts'o
387 + * This file is released under the GPL v2.
389 + * This file may be redistributed under the terms of the GNU Public
393 +#include <linux/fs.h>
394 +#include <linux/jbd.h>
395 +#include <linux/sched.h>
396 +#include <linux/ext3_fs.h>
398 +#define DELTA 0x9E3779B9
400 +static void TEA_transform(__u32 buf[4], __u32 const in[])
403 + __u32 b0 = buf[0], b1 = buf[1];
404 + __u32 a = in[0], b = in[1], c = in[2], d = in[3];
409 + b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
410 + b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
417 +/* F, G and H are basic MD4 functions: selection, majority, parity */
418 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
419 +#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
420 +#define H(x, y, z) ((x) ^ (y) ^ (z))
423 + * The generic round function. The application is so specific that
424 + * we don't bother protecting all the arguments with parens, as is generally
425 + * good macro practice, in favor of extra legibility.
426 + * Rotation is separate from addition to prevent recomputation
428 +#define ROUND(f, a, b, c, d, x, s) \
429 + (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
431 +#define K2 013240474631UL
432 +#define K3 015666365641UL
435 + * Basic cut-down MD4 transform. Returns only 32 bits of result.
437 +static void halfMD4Transform (__u32 buf[4], __u32 const in[])
439 + __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
442 + ROUND(F, a, b, c, d, in[0] + K1, 3);
443 + ROUND(F, d, a, b, c, in[1] + K1, 7);
444 + ROUND(F, c, d, a, b, in[2] + K1, 11);
445 + ROUND(F, b, c, d, a, in[3] + K1, 19);
446 + ROUND(F, a, b, c, d, in[4] + K1, 3);
447 + ROUND(F, d, a, b, c, in[5] + K1, 7);
448 + ROUND(F, c, d, a, b, in[6] + K1, 11);
449 + ROUND(F, b, c, d, a, in[7] + K1, 19);
452 + ROUND(G, a, b, c, d, in[1] + K2, 3);
453 + ROUND(G, d, a, b, c, in[3] + K2, 5);
454 + ROUND(G, c, d, a, b, in[5] + K2, 9);
455 + ROUND(G, b, c, d, a, in[7] + K2, 13);
456 + ROUND(G, a, b, c, d, in[0] + K2, 3);
457 + ROUND(G, d, a, b, c, in[2] + K2, 5);
458 + ROUND(G, c, d, a, b, in[4] + K2, 9);
459 + ROUND(G, b, c, d, a, in[6] + K2, 13);
462 + ROUND(H, a, b, c, d, in[3] + K3, 3);
463 + ROUND(H, d, a, b, c, in[7] + K3, 9);
464 + ROUND(H, c, d, a, b, in[2] + K3, 11);
465 + ROUND(H, b, c, d, a, in[6] + K3, 15);
466 + ROUND(H, a, b, c, d, in[1] + K3, 3);
467 + ROUND(H, d, a, b, c, in[5] + K3, 9);
468 + ROUND(H, c, d, a, b, in[0] + K3, 11);
469 + ROUND(H, b, c, d, a, in[4] + K3, 15);
485 +/* The old legacy hash */
486 +static __u32 dx_hack_hash (const char *name, int len)
488 + __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
490 + __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
492 + if (hash & 0x80000000) hash -= 0x7fffffff;
496 + return (hash0 << 1);
499 +static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
504 + pad = (__u32)len | ((__u32)len << 8);
510 + for (i=0; i < len; i++) {
513 + val = msg[i] + (val << 8);
514 + if ((i % 4) == 3) {
527 + * Returns the hash of a filename. If len is 0 and name is NULL, then
528 + * this function can be used to test whether or not a hash version is
531 + * The seed is an 4 longword (32 bits) "secret" which can be used to
532 + * uniquify a hash. If the seed is all zero's, then some default seed
535 + * A particular hash version specifies whether or not the seed is
536 + * represented, and whether or not the returned hash is 32 bits or 64
537 + * bits. 32 bit hashes will return 0 for the minor hash.
539 +int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
542 + __u32 minor_hash = 0;
545 + __u32 in[8], buf[4];
547 + /* Initialize the default seed for the hash checksum functions */
548 + buf[0] = 0x67452301;
549 + buf[1] = 0xefcdab89;
550 + buf[2] = 0x98badcfe;
551 + buf[3] = 0x10325476;
553 + /* Check to see if the seed is all zero's */
555 + for (i=0; i < 4; i++) {
556 + if (hinfo->seed[i])
560 + memcpy(buf, hinfo->seed, sizeof(buf));
563 + switch (hinfo->hash_version) {
564 + case DX_HASH_LEGACY:
565 + hash = dx_hack_hash(name, len);
567 + case DX_HASH_HALF_MD4:
570 + str2hashbuf(p, len, in, 8);
571 + halfMD4Transform(buf, in);
575 + minor_hash = buf[2];
581 + str2hashbuf(p, len, in, 4);
582 + TEA_transform(buf, in);
587 + minor_hash = buf[1];
593 + hinfo->hash = hash & ~1;
594 + hinfo->minor_hash = minor_hash;
597 Index: linux-2.4.21-chaos/fs/ext3/Makefile
598 ===================================================================
599 --- linux-2.4.21-chaos.orig/fs/ext3/Makefile 2003-12-12 16:17:59.000000000 +0300
600 +++ linux-2.4.21-chaos/fs/ext3/Makefile 2003-12-12 16:18:17.000000000 +0300
602 export-objs := super.o inode.o
604 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
605 - ioctl.o namei.o super.o symlink.o
606 + ioctl.o namei.o super.o symlink.o hash.o
609 export-objs += xattr.o
610 Index: linux-2.4.21-chaos/fs/ext3/namei.c
611 ===================================================================
612 --- linux-2.4.21-chaos.orig/fs/ext3/namei.c 2003-07-15 04:41:01.000000000 +0400
613 +++ linux-2.4.21-chaos/fs/ext3/namei.c 2003-12-12 16:18:17.000000000 +0300
615 * David S. Miller (davem@caip.rutgers.edu), 1995
616 * Directory entry file type support and forward compatibility hooks
617 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
618 + * Hash Tree Directory indexing (c)
619 + * Daniel Phillips, 2001
620 + * Hash Tree Directory indexing porting
621 + * Christopher Li, 2002
622 + * Hash Tree Directory indexing cleanup
623 + * Theodore Ts'o, 2002
626 #include <linux/fs.h>
628 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
629 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
631 +static struct buffer_head *ext3_append(handle_t *handle,
632 + struct inode *inode,
633 + u32 *block, int *err)
635 + struct buffer_head *bh;
637 + *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
639 + if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
640 + inode->i_size += inode->i_sb->s_blocksize;
641 + EXT3_I(inode)->i_disksize = inode->i_size;
642 + ext3_journal_get_write_access(handle,bh);
648 +#define assert(test) J_ASSERT(test)
652 +#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
655 +typedef struct { u32 v; } le_u32;
656 +typedef struct { u16 v; } le_u16;
659 +#define dxtrace(command) command
661 +#define dxtrace(command)
672 +struct dx_countlimit
685 + * dx_root_info is laid out so that if it should somehow get overlaid by a
686 + * dirent the two low bits of the hash version will be zero. Therefore, the
687 + * hash version mod 4 should never be 0. Sincerely, the paranoia department.
692 + struct fake_dirent dot;
694 + struct fake_dirent dotdot;
695 + char dotdot_name[4];
696 + struct dx_root_info
698 + le_u32 reserved_zero;
700 + u8 info_length; /* 8 */
701 + u8 indirect_levels;
705 + struct dx_entry entries[0];
710 + struct fake_dirent fake;
711 + struct dx_entry entries[0];
717 + struct buffer_head *bh;
718 + struct dx_entry *entries;
719 + struct dx_entry *at;
728 +#ifdef CONFIG_EXT3_INDEX
729 +static inline unsigned dx_get_block (struct dx_entry *entry);
730 +static void dx_set_block (struct dx_entry *entry, unsigned value);
731 +static inline unsigned dx_get_hash (struct dx_entry *entry);
732 +static void dx_set_hash (struct dx_entry *entry, unsigned value);
733 +static unsigned dx_get_count (struct dx_entry *entries);
734 +static unsigned dx_get_limit (struct dx_entry *entries);
735 +static void dx_set_count (struct dx_entry *entries, unsigned value);
736 +static void dx_set_limit (struct dx_entry *entries, unsigned value);
737 +static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
738 +static unsigned dx_node_limit (struct inode *dir);
739 +static struct dx_frame *dx_probe(struct dentry *dentry,
741 + struct dx_hash_info *hinfo,
742 + struct dx_frame *frame,
744 +static void dx_release (struct dx_frame *frames);
745 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
746 + struct dx_hash_info *hinfo, struct dx_map_entry map[]);
747 +static void dx_sort_map(struct dx_map_entry *map, unsigned count);
748 +static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
749 + struct dx_map_entry *offsets, int count);
750 +static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
751 +static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
752 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
753 + struct dx_frame *frame,
754 + struct dx_frame *frames, int *err,
755 + __u32 *start_hash);
756 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
757 + struct ext3_dir_entry_2 **res_dir, int *err);
758 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
759 + struct inode *inode);
762 + * Future: use high four bits of block for coalesce-on-delete flags
763 + * Mask them off for now.
766 +static inline unsigned dx_get_block (struct dx_entry *entry)
768 + return le32_to_cpu(entry->block.v) & 0x00ffffff;
771 +static inline void dx_set_block (struct dx_entry *entry, unsigned value)
773 + entry->block.v = cpu_to_le32(value);
776 +static inline unsigned dx_get_hash (struct dx_entry *entry)
778 + return le32_to_cpu(entry->hash.v);
781 +static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
783 + entry->hash.v = cpu_to_le32(value);
786 +static inline unsigned dx_get_count (struct dx_entry *entries)
788 + return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
791 +static inline unsigned dx_get_limit (struct dx_entry *entries)
793 + return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
796 +static inline void dx_set_count (struct dx_entry *entries, unsigned value)
798 + ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
801 +static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
803 + ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
806 +static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
808 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
809 + EXT3_DIR_REC_LEN(2) - infosize;
810 + return 0? 20: entry_space / sizeof(struct dx_entry);
813 +static inline unsigned dx_node_limit (struct inode *dir)
815 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
816 + return 0? 22: entry_space / sizeof(struct dx_entry);
830 +static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
831 + int size, int show_names)
833 + unsigned names = 0, space = 0;
834 + char *base = (char *) de;
835 + struct dx_hash_info h = *hinfo;
838 + while ((char *) de < base + size)
844 + int len = de->name_len;
845 + char *name = de->name;
846 + while (len--) printk("%c", *name++);
847 + ext3fs_dirhash(de->name, de->name_len, &h);
848 + printk(":%x.%u ", h.hash,
849 + ((char *) de - base));
851 + space += EXT3_DIR_REC_LEN(de->name_len);
854 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
856 + printk("(%i)\n", names);
857 + return (struct stats) { names, space, 1 };
860 +struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
861 + struct dx_entry *entries, int levels)
863 + unsigned blocksize = dir->i_sb->s_blocksize;
864 + unsigned count = dx_get_count (entries), names = 0, space = 0, i;
865 + unsigned bcount = 0;
866 + struct buffer_head *bh;
868 + printk("%i indexed blocks...\n", count);
869 + for (i = 0; i < count; i++, entries++)
871 + u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
872 + u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
873 + struct stats stats;
874 + printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
875 + if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
877 + dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
878 + dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
879 + names += stats.names;
880 + space += stats.space;
881 + bcount += stats.bcount;
885 + printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
886 + names, space/bcount,(space/bcount)*100/blocksize);
887 + return (struct stats) { names, space, bcount};
889 +#endif /* DX_DEBUG */
892 + * Probe for a directory leaf block to search.
894 + * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
895 + * error in the directory index, and the caller should fall back to
896 + * searching the directory normally. The callers of dx_probe **MUST**
897 + * check for this error code, and make sure it never gets reflected
898 + * back to userspace.
900 +static struct dx_frame *
901 +dx_probe(struct dentry *dentry, struct inode *dir,
902 + struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
904 + unsigned count, indirect;
905 + struct dx_entry *at, *entries, *p, *q, *m;
906 + struct dx_root *root;
907 + struct buffer_head *bh;
908 + struct dx_frame *frame = frame_in;
913 + dir = dentry->d_parent->d_inode;
914 + if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
916 + root = (struct dx_root *) bh->b_data;
917 + if (root->info.hash_version != DX_HASH_TEA &&
918 + root->info.hash_version != DX_HASH_HALF_MD4 &&
919 + root->info.hash_version != DX_HASH_LEGACY) {
920 + ext3_warning(dir->i_sb, __FUNCTION__,
921 + "Unrecognised inode hash code %d",
922 + root->info.hash_version);
924 + *err = ERR_BAD_DX_DIR;
927 + hinfo->hash_version = root->info.hash_version;
928 + hinfo->seed = dir->i_sb->u.ext3_sb.s_hash_seed;
930 + ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
931 + hash = hinfo->hash;
933 + if (root->info.unused_flags & 1) {
934 + ext3_warning(dir->i_sb, __FUNCTION__,
935 + "Unimplemented inode hash flags: %#06x",
936 + root->info.unused_flags);
938 + *err = ERR_BAD_DX_DIR;
942 + if ((indirect = root->info.indirect_levels) > 1) {
943 + ext3_warning(dir->i_sb, __FUNCTION__,
944 + "Unimplemented inode hash depth: %#06x",
945 + root->info.indirect_levels);
947 + *err = ERR_BAD_DX_DIR;
951 + entries = (struct dx_entry *) (((char *)&root->info) +
952 + root->info.info_length);
953 + assert(dx_get_limit(entries) == dx_root_limit(dir,
954 + root->info.info_length));
955 + dxtrace (printk("Look up %x", hash));
958 + count = dx_get_count(entries);
959 + assert (count && count <= dx_get_limit(entries));
961 + q = entries + count - 1;
965 + dxtrace(printk("."));
966 + if (dx_get_hash(m) > hash)
972 + if (0) // linear search cross check
974 + unsigned n = count - 1;
978 + dxtrace(printk(","));
979 + if (dx_get_hash(++at) > hash)
985 + assert (at == p - 1);
989 + dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
991 + frame->entries = entries;
993 + if (!indirect--) return frame;
994 + if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
996 + at = entries = ((struct dx_node *) bh->b_data)->entries;
997 + assert (dx_get_limit(entries) == dx_node_limit (dir));
1001 + while (frame >= frame_in) {
1002 + brelse(frame->bh);
1009 +static void dx_release (struct dx_frame *frames)
1011 + if (frames[0].bh == NULL)
1014 + if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
1015 + brelse(frames[1].bh);
1016 + brelse(frames[0].bh);
1020 + * This function increments the frame pointer to search the next leaf
1021 + * block, and reads in the necessary intervening nodes if the search
1022 + * should be necessary. Whether or not the search is necessary is
1023 + * controlled by the hash parameter. If the hash value is even, then
1024 + * the search is only continued if the next block starts with that
1025 + * hash value. This is used if we are searching for a specific file.
1027 + * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1029 + * This function returns 1 if the caller should continue to search,
1030 + * or 0 if it should not. If there is an error reading one of the
1031 + * index blocks, it will return -1.
1033 + * If start_hash is non-null, it will be filled in with the starting
1034 + * hash of the next page.
1036 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
1037 + struct dx_frame *frame,
1038 + struct dx_frame *frames, int *err,
1039 + __u32 *start_hash)
1041 + struct dx_frame *p;
1042 + struct buffer_head *bh;
1043 + int num_frames = 0;
1049 + * Find the next leaf page by incrementing the frame pointer.
1050 + * If we run out of entries in the interior node, loop around and
1051 + * increment pointer in the parent node. When we break out of
1052 + * this loop, num_frames indicates the number of interior
1053 + * nodes need to be read.
1056 + if (++(p->at) < p->entries + dx_get_count(p->entries))
1065 + * If the hash is 1, then continue only if the next page has a
1066 + * continuation hash of any value. This is used for readdir
1067 + * handling. Otherwise, check to see if the hash matches the
1068 + * desired contiuation hash. If it doesn't, return since
1069 + * there's no point to read in the successive index pages.
1071 + bhash = dx_get_hash(p->at);
1073 + *start_hash = bhash;
1074 + if ((hash & 1) == 0) {
1075 + if ((bhash & ~1) != hash)
1079 + * If the hash is HASH_NB_ALWAYS, we always go to the next
1080 + * block so no check is necessary
1082 + while (num_frames--) {
1083 + if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
1085 + return -1; /* Failure */
1089 + p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1096 + * p is at least 6 bytes before the end of page
1098 +static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
1100 + return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
1104 + * This function fills a red-black tree with information from a
1105 + * directory. We start scanning the directory in hash order, starting
1106 + * at start_hash and start_minor_hash.
1108 + * This function returns the number of entries inserted into the tree,
1109 + * or a negative error code.
1111 +int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1112 + __u32 start_minor_hash, __u32 *next_hash)
1114 + struct dx_hash_info hinfo;
1115 + struct buffer_head *bh;
1116 + struct ext3_dir_entry_2 *de, *top;
1117 + static struct dx_frame frames[2], *frame;
1118 + struct inode *dir;
1124 + dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
1125 + start_minor_hash));
1126 + dir = dir_file->f_dentry->d_inode;
1127 + hinfo.hash = start_hash;
1128 + hinfo.minor_hash = 0;
1129 + frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
1133 + /* Add '.' and '..' from the htree header */
1134 + if (!start_hash && !start_minor_hash) {
1135 + de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
1136 + if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1138 + de = ext3_next_entry(de);
1139 + if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1145 + block = dx_get_block(frame->at);
1146 + dxtrace(printk("Reading block %d\n", block));
1147 + if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
1150 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1151 + top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
1152 + EXT3_DIR_REC_LEN(0));
1153 + for (; de < top; de = ext3_next_entry(de)) {
1154 + ext3fs_dirhash(de->name, de->name_len, &hinfo);
1155 + if ((hinfo.hash < start_hash) ||
1156 + ((hinfo.hash == start_hash) &&
1157 + (hinfo.minor_hash < start_minor_hash)))
1159 + if ((err = ext3_htree_store_dirent(dir_file,
1160 + hinfo.hash, hinfo.minor_hash, de)) != 0)
1166 + ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
1167 + frame, frames, &err, &hashval);
1169 + *next_hash = hashval;
1173 + * Stop if: (a) there are no more entries, or
1174 + * (b) we have inserted at least one entry and the
1175 + * next hash value is not a continuation
1178 + (count && ((hashval & 1) == 0)))
1181 + dx_release(frames);
1182 + dxtrace(printk("Fill tree: returned %d entries\n", count));
1185 + dx_release(frames);
1191 + * Directory block splitting, compacting
1194 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
1195 + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
1198 + char *base = (char *) de;
1199 + struct dx_hash_info h = *hinfo;
1201 + while ((char *) de < base + size)
1203 + if (de->name_len && de->inode) {
1204 + ext3fs_dirhash(de->name, de->name_len, &h);
1206 + map_tail->hash = h.hash;
1207 + map_tail->offs = (u32) ((char *) de - base);
1210 + /* XXX: do we need to check rec_len == 0 case? -Chris */
1211 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
1216 +static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1218 + struct dx_map_entry *p, *q, *top = map + count - 1;
1220 + /* Combsort until bubble sort doesn't suck */
1223 + count = count*10/13;
1224 + if (count - 9 < 2) /* 9, 10 -> 11 */
1226 + for (p = top, q = p - count; q >= map; p--, q--)
1227 + if (p->hash < q->hash)
1230 + /* Garden variety bubble sort */
1236 + if (q[1].hash >= q[0].hash)
1244 +static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
1246 + struct dx_entry *entries = frame->entries;
1247 + struct dx_entry *old = frame->at, *new = old + 1;
1248 + int count = dx_get_count(entries);
1250 + assert(count < dx_get_limit(entries));
1251 + assert(old < entries + count);
1252 + memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1253 + dx_set_hash(new, hash);
1254 + dx_set_block(new, block);
1255 + dx_set_count(entries, count + 1);
1260 +static void ext3_update_dx_flag(struct inode *inode)
1262 + if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
1263 + EXT3_FEATURE_COMPAT_DIR_INDEX))
1264 + EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
1268 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
1279 * The returned buffer_head has ->b_count elevated. The caller is expected
1280 * to brelse() it when appropriate.
1284 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
1285 struct ext3_dir_entry_2 ** res_dir)
1287 @@ -121,12 +766,32 @@
1289 int nblocks, i, err;
1290 struct inode *dir = dentry->d_parent->d_inode;
1293 + unsigned blocksize;
1298 + blocksize = sb->s_blocksize;
1299 + namelen = dentry->d_name.len;
1300 + name = dentry->d_name.name;
1301 + if (namelen > EXT3_NAME_LEN)
1303 +#ifdef CONFIG_EXT3_INDEX
1305 + bh = ext3_dx_find_entry(dentry, res_dir, &err);
1307 + * On success, or if the error was file not found,
1308 + * return. Otherwise, fall back to doing a search the
1309 + * old fashioned way.
1311 + if (bh || (err != ERR_BAD_DX_DIR))
1313 + dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
1316 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
1317 - start = dir->u.ext3_i.i_dir_start_lookup;
1318 + start = EXT3_I(dir)->i_dir_start_lookup;
1319 if (start >= nblocks)
1323 i = search_dirblock(bh, dir, dentry,
1324 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
1326 - dir->u.ext3_i.i_dir_start_lookup = block;
1327 + EXT3_I(dir)->i_dir_start_lookup = block;
1329 goto cleanup_and_exit;
1331 @@ -198,6 +863,66 @@
1335 +#ifdef CONFIG_EXT3_INDEX
1336 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
1337 + struct ext3_dir_entry_2 **res_dir, int *err)
1339 + struct super_block * sb;
1340 + struct dx_hash_info hinfo;
1342 + struct dx_frame frames[2], *frame;
1343 + struct ext3_dir_entry_2 *de, *top;
1344 + struct buffer_head *bh;
1345 + unsigned long block;
1347 + int namelen = dentry->d_name.len;
1348 + const u8 *name = dentry->d_name.name;
1349 + struct inode *dir = dentry->d_parent->d_inode;
1352 + if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
1354 + hash = hinfo.hash;
1356 + block = dx_get_block(frame->at);
1357 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1359 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1360 + top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
1361 + EXT3_DIR_REC_LEN(0));
1362 + for (; de < top; de = ext3_next_entry(de))
1363 + if (ext3_match (namelen, name, de)) {
1364 + if (!ext3_check_dir_entry("ext3_find_entry",
1366 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1367 + +((char *)de - bh->b_data))) {
1372 + dx_release (frames);
1376 + /* Check to see if we should continue to search */
1377 + retval = ext3_htree_next_block(dir, hash, frame,
1379 + if (retval == -1) {
1380 + ext3_warning(sb, __FUNCTION__,
1381 + "error reading index page in directory #%lu",
1385 + } while (retval == 1);
1389 + dxtrace(printk("%s not found\n", name));
1390 + dx_release (frames);
1395 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1397 struct inode * inode;
1400 inode = iget(dir->i_sb, ino);
1404 return ERR_PTR(-EACCES);
1407 d_add(dentry, inode);
1409 @@ -239,6 +965,301 @@
1410 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1413 +#ifdef CONFIG_EXT3_INDEX
1414 +static struct ext3_dir_entry_2 *
1415 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1417 + unsigned rec_len = 0;
1420 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1421 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1422 + memcpy (to, de, rec_len);
1423 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1428 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1431 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1433 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1434 + unsigned rec_len = 0;
1437 + while ((char*)de < base + size) {
1438 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1439 + le16_to_cpu(de->rec_len));
1440 + if (de->inode && de->name_len) {
1441 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1443 + memmove(to, de, rec_len);
1444 + to->rec_len = cpu_to_le16(rec_len);
1446 + to = (struct ext3_dir_entry_2 *)((char *) to + rec_len);
1453 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1454 + struct buffer_head **bh,struct dx_frame *frame,
1455 + struct dx_hash_info *hinfo, int *error)
1457 + unsigned blocksize = dir->i_sb->s_blocksize;
1458 + unsigned count, continued;
1459 + struct buffer_head *bh2;
1462 + struct dx_map_entry *map;
1463 + char *data1 = (*bh)->b_data, *data2;
1465 + struct ext3_dir_entry_2 *de = NULL, *de2;
1468 + bh2 = ext3_append (handle, dir, &newblock, error);
1475 + BUFFER_TRACE(*bh, "get_write_access");
1476 + err = ext3_journal_get_write_access(handle, *bh);
1482 + ext3_std_error(dir->i_sb, err);
1485 + BUFFER_TRACE(frame->bh, "get_write_access");
1486 + err = ext3_journal_get_write_access(handle, frame->bh);
1488 + goto journal_error;
1490 + data2 = bh2->b_data;
1492 + /* create map in the end of data2 block */
1493 + map = (struct dx_map_entry *) (data2 + blocksize);
1494 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1495 + blocksize, hinfo, map);
1497 + split = count/2; // need to adjust to actual middle
1498 + dx_sort_map (map, count);
1499 + hash2 = map[split].hash;
1500 + continued = hash2 == map[split - 1].hash;
1501 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1502 + dx_get_block(frame->at), hash2, split, count-split));
1504 + /* Fancy dance to stay within two buffers */
1505 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1506 + de = dx_pack_dirents(data1,blocksize);
1507 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1508 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1509 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1510 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1512 + /* Which block gets the new entry? */
1513 + if (hinfo->hash >= hash2)
1518 + dx_insert_block (frame, hash2 + continued, newblock);
1519 + err = ext3_journal_dirty_metadata (handle, bh2);
1521 + goto journal_error;
1522 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1524 + goto journal_error;
1526 + dxtrace(dx_show_index ("frame", frame->entries));
1534 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1535 + * it points to a directory entry which is guaranteed to be large
1536 + * enough for new directory entry. If de is NULL, then
1537 + * add_dirent_to_buf will attempt search the directory block for
1538 + * space. It will return -ENOSPC if no space is available, and -EIO
1539 + * and -EEXIST if directory entry already exists.
1541 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1542 + * all other cases bh is released.
1544 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1545 + struct inode *inode, struct ext3_dir_entry_2 *de,
1546 + struct buffer_head * bh)
1548 + struct inode *dir = dentry->d_parent->d_inode;
1549 + const char *name = dentry->d_name.name;
1550 + int namelen = dentry->d_name.len;
1551 + unsigned long offset = 0;
1552 + unsigned short reclen;
1553 + int nlen, rlen, err;
1556 + reclen = EXT3_DIR_REC_LEN(namelen);
1558 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1559 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1560 + while ((char *) de <= top) {
1561 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1566 + if (ext3_match (namelen, name, de)) {
1570 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1571 + rlen = le16_to_cpu(de->rec_len);
1572 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1574 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1577 + if ((char *) de > top)
1580 + BUFFER_TRACE(bh, "get_write_access");
1581 + err = ext3_journal_get_write_access(handle, bh);
1583 + ext3_std_error(dir->i_sb, err);
1588 + /* By now the buffer is marked for journaling */
1589 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1590 + rlen = le16_to_cpu(de->rec_len);
1592 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1593 + de1->rec_len = cpu_to_le16(rlen - nlen);
1594 + de->rec_len = cpu_to_le16(nlen);
1597 + de->file_type = EXT3_FT_UNKNOWN;
1599 + de->inode = cpu_to_le32(inode->i_ino);
1600 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1603 + de->name_len = namelen;
1604 + memcpy (de->name, name, namelen);
1606 + * XXX shouldn't update any times until successful
1607 + * completion of syscall, but too many callers depend
1610 + * XXX similarly, too many callers depend on
1611 + * ext3_new_inode() setting the times, but error
1612 + * recovery deletes the inode, so the worst that can
1613 + * happen is that the times are slightly out of date
1614 + * and/or different from the directory change time.
1616 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1617 + ext3_update_dx_flag(dir);
1618 + dir->i_version = ++event;
1619 + ext3_mark_inode_dirty(handle, dir);
1620 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1621 + err = ext3_journal_dirty_metadata(handle, bh);
1623 + ext3_std_error(dir->i_sb, err);
1628 +#ifdef CONFIG_EXT3_INDEX
1630 + * This converts a one block unindexed directory to a 3 block indexed
1631 + * directory, and adds the dentry to the indexed directory.
1633 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1634 + struct inode *inode, struct buffer_head *bh)
1636 + struct inode *dir = dentry->d_parent->d_inode;
1637 + const char *name = dentry->d_name.name;
1638 + int namelen = dentry->d_name.len;
1639 + struct buffer_head *bh2;
1640 + struct dx_root *root;
1641 + struct dx_frame frames[2], *frame;
1642 + struct dx_entry *entries;
1643 + struct ext3_dir_entry_2 *de, *de2;
1644 + char *data1, *top;
1647 + unsigned blocksize;
1648 + struct dx_hash_info hinfo;
1651 + blocksize = dir->i_sb->s_blocksize;
1652 + dxtrace(printk("Creating index\n"));
1653 + retval = ext3_journal_get_write_access(handle, bh);
1655 + ext3_std_error(dir->i_sb, retval);
1659 + root = (struct dx_root *) bh->b_data;
1661 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1662 + bh2 = ext3_append (handle, dir, &block, &retval);
1667 + data1 = bh2->b_data;
1669 + /* The 0th block becomes the root, move the dirents out */
1670 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1671 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1672 + len = ((char *) root) + blocksize - (char *) de;
1673 + memcpy (data1, de, len);
1674 + de = (struct ext3_dir_entry_2 *) data1;
1675 + top = data1 + len;
1676 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1678 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1679 + /* Initialize the root; the dot dirents already exist */
1680 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1681 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1682 + memset (&root->info, 0, sizeof(root->info));
1683 + root->info.info_length = sizeof(root->info);
1684 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1685 + entries = root->entries;
1686 + dx_set_block (entries, 1);
1687 + dx_set_count (entries, 1);
1688 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1690 + /* Initialize as for dx_probe */
1691 + hinfo.hash_version = root->info.hash_version;
1692 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1693 + ext3fs_dirhash(name, namelen, &hinfo);
1695 + frame->entries = entries;
1696 + frame->at = entries;
1699 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1700 + dx_release (frames);
1704 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1711 @@ -249,127 +1270,198 @@
1712 * may not sleep between calling this and putting something into
1713 * the entry, as someone else might have used it while you slept.
1717 - * AKPM: the journalling code here looks wrong on the error paths
1719 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1720 struct inode *inode)
1722 struct inode *dir = dentry->d_parent->d_inode;
1723 - const char *name = dentry->d_name.name;
1724 - int namelen = dentry->d_name.len;
1725 unsigned long offset;
1726 - unsigned short rec_len;
1727 struct buffer_head * bh;
1728 - struct ext3_dir_entry_2 * de, * de1;
1729 + struct ext3_dir_entry_2 *de;
1730 struct super_block * sb;
1732 +#ifdef CONFIG_EXT3_INDEX
1733 + int dx_fallback=0;
1735 + unsigned blocksize;
1736 + unsigned nlen, rlen;
1737 + u32 block, blocks;
1742 + blocksize = sb->s_blocksize;
1743 + if (!dentry->d_name.len)
1745 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1746 +#ifdef CONFIG_EXT3_INDEX
1748 + retval = ext3_dx_add_entry(handle, dentry, inode);
1749 + if (!retval || (retval != ERR_BAD_DX_DIR))
1751 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1753 + ext3_mark_inode_dirty(handle, dir);
1756 + blocks = dir->i_size >> sb->s_blocksize_bits;
1757 + for (block = 0, offset = 0; block < blocks; block++) {
1758 + bh = ext3_bread(handle, dir, block, 0, &retval);
1761 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1762 + if (retval != -ENOSPC)
1765 +#ifdef CONFIG_EXT3_INDEX
1766 + if (blocks == 1 && !dx_fallback &&
1767 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1768 + return make_indexed_dir(handle, dentry, inode, bh);
1772 + bh = ext3_append(handle, dir, &block, &retval);
1775 - rec_len = EXT3_DIR_REC_LEN(namelen);
1777 de = (struct ext3_dir_entry_2 *) bh->b_data;
1779 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1782 - bh = ext3_bread (handle, dir,
1783 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1786 - if (dir->i_size <= offset) {
1787 - if (dir->i_size == 0) {
1792 + de->rec_len = cpu_to_le16(rlen = blocksize);
1794 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1797 - ext3_debug ("creating next block\n");
1798 +#ifdef CONFIG_EXT3_INDEX
1800 + * Returns 0 for success, or a negative error value
1802 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1803 + struct inode *inode)
1805 + struct dx_frame frames[2], *frame;
1806 + struct dx_entry *entries, *at;
1807 + struct dx_hash_info hinfo;
1808 + struct buffer_head * bh;
1809 + struct inode *dir = dentry->d_parent->d_inode;
1810 + struct super_block * sb = dir->i_sb;
1811 + struct ext3_dir_entry_2 *de;
1814 - BUFFER_TRACE(bh, "get_write_access");
1815 - ext3_journal_get_write_access(handle, bh);
1816 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1818 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1819 - dir->u.ext3_i.i_disksize =
1820 - dir->i_size = offset + sb->s_blocksize;
1821 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1822 - ext3_mark_inode_dirty(handle, dir);
1824 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1827 + entries = frame->entries;
1830 - ext3_debug ("skipping to next block\n");
1831 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1834 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1837 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1842 - if (ext3_match (namelen, name, de)) {
1845 + BUFFER_TRACE(bh, "get_write_access");
1846 + err = ext3_journal_get_write_access(handle, bh);
1848 + goto journal_error;
1850 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1851 + if (err != -ENOSPC) {
1856 + /* Block full, should compress but for now just split */
1857 + dxtrace(printk("using %u of %u node entries\n",
1858 + dx_get_count(entries), dx_get_limit(entries)));
1859 + /* Need to split index? */
1860 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1862 + unsigned icount = dx_get_count(entries);
1863 + int levels = frame - frames;
1864 + struct dx_entry *entries2;
1865 + struct dx_node *node2;
1866 + struct buffer_head *bh2;
1868 + if (levels && (dx_get_count(frames->entries) ==
1869 + dx_get_limit(frames->entries))) {
1870 + ext3_warning(sb, __FUNCTION__,
1871 + "Directory index full!\n");
1875 - if ((le32_to_cpu(de->inode) == 0 &&
1876 - le16_to_cpu(de->rec_len) >= rec_len) ||
1877 - (le16_to_cpu(de->rec_len) >=
1878 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1879 - BUFFER_TRACE(bh, "get_write_access");
1880 - ext3_journal_get_write_access(handle, bh);
1881 - /* By now the buffer is marked for journaling */
1882 - offset += le16_to_cpu(de->rec_len);
1883 - if (le32_to_cpu(de->inode)) {
1884 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1885 - EXT3_DIR_REC_LEN(de->name_len));
1887 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1888 - EXT3_DIR_REC_LEN(de->name_len));
1889 - de->rec_len = cpu_to_le16(
1890 - EXT3_DIR_REC_LEN(de->name_len));
1892 + bh2 = ext3_append (handle, dir, &newblock, &err);
1895 + node2 = (struct dx_node *)(bh2->b_data);
1896 + entries2 = node2->entries;
1897 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1898 + node2->fake.inode = 0;
1899 + BUFFER_TRACE(frame->bh, "get_write_access");
1900 + err = ext3_journal_get_write_access(handle, frame->bh);
1902 + goto journal_error;
1904 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1905 + unsigned hash2 = dx_get_hash(entries + icount1);
1906 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1908 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1909 + err = ext3_journal_get_write_access(handle,
1912 + goto journal_error;
1914 + memcpy ((char *) entries2, (char *) (entries + icount1),
1915 + icount2 * sizeof(struct dx_entry));
1916 + dx_set_count (entries, icount1);
1917 + dx_set_count (entries2, icount2);
1918 + dx_set_limit (entries2, dx_node_limit(dir));
1920 + /* Which index block gets the new entry? */
1921 + if (at - entries >= icount1) {
1922 + frame->at = at = at - entries - icount1 + entries2;
1923 + frame->entries = entries = entries2;
1924 + swap(frame->bh, bh2);
1926 - de->file_type = EXT3_FT_UNKNOWN;
1928 - de->inode = cpu_to_le32(inode->i_ino);
1929 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1932 - de->name_len = namelen;
1933 - memcpy (de->name, name, namelen);
1935 - * XXX shouldn't update any times until successful
1936 - * completion of syscall, but too many callers depend
1939 - * XXX similarly, too many callers depend on
1940 - * ext3_new_inode() setting the times, but error
1941 - * recovery deletes the inode, so the worst that can
1942 - * happen is that the times are slightly out of date
1943 - * and/or different from the directory change time.
1945 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1946 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1947 - dir->i_version = ++event;
1948 - ext3_mark_inode_dirty(handle, dir);
1949 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1950 - ext3_journal_dirty_metadata(handle, bh);
1953 + dx_insert_block (frames + 0, hash2, newblock);
1954 + dxtrace(dx_show_index ("node", frames[1].entries));
1955 + dxtrace(dx_show_index ("node",
1956 + ((struct dx_node *) bh2->b_data)->entries));
1957 + err = ext3_journal_dirty_metadata(handle, bh2);
1959 + goto journal_error;
1962 + dxtrace(printk("Creating second level index...\n"));
1963 + memcpy((char *) entries2, (char *) entries,
1964 + icount * sizeof(struct dx_entry));
1965 + dx_set_limit(entries2, dx_node_limit(dir));
1968 + dx_set_count(entries, 1);
1969 + dx_set_block(entries + 0, newblock);
1970 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1972 + /* Add new access path frame */
1973 + frame = frames + 1;
1974 + frame->at = at = at - entries + entries2;
1975 + frame->entries = entries = entries2;
1977 + err = ext3_journal_get_write_access(handle,
1980 + goto journal_error;
1982 - offset += le16_to_cpu(de->rec_len);
1983 - de = (struct ext3_dir_entry_2 *)
1984 - ((char *) de + le16_to_cpu(de->rec_len));
1985 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1989 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1992 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1997 + ext3_std_error(dir->i_sb, err);
2001 + dx_release(frames);
2007 * ext3_delete_entry deletes a directory entry by merging it with the
2008 @@ -456,9 +1548,11 @@
2009 struct inode * inode;
2012 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2013 - if (IS_ERR(handle))
2014 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2015 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2016 + if (IS_ERR(handle)) {
2017 return PTR_ERR(handle);
2022 @@ -482,9 +1576,11 @@
2023 struct inode *inode;
2026 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2027 - if (IS_ERR(handle))
2028 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2029 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2030 + if (IS_ERR(handle)) {
2031 return PTR_ERR(handle);
2036 @@ -513,9 +1609,11 @@
2037 if (dir->i_nlink >= EXT3_LINK_MAX)
2040 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2041 - if (IS_ERR(handle))
2042 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2043 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2044 + if (IS_ERR(handle)) {
2045 return PTR_ERR(handle);
2050 @@ -527,7 +1625,7 @@
2052 inode->i_op = &ext3_dir_inode_operations;
2053 inode->i_fop = &ext3_dir_operations;
2054 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2055 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2056 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2058 inode->i_nlink--; /* is this nlink == 0? */
2059 @@ -556,21 +1654,19 @@
2061 ext3_mark_inode_dirty(handle, inode);
2062 err = ext3_add_entry (handle, dentry, inode);
2064 - goto out_no_entry;
2066 + inode->i_nlink = 0;
2067 + ext3_mark_inode_dirty(handle, inode);
2072 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2073 + ext3_update_dx_flag(dir);
2074 ext3_mark_inode_dirty(handle, dir);
2075 d_instantiate(dentry, inode);
2077 ext3_journal_stop(handle, dir);
2081 - inode->i_nlink = 0;
2082 - ext3_mark_inode_dirty(handle, inode);
2088 @@ -657,7 +1753,7 @@
2092 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2093 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2096 /* Orphan handling is only valid for files with data blocks
2097 @@ -698,7 +1794,7 @@
2098 * This is safe: on error we're going to ignore the orphan list
2099 * anyway on the next recovery. */
2101 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2102 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2104 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2105 jbd_debug(4, "orphan inode %ld will point to %d\n",
2106 @@ -716,25 +1812,26 @@
2107 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2109 struct list_head *prev;
2110 + struct ext3_inode_info *ei = EXT3_I(inode);
2111 struct ext3_sb_info *sbi;
2112 unsigned long ino_next;
2113 struct ext3_iloc iloc;
2116 lock_super(inode->i_sb);
2117 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2118 + if (list_empty(&ei->i_orphan)) {
2119 unlock_super(inode->i_sb);
2123 ino_next = NEXT_ORPHAN(inode);
2124 - prev = inode->u.ext3_i.i_orphan.prev;
2125 + prev = ei->i_orphan.prev;
2126 sbi = EXT3_SB(inode->i_sb);
2128 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2130 - list_del(&inode->u.ext3_i.i_orphan);
2131 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2132 + list_del(&ei->i_orphan);
2133 + INIT_LIST_HEAD(&ei->i_orphan);
2135 /* If we're on an error path, we may not have a valid
2136 * transaction handle with which to update the orphan list on
2137 @@ -795,8 +1892,9 @@
2140 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2141 - if (IS_ERR(handle))
2142 + if (IS_ERR(handle)) {
2143 return PTR_ERR(handle);
2147 bh = ext3_find_entry (dentry, &de);
2148 @@ -834,7 +1932,7 @@
2150 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2151 ext3_mark_inode_dirty(handle, inode);
2152 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2153 + ext3_update_dx_flag(dir);
2154 ext3_mark_inode_dirty(handle, dir);
2157 @@ -852,8 +1950,9 @@
2160 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2161 - if (IS_ERR(handle))
2162 + if (IS_ERR(handle)) {
2163 return PTR_ERR(handle);
2168 @@ -880,7 +1979,7 @@
2171 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2172 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2173 + ext3_update_dx_flag(dir);
2174 ext3_mark_inode_dirty(handle, dir);
2176 if (!inode->i_nlink)
2177 @@ -906,9 +2005,11 @@
2178 if (l > dir->i_sb->s_blocksize)
2179 return -ENAMETOOLONG;
2181 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2182 - if (IS_ERR(handle))
2183 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2184 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2185 + if (IS_ERR(handle)) {
2186 return PTR_ERR(handle);
2191 @@ -918,7 +2019,7 @@
2195 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2196 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2197 inode->i_op = &ext3_symlink_inode_operations;
2198 inode->i_mapping->a_ops = &ext3_aops;
2200 @@ -927,24 +2028,22 @@
2201 * i_size in generic_commit_write().
2203 err = block_symlink(inode, symname, l);
2205 - goto out_no_entry;
2207 + ext3_dec_count(handle, inode);
2208 + ext3_mark_inode_dirty(handle, inode);
2213 inode->i_op = &ext3_fast_symlink_inode_operations;
2214 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2215 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2216 inode->i_size = l-1;
2218 - inode->u.ext3_i.i_disksize = inode->i_size;
2219 + EXT3_I(inode)->i_disksize = inode->i_size;
2220 err = ext3_add_nondir(handle, dentry, inode);
2222 ext3_journal_stop(handle, dir);
2226 - ext3_dec_count(handle, inode);
2227 - ext3_mark_inode_dirty(handle, inode);
2232 static int ext3_link (struct dentry * old_dentry,
2233 @@ -957,12 +2056,15 @@
2234 if (S_ISDIR(inode->i_mode))
2237 - if (inode->i_nlink >= EXT3_LINK_MAX)
2238 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2242 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2243 - if (IS_ERR(handle))
2244 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2245 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2246 + if (IS_ERR(handle)) {
2247 return PTR_ERR(handle);
2252 @@ -995,9 +2097,11 @@
2254 old_bh = new_bh = dir_bh = NULL;
2256 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2257 - if (IS_ERR(handle))
2258 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2259 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2260 + if (IS_ERR(handle)) {
2261 return PTR_ERR(handle);
2264 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2266 @@ -1070,14 +2174,37 @@
2270 - ext3_delete_entry(handle, old_dir, old_de, old_bh);
2271 + if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2272 + old_de->name_len != old_dentry->d_name.len ||
2273 + strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2274 + (retval = ext3_delete_entry(handle, old_dir,
2275 + old_de, old_bh)) == -ENOENT) {
2276 + /* old_de could have moved from under us during htree split, so
2277 + * make sure that we are deleting the right entry. We might
2278 + * also be pointing to a stale entry in the unused part of
2279 + * old_bh so just checking inum and the name isn't enough. */
2280 + struct buffer_head *old_bh2;
2281 + struct ext3_dir_entry_2 *old_de2;
2283 + old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2285 + retval = ext3_delete_entry(handle, old_dir,
2286 + old_de2, old_bh2);
2291 + ext3_warning(old_dir->i_sb, "ext3_rename",
2292 + "Deleting old file (%lu), %d, error=%d",
2293 + old_dir->i_ino, old_dir->i_nlink, retval);
2297 new_inode->i_nlink--;
2298 new_inode->i_ctime = CURRENT_TIME;
2300 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2301 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2302 + ext3_update_dx_flag(old_dir);
2304 BUFFER_TRACE(dir_bh, "get_write_access");
2305 ext3_journal_get_write_access(handle, dir_bh);
2306 @@ -1089,7 +2212,7 @@
2307 new_inode->i_nlink--;
2310 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2311 + ext3_update_dx_flag(new_dir);
2312 ext3_mark_inode_dirty(handle, new_dir);
2315 Index: linux-2.4.21-chaos/fs/ext3/super.c
2316 ===================================================================
2317 --- linux-2.4.21-chaos.orig/fs/ext3/super.c 2003-12-12 16:17:59.000000000 +0300
2318 +++ linux-2.4.21-chaos/fs/ext3/super.c 2003-12-12 16:18:17.000000000 +0300
2320 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2321 ext3_update_dynamic_rev(sb);
2322 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2324 ext3_commit_super (sb, es, 1);
2325 if (test_opt (sb, DEBUG))
2328 EXT3_BLOCKS_PER_GROUP(sb),
2329 EXT3_INODES_PER_GROUP(sb),
2332 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2333 bdevname(sb->s_dev));
2334 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2340 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2343 @@ -1146,6 +1149,9 @@
2344 sbi->s_mount_state = le16_to_cpu(es->s_state);
2345 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2346 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2347 + for (i=0; i < 4; i++)
2348 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2349 + sbi->s_def_hash_version = es->s_def_hash_version;
2351 if (sbi->s_blocks_per_group > blocksize * 8) {
2353 @@ -1938,6 +1944,7 @@
2354 unregister_filesystem(&ext3_fs_type);
2357 +EXPORT_SYMBOL(ext3_force_commit);
2358 EXPORT_SYMBOL(ext3_bread);
2360 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2361 Index: linux-2.4.21-chaos/include/linux/ext3_fs.h
2362 ===================================================================
2363 --- linux-2.4.21-chaos.orig/include/linux/ext3_fs.h 2003-12-05 16:54:33.000000000 +0300
2364 +++ linux-2.4.21-chaos/include/linux/ext3_fs.h 2003-12-12 16:18:17.000000000 +0300
2366 #define EXT3FS_VERSION "2.4-0.9.19"
2369 + * Always enable hashed directories
2371 +#define CONFIG_EXT3_INDEX
2377 @@ -415,8 +420,11 @@
2378 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2379 __u32 s_journal_dev; /* device number of journal file */
2380 __u32 s_last_orphan; /* start of list of inodes to delete */
2382 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2383 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2384 + __u8 s_def_hash_version; /* Default hash version to use */
2385 + __u8 s_reserved_char_pad;
2386 + __u16 s_reserved_word_pad;
2387 + __u32 s_reserved[192]; /* Padding to the end of the block */
2391 @@ -553,9 +561,46 @@
2392 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2393 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2396 + * Hash Tree Directory indexing
2397 + * (c) Daniel Phillips, 2001
2400 +#ifdef CONFIG_EXT3_INDEX
2401 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2402 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2403 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2404 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2405 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2407 + #define is_dx(dir) 0
2408 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2409 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2412 +/* Legal values for the dx_root hash_version field: */
2414 +#define DX_HASH_LEGACY 0
2415 +#define DX_HASH_HALF_MD4 1
2416 +#define DX_HASH_TEA 2
2418 +/* hash info structure used by the directory hash */
2419 +struct dx_hash_info
2429 + * Control parameters used by ext3_htree_next_block
2431 +#define HASH_NB_ALWAYS 1
2435 * Describe an inode's exact location on disk and in memory
2438 @@ -565,6 +610,27 @@
2439 unsigned long block_group;
2444 + * This structure is stuffed into the struct file's private_data field
2445 + * for directories. It is where we put information so that we can do
2446 + * readdir operations in hash tree order.
2448 +struct dir_private_info {
2450 + rb_node_t *curr_node;
2451 + struct fname *extra_fname;
2454 + __u32 curr_minor_hash;
2459 + * Special error return code only used by dx_probe() and its callers.
2461 +#define ERR_BAD_DX_DIR -75000
2464 * Function prototypes
2466 @@ -592,11 +658,20 @@
2469 extern int ext3_check_dir_entry(const char *, struct inode *,
2470 - struct ext3_dir_entry_2 *, struct buffer_head *,
2472 + struct ext3_dir_entry_2 *,
2473 + struct buffer_head *, unsigned long);
2474 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2476 + struct ext3_dir_entry_2 *dirent);
2477 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2480 extern int ext3_sync_file (struct file *, struct dentry *, int);
2483 +extern int ext3fs_dirhash(const char *name, int len, struct
2484 + dx_hash_info *hinfo);
2487 extern struct inode * ext3_new_inode (handle_t *, struct inode *, int);
2488 extern void ext3_free_inode (handle_t *, struct inode *);
2491 extern int ext3_orphan_add(handle_t *, struct inode *);
2492 extern int ext3_orphan_del(handle_t *, struct inode *);
2493 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2494 + __u32 start_minor_hash, __u32 *next_hash);
2497 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2498 Index: linux-2.4.21-chaos/include/linux/ext3_fs_sb.h
2499 ===================================================================
2500 --- linux-2.4.21-chaos.orig/include/linux/ext3_fs_sb.h 2003-12-05 16:54:33.000000000 +0300
2501 +++ linux-2.4.21-chaos/include/linux/ext3_fs_sb.h 2003-12-12 16:18:17.000000000 +0300
2505 u32 s_next_generation;
2506 + u32 s_hash_seed[4];
2507 + int s_def_hash_version;
2510 struct inode * s_journal_inode;
2511 Index: linux-2.4.21-chaos/include/linux/ext3_jbd.h
2512 ===================================================================
2513 --- linux-2.4.21-chaos.orig/include/linux/ext3_jbd.h 2003-12-05 16:54:33.000000000 +0300
2514 +++ linux-2.4.21-chaos/include/linux/ext3_jbd.h 2003-12-12 16:18:17.000000000 +0300
2517 #define EXT3_RESERVE_TRANS_BLOCKS 12U
2519 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2522 ext3_mark_iloc_dirty(handle_t *handle,
2523 struct inode *inode,
2524 Index: linux-2.4.21-chaos/include/linux/rbtree.h
2525 ===================================================================
2526 --- linux-2.4.21-chaos.orig/include/linux/rbtree.h 2003-12-05 16:54:33.000000000 +0300
2527 +++ linux-2.4.21-chaos/include/linux/rbtree.h 2003-12-12 16:18:17.000000000 +0300
2530 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2531 extern void rb_erase(rb_node_t *, rb_root_t *);
2532 +extern rb_node_t *rb_get_first(rb_root_t *root);
2533 +extern rb_node_t *rb_get_next(rb_node_t *n);
2535 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2537 Index: linux-2.4.21-chaos/lib/rbtree.c
2538 ===================================================================
2539 --- linux-2.4.21-chaos.orig/lib/rbtree.c 2002-09-25 21:14:03.000000000 +0400
2540 +++ linux-2.4.21-chaos/lib/rbtree.c 2003-12-12 16:18:17.000000000 +0300
2542 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2546 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2549 #include <linux/rbtree.h>
2550 @@ -294,3 +296,43 @@
2551 __rb_erase_color(child, parent, root);
2553 EXPORT_SYMBOL(rb_erase);
2556 + * This function returns the first node (in sort order) of the tree.
2558 +rb_node_t *rb_get_first(rb_root_t *root)
2562 + n = root->rb_node;
2565 + while (n->rb_left)
2569 +EXPORT_SYMBOL(rb_get_first);
2572 + * Given a node, this function will return the next node in the tree.
2574 +rb_node_t *rb_get_next(rb_node_t *n)
2576 + rb_node_t *parent;
2578 + if (n->rb_right) {
2580 + while (n->rb_left)
2584 + while ((parent = n->rb_parent)) {
2585 + if (n == parent->rb_left)
2592 +EXPORT_SYMBOL(rb_get_next);