3 fs/ext3/namei.c | 582 +++++++++++++++++++++++++++++++++++++---------
5 include/linux/ext3_fs.h | 1
6 include/linux/ext3_fs_i.h | 6
7 6 files changed, 500 insertions(+), 109 deletions(-)
9 Index: linux-2.6.7/fs/ext3/namei.c
10 ===================================================================
11 --- linux-2.6.7.orig/fs/ext3/namei.c 2004-08-26 17:12:39.000000000 +0400
12 +++ linux-2.6.7/fs/ext3/namei.c 2004-09-07 17:32:13.000000000 +0400
15 struct buffer_head *bh;
17 + /* with parallel dir operations all appends
18 + * have to be serialized -bzzz */
19 + down(&EXT3_I(inode)->i_append_sem);
20 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
22 if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
24 EXT3_I(inode)->i_disksize = inode->i_size;
25 ext3_journal_get_write_access(handle,bh);
27 + up(&EXT3_I(inode)->i_append_sem);
33 struct buffer_head *bh;
34 struct dx_entry *entries;
37 + unsigned int curidx;
45 +/* FIXME: this should be reworked using bb_spin_lock
46 + * introduced in -mm tree
50 +static inline void dx_lock_bh(struct buffer_head volatile *bh)
53 + while (test_and_set_bit(BH_DXLock, &bh->b_state)) {
54 + while (test_bit(BH_DXLock, &bh->b_state))
60 +static inline void dx_unlock_bh(struct buffer_head *bh)
63 + smp_mb__before_clear_bit();
64 + clear_bit(BH_DXLock, &bh->b_state);
69 #ifdef CONFIG_EXT3_INDEX
70 static inline unsigned dx_get_block (struct dx_entry *entry);
71 static void dx_set_block (struct dx_entry *entry, unsigned value);
73 static void dx_set_limit (struct dx_entry *entries, unsigned value);
74 static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
75 static unsigned dx_node_limit (struct inode *dir);
76 -static struct dx_frame *dx_probe(struct dentry *dentry,
77 +static struct dx_frame *dx_probe(struct qstr *name,
79 struct dx_hash_info *hinfo,
80 struct dx_frame *frame,
82 static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
83 struct dx_map_entry *offsets, int count);
84 static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
85 -static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
86 +static void dx_insert_block (struct inode *, struct dx_frame *, u32, u32, u32);
87 static int ext3_htree_next_block(struct inode *dir, __u32 hash,
88 struct dx_frame *frame,
89 struct dx_frame *frames,
91 static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
92 - struct ext3_dir_entry_2 **res_dir, int *err);
93 + struct ext3_dir_entry_2 **res_dir, int *err,
94 + int rwlock, void **lock);
95 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
97 +static inline void *ext3_lock_htree(struct inode *, unsigned long, int);
98 +static inline void ext3_unlock_htree(struct inode *, void *);
101 * Future: use high four bits of block for coalesce-on-delete flags
103 #endif /* DX_DEBUG */
108 + * search position of specified hash in index
112 +struct dx_entry * dx_find_position(struct dx_entry * entries, u32 hash)
114 + struct dx_entry *p, *q, *m;
117 + count = dx_get_count(entries);
119 + q = entries + count - 1;
123 + if (dx_get_hash(m) > hash)
132 + * returns 1 if path is unchanged
134 +int dx_check_path(struct dx_frame *frame, u32 hash)
136 + struct dx_entry *p;
139 + dx_lock_bh(frame->bh);
140 + p = dx_find_position(frame->entries, hash);
141 + if (frame->leaf != dx_get_block(p))
143 + dx_unlock_bh(frame->bh);
150 + * 1 - hasn't changed
153 +dx_check_full_path(struct dx_frame *frames, struct dx_hash_info *hinfo)
155 + struct dx_entry *p;
156 + struct dx_frame *frame = frames;
159 + /* check first level */
160 + dx_lock_bh(frame->bh);
161 + p = dx_find_position(frame->entries, hinfo->hash);
162 + leaf = dx_get_block(p);
163 + dx_unlock_bh(frame->bh);
165 + if (leaf != frame->leaf)
168 + /* is there 2nd level? */
170 + if (frame->bh == NULL)
173 + /* check second level */
174 + dx_lock_bh(frame->bh);
176 + /* probably 1st level got changed, check it */
177 + if (!dx_check_path(frames, hinfo->hash)) {
179 + dx_unlock_bh(frame->bh);
183 + p = dx_find_position(frame->entries, hinfo->hash);
184 + leaf = dx_get_block(p);
185 + dx_unlock_bh(frame->bh);
187 + if (leaf != frame->leaf)
194 * Probe for a directory leaf block to search.
196 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
197 @@ -328,19 +450,20 @@
200 static struct dx_frame *
201 -dx_probe(struct dentry *dentry, struct inode *dir,
202 +dx_probe(struct qstr *name, struct inode *dir,
203 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
205 - unsigned count, indirect;
206 - struct dx_entry *at, *entries, *p, *q, *m;
208 + struct dx_entry *at, *entries;
209 struct dx_root *root;
210 struct buffer_head *bh;
211 struct dx_frame *frame = frame_in;
213 + unsigned int curidx;
217 - dir = dentry->d_parent->d_inode;
218 + frame[1].bh = NULL;
220 if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
222 root = (struct dx_root *) bh->b_data;
225 hinfo->hash_version = root->info.hash_version;
226 hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
228 - ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
230 + ext3fs_dirhash(name->name, name->len, hinfo);
233 if (root->info.unused_flags & 1) {
240 + entries = (struct dx_entry *) (((char *)&root->info) +
241 + root->info.info_length);
242 + assert(dx_get_limit(entries) == dx_root_limit(dir,
243 + root->info.info_length));
244 + dxtrace (printk("Look up %x", hash));
246 + /* indirect must be initialized under bh lock because
247 + * 2nd level creation procedure may change it and dx_probe()
248 + * will suggest htree is still single-level -bzzz */
249 if ((indirect = root->info.indirect_levels) > 1) {
251 ext3_warning(dir->i_sb, __FUNCTION__,
252 "Unimplemented inode hash depth: %#06x",
253 root->info.indirect_levels);
254 @@ -377,56 +512,46 @@
255 *err = ERR_BAD_DX_DIR;
259 - entries = (struct dx_entry *) (((char *)&root->info) +
260 - root->info.info_length);
261 - assert(dx_get_limit(entries) == dx_root_limit(dir,
262 - root->info.info_length));
263 - dxtrace (printk("Look up %x", hash));
267 - count = dx_get_count(entries);
268 - assert (count && count <= dx_get_limit(entries));
270 - q = entries + count - 1;
274 - dxtrace(printk("."));
275 - if (dx_get_hash(m) > hash)
281 - if (0) // linear search cross check
283 - unsigned n = count - 1;
287 - dxtrace(printk(","));
288 - if (dx_get_hash(++at) > hash)
294 - assert (at == p - 1);
298 - dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
299 + at = dx_find_position(entries, hinfo->hash);
300 + dxtrace(printk(" %x->%u\n",
301 + at == entries? 0: dx_get_hash(at),
302 + dx_get_block(at)));
304 frame->entries = entries;
306 - if (!indirect--) return frame;
307 - if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
308 + frame->curidx = curidx;
309 + frame->leaf = dx_get_block(at);
315 + /* step into next htree level */
316 + curidx = dx_get_block(at);
318 + if (!(bh = ext3_bread (NULL,dir, frame->leaf, 0, err)))
322 + /* splitting may change root index block and move
323 + * hash we're looking for into another index block
324 + * so, we have to check this situation and repeat
325 + * from begining if path got changed -bzzz */
326 + if (!dx_check_path(frame, hash)) {
333 at = entries = ((struct dx_node *) bh->b_data)->entries;
334 assert (dx_get_limit(entries) == dx_node_limit (dir));
339 while (frame >= frame_in) {
343 if (frames[0].bh == NULL)
346 - if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
347 + if (frames[1].bh != NULL)
348 brelse(frames[1].bh);
349 brelse(frames[0].bh);
352 * nodes need to be read.
355 - if (++(p->at) < p->entries + dx_get_count(p->entries))
356 + if (++(p->at) < p->entries + dx_get_count(p->entries)) {
357 + p->leaf = dx_get_block(p->at);
363 @@ -509,13 +635,17 @@
364 * block so no check is necessary
366 while (num_frames--) {
367 - if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
371 + idx = p->leaf = dx_get_block(p->at);
372 + if (!(bh = ext3_bread(NULL, dir, idx, 0, &err)))
373 return err; /* Failure */
377 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
379 + p->leaf = dx_get_block(p->at);
385 hinfo.hash = start_hash;
386 hinfo.minor_hash = 0;
387 - frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
388 + frame = dx_probe(NULL, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
395 /* XXX: do we need to check rec_len == 0 case? -Chris */
396 - de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
397 + de = (struct ext3_dir_entry_2 *)((char*)de +
398 + le16_to_cpu(de->rec_len));
406 -static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
407 +static void dx_insert_block(struct inode *dir, struct dx_frame *frame,
408 + u32 hash, u32 block, u32 idx)
410 struct dx_entry *entries = frame->entries;
411 struct dx_entry *old = frame->at, *new = old + 1;
413 dx_set_hash(new, hash);
414 dx_set_block(new, block);
415 dx_set_count(entries, count + 1);
421 * to brelse() it when appropriate.
423 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
424 - struct ext3_dir_entry_2 ** res_dir)
425 + struct ext3_dir_entry_2 ** res_dir,
426 + int rwlock, void **lock)
428 struct super_block * sb;
429 struct buffer_head * bh_use[NAMEI_RA_SIZE];
434 + int do_not_use_dx = 0;
439 name = dentry->d_name.name;
440 if (namelen > EXT3_NAME_LEN)
443 #ifdef CONFIG_EXT3_INDEX
445 - bh = ext3_dx_find_entry(dentry, res_dir, &err);
446 + bh = ext3_dx_find_entry(dentry, res_dir, &err, rwlock, lock);
448 * On success, or if the error was file not found,
449 * return. Otherwise, fall back to doing a search the
451 if (bh || (err != ERR_BAD_DX_DIR))
453 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
457 + *lock = ext3_lock_htree(dir, 0, rwlock);
458 + if (is_dx(dir) && !do_not_use_dx) {
459 + ext3_unlock_htree(dir, *lock);
462 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
463 start = EXT3_I(dir)->i_dir_start_lookup;
464 if (start >= nblocks)
465 @@ -908,12 +1050,17 @@
466 /* Clean up the read-ahead blocks */
467 for (; ra_ptr < ra_max; ra_ptr++)
468 brelse (bh_use[ra_ptr]);
470 + ext3_unlock_htree(dir, *lock);
476 #ifdef CONFIG_EXT3_INDEX
477 static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
478 - struct ext3_dir_entry_2 **res_dir, int *err)
479 + struct ext3_dir_entry_2 **res_dir, int *err,
480 + int rwlock, void **lock)
482 struct super_block * sb;
483 struct dx_hash_info hinfo;
484 @@ -928,11 +1075,22 @@
485 struct inode *dir = dentry->d_parent->d_inode;
488 - if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
490 + if (!(frame = dx_probe (&dentry->d_name, dir, &hinfo, frames, err)))
493 + *lock = ext3_lock_htree(dir, frame->leaf, rwlock);
494 + /* while locking leaf we just found may get splitted
495 + * so, we need another leaf. check this */
496 + if (!dx_check_full_path(frames, &hinfo)) {
497 + ext3_unlock_htree(dir, *lock);
498 + dx_release(frames);
504 - block = dx_get_block(frame->at);
505 + block = frame->leaf;
506 if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
508 de = (struct ext3_dir_entry_2 *) bh->b_data;
512 dxtrace(printk("%s not found\n", name));
513 + ext3_unlock_htree(dir, *lock);
518 @@ -977,14 +1137,16 @@
519 struct inode * inode;
520 struct ext3_dir_entry_2 * de;
521 struct buffer_head * bh;
524 if (dentry->d_name.len > EXT3_NAME_LEN)
525 return ERR_PTR(-ENAMETOOLONG);
527 - bh = ext3_find_entry(dentry, &de);
528 + bh = ext3_find_entry(dentry, &de, 0, &lock);
531 unsigned long ino = le32_to_cpu(de->inode);
532 + ext3_unlock_htree(dir, lock);
534 inode = iget(dir->i_sb, ino);
536 @@ -1006,17 +1168,19 @@
537 struct dentry dotdot;
538 struct ext3_dir_entry_2 * de;
539 struct buffer_head *bh;
542 dotdot.d_name.name = "..";
543 dotdot.d_name.len = 2;
544 dotdot.d_parent = child; /* confusing, isn't it! */
546 - bh = ext3_find_entry(&dotdot, &de);
547 + bh = ext3_find_entry(&dotdot, &de, 0, &lock);
550 return ERR_PTR(-ENOENT);
551 ino = le32_to_cpu(de->inode);
553 + ext3_unlock_htree(child->d_inode, lock);
554 inode = iget(child->d_inode->i_sb, ino);
557 @@ -1055,7 +1219,8 @@
558 unsigned rec_len = 0;
561 - struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
562 + struct ext3_dir_entry_2 *de =
563 + (struct ext3_dir_entry_2 *) (from + map->offs);
564 rec_len = EXT3_DIR_REC_LEN(de->name_len);
565 memcpy (to, de, rec_len);
566 ((struct ext3_dir_entry_2 *) to)->rec_len =
567 @@ -1069,7 +1234,8 @@
569 static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
571 - struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
572 + struct ext3_dir_entry_2 *next, *to, *prev;
573 + struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) base;
574 unsigned rec_len = 0;
577 @@ -1091,7 +1257,8 @@
579 static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
580 struct buffer_head **bh,struct dx_frame *frame,
581 - struct dx_hash_info *hinfo, int *error)
582 + struct dx_hash_info *hinfo, void **target,
585 unsigned blocksize = dir->i_sb->s_blocksize;
586 unsigned count, continued;
587 @@ -1138,23 +1305,30 @@
588 hash2 = map[split].hash;
589 continued = hash2 == map[split - 1].hash;
590 dxtrace(printk("Split block %i at %x, %i/%i\n",
591 - dx_get_block(frame->at), hash2, split, count-split));
593 + frame->leaf, hash2, split, count-split));
595 /* Fancy dance to stay within two buffers */
596 de2 = dx_move_dirents(data1, data2, map + split, count - split);
597 de = dx_pack_dirents(data1,blocksize);
598 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
599 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
600 - dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
601 - dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
602 + dxtrace(dx_show_leaf(hinfo,(struct ext3_dir_entry_2*) data1, blocksize, 1));
603 + dxtrace(dx_show_leaf(hinfo,(struct ext3_dir_entry_2*) data2, blocksize, 1));
605 /* Which block gets the new entry? */
607 if (hinfo->hash >= hash2)
612 - dx_insert_block (frame, hash2 + continued, newblock);
614 + /* entry will be stored into new block
615 + * we have to lock it before add_dirent_to_buf */
616 + *target = ext3_lock_htree(dir, newblock, 1);
618 + dx_lock_bh(frame->bh);
619 + dx_insert_block (dir, frame, hash2 + continued, newblock, frame->curidx);
620 + dx_unlock_bh(frame->bh);
621 err = ext3_journal_dirty_metadata (handle, bh2);
624 @@ -1228,7 +1402,8 @@
625 nlen = EXT3_DIR_REC_LEN(de->name_len);
626 rlen = le16_to_cpu(de->rec_len);
628 - struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
629 + struct ext3_dir_entry_2 *de1 =
630 + (struct ext3_dir_entry_2 *)((char *)de + nlen);
631 de1->rec_len = cpu_to_le16(rlen - nlen);
632 de->rec_len = cpu_to_le16(nlen);
634 @@ -1287,6 +1462,7 @@
635 struct dx_hash_info hinfo;
637 struct fake_dirent *fde;
638 + void *lock, *new_lock;
640 blocksize = dir->i_sb->s_blocksize;
641 dxtrace(printk("Creating index\n"));
642 @@ -1306,6 +1482,8 @@
643 EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
646 + lock = ext3_lock_htree(dir, block, 1);
648 /* The 0th block becomes the root, move the dirents out */
650 de = (struct ext3_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
651 @@ -1335,13 +1513,25 @@
652 frame->entries = entries;
657 + frame[1].bh = NULL;
659 - de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
660 + de = do_split(handle,dir, &bh, frame, &hinfo, &new_lock, &retval);
666 + retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
669 + ext3_unlock_htree(dir, new_lock);
670 + /* we mark directory indexed in order to
671 + * avoid races while htree being created -bzzz */
672 + EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
673 + ext3_unlock_htree(dir, lock);
675 - return add_dirent_to_buf(handle, dentry, inode, de, bh);
680 @@ -1370,11 +1560,13 @@
687 blocksize = sb->s_blocksize;
688 if (!dentry->d_name.len)
691 #ifdef CONFIG_EXT3_INDEX
693 retval = ext3_dx_add_entry(handle, dentry, inode);
694 @@ -1385,36 +1577,53 @@
695 ext3_mark_inode_dirty(handle, dir);
698 + lock = ext3_lock_htree(dir, 0, 1);
700 + /* we got lock for block 0
701 + * probably previous holder of the lock
702 + * created htree -bzzz */
703 + ext3_unlock_htree(dir, lock);
707 blocks = dir->i_size >> sb->s_blocksize_bits;
708 for (block = 0, offset = 0; block < blocks; block++) {
709 bh = ext3_bread(handle, dir, block, 0, &retval);
712 + ext3_unlock_htree(dir, lock);
715 retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
716 - if (retval != -ENOSPC)
717 + if (retval != -ENOSPC) {
718 + ext3_unlock_htree(dir, lock);
722 #ifdef CONFIG_EXT3_INDEX
723 if (blocks == 1 && !dx_fallback &&
724 - EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
725 - return make_indexed_dir(handle, dentry, inode, bh);
726 + EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX)) {
727 + retval = make_indexed_dir(handle, dentry, inode, bh);
728 + ext3_unlock_htree(dir, lock);
734 bh = ext3_append(handle, dir, &block, &retval);
737 + ext3_unlock_htree(dir, lock);
740 de = (struct ext3_dir_entry_2 *) bh->b_data;
742 de->rec_len = cpu_to_le16(rlen = blocksize);
744 - return add_dirent_to_buf(handle, dentry, inode, de, bh);
745 + retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
746 + ext3_unlock_htree(dir, lock);
750 #ifdef CONFIG_EXT3_INDEX
752 - * Returns 0 for success, or a negative error value
754 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
757 @@ -1426,15 +1635,28 @@
758 struct super_block * sb = dir->i_sb;
759 struct ext3_dir_entry_2 *de;
762 - frame = dx_probe(dentry, 0, &hinfo, frames, &err);
764 + void *idx_lock, *leaf_lock, *newleaf_lock;
767 + frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
770 - entries = frame->entries;
773 - if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
774 + /* we're going to chage leaf, so lock it first */
775 + leaf_lock = ext3_lock_htree(dir, frame->leaf, 1);
777 + /* while locking leaf we just found may get splitted
778 + * so we need to check this */
779 + if (!dx_check_full_path(frames, &hinfo)) {
780 + ext3_unlock_htree(dir, leaf_lock);
781 + dx_release(frames);
784 + if (!(bh = ext3_bread(handle,dir, frame->leaf, 0, &err))) {
785 + printk("can't ext3_bread(%d) = %d\n", (int) frame->leaf, err);
789 BUFFER_TRACE(bh, "get_write_access");
790 err = ext3_journal_get_write_access(handle, bh);
791 @@ -1447,6 +1669,35 @@
795 + /* our leaf has no enough space. hence, we have to
796 + * split it. so lock index for this leaf first */
797 + curidx = frame->curidx;
798 + idx_lock = ext3_lock_htree(dir, curidx, 1);
800 + /* now check did path get changed? */
801 + dx_release(frames);
803 + frame = dx_probe(&dentry->d_name, dentry->d_parent->d_inode,
804 + &hinfo, frames, &err);
806 + /* FIXME: error handling here */
808 + ext3_unlock_htree(dir, idx_lock);
812 + if (frame->curidx != curidx) {
813 + /* path has been changed. we have to drop old lock
816 + ext3_unlock_htree(dir, idx_lock);
817 + ext3_unlock_htree(dir, leaf_lock);
818 + dx_release(frames);
821 + entries = frame->entries;
824 /* Block full, should compress but for now just split */
825 dxtrace(printk("using %u of %u node entries\n",
826 dx_get_count(entries), dx_get_limit(entries)));
827 @@ -1458,7 +1709,8 @@
828 struct dx_entry *entries2;
829 struct dx_node *node2;
830 struct buffer_head *bh2;
834 if (levels && (dx_get_count(frames->entries) ==
835 dx_get_limit(frames->entries))) {
836 ext3_warning(sb, __FUNCTION__,
837 @@ -1469,6 +1721,7 @@
838 bh2 = ext3_append (handle, dir, &newblock, &err);
841 + nb_lock = ext3_lock_htree(dir, newblock, 1);
842 node2 = (struct dx_node *)(bh2->b_data);
843 entries2 = node2->entries;
844 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
845 @@ -1480,27 +1733,73 @@
847 unsigned icount1 = icount/2, icount2 = icount - icount1;
848 unsigned hash2 = dx_get_hash(entries + icount1);
849 - dxtrace(printk("Split index %i/%i\n", icount1, icount2));
852 - BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
853 + /* we have to protect root htree index against
854 + * another dx_add_entry() which would want to
855 + * split it too -bzzz */
856 + ri_lock = ext3_lock_htree(dir, 0, 1);
858 + /* as root index block blocked we must repeat
859 + * searching for current position of our 2nd index -bzzz */
860 + dx_lock_bh(frame->bh);
861 + frames->at = dx_find_position(frames->entries, hinfo.hash);
862 + dx_unlock_bh(frame->bh);
864 + dxtrace(printk("Split index %i/%i\n", icount1, icount2));
866 + BUFFER_TRACE(frame->bh, "get_write_access");
867 err = ext3_journal_get_write_access(handle,
872 + /* copy index into new one */
873 memcpy ((char *) entries2, (char *) (entries + icount1),
874 icount2 * sizeof(struct dx_entry));
875 - dx_set_count (entries, icount1);
876 dx_set_count (entries2, icount2);
877 dx_set_limit (entries2, dx_node_limit(dir));
879 /* Which index block gets the new entry? */
880 if (at - entries >= icount1) {
881 + /* unlock index we won't use */
882 + ext3_unlock_htree(dir, idx_lock);
883 + idx_lock = nb_lock;
884 frame->at = at = at - entries - icount1 + entries2;
885 - frame->entries = entries = entries2;
886 + frame->entries = entries2;
887 + frame->curidx = curidx = newblock;
888 swap(frame->bh, bh2);
890 + /* we'll use old index,so new one may be freed */
891 + ext3_unlock_htree(dir, nb_lock);
893 - dx_insert_block (frames + 0, hash2, newblock);
895 + /* NOTE: very subtle piece of code
896 + * competing dx_probe() may find 2nd level index in root
897 + * index, then we insert new index here and set new count
898 + * in that 2nd level index. so, dx_probe() may see 2nd
899 + * level index w/o hash it looks for. the solution is
900 + * to check root index after we locked just founded 2nd
901 + * level index -bzzz */
902 + dx_lock_bh(frames[0].bh);
903 + dx_insert_block (dir, frames + 0, hash2, newblock, 0);
904 + dx_unlock_bh(frames[0].bh);
906 + /* now old and new 2nd level index blocks contain
907 + * all pointers, so dx_probe() may find it in the both.
910 + dx_lock_bh(frame->bh);
911 + dx_set_count(entries, icount1);
912 + dx_unlock_bh(frame->bh);
914 + /* now old 2nd level index block points to first half
915 + * of leafs. it's importand that dx_probe() must
916 + * check root index block for changes under
917 + * dx_lock_bh(frame->bh) -bzzz */
919 + ext3_unlock_htree(dir, ri_lock);
921 dxtrace(dx_show_index ("node", frames[1].entries));
922 dxtrace(dx_show_index ("node",
923 ((struct dx_node *) bh2->b_data)->entries));
924 @@ -1509,38 +1808,61 @@
928 + unsigned long leaf = frame->leaf;
930 dxtrace(printk("Creating second level index...\n"));
931 memcpy((char *) entries2, (char *) entries,
932 icount * sizeof(struct dx_entry));
933 dx_set_limit(entries2, dx_node_limit(dir));
936 + dx_lock_bh(frames[0].bh);
937 dx_set_count(entries, 1);
938 dx_set_block(entries + 0, newblock);
939 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
940 + dx_unlock_bh(frames[0].bh);
942 /* Add new access path frame */
944 frame->at = at = at - entries + entries2;
945 frame->entries = entries = entries2;
947 + frame->curidx = newblock;
948 + frame->leaf = leaf;
949 err = ext3_journal_get_write_access(handle,
954 + /* first level index was root. it's already initialized */
955 + /* we my unlock it now */
956 + ext3_unlock_htree(dir, idx_lock);
958 + /* current index is just created 2nd level index */
960 + idx_lock = nb_lock;
962 ext3_journal_dirty_metadata(handle, frames[0].bh);
964 - de = do_split(handle, dir, &bh, frame, &hinfo, &err);
965 + de = do_split(handle, dir, &bh, frame, &hinfo, &newleaf_lock, &err);
969 + /* index splitted */
970 + ext3_unlock_htree(dir, idx_lock);
972 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
975 + ext3_unlock_htree(dir, newleaf_lock);
981 ext3_std_error(dir->i_sb, err);
983 + ext3_unlock_htree(dir, leaf_lock);
987 @@ -1981,6 +2303,7 @@
988 struct buffer_head * bh;
989 struct ext3_dir_entry_2 * de;
993 /* Initialize quotas before so that eventual writes go in
994 * separate transaction */
995 @@ -1990,7 +2313,7 @@
996 return PTR_ERR(handle);
999 - bh = ext3_find_entry (dentry, &de);
1000 + bh = ext3_find_entry (dentry, &de, 1, &lock);
1004 @@ -2000,14 +2323,19 @@
1005 inode = dentry->d_inode;
1008 - if (le32_to_cpu(de->inode) != inode->i_ino)
1009 + if (le32_to_cpu(de->inode) != inode->i_ino) {
1010 + ext3_unlock_htree(dir, lock);
1014 retval = -ENOTEMPTY;
1015 - if (!empty_dir (inode))
1016 + if (!empty_dir (inode)) {
1017 + ext3_unlock_htree(dir, lock);
1021 retval = ext3_delete_entry(handle, dir, de, bh);
1022 + ext3_unlock_htree(dir, lock);
1025 if (inode->i_nlink != 2)
1026 @@ -2040,6 +2368,7 @@
1027 struct buffer_head * bh;
1028 struct ext3_dir_entry_2 * de;
1032 /* Initialize quotas before so that eventual writes go
1033 * in separate transaction */
1034 @@ -2052,15 +2381,17 @@
1038 - bh = ext3_find_entry (dentry, &de);
1039 + bh = ext3_find_entry (dentry, &de, 1, &lock);
1043 inode = dentry->d_inode;
1046 - if (le32_to_cpu(de->inode) != inode->i_ino)
1047 + if (le32_to_cpu(de->inode) != inode->i_ino) {
1048 + ext3_unlock_htree(dir, lock);
1052 if (!inode->i_nlink) {
1053 ext3_warning (inode->i_sb, "ext3_unlink",
1054 @@ -2069,6 +2400,7 @@
1057 retval = ext3_delete_entry(handle, dir, de, bh);
1058 + ext3_unlock_htree(dir, lock);
1061 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1062 @@ -2182,6 +2514,7 @@
1063 struct buffer_head * old_bh, * new_bh, * dir_bh;
1064 struct ext3_dir_entry_2 * old_de, * new_de;
1066 + void *lock1 = NULL, *lock2 = NULL, *lock3 = NULL;
1068 old_bh = new_bh = dir_bh = NULL;
1070 @@ -2197,7 +2530,10 @@
1071 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1074 - old_bh = ext3_find_entry (old_dentry, &old_de);
1075 + if (old_dentry->d_parent == new_dentry->d_parent)
1076 + down(&EXT3_I(old_dentry->d_parent->d_inode)->i_rename_sem);
1078 + old_bh = ext3_find_entry (old_dentry, &old_de, 1, &lock1 /* FIXME */);
1080 * Check for inode number is _not_ due to possible IO errors.
1081 * We might rmdir the source, keep it as pwd of some process
1082 @@ -2210,7 +2546,7 @@
1085 new_inode = new_dentry->d_inode;
1086 - new_bh = ext3_find_entry (new_dentry, &new_de);
1087 + new_bh = ext3_find_entry (new_dentry, &new_de, 1, &lock2 /* FIXME */);
1091 @@ -2274,7 +2610,7 @@
1092 struct buffer_head *old_bh2;
1093 struct ext3_dir_entry_2 *old_de2;
1095 - old_bh2 = ext3_find_entry(old_dentry, &old_de2);
1096 + old_bh2 = ext3_find_entry(old_dentry, &old_de2, 1, &lock3 /* FIXME */);
1098 retval = ext3_delete_entry(handle, old_dir,
1100 @@ -2317,6 +2653,14 @@
1105 + ext3_unlock_htree(old_dentry->d_parent->d_inode, lock1);
1107 + ext3_unlock_htree(new_dentry->d_parent->d_inode, lock2);
1109 + ext3_unlock_htree(old_dentry->d_parent->d_inode, lock3);
1110 + if (old_dentry->d_parent == new_dentry->d_parent)
1111 + up(&EXT3_I(old_dentry->d_parent->d_inode)->i_rename_sem);
1115 @@ -2325,6 +2669,29 @@
1119 + * this locking primitives are used to protect parts
1120 + * of dir's htree. protection unit is block: leaf or index
1122 +static inline void *ext3_lock_htree(struct inode *dir,
1123 + unsigned long value, int rwlock)
1127 + if (!test_opt(dir->i_sb, PDIROPS))
1129 + lock = dynlock_lock(&EXT3_I(dir)->i_htree_lock, value, 1, GFP_KERNEL);
1133 +static inline void ext3_unlock_htree(struct inode *dir,
1136 + if (!test_opt(dir->i_sb, PDIROPS) || !lock)
1138 + dynlock_unlock(&EXT3_I(dir)->i_htree_lock, lock);
1142 * directories can handle most operations...
1144 struct inode_operations ext3_dir_inode_operations = {
1145 Index: linux-2.6.7/fs/ext3/super.c
1146 ===================================================================
1147 --- linux-2.6.7.orig/fs/ext3/super.c 2004-09-07 14:12:13.000000000 +0400
1148 +++ linux-2.6.7/fs/ext3/super.c 2004-09-07 17:17:37.000000000 +0400
1150 ei->i_default_acl = EXT3_ACL_NOT_CACHED;
1152 ei->vfs_inode.i_version = 1;
1153 + dynlock_init(&ei->i_htree_lock);
1154 + sema_init(&ei->i_rename_sem, 1);
1155 + sema_init(&ei->i_append_sem, 1);
1156 return &ei->vfs_inode;
1160 Opt_commit, Opt_journal_update, Opt_journal_inum,
1161 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1162 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1163 - Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0,
1164 + Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_pdirops,
1165 Opt_ignore, Opt_err,
1169 {Opt_ignore, "noquota"},
1170 {Opt_ignore, "quota"},
1171 {Opt_ignore, "usrquota"},
1172 + {Opt_pdirops, "pdirops"},
1176 Index: linux-2.6.7/include/linux/ext3_fs.h
1177 ===================================================================
1178 --- linux-2.6.7.orig/include/linux/ext3_fs.h 2003-09-19 18:01:10.000000000 +0400
1179 +++ linux-2.6.7/include/linux/ext3_fs.h 2004-09-07 17:17:37.000000000 +0400
1184 +#define EXT3_MOUNT_PDIROPS 0x800000/* Parallel dir operations */
1185 #define EXT3_MOUNT_CHECK 0x0001 /* Do mount-time checks */
1186 #define EXT3_MOUNT_OLDALLOC 0x0002 /* Don't use the new Orlov allocator */
1187 #define EXT3_MOUNT_GRPID 0x0004 /* Create files with directory's group */
1188 Index: linux-2.6.7/include/linux/ext3_fs_i.h
1189 ===================================================================
1190 --- linux-2.6.7.orig/include/linux/ext3_fs_i.h 2003-12-30 08:32:44.000000000 +0300
1191 +++ linux-2.6.7/include/linux/ext3_fs_i.h 2004-09-07 17:17:37.000000000 +0400
1193 #define _LINUX_EXT3_FS_I
1195 #include <linux/rwsem.h>
1196 +#include <linux/dynlocks.h>
1199 * second extended file system inode data in memory
1200 @@ -108,6 +109,11 @@
1202 struct semaphore truncate_sem;
1203 struct inode vfs_inode;
1205 + /* following fields for parallel directory operations -bzzz */
1206 + struct dynlock i_htree_lock;
1207 + struct semaphore i_append_sem;
1208 + struct semaphore i_rename_sem;
1211 #endif /* _LINUX_EXT3_FS_I */