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 | 467 ++++++++++++++++++++++++++++++++++++++++++-----
29 4 files changed, 505 insertions(+), 42 deletions(-)
31 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
32 index 72206a2..1d15a3a 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 9986f39..cbb58c7 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 @@ -1030,6 +1031,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 @@ -2566,6 +2570,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 @@ -2896,12 +2966,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 54bbe22..9b20bc5 100644
161 --- a/fs/ext4/namei.c
162 +++ b/fs/ext4/namei.c
163 @@ -56,6 +56,7 @@ struct buffer_head *ext4_append(handle_t *handle,
165 struct ext4_map_blocks map;
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 @@ -63,6 +64,10 @@ 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;
182 @@ -74,15 +79,18 @@ struct buffer_head *ext4_append(handle_t *handle,
184 err = ext4_map_blocks(NULL, inode, &map, 0);
186 - return ERR_PTR(err);
189 EXT4_ERROR_INODE(inode, "Logical block already allocated");
190 - return ERR_PTR(-EFSCORRUPTED);
191 + err = -EFSCORRUPTED;
195 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
198 + up(&ei->i_append_sem);
201 inode->i_size += inode->i_sb->s_blocksize;
202 EXT4_I(inode)->i_disksize = inode->i_size;
203 err = ext4_mark_inode_dirty(handle, inode);
204 @@ -93,11 +101,14 @@ struct buffer_head *ext4_append(handle_t *handle,
208 + up(&ei->i_append_sem);
213 ext4_std_error(inode->i_sb, err);
215 + up(&ei->i_append_sem);
219 @@ -296,7 +307,8 @@ static unsigned dx_node_limit(struct inode *dir);
220 static struct dx_frame *dx_probe(struct ext4_filename *fname,
222 struct dx_hash_info *hinfo,
223 - struct dx_frame *frame);
224 + struct dx_frame *frame,
225 + struct htree_lock *lck);
226 static void dx_release(struct dx_frame *frames, struct inode *dir);
227 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
228 struct dx_hash_info *hinfo,
229 @@ -312,12 +324,13 @@ static void dx_insert_block(struct dx_frame *frame,
230 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
231 struct dx_frame *frame,
232 struct dx_frame *frames,
233 - __u32 *start_hash);
234 + __u32 *start_hash, struct htree_lock *lck);
235 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
236 struct ext4_filename *fname,
237 - struct ext4_dir_entry_2 **res_dir);
238 + struct ext4_dir_entry_2 **res_dir, struct htree_lock *lck);
239 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
240 - struct inode *dir, struct inode *inode);
241 + struct inode *dir, struct inode *inode,
242 + struct htree_lock *lck);
244 /* checksumming functions */
245 void ext4_initialize_dirent_tail(struct buffer_head *bh,
246 @@ -802,6 +815,227 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
248 #endif /* DX_DEBUG */
250 +/* private data for htree_lock */
251 +struct ext4_dir_lock_data {
252 + unsigned ld_flags; /* bits-map for lock types */
253 + unsigned ld_count; /* # entries of the last DX block */
254 + struct dx_entry ld_at_entry; /* copy of leaf dx_entry */
255 + struct dx_entry *ld_at; /* position of leaf dx_entry */
258 +#define ext4_htree_lock_data(l) ((struct ext4_dir_lock_data *)(l)->lk_private)
259 +#define ext4_find_entry(dir, name, dirent, inline) \
260 + ext4_find_entry_locked(dir, name, dirent, inline, NULL)
261 +#define ext4_add_entry(handle, dentry, inode) \
262 + ext4_add_entry_locked(handle, dentry, inode, NULL)
264 +/* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
265 +#define EXT4_HTREE_NODE_CHANGED (0xcafeULL << 32)
267 +static void ext4_htree_event_cb(void *target, void *event)
269 + u64 *block = (u64 *)target;
271 + if (*block == dx_get_block((struct dx_entry *)event))
272 + *block = EXT4_HTREE_NODE_CHANGED;
275 +struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits)
277 + struct htree_lock_head *lhead;
279 + lhead = htree_lock_head_alloc(EXT4_LK_MAX, hbits, 0);
280 + if (lhead != NULL) {
281 + htree_lock_event_attach(lhead, EXT4_LK_SPIN, HTREE_EVENT_WR,
282 + ext4_htree_event_cb);
286 +EXPORT_SYMBOL(ext4_htree_lock_head_alloc);
288 +struct htree_lock *ext4_htree_lock_alloc(void)
290 + return htree_lock_alloc(EXT4_LK_MAX,
291 + sizeof(struct ext4_dir_lock_data));
293 +EXPORT_SYMBOL(ext4_htree_lock_alloc);
295 +static htree_lock_mode_t ext4_htree_mode(unsigned flags)
298 + default: /* 0 or unknown flags require EX lock */
299 + return HTREE_LOCK_EX;
300 + case EXT4_HLOCK_READDIR:
301 + return HTREE_LOCK_PR;
302 + case EXT4_HLOCK_LOOKUP:
303 + return HTREE_LOCK_CR;
304 + case EXT4_HLOCK_DEL:
305 + case EXT4_HLOCK_ADD:
306 + return HTREE_LOCK_CW;
310 +/* return PR for read-only operations, otherwise return EX */
311 +static inline htree_lock_mode_t ext4_htree_safe_mode(unsigned flags)
313 + int writer = (flags & EXT4_LB_DE) == EXT4_LB_DE;
315 + /* 0 requires EX lock */
316 + return (flags == 0 || writer) ? HTREE_LOCK_EX : HTREE_LOCK_PR;
319 +static int ext4_htree_safe_locked(struct htree_lock *lck)
323 + if (lck == NULL || lck->lk_mode == HTREE_LOCK_EX)
326 + writer = (ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_DE) ==
328 + if (writer) /* all readers & writers are excluded? */
329 + return lck->lk_mode == HTREE_LOCK_EX;
331 + /* all writers are excluded? */
332 + return lck->lk_mode == HTREE_LOCK_PR ||
333 + lck->lk_mode == HTREE_LOCK_PW ||
334 + lck->lk_mode == HTREE_LOCK_EX;
337 +/* relock htree_lock with EX mode if it's change operation, otherwise
338 + * relock it with PR mode. It's noop if PDO is disabled. */
339 +static void ext4_htree_safe_relock(struct htree_lock *lck)
341 + if (!ext4_htree_safe_locked(lck)) {
342 + unsigned flags = ext4_htree_lock_data(lck)->ld_flags;
344 + htree_change_lock(lck, ext4_htree_safe_mode(flags));
348 +void ext4_htree_lock(struct htree_lock *lck, struct htree_lock_head *lhead,
349 + struct inode *dir, unsigned flags)
351 + htree_lock_mode_t mode = is_dx(dir) ? ext4_htree_mode(flags) :
352 + ext4_htree_safe_mode(flags);
354 + ext4_htree_lock_data(lck)->ld_flags = flags;
355 + htree_lock(lck, lhead, mode);
357 + ext4_htree_safe_relock(lck); /* make sure it's safe locked */
359 +EXPORT_SYMBOL(ext4_htree_lock);
361 +static int ext4_htree_node_lock(struct htree_lock *lck, struct dx_entry *at,
362 + unsigned lmask, int wait, void *ev)
364 + u32 key = (at == NULL) ? 0 : dx_get_block(at);
367 + /* NOOP if htree is well protected or caller doesn't require the lock */
368 + if (ext4_htree_safe_locked(lck) ||
369 + !(ext4_htree_lock_data(lck)->ld_flags & lmask))
372 + mode = (ext4_htree_lock_data(lck)->ld_flags & lmask) == lmask ?
373 + HTREE_LOCK_PW : HTREE_LOCK_PR;
375 + if (htree_node_lock_try(lck, mode, key, ffz(~lmask), wait, ev))
377 + if (!(lmask & EXT4_LB_SPIN)) /* not a spinlock */
379 + cpu_relax(); /* spin until granted */
383 +static int ext4_htree_node_locked(struct htree_lock *lck, unsigned lmask)
385 + return ext4_htree_safe_locked(lck) ||
386 + htree_node_is_granted(lck, ffz(~lmask));
389 +static void ext4_htree_node_unlock(struct htree_lock *lck,
390 + unsigned lmask, void *buf)
392 + /* NB: it's safe to call mutiple times or even it's not locked */
393 + if (!ext4_htree_safe_locked(lck) &&
394 + htree_node_is_granted(lck, ffz(~lmask)))
395 + htree_node_unlock(lck, ffz(~lmask), buf);
398 +#define ext4_htree_dx_lock(lck, key) \
399 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 1, NULL)
400 +#define ext4_htree_dx_lock_try(lck, key) \
401 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 0, NULL)
402 +#define ext4_htree_dx_unlock(lck) \
403 + ext4_htree_node_unlock(lck, EXT4_LB_DX, NULL)
404 +#define ext4_htree_dx_locked(lck) \
405 + ext4_htree_node_locked(lck, EXT4_LB_DX)
407 +static void ext4_htree_dx_need_lock(struct htree_lock *lck)
409 + struct ext4_dir_lock_data *ld;
411 + if (ext4_htree_safe_locked(lck))
414 + ld = ext4_htree_lock_data(lck);
415 + switch (ld->ld_flags) {
418 + case EXT4_HLOCK_LOOKUP:
419 + ld->ld_flags = EXT4_HLOCK_LOOKUP_SAFE;
421 + case EXT4_HLOCK_DEL:
422 + ld->ld_flags = EXT4_HLOCK_DEL_SAFE;
424 + case EXT4_HLOCK_ADD:
425 + ld->ld_flags = EXT4_HLOCK_SPLIT;
430 +#define ext4_htree_de_lock(lck, key) \
431 + ext4_htree_node_lock(lck, key, EXT4_LB_DE, 1, NULL)
432 +#define ext4_htree_de_unlock(lck) \
433 + ext4_htree_node_unlock(lck, EXT4_LB_DE, NULL)
435 +#define ext4_htree_spin_lock(lck, key, event) \
436 + ext4_htree_node_lock(lck, key, EXT4_LB_SPIN, 0, event)
437 +#define ext4_htree_spin_unlock(lck) \
438 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, NULL)
439 +#define ext4_htree_spin_unlock_listen(lck, p) \
440 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, p)
442 +static void ext4_htree_spin_stop_listen(struct htree_lock *lck)
444 + if (!ext4_htree_safe_locked(lck) &&
445 + htree_node_is_listening(lck, ffz(~EXT4_LB_SPIN)))
446 + htree_node_stop_listen(lck, ffz(~EXT4_LB_SPIN));
450 + DX_HASH_COL_IGNORE, /* ignore collision while probing frames */
451 + DX_HASH_COL_YES, /* there is collision and it does matter */
452 + DX_HASH_COL_NO, /* there is no collision */
455 +static int dx_probe_hash_collision(struct htree_lock *lck,
456 + struct dx_entry *entries,
457 + struct dx_entry *at, u32 hash)
459 + if (!(lck && ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_EXACT)) {
460 + return DX_HASH_COL_IGNORE; /* don't care about collision */
462 + } else if (at == entries + dx_get_count(entries) - 1) {
463 + return DX_HASH_COL_IGNORE; /* not in any leaf of this DX */
465 + } else { /* hash collision? */
466 + return ((dx_get_hash(at + 1) & ~1) == hash) ?
467 + DX_HASH_COL_YES : DX_HASH_COL_NO;
472 * Probe for a directory leaf block to search.
474 @@ -813,10 +1047,11 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
476 static struct dx_frame *
477 dx_probe(struct ext4_filename *fname, struct inode *dir,
478 - struct dx_hash_info *hinfo, struct dx_frame *frame_in)
479 + struct dx_hash_info *hinfo, struct dx_frame *frame_in,
480 + struct htree_lock *lck)
482 unsigned count, indirect, level, i;
483 - struct dx_entry *at, *entries, *p, *q, *m;
484 + struct dx_entry *at, *entries, *p, *q, *m, *dx = NULL;
485 struct dx_root_info *info;
486 struct dx_frame *frame = frame_in;
487 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
488 @@ -900,8 +1135,16 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
492 + if (indirect == level) { /* the last index level */
493 + /* NB: ext4_htree_dx_lock() could be noop if
494 + * DX-lock flag is not set for current operation
496 + ext4_htree_dx_lock(lck, dx);
497 + ext4_htree_spin_lock(lck, dx, NULL);
499 count = dx_get_count(entries);
500 if (!count || count > dx_get_limit(entries)) {
501 + ext4_htree_spin_unlock(lck); /* release spin */
502 ext4_warning_inode(dir,
503 "dx entry: count %u beyond limit %u",
504 count, dx_get_limit(entries));
505 @@ -928,6 +1171,74 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
506 frame->entries = entries;
509 + if (indirect == level) { /* the last index level */
510 + struct ext4_dir_lock_data *ld;
513 + /* By default we only lock DE-block, however, we will
514 + * also lock the last level DX-block if:
515 + * a) there is hash collision
516 + * we will set DX-lock flag (a few lines below)
517 + * and redo to lock DX-block
518 + * see detail in dx_probe_hash_collision()
519 + * b) it's a retry from splitting
520 + * we need to lock the last level DX-block so nobody
521 + * else can split any leaf blocks under the same
522 + * DX-block, see detail in ext4_dx_add_entry()
524 + if (ext4_htree_dx_locked(lck)) {
525 + /* DX-block is locked, just lock DE-block
528 + ext4_htree_spin_unlock(lck);
529 + if (!ext4_htree_safe_locked(lck))
530 + ext4_htree_de_lock(lck, frame->at);
533 + /* it's pdirop and no DX lock */
534 + if (dx_probe_hash_collision(lck, entries, at, hash) ==
536 + /* found hash collision, set DX-lock flag
537 + * and retry to abtain DX-lock
539 + ext4_htree_spin_unlock(lck);
540 + ext4_htree_dx_need_lock(lck);
543 + ld = ext4_htree_lock_data(lck);
544 + /* because I don't lock DX, so @at can't be trusted
545 + * after I release spinlock so I have to save it
548 + ld->ld_at_entry = *at;
549 + ld->ld_count = dx_get_count(entries);
551 + frame->at = &ld->ld_at_entry;
552 + myblock = dx_get_block(at);
554 + /* NB: ordering locking */
555 + ext4_htree_spin_unlock_listen(lck, &myblock);
556 + /* other thread can split this DE-block because:
557 + * a) I don't have lock for the DE-block yet
558 + * b) I released spinlock on DX-block
559 + * if it happened I can detect it by listening
560 + * splitting event on this DE-block
562 + ext4_htree_de_lock(lck, frame->at);
563 + ext4_htree_spin_stop_listen(lck);
565 + if (myblock == EXT4_HTREE_NODE_CHANGED) {
566 + /* someone split this DE-block before
567 + * I locked it, I need to retry and lock
570 + ext4_htree_de_unlock(lck);
577 block = dx_get_block(at);
578 for (i = 0; i <= level; i++) {
579 if (blocks[i] == block) {
580 @@ -937,8 +1248,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
584 - if (++level > indirect)
587 blocks[level] = block;
589 frame->bh = ext4_read_dirblock(dir, block, INDEX);
590 @@ -1009,7 +1319,7 @@ static void dx_release(struct dx_frame *frames, struct inode *dir)
591 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
592 struct dx_frame *frame,
593 struct dx_frame *frames,
595 + __u32 *start_hash, struct htree_lock *lck)
598 struct buffer_head *bh;
599 @@ -1024,12 +1334,22 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
600 * this loop, num_frames indicates the number of interior
601 * nodes need to be read.
603 + ext4_htree_de_unlock(lck);
605 - if (++(p->at) < p->entries + dx_get_count(p->entries))
607 + if (num_frames > 0 || ext4_htree_dx_locked(lck)) {
608 + /* num_frames > 0 :
610 + * ext4_htree_dx_locked:
611 + * frame->at is reliable pointer returned by dx_probe,
612 + * otherwise dx_probe already knew no collision */
613 + if (++(p->at) < p->entries + dx_get_count(p->entries))
619 + if (num_frames == 1)
620 + ext4_htree_dx_unlock(lck);
624 @@ -1052,6 +1372,13 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
625 * block so no check is necessary
627 while (num_frames--) {
628 + if (num_frames == 0) {
629 + /* it's not always necessary, we just don't want to
630 + * detect hash collision again */
631 + ext4_htree_dx_need_lock(lck);
632 + ext4_htree_dx_lock(lck, p->at);
635 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
638 @@ -1060,6 +1387,7 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
640 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
642 + ext4_htree_de_lock(lck, p->at);
646 @@ -1221,10 +1549,10 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
648 hinfo.hash = start_hash;
649 hinfo.minor_hash = 0;
650 - frame = dx_probe(NULL, dir, &hinfo, frames);
651 + /* assume it's PR locked */
652 + frame = dx_probe(NULL, dir, &hinfo, frames, NULL);
654 return PTR_ERR(frame);
656 /* Add '.' and '..' from the htree header */
657 if (!start_hash && !start_minor_hash) {
658 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
659 @@ -1264,7 +1592,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
662 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
663 - frame, frames, &hashval);
664 + frame, frames, &hashval, NULL);
665 *next_hash = hashval;
668 @@ -1584,7 +1912,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
669 static struct buffer_head *__ext4_find_entry(struct inode *dir,
670 struct ext4_filename *fname,
671 struct ext4_dir_entry_2 **res_dir,
673 + int *inlined, struct htree_lock *lck)
675 struct super_block *sb;
676 struct buffer_head *bh_use[NAMEI_RA_SIZE];
677 @@ -1626,7 +1954,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
681 - ret = ext4_dx_find_entry(dir, fname, res_dir);
682 + ret = ext4_dx_find_entry(dir, fname, res_dir, lck);
684 * On success, or if the error was file not found,
685 * return. Otherwise, fall back to doing a search the
686 @@ -1636,6 +1964,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
687 goto cleanup_and_exit;
688 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
690 + ext4_htree_safe_relock(lck);
693 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
694 @@ -1726,10 +2055,10 @@ cleanup_and_exit:
698 -static struct buffer_head *ext4_find_entry(struct inode *dir,
699 +struct buffer_head *ext4_find_entry_locked(struct inode *dir,
700 const struct qstr *d_name,
701 struct ext4_dir_entry_2 **res_dir,
703 + int *inlined, struct htree_lock *lck)
706 struct ext4_filename fname;
707 @@ -1741,12 +2070,14 @@ static struct buffer_head *ext4_find_entry(struct inode *dir,
711 - bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
712 + bh = __ext4_find_entry(dir, &fname, res_dir, inlined, lck);
714 ext4_fname_free_filename(&fname);
718 +EXPORT_SYMBOL(ext4_find_entry_locked);
720 static struct buffer_head *ext4_lookup_entry(struct inode *dir,
721 struct dentry *dentry,
722 struct ext4_dir_entry_2 **res_dir)
723 @@ -1762,7 +2093,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
727 - bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
728 + bh = __ext4_find_entry(dir, &fname, res_dir, NULL, NULL);
730 ext4_fname_free_filename(&fname);
732 @@ -1770,7 +2101,8 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
734 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
735 struct ext4_filename *fname,
736 - struct ext4_dir_entry_2 **res_dir)
737 + struct ext4_dir_entry_2 **res_dir,
738 + struct htree_lock *lck)
740 struct super_block * sb = dir->i_sb;
741 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
742 @@ -1781,7 +2113,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
743 #ifdef CONFIG_FS_ENCRYPTION
746 - frame = dx_probe(fname, dir, NULL, frames);
747 + frame = dx_probe(fname, dir, NULL, frames, lck);
749 return (struct buffer_head *) frame;
751 @@ -1803,7 +2135,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
753 /* Check to see if we should continue to search */
754 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
756 + frames, NULL, lck);
758 ext4_warning_inode(dir,
759 "error %d reading directory index block",
760 @@ -1992,8 +2324,9 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
761 * Returns pointer to de in block into which the new entry will be inserted.
763 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
764 - struct buffer_head **bh,struct dx_frame *frame,
765 - struct dx_hash_info *hinfo)
766 + struct buffer_head **bh, struct dx_frame *frames,
767 + struct dx_frame *frame, struct dx_hash_info *hinfo,
768 + struct htree_lock *lck)
770 unsigned blocksize = dir->i_sb->s_blocksize;
772 @@ -2070,8 +2403,14 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
773 hash2, split, count-split));
775 /* Fancy dance to stay within two buffers */
776 - de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
778 + if (hinfo->hash < hash2) {
779 + de2 = dx_move_dirents(dir, data1, data2, map + split,
780 + count - split, blocksize);
782 + /* make sure we will add entry to the same block which
783 + * we have already locked */
784 + de2 = dx_move_dirents(dir, data1, data2, map, split, blocksize);
786 de = dx_pack_dirents(dir, data1, blocksize);
787 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
789 @@ -2089,12 +2428,21 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
790 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
793 - /* Which block gets the new entry? */
794 - if (hinfo->hash >= hash2) {
797 + ext4_htree_spin_lock(lck, frame > frames ? (frame - 1)->at : NULL,
798 + frame->at); /* notify block is being split */
799 + if (hinfo->hash < hash2) {
800 + dx_insert_block(frame, hash2 + continued, newblock);
803 + /* switch block number */
804 + dx_insert_block(frame, hash2 + continued,
805 + dx_get_block(frame->at));
806 + dx_set_block(frame->at, newblock);
809 - dx_insert_block(frame, hash2 + continued, newblock);
810 + ext4_htree_spin_unlock(lck);
811 + ext4_htree_dx_unlock(lck);
813 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
816 @@ -2402,7 +2750,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
820 - de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
821 + de = do_split(handle, dir, &bh2, frames, frame, &fname->hinfo, NULL);
823 retval = PTR_ERR(de);
825 @@ -2512,8 +2860,8 @@ out:
826 * may not sleep between calling this and putting something into
827 * the entry, as someone else might have used it while you slept.
829 -static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
830 - struct inode *inode)
831 +int ext4_add_entry_locked(handle_t *handle, struct dentry *dentry,
832 + struct inode *inode, struct htree_lock *lck)
834 struct inode *dir = d_inode(dentry->d_parent);
835 struct buffer_head *bh = NULL;
836 @@ -2562,9 +2910,10 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
837 return ext4_update_dotdot(handle, dentry, inode);
840 - retval = ext4_dx_add_entry(handle, &fname, dir, inode);
841 + retval = ext4_dx_add_entry(handle, &fname, dir, inode, lck);
842 if (!retval || (retval != ERR_BAD_DX_DIR))
844 + ext4_htree_safe_relock(lck);
845 /* Can we just ignore htree data? */
846 if (ext4_has_metadata_csum(sb)) {
847 EXT4_ERROR_INODE(dir,
848 @@ -2627,12 +2976,14 @@ out:
849 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
852 +EXPORT_SYMBOL(ext4_add_entry_locked);
855 * Returns 0 for success, or a negative error value
857 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
858 - struct inode *dir, struct inode *inode)
859 + struct inode *dir, struct inode *inode,
860 + struct htree_lock *lck)
862 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
863 struct dx_entry *entries, *at;
864 @@ -2644,7 +2995,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
868 - frame = dx_probe(fname, dir, NULL, frames);
869 + frame = dx_probe(fname, dir, NULL, frames, lck);
871 return PTR_ERR(frame);
872 entries = frame->entries;
873 @@ -2679,6 +3030,12 @@ again:
874 struct dx_node *node2;
875 struct buffer_head *bh2;
877 + if (!ext4_htree_safe_locked(lck)) { /* retry with EX lock */
878 + ext4_htree_safe_relock(lck);
883 while (frame > frames) {
884 if (dx_get_count((frame - 1)->entries) <
885 dx_get_limit((frame - 1)->entries)) {
886 @@ -2782,8 +3139,32 @@ again:
890 + } else if (!ext4_htree_dx_locked(lck)) {
891 + struct ext4_dir_lock_data *ld = ext4_htree_lock_data(lck);
893 + /* not well protected, require DX lock */
894 + ext4_htree_dx_need_lock(lck);
895 + at = frame > frames ? (frame - 1)->at : NULL;
897 + /* NB: no risk of deadlock because it's just a try.
899 + * NB: we check ld_count for twice, the first time before
900 + * having DX lock, the second time after holding DX lock.
902 + * NB: We never free blocks for directory so far, which
903 + * means value returned by dx_get_count() should equal to
904 + * ld->ld_count if nobody split any DE-block under @at,
905 + * and ld->ld_at still points to valid dx_entry. */
906 + if ((ld->ld_count != dx_get_count(entries)) ||
907 + !ext4_htree_dx_lock_try(lck, at) ||
908 + (ld->ld_count != dx_get_count(entries))) {
912 + /* OK, I've got DX lock and nothing changed */
913 + frame->at = ld->ld_at;
915 - de = do_split(handle, dir, &bh, frame, &fname->hinfo);
916 + de = do_split(handle, dir, &bh, frames, frame, &fname->hinfo, lck);
920 @@ -2794,6 +3175,8 @@ again:
922 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
924 + ext4_htree_dx_unlock(lck);
925 + ext4_htree_de_unlock(lck);
927 dx_release(frames, dir);
928 /* @restart is true means htree-path has been changed, we need to
929 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
930 index 5a4b03a..8083662 100644
931 --- a/fs/ext4/super.c
932 +++ b/fs/ext4/super.c
933 @@ -1324,6 +1324,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
935 inode_set_iversion(&ei->vfs_inode, 1);
936 spin_lock_init(&ei->i_raw_lock);
937 + sema_init(&ei->i_append_sem, 1);
938 INIT_LIST_HEAD(&ei->i_prealloc_list);
939 atomic_set(&ei->i_prealloc_active, 0);
940 spin_lock_init(&ei->i_prealloc_lock);