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/fs/ext3/dir.c
15 ===================================================================
16 --- linux-2.4.21.orig/fs/ext3/dir.c 2001-11-09 17:25:04.000000000 -0500
17 +++ linux-2.4.21/fs/ext3/dir.c 2004-09-16 19:41:03.000000000 -0400
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_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_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_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/fs/ext3/file.c
364 ===================================================================
365 --- linux-2.4.21.orig/fs/ext3/file.c 2004-09-11 10:16:28.000000000 -0400
366 +++ linux-2.4.21/fs/ext3/file.c 2004-09-16 19:40:16.000000000 -0400
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/fs/ext3/hash.c
378 ===================================================================
379 --- linux-2.4.21.orig/fs/ext3/hash.c 1969-12-31 19:00:00.000000000 -0500
380 +++ linux-2.4.21/fs/ext3/hash.c 2004-09-16 19:40:16.000000000 -0400
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/fs/ext3/Makefile
598 ===================================================================
599 --- linux-2.4.21.orig/fs/ext3/Makefile 2004-09-16 19:21:00.000000000 -0400
600 +++ linux-2.4.21/fs/ext3/Makefile 2004-09-16 19:40:16.000000000 -0400
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/fs/ext3/namei.c
611 ===================================================================
612 --- linux-2.4.21.orig/fs/ext3/namei.c 2004-09-11 10:16:28.000000000 -0400
613 +++ linux-2.4.21/fs/ext3/namei.c 2004-09-16 19:40:16.000000000 -0400
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,74 @@
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 + /* NFS may look up ".." - look at dx_root directory block */
1353 + if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
1354 + if (!(frame = dx_probe(dentry, 0, &hinfo, frames, err)))
1358 + frame->bh = NULL; /* for dx_release() */
1359 + frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
1360 + dx_set_block(frame->at, 0); /* dx_root block is 0 */
1362 + hash = hinfo.hash;
1364 + block = dx_get_block(frame->at);
1365 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1367 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1368 + top = (struct ext3_dir_entry_2 *)((char *)de + sb->s_blocksize -
1369 + EXT3_DIR_REC_LEN(0));
1370 + for (; de < top; de = ext3_next_entry(de))
1371 + if (ext3_match (namelen, name, de)) {
1372 + if (!ext3_check_dir_entry("ext3_find_entry",
1374 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1375 + +((char *)de - bh->b_data))) {
1380 + dx_release (frames);
1384 + /* Check to see if we should continue to search */
1385 + retval = ext3_htree_next_block(dir, hash, frame,
1387 + if (retval == -1) {
1388 + ext3_warning(sb, __FUNCTION__,
1389 + "error reading index page in directory #%lu",
1393 + } while (retval == 1);
1397 + dxtrace(printk("%s not found\n", name));
1398 + dx_release (frames);
1403 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1405 struct inode * inode;
1408 inode = iget(dir->i_sb, ino);
1412 return ERR_PTR(-EACCES);
1415 d_add(dentry, inode);
1417 @@ -239,6 +965,301 @@
1418 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1421 +#ifdef CONFIG_EXT3_INDEX
1422 +static struct ext3_dir_entry_2 *
1423 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1425 + unsigned rec_len = 0;
1428 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1429 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1430 + memcpy (to, de, rec_len);
1431 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1436 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1439 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1441 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1442 + unsigned rec_len = 0;
1445 + while ((char*)de < base + size) {
1446 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1447 + le16_to_cpu(de->rec_len));
1448 + if (de->inode && de->name_len) {
1449 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1451 + memmove(to, de, rec_len);
1452 + to->rec_len = cpu_to_le16(rec_len);
1454 + to = (struct ext3_dir_entry_2 *)((char *) to + rec_len);
1461 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1462 + struct buffer_head **bh,struct dx_frame *frame,
1463 + struct dx_hash_info *hinfo, int *error)
1465 + unsigned blocksize = dir->i_sb->s_blocksize;
1466 + unsigned count, continued;
1467 + struct buffer_head *bh2;
1470 + struct dx_map_entry *map;
1471 + char *data1 = (*bh)->b_data, *data2;
1473 + struct ext3_dir_entry_2 *de = NULL, *de2;
1476 + bh2 = ext3_append (handle, dir, &newblock, error);
1483 + BUFFER_TRACE(*bh, "get_write_access");
1484 + err = ext3_journal_get_write_access(handle, *bh);
1490 + ext3_std_error(dir->i_sb, err);
1493 + BUFFER_TRACE(frame->bh, "get_write_access");
1494 + err = ext3_journal_get_write_access(handle, frame->bh);
1496 + goto journal_error;
1498 + data2 = bh2->b_data;
1500 + /* create map in the end of data2 block */
1501 + map = (struct dx_map_entry *) (data2 + blocksize);
1502 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1503 + blocksize, hinfo, map);
1505 + split = count/2; // need to adjust to actual middle
1506 + dx_sort_map (map, count);
1507 + hash2 = map[split].hash;
1508 + continued = hash2 == map[split - 1].hash;
1509 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1510 + dx_get_block(frame->at), hash2, split, count-split));
1512 + /* Fancy dance to stay within two buffers */
1513 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1514 + de = dx_pack_dirents(data1,blocksize);
1515 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1516 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1517 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1518 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1520 + /* Which block gets the new entry? */
1521 + if (hinfo->hash >= hash2)
1526 + dx_insert_block (frame, hash2 + continued, newblock);
1527 + err = ext3_journal_dirty_metadata (handle, bh2);
1529 + goto journal_error;
1530 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1532 + goto journal_error;
1534 + dxtrace(dx_show_index ("frame", frame->entries));
1542 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1543 + * it points to a directory entry which is guaranteed to be large
1544 + * enough for new directory entry. If de is NULL, then
1545 + * add_dirent_to_buf will attempt search the directory block for
1546 + * space. It will return -ENOSPC if no space is available, and -EIO
1547 + * and -EEXIST if directory entry already exists.
1549 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1550 + * all other cases bh is released.
1552 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1553 + struct inode *inode, struct ext3_dir_entry_2 *de,
1554 + struct buffer_head * bh)
1556 + struct inode *dir = dentry->d_parent->d_inode;
1557 + const char *name = dentry->d_name.name;
1558 + int namelen = dentry->d_name.len;
1559 + unsigned long offset = 0;
1560 + unsigned short reclen;
1561 + int nlen, rlen, err;
1564 + reclen = EXT3_DIR_REC_LEN(namelen);
1566 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1567 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1568 + while ((char *) de <= top) {
1569 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1574 + if (ext3_match (namelen, name, de)) {
1578 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1579 + rlen = le16_to_cpu(de->rec_len);
1580 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1582 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1585 + if ((char *) de > top)
1588 + BUFFER_TRACE(bh, "get_write_access");
1589 + err = ext3_journal_get_write_access(handle, bh);
1591 + ext3_std_error(dir->i_sb, err);
1596 + /* By now the buffer is marked for journaling */
1597 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1598 + rlen = le16_to_cpu(de->rec_len);
1600 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1601 + de1->rec_len = cpu_to_le16(rlen - nlen);
1602 + de->rec_len = cpu_to_le16(nlen);
1605 + de->file_type = EXT3_FT_UNKNOWN;
1607 + de->inode = cpu_to_le32(inode->i_ino);
1608 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1611 + de->name_len = namelen;
1612 + memcpy (de->name, name, namelen);
1614 + * XXX shouldn't update any times until successful
1615 + * completion of syscall, but too many callers depend
1618 + * XXX similarly, too many callers depend on
1619 + * ext3_new_inode() setting the times, but error
1620 + * recovery deletes the inode, so the worst that can
1621 + * happen is that the times are slightly out of date
1622 + * and/or different from the directory change time.
1624 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1625 + ext3_update_dx_flag(dir);
1626 + dir->i_version = ++event;
1627 + ext3_mark_inode_dirty(handle, dir);
1628 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1629 + err = ext3_journal_dirty_metadata(handle, bh);
1631 + ext3_std_error(dir->i_sb, err);
1636 +#ifdef CONFIG_EXT3_INDEX
1638 + * This converts a one block unindexed directory to a 3 block indexed
1639 + * directory, and adds the dentry to the indexed directory.
1641 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1642 + struct inode *inode, struct buffer_head *bh)
1644 + struct inode *dir = dentry->d_parent->d_inode;
1645 + const char *name = dentry->d_name.name;
1646 + int namelen = dentry->d_name.len;
1647 + struct buffer_head *bh2;
1648 + struct dx_root *root;
1649 + struct dx_frame frames[2], *frame;
1650 + struct dx_entry *entries;
1651 + struct ext3_dir_entry_2 *de, *de2;
1652 + char *data1, *top;
1655 + unsigned blocksize;
1656 + struct dx_hash_info hinfo;
1659 + blocksize = dir->i_sb->s_blocksize;
1660 + dxtrace(printk("Creating index\n"));
1661 + retval = ext3_journal_get_write_access(handle, bh);
1663 + ext3_std_error(dir->i_sb, retval);
1667 + root = (struct dx_root *) bh->b_data;
1669 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1670 + bh2 = ext3_append (handle, dir, &block, &retval);
1675 + data1 = bh2->b_data;
1677 + /* The 0th block becomes the root, move the dirents out */
1678 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1679 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1680 + len = ((char *) root) + blocksize - (char *) de;
1681 + memcpy (data1, de, len);
1682 + de = (struct ext3_dir_entry_2 *) data1;
1683 + top = data1 + len;
1684 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1686 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1687 + /* Initialize the root; the dot dirents already exist */
1688 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1689 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1690 + memset (&root->info, 0, sizeof(root->info));
1691 + root->info.info_length = sizeof(root->info);
1692 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1693 + entries = root->entries;
1694 + dx_set_block (entries, 1);
1695 + dx_set_count (entries, 1);
1696 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1698 + /* Initialize as for dx_probe */
1699 + hinfo.hash_version = root->info.hash_version;
1700 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1701 + ext3fs_dirhash(name, namelen, &hinfo);
1703 + frame->entries = entries;
1704 + frame->at = entries;
1707 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1708 + dx_release (frames);
1712 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1719 @@ -249,127 +1270,198 @@
1720 * may not sleep between calling this and putting something into
1721 * the entry, as someone else might have used it while you slept.
1725 - * AKPM: the journalling code here looks wrong on the error paths
1727 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1728 struct inode *inode)
1730 struct inode *dir = dentry->d_parent->d_inode;
1731 - const char *name = dentry->d_name.name;
1732 - int namelen = dentry->d_name.len;
1733 unsigned long offset;
1734 - unsigned short rec_len;
1735 struct buffer_head * bh;
1736 - struct ext3_dir_entry_2 * de, * de1;
1737 + struct ext3_dir_entry_2 *de;
1738 struct super_block * sb;
1740 +#ifdef CONFIG_EXT3_INDEX
1741 + int dx_fallback=0;
1743 + unsigned blocksize;
1744 + unsigned nlen, rlen;
1745 + u32 block, blocks;
1750 + blocksize = sb->s_blocksize;
1751 + if (!dentry->d_name.len)
1753 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1754 +#ifdef CONFIG_EXT3_INDEX
1756 + retval = ext3_dx_add_entry(handle, dentry, inode);
1757 + if (!retval || (retval != ERR_BAD_DX_DIR))
1759 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1761 + ext3_mark_inode_dirty(handle, dir);
1764 + blocks = dir->i_size >> sb->s_blocksize_bits;
1765 + for (block = 0, offset = 0; block < blocks; block++) {
1766 + bh = ext3_bread(handle, dir, block, 0, &retval);
1769 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1770 + if (retval != -ENOSPC)
1773 +#ifdef CONFIG_EXT3_INDEX
1774 + if (blocks == 1 && !dx_fallback &&
1775 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1776 + return make_indexed_dir(handle, dentry, inode, bh);
1780 + bh = ext3_append(handle, dir, &block, &retval);
1783 - rec_len = EXT3_DIR_REC_LEN(namelen);
1785 de = (struct ext3_dir_entry_2 *) bh->b_data;
1787 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1790 - bh = ext3_bread (handle, dir,
1791 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1794 - if (dir->i_size <= offset) {
1795 - if (dir->i_size == 0) {
1800 + de->rec_len = cpu_to_le16(rlen = blocksize);
1802 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1805 - ext3_debug ("creating next block\n");
1806 +#ifdef CONFIG_EXT3_INDEX
1808 + * Returns 0 for success, or a negative error value
1810 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1811 + struct inode *inode)
1813 + struct dx_frame frames[2], *frame;
1814 + struct dx_entry *entries, *at;
1815 + struct dx_hash_info hinfo;
1816 + struct buffer_head * bh;
1817 + struct inode *dir = dentry->d_parent->d_inode;
1818 + struct super_block * sb = dir->i_sb;
1819 + struct ext3_dir_entry_2 *de;
1822 - BUFFER_TRACE(bh, "get_write_access");
1823 - ext3_journal_get_write_access(handle, bh);
1824 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1826 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1827 - dir->u.ext3_i.i_disksize =
1828 - dir->i_size = offset + sb->s_blocksize;
1829 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1830 - ext3_mark_inode_dirty(handle, dir);
1832 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1835 + entries = frame->entries;
1838 - ext3_debug ("skipping to next block\n");
1839 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1842 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1845 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1850 - if (ext3_match (namelen, name, de)) {
1853 + BUFFER_TRACE(bh, "get_write_access");
1854 + err = ext3_journal_get_write_access(handle, bh);
1856 + goto journal_error;
1858 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1859 + if (err != -ENOSPC) {
1864 + /* Block full, should compress but for now just split */
1865 + dxtrace(printk("using %u of %u node entries\n",
1866 + dx_get_count(entries), dx_get_limit(entries)));
1867 + /* Need to split index? */
1868 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1870 + unsigned icount = dx_get_count(entries);
1871 + int levels = frame - frames;
1872 + struct dx_entry *entries2;
1873 + struct dx_node *node2;
1874 + struct buffer_head *bh2;
1876 + if (levels && (dx_get_count(frames->entries) ==
1877 + dx_get_limit(frames->entries))) {
1878 + ext3_warning(sb, __FUNCTION__,
1879 + "Directory index full!\n");
1883 - if ((le32_to_cpu(de->inode) == 0 &&
1884 - le16_to_cpu(de->rec_len) >= rec_len) ||
1885 - (le16_to_cpu(de->rec_len) >=
1886 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1887 - BUFFER_TRACE(bh, "get_write_access");
1888 - ext3_journal_get_write_access(handle, bh);
1889 - /* By now the buffer is marked for journaling */
1890 - offset += le16_to_cpu(de->rec_len);
1891 - if (le32_to_cpu(de->inode)) {
1892 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1893 - EXT3_DIR_REC_LEN(de->name_len));
1895 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1896 - EXT3_DIR_REC_LEN(de->name_len));
1897 - de->rec_len = cpu_to_le16(
1898 - EXT3_DIR_REC_LEN(de->name_len));
1900 + bh2 = ext3_append (handle, dir, &newblock, &err);
1903 + node2 = (struct dx_node *)(bh2->b_data);
1904 + entries2 = node2->entries;
1905 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1906 + node2->fake.inode = 0;
1907 + BUFFER_TRACE(frame->bh, "get_write_access");
1908 + err = ext3_journal_get_write_access(handle, frame->bh);
1910 + goto journal_error;
1912 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1913 + unsigned hash2 = dx_get_hash(entries + icount1);
1914 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1916 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1917 + err = ext3_journal_get_write_access(handle,
1920 + goto journal_error;
1922 + memcpy ((char *) entries2, (char *) (entries + icount1),
1923 + icount2 * sizeof(struct dx_entry));
1924 + dx_set_count (entries, icount1);
1925 + dx_set_count (entries2, icount2);
1926 + dx_set_limit (entries2, dx_node_limit(dir));
1928 + /* Which index block gets the new entry? */
1929 + if (at - entries >= icount1) {
1930 + frame->at = at = at - entries - icount1 + entries2;
1931 + frame->entries = entries = entries2;
1932 + swap(frame->bh, bh2);
1934 - de->file_type = EXT3_FT_UNKNOWN;
1936 - de->inode = cpu_to_le32(inode->i_ino);
1937 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1940 - de->name_len = namelen;
1941 - memcpy (de->name, name, namelen);
1943 - * XXX shouldn't update any times until successful
1944 - * completion of syscall, but too many callers depend
1947 - * XXX similarly, too many callers depend on
1948 - * ext3_new_inode() setting the times, but error
1949 - * recovery deletes the inode, so the worst that can
1950 - * happen is that the times are slightly out of date
1951 - * and/or different from the directory change time.
1953 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1954 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1955 - dir->i_version = ++event;
1956 - ext3_mark_inode_dirty(handle, dir);
1957 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1958 - ext3_journal_dirty_metadata(handle, bh);
1961 + dx_insert_block (frames + 0, hash2, newblock);
1962 + dxtrace(dx_show_index ("node", frames[1].entries));
1963 + dxtrace(dx_show_index ("node",
1964 + ((struct dx_node *) bh2->b_data)->entries));
1965 + err = ext3_journal_dirty_metadata(handle, bh2);
1967 + goto journal_error;
1970 + dxtrace(printk("Creating second level index...\n"));
1971 + memcpy((char *) entries2, (char *) entries,
1972 + icount * sizeof(struct dx_entry));
1973 + dx_set_limit(entries2, dx_node_limit(dir));
1976 + dx_set_count(entries, 1);
1977 + dx_set_block(entries + 0, newblock);
1978 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1980 + /* Add new access path frame */
1981 + frame = frames + 1;
1982 + frame->at = at = at - entries + entries2;
1983 + frame->entries = entries = entries2;
1985 + err = ext3_journal_get_write_access(handle,
1988 + goto journal_error;
1990 - offset += le16_to_cpu(de->rec_len);
1991 - de = (struct ext3_dir_entry_2 *)
1992 - ((char *) de + le16_to_cpu(de->rec_len));
1993 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1997 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2000 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2005 + ext3_std_error(dir->i_sb, err);
2009 + dx_release(frames);
2015 * ext3_delete_entry deletes a directory entry by merging it with the
2016 @@ -456,9 +1548,11 @@
2017 struct inode * inode;
2020 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2021 - if (IS_ERR(handle))
2022 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2023 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2024 + if (IS_ERR(handle)) {
2025 return PTR_ERR(handle);
2030 @@ -482,9 +1576,11 @@
2031 struct inode *inode;
2034 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2035 - if (IS_ERR(handle))
2036 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2037 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2038 + if (IS_ERR(handle)) {
2039 return PTR_ERR(handle);
2044 @@ -513,9 +1609,11 @@
2045 if (dir->i_nlink >= EXT3_LINK_MAX)
2048 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2049 - if (IS_ERR(handle))
2050 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2051 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2052 + if (IS_ERR(handle)) {
2053 return PTR_ERR(handle);
2058 @@ -527,7 +1625,7 @@
2060 inode->i_op = &ext3_dir_inode_operations;
2061 inode->i_fop = &ext3_dir_operations;
2062 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2063 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2064 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2066 inode->i_nlink--; /* is this nlink == 0? */
2067 @@ -556,21 +1654,19 @@
2069 ext3_mark_inode_dirty(handle, inode);
2070 err = ext3_add_entry (handle, dentry, inode);
2072 - goto out_no_entry;
2074 + inode->i_nlink = 0;
2075 + ext3_mark_inode_dirty(handle, inode);
2080 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2081 + ext3_update_dx_flag(dir);
2082 ext3_mark_inode_dirty(handle, dir);
2083 d_instantiate(dentry, inode);
2085 ext3_journal_stop(handle, dir);
2089 - inode->i_nlink = 0;
2090 - ext3_mark_inode_dirty(handle, inode);
2096 @@ -657,7 +1753,7 @@
2100 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2101 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2104 /* Orphan handling is only valid for files with data blocks
2105 @@ -698,7 +1794,7 @@
2106 * This is safe: on error we're going to ignore the orphan list
2107 * anyway on the next recovery. */
2109 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2110 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2112 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2113 jbd_debug(4, "orphan inode %ld will point to %d\n",
2114 @@ -716,25 +1812,26 @@
2115 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2117 struct list_head *prev;
2118 + struct ext3_inode_info *ei = EXT3_I(inode);
2119 struct ext3_sb_info *sbi;
2120 unsigned long ino_next;
2121 struct ext3_iloc iloc;
2124 lock_super(inode->i_sb);
2125 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2126 + if (list_empty(&ei->i_orphan)) {
2127 unlock_super(inode->i_sb);
2131 ino_next = NEXT_ORPHAN(inode);
2132 - prev = inode->u.ext3_i.i_orphan.prev;
2133 + prev = ei->i_orphan.prev;
2134 sbi = EXT3_SB(inode->i_sb);
2136 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2138 - list_del(&inode->u.ext3_i.i_orphan);
2139 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2140 + list_del(&ei->i_orphan);
2141 + INIT_LIST_HEAD(&ei->i_orphan);
2143 /* If we're on an error path, we may not have a valid
2144 * transaction handle with which to update the orphan list on
2145 @@ -795,8 +1892,9 @@
2148 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2149 - if (IS_ERR(handle))
2150 + if (IS_ERR(handle)) {
2151 return PTR_ERR(handle);
2155 bh = ext3_find_entry (dentry, &de);
2156 @@ -834,7 +1932,7 @@
2158 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2159 ext3_mark_inode_dirty(handle, inode);
2160 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2161 + ext3_update_dx_flag(dir);
2162 ext3_mark_inode_dirty(handle, dir);
2165 @@ -852,8 +1950,9 @@
2168 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2169 - if (IS_ERR(handle))
2170 + if (IS_ERR(handle)) {
2171 return PTR_ERR(handle);
2176 @@ -880,7 +1979,7 @@
2179 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2180 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2181 + ext3_update_dx_flag(dir);
2182 ext3_mark_inode_dirty(handle, dir);
2184 if (!inode->i_nlink)
2185 @@ -906,9 +2005,11 @@
2186 if (l > dir->i_sb->s_blocksize)
2187 return -ENAMETOOLONG;
2189 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2190 - if (IS_ERR(handle))
2191 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2192 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2193 + if (IS_ERR(handle)) {
2194 return PTR_ERR(handle);
2199 @@ -918,7 +2019,7 @@
2203 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2204 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2205 inode->i_op = &ext3_symlink_inode_operations;
2206 inode->i_mapping->a_ops = &ext3_aops;
2208 @@ -927,24 +2028,22 @@
2209 * i_size in generic_commit_write().
2211 err = block_symlink(inode, symname, l);
2213 - goto out_no_entry;
2215 + ext3_dec_count(handle, inode);
2216 + ext3_mark_inode_dirty(handle, inode);
2221 inode->i_op = &ext3_fast_symlink_inode_operations;
2222 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2223 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2224 inode->i_size = l-1;
2226 - inode->u.ext3_i.i_disksize = inode->i_size;
2227 + EXT3_I(inode)->i_disksize = inode->i_size;
2228 err = ext3_add_nondir(handle, dentry, inode);
2230 ext3_journal_stop(handle, dir);
2234 - ext3_dec_count(handle, inode);
2235 - ext3_mark_inode_dirty(handle, inode);
2240 static int ext3_link (struct dentry * old_dentry,
2241 @@ -957,12 +2056,15 @@
2242 if (S_ISDIR(inode->i_mode))
2245 - if (inode->i_nlink >= EXT3_LINK_MAX)
2246 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2250 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2251 - if (IS_ERR(handle))
2252 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2253 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2254 + if (IS_ERR(handle)) {
2255 return PTR_ERR(handle);
2260 @@ -995,9 +2097,11 @@
2262 old_bh = new_bh = dir_bh = NULL;
2264 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2265 - if (IS_ERR(handle))
2266 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2267 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2268 + if (IS_ERR(handle)) {
2269 return PTR_ERR(handle);
2272 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2274 @@ -1070,14 +2174,37 @@
2278 - ext3_delete_entry(handle, old_dir, old_de, old_bh);
2279 + if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2280 + old_de->name_len != old_dentry->d_name.len ||
2281 + strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2282 + (retval = ext3_delete_entry(handle, old_dir,
2283 + old_de, old_bh)) == -ENOENT) {
2284 + /* old_de could have moved from under us during htree split, so
2285 + * make sure that we are deleting the right entry. We might
2286 + * also be pointing to a stale entry in the unused part of
2287 + * old_bh so just checking inum and the name isn't enough. */
2288 + struct buffer_head *old_bh2;
2289 + struct ext3_dir_entry_2 *old_de2;
2291 + old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2293 + retval = ext3_delete_entry(handle, old_dir,
2294 + old_de2, old_bh2);
2299 + ext3_warning(old_dir->i_sb, "ext3_rename",
2300 + "Deleting old file (%lu), %d, error=%d",
2301 + old_dir->i_ino, old_dir->i_nlink, retval);
2305 new_inode->i_nlink--;
2306 new_inode->i_ctime = CURRENT_TIME;
2308 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2309 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2310 + ext3_update_dx_flag(old_dir);
2312 BUFFER_TRACE(dir_bh, "get_write_access");
2313 ext3_journal_get_write_access(handle, dir_bh);
2314 @@ -1089,7 +2216,7 @@
2315 new_inode->i_nlink--;
2318 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2319 + ext3_update_dx_flag(new_dir);
2320 ext3_mark_inode_dirty(handle, new_dir);
2323 Index: linux-2.4.21/fs/ext3/super.c
2324 ===================================================================
2325 --- linux-2.4.21.orig/fs/ext3/super.c 2004-09-16 19:21:00.000000000 -0400
2326 +++ linux-2.4.21/fs/ext3/super.c 2004-09-16 19:40:16.000000000 -0400
2328 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2329 ext3_update_dynamic_rev(sb);
2330 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2332 ext3_commit_super (sb, es, 1);
2333 if (test_opt (sb, DEBUG))
2336 EXT3_BLOCKS_PER_GROUP(sb),
2337 EXT3_INODES_PER_GROUP(sb),
2340 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2341 bdevname(sb->s_dev));
2342 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2348 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2351 @@ -1146,6 +1149,9 @@
2352 sbi->s_mount_state = le16_to_cpu(es->s_state);
2353 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2354 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2355 + for (i=0; i < 4; i++)
2356 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2357 + sbi->s_def_hash_version = es->s_def_hash_version;
2359 if (sbi->s_blocks_per_group > blocksize * 8) {
2361 @@ -1938,6 +1944,7 @@
2362 unregister_filesystem(&ext3_fs_type);
2365 +EXPORT_SYMBOL(ext3_force_commit);
2366 EXPORT_SYMBOL(ext3_bread);
2368 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2369 Index: linux-2.4.21/include/linux/ext3_fs.h
2370 ===================================================================
2371 --- linux-2.4.21.orig/include/linux/ext3_fs.h 2004-09-11 10:16:28.000000000 -0400
2372 +++ linux-2.4.21/include/linux/ext3_fs.h 2004-09-16 19:40:16.000000000 -0400
2374 #define EXT3FS_VERSION "2.4-0.9.19"
2377 + * Always enable hashed directories
2379 +#define CONFIG_EXT3_INDEX
2385 @@ -415,8 +420,11 @@
2386 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2387 __u32 s_journal_dev; /* device number of journal file */
2388 __u32 s_last_orphan; /* start of list of inodes to delete */
2390 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2391 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2392 + __u8 s_def_hash_version; /* Default hash version to use */
2393 + __u8 s_reserved_char_pad;
2394 + __u16 s_reserved_word_pad;
2395 + __u32 s_reserved[192]; /* Padding to the end of the block */
2399 @@ -553,9 +561,46 @@
2400 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2401 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2404 + * Hash Tree Directory indexing
2405 + * (c) Daniel Phillips, 2001
2408 +#ifdef CONFIG_EXT3_INDEX
2409 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2410 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2411 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2412 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2413 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2415 + #define is_dx(dir) 0
2416 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2417 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2420 +/* Legal values for the dx_root hash_version field: */
2422 +#define DX_HASH_LEGACY 0
2423 +#define DX_HASH_HALF_MD4 1
2424 +#define DX_HASH_TEA 2
2426 +/* hash info structure used by the directory hash */
2427 +struct dx_hash_info
2437 + * Control parameters used by ext3_htree_next_block
2439 +#define HASH_NB_ALWAYS 1
2443 * Describe an inode's exact location on disk and in memory
2446 @@ -565,6 +610,27 @@
2447 unsigned long block_group;
2452 + * This structure is stuffed into the struct file's private_data field
2453 + * for directories. It is where we put information so that we can do
2454 + * readdir operations in hash tree order.
2456 +struct dir_private_info {
2458 + rb_node_t *curr_node;
2459 + struct fname *extra_fname;
2462 + __u32 curr_minor_hash;
2467 + * Special error return code only used by dx_probe() and its callers.
2469 +#define ERR_BAD_DX_DIR -75000
2472 * Function prototypes
2474 @@ -592,11 +658,20 @@
2477 extern int ext3_check_dir_entry(const char *, struct inode *,
2478 - struct ext3_dir_entry_2 *, struct buffer_head *,
2480 + struct ext3_dir_entry_2 *,
2481 + struct buffer_head *, unsigned long);
2482 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2484 + struct ext3_dir_entry_2 *dirent);
2485 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2488 extern int ext3_sync_file (struct file *, struct dentry *, int);
2491 +extern int ext3fs_dirhash(const char *name, int len, struct
2492 + dx_hash_info *hinfo);
2495 extern struct inode * ext3_new_inode (handle_t *, struct inode *, int);
2496 extern void ext3_free_inode (handle_t *, struct inode *);
2499 extern int ext3_orphan_add(handle_t *, struct inode *);
2500 extern int ext3_orphan_del(handle_t *, struct inode *);
2501 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2502 + __u32 start_minor_hash, __u32 *next_hash);
2505 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2506 Index: linux-2.4.21/include/linux/ext3_fs_sb.h
2507 ===================================================================
2508 --- linux-2.4.21.orig/include/linux/ext3_fs_sb.h 2004-09-11 10:16:39.000000000 -0400
2509 +++ linux-2.4.21/include/linux/ext3_fs_sb.h 2004-09-16 19:40:16.000000000 -0400
2513 u32 s_next_generation;
2514 + u32 s_hash_seed[4];
2515 + int s_def_hash_version;
2518 struct inode * s_journal_inode;
2519 Index: linux-2.4.21/include/linux/ext3_jbd.h
2520 ===================================================================
2521 --- linux-2.4.21.orig/include/linux/ext3_jbd.h 2004-09-11 10:16:39.000000000 -0400
2522 +++ linux-2.4.21/include/linux/ext3_jbd.h 2004-09-16 19:40:16.000000000 -0400
2525 #define EXT3_RESERVE_TRANS_BLOCKS 12U
2527 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2530 ext3_mark_iloc_dirty(handle_t *handle,
2531 struct inode *inode,