1 LU-50 ldiskfs: pdirops patch for ldiskfs
3 Single directory performance is a critical for HPC workloads. In a
4 typical use case an application creates a separate output file for
5 each node and task in a job. As nodes and tasks increase, hundreds
6 of thousands of files may be created in a single directory within
7 a short window of time.
8 Today, both filename lookup and file system modifying operations
9 (such as create and unlink) are protected with a single lock for
10 an entire ldiskfs directory. PDO project will remove this
11 bottleneck by introducing a parallel locking mechanism for entire
12 ldiskfs directories. This work will enable multiple application
13 threads to simultaneously lookup, create and unlink in parallel.
16 - pdirops support for ldiskfs
17 - N-level htree directory
18 - integrate with osd-ldiskfs
20 Signed-off-by: Liang Zhen <liang@whamcloud.com>
21 Change-Id: I269c0e3112e68f3acd79e860dab052a68c7d7aaa
22 Reviewed-on: http://review.whamcloud.com/375
23 Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
25 fs/ext4/Makefile | 1 +
26 fs/ext4/ext4.h | 78 ++++++++
27 fs/ext4/namei.c | 454 ++++++++++++++++++++++++++++++++++++++++++-----
29 4 files changed, 494 insertions(+), 40 deletions(-)
31 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
32 index 49e7af6..f7ced03 100644
33 --- a/fs/ext4/Makefile
34 +++ b/fs/ext4/Makefile
35 @@ -7,6 +7,7 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
37 ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
38 extents_status.o file.o fsmap.o fsync.o hash.o ialloc.o \
40 indirect.o inline.o inode.o ioctl.o mballoc.o migrate.o \
41 mmp.o move_extent.o namei.o page-io.o readpage.o resize.o \
42 super.o symlink.o sysfs.o xattr.o xattr_hurd.o xattr_trusted.o \
43 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
44 index 024f203..21d83fc 100644
48 #include <linux/mutex.h>
49 #include <linux/timer.h>
50 #include <linux/wait.h>
51 +#include <linux/htree_lock.h>
52 #include <linux/sched/signal.h>
53 #include <linux/blockgroup_lock.h>
54 #include <linux/percpu_counter.h>
55 @@ -1020,6 +1021,9 @@ struct ext4_inode_info {
57 ext4_fsblk_t i_file_acl;
59 + /* following fields for parallel directory operations -bzzz */
60 + struct semaphore i_append_sem;
63 * i_block_group is the number of the block group which contains
64 * this file's inode. Constant across the lifetime of the inode,
65 @@ -2504,6 +2508,72 @@ struct dx_hash_info
67 #define HASH_NB_ALWAYS 1
69 +/* assume name-hash is protected by upper layer */
70 +#define EXT4_HTREE_LOCK_HASH 0
72 +enum ext4_pdo_lk_types {
73 +#if EXT4_HTREE_LOCK_HASH
76 + EXT4_LK_DX, /* index block */
77 + EXT4_LK_DE, /* directory entry block */
78 + EXT4_LK_SPIN, /* spinlock */
83 +#define EXT4_LB_RO(b) (1 << (b))
84 +/* read + write, high bits for writer */
85 +#define EXT4_LB_RW(b) ((1 << (b)) | (1 << (EXT4_LK_MAX + (b))))
87 +enum ext4_pdo_lock_bits {
89 + EXT4_LB_DX_RO = EXT4_LB_RO(EXT4_LK_DX),
90 + EXT4_LB_DX = EXT4_LB_RW(EXT4_LK_DX),
92 + EXT4_LB_DE_RO = EXT4_LB_RO(EXT4_LK_DE),
93 + EXT4_LB_DE = EXT4_LB_RW(EXT4_LK_DE),
94 + /* DX spinlock bits */
95 + EXT4_LB_SPIN_RO = EXT4_LB_RO(EXT4_LK_SPIN),
96 + EXT4_LB_SPIN = EXT4_LB_RW(EXT4_LK_SPIN),
97 + /* accurate searching */
98 + EXT4_LB_EXACT = EXT4_LB_RO(EXT4_LK_MAX << 1),
101 +enum ext4_pdo_lock_opc {
103 + EXT4_HLOCK_READDIR = (EXT4_LB_DE_RO | EXT4_LB_DX_RO),
104 + EXT4_HLOCK_LOOKUP = (EXT4_LB_DE_RO | EXT4_LB_SPIN_RO |
106 + EXT4_HLOCK_DEL = (EXT4_LB_DE | EXT4_LB_SPIN_RO |
108 + EXT4_HLOCK_ADD = (EXT4_LB_DE | EXT4_LB_SPIN_RO),
111 + EXT4_HLOCK_LOOKUP_SAFE = (EXT4_LB_DE_RO | EXT4_LB_DX_RO |
113 + EXT4_HLOCK_DEL_SAFE = (EXT4_LB_DE | EXT4_LB_DX_RO | EXT4_LB_EXACT),
114 + EXT4_HLOCK_SPLIT = (EXT4_LB_DE | EXT4_LB_DX | EXT4_LB_SPIN),
117 +extern struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits);
118 +#define ext4_htree_lock_head_free(lhead) htree_lock_head_free(lhead)
120 +extern struct htree_lock *ext4_htree_lock_alloc(void);
121 +#define ext4_htree_lock_free(lck) htree_lock_free(lck)
123 +extern void ext4_htree_lock(struct htree_lock *lck,
124 + struct htree_lock_head *lhead,
125 + struct inode *dir, unsigned flags);
126 +#define ext4_htree_unlock(lck) htree_unlock(lck)
128 +extern struct buffer_head *ext4_find_entry_locked(struct inode *dir,
129 + const struct qstr *d_name,
130 + struct ext4_dir_entry_2 **res_dir,
131 + int *inlined, struct htree_lock *lck);
132 +extern int ext4_add_entry_locked(handle_t *handle, struct dentry *dentry,
133 + struct inode *inode, struct htree_lock *lck);
135 struct ext4_filename {
136 const struct qstr *usr_fname;
137 struct fscrypt_str disk_name;
138 @@ -2882,12 +2952,20 @@ void ext4_insert_dentry(struct inode *dir, struct inode *inode,
140 static inline void ext4_update_dx_flag(struct inode *inode)
142 + /* Disable it for ldiskfs, because going from a DX directory to
143 + * a non-DX directory while it is in use will completely break
144 + * the htree-locking.
145 + * If we really want to support this operation in the future,
146 + * we need to exclusively lock the directory at here which will
147 + * increase complexity of code */
149 if (!ext4_has_feature_dir_index(inode->i_sb) &&
150 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
151 /* ext4_iget() should have caught this... */
152 WARN_ON_ONCE(ext4_has_feature_metadata_csum(inode->i_sb));
153 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
157 static const unsigned char ext4_filetype_table[] = {
158 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
159 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
160 index 059bc08..7d25879 100644
161 --- a/fs/ext4/namei.c
162 +++ b/fs/ext4/namei.c
163 @@ -55,6 +55,7 @@ struct buffer_head *ext4_append(handle_t *handle,
166 struct buffer_head *bh;
167 + struct ext4_inode_info *ei = EXT4_I(inode);
170 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
171 @@ -62,16 +63,23 @@ struct buffer_head *ext4_append(handle_t *handle,
172 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
173 return ERR_PTR(-ENOSPC);
175 + /* with parallel dir operations all appends
176 + * have to be serialized -bzzz */
177 + down(&ei->i_append_sem);
179 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
181 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
184 + up(&ei->i_append_sem);
187 inode->i_size += inode->i_sb->s_blocksize;
188 EXT4_I(inode)->i_disksize = inode->i_size;
189 BUFFER_TRACE(bh, "get_write_access");
190 err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
192 + up(&ei->i_append_sem);
195 ext4_std_error(inode->i_sb, err);
196 @@ -268,7 +276,8 @@ static unsigned dx_node_limit(struct inode *dir);
197 static struct dx_frame *dx_probe(struct ext4_filename *fname,
199 struct dx_hash_info *hinfo,
200 - struct dx_frame *frame);
201 + struct dx_frame *frame,
202 + struct htree_lock *lck);
203 static void dx_release(struct dx_frame *frames, struct inode *dir);
204 static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
205 unsigned blocksize, struct dx_hash_info *hinfo,
206 @@ -284,12 +293,13 @@ static void dx_insert_block(struct dx_frame *frame,
207 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
208 struct dx_frame *frame,
209 struct dx_frame *frames,
210 - __u32 *start_hash);
211 + __u32 *start_hash, struct htree_lock *lck);
212 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
213 struct ext4_filename *fname,
214 - struct ext4_dir_entry_2 **res_dir);
215 + struct ext4_dir_entry_2 **res_dir, struct htree_lock *lck);
216 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
217 - struct inode *dir, struct inode *inode);
218 + struct inode *dir, struct inode *inode,
219 + struct htree_lock *lck);
221 /* checksumming functions */
222 void ext4_initialize_dirent_tail(struct buffer_head *bh,
223 @@ -774,6 +784,227 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
225 #endif /* DX_DEBUG */
227 +/* private data for htree_lock */
228 +struct ext4_dir_lock_data {
229 + unsigned ld_flags; /* bits-map for lock types */
230 + unsigned ld_count; /* # entries of the last DX block */
231 + struct dx_entry ld_at_entry; /* copy of leaf dx_entry */
232 + struct dx_entry *ld_at; /* position of leaf dx_entry */
235 +#define ext4_htree_lock_data(l) ((struct ext4_dir_lock_data *)(l)->lk_private)
236 +#define ext4_find_entry(dir, name, dirent, inline) \
237 + ext4_find_entry_locked(dir, name, dirent, inline, NULL)
238 +#define ext4_add_entry(handle, dentry, inode) \
239 + ext4_add_entry_locked(handle, dentry, inode, NULL)
241 +/* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
242 +#define EXT4_HTREE_NODE_CHANGED (0xcafeULL << 32)
244 +static void ext4_htree_event_cb(void *target, void *event)
246 + u64 *block = (u64 *)target;
248 + if (*block == dx_get_block((struct dx_entry *)event))
249 + *block = EXT4_HTREE_NODE_CHANGED;
252 +struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits)
254 + struct htree_lock_head *lhead;
256 + lhead = htree_lock_head_alloc(EXT4_LK_MAX, hbits, 0);
257 + if (lhead != NULL) {
258 + htree_lock_event_attach(lhead, EXT4_LK_SPIN, HTREE_EVENT_WR,
259 + ext4_htree_event_cb);
263 +EXPORT_SYMBOL(ext4_htree_lock_head_alloc);
265 +struct htree_lock *ext4_htree_lock_alloc(void)
267 + return htree_lock_alloc(EXT4_LK_MAX,
268 + sizeof(struct ext4_dir_lock_data));
270 +EXPORT_SYMBOL(ext4_htree_lock_alloc);
272 +static htree_lock_mode_t ext4_htree_mode(unsigned flags)
275 + default: /* 0 or unknown flags require EX lock */
276 + return HTREE_LOCK_EX;
277 + case EXT4_HLOCK_READDIR:
278 + return HTREE_LOCK_PR;
279 + case EXT4_HLOCK_LOOKUP:
280 + return HTREE_LOCK_CR;
281 + case EXT4_HLOCK_DEL:
282 + case EXT4_HLOCK_ADD:
283 + return HTREE_LOCK_CW;
287 +/* return PR for read-only operations, otherwise return EX */
288 +static inline htree_lock_mode_t ext4_htree_safe_mode(unsigned flags)
290 + int writer = (flags & EXT4_LB_DE) == EXT4_LB_DE;
292 + /* 0 requires EX lock */
293 + return (flags == 0 || writer) ? HTREE_LOCK_EX : HTREE_LOCK_PR;
296 +static int ext4_htree_safe_locked(struct htree_lock *lck)
300 + if (lck == NULL || lck->lk_mode == HTREE_LOCK_EX)
303 + writer = (ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_DE) ==
305 + if (writer) /* all readers & writers are excluded? */
306 + return lck->lk_mode == HTREE_LOCK_EX;
308 + /* all writers are excluded? */
309 + return lck->lk_mode == HTREE_LOCK_PR ||
310 + lck->lk_mode == HTREE_LOCK_PW ||
311 + lck->lk_mode == HTREE_LOCK_EX;
314 +/* relock htree_lock with EX mode if it's change operation, otherwise
315 + * relock it with PR mode. It's noop if PDO is disabled. */
316 +static void ext4_htree_safe_relock(struct htree_lock *lck)
318 + if (!ext4_htree_safe_locked(lck)) {
319 + unsigned flags = ext4_htree_lock_data(lck)->ld_flags;
321 + htree_change_lock(lck, ext4_htree_safe_mode(flags));
325 +void ext4_htree_lock(struct htree_lock *lck, struct htree_lock_head *lhead,
326 + struct inode *dir, unsigned flags)
328 + htree_lock_mode_t mode = is_dx(dir) ? ext4_htree_mode(flags) :
329 + ext4_htree_safe_mode(flags);
331 + ext4_htree_lock_data(lck)->ld_flags = flags;
332 + htree_lock(lck, lhead, mode);
334 + ext4_htree_safe_relock(lck); /* make sure it's safe locked */
336 +EXPORT_SYMBOL(ext4_htree_lock);
338 +static int ext4_htree_node_lock(struct htree_lock *lck, struct dx_entry *at,
339 + unsigned lmask, int wait, void *ev)
341 + u32 key = (at == NULL) ? 0 : dx_get_block(at);
344 + /* NOOP if htree is well protected or caller doesn't require the lock */
345 + if (ext4_htree_safe_locked(lck) ||
346 + !(ext4_htree_lock_data(lck)->ld_flags & lmask))
349 + mode = (ext4_htree_lock_data(lck)->ld_flags & lmask) == lmask ?
350 + HTREE_LOCK_PW : HTREE_LOCK_PR;
352 + if (htree_node_lock_try(lck, mode, key, ffz(~lmask), wait, ev))
354 + if (!(lmask & EXT4_LB_SPIN)) /* not a spinlock */
356 + cpu_relax(); /* spin until granted */
360 +static int ext4_htree_node_locked(struct htree_lock *lck, unsigned lmask)
362 + return ext4_htree_safe_locked(lck) ||
363 + htree_node_is_granted(lck, ffz(~lmask));
366 +static void ext4_htree_node_unlock(struct htree_lock *lck,
367 + unsigned lmask, void *buf)
369 + /* NB: it's safe to call mutiple times or even it's not locked */
370 + if (!ext4_htree_safe_locked(lck) &&
371 + htree_node_is_granted(lck, ffz(~lmask)))
372 + htree_node_unlock(lck, ffz(~lmask), buf);
375 +#define ext4_htree_dx_lock(lck, key) \
376 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 1, NULL)
377 +#define ext4_htree_dx_lock_try(lck, key) \
378 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 0, NULL)
379 +#define ext4_htree_dx_unlock(lck) \
380 + ext4_htree_node_unlock(lck, EXT4_LB_DX, NULL)
381 +#define ext4_htree_dx_locked(lck) \
382 + ext4_htree_node_locked(lck, EXT4_LB_DX)
384 +static void ext4_htree_dx_need_lock(struct htree_lock *lck)
386 + struct ext4_dir_lock_data *ld;
388 + if (ext4_htree_safe_locked(lck))
391 + ld = ext4_htree_lock_data(lck);
392 + switch (ld->ld_flags) {
395 + case EXT4_HLOCK_LOOKUP:
396 + ld->ld_flags = EXT4_HLOCK_LOOKUP_SAFE;
398 + case EXT4_HLOCK_DEL:
399 + ld->ld_flags = EXT4_HLOCK_DEL_SAFE;
401 + case EXT4_HLOCK_ADD:
402 + ld->ld_flags = EXT4_HLOCK_SPLIT;
407 +#define ext4_htree_de_lock(lck, key) \
408 + ext4_htree_node_lock(lck, key, EXT4_LB_DE, 1, NULL)
409 +#define ext4_htree_de_unlock(lck) \
410 + ext4_htree_node_unlock(lck, EXT4_LB_DE, NULL)
412 +#define ext4_htree_spin_lock(lck, key, event) \
413 + ext4_htree_node_lock(lck, key, EXT4_LB_SPIN, 0, event)
414 +#define ext4_htree_spin_unlock(lck) \
415 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, NULL)
416 +#define ext4_htree_spin_unlock_listen(lck, p) \
417 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, p)
419 +static void ext4_htree_spin_stop_listen(struct htree_lock *lck)
421 + if (!ext4_htree_safe_locked(lck) &&
422 + htree_node_is_listening(lck, ffz(~EXT4_LB_SPIN)))
423 + htree_node_stop_listen(lck, ffz(~EXT4_LB_SPIN));
427 + DX_HASH_COL_IGNORE, /* ignore collision while probing frames */
428 + DX_HASH_COL_YES, /* there is collision and it does matter */
429 + DX_HASH_COL_NO, /* there is no collision */
432 +static int dx_probe_hash_collision(struct htree_lock *lck,
433 + struct dx_entry *entries,
434 + struct dx_entry *at, u32 hash)
436 + if (!(lck && ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_EXACT)) {
437 + return DX_HASH_COL_IGNORE; /* don't care about collision */
439 + } else if (at == entries + dx_get_count(entries) - 1) {
440 + return DX_HASH_COL_IGNORE; /* not in any leaf of this DX */
442 + } else { /* hash collision? */
443 + return ((dx_get_hash(at + 1) & ~1) == hash) ?
444 + DX_HASH_COL_YES : DX_HASH_COL_NO;
449 * Probe for a directory leaf block to search.
451 @@ -785,10 +1016,11 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
453 static struct dx_frame *
454 dx_probe(struct ext4_filename *fname, struct inode *dir,
455 - struct dx_hash_info *hinfo, struct dx_frame *frame_in)
456 + struct dx_hash_info *hinfo, struct dx_frame *frame_in,
457 + struct htree_lock *lck)
459 unsigned count, indirect;
460 - struct dx_entry *at, *entries, *p, *q, *m;
461 + struct dx_entry *at, *entries, *p, *q, *m, *dx = NULL;
462 struct dx_root_info *info;
463 struct dx_frame *frame = frame_in;
464 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
465 @@ -868,8 +1100,15 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
467 dxtrace(printk("Look up %x", hash));
469 + if (indirect == 0) { /* the last index level */
470 + /* NB: ext4_htree_dx_lock() could be noop if
471 + * DX-lock flag is not set for current operation */
472 + ext4_htree_dx_lock(lck, dx);
473 + ext4_htree_spin_lock(lck, dx, NULL);
475 count = dx_get_count(entries);
476 - if (!count || count > dx_get_limit(entries)) {
477 + if (count == 0 || count > dx_get_limit(entries)) {
478 + ext4_htree_spin_unlock(lck); /* release spin */
479 ext4_warning_inode(dir,
480 "dx entry: count %u beyond limit %u",
481 count, dx_get_limit(entries));
482 @@ -895,8 +1134,70 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
484 frame->entries = entries;
488 + if (indirect == 0) { /* the last index level */
489 + struct ext4_dir_lock_data *ld;
492 + /* By default we only lock DE-block, however, we will
493 + * also lock the last level DX-block if:
494 + * a) there is hash collision
495 + * we will set DX-lock flag (a few lines below)
496 + * and redo to lock DX-block
497 + * see detail in dx_probe_hash_collision()
498 + * b) it's a retry from splitting
499 + * we need to lock the last level DX-block so nobody
500 + * else can split any leaf blocks under the same
501 + * DX-block, see detail in ext4_dx_add_entry()
503 + if (ext4_htree_dx_locked(lck)) {
504 + /* DX-block is locked, just lock DE-block
506 + ext4_htree_spin_unlock(lck);
507 + if (!ext4_htree_safe_locked(lck))
508 + ext4_htree_de_lock(lck, frame->at);
511 + /* it's pdirop and no DX lock */
512 + if (dx_probe_hash_collision(lck, entries, at, hash) ==
514 + /* found hash collision, set DX-lock flag
515 + * and retry to abtain DX-lock */
516 + ext4_htree_spin_unlock(lck);
517 + ext4_htree_dx_need_lock(lck);
520 + ld = ext4_htree_lock_data(lck);
521 + /* because I don't lock DX, so @at can't be trusted
522 + * after I release spinlock so I have to save it */
524 + ld->ld_at_entry = *at;
525 + ld->ld_count = dx_get_count(entries);
527 + frame->at = &ld->ld_at_entry;
528 + myblock = dx_get_block(at);
530 + /* NB: ordering locking */
531 + ext4_htree_spin_unlock_listen(lck, &myblock);
532 + /* other thread can split this DE-block because:
533 + * a) I don't have lock for the DE-block yet
534 + * b) I released spinlock on DX-block
535 + * if it happened I can detect it by listening
536 + * splitting event on this DE-block */
537 + ext4_htree_de_lock(lck, frame->at);
538 + ext4_htree_spin_stop_listen(lck);
540 + if (myblock == EXT4_HTREE_NODE_CHANGED) {
541 + /* someone split this DE-block before
542 + * I locked it, I need to retry and lock
543 + * valid DE-block */
544 + ext4_htree_de_unlock(lck);
552 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
553 if (IS_ERR(frame->bh)) {
554 @@ -965,7 +1266,7 @@ static void dx_release(struct dx_frame *frames, struct inode *dir)
555 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
556 struct dx_frame *frame,
557 struct dx_frame *frames,
559 + __u32 *start_hash, struct htree_lock *lck)
562 struct buffer_head *bh;
563 @@ -980,12 +1281,22 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
564 * this loop, num_frames indicates the number of interior
565 * nodes need to be read.
567 + ext4_htree_de_unlock(lck);
569 - if (++(p->at) < p->entries + dx_get_count(p->entries))
571 + if (num_frames > 0 || ext4_htree_dx_locked(lck)) {
572 + /* num_frames > 0 :
574 + * ext4_htree_dx_locked:
575 + * frame->at is reliable pointer returned by dx_probe,
576 + * otherwise dx_probe already knew no collision */
577 + if (++(p->at) < p->entries + dx_get_count(p->entries))
583 + if (num_frames == 1)
584 + ext4_htree_dx_unlock(lck);
588 @@ -1008,6 +1319,13 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
589 * block so no check is necessary
591 while (num_frames--) {
592 + if (num_frames == 0) {
593 + /* it's not always necessary, we just don't want to
594 + * detect hash collision again */
595 + ext4_htree_dx_need_lock(lck);
596 + ext4_htree_dx_lock(lck, p->at);
599 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
602 @@ -1016,6 +1334,7 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
604 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
606 + ext4_htree_de_lock(lck, p->at);
610 @@ -1177,10 +1496,10 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
612 hinfo.hash = start_hash;
613 hinfo.minor_hash = 0;
614 - frame = dx_probe(NULL, dir, &hinfo, frames);
615 + /* assume it's PR locked */
616 + frame = dx_probe(NULL, dir, &hinfo, frames, NULL);
618 return PTR_ERR(frame);
620 /* Add '.' and '..' from the htree header */
621 if (!start_hash && !start_minor_hash) {
622 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
623 @@ -1220,7 +1539,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
626 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
627 - frame, frames, &hashval);
628 + frame, frames, &hashval, NULL);
629 *next_hash = hashval;
632 @@ -1533,7 +1852,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
633 static struct buffer_head *__ext4_find_entry(struct inode *dir,
634 struct ext4_filename *fname,
635 struct ext4_dir_entry_2 **res_dir,
637 + int *inlined, struct htree_lock *lck)
639 struct super_block *sb;
640 struct buffer_head *bh_use[NAMEI_RA_SIZE];
641 @@ -1575,7 +1894,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
645 - ret = ext4_dx_find_entry(dir, fname, res_dir);
646 + ret = ext4_dx_find_entry(dir, fname, res_dir, lck);
648 * On success, or if the error was file not found,
649 * return. Otherwise, fall back to doing a search the
650 @@ -1585,6 +1904,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
651 goto cleanup_and_exit;
652 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
654 + ext4_htree_safe_relock(lck);
657 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
658 @@ -1675,10 +1995,10 @@ cleanup_and_exit:
662 -static struct buffer_head *ext4_find_entry(struct inode *dir,
663 +struct buffer_head *ext4_find_entry_locked(struct inode *dir,
664 const struct qstr *d_name,
665 struct ext4_dir_entry_2 **res_dir,
667 + int *inlined, struct htree_lock *lck)
670 struct ext4_filename fname;
671 @@ -1690,12 +2010,14 @@ static struct buffer_head *ext4_find_entry(struct inode *dir,
675 - bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
676 + bh = __ext4_find_entry(dir, &fname, res_dir, inlined, lck);
678 ext4_fname_free_filename(&fname);
682 +EXPORT_SYMBOL(ext4_find_entry_locked);
684 static struct buffer_head *ext4_lookup_entry(struct inode *dir,
685 struct dentry *dentry,
686 struct ext4_dir_entry_2 **res_dir)
687 @@ -1711,7 +2033,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
691 - bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
692 + bh = __ext4_find_entry(dir, &fname, res_dir, NULL, NULL);
694 ext4_fname_free_filename(&fname);
696 @@ -1719,7 +2041,8 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
698 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
699 struct ext4_filename *fname,
700 - struct ext4_dir_entry_2 **res_dir)
701 + struct ext4_dir_entry_2 **res_dir,
702 + struct htree_lock *lck)
704 struct super_block * sb = dir->i_sb;
705 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
706 @@ -1730,7 +2053,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
707 #ifdef CONFIG_FS_ENCRYPTION
710 - frame = dx_probe(fname, dir, NULL, frames);
711 + frame = dx_probe(fname, dir, NULL, frames, lck);
713 return (struct buffer_head *) frame;
715 @@ -1752,7 +2075,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
717 /* Check to see if we should continue to search */
718 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
720 + frames, NULL, lck);
722 ext4_warning_inode(dir,
723 "error %d reading directory index block",
724 @@ -1941,8 +2264,9 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
725 * Returns pointer to de in block into which the new entry will be inserted.
727 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
728 - struct buffer_head **bh,struct dx_frame *frame,
729 - struct dx_hash_info *hinfo)
730 + struct buffer_head **bh, struct dx_frame *frames,
731 + struct dx_frame *frame, struct dx_hash_info *hinfo,
732 + struct htree_lock *lck)
734 unsigned blocksize = dir->i_sb->s_blocksize;
735 unsigned count, continued;
736 @@ -2015,8 +2339,14 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
737 hash2, split, count-split));
739 /* Fancy dance to stay within two buffers */
740 - de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
742 + if (hinfo->hash < hash2) {
743 + de2 = dx_move_dirents(dir, data1, data2, map + split,
744 + count - split, blocksize);
746 + /* make sure we will add entry to the same block which
747 + * we have already locked */
748 + de2 = dx_move_dirents(dir, data1, data2, map, split, blocksize);
750 de = dx_pack_dirents(dir, data1, blocksize);
751 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
753 @@ -2034,12 +2364,21 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
754 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
757 - /* Which block gets the new entry? */
758 - if (hinfo->hash >= hash2) {
761 + ext4_htree_spin_lock(lck, frame > frames ? (frame - 1)->at : NULL,
762 + frame->at); /* notify block is being split */
763 + if (hinfo->hash < hash2) {
764 + dx_insert_block(frame, hash2 + continued, newblock);
767 + /* switch block number */
768 + dx_insert_block(frame, hash2 + continued,
769 + dx_get_block(frame->at));
770 + dx_set_block(frame->at, newblock);
773 - dx_insert_block(frame, hash2 + continued, newblock);
774 + ext4_htree_spin_unlock(lck);
775 + ext4_htree_dx_unlock(lck);
777 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
780 @@ -2338,7 +2677,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
784 - de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
785 + de = do_split(handle, dir, &bh2, frames, frame, &fname->hinfo, NULL);
787 retval = PTR_ERR(de);
789 @@ -2443,8 +2782,8 @@ out:
790 * may not sleep between calling this and putting something into
791 * the entry, as someone else might have used it while you slept.
793 -static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
794 - struct inode *inode)
795 +int ext4_add_entry_locked(handle_t *handle, struct dentry *dentry,
796 + struct inode *inode, struct htree_lock *lck)
798 struct inode *dir = d_inode(dentry->d_parent);
799 struct buffer_head *bh = NULL;
800 @@ -2493,9 +2832,10 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
801 return ext4_update_dotdot(handle, dentry, inode);
804 - retval = ext4_dx_add_entry(handle, &fname, dir, inode);
805 + retval = ext4_dx_add_entry(handle, &fname, dir, inode, lck);
806 if (!retval || (retval != ERR_BAD_DX_DIR))
808 + ext4_htree_safe_relock(lck);
809 /* Can we just ignore htree data? */
810 if (ext4_has_metadata_csum(sb)) {
811 EXT4_ERROR_INODE(dir,
812 @@ -2558,12 +2898,14 @@ out:
813 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
816 +EXPORT_SYMBOL(ext4_add_entry_locked);
819 * Returns 0 for success, or a negative error value
821 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
822 - struct inode *dir, struct inode *inode)
823 + struct inode *dir, struct inode *inode,
824 + struct htree_lock *lck)
826 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
827 struct dx_entry *entries, *at;
828 @@ -2575,7 +2917,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
832 - frame = dx_probe(fname, dir, NULL, frames);
833 + frame = dx_probe(fname, dir, NULL, frames, lck);
835 return PTR_ERR(frame);
836 entries = frame->entries;
837 @@ -2610,6 +2952,12 @@ again:
838 struct dx_node *node2;
839 struct buffer_head *bh2;
841 + if (!ext4_htree_safe_locked(lck)) { /* retry with EX lock */
842 + ext4_htree_safe_relock(lck);
847 while (frame > frames) {
848 if (dx_get_count((frame - 1)->entries) <
849 dx_get_limit((frame - 1)->entries)) {
850 @@ -2713,8 +3061,32 @@ again:
854 + } else if (!ext4_htree_dx_locked(lck)) {
855 + struct ext4_dir_lock_data *ld = ext4_htree_lock_data(lck);
857 + /* not well protected, require DX lock */
858 + ext4_htree_dx_need_lock(lck);
859 + at = frame > frames ? (frame - 1)->at : NULL;
861 + /* NB: no risk of deadlock because it's just a try.
863 + * NB: we check ld_count for twice, the first time before
864 + * having DX lock, the second time after holding DX lock.
866 + * NB: We never free blocks for directory so far, which
867 + * means value returned by dx_get_count() should equal to
868 + * ld->ld_count if nobody split any DE-block under @at,
869 + * and ld->ld_at still points to valid dx_entry. */
870 + if ((ld->ld_count != dx_get_count(entries)) ||
871 + !ext4_htree_dx_lock_try(lck, at) ||
872 + (ld->ld_count != dx_get_count(entries))) {
876 + /* OK, I've got DX lock and nothing changed */
877 + frame->at = ld->ld_at;
879 - de = do_split(handle, dir, &bh, frame, &fname->hinfo);
880 + de = do_split(handle, dir, &bh, frames, frame, &fname->hinfo, lck);
884 @@ -2725,6 +3097,8 @@ again:
886 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
888 + ext4_htree_dx_unlock(lck);
889 + ext4_htree_de_unlock(lck);
891 dx_release(frames, dir);
892 /* @restart is true means htree-path has been changed, we need to
893 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
894 index 5751080..a9372ee 100644
895 --- a/fs/ext4/super.c
896 +++ b/fs/ext4/super.c
897 @@ -1291,6 +1291,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
899 inode_set_iversion(&ei->vfs_inode, 1);
900 spin_lock_init(&ei->i_raw_lock);
901 + sema_init(&ei->i_append_sem, 1);
902 INIT_LIST_HEAD(&ei->i_prealloc_list);
903 atomic_set(&ei->i_prealloc_active, 0);
904 spin_lock_init(&ei->i_prealloc_lock);