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 --- linux-2.4.22-ac1/fs/ext3/dir.c~ext3-htree-2.4.22-rh 2001-11-10 01:25:04.000000000 +0300
15 +++ linux-2.4.22-ac1-alexey/fs/ext3/dir.c 2003-09-25 14:58:30.000000000 +0400
18 #include <linux/jbd.h>
19 #include <linux/ext3_fs.h>
20 +#include <linux/slab.h>
21 +#include <linux/rbtree.h>
23 static unsigned char ext3_filetype_table[] = {
24 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
27 static int ext3_readdir(struct file *, void *, filldir_t);
28 +static int ext3_dx_readdir(struct file * filp,
29 + void * dirent, filldir_t filldir);
31 struct file_operations ext3_dir_operations = {
32 read: generic_read_dir,
33 @@ -35,6 +39,17 @@ struct file_operations ext3_dir_operatio
34 fsync: ext3_sync_file, /* BKL held */
38 +static unsigned char get_dtype(struct super_block *sb, int filetype)
40 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
41 + (filetype >= EXT3_FT_MAX))
44 + return (ext3_filetype_table[filetype]);
48 int ext3_check_dir_entry (const char * function, struct inode * dir,
49 struct ext3_dir_entry_2 * de,
50 struct buffer_head * bh,
51 @@ -79,6 +94,16 @@ static int ext3_readdir(struct file * fi
56 + err = ext3_dx_readdir(filp, dirent, filldir);
57 + if (err != ERR_BAD_DX_DIR)
60 + * We don't set the inode dirty flag since it's not
61 + * critical that it get flushed back to the disk.
63 + EXT3_I(filp->f_dentry->d_inode)->i_flags &= ~EXT3_INDEX_FL;
67 offset = filp->f_pos & (sb->s_blocksize - 1);
68 @@ -162,18 +187,12 @@ revalidate:
69 * during the copy operation.
71 unsigned long version = filp->f_version;
72 - unsigned char d_type = DT_UNKNOWN;
74 - if (EXT3_HAS_INCOMPAT_FEATURE(sb,
75 - EXT3_FEATURE_INCOMPAT_FILETYPE)
76 - && de->file_type < EXT3_FT_MAX)
78 - ext3_filetype_table[de->file_type];
79 error = filldir(dirent, de->name,
82 le32_to_cpu(de->inode),
84 + get_dtype(sb, de->file_type));
87 if (version != filp->f_version)
88 @@ -188,3 +207,272 @@ revalidate:
93 +#ifdef CONFIG_EXT3_INDEX
95 + * These functions convert from the major/minor hash to an f_pos
98 + * Currently we only use major hash numer. This is unfortunate, but
99 + * on 32-bit machines, the same VFS interface is used for lseek and
100 + * llseek, so if we use the 64 bit offset, then the 32-bit versions of
101 + * lseek/telldir/seekdir will blow out spectacularly, and from within
102 + * the ext2 low-level routine, we don't know if we're being called by
103 + * a 64-bit version of the system call or the 32-bit version of the
104 + * system call. Worse yet, NFSv2 only allows for a 32-bit readdir
107 +#define hash2pos(major, minor) (major >> 1)
108 +#define pos2maj_hash(pos) ((pos << 1) & 0xffffffff)
109 +#define pos2min_hash(pos) (0)
112 + * This structure holds the nodes of the red-black tree used to store
113 + * the directory entry in hash order.
119 + struct fname *next;
127 + * This functoin implements a non-recursive way of freeing all of the
128 + * nodes in the red-black tree.
130 +static void free_rb_tree_fname(rb_root_t *root)
132 + rb_node_t *n = root->rb_node;
134 + struct fname *fname;
137 + /* Do the node's children first */
138 + if ((n)->rb_left) {
147 + * The node has no children; free it, and then zero
148 + * out parent's link to it. Finally go to the
149 + * beginning of the loop and try to free the parent
152 + parent = n->rb_parent;
153 + fname = rb_entry(n, struct fname, rb_hash);
157 + else if (parent->rb_left == n)
158 + parent->rb_left = 0;
159 + else if (parent->rb_right == n)
160 + parent->rb_right = 0;
167 +struct dir_private_info *create_dir_info(loff_t pos)
169 + struct dir_private_info *p;
171 + p = kmalloc(sizeof(struct dir_private_info), GFP_KERNEL);
174 + p->root.rb_node = 0;
176 + p->extra_fname = 0;
178 + p->curr_hash = pos2maj_hash(pos);
179 + p->curr_minor_hash = pos2min_hash(pos);
184 +void ext3_htree_free_dir_info(struct dir_private_info *p)
186 + free_rb_tree_fname(&p->root);
191 + * Given a directory entry, enter it into the fname rb tree.
193 +int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
195 + struct ext3_dir_entry_2 *dirent)
197 + rb_node_t **p, *parent = NULL;
198 + struct fname * fname, *new_fn;
199 + struct dir_private_info *info;
202 + info = (struct dir_private_info *) dir_file->private_data;
203 + p = &info->root.rb_node;
205 + /* Create and allocate the fname structure */
206 + len = sizeof(struct fname) + dirent->name_len + 1;
207 + new_fn = kmalloc(len, GFP_KERNEL);
210 + memset(new_fn, 0, len);
211 + new_fn->hash = hash;
212 + new_fn->minor_hash = minor_hash;
213 + new_fn->inode = le32_to_cpu(dirent->inode);
214 + new_fn->name_len = dirent->name_len;
215 + new_fn->file_type = dirent->file_type;
216 + memcpy(new_fn->name, dirent->name, dirent->name_len);
217 + new_fn->name[dirent->name_len] = 0;
221 + fname = rb_entry(parent, struct fname, rb_hash);
224 + * If the hash and minor hash match up, then we put
225 + * them on a linked list. This rarely happens...
227 + if ((new_fn->hash == fname->hash) &&
228 + (new_fn->minor_hash == fname->minor_hash)) {
229 + new_fn->next = fname->next;
230 + fname->next = new_fn;
234 + if (new_fn->hash < fname->hash)
235 + p = &(*p)->rb_left;
236 + else if (new_fn->hash > fname->hash)
237 + p = &(*p)->rb_right;
238 + else if (new_fn->minor_hash < fname->minor_hash)
239 + p = &(*p)->rb_left;
240 + else /* if (new_fn->minor_hash > fname->minor_hash) */
241 + p = &(*p)->rb_right;
244 + rb_link_node(&new_fn->rb_hash, parent, p);
245 + rb_insert_color(&new_fn->rb_hash, &info->root);
252 + * This is a helper function for ext3_dx_readdir. It calls filldir
253 + * for all entres on the fname linked list. (Normally there is only
254 + * one entry on the linked list, unless there are 62 bit hash collisions.)
256 +static int call_filldir(struct file * filp, void * dirent,
257 + filldir_t filldir, struct fname *fname)
259 + struct dir_private_info *info = filp->private_data;
261 + struct inode *inode = filp->f_dentry->d_inode;
262 + struct super_block * sb;
268 + printk("call_filldir: called with null fname?!?\n");
271 + curr_pos = hash2pos(fname->hash, fname->minor_hash);
273 + error = filldir(dirent, fname->name,
274 + fname->name_len, curr_pos,
276 + get_dtype(sb, fname->file_type));
278 + filp->f_pos = curr_pos;
279 + info->extra_fname = fname->next;
282 + fname = fname->next;
287 +static int ext3_dx_readdir(struct file * filp,
288 + void * dirent, filldir_t filldir)
290 + struct dir_private_info *info = filp->private_data;
291 + struct inode *inode = filp->f_dentry->d_inode;
292 + struct fname *fname;
296 + info = create_dir_info(filp->f_pos);
299 + filp->private_data = info;
302 + /* Some one has messed with f_pos; reset the world */
303 + if (info->last_pos != filp->f_pos) {
304 + free_rb_tree_fname(&info->root);
305 + info->curr_node = 0;
306 + info->extra_fname = 0;
307 + info->curr_hash = pos2maj_hash(filp->f_pos);
308 + info->curr_minor_hash = pos2min_hash(filp->f_pos);
312 + * If there are any leftover names on the hash collision
313 + * chain, return them first.
315 + if (info->extra_fname &&
316 + call_filldir(filp, dirent, filldir, info->extra_fname))
319 + if (!info->curr_node)
320 + info->curr_node = rb_get_first(&info->root);
324 + * Fill the rbtree if we have no more entries,
325 + * or the inode has changed since we last read in the
328 + if ((!info->curr_node) ||
329 + (filp->f_version != inode->i_version)) {
330 + info->curr_node = 0;
331 + free_rb_tree_fname(&info->root);
332 + filp->f_version = inode->i_version;
333 + ret = ext3_htree_fill_tree(filp, info->curr_hash,
334 + info->curr_minor_hash,
340 + info->curr_node = rb_get_first(&info->root);
343 + fname = rb_entry(info->curr_node, struct fname, rb_hash);
344 + info->curr_hash = fname->hash;
345 + info->curr_minor_hash = fname->minor_hash;
346 + if (call_filldir(filp, dirent, filldir, fname))
349 + info->curr_node = rb_get_next(info->curr_node);
350 + if (!info->curr_node) {
351 + info->curr_hash = info->next_hash;
352 + info->curr_minor_hash = 0;
356 + info->last_pos = filp->f_pos;
357 + UPDATE_ATIME(inode);
361 --- linux-2.4.22-ac1/fs/ext3/file.c~ext3-htree-2.4.22-rh 2003-08-25 15:44:43.000000000 +0400
362 +++ linux-2.4.22-ac1-alexey/fs/ext3/file.c 2003-09-25 14:55:12.000000000 +0400
363 @@ -35,6 +35,9 @@ static int ext3_release_file (struct ino
365 if (filp->f_mode & FMODE_WRITE)
366 ext3_discard_prealloc (inode);
367 + if (is_dx(inode) && filp->private_data)
368 + ext3_htree_free_dir_info(filp->private_data);
373 --- /dev/null 2003-01-30 13:24:37.000000000 +0300
374 +++ linux-2.4.22-ac1-alexey/fs/ext3/hash.c 2003-09-25 14:55:12.000000000 +0400
377 + * linux/fs/ext3/hash.c
379 + * Copyright (C) 2002 by Theodore Ts'o
381 + * This file is released under the GPL v2.
383 + * This file may be redistributed under the terms of the GNU Public
387 +#include <linux/fs.h>
388 +#include <linux/jbd.h>
389 +#include <linux/sched.h>
390 +#include <linux/ext3_fs.h>
392 +#define DELTA 0x9E3779B9
394 +static void TEA_transform(__u32 buf[4], __u32 const in[])
397 + __u32 b0 = buf[0], b1 = buf[1];
398 + __u32 a = in[0], b = in[1], c = in[2], d = in[3];
403 + b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
404 + b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
411 +/* F, G and H are basic MD4 functions: selection, majority, parity */
412 +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
413 +#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
414 +#define H(x, y, z) ((x) ^ (y) ^ (z))
417 + * The generic round function. The application is so specific that
418 + * we don't bother protecting all the arguments with parens, as is generally
419 + * good macro practice, in favor of extra legibility.
420 + * Rotation is separate from addition to prevent recomputation
422 +#define ROUND(f, a, b, c, d, x, s) \
423 + (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
425 +#define K2 013240474631UL
426 +#define K3 015666365641UL
429 + * Basic cut-down MD4 transform. Returns only 32 bits of result.
431 +static void halfMD4Transform (__u32 buf[4], __u32 const in[])
433 + __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
436 + ROUND(F, a, b, c, d, in[0] + K1, 3);
437 + ROUND(F, d, a, b, c, in[1] + K1, 7);
438 + ROUND(F, c, d, a, b, in[2] + K1, 11);
439 + ROUND(F, b, c, d, a, in[3] + K1, 19);
440 + ROUND(F, a, b, c, d, in[4] + K1, 3);
441 + ROUND(F, d, a, b, c, in[5] + K1, 7);
442 + ROUND(F, c, d, a, b, in[6] + K1, 11);
443 + ROUND(F, b, c, d, a, in[7] + K1, 19);
446 + ROUND(G, a, b, c, d, in[1] + K2, 3);
447 + ROUND(G, d, a, b, c, in[3] + K2, 5);
448 + ROUND(G, c, d, a, b, in[5] + K2, 9);
449 + ROUND(G, b, c, d, a, in[7] + K2, 13);
450 + ROUND(G, a, b, c, d, in[0] + K2, 3);
451 + ROUND(G, d, a, b, c, in[2] + K2, 5);
452 + ROUND(G, c, d, a, b, in[4] + K2, 9);
453 + ROUND(G, b, c, d, a, in[6] + K2, 13);
456 + ROUND(H, a, b, c, d, in[3] + K3, 3);
457 + ROUND(H, d, a, b, c, in[7] + K3, 9);
458 + ROUND(H, c, d, a, b, in[2] + K3, 11);
459 + ROUND(H, b, c, d, a, in[6] + K3, 15);
460 + ROUND(H, a, b, c, d, in[1] + K3, 3);
461 + ROUND(H, d, a, b, c, in[5] + K3, 9);
462 + ROUND(H, c, d, a, b, in[0] + K3, 11);
463 + ROUND(H, b, c, d, a, in[4] + K3, 15);
479 +/* The old legacy hash */
480 +static __u32 dx_hack_hash (const char *name, int len)
482 + __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
484 + __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
486 + if (hash & 0x80000000) hash -= 0x7fffffff;
490 + return (hash0 << 1);
493 +static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
498 + pad = (__u32)len | ((__u32)len << 8);
504 + for (i=0; i < len; i++) {
507 + val = msg[i] + (val << 8);
508 + if ((i % 4) == 3) {
521 + * Returns the hash of a filename. If len is 0 and name is NULL, then
522 + * this function can be used to test whether or not a hash version is
525 + * The seed is an 4 longword (32 bits) "secret" which can be used to
526 + * uniquify a hash. If the seed is all zero's, then some default seed
529 + * A particular hash version specifies whether or not the seed is
530 + * represented, and whether or not the returned hash is 32 bits or 64
531 + * bits. 32 bit hashes will return 0 for the minor hash.
533 +int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
536 + __u32 minor_hash = 0;
539 + __u32 in[8], buf[4];
541 + /* Initialize the default seed for the hash checksum functions */
542 + buf[0] = 0x67452301;
543 + buf[1] = 0xefcdab89;
544 + buf[2] = 0x98badcfe;
545 + buf[3] = 0x10325476;
547 + /* Check to see if the seed is all zero's */
549 + for (i=0; i < 4; i++) {
550 + if (hinfo->seed[i])
554 + memcpy(buf, hinfo->seed, sizeof(buf));
557 + switch (hinfo->hash_version) {
558 + case DX_HASH_LEGACY:
559 + hash = dx_hack_hash(name, len);
561 + case DX_HASH_HALF_MD4:
564 + str2hashbuf(p, len, in, 8);
565 + halfMD4Transform(buf, in);
569 + minor_hash = buf[2];
575 + str2hashbuf(p, len, in, 4);
576 + TEA_transform(buf, in);
581 + minor_hash = buf[1];
587 + hinfo->hash = hash & ~1;
588 + hinfo->minor_hash = minor_hash;
591 --- linux-2.4.22-ac1/fs/ext3/Makefile~ext3-htree-2.4.22-rh 2003-09-25 14:39:01.000000000 +0400
592 +++ linux-2.4.22-ac1-alexey/fs/ext3/Makefile 2003-09-25 14:55:12.000000000 +0400
593 @@ -12,7 +12,7 @@ O_TARGET := ext3.o
594 export-objs := super.o inode.o
596 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
597 - ioctl.o namei.o super.o symlink.o
598 + ioctl.o namei.o super.o symlink.o hash.o
601 include $(TOPDIR)/Rules.make
602 --- linux-2.4.22-ac1/fs/ext3/namei.c~ext3-htree-2.4.22-rh 2003-09-25 14:16:29.000000000 +0400
603 +++ linux-2.4.22-ac1-alexey/fs/ext3/namei.c 2003-09-25 14:58:37.000000000 +0400
605 * David S. Miller (davem@caip.rutgers.edu), 1995
606 * Directory entry file type support and forward compatibility hooks
607 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
608 + * Hash Tree Directory indexing (c)
609 + * Daniel Phillips, 2001
610 + * Hash Tree Directory indexing porting
611 + * Christopher Li, 2002
612 + * Hash Tree Directory indexing cleanup
613 + * Theodore Ts'o, 2002
616 #include <linux/fs.h>
618 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
619 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
621 +static struct buffer_head *ext3_append(handle_t *handle,
622 + struct inode *inode,
623 + u32 *block, int *err)
625 + struct buffer_head *bh;
627 + *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
629 + if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
630 + inode->i_size += inode->i_sb->s_blocksize;
631 + EXT3_I(inode)->i_disksize = inode->i_size;
632 + ext3_journal_get_write_access(handle,bh);
638 +#define assert(test) J_ASSERT(test)
642 +#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
645 +typedef struct { u32 v; } le_u32;
646 +typedef struct { u16 v; } le_u16;
649 +#define dxtrace(command) command
651 +#define dxtrace(command)
662 +struct dx_countlimit
675 + * dx_root_info is laid out so that if it should somehow get overlaid by a
676 + * dirent the two low bits of the hash version will be zero. Therefore, the
677 + * hash version mod 4 should never be 0. Sincerely, the paranoia department.
682 + struct fake_dirent dot;
684 + struct fake_dirent dotdot;
685 + char dotdot_name[4];
686 + struct dx_root_info
688 + le_u32 reserved_zero;
690 + u8 info_length; /* 8 */
691 + u8 indirect_levels;
695 + struct dx_entry entries[0];
700 + struct fake_dirent fake;
701 + struct dx_entry entries[0];
707 + struct buffer_head *bh;
708 + struct dx_entry *entries;
709 + struct dx_entry *at;
718 +#ifdef CONFIG_EXT3_INDEX
719 +static inline unsigned dx_get_block (struct dx_entry *entry);
720 +static void dx_set_block (struct dx_entry *entry, unsigned value);
721 +static inline unsigned dx_get_hash (struct dx_entry *entry);
722 +static void dx_set_hash (struct dx_entry *entry, unsigned value);
723 +static unsigned dx_get_count (struct dx_entry *entries);
724 +static unsigned dx_get_limit (struct dx_entry *entries);
725 +static void dx_set_count (struct dx_entry *entries, unsigned value);
726 +static void dx_set_limit (struct dx_entry *entries, unsigned value);
727 +static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
728 +static unsigned dx_node_limit (struct inode *dir);
729 +static struct dx_frame *dx_probe(struct dentry *dentry,
731 + struct dx_hash_info *hinfo,
732 + struct dx_frame *frame,
734 +static void dx_release (struct dx_frame *frames);
735 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
736 + struct dx_hash_info *hinfo, struct dx_map_entry map[]);
737 +static void dx_sort_map(struct dx_map_entry *map, unsigned count);
738 +static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
739 + struct dx_map_entry *offsets, int count);
740 +static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
741 +static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
742 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
743 + struct dx_frame *frame,
744 + struct dx_frame *frames, int *err,
745 + __u32 *start_hash);
746 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
747 + struct ext3_dir_entry_2 **res_dir, int *err);
748 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
749 + struct inode *inode);
752 + * Future: use high four bits of block for coalesce-on-delete flags
753 + * Mask them off for now.
756 +static inline unsigned dx_get_block (struct dx_entry *entry)
758 + return le32_to_cpu(entry->block.v) & 0x00ffffff;
761 +static inline void dx_set_block (struct dx_entry *entry, unsigned value)
763 + entry->block.v = cpu_to_le32(value);
766 +static inline unsigned dx_get_hash (struct dx_entry *entry)
768 + return le32_to_cpu(entry->hash.v);
771 +static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
773 + entry->hash.v = cpu_to_le32(value);
776 +static inline unsigned dx_get_count (struct dx_entry *entries)
778 + return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
781 +static inline unsigned dx_get_limit (struct dx_entry *entries)
783 + return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
786 +static inline void dx_set_count (struct dx_entry *entries, unsigned value)
788 + ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
791 +static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
793 + ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
796 +static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
798 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
799 + EXT3_DIR_REC_LEN(2) - infosize;
800 + return 0? 20: entry_space / sizeof(struct dx_entry);
803 +static inline unsigned dx_node_limit (struct inode *dir)
805 + unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
806 + return 0? 22: entry_space / sizeof(struct dx_entry);
820 +static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
821 + int size, int show_names)
823 + unsigned names = 0, space = 0;
824 + char *base = (char *) de;
825 + struct dx_hash_info h = *hinfo;
828 + while ((char *) de < base + size)
834 + int len = de->name_len;
835 + char *name = de->name;
836 + while (len--) printk("%c", *name++);
837 + ext3fs_dirhash(de->name, de->name_len, &h);
838 + printk(":%x.%u ", h.hash,
839 + ((char *) de - base));
841 + space += EXT3_DIR_REC_LEN(de->name_len);
844 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
846 + printk("(%i)\n", names);
847 + return (struct stats) { names, space, 1 };
850 +struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
851 + struct dx_entry *entries, int levels)
853 + unsigned blocksize = dir->i_sb->s_blocksize;
854 + unsigned count = dx_get_count (entries), names = 0, space = 0, i;
855 + unsigned bcount = 0;
856 + struct buffer_head *bh;
858 + printk("%i indexed blocks...\n", count);
859 + for (i = 0; i < count; i++, entries++)
861 + u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
862 + u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
863 + struct stats stats;
864 + printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
865 + if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
867 + dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
868 + dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
869 + names += stats.names;
870 + space += stats.space;
871 + bcount += stats.bcount;
875 + printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
876 + names, space/bcount,(space/bcount)*100/blocksize);
877 + return (struct stats) { names, space, bcount};
879 +#endif /* DX_DEBUG */
882 + * Probe for a directory leaf block to search.
884 + * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
885 + * error in the directory index, and the caller should fall back to
886 + * searching the directory normally. The callers of dx_probe **MUST**
887 + * check for this error code, and make sure it never gets reflected
888 + * back to userspace.
890 +static struct dx_frame *
891 +dx_probe(struct dentry *dentry, struct inode *dir,
892 + struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
894 + unsigned count, indirect;
895 + struct dx_entry *at, *entries, *p, *q, *m;
896 + struct dx_root *root;
897 + struct buffer_head *bh;
898 + struct dx_frame *frame = frame_in;
903 + dir = dentry->d_parent->d_inode;
904 + if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
906 + root = (struct dx_root *) bh->b_data;
907 + if (root->info.hash_version != DX_HASH_TEA &&
908 + root->info.hash_version != DX_HASH_HALF_MD4 &&
909 + root->info.hash_version != DX_HASH_LEGACY) {
910 + ext3_warning(dir->i_sb, __FUNCTION__,
911 + "Unrecognised inode hash code %d",
912 + root->info.hash_version);
914 + *err = ERR_BAD_DX_DIR;
917 + hinfo->hash_version = root->info.hash_version;
918 + hinfo->seed = dir->i_sb->u.ext3_sb.s_hash_seed;
920 + ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
921 + hash = hinfo->hash;
923 + if (root->info.unused_flags & 1) {
924 + ext3_warning(dir->i_sb, __FUNCTION__,
925 + "Unimplemented inode hash flags: %#06x",
926 + root->info.unused_flags);
928 + *err = ERR_BAD_DX_DIR;
932 + if ((indirect = root->info.indirect_levels) > 1) {
933 + ext3_warning(dir->i_sb, __FUNCTION__,
934 + "Unimplemented inode hash depth: %#06x",
935 + root->info.indirect_levels);
937 + *err = ERR_BAD_DX_DIR;
941 + entries = (struct dx_entry *) (((char *)&root->info) +
942 + root->info.info_length);
943 + assert(dx_get_limit(entries) == dx_root_limit(dir,
944 + root->info.info_length));
945 + dxtrace (printk("Look up %x", hash));
948 + count = dx_get_count(entries);
949 + assert (count && count <= dx_get_limit(entries));
951 + q = entries + count - 1;
955 + dxtrace(printk("."));
956 + if (dx_get_hash(m) > hash)
962 + if (0) // linear search cross check
964 + unsigned n = count - 1;
968 + dxtrace(printk(","));
969 + if (dx_get_hash(++at) > hash)
975 + assert (at == p - 1);
979 + dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
981 + frame->entries = entries;
983 + if (!indirect--) return frame;
984 + if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
986 + at = entries = ((struct dx_node *) bh->b_data)->entries;
987 + assert (dx_get_limit(entries) == dx_node_limit (dir));
991 + while (frame >= frame_in) {
999 +static void dx_release (struct dx_frame *frames)
1001 + if (frames[0].bh == NULL)
1004 + if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
1005 + brelse(frames[1].bh);
1006 + brelse(frames[0].bh);
1010 + * This function increments the frame pointer to search the next leaf
1011 + * block, and reads in the necessary intervening nodes if the search
1012 + * should be necessary. Whether or not the search is necessary is
1013 + * controlled by the hash parameter. If the hash value is even, then
1014 + * the search is only continued if the next block starts with that
1015 + * hash value. This is used if we are searching for a specific file.
1017 + * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
1019 + * This function returns 1 if the caller should continue to search,
1020 + * or 0 if it should not. If there is an error reading one of the
1021 + * index blocks, it will return -1.
1023 + * If start_hash is non-null, it will be filled in with the starting
1024 + * hash of the next page.
1026 +static int ext3_htree_next_block(struct inode *dir, __u32 hash,
1027 + struct dx_frame *frame,
1028 + struct dx_frame *frames, int *err,
1029 + __u32 *start_hash)
1031 + struct dx_frame *p;
1032 + struct buffer_head *bh;
1033 + int num_frames = 0;
1039 + * Find the next leaf page by incrementing the frame pointer.
1040 + * If we run out of entries in the interior node, loop around and
1041 + * increment pointer in the parent node. When we break out of
1042 + * this loop, num_frames indicates the number of interior
1043 + * nodes need to be read.
1046 + if (++(p->at) < p->entries + dx_get_count(p->entries))
1055 + * If the hash is 1, then continue only if the next page has a
1056 + * continuation hash of any value. This is used for readdir
1057 + * handling. Otherwise, check to see if the hash matches the
1058 + * desired contiuation hash. If it doesn't, return since
1059 + * there's no point to read in the successive index pages.
1061 + bhash = dx_get_hash(p->at);
1063 + *start_hash = bhash;
1064 + if ((hash & 1) == 0) {
1065 + if ((bhash & ~1) != hash)
1069 + * If the hash is HASH_NB_ALWAYS, we always go to the next
1070 + * block so no check is necessary
1072 + while (num_frames--) {
1073 + if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
1075 + return -1; /* Failure */
1079 + p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1086 + * p is at least 6 bytes before the end of page
1088 +static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
1090 + return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
1094 + * This function fills a red-black tree with information from a
1095 + * directory. We start scanning the directory in hash order, starting
1096 + * at start_hash and start_minor_hash.
1098 + * This function returns the number of entries inserted into the tree,
1099 + * or a negative error code.
1101 +int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1102 + __u32 start_minor_hash, __u32 *next_hash)
1104 + struct dx_hash_info hinfo;
1105 + struct buffer_head *bh;
1106 + struct ext3_dir_entry_2 *de, *top;
1107 + static struct dx_frame frames[2], *frame;
1108 + struct inode *dir;
1114 + dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
1115 + start_minor_hash));
1116 + dir = dir_file->f_dentry->d_inode;
1117 + hinfo.hash = start_hash;
1118 + hinfo.minor_hash = 0;
1119 + frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
1123 + /* Add '.' and '..' from the htree header */
1124 + if (!start_hash && !start_minor_hash) {
1125 + de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
1126 + if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1128 + de = ext3_next_entry(de);
1129 + if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
1135 + block = dx_get_block(frame->at);
1136 + dxtrace(printk("Reading block %d\n", block));
1137 + if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
1140 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1141 + top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
1142 + EXT3_DIR_REC_LEN(0));
1143 + for (; de < top; de = ext3_next_entry(de)) {
1144 + ext3fs_dirhash(de->name, de->name_len, &hinfo);
1145 + if ((hinfo.hash < start_hash) ||
1146 + ((hinfo.hash == start_hash) &&
1147 + (hinfo.minor_hash < start_minor_hash)))
1149 + if ((err = ext3_htree_store_dirent(dir_file,
1150 + hinfo.hash, hinfo.minor_hash, de)) != 0)
1156 + ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
1157 + frame, frames, &err, &hashval);
1159 + *next_hash = hashval;
1163 + * Stop if: (a) there are no more entries, or
1164 + * (b) we have inserted at least one entry and the
1165 + * next hash value is not a continuation
1168 + (count && ((hashval & 1) == 0)))
1171 + dx_release(frames);
1172 + dxtrace(printk("Fill tree: returned %d entries\n", count));
1175 + dx_release(frames);
1181 + * Directory block splitting, compacting
1184 +static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
1185 + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
1188 + char *base = (char *) de;
1189 + struct dx_hash_info h = *hinfo;
1191 + while ((char *) de < base + size)
1193 + if (de->name_len && de->inode) {
1194 + ext3fs_dirhash(de->name, de->name_len, &h);
1196 + map_tail->hash = h.hash;
1197 + map_tail->offs = (u32) ((char *) de - base);
1200 + /* XXX: do we need to check rec_len == 0 case? -Chris */
1201 + de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
1206 +static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1208 + struct dx_map_entry *p, *q, *top = map + count - 1;
1210 + /* Combsort until bubble sort doesn't suck */
1213 + count = count*10/13;
1214 + if (count - 9 < 2) /* 9, 10 -> 11 */
1216 + for (p = top, q = p - count; q >= map; p--, q--)
1217 + if (p->hash < q->hash)
1220 + /* Garden variety bubble sort */
1226 + if (q[1].hash >= q[0].hash)
1234 +static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
1236 + struct dx_entry *entries = frame->entries;
1237 + struct dx_entry *old = frame->at, *new = old + 1;
1238 + int count = dx_get_count(entries);
1240 + assert(count < dx_get_limit(entries));
1241 + assert(old < entries + count);
1242 + memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1243 + dx_set_hash(new, hash);
1244 + dx_set_block(new, block);
1245 + dx_set_count(entries, count + 1);
1250 +static void ext3_update_dx_flag(struct inode *inode)
1252 + if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
1253 + EXT3_FEATURE_COMPAT_DIR_INDEX))
1254 + EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
1258 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
1260 @@ -94,6 +736,7 @@ static int inline search_dirblock(struct
1268 @@ -105,6 +748,8 @@ static int inline search_dirblock(struct
1269 * The returned buffer_head has ->b_count elevated. The caller is expected
1270 * to brelse() it when appropriate.
1274 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
1275 struct ext3_dir_entry_2 ** res_dir)
1277 @@ -119,12 +764,32 @@ static struct buffer_head * ext3_find_en
1279 int nblocks, i, err;
1280 struct inode *dir = dentry->d_parent->d_inode;
1283 + unsigned blocksize;
1288 + blocksize = sb->s_blocksize;
1289 + namelen = dentry->d_name.len;
1290 + name = dentry->d_name.name;
1291 + if (namelen > EXT3_NAME_LEN)
1293 +#ifdef CONFIG_EXT3_INDEX
1295 + bh = ext3_dx_find_entry(dentry, res_dir, &err);
1297 + * On success, or if the error was file not found,
1298 + * return. Otherwise, fall back to doing a search the
1299 + * old fashioned way.
1301 + if (bh || (err != ERR_BAD_DX_DIR))
1303 + dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
1306 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
1307 - start = dir->u.ext3_i.i_dir_start_lookup;
1308 + start = EXT3_I(dir)->i_dir_start_lookup;
1309 if (start >= nblocks)
1312 @@ -166,7 +831,7 @@ restart:
1313 i = search_dirblock(bh, dir, dentry,
1314 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
1316 - dir->u.ext3_i.i_dir_start_lookup = block;
1317 + EXT3_I(dir)->i_dir_start_lookup = block;
1319 goto cleanup_and_exit;
1321 @@ -197,6 +862,74 @@ cleanup_and_exit:
1325 +#ifdef CONFIG_EXT3_INDEX
1326 +static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
1327 + struct ext3_dir_entry_2 **res_dir, int *err)
1329 + struct super_block * sb;
1330 + struct dx_hash_info hinfo;
1332 + struct dx_frame frames[2], *frame;
1333 + struct ext3_dir_entry_2 *de, *top;
1334 + struct buffer_head *bh;
1335 + unsigned long block;
1337 + int namelen = dentry->d_name.len;
1338 + const u8 *name = dentry->d_name.name;
1339 + struct inode *dir = dentry->d_parent->d_inode;
1342 + /* NFS may look up ".." - look at dx_root directory block */
1343 + if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
1344 + if (!(frame = dx_probe(dentry, 0, &hinfo, frames, err)))
1348 + frame->bh = NULL; /* for dx_release() */
1349 + frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
1350 + dx_set_block(frame->at, 0); /* dx_root block is 0 */
1352 + hash = hinfo.hash;
1354 + block = dx_get_block(frame->at);
1355 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1357 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1358 + top = (struct ext3_dir_entry_2 *)((char *)de + sb->s_blocksize -
1359 + EXT3_DIR_REC_LEN(0));
1360 + for (; de < top; de = ext3_next_entry(de))
1361 + if (ext3_match (namelen, name, de)) {
1362 + if (!ext3_check_dir_entry("ext3_find_entry",
1364 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1365 + +((char *)de - bh->b_data))) {
1370 + dx_release (frames);
1374 + /* Check to see if we should continue to search */
1375 + retval = ext3_htree_next_block(dir, hash, frame,
1377 + if (retval == -1) {
1378 + ext3_warning(sb, __FUNCTION__,
1379 + "error reading index page in directory #%lu",
1383 + } while (retval == 1);
1387 + dxtrace(printk("%s not found\n", name));
1388 + dx_release (frames);
1393 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1395 struct inode * inode;
1396 @@ -213,8 +938,9 @@ static struct dentry *ext3_lookup(struct
1398 inode = iget(dir->i_sb, ino);
1402 return ERR_PTR(-EACCES);
1405 d_add(dentry, inode);
1407 @@ -238,6 +964,301 @@ static inline void ext3_set_de_type(stru
1408 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1411 +#ifdef CONFIG_EXT3_INDEX
1412 +static struct ext3_dir_entry_2 *
1413 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1415 + unsigned rec_len = 0;
1418 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1419 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1420 + memcpy (to, de, rec_len);
1421 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1426 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1429 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1431 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1432 + unsigned rec_len = 0;
1435 + while ((char*)de < base + size) {
1436 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1437 + le16_to_cpu(de->rec_len));
1438 + if (de->inode && de->name_len) {
1439 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1441 + memmove(to, de, rec_len);
1442 + to->rec_len = cpu_to_le16(rec_len);
1444 + to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1451 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1452 + struct buffer_head **bh,struct dx_frame *frame,
1453 + struct dx_hash_info *hinfo, int *error)
1455 + unsigned blocksize = dir->i_sb->s_blocksize;
1456 + unsigned count, continued;
1457 + struct buffer_head *bh2;
1460 + struct dx_map_entry *map;
1461 + char *data1 = (*bh)->b_data, *data2;
1463 + struct ext3_dir_entry_2 *de = NULL, *de2;
1466 + bh2 = ext3_append (handle, dir, &newblock, error);
1473 + BUFFER_TRACE(*bh, "get_write_access");
1474 + err = ext3_journal_get_write_access(handle, *bh);
1480 + ext3_std_error(dir->i_sb, err);
1483 + BUFFER_TRACE(frame->bh, "get_write_access");
1484 + err = ext3_journal_get_write_access(handle, frame->bh);
1486 + goto journal_error;
1488 + data2 = bh2->b_data;
1490 + /* create map in the end of data2 block */
1491 + map = (struct dx_map_entry *) (data2 + blocksize);
1492 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1493 + blocksize, hinfo, map);
1495 + split = count/2; // need to adjust to actual middle
1496 + dx_sort_map (map, count);
1497 + hash2 = map[split].hash;
1498 + continued = hash2 == map[split - 1].hash;
1499 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1500 + dx_get_block(frame->at), hash2, split, count-split));
1502 + /* Fancy dance to stay within two buffers */
1503 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1504 + de = dx_pack_dirents(data1,blocksize);
1505 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1506 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1507 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1508 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1510 + /* Which block gets the new entry? */
1511 + if (hinfo->hash >= hash2)
1516 + dx_insert_block (frame, hash2 + continued, newblock);
1517 + err = ext3_journal_dirty_metadata (handle, bh2);
1519 + goto journal_error;
1520 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1522 + goto journal_error;
1524 + dxtrace(dx_show_index ("frame", frame->entries));
1532 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1533 + * it points to a directory entry which is guaranteed to be large
1534 + * enough for new directory entry. If de is NULL, then
1535 + * add_dirent_to_buf will attempt search the directory block for
1536 + * space. It will return -ENOSPC if no space is available, and -EIO
1537 + * and -EEXIST if directory entry already exists.
1539 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1540 + * all other cases bh is released.
1542 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1543 + struct inode *inode, struct ext3_dir_entry_2 *de,
1544 + struct buffer_head * bh)
1546 + struct inode *dir = dentry->d_parent->d_inode;
1547 + const char *name = dentry->d_name.name;
1548 + int namelen = dentry->d_name.len;
1549 + unsigned long offset = 0;
1550 + unsigned short reclen;
1551 + int nlen, rlen, err;
1554 + reclen = EXT3_DIR_REC_LEN(namelen);
1556 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1557 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1558 + while ((char *) de <= top) {
1559 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1564 + if (ext3_match (namelen, name, de)) {
1568 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1569 + rlen = le16_to_cpu(de->rec_len);
1570 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1572 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1575 + if ((char *) de > top)
1578 + BUFFER_TRACE(bh, "get_write_access");
1579 + err = ext3_journal_get_write_access(handle, bh);
1581 + ext3_std_error(dir->i_sb, err);
1586 + /* By now the buffer is marked for journaling */
1587 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1588 + rlen = le16_to_cpu(de->rec_len);
1590 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1591 + de1->rec_len = cpu_to_le16(rlen - nlen);
1592 + de->rec_len = cpu_to_le16(nlen);
1595 + de->file_type = EXT3_FT_UNKNOWN;
1597 + de->inode = cpu_to_le32(inode->i_ino);
1598 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1601 + de->name_len = namelen;
1602 + memcpy (de->name, name, namelen);
1604 + * XXX shouldn't update any times until successful
1605 + * completion of syscall, but too many callers depend
1608 + * XXX similarly, too many callers depend on
1609 + * ext3_new_inode() setting the times, but error
1610 + * recovery deletes the inode, so the worst that can
1611 + * happen is that the times are slightly out of date
1612 + * and/or different from the directory change time.
1614 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1615 + ext3_update_dx_flag(dir);
1616 + dir->i_version = ++event;
1617 + ext3_mark_inode_dirty(handle, dir);
1618 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1619 + err = ext3_journal_dirty_metadata(handle, bh);
1621 + ext3_std_error(dir->i_sb, err);
1626 +#ifdef CONFIG_EXT3_INDEX
1628 + * This converts a one block unindexed directory to a 3 block indexed
1629 + * directory, and adds the dentry to the indexed directory.
1631 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1632 + struct inode *inode, struct buffer_head *bh)
1634 + struct inode *dir = dentry->d_parent->d_inode;
1635 + const char *name = dentry->d_name.name;
1636 + int namelen = dentry->d_name.len;
1637 + struct buffer_head *bh2;
1638 + struct dx_root *root;
1639 + struct dx_frame frames[2], *frame;
1640 + struct dx_entry *entries;
1641 + struct ext3_dir_entry_2 *de, *de2;
1642 + char *data1, *top;
1645 + unsigned blocksize;
1646 + struct dx_hash_info hinfo;
1649 + blocksize = dir->i_sb->s_blocksize;
1650 + dxtrace(printk("Creating index\n"));
1651 + retval = ext3_journal_get_write_access(handle, bh);
1653 + ext3_std_error(dir->i_sb, retval);
1657 + root = (struct dx_root *) bh->b_data;
1659 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1660 + bh2 = ext3_append (handle, dir, &block, &retval);
1665 + data1 = bh2->b_data;
1667 + /* The 0th block becomes the root, move the dirents out */
1668 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1669 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1670 + len = ((char *) root) + blocksize - (char *) de;
1671 + memcpy (data1, de, len);
1672 + de = (struct ext3_dir_entry_2 *) data1;
1673 + top = data1 + len;
1674 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1676 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1677 + /* Initialize the root; the dot dirents already exist */
1678 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1679 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1680 + memset (&root->info, 0, sizeof(root->info));
1681 + root->info.info_length = sizeof(root->info);
1682 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1683 + entries = root->entries;
1684 + dx_set_block (entries, 1);
1685 + dx_set_count (entries, 1);
1686 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1688 + /* Initialize as for dx_probe */
1689 + hinfo.hash_version = root->info.hash_version;
1690 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1691 + ext3fs_dirhash(name, namelen, &hinfo);
1693 + frame->entries = entries;
1694 + frame->at = entries;
1697 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1698 + dx_release (frames);
1702 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1709 @@ -248,127 +1269,198 @@ static inline void ext3_set_de_type(stru
1710 * may not sleep between calling this and putting something into
1711 * the entry, as someone else might have used it while you slept.
1715 - * AKPM: the journalling code here looks wrong on the error paths
1717 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1718 struct inode *inode)
1720 struct inode *dir = dentry->d_parent->d_inode;
1721 - const char *name = dentry->d_name.name;
1722 - int namelen = dentry->d_name.len;
1723 unsigned long offset;
1724 - unsigned short rec_len;
1725 struct buffer_head * bh;
1726 - struct ext3_dir_entry_2 * de, * de1;
1727 + struct ext3_dir_entry_2 *de;
1728 struct super_block * sb;
1730 +#ifdef CONFIG_EXT3_INDEX
1731 + int dx_fallback=0;
1733 + unsigned blocksize;
1734 + unsigned nlen, rlen;
1735 + u32 block, blocks;
1740 + blocksize = sb->s_blocksize;
1741 + if (!dentry->d_name.len)
1743 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1744 +#ifdef CONFIG_EXT3_INDEX
1746 + retval = ext3_dx_add_entry(handle, dentry, inode);
1747 + if (!retval || (retval != ERR_BAD_DX_DIR))
1749 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1751 + ext3_mark_inode_dirty(handle, dir);
1754 + blocks = dir->i_size >> sb->s_blocksize_bits;
1755 + for (block = 0, offset = 0; block < blocks; block++) {
1756 + bh = ext3_bread(handle, dir, block, 0, &retval);
1759 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1760 + if (retval != -ENOSPC)
1763 +#ifdef CONFIG_EXT3_INDEX
1764 + if (blocks == 1 && !dx_fallback &&
1765 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1766 + return make_indexed_dir(handle, dentry, inode, bh);
1770 + bh = ext3_append(handle, dir, &block, &retval);
1773 - rec_len = EXT3_DIR_REC_LEN(namelen);
1775 de = (struct ext3_dir_entry_2 *) bh->b_data;
1777 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1780 - bh = ext3_bread (handle, dir,
1781 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1784 - if (dir->i_size <= offset) {
1785 - if (dir->i_size == 0) {
1790 + de->rec_len = cpu_to_le16(rlen = blocksize);
1792 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1795 - ext3_debug ("creating next block\n");
1796 +#ifdef CONFIG_EXT3_INDEX
1798 + * Returns 0 for success, or a negative error value
1800 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1801 + struct inode *inode)
1803 + struct dx_frame frames[2], *frame;
1804 + struct dx_entry *entries, *at;
1805 + struct dx_hash_info hinfo;
1806 + struct buffer_head * bh;
1807 + struct inode *dir = dentry->d_parent->d_inode;
1808 + struct super_block * sb = dir->i_sb;
1809 + struct ext3_dir_entry_2 *de;
1812 - BUFFER_TRACE(bh, "get_write_access");
1813 - ext3_journal_get_write_access(handle, bh);
1814 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1816 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1817 - dir->u.ext3_i.i_disksize =
1818 - dir->i_size = offset + sb->s_blocksize;
1819 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1820 - ext3_mark_inode_dirty(handle, dir);
1822 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1825 + entries = frame->entries;
1828 - ext3_debug ("skipping to next block\n");
1829 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1832 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1835 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1840 - if (ext3_match (namelen, name, de)) {
1843 + BUFFER_TRACE(bh, "get_write_access");
1844 + err = ext3_journal_get_write_access(handle, bh);
1846 + goto journal_error;
1848 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1849 + if (err != -ENOSPC) {
1854 + /* Block full, should compress but for now just split */
1855 + dxtrace(printk("using %u of %u node entries\n",
1856 + dx_get_count(entries), dx_get_limit(entries)));
1857 + /* Need to split index? */
1858 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1860 + unsigned icount = dx_get_count(entries);
1861 + int levels = frame - frames;
1862 + struct dx_entry *entries2;
1863 + struct dx_node *node2;
1864 + struct buffer_head *bh2;
1866 + if (levels && (dx_get_count(frames->entries) ==
1867 + dx_get_limit(frames->entries))) {
1868 + ext3_warning(sb, __FUNCTION__,
1869 + "Directory index full!\n");
1873 - if ((le32_to_cpu(de->inode) == 0 &&
1874 - le16_to_cpu(de->rec_len) >= rec_len) ||
1875 - (le16_to_cpu(de->rec_len) >=
1876 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1877 - BUFFER_TRACE(bh, "get_write_access");
1878 - ext3_journal_get_write_access(handle, bh);
1879 - /* By now the buffer is marked for journaling */
1880 - offset += le16_to_cpu(de->rec_len);
1881 - if (le32_to_cpu(de->inode)) {
1882 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1883 - EXT3_DIR_REC_LEN(de->name_len));
1885 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1886 - EXT3_DIR_REC_LEN(de->name_len));
1887 - de->rec_len = cpu_to_le16(
1888 - EXT3_DIR_REC_LEN(de->name_len));
1890 + bh2 = ext3_append (handle, dir, &newblock, &err);
1893 + node2 = (struct dx_node *)(bh2->b_data);
1894 + entries2 = node2->entries;
1895 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1896 + node2->fake.inode = 0;
1897 + BUFFER_TRACE(frame->bh, "get_write_access");
1898 + err = ext3_journal_get_write_access(handle, frame->bh);
1900 + goto journal_error;
1902 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1903 + unsigned hash2 = dx_get_hash(entries + icount1);
1904 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1906 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1907 + err = ext3_journal_get_write_access(handle,
1910 + goto journal_error;
1912 + memcpy ((char *) entries2, (char *) (entries + icount1),
1913 + icount2 * sizeof(struct dx_entry));
1914 + dx_set_count (entries, icount1);
1915 + dx_set_count (entries2, icount2);
1916 + dx_set_limit (entries2, dx_node_limit(dir));
1918 + /* Which index block gets the new entry? */
1919 + if (at - entries >= icount1) {
1920 + frame->at = at = at - entries - icount1 + entries2;
1921 + frame->entries = entries = entries2;
1922 + swap(frame->bh, bh2);
1924 - de->file_type = EXT3_FT_UNKNOWN;
1926 - de->inode = cpu_to_le32(inode->i_ino);
1927 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1930 - de->name_len = namelen;
1931 - memcpy (de->name, name, namelen);
1933 - * XXX shouldn't update any times until successful
1934 - * completion of syscall, but too many callers depend
1937 - * XXX similarly, too many callers depend on
1938 - * ext3_new_inode() setting the times, but error
1939 - * recovery deletes the inode, so the worst that can
1940 - * happen is that the times are slightly out of date
1941 - * and/or different from the directory change time.
1943 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1944 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1945 - dir->i_version = ++event;
1946 - ext3_mark_inode_dirty(handle, dir);
1947 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1948 - ext3_journal_dirty_metadata(handle, bh);
1951 + dx_insert_block (frames + 0, hash2, newblock);
1952 + dxtrace(dx_show_index ("node", frames[1].entries));
1953 + dxtrace(dx_show_index ("node",
1954 + ((struct dx_node *) bh2->b_data)->entries));
1955 + err = ext3_journal_dirty_metadata(handle, bh2);
1957 + goto journal_error;
1960 + dxtrace(printk("Creating second level index...\n"));
1961 + memcpy((char *) entries2, (char *) entries,
1962 + icount * sizeof(struct dx_entry));
1963 + dx_set_limit(entries2, dx_node_limit(dir));
1966 + dx_set_count(entries, 1);
1967 + dx_set_block(entries + 0, newblock);
1968 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1970 + /* Add new access path frame */
1971 + frame = frames + 1;
1972 + frame->at = at = at - entries + entries2;
1973 + frame->entries = entries = entries2;
1975 + err = ext3_journal_get_write_access(handle,
1978 + goto journal_error;
1980 - offset += le16_to_cpu(de->rec_len);
1981 - de = (struct ext3_dir_entry_2 *)
1982 - ((char *) de + le16_to_cpu(de->rec_len));
1983 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1987 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1990 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1995 + ext3_std_error(dir->i_sb, err);
1999 + dx_release(frames);
2005 * ext3_delete_entry deletes a directory entry by merging it with the
2006 @@ -455,9 +1547,11 @@ static int ext3_create (struct inode * d
2007 struct inode * inode;
2010 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2011 - if (IS_ERR(handle))
2012 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2013 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2014 + if (IS_ERR(handle)) {
2015 return PTR_ERR(handle);
2020 @@ -481,9 +1575,11 @@ static int ext3_mknod (struct inode * di
2021 struct inode *inode;
2024 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2025 - if (IS_ERR(handle))
2026 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2027 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2028 + if (IS_ERR(handle)) {
2029 return PTR_ERR(handle);
2034 @@ -509,9 +1605,11 @@ static int ext3_mkdir(struct inode * dir
2035 if (dir->i_nlink >= EXT3_LINK_MAX)
2038 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2039 - if (IS_ERR(handle))
2040 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2041 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2042 + if (IS_ERR(handle)) {
2043 return PTR_ERR(handle);
2048 @@ -523,7 +1621,7 @@ static int ext3_mkdir(struct inode * dir
2050 inode->i_op = &ext3_dir_inode_operations;
2051 inode->i_fop = &ext3_dir_operations;
2052 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2053 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2054 inode->i_blocks = 0;
2055 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2057 @@ -556,21 +1654,19 @@ static int ext3_mkdir(struct inode * dir
2058 inode->i_mode |= S_ISGID;
2059 ext3_mark_inode_dirty(handle, inode);
2060 err = ext3_add_entry (handle, dentry, inode);
2062 - goto out_no_entry;
2064 + inode->i_nlink = 0;
2065 + ext3_mark_inode_dirty(handle, inode);
2070 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2071 + ext3_update_dx_flag(dir);
2072 ext3_mark_inode_dirty(handle, dir);
2073 d_instantiate(dentry, inode);
2075 ext3_journal_stop(handle, dir);
2079 - inode->i_nlink = 0;
2080 - ext3_mark_inode_dirty(handle, inode);
2086 @@ -657,7 +1753,7 @@ int ext3_orphan_add(handle_t *handle, st
2090 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2091 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2094 /* Orphan handling is only valid for files with data blocks
2095 @@ -698,7 +1794,7 @@ int ext3_orphan_add(handle_t *handle, st
2096 * This is safe: on error we're going to ignore the orphan list
2097 * anyway on the next recovery. */
2099 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2100 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2102 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2103 jbd_debug(4, "orphan inode %ld will point to %d\n",
2104 @@ -716,25 +1812,26 @@ out_unlock:
2105 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2107 struct list_head *prev;
2108 + struct ext3_inode_info *ei = EXT3_I(inode);
2109 struct ext3_sb_info *sbi;
2110 unsigned long ino_next;
2111 struct ext3_iloc iloc;
2114 lock_super(inode->i_sb);
2115 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2116 + if (list_empty(&ei->i_orphan)) {
2117 unlock_super(inode->i_sb);
2121 ino_next = NEXT_ORPHAN(inode);
2122 - prev = inode->u.ext3_i.i_orphan.prev;
2123 + prev = ei->i_orphan.prev;
2124 sbi = EXT3_SB(inode->i_sb);
2126 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2128 - list_del(&inode->u.ext3_i.i_orphan);
2129 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2130 + list_del(&ei->i_orphan);
2131 + INIT_LIST_HEAD(&ei->i_orphan);
2133 /* If we're on an error path, we may not have a valid
2134 * transaction handle with which to update the orphan list on
2135 @@ -795,8 +1892,9 @@ static int ext3_rmdir (struct inode * di
2138 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2139 - if (IS_ERR(handle))
2140 + if (IS_ERR(handle)) {
2141 return PTR_ERR(handle);
2145 bh = ext3_find_entry (dentry, &de);
2146 @@ -834,7 +1932,7 @@ static int ext3_rmdir (struct inode * di
2148 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2149 ext3_mark_inode_dirty(handle, inode);
2150 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2151 + ext3_update_dx_flag(dir);
2152 ext3_mark_inode_dirty(handle, dir);
2155 @@ -852,8 +1950,9 @@ static int ext3_unlink(struct inode * di
2158 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2159 - if (IS_ERR(handle))
2160 + if (IS_ERR(handle)) {
2161 return PTR_ERR(handle);
2166 @@ -880,7 +1979,7 @@ static int ext3_unlink(struct inode * di
2169 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2170 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2171 + ext3_update_dx_flag(dir);
2172 ext3_mark_inode_dirty(handle, dir);
2174 if (!inode->i_nlink)
2175 @@ -906,9 +2005,11 @@ static int ext3_symlink (struct inode *
2176 if (l > dir->i_sb->s_blocksize)
2177 return -ENAMETOOLONG;
2179 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2180 - if (IS_ERR(handle))
2181 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2182 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2183 + if (IS_ERR(handle)) {
2184 return PTR_ERR(handle);
2189 @@ -918,7 +2019,7 @@ static int ext3_symlink (struct inode *
2193 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2194 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2195 inode->i_op = &page_symlink_inode_operations;
2196 inode->i_mapping->a_ops = &ext3_aops;
2198 @@ -927,24 +2028,22 @@ static int ext3_symlink (struct inode *
2199 * i_size in generic_commit_write().
2201 err = block_symlink(inode, symname, l);
2203 - goto out_no_entry;
2205 + ext3_dec_count(handle, inode);
2206 + ext3_mark_inode_dirty(handle, inode);
2211 inode->i_op = &ext3_fast_symlink_inode_operations;
2212 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2213 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2214 inode->i_size = l-1;
2216 - inode->u.ext3_i.i_disksize = inode->i_size;
2217 + EXT3_I(inode)->i_disksize = inode->i_size;
2218 err = ext3_add_nondir(handle, dentry, inode);
2220 ext3_journal_stop(handle, dir);
2224 - ext3_dec_count(handle, inode);
2225 - ext3_mark_inode_dirty(handle, inode);
2230 static int ext3_link (struct dentry * old_dentry,
2231 @@ -957,12 +2056,15 @@ static int ext3_link (struct dentry * ol
2232 if (S_ISDIR(inode->i_mode))
2235 - if (inode->i_nlink >= EXT3_LINK_MAX)
2236 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2240 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2241 - if (IS_ERR(handle))
2242 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2243 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2244 + if (IS_ERR(handle)) {
2245 return PTR_ERR(handle);
2250 @@ -995,9 +2097,11 @@ static int ext3_rename (struct inode * o
2252 old_bh = new_bh = dir_bh = NULL;
2254 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2255 - if (IS_ERR(handle))
2256 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2257 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2258 + if (IS_ERR(handle)) {
2259 return PTR_ERR(handle);
2262 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2264 @@ -1070,14 +2174,37 @@ static int ext3_rename (struct inode * o
2268 - ext3_delete_entry(handle, old_dir, old_de, old_bh);
2269 + if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2270 + old_de->name_len != old_dentry->d_name.len ||
2271 + strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2272 + (retval = ext3_delete_entry(handle, old_dir,
2273 + old_de, old_bh)) == -ENOENT) {
2274 + /* old_de could have moved from under us during htree split, so
2275 + * make sure that we are deleting the right entry. We might
2276 + * also be pointing to a stale entry in the unused part of
2277 + * old_bh so just checking inum and the name isn't enough. */
2278 + struct buffer_head *old_bh2;
2279 + struct ext3_dir_entry_2 *old_de2;
2281 + old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2283 + retval = ext3_delete_entry(handle, old_dir,
2284 + old_de2, old_bh2);
2289 + ext3_warning(old_dir->i_sb, "ext3_rename",
2290 + "Deleting old file (%lu), %d, error=%d",
2291 + old_dir->i_ino, old_dir->i_nlink, retval);
2295 new_inode->i_nlink--;
2296 new_inode->i_ctime = CURRENT_TIME;
2298 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2299 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2300 + ext3_update_dx_flag(old_dir);
2302 BUFFER_TRACE(dir_bh, "get_write_access");
2303 ext3_journal_get_write_access(handle, dir_bh);
2304 @@ -1089,7 +2212,7 @@ static int ext3_rename (struct inode * o
2305 new_inode->i_nlink--;
2308 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2309 + ext3_update_dx_flag(new_dir);
2310 ext3_mark_inode_dirty(handle, new_dir);
2313 --- linux-2.4.22-ac1/fs/ext3/super.c~ext3-htree-2.4.22-rh 2003-09-25 14:39:01.000000000 +0400
2314 +++ linux-2.4.22-ac1-alexey/fs/ext3/super.c 2003-09-25 14:55:12.000000000 +0400
2315 @@ -714,6 +714,7 @@ static int ext3_setup_super(struct super
2316 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2317 ext3_update_dynamic_rev(sb);
2318 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2320 ext3_commit_super (sb, es, 1);
2321 if (test_opt (sb, DEBUG))
2323 @@ -724,6 +725,7 @@ static int ext3_setup_super(struct super
2324 EXT3_BLOCKS_PER_GROUP(sb),
2325 EXT3_INODES_PER_GROUP(sb),
2328 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2329 bdevname(sb->s_dev));
2330 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2331 @@ -897,6 +899,7 @@ static loff_t ext3_max_size(int bits)
2336 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2339 @@ -1073,6 +1076,9 @@ struct super_block * ext3_read_super (st
2340 sbi->s_mount_state = le16_to_cpu(es->s_state);
2341 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2342 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2343 + for (i=0; i < 4; i++)
2344 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2345 + sbi->s_def_hash_version = es->s_def_hash_version;
2347 if (sbi->s_blocks_per_group > blocksize * 8) {
2349 @@ -1846,6 +1852,7 @@ static void __exit exit_ext3_fs(void)
2350 unregister_filesystem(&ext3_fs_type);
2353 +EXPORT_SYMBOL(ext3_force_commit);
2354 EXPORT_SYMBOL(ext3_bread);
2356 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2357 --- linux-2.4.22-ac1/include/linux/ext3_fs.h~ext3-htree-2.4.22-rh 2003-09-25 14:16:29.000000000 +0400
2358 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_fs.h 2003-09-25 14:58:30.000000000 +0400
2360 #define EXT3FS_VERSION "2.4-0.9.19"
2363 + * Always enable hashed directories
2365 +#define CONFIG_EXT3_INDEX
2371 @@ -440,8 +445,11 @@ struct ext3_super_block {
2372 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2373 __u32 s_journal_dev; /* device number of journal file */
2374 __u32 s_last_orphan; /* start of list of inodes to delete */
2376 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2377 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2378 + __u8 s_def_hash_version; /* Default hash version to use */
2379 + __u8 s_reserved_char_pad;
2380 + __u16 s_reserved_word_pad;
2381 + __u32 s_reserved[192]; /* Padding to the end of the block */
2385 @@ -578,9 +586,46 @@ struct ext3_dir_entry_2 {
2386 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2387 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2390 + * Hash Tree Directory indexing
2391 + * (c) Daniel Phillips, 2001
2394 +#ifdef CONFIG_EXT3_INDEX
2395 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2396 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2397 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2398 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2399 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2401 + #define is_dx(dir) 0
2402 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2403 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2406 +/* Legal values for the dx_root hash_version field: */
2408 +#define DX_HASH_LEGACY 0
2409 +#define DX_HASH_HALF_MD4 1
2410 +#define DX_HASH_TEA 2
2412 +/* hash info structure used by the directory hash */
2413 +struct dx_hash_info
2423 + * Control parameters used by ext3_htree_next_block
2425 +#define HASH_NB_ALWAYS 1
2429 * Describe an inode's exact location on disk and in memory
2432 @@ -590,6 +635,27 @@ struct ext3_iloc
2433 unsigned long block_group;
2438 + * This structure is stuffed into the struct file's private_data field
2439 + * for directories. It is where we put information so that we can do
2440 + * readdir operations in hash tree order.
2442 +struct dir_private_info {
2444 + rb_node_t *curr_node;
2445 + struct fname *extra_fname;
2448 + __u32 curr_minor_hash;
2453 + * Special error return code only used by dx_probe() and its callers.
2455 +#define ERR_BAD_DX_DIR -75000
2458 * Function prototypes
2460 @@ -617,11 +683,20 @@ extern struct ext3_group_desc * ext3_get
2463 extern int ext3_check_dir_entry(const char *, struct inode *,
2464 - struct ext3_dir_entry_2 *, struct buffer_head *,
2466 + struct ext3_dir_entry_2 *,
2467 + struct buffer_head *, unsigned long);
2468 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2470 + struct ext3_dir_entry_2 *dirent);
2471 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2474 extern int ext3_sync_file (struct file *, struct dentry *, int);
2477 +extern int ext3fs_dirhash(const char *name, int len, struct
2478 + dx_hash_info *hinfo);
2481 extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2482 extern void ext3_free_inode (handle_t *, struct inode *);
2483 @@ -655,6 +730,8 @@ extern int ext3_ioctl (struct inode *, s
2485 extern int ext3_orphan_add(handle_t *, struct inode *);
2486 extern int ext3_orphan_del(handle_t *, struct inode *);
2487 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2488 + __u32 start_minor_hash, __u32 *next_hash);
2491 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2492 --- linux-2.4.22-ac1/include/linux/ext3_fs_sb.h~ext3-htree-2.4.22-rh 2003-09-25 14:16:34.000000000 +0400
2493 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_fs_sb.h 2003-09-25 14:55:12.000000000 +0400
2494 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2497 u32 s_next_generation;
2498 + u32 s_hash_seed[4];
2499 + int s_def_hash_version;
2501 unsigned long s_dir_count;
2503 --- linux-2.4.22-ac1/include/linux/ext3_jbd.h~ext3-htree-2.4.22-rh 2003-06-13 18:51:38.000000000 +0400
2504 +++ linux-2.4.22-ac1-alexey/include/linux/ext3_jbd.h 2003-09-25 14:55:12.000000000 +0400
2505 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2507 #define EXT3_RESERVE_TRANS_BLOCKS 12U
2509 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2512 ext3_mark_iloc_dirty(handle_t *handle,
2513 struct inode *inode,
2514 --- linux-2.4.22-ac1/include/linux/rbtree.h~ext3-htree-2.4.22-rh 2001-11-22 22:46:18.000000000 +0300
2515 +++ linux-2.4.22-ac1-alexey/include/linux/rbtree.h 2003-09-25 14:55:12.000000000 +0400
2516 @@ -120,6 +120,8 @@ rb_root_t;
2518 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2519 extern void rb_erase(rb_node_t *, rb_root_t *);
2520 +extern rb_node_t *rb_get_first(rb_root_t *root);
2521 +extern rb_node_t *rb_get_next(rb_node_t *n);
2523 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2525 --- linux-2.4.22-ac1/lib/rbtree.c~ext3-htree-2.4.22-rh 2002-08-03 04:39:46.000000000 +0400
2526 +++ linux-2.4.22-ac1-alexey/lib/rbtree.c 2003-09-25 14:55:12.000000000 +0400
2528 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2532 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2535 #include <linux/rbtree.h>
2536 @@ -294,3 +296,43 @@ void rb_erase(rb_node_t * node, rb_root_
2537 __rb_erase_color(child, parent, root);
2539 EXPORT_SYMBOL(rb_erase);
2542 + * This function returns the first node (in sort order) of the tree.
2544 +rb_node_t *rb_get_first(rb_root_t *root)
2548 + n = root->rb_node;
2551 + while (n->rb_left)
2555 +EXPORT_SYMBOL(rb_get_first);
2558 + * Given a node, this function will return the next node in the tree.
2560 +rb_node_t *rb_get_next(rb_node_t *n)
2562 + rb_node_t *parent;
2564 + if (n->rb_right) {
2566 + while (n->rb_left)
2570 + while ((parent = n->rb_parent)) {
2571 + if (n == parent->rb_left)
2578 +EXPORT_SYMBOL(rb_get_next);