1 Single directory performance is a critical for HPC workloads. In a
2 typical use case an application creates a separate output file for
3 each node and task in a job. As nodes and tasks increase, hundreds
4 of thousands of files may be created in a single directory within
5 a short window of time.
6 Today, both filename lookup and file system modifying operations
7 (such as create and unlink) are protected with a single lock for
8 an entire ldiskfs directory. PDO project will remove this
9 bottleneck by introducing a parallel locking mechanism for entire
10 ldiskfs directories. This work will enable multiple application
11 threads to simultaneously lookup, create and unlink in parallel.
14 - pdirops support for ldiskfs
15 - integrate with osd-ldiskfs
17 Index: linux-3.10.0-229.1.2.fc21.x86_64/include/linux/htree_lock.h
18 ===================================================================
20 +++ linux-3.10.0-229.1.2.fc21.x86_64/include/linux/htree_lock.h
23 + * include/linux/htree_lock.h
25 + * Copyright (c) 2011, 2012, Intel Corporation.
27 + * Author: Liang Zhen <liang@whamcloud.com>
33 + * htree_lock is an advanced lock, it can support five lock modes (concept is
34 + * taken from DLM) and it's a sleeping lock.
36 + * most common use case is:
37 + * - create a htree_lock_head for data
38 + * - each thread (contender) creates it's own htree_lock
39 + * - contender needs to call htree_lock(lock_node, mode) to protect data and
40 + * call htree_unlock to release lock
42 + * Also, there is advanced use-case which is more complex, user can have
43 + * PW/PR lock on particular key, it's mostly used while user holding shared
44 + * lock on the htree (CW, CR)
46 + * htree_lock(lock_node, HTREE_LOCK_CR); lock the htree with CR
47 + * htree_node_lock(lock_node, HTREE_LOCK_PR, key...); lock @key with PR
49 + * htree_node_unlock(lock_node);; unlock the key
51 + * Another tip is, we can have N-levels of this kind of keys, all we need to
52 + * do is specifying N-levels while creating htree_lock_head, then we can
53 + * lock/unlock a specific level by:
54 + * htree_node_lock(lock_node, mode1, key1, level1...);
56 + * htree_node_lock(lock_node, mode1, key2, level2...);
58 + * htree_node_unlock(lock_node, level2);
59 + * htree_node_unlock(lock_node, level1);
61 + * NB: for multi-level, should be careful about locking order to avoid deadlock
64 +#ifndef _LINUX_HTREE_LOCK_H
65 +#define _LINUX_HTREE_LOCK_H
67 +#include <linux/list.h>
68 +#include <linux/spinlock.h>
69 +#include <linux/sched.h>
73 + * more details can be found here:
74 + * http://en.wikipedia.org/wiki/Distributed_lock_manager
77 + HTREE_LOCK_EX = 0, /* exclusive lock: incompatible with all others */
78 + HTREE_LOCK_PW, /* protected write: allows only CR users */
79 + HTREE_LOCK_PR, /* protected read: allow PR, CR users */
80 + HTREE_LOCK_CW, /* concurrent write: allow CR, CW users */
81 + HTREE_LOCK_CR, /* concurrent read: allow all but EX users */
82 + HTREE_LOCK_MAX, /* number of lock modes */
85 +#define HTREE_LOCK_NL HTREE_LOCK_MAX
86 +#define HTREE_LOCK_INVAL 0xdead10c
89 + HTREE_HBITS_MIN = 2,
90 + HTREE_HBITS_DEF = 14,
91 + HTREE_HBITS_MAX = 32,
95 + HTREE_EVENT_DISABLE = (0),
96 + HTREE_EVENT_RD = (1 << HTREE_LOCK_PR),
97 + HTREE_EVENT_WR = (1 << HTREE_LOCK_PW),
98 + HTREE_EVENT_RDWR = (HTREE_EVENT_RD | HTREE_EVENT_WR),
103 +typedef void (*htree_event_cb_t)(void *target, void *event);
105 +struct htree_lock_child {
106 + struct list_head lc_list; /* granted list */
107 + htree_event_cb_t lc_callback; /* event callback */
108 + unsigned lc_events; /* event types */
111 +struct htree_lock_head {
112 + unsigned long lh_lock; /* bits lock */
113 + /* blocked lock list (htree_lock) */
114 + struct list_head lh_blocked_list;
117 + /* hash bits for key and limit number of locks */
119 + /* counters for blocked locks */
120 + u16 lh_nblocked[HTREE_LOCK_MAX];
121 + /* counters for granted locks */
122 + u16 lh_ngranted[HTREE_LOCK_MAX];
125 + /* array of children locks */
126 + struct htree_lock_child lh_children[0];
129 +/* htree_lock_node_t is child-lock for a specific key (ln_value) */
130 +struct htree_lock_node {
131 + htree_lock_mode_t ln_mode;
132 + /* major hash key */
134 + /* minor hash key */
136 + struct list_head ln_major_list;
137 + struct list_head ln_minor_list;
138 + /* alive list, all locks (granted, blocked, listening) are on it */
139 + struct list_head ln_alive_list;
141 + struct list_head ln_blocked_list;
143 + struct list_head ln_granted_list;
144 + void *ln_ev_target;
148 + struct task_struct *lk_task;
149 + struct htree_lock_head *lk_head;
152 + htree_lock_mode_t lk_mode;
153 + struct list_head lk_blocked_list;
154 + struct htree_lock_node lk_nodes[0];
157 +/* create a lock head, which stands for a resource */
158 +struct htree_lock_head *htree_lock_head_alloc(unsigned depth,
159 + unsigned hbits, unsigned priv);
160 +/* free a lock head */
161 +void htree_lock_head_free(struct htree_lock_head *lhead);
162 +/* register event callback for child lock at level @depth */
163 +void htree_lock_event_attach(struct htree_lock_head *lhead, unsigned depth,
164 + unsigned events, htree_event_cb_t callback);
165 +/* create a lock handle, which stands for a thread */
166 +struct htree_lock *htree_lock_alloc(unsigned depth, unsigned pbytes);
167 +/* free a lock handle */
168 +void htree_lock_free(struct htree_lock *lck);
169 +/* lock htree, when @wait is true, 0 is returned if the lock can't
170 + * be granted immediately */
171 +int htree_lock_try(struct htree_lock *lck, struct htree_lock_head *lhead,
172 + htree_lock_mode_t mode, int wait);
174 +void htree_unlock(struct htree_lock *lck);
175 +/* unlock and relock htree with @new_mode */
176 +int htree_change_lock_try(struct htree_lock *lck,
177 + htree_lock_mode_t new_mode, int wait);
178 +void htree_change_mode(struct htree_lock *lck, htree_lock_mode_t mode);
179 +/* require child lock (key) of htree at level @dep, @event will be sent to all
180 + * listeners on this @key while lock being granted */
181 +int htree_node_lock_try(struct htree_lock *lck, htree_lock_mode_t mode,
182 + u32 key, unsigned dep, int wait, void *event);
183 +/* release child lock at level @dep, this lock will listen on it's key
184 + * if @event isn't NULL, event_cb will be called against @lck while granting
185 + * any other lock at level @dep with the same key */
186 +void htree_node_unlock(struct htree_lock *lck, unsigned dep, void *event);
187 +/* stop listening on child lock at level @dep */
188 +void htree_node_stop_listen(struct htree_lock *lck, unsigned dep);
190 +void htree_lock_stat_print(int depth);
191 +void htree_lock_stat_reset(void);
193 +#define htree_lock(lck, lh, mode) htree_lock_try(lck, lh, mode, 1)
194 +#define htree_change_lock(lck, mode) htree_change_lock_try(lck, mode, 1)
196 +#define htree_lock_mode(lck) ((lck)->lk_mode)
198 +#define htree_node_lock(lck, mode, key, dep) \
199 + htree_node_lock_try(lck, mode, key, dep, 1, NULL)
200 +/* this is only safe in thread context of lock owner */
201 +#define htree_node_is_granted(lck, dep) \
202 + ((lck)->lk_nodes[dep].ln_mode != HTREE_LOCK_INVAL && \
203 + (lck)->lk_nodes[dep].ln_mode != HTREE_LOCK_NL)
204 +/* this is only safe in thread context of lock owner */
205 +#define htree_node_is_listening(lck, dep) \
206 + ((lck)->lk_nodes[dep].ln_mode == HTREE_LOCK_NL)
209 Index: linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/htree_lock.c
210 ===================================================================
212 +++ linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/htree_lock.c
215 + * fs/ext4/htree_lock.c
217 + * Copyright (c) 2011, 2012, Intel Corporation.
219 + * Author: Liang Zhen <liang@whamcloud.com>
221 +#include <linux/jbd2.h>
222 +#include <linux/hash.h>
223 +#include <linux/module.h>
224 +#include <linux/htree_lock.h>
227 + HTREE_LOCK_BIT_EX = (1 << HTREE_LOCK_EX),
228 + HTREE_LOCK_BIT_PW = (1 << HTREE_LOCK_PW),
229 + HTREE_LOCK_BIT_PR = (1 << HTREE_LOCK_PR),
230 + HTREE_LOCK_BIT_CW = (1 << HTREE_LOCK_CW),
231 + HTREE_LOCK_BIT_CR = (1 << HTREE_LOCK_CR),
235 + HTREE_LOCK_COMPAT_EX = 0,
236 + HTREE_LOCK_COMPAT_PW = HTREE_LOCK_COMPAT_EX | HTREE_LOCK_BIT_CR,
237 + HTREE_LOCK_COMPAT_PR = HTREE_LOCK_COMPAT_PW | HTREE_LOCK_BIT_PR,
238 + HTREE_LOCK_COMPAT_CW = HTREE_LOCK_COMPAT_PW | HTREE_LOCK_BIT_CW,
239 + HTREE_LOCK_COMPAT_CR = HTREE_LOCK_COMPAT_CW | HTREE_LOCK_BIT_PR |
243 +static int htree_lock_compat[] = {
244 + [HTREE_LOCK_EX] HTREE_LOCK_COMPAT_EX,
245 + [HTREE_LOCK_PW] HTREE_LOCK_COMPAT_PW,
246 + [HTREE_LOCK_PR] HTREE_LOCK_COMPAT_PR,
247 + [HTREE_LOCK_CW] HTREE_LOCK_COMPAT_CW,
248 + [HTREE_LOCK_CR] HTREE_LOCK_COMPAT_CR,
251 +/* max allowed htree-lock depth.
252 + * We only need depth=3 for ext4 although user can have higher value. */
253 +#define HTREE_LOCK_DEP_MAX 16
255 +#ifdef HTREE_LOCK_DEBUG
257 +static char *hl_name[] = {
258 + [HTREE_LOCK_EX] "EX",
259 + [HTREE_LOCK_PW] "PW",
260 + [HTREE_LOCK_PR] "PR",
261 + [HTREE_LOCK_CW] "CW",
262 + [HTREE_LOCK_CR] "CR",
266 +struct htree_lock_node_stats {
267 + unsigned long long blocked[HTREE_LOCK_MAX];
268 + unsigned long long granted[HTREE_LOCK_MAX];
269 + unsigned long long retried[HTREE_LOCK_MAX];
270 + unsigned long long events;
273 +struct htree_lock_stats {
274 + struct htree_lock_node_stats nodes[HTREE_LOCK_DEP_MAX];
275 + unsigned long long granted[HTREE_LOCK_MAX];
276 + unsigned long long blocked[HTREE_LOCK_MAX];
279 +static struct htree_lock_stats hl_stats;
281 +void htree_lock_stat_reset(void)
283 + memset(&hl_stats, 0, sizeof(hl_stats));
286 +void htree_lock_stat_print(int depth)
291 + printk(KERN_DEBUG "HTREE LOCK STATS:\n");
292 + for (i = 0; i < HTREE_LOCK_MAX; i++) {
293 + printk(KERN_DEBUG "[%s]: G [%10llu], B [%10llu]\n",
294 + hl_name[i], hl_stats.granted[i], hl_stats.blocked[i]);
296 + for (i = 0; i < depth; i++) {
297 + printk(KERN_DEBUG "HTREE CHILD [%d] STATS:\n", i);
298 + for (j = 0; j < HTREE_LOCK_MAX; j++) {
300 + "[%s]: G [%10llu], B [%10llu], R [%10llu]\n",
301 + hl_name[j], hl_stats.nodes[i].granted[j],
302 + hl_stats.nodes[i].blocked[j],
303 + hl_stats.nodes[i].retried[j]);
308 +#define lk_grant_inc(m) do { hl_stats.granted[m]++; } while (0)
309 +#define lk_block_inc(m) do { hl_stats.blocked[m]++; } while (0)
310 +#define ln_grant_inc(d, m) do { hl_stats.nodes[d].granted[m]++; } while (0)
311 +#define ln_block_inc(d, m) do { hl_stats.nodes[d].blocked[m]++; } while (0)
312 +#define ln_retry_inc(d, m) do { hl_stats.nodes[d].retried[m]++; } while (0)
313 +#define ln_event_inc(d) do { hl_stats.nodes[d].events++; } while (0)
317 +void htree_lock_stat_reset(void) {}
318 +void htree_lock_stat_print(int depth) {}
320 +#define lk_grant_inc(m) do {} while (0)
321 +#define lk_block_inc(m) do {} while (0)
322 +#define ln_grant_inc(d, m) do {} while (0)
323 +#define ln_block_inc(d, m) do {} while (0)
324 +#define ln_retry_inc(d, m) do {} while (0)
325 +#define ln_event_inc(d) do {} while (0)
329 +EXPORT_SYMBOL(htree_lock_stat_reset);
330 +EXPORT_SYMBOL(htree_lock_stat_print);
332 +#define HTREE_DEP_ROOT (-1)
334 +#define htree_spin_lock(lhead, dep) \
335 + bit_spin_lock((dep) + 1, &(lhead)->lh_lock)
336 +#define htree_spin_unlock(lhead, dep) \
337 + bit_spin_unlock((dep) + 1, &(lhead)->lh_lock)
339 +#define htree_key_event_ignore(child, ln) \
340 + (!((child)->lc_events & (1 << (ln)->ln_mode)))
343 +htree_key_list_empty(struct htree_lock_node *ln)
345 + return list_empty(&ln->ln_major_list) && list_empty(&ln->ln_minor_list);
349 +htree_key_list_del_init(struct htree_lock_node *ln)
351 + struct htree_lock_node *tmp = NULL;
353 + if (!list_empty(&ln->ln_minor_list)) {
354 + tmp = list_entry(ln->ln_minor_list.next,
355 + struct htree_lock_node, ln_minor_list);
356 + list_del_init(&ln->ln_minor_list);
359 + if (list_empty(&ln->ln_major_list))
362 + if (tmp == NULL) { /* not on minor key list */
363 + list_del_init(&ln->ln_major_list);
365 + BUG_ON(!list_empty(&tmp->ln_major_list));
366 + list_replace_init(&ln->ln_major_list, &tmp->ln_major_list);
371 +htree_key_list_replace_init(struct htree_lock_node *old,
372 + struct htree_lock_node *new)
374 + if (!list_empty(&old->ln_major_list))
375 + list_replace_init(&old->ln_major_list, &new->ln_major_list);
377 + if (!list_empty(&old->ln_minor_list))
378 + list_replace_init(&old->ln_minor_list, &new->ln_minor_list);
382 +htree_key_event_enqueue(struct htree_lock_child *child,
383 + struct htree_lock_node *ln, int dep, void *event)
385 + struct htree_lock_node *tmp;
387 + /* NB: ALWAYS called holding lhead::lh_lock(dep) */
388 + BUG_ON(ln->ln_mode == HTREE_LOCK_NL);
389 + if (event == NULL || htree_key_event_ignore(child, ln))
392 + /* shouldn't be a very long list */
393 + list_for_each_entry(tmp, &ln->ln_alive_list, ln_alive_list) {
394 + if (tmp->ln_mode == HTREE_LOCK_NL) {
396 + if (child->lc_callback != NULL)
397 + child->lc_callback(tmp->ln_ev_target, event);
403 +htree_node_lock_enqueue(struct htree_lock *newlk, struct htree_lock *curlk,
404 + unsigned dep, int wait, void *event)
406 + struct htree_lock_child *child = &newlk->lk_head->lh_children[dep];
407 + struct htree_lock_node *newln = &newlk->lk_nodes[dep];
408 + struct htree_lock_node *curln = &curlk->lk_nodes[dep];
410 + /* NB: ALWAYS called holding lhead::lh_lock(dep) */
411 + /* NB: we only expect PR/PW lock mode at here, only these two modes are
412 + * allowed for htree_node_lock(asserted in htree_node_lock_internal),
413 + * NL is only used for listener, user can't directly require NL mode */
414 + if ((curln->ln_mode == HTREE_LOCK_NL) ||
415 + (curln->ln_mode != HTREE_LOCK_PW &&
416 + newln->ln_mode != HTREE_LOCK_PW)) {
417 + /* no conflict, attach it on granted list of @curlk */
418 + if (curln->ln_mode != HTREE_LOCK_NL) {
419 + list_add(&newln->ln_granted_list,
420 + &curln->ln_granted_list);
422 + /* replace key owner */
423 + htree_key_list_replace_init(curln, newln);
426 + list_add(&newln->ln_alive_list, &curln->ln_alive_list);
427 + htree_key_event_enqueue(child, newln, dep, event);
428 + ln_grant_inc(dep, newln->ln_mode);
429 + return 1; /* still hold lh_lock */
432 + if (!wait) { /* can't grant and don't want to wait */
433 + ln_retry_inc(dep, newln->ln_mode);
434 + newln->ln_mode = HTREE_LOCK_INVAL;
435 + return -1; /* don't wait and just return -1 */
438 + newlk->lk_task = current;
439 + set_current_state(TASK_UNINTERRUPTIBLE);
440 + /* conflict, attach it on blocked list of curlk */
441 + list_add_tail(&newln->ln_blocked_list, &curln->ln_blocked_list);
442 + list_add(&newln->ln_alive_list, &curln->ln_alive_list);
443 + ln_block_inc(dep, newln->ln_mode);
445 + htree_spin_unlock(newlk->lk_head, dep);
446 + /* wait to be given the lock */
447 + if (newlk->lk_task != NULL)
449 + /* granted, no doubt, wake up will set me RUNNING */
450 + if (event == NULL || htree_key_event_ignore(child, newln))
451 + return 0; /* granted without lh_lock */
453 + htree_spin_lock(newlk->lk_head, dep);
454 + htree_key_event_enqueue(child, newln, dep, event);
455 + return 1; /* still hold lh_lock */
459 + * get PR/PW access to particular tree-node according to @dep and @key,
460 + * it will return -1 if @wait is false and can't immediately grant this lock.
461 + * All listeners(HTREE_LOCK_NL) on @dep and with the same @key will get
462 + * @event if it's not NULL.
463 + * NB: ALWAYS called holding lhead::lh_lock
466 +htree_node_lock_internal(struct htree_lock_head *lhead, struct htree_lock *lck,
467 + htree_lock_mode_t mode, u32 key, unsigned dep,
468 + int wait, void *event)
471 + struct htree_lock *tmp;
472 + struct htree_lock *tmp2;
479 + BUG_ON(mode != HTREE_LOCK_PW && mode != HTREE_LOCK_PR);
480 + BUG_ON(htree_node_is_granted(lck, dep));
482 + key = hash_long(key, lhead->lh_hbits);
484 + mi_bits = lhead->lh_hbits >> 1;
485 + ma_bits = lhead->lh_hbits - mi_bits;
487 + lck->lk_nodes[dep].ln_major_key = major = key & ((1U << ma_bits) - 1);
488 + lck->lk_nodes[dep].ln_minor_key = minor = key >> ma_bits;
489 + lck->lk_nodes[dep].ln_mode = mode;
492 + * The major key list is an ordered list, so searches are started
493 + * at the end of the list that is numerically closer to major_key,
494 + * so at most half of the list will be walked (for well-distributed
495 + * keys). The list traversal aborts early if the expected key
496 + * location is passed.
498 + reverse = (major >= (1 << (ma_bits - 1)));
501 + list_for_each_entry_reverse(tmp,
502 + &lhead->lh_children[dep].lc_list,
503 + lk_nodes[dep].ln_major_list) {
504 + if (tmp->lk_nodes[dep].ln_major_key == major) {
507 + } else if (tmp->lk_nodes[dep].ln_major_key < major) {
508 + /* attach _after_ @tmp */
509 + list_add(&lck->lk_nodes[dep].ln_major_list,
510 + &tmp->lk_nodes[dep].ln_major_list);
511 + goto out_grant_major;
515 + list_add(&lck->lk_nodes[dep].ln_major_list,
516 + &lhead->lh_children[dep].lc_list);
517 + goto out_grant_major;
520 + list_for_each_entry(tmp, &lhead->lh_children[dep].lc_list,
521 + lk_nodes[dep].ln_major_list) {
522 + if (tmp->lk_nodes[dep].ln_major_key == major) {
525 + } else if (tmp->lk_nodes[dep].ln_major_key > major) {
526 + /* insert _before_ @tmp */
527 + list_add_tail(&lck->lk_nodes[dep].ln_major_list,
528 + &tmp->lk_nodes[dep].ln_major_list);
529 + goto out_grant_major;
533 + list_add_tail(&lck->lk_nodes[dep].ln_major_list,
534 + &lhead->lh_children[dep].lc_list);
535 + goto out_grant_major;
540 + * NB: minor_key list doesn't have a "head", @list is just a
541 + * temporary stub for helping list searching, make sure it's removed
543 + * minor_key list is an ordered list too.
545 + list_add_tail(&list, &tmp->lk_nodes[dep].ln_minor_list);
547 + reverse = (minor >= (1 << (mi_bits - 1)));
550 + list_for_each_entry_reverse(tmp2, &list,
551 + lk_nodes[dep].ln_minor_list) {
552 + if (tmp2->lk_nodes[dep].ln_minor_key == minor) {
555 + } else if (tmp2->lk_nodes[dep].ln_minor_key < minor) {
556 + /* attach _after_ @tmp2 */
557 + list_add(&lck->lk_nodes[dep].ln_minor_list,
558 + &tmp2->lk_nodes[dep].ln_minor_list);
559 + goto out_grant_minor;
563 + list_add(&lck->lk_nodes[dep].ln_minor_list, &list);
566 + list_for_each_entry(tmp2, &list,
567 + lk_nodes[dep].ln_minor_list) {
568 + if (tmp2->lk_nodes[dep].ln_minor_key == minor) {
571 + } else if (tmp2->lk_nodes[dep].ln_minor_key > minor) {
572 + /* insert _before_ @tmp2 */
573 + list_add_tail(&lck->lk_nodes[dep].ln_minor_list,
574 + &tmp2->lk_nodes[dep].ln_minor_list);
575 + goto out_grant_minor;
579 + list_add_tail(&lck->lk_nodes[dep].ln_minor_list, &list);
583 + if (list.next == &lck->lk_nodes[dep].ln_minor_list) {
584 + /* new lock @lck is the first one on minor_key list, which
585 + * means it has the smallest minor_key and it should
586 + * replace @tmp as minor_key owner */
587 + list_replace_init(&tmp->lk_nodes[dep].ln_major_list,
588 + &lck->lk_nodes[dep].ln_major_list);
590 + /* remove the temporary head */
594 + ln_grant_inc(dep, lck->lk_nodes[dep].ln_mode);
595 + return 1; /* granted with holding lh_lock */
598 + list_del(&list); /* remove temprary head */
599 + return htree_node_lock_enqueue(lck, tmp2, dep, wait, event);
603 + * release the key of @lck at level @dep, and grant any blocked locks.
604 + * caller will still listen on @key if @event is not NULL, which means
605 + * caller can see a event (by event_cb) while granting any lock with
606 + * the same key at level @dep.
607 + * NB: ALWAYS called holding lhead::lh_lock
608 + * NB: listener will not block anyone because listening mode is HTREE_LOCK_NL
611 +htree_node_unlock_internal(struct htree_lock_head *lhead,
612 + struct htree_lock *curlk, unsigned dep, void *event)
614 + struct htree_lock_node *curln = &curlk->lk_nodes[dep];
615 + struct htree_lock *grtlk = NULL;
616 + struct htree_lock_node *grtln;
617 + struct htree_lock *poslk;
618 + struct htree_lock *tmplk;
620 + if (!htree_node_is_granted(curlk, dep))
623 + if (!list_empty(&curln->ln_granted_list)) {
624 + /* there is another granted lock */
625 + grtlk = list_entry(curln->ln_granted_list.next,
627 + lk_nodes[dep].ln_granted_list);
628 + list_del_init(&curln->ln_granted_list);
631 + if (grtlk == NULL && !list_empty(&curln->ln_blocked_list)) {
633 + * @curlk is the only granted lock, so we confirmed:
634 + * a) curln is key owner (attached on major/minor_list),
635 + * so if there is any blocked lock, it should be attached
636 + * on curln->ln_blocked_list
637 + * b) we always can grant the first blocked lock
639 + grtlk = list_entry(curln->ln_blocked_list.next,
641 + lk_nodes[dep].ln_blocked_list);
642 + BUG_ON(grtlk->lk_task == NULL);
643 + wake_up_process(grtlk->lk_task);
646 + if (event != NULL &&
647 + lhead->lh_children[dep].lc_events != HTREE_EVENT_DISABLE) {
648 + curln->ln_ev_target = event;
649 + curln->ln_mode = HTREE_LOCK_NL; /* listen! */
651 + curln->ln_mode = HTREE_LOCK_INVAL;
654 + if (grtlk == NULL) { /* I must be the only one locking this key */
655 + struct htree_lock_node *tmpln;
657 + BUG_ON(htree_key_list_empty(curln));
659 + if (curln->ln_mode == HTREE_LOCK_NL) /* listening */
662 + /* not listening */
663 + if (list_empty(&curln->ln_alive_list)) { /* no more listener */
664 + htree_key_list_del_init(curln);
668 + tmpln = list_entry(curln->ln_alive_list.next,
669 + struct htree_lock_node, ln_alive_list);
671 + BUG_ON(tmpln->ln_mode != HTREE_LOCK_NL);
673 + htree_key_list_replace_init(curln, tmpln);
674 + list_del_init(&curln->ln_alive_list);
679 + /* have a granted lock */
680 + grtln = &grtlk->lk_nodes[dep];
681 + if (!list_empty(&curln->ln_blocked_list)) {
682 + /* only key owner can be on both lists */
683 + BUG_ON(htree_key_list_empty(curln));
685 + if (list_empty(&grtln->ln_blocked_list)) {
686 + list_add(&grtln->ln_blocked_list,
687 + &curln->ln_blocked_list);
689 + list_del_init(&curln->ln_blocked_list);
692 + * NB: this is the tricky part:
693 + * We have only two modes for child-lock (PR and PW), also,
694 + * only owner of the key (attached on major/minor_list) can be on
695 + * both blocked_list and granted_list, so @grtlk must be one
696 + * of these two cases:
698 + * a) @grtlk is taken from granted_list, which means we've granted
699 + * more than one lock so @grtlk has to be PR, the first blocked
700 + * lock must be PW and we can't grant it at all.
701 + * So even @grtlk is not owner of the key (empty blocked_list),
702 + * we don't care because we can't grant any lock.
703 + * b) we just grant a new lock which is taken from head of blocked
704 + * list, and it should be the first granted lock, and it should
705 + * be the first one linked on blocked_list.
707 + * Either way, we can get correct result by iterating blocked_list
708 + * of @grtlk, and don't have to bother on how to find out
709 + * owner of current key.
711 + list_for_each_entry_safe(poslk, tmplk, &grtln->ln_blocked_list,
712 + lk_nodes[dep].ln_blocked_list) {
713 + if (grtlk->lk_nodes[dep].ln_mode == HTREE_LOCK_PW ||
714 + poslk->lk_nodes[dep].ln_mode == HTREE_LOCK_PW)
716 + /* grant all readers */
717 + list_del_init(&poslk->lk_nodes[dep].ln_blocked_list);
718 + list_add(&poslk->lk_nodes[dep].ln_granted_list,
719 + &grtln->ln_granted_list);
721 + BUG_ON(poslk->lk_task == NULL);
722 + wake_up_process(poslk->lk_task);
725 + /* if @curln is the owner of this key, replace it with @grtln */
726 + if (!htree_key_list_empty(curln))
727 + htree_key_list_replace_init(curln, grtln);
729 + if (curln->ln_mode == HTREE_LOCK_INVAL)
730 + list_del_init(&curln->ln_alive_list);
734 + * it's just wrapper of htree_node_lock_internal, it returns 1 on granted
735 + * and 0 only if @wait is false and can't grant it immediately
738 +htree_node_lock_try(struct htree_lock *lck, htree_lock_mode_t mode,
739 + u32 key, unsigned dep, int wait, void *event)
741 + struct htree_lock_head *lhead = lck->lk_head;
744 + BUG_ON(dep >= lck->lk_depth);
745 + BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
747 + htree_spin_lock(lhead, dep);
748 + rc = htree_node_lock_internal(lhead, lck, mode, key, dep, wait, event);
750 + htree_spin_unlock(lhead, dep);
753 +EXPORT_SYMBOL(htree_node_lock_try);
755 +/* it's wrapper of htree_node_unlock_internal */
757 +htree_node_unlock(struct htree_lock *lck, unsigned dep, void *event)
759 + struct htree_lock_head *lhead = lck->lk_head;
761 + BUG_ON(dep >= lck->lk_depth);
762 + BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
764 + htree_spin_lock(lhead, dep);
765 + htree_node_unlock_internal(lhead, lck, dep, event);
766 + htree_spin_unlock(lhead, dep);
768 +EXPORT_SYMBOL(htree_node_unlock);
770 +/* stop listening on child-lock level @dep */
772 +htree_node_stop_listen(struct htree_lock *lck, unsigned dep)
774 + struct htree_lock_node *ln = &lck->lk_nodes[dep];
775 + struct htree_lock_node *tmp;
777 + BUG_ON(htree_node_is_granted(lck, dep));
778 + BUG_ON(!list_empty(&ln->ln_blocked_list));
779 + BUG_ON(!list_empty(&ln->ln_granted_list));
781 + if (!htree_node_is_listening(lck, dep))
784 + htree_spin_lock(lck->lk_head, dep);
785 + ln->ln_mode = HTREE_LOCK_INVAL;
786 + ln->ln_ev_target = NULL;
788 + if (htree_key_list_empty(ln)) { /* not owner */
789 + list_del_init(&ln->ln_alive_list);
793 + /* I'm the owner... */
794 + if (list_empty(&ln->ln_alive_list)) { /* no more listener */
795 + htree_key_list_del_init(ln);
799 + tmp = list_entry(ln->ln_alive_list.next,
800 + struct htree_lock_node, ln_alive_list);
802 + BUG_ON(tmp->ln_mode != HTREE_LOCK_NL);
803 + htree_key_list_replace_init(ln, tmp);
804 + list_del_init(&ln->ln_alive_list);
806 + htree_spin_unlock(lck->lk_head, dep);
808 +EXPORT_SYMBOL(htree_node_stop_listen);
810 +/* release all child-locks if we have any */
812 +htree_node_release_all(struct htree_lock *lck)
816 + for (i = 0; i < lck->lk_depth; i++) {
817 + if (htree_node_is_granted(lck, i))
818 + htree_node_unlock(lck, i, NULL);
819 + else if (htree_node_is_listening(lck, i))
820 + htree_node_stop_listen(lck, i);
825 + * obtain htree lock, it could be blocked inside if there's conflict
826 + * with any granted or blocked lock and @wait is true.
827 + * NB: ALWAYS called holding lhead::lh_lock
830 +htree_lock_internal(struct htree_lock *lck, int wait)
832 + struct htree_lock_head *lhead = lck->lk_head;
837 + for (i = 0; i < HTREE_LOCK_MAX; i++) {
838 + if (lhead->lh_ngranted[i] != 0)
840 + if (lhead->lh_nblocked[i] != 0)
843 + if ((htree_lock_compat[lck->lk_mode] & granted) != granted ||
844 + (htree_lock_compat[lck->lk_mode] & blocked) != blocked) {
845 + /* will block current lock even it just conflicts with any
846 + * other blocked lock, so lock like EX wouldn't starve */
849 + lhead->lh_nblocked[lck->lk_mode]++;
850 + lk_block_inc(lck->lk_mode);
852 + lck->lk_task = current;
853 + list_add_tail(&lck->lk_blocked_list, &lhead->lh_blocked_list);
856 + set_current_state(TASK_UNINTERRUPTIBLE);
857 + htree_spin_unlock(lhead, HTREE_DEP_ROOT);
858 + /* wait to be given the lock */
859 + if (lck->lk_task != NULL)
861 + /* granted, no doubt. wake up will set me RUNNING.
862 + * Since thread would be waken up accidentally,
863 + * so we need check lock whether granted or not again. */
864 + if (!list_empty(&lck->lk_blocked_list)) {
865 + htree_spin_lock(lhead, HTREE_DEP_ROOT);
866 + if (list_empty(&lck->lk_blocked_list)) {
867 + htree_spin_unlock(lhead, HTREE_DEP_ROOT);
872 + return 0; /* without lh_lock */
874 + lhead->lh_ngranted[lck->lk_mode]++;
875 + lk_grant_inc(lck->lk_mode);
879 +/* release htree lock. NB: ALWAYS called holding lhead::lh_lock */
881 +htree_unlock_internal(struct htree_lock *lck)
883 + struct htree_lock_head *lhead = lck->lk_head;
884 + struct htree_lock *tmp;
885 + struct htree_lock *tmp2;
889 + BUG_ON(lhead->lh_ngranted[lck->lk_mode] == 0);
891 + lhead->lh_ngranted[lck->lk_mode]--;
892 + lck->lk_mode = HTREE_LOCK_INVAL;
894 + for (i = 0; i < HTREE_LOCK_MAX; i++) {
895 + if (lhead->lh_ngranted[i] != 0)
898 + list_for_each_entry_safe(tmp, tmp2,
899 + &lhead->lh_blocked_list, lk_blocked_list) {
900 + /* conflict with any granted lock? */
901 + if ((htree_lock_compat[tmp->lk_mode] & granted) != granted)
904 + list_del_init(&tmp->lk_blocked_list);
906 + BUG_ON(lhead->lh_nblocked[tmp->lk_mode] == 0);
908 + lhead->lh_nblocked[tmp->lk_mode]--;
909 + lhead->lh_ngranted[tmp->lk_mode]++;
910 + granted |= 1 << tmp->lk_mode;
912 + BUG_ON(tmp->lk_task == NULL);
913 + wake_up_process(tmp->lk_task);
917 +/* it's wrapper of htree_lock_internal and exported interface.
918 + * It always return 1 with granted lock if @wait is true, it can return 0
919 + * if @wait is false and locking request can't be granted immediately */
921 +htree_lock_try(struct htree_lock *lck, struct htree_lock_head *lhead,
922 + htree_lock_mode_t mode, int wait)
926 + BUG_ON(lck->lk_depth > lhead->lh_depth);
927 + BUG_ON(lck->lk_head != NULL);
928 + BUG_ON(lck->lk_task != NULL);
930 + lck->lk_head = lhead;
931 + lck->lk_mode = mode;
933 + htree_spin_lock(lhead, HTREE_DEP_ROOT);
934 + rc = htree_lock_internal(lck, wait);
936 + htree_spin_unlock(lhead, HTREE_DEP_ROOT);
939 +EXPORT_SYMBOL(htree_lock_try);
941 +/* it's wrapper of htree_unlock_internal and exported interface.
942 + * It will release all htree_node_locks and htree_lock */
944 +htree_unlock(struct htree_lock *lck)
946 + BUG_ON(lck->lk_head == NULL);
947 + BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
949 + htree_node_release_all(lck);
951 + htree_spin_lock(lck->lk_head, HTREE_DEP_ROOT);
952 + htree_unlock_internal(lck);
953 + htree_spin_unlock(lck->lk_head, HTREE_DEP_ROOT);
954 + lck->lk_head = NULL;
955 + lck->lk_task = NULL;
957 +EXPORT_SYMBOL(htree_unlock);
959 +/* change lock mode */
961 +htree_change_mode(struct htree_lock *lck, htree_lock_mode_t mode)
963 + BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
964 + lck->lk_mode = mode;
966 +EXPORT_SYMBOL(htree_change_mode);
968 +/* release htree lock, and lock it again with new mode.
969 + * This function will first release all htree_node_locks and htree_lock,
970 + * then try to gain htree_lock with new @mode.
971 + * It always return 1 with granted lock if @wait is true, it can return 0
972 + * if @wait is false and locking request can't be granted immediately */
974 +htree_change_lock_try(struct htree_lock *lck, htree_lock_mode_t mode, int wait)
976 + struct htree_lock_head *lhead = lck->lk_head;
979 + BUG_ON(lhead == NULL);
980 + BUG_ON(lck->lk_mode == mode);
981 + BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL || mode == HTREE_LOCK_INVAL);
983 + htree_node_release_all(lck);
985 + htree_spin_lock(lhead, HTREE_DEP_ROOT);
986 + htree_unlock_internal(lck);
987 + lck->lk_mode = mode;
988 + rc = htree_lock_internal(lck, wait);
990 + htree_spin_unlock(lhead, HTREE_DEP_ROOT);
993 +EXPORT_SYMBOL(htree_change_lock_try);
995 +/* create a htree_lock head with @depth levels (number of child-locks),
996 + * it is a per resoruce structure */
997 +struct htree_lock_head *
998 +htree_lock_head_alloc(unsigned depth, unsigned hbits, unsigned priv)
1000 + struct htree_lock_head *lhead;
1003 + if (depth > HTREE_LOCK_DEP_MAX) {
1004 + printk(KERN_ERR "%d is larger than max htree_lock depth %d\n",
1005 + depth, HTREE_LOCK_DEP_MAX);
1009 + lhead = kzalloc(offsetof(struct htree_lock_head,
1010 + lh_children[depth]) + priv, GFP_NOFS);
1011 + if (lhead == NULL)
1014 + if (hbits < HTREE_HBITS_MIN)
1015 + lhead->lh_hbits = HTREE_HBITS_MIN;
1016 + else if (hbits > HTREE_HBITS_MAX)
1017 + lhead->lh_hbits = HTREE_HBITS_MAX;
1019 + lhead->lh_lock = 0;
1020 + lhead->lh_depth = depth;
1021 + INIT_LIST_HEAD(&lhead->lh_blocked_list);
1023 + lhead->lh_private = (void *)lhead +
1024 + offsetof(struct htree_lock_head, lh_children[depth]);
1027 + for (i = 0; i < depth; i++) {
1028 + INIT_LIST_HEAD(&lhead->lh_children[i].lc_list);
1029 + lhead->lh_children[i].lc_events = HTREE_EVENT_DISABLE;
1033 +EXPORT_SYMBOL(htree_lock_head_alloc);
1035 +/* free the htree_lock head */
1037 +htree_lock_head_free(struct htree_lock_head *lhead)
1041 + BUG_ON(!list_empty(&lhead->lh_blocked_list));
1042 + for (i = 0; i < lhead->lh_depth; i++)
1043 + BUG_ON(!list_empty(&lhead->lh_children[i].lc_list));
1046 +EXPORT_SYMBOL(htree_lock_head_free);
1048 +/* register event callback for @events of child-lock at level @dep */
1050 +htree_lock_event_attach(struct htree_lock_head *lhead, unsigned dep,
1051 + unsigned events, htree_event_cb_t callback)
1053 + BUG_ON(lhead->lh_depth <= dep);
1054 + lhead->lh_children[dep].lc_events = events;
1055 + lhead->lh_children[dep].lc_callback = callback;
1057 +EXPORT_SYMBOL(htree_lock_event_attach);
1059 +/* allocate a htree_lock, which is per-thread structure, @pbytes is some
1060 + * extra-bytes as private data for caller */
1061 +struct htree_lock *
1062 +htree_lock_alloc(unsigned depth, unsigned pbytes)
1064 + struct htree_lock *lck;
1065 + int i = offsetof(struct htree_lock, lk_nodes[depth]);
1067 + if (depth > HTREE_LOCK_DEP_MAX) {
1068 + printk(KERN_ERR "%d is larger than max htree_lock depth %d\n",
1069 + depth, HTREE_LOCK_DEP_MAX);
1072 + lck = kzalloc(i + pbytes, GFP_NOFS);
1077 + lck->lk_private = (void *)lck + i;
1078 + lck->lk_mode = HTREE_LOCK_INVAL;
1079 + lck->lk_depth = depth;
1080 + INIT_LIST_HEAD(&lck->lk_blocked_list);
1082 + for (i = 0; i < depth; i++) {
1083 + struct htree_lock_node *node = &lck->lk_nodes[i];
1085 + node->ln_mode = HTREE_LOCK_INVAL;
1086 + INIT_LIST_HEAD(&node->ln_major_list);
1087 + INIT_LIST_HEAD(&node->ln_minor_list);
1088 + INIT_LIST_HEAD(&node->ln_alive_list);
1089 + INIT_LIST_HEAD(&node->ln_blocked_list);
1090 + INIT_LIST_HEAD(&node->ln_granted_list);
1095 +EXPORT_SYMBOL(htree_lock_alloc);
1097 +/* free htree_lock node */
1099 +htree_lock_free(struct htree_lock *lck)
1101 + BUG_ON(lck->lk_mode != HTREE_LOCK_INVAL);
1104 +EXPORT_SYMBOL(htree_lock_free);
1105 Index: linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/Makefile
1106 ===================================================================
1107 --- linux-3.10.0-229.1.2.fc21.x86_64.orig/fs/ext4/Makefile
1108 +++ linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/Makefile
1109 @@ -6,6 +6,7 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
1111 ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
1112 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
1114 ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
1115 mmp.o indirect.o extents_status.o xattr.o xattr_user.o \
1116 xattr_trusted.o inline.o
1117 Index: linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/ext4.h
1118 ===================================================================
1119 --- linux-3.10.0-229.1.2.fc21.x86_64.orig/fs/ext4/ext4.h
1120 +++ linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/ext4.h
1122 #include <linux/mutex.h>
1123 #include <linux/timer.h>
1124 #include <linux/wait.h>
1125 +#include <linux/htree_lock.h>
1126 #include <linux/blockgroup_lock.h>
1127 #include <linux/percpu_counter.h>
1128 #include <linux/ratelimit.h>
1129 @@ -821,6 +822,9 @@ struct ext4_inode_info {
1131 ext4_fsblk_t i_file_acl;
1133 + /* following fields for parallel directory operations -bzzz */
1134 + struct semaphore i_append_sem;
1137 * i_block_group is the number of the block group which contains
1138 * this file's inode. Constant across the lifetime of the inode,
1139 @@ -1846,6 +1850,71 @@ struct dx_hash_info
1141 #define HASH_NB_ALWAYS 1
1143 +/* assume name-hash is protected by upper layer */
1144 +#define EXT4_HTREE_LOCK_HASH 0
1146 +enum ext4_pdo_lk_types {
1147 +#if EXT4_HTREE_LOCK_HASH
1150 + EXT4_LK_DX, /* index block */
1151 + EXT4_LK_DE, /* directory entry block */
1152 + EXT4_LK_SPIN, /* spinlock */
1156 +/* read-only bit */
1157 +#define EXT4_LB_RO(b) (1 << (b))
1158 +/* read + write, high bits for writer */
1159 +#define EXT4_LB_RW(b) ((1 << (b)) | (1 << (EXT4_LK_MAX + (b))))
1161 +enum ext4_pdo_lock_bits {
1162 + /* DX lock bits */
1163 + EXT4_LB_DX_RO = EXT4_LB_RO(EXT4_LK_DX),
1164 + EXT4_LB_DX = EXT4_LB_RW(EXT4_LK_DX),
1165 + /* DE lock bits */
1166 + EXT4_LB_DE_RO = EXT4_LB_RO(EXT4_LK_DE),
1167 + EXT4_LB_DE = EXT4_LB_RW(EXT4_LK_DE),
1168 + /* DX spinlock bits */
1169 + EXT4_LB_SPIN_RO = EXT4_LB_RO(EXT4_LK_SPIN),
1170 + EXT4_LB_SPIN = EXT4_LB_RW(EXT4_LK_SPIN),
1171 + /* accurate searching */
1172 + EXT4_LB_EXACT = EXT4_LB_RO(EXT4_LK_MAX << 1),
1175 +enum ext4_pdo_lock_opc {
1177 + EXT4_HLOCK_READDIR = (EXT4_LB_DE_RO | EXT4_LB_DX_RO),
1178 + EXT4_HLOCK_LOOKUP = (EXT4_LB_DE_RO | EXT4_LB_SPIN_RO |
1180 + EXT4_HLOCK_DEL = (EXT4_LB_DE | EXT4_LB_SPIN_RO |
1182 + EXT4_HLOCK_ADD = (EXT4_LB_DE | EXT4_LB_SPIN_RO),
1185 + EXT4_HLOCK_LOOKUP_SAFE = (EXT4_LB_DE_RO | EXT4_LB_DX_RO |
1187 + EXT4_HLOCK_DEL_SAFE = (EXT4_LB_DE | EXT4_LB_DX_RO | EXT4_LB_EXACT),
1188 + EXT4_HLOCK_SPLIT = (EXT4_LB_DE | EXT4_LB_DX | EXT4_LB_SPIN),
1191 +extern struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits);
1192 +#define ext4_htree_lock_head_free(lhead) htree_lock_head_free(lhead)
1194 +extern struct htree_lock *ext4_htree_lock_alloc(void);
1195 +#define ext4_htree_lock_free(lck) htree_lock_free(lck)
1197 +extern void ext4_htree_lock(struct htree_lock *lck,
1198 + struct htree_lock_head *lhead,
1199 + struct inode *dir, unsigned flags);
1200 +#define ext4_htree_unlock(lck) htree_unlock(lck)
1202 +extern struct buffer_head *__ext4_find_entry(struct inode *dir,
1203 + const struct qstr *d_name,
1204 + struct ext4_dir_entry_2 **res_dir,
1205 + int *inlined, struct htree_lock *lck);
1206 +extern int __ext4_add_entry(handle_t *handle, struct dentry *dentry,
1207 + struct inode *inode, struct htree_lock *lck);
1210 * Describe an inode's exact location on disk and in memory
1211 @@ -2088,8 +2157,16 @@ void ext4_insert_dentry(struct inode *in
1212 const char *name, int namelen, void *data);
1213 static inline void ext4_update_dx_flag(struct inode *inode)
1215 + /* Disable it for ldiskfs, because going from a DX directory to
1216 + * a non-DX directory while it is in use will completely break
1217 + * the htree-locking.
1218 + * If we really want to support this operation in the future,
1219 + * we need to exclusively lock the directory at here which will
1220 + * increase complexity of code */
1222 if (!ext4_has_feature_dir_index(inode->i_sb))
1223 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
1226 static unsigned char ext4_filetype_table[] = {
1227 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1228 Index: linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/namei.c
1229 ===================================================================
1230 --- linux-3.10.0-229.1.2.fc21.x86_64.orig/fs/ext4/namei.c
1231 +++ linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/namei.c
1232 @@ -53,6 +53,7 @@ struct buffer_head *ext4_append(handle_t
1235 struct buffer_head *bh;
1236 + struct ext4_inode_info *ei = EXT4_I(inode);
1239 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
1240 @@ -60,15 +61,22 @@ struct buffer_head *ext4_append(handle_t
1241 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
1242 return ERR_PTR(-ENOSPC);
1244 + /* with parallel dir operations all appends
1245 + * have to be serialized -bzzz */
1246 + down(&ei->i_append_sem);
1248 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
1250 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
1253 + up(&ei->i_append_sem);
1256 inode->i_size += inode->i_sb->s_blocksize;
1257 EXT4_I(inode)->i_disksize = inode->i_size;
1258 BUFFER_TRACE(bh, "get_write_access");
1259 err = ext4_journal_get_write_access(handle, bh);
1260 + up(&ei->i_append_sem);
1263 ext4_std_error(inode->i_sb, err);
1264 @@ -246,7 +254,8 @@ static struct dx_frame *dx_probe(const s
1267 struct dx_hash_info *hinfo,
1268 - struct dx_frame *frame);
1269 + struct dx_frame *frame,
1270 + struct htree_lock *lck);
1271 static void dx_release(struct dx_frame *frames);
1272 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1273 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
1274 @@ -259,12 +267,13 @@ static void dx_insert_block(struct dx_fr
1275 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
1276 struct dx_frame *frame,
1277 struct dx_frame *frames,
1278 - __u32 *start_hash);
1279 + __u32 *start_hash, struct htree_lock *lck);
1280 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1281 struct ext4_filename *fname,
1282 - struct ext4_dir_entry_2 **res_dir);
1283 + struct ext4_dir_entry_2 **res_dir, struct htree_lock *lck);
1284 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
1285 - struct dentry *dentry, struct inode *inode);
1286 + struct dentry *dentry, struct inode *inode,
1287 + struct htree_lock *lck);
1289 /* checksumming functions */
1290 void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
1291 @@ -668,6 +676,227 @@ struct stats dx_show_entries(struct dx_h
1293 #endif /* DX_DEBUG */
1295 +/* private data for htree_lock */
1296 +struct ext4_dir_lock_data {
1297 + unsigned ld_flags; /* bits-map for lock types */
1298 + unsigned ld_count; /* # entries of the last DX block */
1299 + struct dx_entry ld_at_entry; /* copy of leaf dx_entry */
1300 + struct dx_entry *ld_at; /* position of leaf dx_entry */
1303 +#define ext4_htree_lock_data(l) ((struct ext4_dir_lock_data *)(l)->lk_private)
1304 +#define ext4_find_entry(dir, name, dirent, inline) \
1305 + __ext4_find_entry(dir, name, dirent, inline, NULL)
1306 +#define ext4_add_entry(handle, dentry, inode) \
1307 + __ext4_add_entry(handle, dentry, inode, NULL)
1309 +/* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
1310 +#define EXT4_HTREE_NODE_CHANGED (0xcafeULL << 32)
1312 +static void ext4_htree_event_cb(void *target, void *event)
1314 + u64 *block = (u64 *)target;
1316 + if (*block == dx_get_block((struct dx_entry *)event))
1317 + *block = EXT4_HTREE_NODE_CHANGED;
1320 +struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits)
1322 + struct htree_lock_head *lhead;
1324 + lhead = htree_lock_head_alloc(EXT4_LK_MAX, hbits, 0);
1325 + if (lhead != NULL) {
1326 + htree_lock_event_attach(lhead, EXT4_LK_SPIN, HTREE_EVENT_WR,
1327 + ext4_htree_event_cb);
1331 +EXPORT_SYMBOL(ext4_htree_lock_head_alloc);
1333 +struct htree_lock *ext4_htree_lock_alloc(void)
1335 + return htree_lock_alloc(EXT4_LK_MAX,
1336 + sizeof(struct ext4_dir_lock_data));
1338 +EXPORT_SYMBOL(ext4_htree_lock_alloc);
1340 +static htree_lock_mode_t ext4_htree_mode(unsigned flags)
1343 + default: /* 0 or unknown flags require EX lock */
1344 + return HTREE_LOCK_EX;
1345 + case EXT4_HLOCK_READDIR:
1346 + return HTREE_LOCK_PR;
1347 + case EXT4_HLOCK_LOOKUP:
1348 + return HTREE_LOCK_CR;
1349 + case EXT4_HLOCK_DEL:
1350 + case EXT4_HLOCK_ADD:
1351 + return HTREE_LOCK_CW;
1355 +/* return PR for read-only operations, otherwise return EX */
1356 +static inline htree_lock_mode_t ext4_htree_safe_mode(unsigned flags)
1358 + int writer = (flags & EXT4_LB_DE) == EXT4_LB_DE;
1360 + /* 0 requires EX lock */
1361 + return (flags == 0 || writer) ? HTREE_LOCK_EX : HTREE_LOCK_PR;
1364 +static int ext4_htree_safe_locked(struct htree_lock *lck)
1368 + if (lck == NULL || lck->lk_mode == HTREE_LOCK_EX)
1371 + writer = (ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_DE) ==
1373 + if (writer) /* all readers & writers are excluded? */
1374 + return lck->lk_mode == HTREE_LOCK_EX;
1376 + /* all writers are excluded? */
1377 + return lck->lk_mode == HTREE_LOCK_PR ||
1378 + lck->lk_mode == HTREE_LOCK_PW ||
1379 + lck->lk_mode == HTREE_LOCK_EX;
1382 +/* relock htree_lock with EX mode if it's change operation, otherwise
1383 + * relock it with PR mode. It's noop if PDO is disabled. */
1384 +static void ext4_htree_safe_relock(struct htree_lock *lck)
1386 + if (!ext4_htree_safe_locked(lck)) {
1387 + unsigned flags = ext4_htree_lock_data(lck)->ld_flags;
1389 + htree_change_lock(lck, ext4_htree_safe_mode(flags));
1393 +void ext4_htree_lock(struct htree_lock *lck, struct htree_lock_head *lhead,
1394 + struct inode *dir, unsigned flags)
1396 + htree_lock_mode_t mode = is_dx(dir) ? ext4_htree_mode(flags) :
1397 + ext4_htree_safe_mode(flags);
1399 + ext4_htree_lock_data(lck)->ld_flags = flags;
1400 + htree_lock(lck, lhead, mode);
1402 + ext4_htree_safe_relock(lck); /* make sure it's safe locked */
1404 +EXPORT_SYMBOL(ext4_htree_lock);
1406 +static int ext4_htree_node_lock(struct htree_lock *lck, struct dx_entry *at,
1407 + unsigned lmask, int wait, void *ev)
1409 + u32 key = (at == NULL) ? 0 : dx_get_block(at);
1412 + /* NOOP if htree is well protected or caller doesn't require the lock */
1413 + if (ext4_htree_safe_locked(lck) ||
1414 + !(ext4_htree_lock_data(lck)->ld_flags & lmask))
1417 + mode = (ext4_htree_lock_data(lck)->ld_flags & lmask) == lmask ?
1418 + HTREE_LOCK_PW : HTREE_LOCK_PR;
1420 + if (htree_node_lock_try(lck, mode, key, ffz(~lmask), wait, ev))
1422 + if (!(lmask & EXT4_LB_SPIN)) /* not a spinlock */
1424 + cpu_relax(); /* spin until granted */
1428 +static int ext4_htree_node_locked(struct htree_lock *lck, unsigned lmask)
1430 + return ext4_htree_safe_locked(lck) ||
1431 + htree_node_is_granted(lck, ffz(~lmask));
1434 +static void ext4_htree_node_unlock(struct htree_lock *lck,
1435 + unsigned lmask, void *buf)
1437 + /* NB: it's safe to call mutiple times or even it's not locked */
1438 + if (!ext4_htree_safe_locked(lck) &&
1439 + htree_node_is_granted(lck, ffz(~lmask)))
1440 + htree_node_unlock(lck, ffz(~lmask), buf);
1443 +#define ext4_htree_dx_lock(lck, key) \
1444 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 1, NULL)
1445 +#define ext4_htree_dx_lock_try(lck, key) \
1446 + ext4_htree_node_lock(lck, key, EXT4_LB_DX, 0, NULL)
1447 +#define ext4_htree_dx_unlock(lck) \
1448 + ext4_htree_node_unlock(lck, EXT4_LB_DX, NULL)
1449 +#define ext4_htree_dx_locked(lck) \
1450 + ext4_htree_node_locked(lck, EXT4_LB_DX)
1452 +static void ext4_htree_dx_need_lock(struct htree_lock *lck)
1454 + struct ext4_dir_lock_data *ld;
1456 + if (ext4_htree_safe_locked(lck))
1459 + ld = ext4_htree_lock_data(lck);
1460 + switch (ld->ld_flags) {
1463 + case EXT4_HLOCK_LOOKUP:
1464 + ld->ld_flags = EXT4_HLOCK_LOOKUP_SAFE;
1466 + case EXT4_HLOCK_DEL:
1467 + ld->ld_flags = EXT4_HLOCK_DEL_SAFE;
1469 + case EXT4_HLOCK_ADD:
1470 + ld->ld_flags = EXT4_HLOCK_SPLIT;
1475 +#define ext4_htree_de_lock(lck, key) \
1476 + ext4_htree_node_lock(lck, key, EXT4_LB_DE, 1, NULL)
1477 +#define ext4_htree_de_unlock(lck) \
1478 + ext4_htree_node_unlock(lck, EXT4_LB_DE, NULL)
1480 +#define ext4_htree_spin_lock(lck, key, event) \
1481 + ext4_htree_node_lock(lck, key, EXT4_LB_SPIN, 0, event)
1482 +#define ext4_htree_spin_unlock(lck) \
1483 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, NULL)
1484 +#define ext4_htree_spin_unlock_listen(lck, p) \
1485 + ext4_htree_node_unlock(lck, EXT4_LB_SPIN, p)
1487 +static void ext4_htree_spin_stop_listen(struct htree_lock *lck)
1489 + if (!ext4_htree_safe_locked(lck) &&
1490 + htree_node_is_listening(lck, ffz(~EXT4_LB_SPIN)))
1491 + htree_node_stop_listen(lck, ffz(~EXT4_LB_SPIN));
1495 + DX_HASH_COL_IGNORE, /* ignore collision while probing frames */
1496 + DX_HASH_COL_YES, /* there is collision and it does matter */
1497 + DX_HASH_COL_NO, /* there is no collision */
1500 +static int dx_probe_hash_collision(struct htree_lock *lck,
1501 + struct dx_entry *entries,
1502 + struct dx_entry *at, u32 hash)
1504 + if (!(lck && ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_EXACT)) {
1505 + return DX_HASH_COL_IGNORE; /* don't care about collision */
1507 + } else if (at == entries + dx_get_count(entries) - 1) {
1508 + return DX_HASH_COL_IGNORE; /* not in any leaf of this DX */
1510 + } else { /* hash collision? */
1511 + return ((dx_get_hash(at + 1) & ~1) == hash) ?
1512 + DX_HASH_COL_YES : DX_HASH_COL_NO;
1517 * Probe for a directory leaf block to search.
1519 @@ -679,10 +908,11 @@ struct stats dx_show_entries(struct dx_h
1521 static struct dx_frame *
1522 dx_probe(struct ext4_filename *fname, struct inode *dir,
1523 - struct dx_hash_info *hinfo, struct dx_frame *frame_in)
1524 + struct dx_hash_info *hinfo, struct dx_frame *frame_in,
1525 + struct htree_lock *lck)
1527 unsigned count, indirect;
1528 - struct dx_entry *at, *entries, *p, *q, *m;
1529 + struct dx_entry *at, *entries, *p, *q, *m, *dx = NULL;
1530 struct dx_root_info *info;
1531 struct dx_frame *frame = frame_in;
1532 struct buffer_head *bh;
1533 @@ -750,8 +980,15 @@ dx_probe(const struct qstr *d_name, stru
1535 dxtrace(printk("Look up %x", hash));
1537 + if (indirect == 0) { /* the last index level */
1538 + /* NB: ext4_htree_dx_lock() could be noop if
1539 + * DX-lock flag is not set for current operation */
1540 + ext4_htree_dx_lock(lck, dx);
1541 + ext4_htree_spin_lock(lck, dx, NULL);
1543 count = dx_get_count(entries);
1544 - if (!count || count > dx_get_limit(entries)) {
1545 + if (count == 0 || count > dx_get_limit(entries)) {
1546 + ext4_htree_spin_unlock(lck); /* release spin */
1547 ext4_warning_inode(dir,
1548 "dx entry: no count or count > limit");
1550 @@ -792,8 +1029,70 @@ dx_probe(const struct qstr *d_name, stru
1552 frame->entries = entries;
1557 + if (indirect == 0) { /* the last index level */
1558 + struct ext4_dir_lock_data *ld;
1561 + /* By default we only lock DE-block, however, we will
1562 + * also lock the last level DX-block if:
1563 + * a) there is hash collision
1564 + * we will set DX-lock flag (a few lines below)
1565 + * and redo to lock DX-block
1566 + * see detail in dx_probe_hash_collision()
1567 + * b) it's a retry from splitting
1568 + * we need to lock the last level DX-block so nobody
1569 + * else can split any leaf blocks under the same
1570 + * DX-block, see detail in ext4_dx_add_entry()
1572 + if (ext4_htree_dx_locked(lck)) {
1573 + /* DX-block is locked, just lock DE-block
1575 + ext4_htree_spin_unlock(lck);
1576 + if (!ext4_htree_safe_locked(lck))
1577 + ext4_htree_de_lock(lck, frame->at);
1580 + /* it's pdirop and no DX lock */
1581 + if (dx_probe_hash_collision(lck, entries, at, hash) ==
1582 + DX_HASH_COL_YES) {
1583 + /* found hash collision, set DX-lock flag
1584 + * and retry to abtain DX-lock */
1585 + ext4_htree_spin_unlock(lck);
1586 + ext4_htree_dx_need_lock(lck);
1589 + ld = ext4_htree_lock_data(lck);
1590 + /* because I don't lock DX, so @at can't be trusted
1591 + * after I release spinlock so I have to save it */
1593 + ld->ld_at_entry = *at;
1594 + ld->ld_count = dx_get_count(entries);
1596 + frame->at = &ld->ld_at_entry;
1597 + myblock = dx_get_block(at);
1599 + /* NB: ordering locking */
1600 + ext4_htree_spin_unlock_listen(lck, &myblock);
1601 + /* other thread can split this DE-block because:
1602 + * a) I don't have lock for the DE-block yet
1603 + * b) I released spinlock on DX-block
1604 + * if it happened I can detect it by listening
1605 + * splitting event on this DE-block */
1606 + ext4_htree_de_lock(lck, frame->at);
1607 + ext4_htree_spin_stop_listen(lck);
1609 + if (myblock == EXT4_HTREE_NODE_CHANGED) {
1610 + /* someone split this DE-block before
1611 + * I locked it, I need to retry and lock
1612 + * valid DE-block */
1613 + ext4_htree_de_unlock(lck);
1621 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
1623 @@ -860,7 +1160,7 @@ static void dx_release (struct dx_frame
1624 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
1625 struct dx_frame *frame,
1626 struct dx_frame *frames,
1627 - __u32 *start_hash)
1628 + __u32 *start_hash, struct htree_lock *lck)
1631 struct buffer_head *bh;
1632 @@ -875,12 +1175,22 @@ static int ext4_htree_next_block(struct
1633 * this loop, num_frames indicates the number of interior
1634 * nodes need to be read.
1636 + ext4_htree_de_unlock(lck);
1638 - if (++(p->at) < p->entries + dx_get_count(p->entries))
1640 + if (num_frames > 0 || ext4_htree_dx_locked(lck)) {
1641 + /* num_frames > 0 :
1643 + * ext4_htree_dx_locked:
1644 + * frame->at is reliable pointer returned by dx_probe,
1645 + * otherwise dx_probe already knew no collision */
1646 + if (++(p->at) < p->entries + dx_get_count(p->entries))
1652 + if (num_frames == 1)
1653 + ext4_htree_dx_unlock(lck);
1657 @@ -903,6 +1213,13 @@ static int ext4_htree_next_block(struct
1658 * block so no check is necessary
1660 while (num_frames--) {
1661 + if (num_frames == 0) {
1662 + /* it's not always necessary, we just don't want to
1663 + * detect hash collision again */
1664 + ext4_htree_dx_need_lock(lck);
1665 + ext4_htree_dx_lock(lck, p->at);
1668 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
1671 @@ -911,6 +1228,7 @@ static int ext4_htree_next_block(struct
1673 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1675 + ext4_htree_de_lock(lck, p->at);
1679 @@ -1013,10 +1331,10 @@ int ext4_htree_fill_tree(struct file *di
1681 hinfo.hash = start_hash;
1682 hinfo.minor_hash = 0;
1683 - frame = dx_probe(NULL, dir, &hinfo, frames);
1684 + /* assume it's PR locked */
1685 + frame = dx_probe(NULL, dir, &hinfo, frames, NULL);
1687 return PTR_ERR(frame);
1689 /* Add '.' and '..' from the htree header */
1690 if (!start_hash && !start_minor_hash) {
1691 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1692 @@ -1043,7 +1361,7 @@ int ext4_htree_fill_tree(struct file *di
1695 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1696 - frame, frames, &hashval);
1697 + frame, frames, &hashval, NULL);
1698 *next_hash = hashval;
1701 @@ -1236,10 +1554,10 @@ static int is_dx_internal_node(struct in
1702 * The returned buffer_head has ->b_count elevated. The caller is expected
1703 * to brelse() it when appropriate.
1705 -static struct buffer_head * ext4_find_entry (struct inode *dir,
1706 +struct buffer_head *__ext4_find_entry(struct inode *dir,
1707 const struct qstr *d_name,
1708 struct ext4_dir_entry_2 **res_dir,
1710 + int *inlined, struct htree_lock *lck)
1712 struct super_block *sb;
1713 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1714 @@ -1283,7 +1601,7 @@ static struct buffer_head * ext4_find_en
1718 - ret = ext4_dx_find_entry(dir, &fname, res_dir);
1719 + ret = ext4_dx_find_entry(dir, &fname, res_dir, lck);
1721 * On success, or if the error was file not found,
1722 * return. Otherwise, fall back to doing a search the
1723 @@ -1297,6 +1615,7 @@ static struct buffer_head * ext4_find_en
1725 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1727 + ext4_htree_safe_relock(lck);
1729 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1730 start = EXT4_I(dir)->i_dir_start_lookup;
1731 @@ -1389,10 +1708,12 @@ cleanup_and_exit:
1732 brelse(bh_use[ra_ptr]);
1735 +EXPORT_SYMBOL(__ext4_find_entry);
1737 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1738 struct ext4_filename *fname,
1739 - struct ext4_dir_entry_2 **res_dir)
1740 + struct ext4_dir_entry_2 **res_dir,
1741 + struct htree_lock *lck)
1743 struct super_block * sb = dir->i_sb;
1744 struct dx_hash_info hinfo;
1745 @@ -1400,7 +1722,7 @@ static struct buffer_head * ext4_dx_find
1749 - frame = dx_probe(fname, dir, NULL, frames);
1750 + frame = dx_probe(fname, dir, NULL, frames, lck);
1754 @@ -1424,7 +1746,7 @@ static struct buffer_head * ext4_dx_find
1756 /* Check to see if we should continue to search */
1757 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
1759 + frames, NULL, lck);
1762 "error reading index page in directory #%lu",
1763 @@ -1583,8 +1905,9 @@ static struct ext4_dir_entry_2* dx_pack_
1764 * Returns pointer to de in block into which the new entry will be inserted.
1766 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1767 - struct buffer_head **bh,struct dx_frame *frame,
1768 - struct dx_hash_info *hinfo)
1769 + struct buffer_head **bh, struct dx_frame *frames,
1770 + struct dx_frame *frame, struct dx_hash_info *hinfo,
1771 + struct htree_lock *lck)
1773 unsigned blocksize = dir->i_sb->s_blocksize;
1774 unsigned count, continued;
1775 @@ -1647,8 +1970,14 @@ static struct ext4_dir_entry_2 *do_split
1776 hash2, split, count-split));
1778 /* Fancy dance to stay within two buffers */
1779 - de2 = dx_move_dirents(data1, data2, map + split, count - split,
1781 + if (hinfo->hash < hash2) {
1782 + de2 = dx_move_dirents(data1, data2, map + split,
1783 + count - split, blocksize);
1785 + /* make sure we will add entry to the same block which
1786 + * we have already locked */
1787 + de2 = dx_move_dirents(data1, data2, map, split, blocksize);
1789 de = dx_pack_dirents(data1, blocksize);
1790 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1792 @@ -1666,12 +1996,21 @@ static struct ext4_dir_entry_2 *do_split
1793 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1794 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1796 - /* Which block gets the new entry? */
1797 - if (hinfo->hash >= hash2) {
1800 + ext4_htree_spin_lock(lck, frame > frames ? (frame - 1)->at : NULL,
1801 + frame->at); /* notify block is being split */
1802 + if (hinfo->hash < hash2) {
1803 + dx_insert_block(frame, hash2 + continued, newblock);
1806 + /* switch block number */
1807 + dx_insert_block(frame, hash2 + continued,
1808 + dx_get_block(frame->at));
1809 + dx_set_block(frame->at, newblock);
1812 - dx_insert_block(frame, hash2 + continued, newblock);
1813 + ext4_htree_spin_unlock(lck);
1814 + ext4_htree_dx_unlock(lck);
1816 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1819 @@ -1945,9 +2283,9 @@ static int make_indexed_dir(handle_t *ha
1820 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1824 - de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
1825 + de = do_split(handle, dir, &bh2, frames, frame, &fname->hinfo, NULL);
1827 retval = PTR_ERR(de);
1830 @@ -2051,8 +2389,8 @@ out:
1831 * may not sleep between calling this and putting something into
1832 * the entry, as someone else might have used it while you slept.
1834 -static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1835 - struct inode *inode)
1836 +int __ext4_add_entry(handle_t *handle, struct dentry *dentry,
1837 + struct inode *inode, struct htree_lock *lck)
1839 struct inode *dir = dentry->d_parent->d_inode;
1840 struct buffer_head *bh;
1841 @@ -2087,9 +2425,10 @@ static int ext4_add_entry(handle_t *hand
1842 if (dentry->d_name.len == 2 &&
1843 memcmp(dentry->d_name.name, "..", 2) == 0)
1844 return ext4_update_dotdot(handle, dentry, inode);
1845 - retval = ext4_dx_add_entry(handle, &fname, dentry, inode);
1846 + retval = ext4_dx_add_entry(handle, &fname, dentry, inode, lck);
1847 if (!retval || (retval != ERR_BAD_DX_DIR))
1849 + ext4_htree_safe_relock(lck);
1850 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1852 ext4_mark_inode_dirty(handle, dir);
1853 @@ -2129,12 +2468,14 @@ static int ext4_add_entry(handle_t *hand
1854 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1857 +EXPORT_SYMBOL(__ext4_add_entry);
1860 * Returns 0 for success, or a negative error value
1862 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
1863 - struct dentry *dentry, struct inode *inode)
1864 + struct dentry *dentry, struct inode *inode,
1865 + struct htree_lock *lck)
1867 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1868 struct dx_entry *entries, *at;
1869 @@ -2148,7 +2488,7 @@ static int ext4_dx_add_entry(handle_t *h
1873 - frame = dx_probe(fname, dir, NULL, frames);
1874 + frame = dx_probe(fname, dir, NULL, frames, lck);
1876 return PTR_ERR(frame);
1877 entries = frame->entries;
1878 @@ -2178,6 +2518,11 @@ again:
1879 struct dx_node *node2;
1880 struct buffer_head *bh2;
1882 + if (!ext4_htree_safe_locked(lck)) { /* retry with EX lock */
1883 + ext4_htree_safe_relock(lck);
1887 while (frame > frames) {
1888 if (dx_get_count((frame - 1)->entries) <
1889 dx_get_limit((frame - 1)->entries)) {
1890 @@ -2277,8 +2622,32 @@ again:
1894 + } else if (!ext4_htree_dx_locked(lck)) {
1895 + struct ext4_dir_lock_data *ld = ext4_htree_lock_data(lck);
1897 + /* not well protected, require DX lock */
1898 + ext4_htree_dx_need_lock(lck);
1899 + at = frame > frames ? (frame - 1)->at : NULL;
1901 + /* NB: no risk of deadlock because it's just a try.
1903 + * NB: we check ld_count for twice, the first time before
1904 + * having DX lock, the second time after holding DX lock.
1906 + * NB: We never free blocks for directory so far, which
1907 + * means value returned by dx_get_count() should equal to
1908 + * ld->ld_count if nobody split any DE-block under @at,
1909 + * and ld->ld_at still points to valid dx_entry. */
1910 + if ((ld->ld_count != dx_get_count(entries)) ||
1911 + !ext4_htree_dx_lock_try(lck, at) ||
1912 + (ld->ld_count != dx_get_count(entries))) {
1916 + /* OK, I've got DX lock and nothing changed */
1917 + frame->at = ld->ld_at;
1919 - de = do_split(handle, dir, &bh, frame, &fname->hinfo);
1920 + de = do_split(handle, dir, &bh, frames, frame, &fname->hinfo, lck);
1924 @@ -2277,6 +2622,8 @@ again:
1926 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
1928 + ext4_htree_dx_unlock(lck);
1929 + ext4_htree_de_unlock(lck);
1932 /* @restart is true means htree-path has been changed, we need to
1933 Index: linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/super.c
1934 ===================================================================
1935 --- linux-3.10.0-229.1.2.fc21.x86_64.orig/fs/ext4/super.c
1936 +++ linux-3.10.0-229.1.2.fc21.x86_64/fs/ext4/super.c
1937 @@ -875,6 +875,7 @@ static struct inode *ext4_alloc_inode(st
1939 ei->vfs_inode.i_version = 1;
1940 spin_lock_init(&ei->i_raw_lock);
1941 + sema_init(&ei->i_append_sem, 1);
1942 INIT_LIST_HEAD(&ei->i_prealloc_list);
1943 spin_lock_init(&ei->i_prealloc_lock);
1944 ext4_es_init_tree(&ei->i_es_tree);