2 fs/ext3/dir.c | 302 +++++++++
4 fs/ext3/hash.c | 215 ++++++
5 fs/ext3/namei.c | 1420 ++++++++++++++++++++++++++++++++++++++++-----
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, 1921 insertions(+), 161 deletions(-)
14 --- linux-2.4.18-chaos-pdirops/fs/ext3/dir.c~ext3-htree 2001-11-10 01:25:04.000000000 +0300
15 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/dir.c 2003-09-23 12:12:04.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.18-chaos-pdirops/fs/ext3/file.c~ext3-htree 2003-07-28 17:52:04.000000000 +0400
362 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/file.c 2003-09-23 12:11:54.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.18-chaos-pdirops-alexey/fs/ext3/hash.c 2003-09-23 12:11:54.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.18-chaos-pdirops/fs/ext3/Makefile~ext3-htree 2003-09-23 11:52:24.000000000 +0400
592 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/Makefile 2003-09-23 12:11:54.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.18-chaos-pdirops/fs/ext3/namei.c~ext3-htree 2003-09-23 11:52:34.000000000 +0400
603 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/namei.c 2003-09-23 12:12:09.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,66 @@ 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 + if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
1344 + hash = hinfo.hash;
1346 + block = dx_get_block(frame->at);
1347 + if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
1349 + de = (struct ext3_dir_entry_2 *) bh->b_data;
1350 + top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
1351 + EXT3_DIR_REC_LEN(0));
1352 + for (; de < top; de = ext3_next_entry(de))
1353 + if (ext3_match (namelen, name, de)) {
1354 + if (!ext3_check_dir_entry("ext3_find_entry",
1356 + (block<<EXT3_BLOCK_SIZE_BITS(sb))
1357 + +((char *)de - bh->b_data))) {
1362 + dx_release (frames);
1366 + /* Check to see if we should continue to search */
1367 + retval = ext3_htree_next_block(dir, hash, frame,
1369 + if (retval == -1) {
1370 + ext3_warning(sb, __FUNCTION__,
1371 + "error reading index page in directory #%lu",
1375 + } while (retval == 1);
1379 + dxtrace(printk("%s not found\n", name));
1380 + dx_release (frames);
1385 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
1387 struct inode * inode;
1388 @@ -213,8 +938,9 @@ static struct dentry *ext3_lookup(struct
1390 inode = iget(dir->i_sb, ino);
1394 return ERR_PTR(-EACCES);
1397 d_add(dentry, inode);
1399 @@ -238,6 +964,301 @@ static inline void ext3_set_de_type(stru
1400 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1403 +#ifdef CONFIG_EXT3_INDEX
1404 +static struct ext3_dir_entry_2 *
1405 +dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1407 + unsigned rec_len = 0;
1410 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1411 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1412 + memcpy (to, de, rec_len);
1413 + ((struct ext3_dir_entry_2 *)to)->rec_len = cpu_to_le16(rec_len);
1418 + return (struct ext3_dir_entry_2 *) (to - rec_len);
1421 +static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1423 + struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1424 + unsigned rec_len = 0;
1427 + while ((char*)de < base + size) {
1428 + next = (struct ext3_dir_entry_2 *) ((char *) de +
1429 + le16_to_cpu(de->rec_len));
1430 + if (de->inode && de->name_len) {
1431 + rec_len = EXT3_DIR_REC_LEN(de->name_len);
1433 + memmove(to, de, rec_len);
1434 + to->rec_len = cpu_to_le16(rec_len);
1436 + to = (struct ext3_dir_entry_2 *)((char *)to + rec_len);
1443 +static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1444 + struct buffer_head **bh,struct dx_frame *frame,
1445 + struct dx_hash_info *hinfo, int *error)
1447 + unsigned blocksize = dir->i_sb->s_blocksize;
1448 + unsigned count, continued;
1449 + struct buffer_head *bh2;
1452 + struct dx_map_entry *map;
1453 + char *data1 = (*bh)->b_data, *data2;
1455 + struct ext3_dir_entry_2 *de = NULL, *de2;
1458 + bh2 = ext3_append (handle, dir, &newblock, error);
1465 + BUFFER_TRACE(*bh, "get_write_access");
1466 + err = ext3_journal_get_write_access(handle, *bh);
1472 + ext3_std_error(dir->i_sb, err);
1475 + BUFFER_TRACE(frame->bh, "get_write_access");
1476 + err = ext3_journal_get_write_access(handle, frame->bh);
1478 + goto journal_error;
1480 + data2 = bh2->b_data;
1482 + /* create map in the end of data2 block */
1483 + map = (struct dx_map_entry *) (data2 + blocksize);
1484 + count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1485 + blocksize, hinfo, map);
1487 + split = count/2; // need to adjust to actual middle
1488 + dx_sort_map (map, count);
1489 + hash2 = map[split].hash;
1490 + continued = hash2 == map[split - 1].hash;
1491 + dxtrace(printk("Split block %i at %x, %i/%i\n",
1492 + dx_get_block(frame->at), hash2, split, count-split));
1494 + /* Fancy dance to stay within two buffers */
1495 + de2 = dx_move_dirents(data1, data2, map + split, count - split);
1496 + de = dx_pack_dirents(data1,blocksize);
1497 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1498 + de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1499 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1500 + dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1502 + /* Which block gets the new entry? */
1503 + if (hinfo->hash >= hash2)
1508 + dx_insert_block (frame, hash2 + continued, newblock);
1509 + err = ext3_journal_dirty_metadata (handle, bh2);
1511 + goto journal_error;
1512 + err = ext3_journal_dirty_metadata (handle, frame->bh);
1514 + goto journal_error;
1516 + dxtrace(dx_show_index ("frame", frame->entries));
1524 + * Add a new entry into a directory (leaf) block. If de is non-NULL,
1525 + * it points to a directory entry which is guaranteed to be large
1526 + * enough for new directory entry. If de is NULL, then
1527 + * add_dirent_to_buf will attempt search the directory block for
1528 + * space. It will return -ENOSPC if no space is available, and -EIO
1529 + * and -EEXIST if directory entry already exists.
1531 + * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1532 + * all other cases bh is released.
1534 +static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1535 + struct inode *inode, struct ext3_dir_entry_2 *de,
1536 + struct buffer_head * bh)
1538 + struct inode *dir = dentry->d_parent->d_inode;
1539 + const char *name = dentry->d_name.name;
1540 + int namelen = dentry->d_name.len;
1541 + unsigned long offset = 0;
1542 + unsigned short reclen;
1543 + int nlen, rlen, err;
1546 + reclen = EXT3_DIR_REC_LEN(namelen);
1548 + de = (struct ext3_dir_entry_2 *)bh->b_data;
1549 + top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1550 + while ((char *) de <= top) {
1551 + if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1556 + if (ext3_match (namelen, name, de)) {
1560 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1561 + rlen = le16_to_cpu(de->rec_len);
1562 + if ((de->inode? rlen - nlen: rlen) >= reclen)
1564 + de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1567 + if ((char *) de > top)
1570 + BUFFER_TRACE(bh, "get_write_access");
1571 + err = ext3_journal_get_write_access(handle, bh);
1573 + ext3_std_error(dir->i_sb, err);
1578 + /* By now the buffer is marked for journaling */
1579 + nlen = EXT3_DIR_REC_LEN(de->name_len);
1580 + rlen = le16_to_cpu(de->rec_len);
1582 + struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1583 + de1->rec_len = cpu_to_le16(rlen - nlen);
1584 + de->rec_len = cpu_to_le16(nlen);
1587 + de->file_type = EXT3_FT_UNKNOWN;
1589 + de->inode = cpu_to_le32(inode->i_ino);
1590 + ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1593 + de->name_len = namelen;
1594 + memcpy (de->name, name, namelen);
1596 + * XXX shouldn't update any times until successful
1597 + * completion of syscall, but too many callers depend
1600 + * XXX similarly, too many callers depend on
1601 + * ext3_new_inode() setting the times, but error
1602 + * recovery deletes the inode, so the worst that can
1603 + * happen is that the times are slightly out of date
1604 + * and/or different from the directory change time.
1606 + dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1607 + ext3_update_dx_flag(dir);
1608 + dir->i_version = ++event;
1609 + ext3_mark_inode_dirty(handle, dir);
1610 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1611 + err = ext3_journal_dirty_metadata(handle, bh);
1613 + ext3_std_error(dir->i_sb, err);
1618 +#ifdef CONFIG_EXT3_INDEX
1620 + * This converts a one block unindexed directory to a 3 block indexed
1621 + * directory, and adds the dentry to the indexed directory.
1623 +static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1624 + struct inode *inode, struct buffer_head *bh)
1626 + struct inode *dir = dentry->d_parent->d_inode;
1627 + const char *name = dentry->d_name.name;
1628 + int namelen = dentry->d_name.len;
1629 + struct buffer_head *bh2;
1630 + struct dx_root *root;
1631 + struct dx_frame frames[2], *frame;
1632 + struct dx_entry *entries;
1633 + struct ext3_dir_entry_2 *de, *de2;
1634 + char *data1, *top;
1637 + unsigned blocksize;
1638 + struct dx_hash_info hinfo;
1641 + blocksize = dir->i_sb->s_blocksize;
1642 + dxtrace(printk("Creating index\n"));
1643 + retval = ext3_journal_get_write_access(handle, bh);
1645 + ext3_std_error(dir->i_sb, retval);
1649 + root = (struct dx_root *) bh->b_data;
1651 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1652 + bh2 = ext3_append (handle, dir, &block, &retval);
1657 + data1 = bh2->b_data;
1659 + /* The 0th block becomes the root, move the dirents out */
1660 + de = (struct ext3_dir_entry_2 *)&root->dotdot;
1661 + de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
1662 + len = ((char *) root) + blocksize - (char *) de;
1663 + memcpy (data1, de, len);
1664 + de = (struct ext3_dir_entry_2 *) data1;
1665 + top = data1 + len;
1666 + while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1668 + de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1669 + /* Initialize the root; the dot dirents already exist */
1670 + de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1671 + de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1672 + memset (&root->info, 0, sizeof(root->info));
1673 + root->info.info_length = sizeof(root->info);
1674 + root->info.hash_version = dir->i_sb->u.ext3_sb.s_def_hash_version;
1675 + entries = root->entries;
1676 + dx_set_block (entries, 1);
1677 + dx_set_count (entries, 1);
1678 + dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1680 + /* Initialize as for dx_probe */
1681 + hinfo.hash_version = root->info.hash_version;
1682 + hinfo.seed = dir->i_sb->u.ext3_sb.s_hash_seed;
1683 + ext3fs_dirhash(name, namelen, &hinfo);
1685 + frame->entries = entries;
1686 + frame->at = entries;
1689 + de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1690 + dx_release (frames);
1694 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1701 @@ -248,127 +1268,198 @@ static inline void ext3_set_de_type(stru
1702 * may not sleep between calling this and putting something into
1703 * the entry, as someone else might have used it while you slept.
1707 - * AKPM: the journalling code here looks wrong on the error paths
1709 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1710 struct inode *inode)
1712 struct inode *dir = dentry->d_parent->d_inode;
1713 - const char *name = dentry->d_name.name;
1714 - int namelen = dentry->d_name.len;
1715 unsigned long offset;
1716 - unsigned short rec_len;
1717 struct buffer_head * bh;
1718 - struct ext3_dir_entry_2 * de, * de1;
1719 + struct ext3_dir_entry_2 *de;
1720 struct super_block * sb;
1722 +#ifdef CONFIG_EXT3_INDEX
1723 + int dx_fallback=0;
1725 + unsigned blocksize;
1726 + unsigned nlen, rlen;
1727 + u32 block, blocks;
1732 + blocksize = sb->s_blocksize;
1733 + if (!dentry->d_name.len)
1735 - bh = ext3_bread (handle, dir, 0, 0, &retval);
1736 +#ifdef CONFIG_EXT3_INDEX
1738 + retval = ext3_dx_add_entry(handle, dentry, inode);
1739 + if (!retval || (retval != ERR_BAD_DX_DIR))
1741 + EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1743 + ext3_mark_inode_dirty(handle, dir);
1746 + blocks = dir->i_size >> sb->s_blocksize_bits;
1747 + for (block = 0, offset = 0; block < blocks; block++) {
1748 + bh = ext3_bread(handle, dir, block, 0, &retval);
1751 + retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1752 + if (retval != -ENOSPC)
1755 +#ifdef CONFIG_EXT3_INDEX
1756 + if (blocks == 1 && !dx_fallback &&
1757 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1758 + return make_indexed_dir(handle, dentry, inode, bh);
1762 + bh = ext3_append(handle, dir, &block, &retval);
1765 - rec_len = EXT3_DIR_REC_LEN(namelen);
1767 de = (struct ext3_dir_entry_2 *) bh->b_data;
1769 - if ((char *)de >= sb->s_blocksize + bh->b_data) {
1772 - bh = ext3_bread (handle, dir,
1773 - offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
1776 - if (dir->i_size <= offset) {
1777 - if (dir->i_size == 0) {
1782 + de->rec_len = cpu_to_le16(rlen = blocksize);
1784 + return add_dirent_to_buf(handle, dentry, inode, de, bh);
1787 - ext3_debug ("creating next block\n");
1788 +#ifdef CONFIG_EXT3_INDEX
1790 + * Returns 0 for success, or a negative error value
1792 +static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1793 + struct inode *inode)
1795 + struct dx_frame frames[2], *frame;
1796 + struct dx_entry *entries, *at;
1797 + struct dx_hash_info hinfo;
1798 + struct buffer_head * bh;
1799 + struct inode *dir = dentry->d_parent->d_inode;
1800 + struct super_block * sb = dir->i_sb;
1801 + struct ext3_dir_entry_2 *de;
1804 - BUFFER_TRACE(bh, "get_write_access");
1805 - ext3_journal_get_write_access(handle, bh);
1806 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1808 - de->rec_len = le16_to_cpu(sb->s_blocksize);
1809 - dir->u.ext3_i.i_disksize =
1810 - dir->i_size = offset + sb->s_blocksize;
1811 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1812 - ext3_mark_inode_dirty(handle, dir);
1814 + frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1817 + entries = frame->entries;
1820 - ext3_debug ("skipping to next block\n");
1821 + if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1824 - de = (struct ext3_dir_entry_2 *) bh->b_data;
1827 - if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
1832 - if (ext3_match (namelen, name, de)) {
1835 + BUFFER_TRACE(bh, "get_write_access");
1836 + err = ext3_journal_get_write_access(handle, bh);
1838 + goto journal_error;
1840 + err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1841 + if (err != -ENOSPC) {
1846 + /* Block full, should compress but for now just split */
1847 + dxtrace(printk("using %u of %u node entries\n",
1848 + dx_get_count(entries), dx_get_limit(entries)));
1849 + /* Need to split index? */
1850 + if (dx_get_count(entries) == dx_get_limit(entries)) {
1852 + unsigned icount = dx_get_count(entries);
1853 + int levels = frame - frames;
1854 + struct dx_entry *entries2;
1855 + struct dx_node *node2;
1856 + struct buffer_head *bh2;
1858 + if (levels && (dx_get_count(frames->entries) ==
1859 + dx_get_limit(frames->entries))) {
1860 + ext3_warning(sb, __FUNCTION__,
1861 + "Directory index full!\n");
1865 - if ((le32_to_cpu(de->inode) == 0 &&
1866 - le16_to_cpu(de->rec_len) >= rec_len) ||
1867 - (le16_to_cpu(de->rec_len) >=
1868 - EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
1869 - BUFFER_TRACE(bh, "get_write_access");
1870 - ext3_journal_get_write_access(handle, bh);
1871 - /* By now the buffer is marked for journaling */
1872 - offset += le16_to_cpu(de->rec_len);
1873 - if (le32_to_cpu(de->inode)) {
1874 - de1 = (struct ext3_dir_entry_2 *) ((char *) de +
1875 - EXT3_DIR_REC_LEN(de->name_len));
1877 - cpu_to_le16(le16_to_cpu(de->rec_len) -
1878 - EXT3_DIR_REC_LEN(de->name_len));
1879 - de->rec_len = cpu_to_le16(
1880 - EXT3_DIR_REC_LEN(de->name_len));
1882 + bh2 = ext3_append (handle, dir, &newblock, &err);
1885 + node2 = (struct dx_node *)(bh2->b_data);
1886 + entries2 = node2->entries;
1887 + node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1888 + node2->fake.inode = 0;
1889 + BUFFER_TRACE(frame->bh, "get_write_access");
1890 + err = ext3_journal_get_write_access(handle, frame->bh);
1892 + goto journal_error;
1894 + unsigned icount1 = icount/2, icount2 = icount - icount1;
1895 + unsigned hash2 = dx_get_hash(entries + icount1);
1896 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1898 + BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1899 + err = ext3_journal_get_write_access(handle,
1902 + goto journal_error;
1904 + memcpy ((char *) entries2, (char *) (entries + icount1),
1905 + icount2 * sizeof(struct dx_entry));
1906 + dx_set_count (entries, icount1);
1907 + dx_set_count (entries2, icount2);
1908 + dx_set_limit (entries2, dx_node_limit(dir));
1910 + /* Which index block gets the new entry? */
1911 + if (at - entries >= icount1) {
1912 + frame->at = at = at - entries - icount1 + entries2;
1913 + frame->entries = entries = entries2;
1914 + swap(frame->bh, bh2);
1916 - de->file_type = EXT3_FT_UNKNOWN;
1918 - de->inode = cpu_to_le32(inode->i_ino);
1919 - ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1922 - de->name_len = namelen;
1923 - memcpy (de->name, name, namelen);
1925 - * XXX shouldn't update any times until successful
1926 - * completion of syscall, but too many callers depend
1929 - * XXX similarly, too many callers depend on
1930 - * ext3_new_inode() setting the times, but error
1931 - * recovery deletes the inode, so the worst that can
1932 - * happen is that the times are slightly out of date
1933 - * and/or different from the directory change time.
1935 - dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1936 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1937 - dir->i_version = ++event;
1938 - ext3_mark_inode_dirty(handle, dir);
1939 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1940 - ext3_journal_dirty_metadata(handle, bh);
1943 + dx_insert_block (frames + 0, hash2, newblock);
1944 + dxtrace(dx_show_index ("node", frames[1].entries));
1945 + dxtrace(dx_show_index ("node",
1946 + ((struct dx_node *) bh2->b_data)->entries));
1947 + err = ext3_journal_dirty_metadata(handle, bh2);
1949 + goto journal_error;
1952 + dxtrace(printk("Creating second level index...\n"));
1953 + memcpy((char *) entries2, (char *) entries,
1954 + icount * sizeof(struct dx_entry));
1955 + dx_set_limit(entries2, dx_node_limit(dir));
1958 + dx_set_count(entries, 1);
1959 + dx_set_block(entries + 0, newblock);
1960 + ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1962 + /* Add new access path frame */
1963 + frame = frames + 1;
1964 + frame->at = at = at - entries + entries2;
1965 + frame->entries = entries = entries2;
1967 + err = ext3_journal_get_write_access(handle,
1970 + goto journal_error;
1972 - offset += le16_to_cpu(de->rec_len);
1973 - de = (struct ext3_dir_entry_2 *)
1974 - ((char *) de + le16_to_cpu(de->rec_len));
1975 + ext3_journal_dirty_metadata(handle, frames[0].bh);
1979 + de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1982 + err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1987 + ext3_std_error(dir->i_sb, err);
1991 + dx_release(frames);
1997 * ext3_delete_entry deletes a directory entry by merging it with the
1998 @@ -452,9 +1543,11 @@ static int ext3_create (struct inode * d
1999 struct inode * inode;
2002 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2003 - if (IS_ERR(handle))
2004 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2005 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2006 + if (IS_ERR(handle)) {
2007 return PTR_ERR(handle);
2012 @@ -479,9 +1572,11 @@ static int ext3_mknod (struct inode * di
2013 struct inode *inode;
2016 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2017 - if (IS_ERR(handle))
2018 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2019 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2020 + if (IS_ERR(handle)) {
2021 return PTR_ERR(handle);
2026 @@ -508,9 +1603,11 @@ static int ext3_mkdir(struct inode * dir
2027 if (dir->i_nlink >= EXT3_LINK_MAX)
2030 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
2031 - if (IS_ERR(handle))
2032 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2033 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
2034 + if (IS_ERR(handle)) {
2035 return PTR_ERR(handle);
2040 @@ -522,7 +1619,7 @@ static int ext3_mkdir(struct inode * dir
2042 inode->i_op = &ext3_dir_inode_operations;
2043 inode->i_fop = &ext3_dir_operations;
2044 - inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
2045 + inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2046 inode->i_blocks = 0;
2047 dir_block = ext3_bread (handle, inode, 0, 1, &err);
2049 @@ -555,21 +1652,19 @@ static int ext3_mkdir(struct inode * dir
2050 inode->i_mode |= S_ISGID;
2051 ext3_mark_inode_dirty(handle, inode);
2052 err = ext3_add_entry (handle, dentry, inode);
2054 - goto out_no_entry;
2056 + inode->i_nlink = 0;
2057 + ext3_mark_inode_dirty(handle, inode);
2062 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2063 + ext3_update_dx_flag(dir);
2064 ext3_mark_inode_dirty(handle, dir);
2065 d_instantiate(dentry, inode);
2067 ext3_journal_stop(handle, dir);
2071 - inode->i_nlink = 0;
2072 - ext3_mark_inode_dirty(handle, inode);
2078 @@ -656,7 +1751,7 @@ int ext3_orphan_add(handle_t *handle, st
2082 - if (!list_empty(&inode->u.ext3_i.i_orphan))
2083 + if (!list_empty(&EXT3_I(inode)->i_orphan))
2086 /* Orphan handling is only valid for files with data blocks
2087 @@ -697,7 +1792,7 @@ int ext3_orphan_add(handle_t *handle, st
2088 * This is safe: on error we're going to ignore the orphan list
2089 * anyway on the next recovery. */
2091 - list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
2092 + list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
2094 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
2095 jbd_debug(4, "orphan inode %ld will point to %d\n",
2096 @@ -715,25 +1810,26 @@ out_unlock:
2097 int ext3_orphan_del(handle_t *handle, struct inode *inode)
2099 struct list_head *prev;
2100 + struct ext3_inode_info *ei = EXT3_I(inode);
2101 struct ext3_sb_info *sbi;
2103 struct ext3_iloc iloc;
2106 lock_super(inode->i_sb);
2107 - if (list_empty(&inode->u.ext3_i.i_orphan)) {
2108 + if (list_empty(&ei->i_orphan)) {
2109 unlock_super(inode->i_sb);
2113 ino_next = NEXT_ORPHAN(inode);
2114 - prev = inode->u.ext3_i.i_orphan.prev;
2115 + prev = ei->i_orphan.prev;
2116 sbi = EXT3_SB(inode->i_sb);
2118 jbd_debug(4, "remove inode %ld from orphan list\n", inode->i_ino);
2120 - list_del(&inode->u.ext3_i.i_orphan);
2121 - INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2122 + list_del(&ei->i_orphan);
2123 + INIT_LIST_HEAD(&ei->i_orphan);
2125 /* If we're on an error path, we may not have a valid
2126 * transaction handle with which to update the orphan list on
2127 @@ -794,8 +1890,9 @@ static int ext3_rmdir (struct inode * di
2130 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2131 - if (IS_ERR(handle))
2132 + if (IS_ERR(handle)) {
2133 return PTR_ERR(handle);
2137 bh = ext3_find_entry (dentry, &de);
2138 @@ -833,7 +1930,7 @@ static int ext3_rmdir (struct inode * di
2140 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2141 ext3_mark_inode_dirty(handle, inode);
2142 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2143 + ext3_update_dx_flag(dir);
2144 ext3_mark_inode_dirty(handle, dir);
2147 @@ -851,8 +1948,9 @@ static int ext3_unlink(struct inode * di
2150 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2151 - if (IS_ERR(handle))
2152 + if (IS_ERR(handle)) {
2153 return PTR_ERR(handle);
2158 @@ -879,7 +1977,7 @@ static int ext3_unlink(struct inode * di
2161 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2162 - dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2163 + ext3_update_dx_flag(dir);
2164 ext3_mark_inode_dirty(handle, dir);
2166 if (!inode->i_nlink)
2167 @@ -905,9 +2003,11 @@ static int ext3_symlink (struct inode *
2168 if (l > dir->i_sb->s_blocksize)
2169 return -ENAMETOOLONG;
2171 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
2172 - if (IS_ERR(handle))
2173 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2174 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2175 + if (IS_ERR(handle)) {
2176 return PTR_ERR(handle);
2181 @@ -917,7 +2017,7 @@ static int ext3_symlink (struct inode *
2185 - if (l > sizeof (inode->u.ext3_i.i_data)) {
2186 + if (l > sizeof (EXT3_I(inode)->i_data)) {
2187 inode->i_op = &page_symlink_inode_operations;
2188 inode->i_mapping->a_ops = &ext3_aops;
2190 @@ -926,25 +2026,23 @@ static int ext3_symlink (struct inode *
2191 * i_size in generic_commit_write().
2193 err = block_symlink(inode, symname, l);
2195 - goto out_no_entry;
2197 + ext3_dec_count(handle, inode);
2198 + ext3_mark_inode_dirty(handle, inode);
2203 inode->i_op = &ext3_fast_symlink_inode_operations;
2204 - memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
2205 + memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2206 inode->i_size = l-1;
2208 - inode->u.ext3_i.i_disksize = inode->i_size;
2209 + EXT3_I(inode)->i_disksize = inode->i_size;
2210 err = ext3_add_nondir(handle, dentry, inode);
2211 ext3_mark_inode_dirty(handle, inode);
2213 ext3_journal_stop(handle, dir);
2217 - ext3_dec_count(handle, inode);
2218 - ext3_mark_inode_dirty(handle, inode);
2223 static int ext3_link (struct dentry * old_dentry,
2224 @@ -957,12 +2055,15 @@ static int ext3_link (struct dentry * ol
2225 if (S_ISDIR(inode->i_mode))
2228 - if (inode->i_nlink >= EXT3_LINK_MAX)
2229 + if (inode->i_nlink >= EXT3_LINK_MAX) {
2233 - handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
2234 - if (IS_ERR(handle))
2235 + handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2236 + EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2237 + if (IS_ERR(handle)) {
2238 return PTR_ERR(handle);
2243 @@ -996,9 +2097,11 @@ static int ext3_rename (struct inode * o
2245 old_bh = new_bh = dir_bh = NULL;
2247 - handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
2248 - if (IS_ERR(handle))
2249 + handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2250 + EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2251 + if (IS_ERR(handle)) {
2252 return PTR_ERR(handle);
2255 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
2257 @@ -1071,14 +2174,37 @@ static int ext3_rename (struct inode * o
2261 - ext3_delete_entry(handle, old_dir, old_de, old_bh);
2262 + if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2263 + old_de->name_len != old_dentry->d_name.len ||
2264 + strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2265 + (retval = ext3_delete_entry(handle, old_dir,
2266 + old_de, old_bh)) == -ENOENT) {
2267 + /* old_de could have moved from under us during htree split, so
2268 + * make sure that we are deleting the right entry. We might
2269 + * also be pointing to a stale entry in the unused part of
2270 + * old_bh so just checking inum and the name isn't enough. */
2271 + struct buffer_head *old_bh2;
2272 + struct ext3_dir_entry_2 *old_de2;
2274 + old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2276 + retval = ext3_delete_entry(handle, old_dir,
2277 + old_de2, old_bh2);
2282 + ext3_warning(old_dir->i_sb, "ext3_rename",
2283 + "Deleting old file (%lu), %d, error=%d",
2284 + old_dir->i_ino, old_dir->i_nlink, retval);
2288 new_inode->i_nlink--;
2289 new_inode->i_ctime = CURRENT_TIME;
2291 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2292 - old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2293 + ext3_update_dx_flag(old_dir);
2295 BUFFER_TRACE(dir_bh, "get_write_access");
2296 ext3_journal_get_write_access(handle, dir_bh);
2297 @@ -1090,7 +2212,7 @@ static int ext3_rename (struct inode * o
2298 new_inode->i_nlink--;
2301 - new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
2302 + ext3_update_dx_flag(new_dir);
2303 ext3_mark_inode_dirty(handle, new_dir);
2306 --- linux-2.4.18-chaos-pdirops/fs/ext3/super.c~ext3-htree 2003-09-23 11:52:24.000000000 +0400
2307 +++ linux-2.4.18-chaos-pdirops-alexey/fs/ext3/super.c 2003-09-23 12:11:54.000000000 +0400
2308 @@ -705,6 +705,7 @@ static int ext3_setup_super(struct super
2309 es->s_mtime = cpu_to_le32(CURRENT_TIME);
2310 ext3_update_dynamic_rev(sb);
2311 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2313 ext3_commit_super (sb, es, 1);
2314 if (test_opt (sb, DEBUG))
2316 @@ -715,6 +716,7 @@ static int ext3_setup_super(struct super
2317 EXT3_BLOCKS_PER_GROUP(sb),
2318 EXT3_INODES_PER_GROUP(sb),
2321 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
2322 bdevname(sb->s_dev));
2323 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
2324 @@ -889,6 +891,7 @@ static loff_t ext3_max_size(int bits)
2329 struct super_block * ext3_read_super (struct super_block * sb, void * data,
2332 @@ -1065,6 +1068,9 @@ struct super_block * ext3_read_super (st
2333 sbi->s_mount_state = le16_to_cpu(es->s_state);
2334 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2335 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2336 + for (i=0; i < 4; i++)
2337 + sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2338 + sbi->s_def_hash_version = es->s_def_hash_version;
2340 if (sbi->s_blocks_per_group > blocksize * 8) {
2342 @@ -1747,6 +1753,7 @@ static void __exit exit_ext3_fs(void)
2343 unregister_filesystem(&ext3_fs_type);
2346 +EXPORT_SYMBOL(ext3_force_commit);
2347 EXPORT_SYMBOL(ext3_bread);
2349 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2350 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_fs.h~ext3-htree 2003-07-28 17:52:17.000000000 +0400
2351 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_fs.h 2003-09-23 12:12:04.000000000 +0400
2353 #define EXT3FS_VERSION "2.4-0.9.18"
2356 + * Always enable hashed directories
2358 +#define CONFIG_EXT3_INDEX
2364 @@ -437,8 +442,11 @@ struct ext3_super_block {
2365 /*E0*/ __u32 s_journal_inum; /* inode number of journal file */
2366 __u32 s_journal_dev; /* device number of journal file */
2367 __u32 s_last_orphan; /* start of list of inodes to delete */
2369 -/*EC*/ __u32 s_reserved[197]; /* Padding to the end of the block */
2370 + __u32 s_hash_seed[4]; /* HTREE hash seed */
2371 + __u8 s_def_hash_version; /* Default hash version to use */
2372 + __u8 s_reserved_char_pad;
2373 + __u16 s_reserved_word_pad;
2374 + __u32 s_reserved[192]; /* Padding to the end of the block */
2378 @@ -575,9 +583,46 @@ struct ext3_dir_entry_2 {
2379 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
2380 #define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
2383 + * Hash Tree Directory indexing
2384 + * (c) Daniel Phillips, 2001
2387 +#ifdef CONFIG_EXT3_INDEX
2388 + #define is_dx(dir) (EXT3_HAS_COMPAT_FEATURE(dir->i_sb, \
2389 + EXT3_FEATURE_COMPAT_DIR_INDEX) && \
2390 + (EXT3_I(dir)->i_flags & EXT3_INDEX_FL))
2391 +#define EXT3_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT3_LINK_MAX)
2392 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
2394 + #define is_dx(dir) 0
2395 +#define EXT3_DIR_LINK_MAX(dir) ((dir)->i_nlink >= EXT3_LINK_MAX)
2396 +#define EXT3_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2)
2399 +/* Legal values for the dx_root hash_version field: */
2401 +#define DX_HASH_LEGACY 0
2402 +#define DX_HASH_HALF_MD4 1
2403 +#define DX_HASH_TEA 2
2405 +/* hash info structure used by the directory hash */
2406 +struct dx_hash_info
2416 + * Control parameters used by ext3_htree_next_block
2418 +#define HASH_NB_ALWAYS 1
2422 * Describe an inode's exact location on disk and in memory
2425 @@ -587,6 +632,27 @@ struct ext3_iloc
2426 unsigned long block_group;
2431 + * This structure is stuffed into the struct file's private_data field
2432 + * for directories. It is where we put information so that we can do
2433 + * readdir operations in hash tree order.
2435 +struct dir_private_info {
2437 + rb_node_t *curr_node;
2438 + struct fname *extra_fname;
2441 + __u32 curr_minor_hash;
2446 + * Special error return code only used by dx_probe() and its callers.
2448 +#define ERR_BAD_DX_DIR -75000
2451 * Function prototypes
2453 @@ -614,11 +680,20 @@ extern struct ext3_group_desc * ext3_get
2456 extern int ext3_check_dir_entry(const char *, struct inode *,
2457 - struct ext3_dir_entry_2 *, struct buffer_head *,
2459 + struct ext3_dir_entry_2 *,
2460 + struct buffer_head *, unsigned long);
2461 +extern int ext3_htree_store_dirent(struct file *dir_file, __u32 hash,
2463 + struct ext3_dir_entry_2 *dirent);
2464 +extern void ext3_htree_free_dir_info(struct dir_private_info *p);
2467 extern int ext3_sync_file (struct file *, struct dentry *, int);
2470 +extern int ext3fs_dirhash(const char *name, int len, struct
2471 + dx_hash_info *hinfo);
2474 extern struct inode * ext3_new_inode (handle_t *, const struct inode *, int);
2475 extern void ext3_free_inode (handle_t *, struct inode *);
2476 @@ -650,6 +725,8 @@ extern int ext3_ioctl (struct inode *, s
2478 extern int ext3_orphan_add(handle_t *, struct inode *);
2479 extern int ext3_orphan_del(handle_t *, struct inode *);
2480 +extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2481 + __u32 start_minor_hash, __u32 *next_hash);
2484 extern void ext3_error (struct super_block *, const char *, const char *, ...)
2485 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_fs_sb.h~ext3-htree 2003-07-28 17:52:17.000000000 +0400
2486 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_fs_sb.h 2003-09-23 12:11:54.000000000 +0400
2487 @@ -62,6 +62,8 @@ struct ext3_sb_info {
2490 u32 s_next_generation;
2491 + u32 s_hash_seed[4];
2492 + int s_def_hash_version;
2495 struct inode * s_journal_inode;
2496 --- linux-2.4.18-chaos-pdirops/include/linux/ext3_jbd.h~ext3-htree 2001-12-21 20:42:03.000000000 +0300
2497 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/ext3_jbd.h 2003-09-23 12:11:54.000000000 +0400
2498 @@ -63,6 +63,8 @@ extern int ext3_writepage_trans_blocks(s
2500 #define EXT3_RESERVE_TRANS_BLOCKS 12
2502 +#define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
2505 ext3_mark_iloc_dirty(handle_t *handle,
2506 struct inode *inode,
2507 --- linux-2.4.18-chaos-pdirops/include/linux/rbtree.h~ext3-htree 2001-11-22 22:46:18.000000000 +0300
2508 +++ linux-2.4.18-chaos-pdirops-alexey/include/linux/rbtree.h 2003-09-23 12:11:54.000000000 +0400
2509 @@ -120,6 +120,8 @@ rb_root_t;
2511 extern void rb_insert_color(rb_node_t *, rb_root_t *);
2512 extern void rb_erase(rb_node_t *, rb_root_t *);
2513 +extern rb_node_t *rb_get_first(rb_root_t *root);
2514 +extern rb_node_t *rb_get_next(rb_node_t *n);
2516 static inline void rb_link_node(rb_node_t * node, rb_node_t * parent, rb_node_t ** rb_link)
2518 --- linux-2.4.18-chaos-pdirops/lib/rbtree.c~ext3-htree 2003-09-23 11:52:34.000000000 +0400
2519 +++ linux-2.4.18-chaos-pdirops-alexey/lib/rbtree.c 2003-09-23 12:11:54.000000000 +0400
2521 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2525 + rb_get_first and rb_get_next written by Theodore Ts'o, 9/8/2002
2528 #include <linux/rbtree.h>
2529 @@ -295,3 +297,43 @@ void rb_erase(rb_node_t * node, rb_root_
2530 __rb_erase_color(child, parent, root);
2532 EXPORT_SYMBOL(rb_erase);
2535 + * This function returns the first node (in sort order) of the tree.
2537 +rb_node_t *rb_get_first(rb_root_t *root)
2541 + n = root->rb_node;
2544 + while (n->rb_left)
2548 +EXPORT_SYMBOL(rb_get_first);
2551 + * Given a node, this function will return the next node in the tree.
2553 +rb_node_t *rb_get_next(rb_node_t *n)
2555 + rb_node_t *parent;
2557 + if (n->rb_right) {
2559 + while (n->rb_left)
2563 + while ((parent = n->rb_parent)) {
2564 + if (n == parent->rb_left)
2571 +EXPORT_SYMBOL(rb_get_next);